Index: webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc |
diff --git a/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc b/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a337ba8740bf728d0f6d5100b6dcea44c91bd04f |
--- /dev/null |
+++ b/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc |
@@ -0,0 +1,185 @@ |
+/* |
+ * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ * |
+ */ |
+ |
+#include "webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.h" |
+ |
+#include <bitset> |
+ |
+// OpenH264 |
+#include "codec_api.h" |
+#include "codec_app_def.h" |
+#include "codec_def.h" |
+ |
+#include "webrtc/base/checks.h" |
+#include "webrtc/base/logging.h" |
+#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
+ |
+namespace webrtc { |
+ |
+namespace { |
+const bool kOpenH264DecoderDetailedLogging = false; |
+} // namespace |
+ |
+H264DecoderImpl::H264DecoderImpl() |
+ : openh264_decoder_(nullptr), |
+ decoded_image_(), |
+ decoded_image_callback_(nullptr) { |
+} |
+ |
+H264DecoderImpl::~H264DecoderImpl() { |
+ Release(); |
+} |
+ |
+int32_t H264DecoderImpl::InitDecode(const VideoCodec* codec_settings, |
+ int32_t /*number_of_cores*/) { |
+ if (codec_settings && |
+ codec_settings->codecType != VideoCodecType::kVideoCodecH264) { |
+ return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
+ } |
+ |
+ int release_ret = Release(); |
+ if (release_ret != WEBRTC_VIDEO_CODEC_OK) |
+ return release_ret; |
+ RTC_DCHECK(!openh264_decoder_); |
+ |
+ // Create decoder. |
+ if (WelsCreateDecoder(&openh264_decoder_) != 0) { |
+ LOG(LS_ERROR) << "Failed to create OpenH264 decoder"; |
+ RTC_DCHECK(!openh264_decoder_); |
+ return WEBRTC_VIDEO_CODEC_ERROR; |
+ } |
+ RTC_DCHECK(openh264_decoder_); |
+ if (kOpenH264DecoderDetailedLogging) { |
+ int trace_level = WELS_LOG_DETAIL; |
+ openh264_decoder_->SetOption(DECODER_OPTION_TRACE_LEVEL, |
+ &trace_level); |
+ } |
+ // else WELS_LOG_DEFAULT is used by default. |
+ |
+ // Initialization parameters. |
+ SDecodingParam init_params; |
+ memset(&init_params, 0, sizeof(SDecodingParam)); |
+ init_params.eOutputColorFormat = EVideoFormatType::videoFormatI420; |
+ init_params.uiCpuLoad = 0; |
+ init_params.uiTargetDqLayer = 0xFF; |
+ init_params.eEcActiveIdc = ERROR_CON_IDC::ERROR_CON_DISABLE; |
+ init_params.bParseOnly = false; |
+ init_params.sVideoProperty.eVideoBsType = |
+ VIDEO_BITSTREAM_TYPE::VIDEO_BITSTREAM_DEFAULT; |
+ |
+ // Initialize. |
+ if (openh264_decoder_->Initialize(&init_params) != 0) { |
+ LOG(LS_ERROR) << "Failed to initialize OpenH264 decoder"; |
+ Release(); |
+ return WEBRTC_VIDEO_CODEC_ERROR; |
+ } |
+ |
+ return WEBRTC_VIDEO_CODEC_OK; |
+} |
+ |
+int32_t H264DecoderImpl::Release() { |
+ if (openh264_decoder_) { |
+ int64_t uninit_ret = openh264_decoder_->Uninitialize(); |
+ if (uninit_ret != 0) { |
+ LOG(LS_WARNING) << "OpenH264 decoder's Uninitialize() returned " |
+ << "unsuccessful: " << uninit_ret; |
+ } |
+ WelsDestroyDecoder(openh264_decoder_); |
+ openh264_decoder_ = nullptr; |
+ } |
+ return WEBRTC_VIDEO_CODEC_OK; |
+} |
+ |
+int32_t H264DecoderImpl::Reset() { |
+ if (!IsInitialized()) |
+ return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
+ InitDecode(nullptr, 1); |
+ return WEBRTC_VIDEO_CODEC_OK; |
+} |
+ |
+int32_t H264DecoderImpl::RegisterDecodeCompleteCallback( |
+ DecodedImageCallback* callback) { |
+ decoded_image_callback_ = callback; |
+ return WEBRTC_VIDEO_CODEC_OK; |
+} |
+ |
+int32_t H264DecoderImpl::Decode(const EncodedImage& input_image, |
+ bool /*missing_frames*/, |
+ const RTPFragmentationHeader* /*fragmentation*/, |
+ const CodecSpecificInfo* codec_specific_info, |
+ int64_t /*render_time_ms*/) { |
+ if (!IsInitialized()) { |
+ return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
+ } |
+ if (!decoded_image_callback_) { |
+ LOG(LS_WARNING) << "InitDecode() has been called, but a callback function " |
+ << "has not been set with RegisterDecodeCompleteCallback()"; |
+ return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
+ } |
+ if (!input_image._buffer || !input_image._length) |
+ return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
+ if (codec_specific_info && |
+ codec_specific_info->codecType != VideoCodecType::kVideoCodecH264) { |
+ return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
+ } |
+ |
+ // DecodeFrameNoDelay output. |
+ uint8_t* data[3] = { 0, 0, 0 }; |
+ SBufferInfo buffer_info; |
+ memset(&buffer_info, 0, sizeof(SBufferInfo)); |
+ |
+ // Decode! |
+ DECODING_STATE decode_ret = openh264_decoder_->DecodeFrameNoDelay( |
+ input_image._buffer, input_image._length, data, &buffer_info); |
+ if (decode_ret != 0) { |
+ LOG(LS_ERROR) << "H264 decode failed, returned error bitmask: " |
+ << std::bitset<8>(decode_ret).to_string() << " = " |
+ << decode_ret; |
+ if (decode_ret & dsFramePending) |
+ LOG(LS_ERROR) << " dsFramePending"; |
+ if (decode_ret & dsRefLost) |
+ LOG(LS_ERROR) << " dsRefLost"; |
+ if (decode_ret & dsBitstreamError) |
+ LOG(LS_ERROR) << " dsBitstreamError"; |
+ if (decode_ret & dsDepLayerLost) |
+ LOG(LS_ERROR) << " dsDepLayerLost"; |
+ if (decode_ret & dsNoParamSets) |
+ LOG(LS_ERROR) << " dsNoParamSets"; |
+ if (decode_ret & dsDataErrorConcealed) |
+ LOG(LS_ERROR) << " dsDataErrorConcealed"; |
+ return WEBRTC_VIDEO_CODEC_ERROR; |
+ } |
+ RTC_DCHECK_EQ(decode_ret, 0); |
+ |
+ // Frame data ready? |
+ if (buffer_info.iBufferStatus == 1) { |
+ // Copy decoded data into |decoded_image_|. Must copy because the internal |
+ // VideoFrameBuffer is reference counted. |
+ decoded_image_.CreateFrame(data[0], data[1], data[2], |
+ buffer_info.UsrData.sSystemBuffer.iWidth, |
+ buffer_info.UsrData.sSystemBuffer.iHeight, |
+ buffer_info.UsrData.sSystemBuffer.iStride[0], |
+ buffer_info.UsrData.sSystemBuffer.iStride[1], |
+ buffer_info.UsrData.sSystemBuffer.iStride[1]); |
+ decoded_image_.set_timestamp(input_image._timeStamp); |
+ decoded_image_.set_ntp_time_ms(input_image.ntp_time_ms_); |
+ |
+ // Deliver decoded image. |
+ decoded_image_callback_->Decoded(decoded_image_); |
+ } |
+ return WEBRTC_VIDEO_CODEC_OK; |
+} |
+ |
+bool H264DecoderImpl::IsInitialized() { |
+ return openh264_decoder_ != nullptr; |
+} |
+ |
+} // namespace webrtc |