OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "services/media/framework_ffmpeg/ffmpeg_video_decoder.h" | 6 #include "services/media/framework_ffmpeg/ffmpeg_video_decoder.h" |
7 | 7 |
8 namespace mojo { | 8 namespace mojo { |
9 namespace media { | 9 namespace media { |
10 | 10 |
11 FfmpegVideoDecoder::FfmpegVideoDecoder(AvCodecContextPtr av_codec_context) : | 11 FfmpegVideoDecoder::FfmpegVideoDecoder(AvCodecContextPtr av_codec_context) |
12 FfmpegDecoderBase(std::move(av_codec_context)) { | 12 : FfmpegDecoderBase(std::move(av_codec_context)) { |
13 DCHECK(context()); | 13 DCHECK(context()); |
14 } | 14 } |
15 | 15 |
16 FfmpegVideoDecoder::~FfmpegVideoDecoder() {} | 16 FfmpegVideoDecoder::~FfmpegVideoDecoder() {} |
17 | 17 |
18 int FfmpegVideoDecoder::Decode( | 18 int FfmpegVideoDecoder::Decode(const AVPacket& av_packet, |
19 const AVPacket& av_packet, | 19 const ffmpeg::AvFramePtr& av_frame_ptr, |
20 const ffmpeg::AvFramePtr& av_frame_ptr, | 20 PayloadAllocator* allocator, |
21 PayloadAllocator* allocator, | 21 bool* frame_decoded_out) { |
22 bool* frame_decoded_out) { | |
23 DCHECK(av_frame_ptr); | 22 DCHECK(av_frame_ptr); |
24 DCHECK(allocator); | 23 DCHECK(allocator); |
25 DCHECK(frame_decoded_out); | 24 DCHECK(frame_decoded_out); |
26 DCHECK(context()); | 25 DCHECK(context()); |
27 | 26 |
28 int frame_decoded = 0; | 27 int frame_decoded = 0; |
29 int input_bytes_used = avcodec_decode_video2( | 28 int input_bytes_used = avcodec_decode_video2( |
30 context().get(), | 29 context().get(), av_frame_ptr.get(), &frame_decoded, &av_packet); |
31 av_frame_ptr.get(), | |
32 &frame_decoded, | |
33 &av_packet); | |
34 *frame_decoded_out = frame_decoded != 0; | 30 *frame_decoded_out = frame_decoded != 0; |
35 return input_bytes_used; | 31 return input_bytes_used; |
36 } | 32 } |
37 | 33 |
38 | 34 PacketPtr FfmpegVideoDecoder::CreateOutputPacket(const AVFrame& av_frame, |
39 PacketPtr FfmpegVideoDecoder::CreateOutputPacket( | 35 PayloadAllocator* allocator) { |
40 const AVFrame& av_frame, | |
41 PayloadAllocator* allocator) { | |
42 DCHECK(allocator); | 36 DCHECK(allocator); |
43 | 37 |
44 // End of stream is indicated when we're draining and produce no packet. | 38 // End of stream is indicated when we're draining and produce no packet. |
45 // TODO(dalesat): This is just a copy of the audio version. | 39 // TODO(dalesat): This is just a copy of the audio version. |
46 return Packet::Create( | 40 return Packet::Create(av_frame.pts, false, packet_size_, av_frame.data[0], |
47 av_frame.pts, | 41 allocator); |
48 false, | |
49 packet_size_, | |
50 av_frame.data[0], | |
51 allocator); | |
52 } | 42 } |
53 | 43 |
54 PacketPtr FfmpegVideoDecoder::CreateOutputEndOfStreamPacket() { | 44 PacketPtr FfmpegVideoDecoder::CreateOutputEndOfStreamPacket() { |
55 // TODO(dalesat): Presentation time for this packet. | 45 // TODO(dalesat): Presentation time for this packet. |
56 return Packet::CreateEndOfStream(0); | 46 return Packet::CreateEndOfStream(0); |
57 } | 47 } |
58 | 48 |
59 int FfmpegVideoDecoder::AllocateBufferForAvFrame( | 49 int FfmpegVideoDecoder::AllocateBufferForAvFrame( |
60 AVCodecContext* av_codec_context, | 50 AVCodecContext* av_codec_context, |
61 AVFrame* av_frame, | 51 AVFrame* av_frame, |
62 int flags) { | 52 int flags) { |
63 // It's important to use av_codec_context here rather than context(), | 53 // It's important to use av_codec_context here rather than context(), |
64 // because av_codec_context is different for different threads when we're | 54 // because av_codec_context is different for different threads when we're |
65 // decoding on multiple threads. If this code is moved to an instance method, | 55 // decoding on multiple threads. If this code is moved to an instance method, |
66 // be sure to avoid using context(). | 56 // be sure to avoid using context(). |
67 | 57 |
68 // TODO(dalesat): Not sure why/if this is needed. | 58 // TODO(dalesat): Not sure why/if this is needed. |
69 //int result = av_image_check_size( | 59 // int result = av_image_check_size( |
70 // av_codec_context->width, | 60 // av_codec_context->width, |
71 // av_codec_context->height, | 61 // av_codec_context->height, |
72 // 0, | 62 // 0, |
73 // NULL); | 63 // NULL); |
74 //if (result < 0) { | 64 // if (result < 0) { |
75 // DCHECK(false) << "av_image_check_size failed"; | 65 // DCHECK(false) << "av_image_check_size failed"; |
76 // return result; | 66 // return result; |
77 //} | 67 //} |
78 | 68 |
79 // TODO(dalesat): Not sure why this is needed. | 69 // TODO(dalesat): Not sure why this is needed. |
80 int coded_width = | 70 int coded_width = |
81 std::max(av_codec_context->width, av_codec_context->coded_width); | 71 std::max(av_codec_context->width, av_codec_context->coded_width); |
82 int coded_height = | 72 int coded_height = |
83 std::max(av_codec_context->height, av_codec_context->coded_height); | 73 std::max(av_codec_context->height, av_codec_context->coded_height); |
84 DCHECK_EQ(coded_width, av_codec_context->coded_width) << | 74 DCHECK_EQ(coded_width, av_codec_context->coded_width) |
85 "coded width is less than width"; | 75 << "coded width is less than width"; |
86 DCHECK_EQ(coded_height, av_codec_context->coded_height) << | 76 DCHECK_EQ(coded_height, av_codec_context->coded_height) |
87 "coded height is less than height"; | 77 << "coded height is less than height"; |
88 | 78 |
89 // TODO(dalesat): Fill in av_frame->data and av_frame->data for each plane. | 79 // TODO(dalesat): Fill in av_frame->data and av_frame->data for each plane. |
90 | 80 |
91 av_frame->width = coded_width; | 81 av_frame->width = coded_width; |
92 av_frame->height = coded_height; | 82 av_frame->height = coded_height; |
93 av_frame->format = av_codec_context->pix_fmt; | 83 av_frame->format = av_codec_context->pix_fmt; |
94 av_frame->reordered_opaque = av_codec_context->reordered_opaque; | 84 av_frame->reordered_opaque = av_codec_context->reordered_opaque; |
95 | 85 |
96 av_frame->buf[0] = av_buffer_create( | 86 av_frame->buf[0] = av_buffer_create( |
97 av_frame->data[0], // Because this is the first chunk in the buffer. | 87 av_frame->data[0], // Because this is the first chunk in the buffer. |
98 0, // TODO(dalesat): Provide this. | 88 0, // TODO(dalesat): Provide this. |
99 ReleaseBufferForAvFrame, | 89 ReleaseBufferForAvFrame, |
100 nullptr, // opaque | 90 nullptr, // opaque |
101 0); // flags | 91 0); // flags |
102 | 92 |
103 return 0; | 93 return 0; |
104 } | 94 } |
105 | 95 |
106 void FfmpegVideoDecoder::ReleaseBufferForAvFrame( | 96 void FfmpegVideoDecoder::ReleaseBufferForAvFrame(void* opaque, |
107 void* opaque, uint8_t* buffer) { | 97 uint8_t* buffer) { |
108 // Nothing to do. | 98 // Nothing to do. |
109 // TODO(dalesat): Can we get rid of this method altogether? | 99 // TODO(dalesat): Can we get rid of this method altogether? |
110 } | 100 } |
111 | 101 |
112 } // namespace media | 102 } // namespace media |
113 } // namespace mojo | 103 } // namespace mojo |
OLD | NEW |