2 * Copyright (c) 2014, Ericsson AB. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or other
12 * materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 #include "gstopenh264dec.h"
32 #include <wels/codec_ver.h>
33 #define OPENH264_VERSION_CHECK(maj,min) ((OPENH264_MAJOR > (maj)) || (OPENH264_MAJOR == (maj) && OPENH264_MINOR >= (min)))
36 #include <gst/video/video.h>
37 #include <gst/video/gstvideodecoder.h>
38 #include <string.h> /* for memcpy */
40 #if OPENH264_VERSION_CHECK (1,9)
41 #define HAVE_OPENH264_MAIN_PROFILE 1
43 #define HAVE_OPENH264_MAIN_PROFILE 0
46 GST_DEBUG_CATEGORY_STATIC (gst_openh264dec_debug_category);
47 #define GST_CAT_DEFAULT gst_openh264dec_debug_category
50 static gboolean gst_openh264dec_start (GstVideoDecoder * decoder);
51 static gboolean gst_openh264dec_stop (GstVideoDecoder * decoder);
53 static gboolean gst_openh264dec_set_format (GstVideoDecoder * decoder,
54 GstVideoCodecState * state);
55 static gboolean gst_openh264dec_reset (GstVideoDecoder * decoder,
57 static GstFlowReturn gst_openh264dec_finish (GstVideoDecoder * decoder);
58 static GstFlowReturn gst_openh264dec_handle_frame (GstVideoDecoder * decoder,
59 GstVideoCodecFrame * frame);
60 static gboolean gst_openh264dec_decide_allocation (GstVideoDecoder * decoder,
63 #if HAVE_OPENH264_MAIN_PROFILE
64 #define SUPPORTED_PROFILE_STR "profile=(string){ constrained-baseline, baseline, main, high, constrained-high, progressive-high }"
66 #define SUPPORTED_PROFILE_STR "profile=(string){ constrained-baseline, baseline }"
70 static GstStaticPadTemplate gst_openh264dec_sink_template =
71 GST_STATIC_PAD_TEMPLATE ("sink",
75 ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)au, "
79 static GstStaticPadTemplate gst_openh264dec_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
83 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")));
85 /* class initialization */
87 G_DEFINE_TYPE_WITH_CODE (GstOpenh264Dec, gst_openh264dec,
88 GST_TYPE_VIDEO_DECODER,
89 GST_DEBUG_CATEGORY_INIT (gst_openh264dec_debug_category, "openh264dec", 0,
90 "debug category for openh264dec element"));
93 gst_openh264dec_class_init (GstOpenh264DecClass * klass)
95 GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
97 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
98 &gst_openh264dec_sink_template);
99 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
100 &gst_openh264dec_src_template);
102 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
103 "OpenH264 video decoder", "Decoder/Video", "OpenH264 video decoder",
104 "Ericsson AB, http://www.ericsson.com");
106 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_openh264dec_start);
107 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_openh264dec_stop);
109 video_decoder_class->set_format =
110 GST_DEBUG_FUNCPTR (gst_openh264dec_set_format);
111 video_decoder_class->reset = GST_DEBUG_FUNCPTR (gst_openh264dec_reset);
112 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_openh264dec_finish);
113 video_decoder_class->handle_frame =
114 GST_DEBUG_FUNCPTR (gst_openh264dec_handle_frame);
115 video_decoder_class->decide_allocation =
116 GST_DEBUG_FUNCPTR (gst_openh264dec_decide_allocation);
120 gst_openh264dec_init (GstOpenh264Dec * openh264dec)
122 openh264dec->decoder = NULL;
124 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (openh264dec), TRUE);
125 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (openh264dec), TRUE);
128 #ifndef GST_DISABLE_GST_DEBUG
130 openh264_trace_cb (void *ctx, int level, const char *string)
132 GObject *o = G_OBJECT (ctx);
133 GstDebugLevel lvl = GST_LEVEL_WARNING;
135 if (level >= WELS_LOG_DETAIL)
137 else if (level >= WELS_LOG_DEBUG)
138 lvl = GST_LEVEL_DEBUG;
139 else if (level >= WELS_LOG_INFO)
140 lvl = GST_LEVEL_INFO;
141 else if (level >= WELS_LOG_WARNING)
142 lvl = GST_LEVEL_WARNING;
143 else if (level >= WELS_LOG_ERROR)
144 lvl = GST_LEVEL_ERROR;
146 gst_debug_log (GST_CAT_DEFAULT, lvl, "", "", 0, o, "%s", string);
151 gst_openh264dec_start (GstVideoDecoder * decoder)
153 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
155 SDecodingParam dec_param = { 0 };
157 if (openh264dec->decoder != NULL) {
158 openh264dec->decoder->Uninitialize ();
159 WelsDestroyDecoder (openh264dec->decoder);
160 openh264dec->decoder = NULL;
162 WelsCreateDecoder (&(openh264dec->decoder));
164 #ifndef GST_DISABLE_GST_DEBUG
166 int log_level = WELS_LOG_WARNING;
167 WelsTraceCallback log_cb = openh264_trace_cb;
169 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_LEVEL, &log_level);
170 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK,
172 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
177 dec_param.uiTargetDqLayer = 255;
178 dec_param.eEcActiveIdc = ERROR_CON_FRAME_COPY;
179 #if OPENH264_MAJOR == 1 && OPENH264_MINOR < 6
180 dec_param.eOutputColorFormat = videoFormatI420;
182 dec_param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
184 ret = openh264dec->decoder->Initialize (&dec_param);
186 GST_DEBUG_OBJECT (openh264dec,
187 "openh264_dec_start called, openh264dec %sinitialized OK!",
188 (ret != cmResultSuccess) ? "NOT " : "");
190 return (ret == cmResultSuccess);
194 gst_openh264dec_stop (GstVideoDecoder * decoder)
196 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
198 if (openh264dec->decoder) {
199 openh264dec->decoder->Uninitialize ();
200 WelsDestroyDecoder (openh264dec->decoder);
201 openh264dec->decoder = NULL;
204 if (openh264dec->input_state) {
205 gst_video_codec_state_unref (openh264dec->input_state);
206 openh264dec->input_state = NULL;
208 openh264dec->width = openh264dec->height = 0;
214 gst_openh264dec_set_format (GstVideoDecoder * decoder,
215 GstVideoCodecState * state)
217 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
219 GST_DEBUG_OBJECT (openh264dec, "input caps: %" GST_PTR_FORMAT, state->caps);
221 if (openh264dec->input_state) {
222 gst_video_codec_state_unref (openh264dec->input_state);
223 openh264dec->input_state = NULL;
225 openh264dec->input_state = gst_video_codec_state_ref (state);
231 gst_openh264dec_reset (GstVideoDecoder * decoder, gboolean hard)
233 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
235 GST_DEBUG_OBJECT (openh264dec, "reset");
241 gst_openh264dec_handle_frame (GstVideoDecoder * decoder,
242 GstVideoCodecFrame * frame)
244 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
246 GstVideoCodecState *state;
247 SBufferInfo dst_buf_info;
250 GstFlowReturn flow_status;
251 GstVideoFrame video_frame;
252 guint actual_width, actual_height;
255 guint row_stride, component_width, component_height, src_width, row;
258 #if OPENH264_VERSION_CHECK (1,9)
259 /* Called with no videoframe for EOS logic. Drain out */
260 int end_of_stream = 1;
261 memset (&dst_buf_info, 0, sizeof (SBufferInfo));
263 openh264dec->decoder->SetOption (DECODER_OPTION_END_OF_STREAM,
265 ret = openh264dec->decoder->FlushFrame (yuvdata, &dst_buf_info);
267 if (ret != dsErrorFree || dst_buf_info.iBufferStatus != 1) {
268 GST_DEBUG_OBJECT (decoder, "No more frames to retrieve at EOS");
275 if (!gst_buffer_map (frame->input_buffer, &map_info, GST_MAP_READ)) {
276 GST_ERROR_OBJECT (openh264dec, "Cannot map input buffer!");
277 gst_video_codec_frame_unref (frame);
278 return GST_FLOW_ERROR;
281 GST_LOG_OBJECT (openh264dec, "handle frame, 1st NAL type %d",
282 map_info.size > 4 ? map_info.data[4] & 0x1f : -1);
284 memset (&dst_buf_info, 0, sizeof (SBufferInfo));
285 /* Use the unsigned long long OpenH264 timestamp to store the system_frame_number
286 * to track the original frame through any OpenH264 reordering */
287 dst_buf_info.uiInBsTimeStamp = frame->system_frame_number;
289 GST_LOG_OBJECT (decoder, "Submitting frame with PTS %" GST_TIME_FORMAT
290 " and frame ref %" G_GUINT64_FORMAT,
291 GST_TIME_ARGS (frame->pts), (guint64) frame->system_frame_number);
294 openh264dec->decoder->DecodeFrameNoDelay (map_info.data, map_info.size,
295 yuvdata, &dst_buf_info);
296 gst_buffer_unmap (frame->input_buffer, &map_info);
298 if (ret != dsErrorFree) {
299 /* Request a key unit from upstream */
300 GST_DEBUG_OBJECT (openh264dec, "Requesting a key unit");
302 gst_video_decoder_request_sync_point (decoder, frame,
303 (GstVideoDecoderRequestSyncPointFlags) 0);
305 GST_LOG_OBJECT (openh264dec, "error decoding nal, return code: %d", ret);
306 gst_video_codec_frame_unref (frame);
308 /* Get back the frame that was reported as errored */
310 gst_video_decoder_get_frame (decoder, dst_buf_info.uiOutYuvTimeStamp);
312 GST_LOG_OBJECT (decoder,
313 "Dropping errored frame ref %" G_GUINT64_FORMAT,
314 (guint64) dst_buf_info.uiOutYuvTimeStamp);
315 return gst_video_decoder_drop_frame (decoder, frame);
320 gst_video_codec_frame_unref (frame);
323 /* No output available yet */
324 if (dst_buf_info.iBufferStatus != 1) {
325 GST_LOG_OBJECT (decoder, "No buffer decoded yet");
330 GST_LOG_OBJECT (decoder, "Got back frame with frame ref %" G_GUINT64_FORMAT,
331 (guint64) dst_buf_info.uiOutYuvTimeStamp);
333 /* OpenH264 lets us pass an int reference through
334 * so we can retrieve the input frame now */
335 frame = gst_video_decoder_get_frame (decoder, dst_buf_info.uiOutYuvTimeStamp);
337 /* Where did our frame go? This is a reference tracking error. */
338 GST_WARNING_OBJECT (decoder,
339 "Failed to look up frame ref %" G_GUINT64_FORMAT,
340 (guint64) dst_buf_info.uiOutYuvTimeStamp);
344 actual_width = dst_buf_info.UsrData.sSystemBuffer.iWidth;
345 actual_height = dst_buf_info.UsrData.sSystemBuffer.iHeight;
347 if (!gst_pad_has_current_caps (GST_VIDEO_DECODER_SRC_PAD (openh264dec))
348 || actual_width != openh264dec->width
349 || actual_height != openh264dec->height) {
351 gst_video_decoder_set_output_state (decoder, GST_VIDEO_FORMAT_I420,
352 actual_width, actual_height, openh264dec->input_state);
353 openh264dec->width = actual_width;
354 openh264dec->height = actual_height;
356 if (!gst_video_decoder_negotiate (decoder)) {
357 GST_ERROR_OBJECT (openh264dec,
358 "Failed to negotiate with downstream elements");
359 gst_video_codec_state_unref (state);
360 gst_video_codec_frame_unref (frame);
361 return GST_FLOW_NOT_NEGOTIATED;
364 state = gst_video_decoder_get_output_state (decoder);
367 flow_status = gst_video_decoder_allocate_output_frame (decoder, frame);
368 if (flow_status != GST_FLOW_OK) {
369 gst_video_codec_state_unref (state);
370 gst_video_codec_frame_unref (frame);
374 if (!gst_video_frame_map (&video_frame, &state->info, frame->output_buffer,
376 GST_ERROR_OBJECT (openh264dec, "Cannot map output buffer!");
377 gst_video_codec_state_unref (state);
378 gst_video_codec_frame_unref (frame);
379 return GST_FLOW_ERROR;
382 for (i = 0; i < 3; i++) {
383 p = GST_VIDEO_FRAME_COMP_DATA (&video_frame, i);
384 row_stride = GST_VIDEO_FRAME_COMP_STRIDE (&video_frame, i);
385 component_width = GST_VIDEO_FRAME_COMP_WIDTH (&video_frame, i);
386 component_height = GST_VIDEO_FRAME_COMP_HEIGHT (&video_frame, i);
389 1 ? dst_buf_info.UsrData.sSystemBuffer.
390 iStride[0] : dst_buf_info.UsrData.sSystemBuffer.iStride[1];
391 for (row = 0; row < component_height; row++) {
392 memcpy (p, yuvdata[i], component_width);
394 yuvdata[i] += src_width;
397 gst_video_codec_state_unref (state);
398 gst_video_frame_unmap (&video_frame);
400 return gst_video_decoder_finish_frame (decoder, frame);
404 gst_openh264dec_finish (GstVideoDecoder * decoder)
406 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
408 GST_DEBUG_OBJECT (openh264dec, "finish");
410 /* Decoder not negotiated yet */
411 if (openh264dec->width == 0)
414 /* Drain all pending frames */
415 while ((gst_openh264dec_handle_frame (decoder, NULL)) == GST_FLOW_OK);
421 gst_openh264dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
423 GstVideoCodecState *state;
425 guint size, min, max;
426 GstStructure *config;
428 if (!GST_VIDEO_DECODER_CLASS (gst_openh264dec_parent_class)->decide_allocation
432 state = gst_video_decoder_get_output_state (decoder);
434 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
436 config = gst_buffer_pool_get_config (pool);
437 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
438 gst_buffer_pool_config_add_option (config,
439 GST_BUFFER_POOL_OPTION_VIDEO_META);
442 gst_buffer_pool_set_config (pool, config);
444 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
446 gst_object_unref (pool);
447 gst_video_codec_state_unref (state);