OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SERVICES_MEDIA_FRAMEWORK_FFMPEG_FFMPEG_AUDIO_DECODER_H_ |
| 6 #define SERVICES_MEDIA_FRAMEWORK_FFMPEG_FFMPEG_AUDIO_DECODER_H_ |
| 7 |
| 8 #include "services/media/framework/lpcm_util.h" |
| 9 #include "services/media/framework_ffmpeg/ffmpeg_decoder_base.h" |
| 10 |
| 11 namespace mojo { |
| 12 namespace media { |
| 13 |
| 14 // Decoder implementation employing an ffmpeg audio decoder. |
| 15 class FfmpegAudioDecoder : public FfmpegDecoderBase { |
| 16 public: |
| 17 FfmpegAudioDecoder(AvCodecContextPtr av_codec_context); |
| 18 |
| 19 ~FfmpegAudioDecoder() override; |
| 20 |
| 21 protected: |
| 22 // FfmpegDecoderBase overrides. |
| 23 int Decode(PayloadAllocator* allocator, bool* frame_decoded_out) override; |
| 24 |
| 25 PacketPtr CreateOutputPacket(PayloadAllocator* allocator) override; |
| 26 |
| 27 PacketPtr CreateOutputEndOfStreamPacket() override; |
| 28 |
| 29 private: |
| 30 // Used to control deallocation of buffers. |
| 31 class AvBufferContext { |
| 32 public: |
| 33 AvBufferContext(size_t size, PayloadAllocator* allocator) : |
| 34 size_(size), |
| 35 allocator_(allocator) { |
| 36 DCHECK(allocator_); |
| 37 if (size_ == 0) { |
| 38 buffer_ = nullptr; |
| 39 } else { |
| 40 buffer_ = static_cast<uint8_t*>( |
| 41 allocator_->AllocatePayloadBuffer(size_)); |
| 42 } |
| 43 } |
| 44 |
| 45 ~AvBufferContext() { |
| 46 if (allocator_ == nullptr) { |
| 47 // Previously released. |
| 48 return; |
| 49 } |
| 50 |
| 51 if (size_ != 0) { |
| 52 DCHECK(buffer_ != nullptr); |
| 53 allocator_->ReleasePayloadBuffer(size_, buffer_); |
| 54 return; |
| 55 } |
| 56 |
| 57 DCHECK(buffer_ == nullptr); |
| 58 } |
| 59 |
| 60 uint8_t* buffer() { return buffer_; } |
| 61 |
| 62 size_t size() { return size_; } |
| 63 |
| 64 // Releases ownership of the buffer. |
| 65 uint8_t* Release() { |
| 66 DCHECK(allocator_) << "AvBufferContext released twice"; |
| 67 uint8_t* result = buffer_; |
| 68 buffer_ = nullptr; |
| 69 size_ = 0; |
| 70 allocator_ = nullptr; |
| 71 return result; |
| 72 } |
| 73 |
| 74 private: |
| 75 uint8_t* buffer_; |
| 76 size_t size_; |
| 77 PayloadAllocator* allocator_; |
| 78 }; |
| 79 |
| 80 // Align sample buffers on 32-byte boundaries. This is the value that Chromium |
| 81 // uses and is supposed to work for all processor architectures. Strangely, if |
| 82 // we were to tell ffmpeg to use the default (by passing 0), it aligns on 32 |
| 83 // sample (not byte) boundaries. |
| 84 static const int kChannelAlign = 32; |
| 85 |
| 86 // Callback used by the ffmpeg decoder to acquire a buffer. |
| 87 static int AllocateBufferForAvFrame( |
| 88 AVCodecContext* av_codec_context, |
| 89 AVFrame* av_frame, |
| 90 int flags); |
| 91 |
| 92 // Callback used by the ffmpeg decoder to release a buffer. |
| 93 static void ReleaseBufferForAvFrame(void* opaque, uint8_t* buffer); |
| 94 |
| 95 // The allocator used by avcodec_decode_audio4 to provide context for |
| 96 // AllocateBufferForAvFrame. This is set only during the call to |
| 97 // avcodec_decode_audio4. |
| 98 PayloadAllocator* allocator_; |
| 99 |
| 100 // For interleaving, if needed. |
| 101 std::unique_ptr<LpcmUtil> lpcm_util_; |
| 102 |
| 103 // For interleaving, if needed. |
| 104 std::unique_ptr<StreamType> stream_type_; |
| 105 |
| 106 // Used to supply missing PTS. |
| 107 int64_t next_presentation_time_= 0; |
| 108 }; |
| 109 |
| 110 } // namespace media |
| 111 } // namespace mojo |
| 112 |
| 113 #endif // SERVICES_MEDIA_FRAMEWORK_FFMPEG_FFMPEG_AUDIO_DECODER_H_ |
OLD | NEW |