Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(801)

Unified Diff: services/media/framework_ffmpeg/ffmpeg_audio_decoder.h

Issue 1686363002: Motown: ffmpeg implementations of framework 'parts' (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Changed the way AVBuffer allocation/deallocation is done in the ffmpeg audio decoder. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/media/framework_ffmpeg/ffmpeg_audio_decoder.h
diff --git a/services/media/framework_ffmpeg/ffmpeg_audio_decoder.h b/services/media/framework_ffmpeg/ffmpeg_audio_decoder.h
new file mode 100644
index 0000000000000000000000000000000000000000..e537b34fe253672e11586087edcd202de977d194
--- /dev/null
+++ b/services/media/framework_ffmpeg/ffmpeg_audio_decoder.h
@@ -0,0 +1,113 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SERVICES_MEDIA_FRAMEWORK_FFMPEG_FFMPEG_AUDIO_DECODER_H_
+#define SERVICES_MEDIA_FRAMEWORK_FFMPEG_FFMPEG_AUDIO_DECODER_H_
+
+#include "services/media/framework/lpcm_util.h"
+#include "services/media/framework_ffmpeg/ffmpeg_decoder_base.h"
+
+namespace mojo {
+namespace media {
+
+// Decoder implementation employing an ffmpeg audio decoder.
+class FfmpegAudioDecoder : public FfmpegDecoderBase {
+ public:
+ FfmpegAudioDecoder(AvCodecContextPtr av_codec_context);
+
+ ~FfmpegAudioDecoder() override;
+
+ protected:
+ // FfmpegDecoderBase overrides.
+ int Decode(PayloadAllocator* allocator, bool* frame_decoded_out) override;
+
+ PacketPtr CreateOutputPacket(PayloadAllocator* allocator) override;
+
+ PacketPtr CreateOutputEndOfStreamPacket() override;
+
+ private:
+ // Used to control deallocation of buffers.
+ class AvBufferContext {
+ public:
+ AvBufferContext(size_t size, PayloadAllocator* allocator) :
+ size_(size),
+ allocator_(allocator) {
+ DCHECK(allocator_);
+ if (size_ == 0) {
+ buffer_ = nullptr;
+ } else {
+ buffer_ = static_cast<uint8_t*>(
+ allocator_->AllocatePayloadBuffer(size_));
+ }
+ }
+
+ ~AvBufferContext() {
+ if (allocator_ == nullptr) {
+ // Previously released.
+ return;
+ }
+
+ if (size_ != 0) {
+ DCHECK(buffer_ != nullptr);
+ allocator_->ReleasePayloadBuffer(size_, buffer_);
+ return;
+ }
+
+ DCHECK(buffer_ == nullptr);
+ }
+
+ uint8_t* buffer() { return buffer_; }
+
+ size_t size() { return size_; }
+
+ // Releases ownership of the buffer.
+ uint8_t* Release() {
+ DCHECK(allocator_) << "AvBufferContext released twice";
+ uint8_t* result = buffer_;
+ buffer_ = nullptr;
+ size_ = 0;
+ allocator_ = nullptr;
+ return result;
+ }
+
+ private:
+ uint8_t* buffer_;
+ size_t size_;
+ PayloadAllocator* allocator_;
+ };
+
+ // Align sample buffers on 32-byte boundaries. This is the value that Chromium
+ // uses and is supposed to work for all processor architectures. Strangely, if
+ // we were to tell ffmpeg to use the default (by passing 0), it aligns on 32
+ // sample (not byte) boundaries.
+ static const int kChannelAlign = 32;
+
+ // Callback used by the ffmpeg decoder to acquire a buffer.
+ static int AllocateBufferForAvFrame(
+ AVCodecContext* av_codec_context,
+ AVFrame* av_frame,
+ int flags);
+
+ // Callback used by the ffmpeg decoder to release a buffer.
+ static void ReleaseBufferForAvFrame(void* opaque, uint8_t* buffer);
+
+ // The allocator used by avcodec_decode_audio4 to provide context for
+ // AllocateBufferForAvFrame. This is set only during the call to
+ // avcodec_decode_audio4.
+ PayloadAllocator* allocator_;
+
+ // For interleaving, if needed.
+ std::unique_ptr<LpcmUtil> lpcm_util_;
+
+ // For interleaving, if needed.
+ std::unique_ptr<StreamType> stream_type_;
+
+ // Used to supply missing PTS.
+ int64_t next_presentation_time_= 0;
+};
+
+} // namespace media
+} // namespace mojo
+
+#endif // SERVICES_MEDIA_FRAMEWORK_FFMPEG_FFMPEG_AUDIO_DECODER_H_

Powered by Google App Engine
This is Rietveld 408576698