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

Side by Side Diff: webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc

Issue 1645543003: H264: Improve FFmpeg decoder performance by using I420BufferPool. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Addressed comments 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 * 9 *
10 */ 10 */
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 RTC_NOTREACHED() << "av_lockmgr_register failed."; 70 RTC_NOTREACHED() << "av_lockmgr_register failed.";
71 return; 71 return;
72 } 72 }
73 av_register_all(); 73 av_register_all();
74 ffmpeg_initialized = true; 74 ffmpeg_initialized = true;
75 } 75 }
76 } 76 }
77 77
78 #endif // !defined(WEBRTC_CHROMIUM_BUILD) 78 #endif // !defined(WEBRTC_CHROMIUM_BUILD)
79 79
80 // Called by FFmpeg when it is done with a frame buffer, see AVGetBuffer2. 80 } // namespace
81 void AVFreeBuffer2(void* opaque, uint8_t* data) {
82 VideoFrame* video_frame = static_cast<VideoFrame*>(opaque);
83 delete video_frame;
84 }
85 81
86 // Called by FFmpeg when it needs a frame buffer to store decoded frames in. 82 int H264DecoderImpl::AVGetBuffer2(
87 // The VideoFrames returned by FFmpeg at |Decode| originate from here. They are 83 AVCodecContext* context, AVFrame* av_frame, int flags) {
88 // reference counted and freed by FFmpeg using |AVFreeBuffer2|. 84 // Set in |InitDecode|.
89 // TODO(hbos): Use a frame pool for better performance instead of create/free. 85 H264DecoderImpl* decoder = static_cast<H264DecoderImpl*>(context->opaque);
90 // Could be owned by decoder, |static_cast<H264DecoderImpl*>(context->opaque)|. 86 // DCHECK values set in |InitDecode|.
91 // Consider verifying that the buffer was allocated by us to avoid unsafe type 87 RTC_DCHECK(decoder);
92 // cast. See https://bugs.chromium.org/p/webrtc/issues/detail?id=5428. 88 RTC_DCHECK_EQ(context->pix_fmt, kPixelFormat);
93 int AVGetBuffer2(AVCodecContext* context, AVFrame* av_frame, int flags) {
94 RTC_CHECK_EQ(context->pix_fmt, kPixelFormat); // Same as in InitDecode.
95 // Necessary capability to be allowed to provide our own buffers. 89 // Necessary capability to be allowed to provide our own buffers.
96 RTC_CHECK(context->codec->capabilities | AV_CODEC_CAP_DR1); 90 RTC_DCHECK(context->codec->capabilities | AV_CODEC_CAP_DR1);
91
97 // |av_frame->width| and |av_frame->height| are set by FFmpeg. These are the 92 // |av_frame->width| and |av_frame->height| are set by FFmpeg. These are the
98 // actual image's dimensions and may be different from |context->width| and 93 // actual image's dimensions and may be different from |context->width| and
99 // |context->coded_width| due to reordering. 94 // |context->coded_width| due to reordering.
100 int width = av_frame->width; 95 int width = av_frame->width;
101 int height = av_frame->height; 96 int height = av_frame->height;
102 // See |lowres|, if used the decoder scales the image by 1/2^(lowres). This 97 // See |lowres|, if used the decoder scales the image by 1/2^(lowres). This
103 // has implications on which resolutions are valid, but we don't use it. 98 // has implications on which resolutions are valid, but we don't use it.
104 RTC_CHECK_EQ(context->lowres, 0); 99 RTC_CHECK_EQ(context->lowres, 0);
105 // Adjust the |width| and |height| to values acceptable by the decoder. 100 // Adjust the |width| and |height| to values acceptable by the decoder.
106 // Without this, FFmpeg may overflow the buffer. If modified, |width| and/or 101 // Without this, FFmpeg may overflow the buffer. If modified, |width| and/or
107 // |height| are larger than the actual image and the image has to be cropped 102 // |height| are larger than the actual image and the image has to be cropped
108 // (top-left corner) after decoding to avoid visible borders to the right and 103 // (top-left corner) after decoding to avoid visible borders to the right and
109 // bottom of the actual image. 104 // bottom of the actual image.
110 avcodec_align_dimensions(context, &width, &height); 105 avcodec_align_dimensions(context, &width, &height);
111 106
112 RTC_CHECK_GE(width, 0); 107 RTC_CHECK_GE(width, 0);
113 RTC_CHECK_GE(height, 0); 108 RTC_CHECK_GE(height, 0);
114 int ret = av_image_check_size(static_cast<unsigned int>(width), 109 int ret = av_image_check_size(static_cast<unsigned int>(width),
115 static_cast<unsigned int>(height), 0, nullptr); 110 static_cast<unsigned int>(height), 0, nullptr);
116 if (ret < 0) { 111 if (ret < 0) {
117 LOG(LS_ERROR) << "Invalid picture size " << width << "x" << height; 112 LOG(LS_ERROR) << "Invalid picture size " << width << "x" << height;
118 return ret; 113 return ret;
119 } 114 }
120 115
121 // The video frame is stored in |video_frame|. |av_frame| is FFmpeg's version 116 // The video frame is stored in |video_frame|. |av_frame| is FFmpeg's version
122 // of a video frame and will be set up to reference |video_frame|'s buffers. 117 // of a video frame and will be set up to reference |video_frame|'s buffers.
123 VideoFrame* video_frame = new VideoFrame(); 118 VideoFrame* video_frame = new VideoFrame();
124 int stride_y = width;
125 int stride_uv = (width + 1) / 2;
126 RTC_CHECK_EQ(0, video_frame->CreateEmptyFrame(
127 width, height, stride_y, stride_uv, stride_uv));
128 int total_size = video_frame->allocated_size(kYPlane) +
129 video_frame->allocated_size(kUPlane) +
130 video_frame->allocated_size(kVPlane);
131 RTC_DCHECK_EQ(total_size, stride_y * height +
132 (stride_uv + stride_uv) * ((height + 1) / 2));
133
134 // FFmpeg expects the initial allocation to be zero-initialized according to 119 // FFmpeg expects the initial allocation to be zero-initialized according to
135 // http://crbug.com/390941. 120 // http://crbug.com/390941. Our pool is set up to zero-initialize new buffers.
136 // Using a single |av_frame->buf| - YUV is required to be a continuous blob of 121 video_frame->set_video_frame_buffer(
137 // memory. We can zero-initialize with one memset operation for all planes. 122 decoder->pool_.CreateBuffer(width, height));
123 // DCHECK that we have a continuous buffer as is required.
138 RTC_DCHECK_EQ(video_frame->buffer(kUPlane), 124 RTC_DCHECK_EQ(video_frame->buffer(kUPlane),
139 video_frame->buffer(kYPlane) + video_frame->allocated_size(kYPlane)); 125 video_frame->buffer(kYPlane) + video_frame->allocated_size(kYPlane));
140 RTC_DCHECK_EQ(video_frame->buffer(kVPlane), 126 RTC_DCHECK_EQ(video_frame->buffer(kVPlane),
141 video_frame->buffer(kUPlane) + video_frame->allocated_size(kUPlane)); 127 video_frame->buffer(kUPlane) + video_frame->allocated_size(kUPlane));
142 memset(video_frame->buffer(kYPlane), 0, total_size); 128 int total_size = video_frame->allocated_size(kYPlane) +
129 video_frame->allocated_size(kUPlane) +
130 video_frame->allocated_size(kVPlane);
143 131
144 av_frame->format = context->pix_fmt; 132 av_frame->format = context->pix_fmt;
145 av_frame->reordered_opaque = context->reordered_opaque; 133 av_frame->reordered_opaque = context->reordered_opaque;
146 134
147 // Set |av_frame| members as required by FFmpeg. 135 // Set |av_frame| members as required by FFmpeg.
148 av_frame->data[kYPlaneIndex] = video_frame->buffer(kYPlane); 136 av_frame->data[kYPlaneIndex] = video_frame->buffer(kYPlane);
149 av_frame->linesize[kYPlaneIndex] = video_frame->stride(kYPlane); 137 av_frame->linesize[kYPlaneIndex] = video_frame->stride(kYPlane);
150 av_frame->data[kUPlaneIndex] = video_frame->buffer(kUPlane); 138 av_frame->data[kUPlaneIndex] = video_frame->buffer(kUPlane);
151 av_frame->linesize[kUPlaneIndex] = video_frame->stride(kUPlane); 139 av_frame->linesize[kUPlaneIndex] = video_frame->stride(kUPlane);
152 av_frame->data[kVPlaneIndex] = video_frame->buffer(kVPlane); 140 av_frame->data[kVPlaneIndex] = video_frame->buffer(kVPlane);
153 av_frame->linesize[kVPlaneIndex] = video_frame->stride(kVPlane); 141 av_frame->linesize[kVPlaneIndex] = video_frame->stride(kVPlane);
154 RTC_DCHECK_EQ(av_frame->extended_data, av_frame->data); 142 RTC_DCHECK_EQ(av_frame->extended_data, av_frame->data);
155 143
156 av_frame->buf[0] = av_buffer_create(av_frame->data[kYPlaneIndex], 144 av_frame->buf[0] = av_buffer_create(av_frame->data[kYPlaneIndex],
157 total_size, 145 total_size,
158 AVFreeBuffer2, 146 AVFreeBuffer2,
159 static_cast<void*>(video_frame), 147 static_cast<void*>(video_frame),
160 0); 148 0);
161 RTC_CHECK(av_frame->buf[0]); 149 RTC_CHECK(av_frame->buf[0]);
162 return 0; 150 return 0;
163 } 151 }
164 152
165 } // namespace 153 void H264DecoderImpl::AVFreeBuffer2(void* opaque, uint8_t* data) {
154 // The buffer pool recycles the buffer used by |video_frame| when there are no
155 // more references to it. |video_frame| is a thin buffer holder and is not
156 // recycled.
157 VideoFrame* video_frame = static_cast<VideoFrame*>(opaque);
158 delete video_frame;
159 }
166 160
167 H264DecoderImpl::H264DecoderImpl() 161 H264DecoderImpl::H264DecoderImpl() : pool_(true),
168 : decoded_image_callback_(nullptr) { 162 decoded_image_callback_(nullptr) {
169 } 163 }
170 164
171 H264DecoderImpl::~H264DecoderImpl() { 165 H264DecoderImpl::~H264DecoderImpl() {
172 Release(); 166 Release();
173 } 167 }
174 168
175 int32_t H264DecoderImpl::InitDecode(const VideoCodec* codec_settings, 169 int32_t H264DecoderImpl::InitDecode(const VideoCodec* codec_settings,
176 int32_t number_of_cores) { 170 int32_t number_of_cores) {
177 if (codec_settings && 171 if (codec_settings &&
178 codec_settings->codecType != kVideoCodecH264) { 172 codec_settings->codecType != kVideoCodecH264) {
(...skipping 22 matching lines...) Expand all
201 av_context_->codec_type = AVMEDIA_TYPE_VIDEO; 195 av_context_->codec_type = AVMEDIA_TYPE_VIDEO;
202 av_context_->codec_id = AV_CODEC_ID_H264; 196 av_context_->codec_id = AV_CODEC_ID_H264;
203 if (codec_settings) { 197 if (codec_settings) {
204 av_context_->coded_width = codec_settings->width; 198 av_context_->coded_width = codec_settings->width;
205 av_context_->coded_height = codec_settings->height; 199 av_context_->coded_height = codec_settings->height;
206 } 200 }
207 av_context_->pix_fmt = kPixelFormat; 201 av_context_->pix_fmt = kPixelFormat;
208 av_context_->extradata = nullptr; 202 av_context_->extradata = nullptr;
209 av_context_->extradata_size = 0; 203 av_context_->extradata_size = 0;
210 204
205 // If this is ever increased, look at |av_context_->thread_safe_callbacks| and
206 // make it possible to disable the thread checker in the frame buffer pool.
211 av_context_->thread_count = 1; 207 av_context_->thread_count = 1;
212 av_context_->thread_type = FF_THREAD_SLICE; 208 av_context_->thread_type = FF_THREAD_SLICE;
213 209
214 // FFmpeg will get video buffers from our AVGetBuffer2, memory managed by us. 210 // Function used by FFmpeg to get buffers to store decoded frames in.
215 av_context_->get_buffer2 = AVGetBuffer2; 211 av_context_->get_buffer2 = AVGetBuffer2;
216 // get_buffer2 is called with the context, there |opaque| can be used to get a 212 // |get_buffer2| is called with the context, there |opaque| can be used to get
217 // pointer |this|. 213 // a pointer |this|.
218 av_context_->opaque = this; 214 av_context_->opaque = this;
219 // Use ref counted frames (av_frame_unref). 215 // Use ref counted frames (av_frame_unref).
220 av_context_->refcounted_frames = 1; // true 216 av_context_->refcounted_frames = 1; // true
221 217
222 AVCodec* codec = avcodec_find_decoder(av_context_->codec_id); 218 AVCodec* codec = avcodec_find_decoder(av_context_->codec_id);
223 if (!codec) { 219 if (!codec) {
224 // This is an indication that FFmpeg has not been initialized or it has not 220 // This is an indication that FFmpeg has not been initialized or it has not
225 // been compiled/initialized with the correct set of codecs. 221 // been compiled/initialized with the correct set of codecs.
226 LOG(LS_ERROR) << "FFmpeg H.264 decoder not found."; 222 LOG(LS_ERROR) << "FFmpeg H.264 decoder not found.";
227 Release(); 223 Release();
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 return ret; 349 return ret;
354 } 350 }
355 return WEBRTC_VIDEO_CODEC_OK; 351 return WEBRTC_VIDEO_CODEC_OK;
356 } 352 }
357 353
358 bool H264DecoderImpl::IsInitialized() const { 354 bool H264DecoderImpl::IsInitialized() const {
359 return av_context_ != nullptr; 355 return av_context_ != nullptr;
360 } 356 }
361 357
362 } // namespace webrtc 358 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698