OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 * |
| 10 */ |
| 11 |
| 12 #include "webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.h" |
| 13 |
| 14 #include <algorithm> |
| 15 |
| 16 extern "C" { |
| 17 #include "third_party/ffmpeg/libavcodec/avcodec.h" |
| 18 #include "third_party/ffmpeg/libavformat/avformat.h" |
| 19 #include "third_party/ffmpeg/libavutil/imgutils.h" |
| 20 } // extern "C" |
| 21 |
| 22 #include "webrtc/base/checks.h" |
| 23 #include "webrtc/base/criticalsection.h" |
| 24 #include "webrtc/base/logging.h" |
| 25 |
| 26 namespace webrtc { |
| 27 |
| 28 namespace { |
| 29 |
| 30 static bool ffmpeg_initialized = false; |
| 31 static const AVPixelFormat pixel_format = AV_PIX_FMT_YUV420P; |
| 32 |
| 33 // Called by FFmpeg to do mutex operations if init using InitializeFFmpeg. |
| 34 static int LockManagerOperation(void** lock, AVLockOp op) |
| 35 EXCLUSIVE_LOCK_FUNCTION() UNLOCK_FUNCTION() { |
| 36 switch (op) { |
| 37 case AV_LOCK_CREATE: |
| 38 *lock = new rtc::CriticalSection(); |
| 39 return 0; |
| 40 case AV_LOCK_OBTAIN: |
| 41 static_cast<rtc::CriticalSection*>(*lock)->Enter(); |
| 42 return 0; |
| 43 case AV_LOCK_RELEASE: |
| 44 static_cast<rtc::CriticalSection*>(*lock)->Leave(); |
| 45 return 0; |
| 46 case AV_LOCK_DESTROY: |
| 47 delete static_cast<rtc::CriticalSection*>(*lock); |
| 48 *lock = nullptr; |
| 49 return 0; |
| 50 } |
| 51 return 1; |
| 52 } |
| 53 |
| 54 // TODO(hbos): Only init in webrtc standalone (chromium should init for us). |
| 55 // TODO(hbos): Non-racey init |
| 56 static bool InitializeFFmpeg() { |
| 57 if (!ffmpeg_initialized) { |
| 58 if (av_lockmgr_register(LockManagerOperation) < 0) { |
| 59 LOG(LS_ERROR) << "av_lockmgr_register failed."; |
| 60 return false; |
| 61 } |
| 62 av_register_all(); |
| 63 ffmpeg_initialized = true; |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 // Called by FFmpeg when it is done with a frame buffer, see AVGetBuffer2. |
| 69 static void AVFreeBuffer2(void* opaque, uint8_t* data) { |
| 70 VideoFrame* video_frame = static_cast<VideoFrame*>(opaque); |
| 71 delete video_frame; |
| 72 } |
| 73 |
| 74 // Called by FFmpeg when it needs a frame buffer to store decoded frames in. |
| 75 // The VideoFrames in returned by FFmpeg at Decode originate from here. They are |
| 76 // reference counted and freed by FFmpeg using AVFreeBuffer2. |
| 77 // TODO(hbos): Use a frame pool for better performance instead of create/free. |
| 78 // Could be owned by decoder, static_cast<H264DecoderImpl*>(context->opaque). |
| 79 static int AVGetBuffer2(AVCodecContext* context, AVFrame* frame, int flags) { |
| 80 RTC_CHECK_EQ(context->pix_fmt, pixel_format); // Same as in InitDecode. |
| 81 |
| 82 // width/height and coded_width/coded_height can be different due to cropping |
| 83 // or |lowres|. |
| 84 int width = std::max(context->width, context->coded_width); |
| 85 int height = std::max(context->height, context->coded_height); |
| 86 // See |lowres|, if used the decoder scales the image by 1/2^(lowres). This |
| 87 // has implications on which resolutions are valid, but we don't use it. |
| 88 RTC_CHECK_EQ(context->lowres, 0); |
| 89 |
| 90 RTC_CHECK_GE(width, 0); |
| 91 RTC_CHECK_GE(height, 0); |
| 92 int ret = av_image_check_size(width, height, 0, nullptr); |
| 93 if (ret < 0) { |
| 94 LOG(LS_ERROR) << "Invalid picture size " << width << "x" << height; |
| 95 return ret; |
| 96 } |
| 97 |
| 98 VideoFrame* video_frame = new VideoFrame(); |
| 99 int stride_y = width; |
| 100 int stride_u = (width + 1) / 2; |
| 101 int stride_v = (width + 1) / 2; |
| 102 RTC_CHECK_EQ(0, video_frame->CreateEmptyFrame(width, height, |
| 103 stride_y, stride_u, stride_v)); |
| 104 size_t total_size = video_frame->allocated_size(kYPlane) + |
| 105 video_frame->allocated_size(kUPlane) + |
| 106 video_frame->allocated_size(kVPlane); |
| 107 RTC_DCHECK_EQ(total_size, static_cast<size_t>(stride_y * height + |
| 108 (stride_u + stride_v) * ((height + 1) / 2))); |
| 109 // FFmpeg note: "Each data plane must be aligned to the maximum required by |
| 110 // the target CPU." See get_buffer2. |
| 111 // TODO(hbos): Memory alignment on a per-plane basis. CreateEmptyFrame only |
| 112 // guarantees that the buffer of all planes is memory aligned, not each |
| 113 // individual plane. |
| 114 |
| 115 // FFmpeg expects the initial allocation to be zero-initialized according to |
| 116 // http://crbug.com/390941. |
| 117 // Expect YUV to be a continuous blob of memory so that we can zero-initialize |
| 118 // with a single memset operation instead of three. |
| 119 RTC_DCHECK_EQ(video_frame->buffer(kUPlane), |
| 120 video_frame->buffer(kYPlane) + video_frame->allocated_size(kYPlane)); |
| 121 RTC_DCHECK_EQ(video_frame->buffer(kVPlane), |
| 122 video_frame->buffer(kUPlane) + video_frame->allocated_size(kUPlane)); |
| 123 memset(video_frame->buffer(kYPlane), 0, total_size); |
| 124 |
| 125 frame->width = width; |
| 126 frame->height = height; |
| 127 frame->format = context->pix_fmt; |
| 128 frame->reordered_opaque = context->reordered_opaque; |
| 129 |
| 130 frame->data[kYPlane] = video_frame->buffer(kYPlane); |
| 131 frame->linesize[kYPlane] = video_frame->stride(kYPlane); |
| 132 frame->data[kUPlane] = video_frame->buffer(kUPlane); |
| 133 frame->linesize[kUPlane] = video_frame->stride(kUPlane); |
| 134 frame->data[kVPlane] = video_frame->buffer(kVPlane); |
| 135 frame->linesize[kVPlane] = video_frame->stride(kVPlane); |
| 136 RTC_DCHECK_EQ(frame->extended_data, frame->data); |
| 137 |
| 138 frame->buf[0] = av_buffer_create(frame->data[0], |
| 139 total_size, |
| 140 AVFreeBuffer2, |
| 141 static_cast<void*>(video_frame), |
| 142 0); |
| 143 RTC_CHECK(frame->buf[0]); |
| 144 return 0; |
| 145 } |
| 146 |
| 147 } // namespace |
| 148 |
| 149 H264DecoderImpl::H264DecoderImpl() |
| 150 : decoded_image_callback_(nullptr) { |
| 151 } |
| 152 |
| 153 H264DecoderImpl::~H264DecoderImpl() { |
| 154 Release(); |
| 155 } |
| 156 |
| 157 int32_t H264DecoderImpl::InitDecode(const VideoCodec* codec_settings, |
| 158 int32_t /*number_of_cores*/) { |
| 159 if (codec_settings && |
| 160 codec_settings->codecType != kVideoCodecH264) { |
| 161 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 162 } |
| 163 |
| 164 // Make sure FFmpeg has been initialized. |
| 165 InitializeFFmpeg(); |
| 166 |
| 167 // Release necessary in case of re-initializing. |
| 168 int32_t ret = Release(); |
| 169 if (ret != WEBRTC_VIDEO_CODEC_OK) |
| 170 return ret; |
| 171 RTC_DCHECK(!av_context_); |
| 172 |
| 173 // Initialize AVCodecContext. |
| 174 av_context_.reset(avcodec_alloc_context3(nullptr)); |
| 175 |
| 176 av_context_->codec_type = AVMEDIA_TYPE_VIDEO; |
| 177 av_context_->codec_id = AV_CODEC_ID_H264; |
| 178 // This is meant to be able to decode OpenH264 streams, which should be |
| 179 // baseline profile. |
| 180 av_context_->profile = FF_PROFILE_H264_BASELINE; |
| 181 if (codec_settings) { |
| 182 av_context_->coded_width = codec_settings->width; |
| 183 av_context_->coded_height = codec_settings->height; |
| 184 } |
| 185 av_context_->pix_fmt = pixel_format; |
| 186 av_context_->extradata = nullptr; |
| 187 av_context_->extradata_size = 0; |
| 188 |
| 189 av_context_->thread_count = 4; |
| 190 av_context_->thread_type = FF_THREAD_SLICE; |
| 191 |
| 192 // FFmpeg will get video buffers from our AVGetBuffer2, memory managed by us. |
| 193 av_context_->get_buffer2 = AVGetBuffer2; |
| 194 // get_buffer2 is called with the context, there |opaque| can be used to get a |
| 195 // pointer |this|. |
| 196 av_context_->opaque = this; |
| 197 // Use ref counted frames (av_frame_unref). |
| 198 av_context_->refcounted_frames = 1; // "true" |
| 199 |
| 200 AVCodec* codec = avcodec_find_decoder(av_context_->codec_id); |
| 201 if (!codec) { |
| 202 // This is an indication that FFmpeg has not been initialized or it has not |
| 203 // been compiled/initialized with the correct set of codecs. |
| 204 LOG(LS_ERROR) << "FFmpeg H.264 decoder not found."; |
| 205 Release(); |
| 206 return WEBRTC_VIDEO_CODEC_ERROR; |
| 207 } |
| 208 int res = avcodec_open2(av_context_.get(), codec, nullptr); |
| 209 if (res < 0) { |
| 210 LOG(LS_ERROR) << "avcodec_open2 error: " << res; |
| 211 Release(); |
| 212 return WEBRTC_VIDEO_CODEC_ERROR; |
| 213 } |
| 214 |
| 215 av_frame_.reset(av_frame_alloc()); |
| 216 return WEBRTC_VIDEO_CODEC_OK; |
| 217 } |
| 218 |
| 219 int32_t H264DecoderImpl::Release() { |
| 220 av_context_.reset(); |
| 221 av_frame_.reset(); |
| 222 return WEBRTC_VIDEO_CODEC_OK; |
| 223 } |
| 224 |
| 225 int32_t H264DecoderImpl::Reset() { |
| 226 if (!IsInitialized()) |
| 227 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 228 InitDecode(nullptr, 1); |
| 229 return WEBRTC_VIDEO_CODEC_OK; |
| 230 } |
| 231 |
| 232 int32_t H264DecoderImpl::RegisterDecodeCompleteCallback( |
| 233 DecodedImageCallback* callback) { |
| 234 decoded_image_callback_ = callback; |
| 235 return WEBRTC_VIDEO_CODEC_OK; |
| 236 } |
| 237 |
| 238 int32_t H264DecoderImpl::Decode(const EncodedImage& input_image, |
| 239 bool /*missing_frames*/, |
| 240 const RTPFragmentationHeader* /*fragmentation*/, |
| 241 const CodecSpecificInfo* codec_specific_info, |
| 242 int64_t /*render_time_ms*/) { |
| 243 if (!IsInitialized()) |
| 244 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 245 if (!decoded_image_callback_) { |
| 246 LOG(LS_WARNING) << "InitDecode() has been called, but a callback function " |
| 247 "has not been set with RegisterDecodeCompleteCallback()"; |
| 248 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 249 } |
| 250 if (!input_image._buffer || !input_image._length) |
| 251 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 252 if (codec_specific_info && |
| 253 codec_specific_info->codecType != kVideoCodecH264) { |
| 254 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 255 } |
| 256 |
| 257 AVPacket packet; |
| 258 av_init_packet(&packet); |
| 259 // TODO(hbos): "The input buffer must be AV_INPUT_BUFFER_PADDING_SIZE larger |
| 260 // than the actual read bytes because some optimized bitstream readers read 32 |
| 261 // or 64 bits at once and could read over the end." See avcodec_decode_video2. |
| 262 // - Is this an issue? Do we have to make sure EncodedImage is allocated with |
| 263 // additional bytes or do we have to do an otherwise unnecessary copy? Might |
| 264 // only be a problem with non-mul-16 frame widths? |
| 265 // ("If the first 23 bits of the additional bytes are not 0, then damaged MPEG |
| 266 // bitstreams could cause overread and segfault.") |
| 267 packet.data = input_image._buffer; |
| 268 packet.size = input_image._length; |
| 269 av_context_->reordered_opaque = input_image.ntp_time_ms_ * 1000; // ms -> μs |
| 270 |
| 271 int frame_decoded = 0; |
| 272 int result = avcodec_decode_video2(av_context_.get(), |
| 273 av_frame_.get(), |
| 274 &frame_decoded, |
| 275 &packet); |
| 276 if (result < 0) { |
| 277 LOG(LS_ERROR) << "avcodec_decode_video2 error: " << result; |
| 278 return WEBRTC_VIDEO_CODEC_ERROR; |
| 279 } |
| 280 // |result| is number of bytes used, which should be all of them. |
| 281 if (result != packet.size) { |
| 282 LOG(LS_ERROR) << "avcodec_decode_video2 consumed " << result << " bytes " |
| 283 "when " << packet.size << " bytes were expected."; |
| 284 return WEBRTC_VIDEO_CODEC_ERROR; |
| 285 } |
| 286 |
| 287 if (!frame_decoded) { |
| 288 LOG(LS_WARNING) << "avcodec_decode_video2 successful but no frame was " |
| 289 "decoded."; |
| 290 return WEBRTC_VIDEO_CODEC_OK; |
| 291 } |
| 292 |
| 293 // Obtain the |video_frame| containing the decoded image. |
| 294 VideoFrame* video_frame = static_cast<VideoFrame*>( |
| 295 av_buffer_get_opaque(av_frame_->buf[0])); |
| 296 RTC_DCHECK(video_frame); |
| 297 RTC_CHECK_EQ(av_frame_->data[kYPlane], video_frame->buffer(kYPlane)); |
| 298 RTC_CHECK_EQ(av_frame_->data[kUPlane], video_frame->buffer(kUPlane)); |
| 299 RTC_CHECK_EQ(av_frame_->data[kVPlane], video_frame->buffer(kVPlane)); |
| 300 video_frame->set_timestamp(input_image._timeStamp); |
| 301 |
| 302 // Return decoded frame. |
| 303 int32_t ret = decoded_image_callback_->Decoded(*video_frame); |
| 304 // Stop referencing it, possibly freeing |video_frame|. |
| 305 av_frame_unref(av_frame_.get()); |
| 306 video_frame = nullptr; |
| 307 |
| 308 if (ret) { |
| 309 LOG(LS_WARNING) << "DecodedImageCallback::Decoded returned " << ret; |
| 310 return ret; |
| 311 } |
| 312 return WEBRTC_VIDEO_CODEC_OK; |
| 313 } |
| 314 |
| 315 bool H264DecoderImpl::IsInitialized() const { |
| 316 return av_context_; |
| 317 } |
| 318 |
| 319 } // namespace webrtc |
OLD | NEW |