Chromium Code Reviews| 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(AVCodecContext *av_codec_context) : | |
| 13 av_codec_context_(av_codec_context), | |
| 14 av_frame_(av_frame_alloc()), | |
| 15 next_presentation_time_(0) { | |
| 16 DCHECK(av_codec_context); | |
| 17 } | |
| 18 | |
| 19 FfmpegDecoderBase::~FfmpegDecoderBase() {} | |
| 20 | |
| 21 Result FfmpegDecoderBase::Init(const StreamType& stream_type) { | |
| 22 return Result::kOk; | |
|
johngro
2016/03/01 01:31:38
Since this class is already abstract, why provide
dalesat
2016/03/01 20:43:01
AVCodecContext is established in the constructor a
| |
| 23 } | |
| 24 | |
| 25 std::unique_ptr<StreamType> FfmpegDecoderBase::output_stream_type() { | |
| 26 return StreamTypeFromAVCodecContext(av_codec_context_.get()); | |
| 27 } | |
| 28 | |
| 29 void FfmpegDecoderBase::Flush() { | |
| 30 avcodec_flush_buffers(av_codec_context_.get()); | |
|
johngro
2016/03/01 01:31:38
avcodec_flush_buffers is not safe against being ca
dalesat
2016/03/01 20:43:01
Done.
| |
| 31 } | |
| 32 | |
| 33 bool FfmpegDecoderBase::TransformPacket( | |
| 34 const PacketPtr& input, | |
| 35 bool new_input, | |
| 36 PayloadAllocator* allocator, | |
| 37 PacketPtr* output) { | |
| 38 DCHECK(input); | |
| 39 DCHECK(allocator); | |
| 40 DCHECK(output); | |
| 41 | |
| 42 *output = nullptr; | |
| 43 | |
| 44 if (new_input) { | |
| 45 PrepareInputPacket(input); | |
| 46 } | |
| 47 | |
| 48 bool frame_decoded = false; | |
| 49 int input_bytes_used = Decode(allocator, &frame_decoded); | |
|
johngro
2016/03/01 01:31:38
ssize_t instead of int?
dalesat
2016/03/01 20:43:01
Decode wraps one of two avcodec_decode_* functions
| |
| 50 if (input_bytes_used < 0) { | |
| 51 // Decode failed. | |
| 52 return UnprepareInputPacket(input, output); | |
| 53 } | |
| 54 | |
| 55 if (frame_decoded) { | |
| 56 *output = PacketFromAvFrame(allocator); | |
| 57 } | |
| 58 | |
| 59 av_packet_.size -= input_bytes_used; | |
| 60 av_packet_.data += input_bytes_used; | |
|
johngro
2016/03/01 01:31:38
DCHECK(av_packet_.size >= input_bytes_used) and ru
dalesat
2016/03/01 20:43:01
Added a CHECK
| |
| 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 = Packet::CreateEndOfStream(next_presentation_time_); | |
|
johngro
2016/03/01 01:31:38
what is the purpose of putting a PTS on an empty E
dalesat
2016/03/01 20:43:01
Design discussion.
| |
| 88 } | |
| 89 | |
| 90 av_packet_.size = 0; | |
| 91 av_packet_.data = nullptr; | |
| 92 | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 PacketPtr FfmpegDecoderBase::PacketFromAvFrame(PayloadAllocator* allocator) { | |
| 97 DCHECK(allocator); | |
| 98 | |
| 99 PacketPtr packet = CreateOutputPacket(allocator); | |
| 100 av_frame_unref(av_frame_.get()); | |
| 101 next_presentation_time_ = packet->presentation_time() + packet->duration(); | |
|
johngro
2016/03/01 01:31:38
either presentation_time or duration may be undefi
dalesat
2016/03/01 20:43:01
I've moved the next_presentation_time_ into the su
| |
| 102 return packet; | |
| 103 } | |
| 104 | |
| 105 } // namespace media | |
| 106 } // namespace mojo | |
| OLD | NEW |