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 <bitset> |
| 15 |
| 16 // OpenH264 |
| 17 #include "codec_api.h" |
| 18 #include "codec_app_def.h" |
| 19 #include "codec_def.h" |
| 20 |
| 21 #include "webrtc/base/checks.h" |
| 22 #include "webrtc/base/logging.h" |
| 23 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 24 |
| 25 namespace webrtc { |
| 26 |
| 27 namespace { |
| 28 const bool kOpenH264DecoderDetailedLogging = false; |
| 29 } // namespace |
| 30 |
| 31 H264DecoderImpl::H264DecoderImpl() |
| 32 : openh264_decoder_(nullptr), |
| 33 decoded_image_(), |
| 34 decoded_image_callback_(nullptr) { |
| 35 } |
| 36 |
| 37 H264DecoderImpl::~H264DecoderImpl() { |
| 38 Release(); |
| 39 } |
| 40 |
| 41 int32_t H264DecoderImpl::InitDecode(const VideoCodec* codec_settings, |
| 42 int32_t /*number_of_cores*/) { |
| 43 if (codec_settings && |
| 44 codec_settings->codecType != VideoCodecType::kVideoCodecH264) { |
| 45 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 46 } |
| 47 |
| 48 int release_ret = Release(); |
| 49 if (release_ret != WEBRTC_VIDEO_CODEC_OK) |
| 50 return release_ret; |
| 51 RTC_DCHECK(!openh264_decoder_); |
| 52 |
| 53 // Create decoder. |
| 54 if (WelsCreateDecoder(&openh264_decoder_) != 0) { |
| 55 LOG(LS_ERROR) << "Failed to create OpenH264 decoder"; |
| 56 RTC_DCHECK(!openh264_decoder_); |
| 57 return WEBRTC_VIDEO_CODEC_ERROR; |
| 58 } |
| 59 RTC_DCHECK(openh264_decoder_); |
| 60 if (kOpenH264DecoderDetailedLogging) { |
| 61 int trace_level = WELS_LOG_DETAIL; |
| 62 openh264_decoder_->SetOption(DECODER_OPTION_TRACE_LEVEL, |
| 63 &trace_level); |
| 64 } |
| 65 // else WELS_LOG_DEFAULT is used by default. |
| 66 |
| 67 // Initialization parameters. |
| 68 SDecodingParam init_params; |
| 69 memset(&init_params, 0, sizeof(SDecodingParam)); |
| 70 init_params.eOutputColorFormat = EVideoFormatType::videoFormatI420; |
| 71 init_params.uiCpuLoad = 0; |
| 72 init_params.uiTargetDqLayer = 0xFF; |
| 73 init_params.eEcActiveIdc = ERROR_CON_IDC::ERROR_CON_DISABLE; |
| 74 init_params.bParseOnly = false; |
| 75 init_params.sVideoProperty.eVideoBsType = |
| 76 VIDEO_BITSTREAM_TYPE::VIDEO_BITSTREAM_DEFAULT; |
| 77 |
| 78 // Initialize. |
| 79 if (openh264_decoder_->Initialize(&init_params) != 0) { |
| 80 LOG(LS_ERROR) << "Failed to initialize OpenH264 decoder"; |
| 81 Release(); |
| 82 return WEBRTC_VIDEO_CODEC_ERROR; |
| 83 } |
| 84 |
| 85 return WEBRTC_VIDEO_CODEC_OK; |
| 86 } |
| 87 |
| 88 int32_t H264DecoderImpl::Release() { |
| 89 if (openh264_decoder_) { |
| 90 int64_t uninit_ret = openh264_decoder_->Uninitialize(); |
| 91 if (uninit_ret != 0) { |
| 92 LOG(LS_WARNING) << "OpenH264 decoder's Uninitialize() returned " |
| 93 << "unsuccessful: " << uninit_ret; |
| 94 } |
| 95 WelsDestroyDecoder(openh264_decoder_); |
| 96 openh264_decoder_ = nullptr; |
| 97 } |
| 98 return WEBRTC_VIDEO_CODEC_OK; |
| 99 } |
| 100 |
| 101 int32_t H264DecoderImpl::Reset() { |
| 102 if (!IsInitialized()) |
| 103 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 104 InitDecode(nullptr, 1); |
| 105 return WEBRTC_VIDEO_CODEC_OK; |
| 106 } |
| 107 |
| 108 int32_t H264DecoderImpl::RegisterDecodeCompleteCallback( |
| 109 DecodedImageCallback* callback) { |
| 110 decoded_image_callback_ = callback; |
| 111 return WEBRTC_VIDEO_CODEC_OK; |
| 112 } |
| 113 |
| 114 int32_t H264DecoderImpl::Decode(const EncodedImage& input_image, |
| 115 bool /*missing_frames*/, |
| 116 const RTPFragmentationHeader* /*fragmentation*/, |
| 117 const CodecSpecificInfo* codec_specific_info, |
| 118 int64_t /*render_time_ms*/) { |
| 119 if (!IsInitialized()) { |
| 120 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 121 } |
| 122 if (!decoded_image_callback_) { |
| 123 LOG(LS_WARNING) << "InitDecode() has been called, but a callback function " |
| 124 << "has not been set with RegisterDecodeCompleteCallback()"; |
| 125 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 126 } |
| 127 if (!input_image._buffer || !input_image._length) |
| 128 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 129 if (codec_specific_info && |
| 130 codec_specific_info->codecType != VideoCodecType::kVideoCodecH264) { |
| 131 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 132 } |
| 133 |
| 134 // DecodeFrameNoDelay output. |
| 135 uint8_t* data[3] = { 0, 0, 0 }; |
| 136 SBufferInfo buffer_info; |
| 137 memset(&buffer_info, 0, sizeof(SBufferInfo)); |
| 138 |
| 139 // Decode! |
| 140 DECODING_STATE decode_ret = openh264_decoder_->DecodeFrameNoDelay( |
| 141 input_image._buffer, input_image._length, data, &buffer_info); |
| 142 if (decode_ret != 0) { |
| 143 LOG(LS_ERROR) << "H264 decode failed, returned error bitmask: " |
| 144 << std::bitset<8>(decode_ret).to_string() << " = " |
| 145 << decode_ret; |
| 146 if (decode_ret & dsFramePending) |
| 147 LOG(LS_ERROR) << " dsFramePending"; |
| 148 if (decode_ret & dsRefLost) |
| 149 LOG(LS_ERROR) << " dsRefLost"; |
| 150 if (decode_ret & dsBitstreamError) |
| 151 LOG(LS_ERROR) << " dsBitstreamError"; |
| 152 if (decode_ret & dsDepLayerLost) |
| 153 LOG(LS_ERROR) << " dsDepLayerLost"; |
| 154 if (decode_ret & dsNoParamSets) |
| 155 LOG(LS_ERROR) << " dsNoParamSets"; |
| 156 if (decode_ret & dsDataErrorConcealed) |
| 157 LOG(LS_ERROR) << " dsDataErrorConcealed"; |
| 158 return WEBRTC_VIDEO_CODEC_ERROR; |
| 159 } |
| 160 RTC_DCHECK_EQ(decode_ret, 0); |
| 161 |
| 162 // Frame data ready? |
| 163 if (buffer_info.iBufferStatus == 1) { |
| 164 // Copy decoded data into |decoded_image_|. Must copy because the internal |
| 165 // VideoFrameBuffer is reference counted. |
| 166 decoded_image_.CreateFrame(data[0], data[1], data[2], |
| 167 buffer_info.UsrData.sSystemBuffer.iWidth, |
| 168 buffer_info.UsrData.sSystemBuffer.iHeight, |
| 169 buffer_info.UsrData.sSystemBuffer.iStride[0], |
| 170 buffer_info.UsrData.sSystemBuffer.iStride[1], |
| 171 buffer_info.UsrData.sSystemBuffer.iStride[1]); |
| 172 decoded_image_.set_timestamp(input_image._timeStamp); |
| 173 decoded_image_.set_ntp_time_ms(input_image.ntp_time_ms_); |
| 174 |
| 175 // Deliver decoded image. |
| 176 decoded_image_callback_->Decoded(decoded_image_); |
| 177 } |
| 178 return WEBRTC_VIDEO_CODEC_OK; |
| 179 } |
| 180 |
| 181 bool H264DecoderImpl::IsInitialized() { |
| 182 return openh264_decoder_ != nullptr; |
| 183 } |
| 184 |
| 185 } // namespace webrtc |
OLD | NEW |