| 6 | namespace tgcalls { |
| 7 |
| 8 | WrappedCodecParameters::WrappedCodecParameters(AVCodecParameters const *codecParameters) { |
| 9 | _value = avcodec_parameters_alloc(); |
| 10 | avcodec_parameters_copy(_value, codecParameters); |
| 11 | } |
| 12 |
| 13 | WrappedCodecParameters::~WrappedCodecParameters() { |
| 14 | avcodec_parameters_free(&_value); |
| 15 | } |
| 16 |
| 17 | bool WrappedCodecParameters::isEqual(AVCodecParameters const *other) { |
| 18 | if (_value->codec_id != other->codec_id) { |
| 19 | return false; |
| 20 | } |
| 21 | if (_value->format != other->format) { |
| 22 | return false; |
| 23 | } |
| 24 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 25 | if (_value->ch_layout.nb_channels != other->ch_layout.nb_channels) { |
| 26 | #else |
| 27 | if (_value->channels != other->channels) { |
| 28 | #endif |
| 29 | return false; |
| 30 | } |
| 31 |
| 32 | return true; |
| 33 | } |
| 34 |
| 35 | class AudioStreamingPartPersistentDecoderState { |
| 36 | public: |
| 37 | AudioStreamingPartPersistentDecoderState(AVCodecParameters const *codecParameters, AVRational timeBase) : |
| 38 | _codecParameters(codecParameters), |
| 39 | _timeBase(timeBase) { |
| 40 | const AVCodec *codec = avcodec_find_decoder(codecParameters->codec_id); |
| 41 | if (codec) { |
| 42 | _codecContext = avcodec_alloc_context3(codec); |
| 43 | int ret = avcodec_parameters_to_context(_codecContext, codecParameters); |
| 44 | if (ret < 0) { |
| 45 | avcodec_free_context(&_codecContext); |
| 46 | _codecContext = nullptr; |
| 47 | } else { |
| 48 | _codecContext->pkt_timebase = timeBase; |
| 49 |
| 50 | #if LIBAVFORMAT_VERSION_MAJOR >= 59 |
| 51 | _channelCount = _codecContext->ch_layout.nb_channels; |
| 52 | #else |
| 53 | _channelCount = _codecContext->channels; |
| 54 | #endif |
| 55 |
| 56 | ret = avcodec_open2(_codecContext, codec, nullptr); |
| 57 | if (ret < 0) { |
| 58 | avcodec_free_context(&_codecContext); |
| 59 | _codecContext = nullptr; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 |
| 65 | ~AudioStreamingPartPersistentDecoderState() { |
| 66 | if (_codecContext) { |
| 67 | avcodec_free_context(&_codecContext); |
| 68 | } |
| 69 | } |
| 70 |
| 71 | int decode(AVPacket &packet, AVFrame *frame) { |
| 72 | int ret = avcodec_send_packet(_codecContext, &packet); |
| 73 | if (ret < 0) { |
| 74 | return ret; |
| 75 | } |
| 76 |
| 77 | int bytesPerSample = av_get_bytes_per_sample(_codecContext->sample_fmt); |
| 78 | if (bytesPerSample != 2 && bytesPerSample != 4) { |
| 79 | return -1; |
| 80 | } |
| 81 |
| 82 | ret = avcodec_receive_frame(_codecContext, frame); |
| 83 | return ret; |
| 84 | } |
| 85 |
| 86 | public: |
| 87 | WrappedCodecParameters _codecParameters; |
| 88 | AVRational _timeBase; |
| 89 | AVCodecContext *_codecContext = nullptr; |
| 90 | int _channelCount = 0; |
| 91 | }; |
| 92 |
| 93 | AudioStreamingPartPersistentDecoder::AudioStreamingPartPersistentDecoder() { |
| 94 | } |
| 95 |
| 96 | AudioStreamingPartPersistentDecoder::~AudioStreamingPartPersistentDecoder() { |
| 97 | if (_state) { |
| 98 | delete _state; |
| 99 | _state = nullptr; |
| 100 | } |
| 101 | } |
| 102 |
| 103 | void AudioStreamingPartPersistentDecoder::maybeReset(AVCodecParameters const *codecParameters, AVRational timeBase) { |
| 104 | if (_state) { |
| 105 | bool isUpdated = false; |
| 106 | if (!_state->_codecParameters.isEqual(codecParameters)) { |
| 107 | isUpdated = true; |
| 108 | } |
| 109 | if (_state->_timeBase.num != timeBase.num || _state->_timeBase.den != timeBase.den) { |
| 110 | isUpdated = true; |
| 111 | } |
| 112 | if (!isUpdated) { |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 |
| 117 | if (_state) { |
| 118 | delete _state; |
| 119 | _state = nullptr; |
| 120 | } |
| 121 |
| 122 | _state = new AudioStreamingPartPersistentDecoderState(codecParameters, timeBase); |
| 123 | } |
| 124 |
| 125 | int AudioStreamingPartPersistentDecoder::decode(AVCodecParameters const *codecParameters, AVRational timeBase, AVPacket &packet, AVFrame *frame) { |
| 126 | maybeReset(codecParameters, timeBase); |
| 127 |
| 128 | if (!_state) { |
| 129 | return -1; |
| 130 | } |
| 131 |
| 132 | return _state->decode(packet, frame); |
| 133 | } |
| 134 |
| 135 | } |
| 136 |