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