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