| 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_close(_codecContext); |
| 68 | avcodec_free_context(&_codecContext); |
| 69 | } |
| 70 | } |
| 71 |
| 72 | int decode(AVPacket &packet, AVFrame *frame) { |
| 73 | int ret = avcodec_send_packet(_codecContext, &packet); |
| 74 | if (ret < 0) { |
| 75 | return ret; |
| 76 | } |
| 77 |
| 78 | int bytesPerSample = av_get_bytes_per_sample(_codecContext->sample_fmt); |
| 79 | if (bytesPerSample != 2 && bytesPerSample != 4) { |
| 80 | return -1; |
| 81 | } |
| 82 |
| 83 | ret = avcodec_receive_frame(_codecContext, frame); |
| 84 | return ret; |
| 85 | } |
| 86 |
| 87 | public: |
| 88 | WrappedCodecParameters _codecParameters; |
| 89 | AVRational _timeBase; |
| 90 | AVCodecContext *_codecContext = nullptr; |
| 91 | int _channelCount = 0; |
| 92 | }; |
| 93 |
| 94 | AudioStreamingPartPersistentDecoder::AudioStreamingPartPersistentDecoder() { |
| 95 | } |
| 96 |
| 97 | AudioStreamingPartPersistentDecoder::~AudioStreamingPartPersistentDecoder() { |
| 98 | if (_state) { |
| 99 | delete _state; |
| 100 | _state = nullptr; |
| 101 | } |
| 102 | } |
| 103 |
| 104 | void AudioStreamingPartPersistentDecoder::maybeReset(AVCodecParameters const *codecParameters, AVRational timeBase) { |
| 105 | if (_state) { |
| 106 | bool isUpdated = false; |
| 107 | if (!_state->_codecParameters.isEqual(codecParameters)) { |
| 108 | isUpdated = true; |
| 109 | } |
| 110 | if (_state->_timeBase.num != timeBase.num || _state->_timeBase.den != timeBase.den) { |
| 111 | isUpdated = true; |
| 112 | } |
| 113 | if (!isUpdated) { |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 |
| 118 | if (_state) { |
| 119 | delete _state; |
| 120 | _state = nullptr; |
| 121 | } |
| 122 |
| 123 | _state = new AudioStreamingPartPersistentDecoderState(codecParameters, timeBase); |
| 124 | } |
| 125 |
| 126 | int AudioStreamingPartPersistentDecoder::decode(AVCodecParameters const *codecParameters, AVRational timeBase, AVPacket &packet, AVFrame *frame) { |
| 127 | maybeReset(codecParameters, timeBase); |
| 128 |
| 129 | if (!_state) { |
| 130 | return -1; |
| 131 | } |
| 132 |
| 133 | return _state->decode(packet, frame); |
| 134 | } |
| 135 |
| 136 | } |
| 137 |