1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
// Copyright (c) 2011 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.
#include "media/video/ffmpeg_video_decode_engine.h"
#include "base/command_line.h"
#include "base/string_number_conversions.h"
#include "base/task.h"
#include "media/base/buffers.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
#include "media/base/pipeline.h"
#include "media/base/video_util.h"
#include "media/ffmpeg/ffmpeg_common.h"
namespace media {
FFmpegVideoDecodeEngine::FFmpegVideoDecodeEngine()
: codec_context_(NULL),
event_handler_(NULL),
frame_rate_numerator_(0),
frame_rate_denominator_(0),
pending_input_buffers_(0),
pending_output_buffers_(0),
output_eos_reached_(false),
flush_pending_(false) {
}
FFmpegVideoDecodeEngine::~FFmpegVideoDecodeEngine() {
if (codec_context_) {
av_free(codec_context_->extradata);
avcodec_close(codec_context_);
av_free(codec_context_);
}
}
void FFmpegVideoDecodeEngine::Initialize(
VideoDecodeEngine::EventHandler* event_handler,
const VideoDecoderConfig& config) {
frame_rate_numerator_ = config.frame_rate_numerator();
frame_rate_denominator_ = config.frame_rate_denominator();
// Always try to use three threads for video decoding. There is little reason
// not to since current day CPUs tend to be multi-core and we measured
// performance benefits on older machines such as P4s with hyperthreading.
//
// Handling decoding on separate threads also frees up the pipeline thread to
// continue processing. Although it'd be nice to have the option of a single
// decoding thread, FFmpeg treats having one thread the same as having zero
// threads (i.e., avcodec_decode_video() will execute on the calling thread).
// Yet another reason for having two threads :)
static const int kDecodeThreads = 2;
static const int kMaxDecodeThreads = 16;
// Initialize AVCodecContext structure.
codec_context_ = avcodec_alloc_context();
VideoDecoderConfigToAVCodecContext(config, codec_context_);
// Enable motion vector search (potentially slow), strong deblocking filter
// for damaged macroblocks, and set our error detection sensitivity.
codec_context_->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
codec_context_->error_recognition = FF_ER_CAREFUL;
AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
// TODO(fbarchard): Improve thread logic based on size / codec.
// TODO(fbarchard): Fix bug affecting video-cookie.html
// 07/21/11(ihf): Still about 20 failures when enabling.
int decode_threads = (codec_context_->codec_id == CODEC_ID_THEORA) ?
1 : kDecodeThreads;
const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
std::string threads(cmd_line->GetSwitchValueASCII(switches::kVideoThreads));
if ((!threads.empty() &&
!base::StringToInt(threads, &decode_threads)) ||
decode_threads < 0 || decode_threads > kMaxDecodeThreads) {
decode_threads = kDecodeThreads;
}
codec_context_->thread_count = decode_threads;
// We don't allocate AVFrame on the stack since different versions of FFmpeg
// may change the size of AVFrame, causing stack corruption. The solution is
// to let FFmpeg allocate the structure via avcodec_alloc_frame().
av_frame_.reset(avcodec_alloc_frame());
// If we do not have enough buffers, we will report error too.
frame_queue_available_.clear();
// Convert the pixel format to video format and ensure we support it.
VideoFrame::Format format =
PixelFormatToVideoFormat(codec_context_->pix_fmt);
bool success = false;
if (format != VideoFrame::INVALID) {
// Create output buffer pool when direct rendering is not used.
for (size_t i = 0; i < Limits::kMaxVideoFrames; ++i) {
scoped_refptr<VideoFrame> video_frame =
VideoFrame::CreateFrame(format,
config.visible_rect().width(),
config.visible_rect().height(),
kNoTimestamp,
kNoTimestamp);
frame_queue_available_.push_back(video_frame);
}
// Open the codec!
success = codec && avcodec_open(codec_context_, codec) >= 0;
}
event_handler_ = event_handler;
event_handler_->OnInitializeComplete(success);
}
void FFmpegVideoDecodeEngine::ConsumeVideoSample(
scoped_refptr<Buffer> buffer) {
pending_input_buffers_--;
if (flush_pending_) {
TryToFinishPendingFlush();
} else {
// Otherwise try to decode this buffer.
DecodeFrame(buffer);
}
}
void FFmpegVideoDecodeEngine::ProduceVideoFrame(
scoped_refptr<VideoFrame> frame) {
// We should never receive NULL frame or EOS frame.
DCHECK(frame.get() && !frame->IsEndOfStream());
// Increment pending output buffer count.
pending_output_buffers_++;
// Return this frame to available pool after display.
frame_queue_available_.push_back(frame);
if (flush_pending_) {
TryToFinishPendingFlush();
} else if (!output_eos_reached_) {
// If we already deliver EOS to renderer, we stop reading new input.
ReadInput();
}
}
// Try to decode frame when both input and output are ready.
void FFmpegVideoDecodeEngine::DecodeFrame(scoped_refptr<Buffer> buffer) {
scoped_refptr<VideoFrame> video_frame;
// Create a packet for input data.
// Due to FFmpeg API changes we no longer have const read-only pointers.
AVPacket packet;
av_init_packet(&packet);
packet.data = const_cast<uint8*>(buffer->GetData());
packet.size = buffer->GetDataSize();
PipelineStatistics statistics;
statistics.video_bytes_decoded = buffer->GetDataSize();
// Let FFmpeg handle presentation timestamp reordering.
codec_context_->reordered_opaque = buffer->GetTimestamp().InMicroseconds();
// This is for codecs not using get_buffer to initialize
// |av_frame_->reordered_opaque|
av_frame_->reordered_opaque = codec_context_->reordered_opaque;
int frame_decoded = 0;
int result = avcodec_decode_video2(codec_context_,
av_frame_.get(),
&frame_decoded,
&packet);
// Log the problem if we can't decode a video frame and exit early.
if (result < 0) {
LOG(ERROR) << "Error decoding a video frame with timestamp: "
<< buffer->GetTimestamp().InMicroseconds() << " us, duration: "
<< buffer->GetDuration().InMicroseconds() << " us, packet size: "
<< buffer->GetDataSize() << " bytes";
event_handler_->OnError();
return;
}
// If frame_decoded == 0, then no frame was produced.
// In this case, if we already begin to flush codec with empty
// input packet at the end of input stream, the first time we
// encounter frame_decoded == 0 signal output frame had been
// drained, we mark the flag. Otherwise we read from demuxer again.
if (frame_decoded == 0) {
if (buffer->IsEndOfStream()) { // We had started flushing.
event_handler_->ConsumeVideoFrame(video_frame, statistics);
output_eos_reached_ = true;
} else {
ReadInput();
}
return;
}
// TODO(fbarchard): Work around for FFmpeg http://crbug.com/27675
// The decoder is in a bad state and not decoding correctly.
// Checking for NULL avoids a crash in CopyPlane().
if (!av_frame_->data[VideoFrame::kYPlane] ||
!av_frame_->data[VideoFrame::kUPlane] ||
!av_frame_->data[VideoFrame::kVPlane]) {
event_handler_->OnError();
return;
}
// Determine timestamp and calculate the duration based on the repeat picture
// count. According to FFmpeg docs, the total duration can be calculated as
// follows:
// fps = 1 / time_base
//
// duration = (1 / fps) + (repeat_pict) / (2 * fps)
// = (2 + repeat_pict) / (2 * fps)
// = (2 + repeat_pict) / (2 * (1 / time_base))
DCHECK_LE(av_frame_->repeat_pict, 2); // Sanity check.
AVRational doubled_time_base;
doubled_time_base.num = frame_rate_denominator_;
doubled_time_base.den = frame_rate_numerator_ * 2;
base::TimeDelta timestamp =
base::TimeDelta::FromMicroseconds(av_frame_->reordered_opaque);
base::TimeDelta duration =
ConvertFromTimeBase(doubled_time_base, 2 + av_frame_->repeat_pict);
// Available frame is guaranteed, because we issue as much reads as
// available frame, except the case of |frame_decoded| == 0, which
// implies decoder order delay, and force us to read more inputs.
DCHECK(frame_queue_available_.size());
video_frame = frame_queue_available_.front();
frame_queue_available_.pop_front();
// Copy the frame data since FFmpeg reuses internal buffers for AVFrame
// output, meaning the data is only valid until the next
// avcodec_decode_video() call.
//
// TODO(scherkus): use VideoFrame dimensions instead and re-allocate
// VideoFrame if dimensions changes, but for now adjust size locally.
int y_rows = codec_context_->height;
int uv_rows = codec_context_->height;
if (codec_context_->pix_fmt == PIX_FMT_YUV420P) {
uv_rows /= 2;
}
CopyYPlane(av_frame_->data[0], av_frame_->linesize[0], y_rows, video_frame);
CopyUPlane(av_frame_->data[1], av_frame_->linesize[1], uv_rows, video_frame);
CopyVPlane(av_frame_->data[2], av_frame_->linesize[2], uv_rows, video_frame);
video_frame->SetTimestamp(timestamp);
video_frame->SetDuration(duration);
pending_output_buffers_--;
event_handler_->ConsumeVideoFrame(video_frame, statistics);
}
void FFmpegVideoDecodeEngine::Uninitialize() {
event_handler_->OnUninitializeComplete();
}
void FFmpegVideoDecodeEngine::Flush() {
avcodec_flush_buffers(codec_context_);
flush_pending_ = true;
TryToFinishPendingFlush();
}
void FFmpegVideoDecodeEngine::TryToFinishPendingFlush() {
DCHECK(flush_pending_);
// We consider ourself flushed when there is no pending input buffers
// and output buffers, which implies that all buffers had been returned
// to its owner.
if (!pending_input_buffers_ && !pending_output_buffers_) {
// Try to finish flushing and notify pipeline.
flush_pending_ = false;
event_handler_->OnFlushComplete();
}
}
void FFmpegVideoDecodeEngine::Seek() {
// After a seek, output stream no longer considered as EOS.
output_eos_reached_ = false;
// The buffer provider is assumed to perform pre-roll operation.
for (unsigned int i = 0; i < Limits::kMaxVideoFrames; ++i)
ReadInput();
event_handler_->OnSeekComplete();
}
void FFmpegVideoDecodeEngine::ReadInput() {
DCHECK_EQ(output_eos_reached_, false);
pending_input_buffers_++;
event_handler_->ProduceVideoSample(NULL);
}
} // namespace media
// Disable refcounting for this object because this object only lives
// on the video decoder thread and there's no need to refcount it.
DISABLE_RUNNABLE_METHOD_REFCOUNT(media::FFmpegVideoDecodeEngine);
|