Chromium Code Reviews| 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..15abc3db3b06ff563b05b8d9e42c64cae55b49a8 |
| --- /dev/null |
| +++ b/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc |
| @@ -0,0 +1,175 @@ |
| +/* |
| + * 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 <iostream> |
|
Niklas Enbom
2015/09/14 22:33:14
#include bitset
hbos
2015/09/15 15:25:33
Done.
|
| +#include <vector> |
| + |
| +// 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 OPENH264_DECODER_LOGGING = false; |
| +} // anonymous 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; |
| + DCHECK(!openh264_decoder_); |
| + |
| + // Create decoder. |
| + if (WelsCreateDecoder(&openh264_decoder_) != 0) { |
| + // Failed to create decoder. |
| + LOG(LS_ERROR) << "Failed to create OpenH264 decoder"; |
| + DCHECK(!openh264_decoder_); |
| + return WEBRTC_VIDEO_CODEC_ERROR; |
| + } |
| + DCHECK(openh264_decoder_); |
| + |
| + // Initialization parameters. |
| + SDecodingParam init_params; |
| + memset(&init_params, 0, sizeof(SDecodingParam)); |
| + init_params.eOutputColorFormat = EVideoFormatType::videoFormatI420; |
| + init_params.uiTargetDqLayer = 0xFF; |
| + init_params.eEcActiveIdc = ERROR_CON_IDC::ERROR_CON_DISABLE; |
| + 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; |
| + } |
| + |
| + if (OPENH264_DECODER_LOGGING) { |
| + int trace_level = WELS_LOG_DETAIL; |
| + openh264_decoder_->SetOption(DECODER_OPTION_TRACE_LEVEL, |
| + &trace_level); |
| + } |
| + |
| + // 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) { |
| + std::cout << " DECODE FAILED: " << std::bitset<8>(decode_ret).to_string() |
| + << " = " << decode_ret << "\n"; |
| + if (decode_ret & dsBitstreamError) |
| + std::cout << " dsBitstreamError\n"; |
| + if (decode_ret & dsRefLost) |
| + std::cout << " dsRefLost\n"; |
| + if (decode_ret & dsNoParamSets) |
| + std::cout << " dsNoParamSets\n"; |
| + return WEBRTC_VIDEO_CODEC_ERROR; |
| + } |
| + CHECK_EQ(decode_ret, 0); |
| + |
| + // Frame data ready? |
| + if (buffer_info.iBufferStatus == 1) { |
| + // TODO(hbos): Lifetime of buffers? Need to malloc+copy in CreateFrame? |
| + 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); |
| + |
| + // Deliver decoded image. |
| + decoded_image_callback_->Decoded(decoded_image_); |
| + } |
| + return WEBRTC_VIDEO_CODEC_OK; |
| +} |
| + |
| +bool H264DecoderImpl::IsInitialized() { |
| + return openh264_decoder_ != nullptr; |
| +} |
| + |
| +} // namespace webrtc |