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 #include "base/logging.h" |
| 6 #include "services/media/framework_ffmpeg/ffmpeg_decoder_base.h" |
| 7 #include "services/media/framework_ffmpeg/ffmpeg_type_converters.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace media { |
| 11 |
| 12 FfmpegDecoderBase::FfmpegDecoderBase(AvCodecContextPtr av_codec_context) : |
| 13 av_codec_context_(std::move(av_codec_context)), |
| 14 av_frame_(av_frame_alloc()) { |
| 15 DCHECK(av_codec_context); |
| 16 } |
| 17 |
| 18 FfmpegDecoderBase::~FfmpegDecoderBase() {} |
| 19 |
| 20 std::unique_ptr<StreamType> FfmpegDecoderBase::output_stream_type() { |
| 21 return StreamTypeFromAVCodecContext(*av_codec_context_); |
| 22 } |
| 23 |
| 24 void FfmpegDecoderBase::Flush() { |
| 25 DCHECK(av_codec_context_); |
| 26 avcodec_flush_buffers(av_codec_context_.get()); |
| 27 } |
| 28 |
| 29 bool FfmpegDecoderBase::TransformPacket( |
| 30 const PacketPtr& input, |
| 31 bool new_input, |
| 32 PayloadAllocator* allocator, |
| 33 PacketPtr* output) { |
| 34 DCHECK(input); |
| 35 DCHECK(allocator); |
| 36 DCHECK(output); |
| 37 |
| 38 *output = nullptr; |
| 39 |
| 40 if (new_input) { |
| 41 PrepareInputPacket(input); |
| 42 } |
| 43 |
| 44 bool frame_decoded = false; |
| 45 int input_bytes_used = Decode(allocator, &frame_decoded); |
| 46 if (input_bytes_used < 0) { |
| 47 // Decode failed. |
| 48 return UnprepareInputPacket(input, output); |
| 49 } |
| 50 |
| 51 if (frame_decoded) { |
| 52 DCHECK(allocator); |
| 53 *output = CreateOutputPacket(allocator); |
| 54 av_frame_unref(av_frame_.get()); |
| 55 } |
| 56 |
| 57 CHECK(input_bytes_used <= av_packet_.size) |
| 58 << "Ffmpeg decoder read beyond end of packet"; |
| 59 av_packet_.size -= input_bytes_used; |
| 60 av_packet_.data += input_bytes_used; |
| 61 |
| 62 if (av_packet_.size != 0 || (input->end_of_stream() && frame_decoded)) { |
| 63 // The input packet is only partially decoded, or it's an end-of-stream |
| 64 // packet and we're still draining. Let the caller know we want to see the |
| 65 // input packet again. |
| 66 return false; |
| 67 } |
| 68 |
| 69 // Used up the whole input packet, and, if we were draining, we're done with |
| 70 // that too. |
| 71 return UnprepareInputPacket(input, output); |
| 72 } |
| 73 |
| 74 void FfmpegDecoderBase::PrepareInputPacket(const PacketPtr& input) { |
| 75 av_init_packet(&av_packet_); |
| 76 av_packet_.data = reinterpret_cast<uint8_t*>(input->payload()); |
| 77 av_packet_.size = input->size(); |
| 78 } |
| 79 |
| 80 bool FfmpegDecoderBase::UnprepareInputPacket( |
| 81 const PacketPtr& input, |
| 82 PacketPtr* output) { |
| 83 if (input->end_of_stream()) { |
| 84 // Indicate end of stream. This happens when we're draining for the last |
| 85 // time, so there should be no output packet yet. |
| 86 DCHECK(*output == nullptr); |
| 87 *output = CreateOutputEndOfStreamPacket(); |
| 88 } |
| 89 |
| 90 av_packet_.size = 0; |
| 91 av_packet_.data = nullptr; |
| 92 |
| 93 return true; |
| 94 } |
| 95 |
| 96 } // namespace media |
| 97 } // namespace mojo |
OLD | NEW |