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 "gstopenh264elements.h"
31 #include "gstopenh264dec.h"
33 #include <wels/codec_ver.h>
34 #define OPENH264_VERSION_CHECK(maj,min) ((OPENH264_MAJOR > (maj)) || (OPENH264_MAJOR == (maj) && OPENH264_MINOR >= (min)))
37 #include <gst/video/video.h>
38 #include <gst/video/gstvideodecoder.h>
39 #include <string.h> /* for memcpy */
41 #if OPENH264_VERSION_CHECK (1,9)
42 #define HAVE_OPENH264_MAIN_PROFILE 1
44 #define HAVE_OPENH264_MAIN_PROFILE 0
47 GST_DEBUG_CATEGORY_STATIC (gst_openh264dec_debug_category);
48 #define GST_CAT_DEFAULT gst_openh264dec_debug_category
51 static gboolean gst_openh264dec_start (GstVideoDecoder * decoder);
52 static gboolean gst_openh264dec_stop (GstVideoDecoder * decoder);
54 static gboolean gst_openh264dec_set_format (GstVideoDecoder * decoder,
55 GstVideoCodecState * state);
56 static gboolean gst_openh264dec_reset (GstVideoDecoder * decoder,
58 static GstFlowReturn gst_openh264dec_finish (GstVideoDecoder * decoder);
59 static GstFlowReturn gst_openh264dec_handle_frame (GstVideoDecoder * decoder,
60 GstVideoCodecFrame * frame);
61 static gboolean gst_openh264dec_decide_allocation (GstVideoDecoder * decoder,
63 static gboolean openh264dec_element_init (GstPlugin * plugin);
65 #if HAVE_OPENH264_MAIN_PROFILE
66 #define SUPPORTED_PROFILE_STR "profile=(string){ constrained-baseline, baseline, main, high, constrained-high, progressive-high }"
68 #define SUPPORTED_PROFILE_STR "profile=(string){ constrained-baseline, baseline }"
72 static GstStaticPadTemplate gst_openh264dec_sink_template =
73 GST_STATIC_PAD_TEMPLATE ("sink",
77 ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)au, "
81 static GstStaticPadTemplate gst_openh264dec_src_template =
82 GST_STATIC_PAD_TEMPLATE ("src",
85 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")));
87 /* class initialization */
89 G_DEFINE_TYPE (GstOpenh264Dec, gst_openh264dec, GST_TYPE_VIDEO_DECODER);
90 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (openh264dec, openh264dec_element_init);
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;
163 if (WelsCreateDecoder (&(openh264dec->decoder)) != 0) {
164 GST_ELEMENT_ERROR (openh264dec, LIBRARY, INIT, (NULL),
165 ("Failed to create OpenH264 decoder."));
169 #ifndef GST_DISABLE_GST_DEBUG
171 int log_level = WELS_LOG_WARNING;
172 WelsTraceCallback log_cb = openh264_trace_cb;
174 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_LEVEL, &log_level);
175 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK,
177 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
182 dec_param.uiTargetDqLayer = 255;
183 dec_param.eEcActiveIdc = ERROR_CON_FRAME_COPY;
184 #if OPENH264_MAJOR == 1 && OPENH264_MINOR < 6
185 dec_param.eOutputColorFormat = videoFormatI420;
187 dec_param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
189 ret = openh264dec->decoder->Initialize (&dec_param);
191 GST_DEBUG_OBJECT (openh264dec,
192 "openh264_dec_start called, openh264dec %sinitialized OK!",
193 (ret != cmResultSuccess) ? "NOT " : "");
195 return (ret == cmResultSuccess);
199 gst_openh264dec_stop (GstVideoDecoder * decoder)
201 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
203 if (openh264dec->decoder) {
204 openh264dec->decoder->Uninitialize ();
205 WelsDestroyDecoder (openh264dec->decoder);
206 openh264dec->decoder = NULL;
209 if (openh264dec->input_state) {
210 gst_video_codec_state_unref (openh264dec->input_state);
211 openh264dec->input_state = NULL;
213 openh264dec->width = openh264dec->height = 0;
219 gst_openh264dec_set_format (GstVideoDecoder * decoder,
220 GstVideoCodecState * state)
222 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
224 GST_DEBUG_OBJECT (openh264dec, "input caps: %" GST_PTR_FORMAT, state->caps);
226 if (openh264dec->input_state) {
227 gst_video_codec_state_unref (openh264dec->input_state);
228 openh264dec->input_state = NULL;
230 openh264dec->input_state = gst_video_codec_state_ref (state);
236 gst_openh264dec_reset (GstVideoDecoder * decoder, gboolean hard)
238 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
240 GST_DEBUG_OBJECT (openh264dec, "reset");
246 gst_openh264dec_handle_frame (GstVideoDecoder * decoder,
247 GstVideoCodecFrame * frame)
249 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
251 GstVideoCodecState *state;
252 SBufferInfo dst_buf_info;
255 GstFlowReturn flow_status;
256 GstVideoFrame video_frame;
257 guint actual_width, actual_height;
260 guint row_stride, component_width, component_height, src_width, row;
263 #if OPENH264_VERSION_CHECK (1,9)
264 /* Called with no videoframe for EOS logic. Drain out */
265 int end_of_stream = 1;
266 memset (&dst_buf_info, 0, sizeof (SBufferInfo));
268 openh264dec->decoder->SetOption (DECODER_OPTION_END_OF_STREAM,
270 ret = openh264dec->decoder->FlushFrame (yuvdata, &dst_buf_info);
272 if (ret != dsErrorFree || dst_buf_info.iBufferStatus != 1) {
273 GST_DEBUG_OBJECT (decoder, "No more frames to retrieve at EOS");
280 if (!gst_buffer_map (frame->input_buffer, &map_info, GST_MAP_READ)) {
281 GST_ERROR_OBJECT (openh264dec, "Cannot map input buffer!");
282 gst_video_codec_frame_unref (frame);
283 return GST_FLOW_ERROR;
286 GST_LOG_OBJECT (openh264dec, "handle frame, 1st NAL type %d",
287 map_info.size > 4 ? map_info.data[4] & 0x1f : -1);
289 memset (&dst_buf_info, 0, sizeof (SBufferInfo));
290 /* Use the unsigned long long OpenH264 timestamp to store the system_frame_number
291 * to track the original frame through any OpenH264 reordering */
292 dst_buf_info.uiInBsTimeStamp = frame->system_frame_number;
294 GST_LOG_OBJECT (decoder, "Submitting frame with PTS %" GST_TIME_FORMAT
295 " and frame ref %" G_GUINT64_FORMAT,
296 GST_TIME_ARGS (frame->pts), (guint64) frame->system_frame_number);
299 openh264dec->decoder->DecodeFrameNoDelay (map_info.data, map_info.size,
300 yuvdata, &dst_buf_info);
301 gst_buffer_unmap (frame->input_buffer, &map_info);
303 if (ret != dsErrorFree) {
304 /* Request a key unit from upstream */
305 GST_DEBUG_OBJECT (openh264dec, "Requesting a key unit");
307 gst_video_decoder_request_sync_point (decoder, frame,
308 (GstVideoDecoderRequestSyncPointFlags) 0);
310 GST_LOG_OBJECT (openh264dec, "error decoding nal, return code: %d", ret);
311 gst_video_codec_frame_unref (frame);
313 /* Get back the frame that was reported as errored */
315 gst_video_decoder_get_frame (decoder, dst_buf_info.uiOutYuvTimeStamp);
317 GST_LOG_OBJECT (decoder,
318 "Dropping errored frame ref %" G_GUINT64_FORMAT,
319 (guint64) dst_buf_info.uiOutYuvTimeStamp);
320 return gst_video_decoder_drop_frame (decoder, frame);
325 gst_video_codec_frame_unref (frame);
328 /* No output available yet */
329 if (dst_buf_info.iBufferStatus != 1) {
330 GST_LOG_OBJECT (decoder, "No buffer decoded yet");
335 GST_LOG_OBJECT (decoder, "Got back frame with frame ref %" G_GUINT64_FORMAT,
336 (guint64) dst_buf_info.uiOutYuvTimeStamp);
338 /* OpenH264 lets us pass an int reference through
339 * so we can retrieve the input frame now */
340 frame = gst_video_decoder_get_frame (decoder, dst_buf_info.uiOutYuvTimeStamp);
342 /* Where did our frame go? This is a reference tracking error. */
343 GST_WARNING_OBJECT (decoder,
344 "Failed to look up frame ref %" G_GUINT64_FORMAT,
345 (guint64) dst_buf_info.uiOutYuvTimeStamp);
349 actual_width = dst_buf_info.UsrData.sSystemBuffer.iWidth;
350 actual_height = dst_buf_info.UsrData.sSystemBuffer.iHeight;
352 if (!gst_pad_has_current_caps (GST_VIDEO_DECODER_SRC_PAD (openh264dec))
353 || actual_width != openh264dec->width
354 || actual_height != openh264dec->height) {
356 gst_video_decoder_set_output_state (decoder, GST_VIDEO_FORMAT_I420,
357 actual_width, actual_height, openh264dec->input_state);
358 openh264dec->width = actual_width;
359 openh264dec->height = actual_height;
361 if (!gst_video_decoder_negotiate (decoder)) {
362 GST_ERROR_OBJECT (openh264dec,
363 "Failed to negotiate with downstream elements");
364 gst_video_codec_state_unref (state);
365 gst_video_codec_frame_unref (frame);
366 return GST_FLOW_NOT_NEGOTIATED;
369 state = gst_video_decoder_get_output_state (decoder);
372 flow_status = gst_video_decoder_allocate_output_frame (decoder, frame);
373 if (flow_status != GST_FLOW_OK) {
374 gst_video_codec_state_unref (state);
375 gst_video_codec_frame_unref (frame);
379 if (!gst_video_frame_map (&video_frame, &state->info, frame->output_buffer,
381 GST_ERROR_OBJECT (openh264dec, "Cannot map output buffer!");
382 gst_video_codec_state_unref (state);
383 gst_video_codec_frame_unref (frame);
384 return GST_FLOW_ERROR;
387 for (i = 0; i < 3; i++) {
388 p = GST_VIDEO_FRAME_COMP_DATA (&video_frame, i);
389 row_stride = GST_VIDEO_FRAME_COMP_STRIDE (&video_frame, i);
390 component_width = GST_VIDEO_FRAME_COMP_WIDTH (&video_frame, i);
391 component_height = GST_VIDEO_FRAME_COMP_HEIGHT (&video_frame, i);
394 1 ? dst_buf_info.UsrData.sSystemBuffer.
395 iStride[0] : dst_buf_info.UsrData.sSystemBuffer.iStride[1];
396 for (row = 0; row < component_height; row++) {
397 memcpy (p, yuvdata[i], component_width);
399 yuvdata[i] += src_width;
402 gst_video_codec_state_unref (state);
403 gst_video_frame_unmap (&video_frame);
405 return gst_video_decoder_finish_frame (decoder, frame);
409 gst_openh264dec_finish (GstVideoDecoder * decoder)
411 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
413 GST_DEBUG_OBJECT (openh264dec, "finish");
415 /* Decoder not negotiated yet */
416 if (openh264dec->width == 0)
419 /* Drain all pending frames */
420 while ((gst_openh264dec_handle_frame (decoder, NULL)) == GST_FLOW_OK);
426 gst_openh264dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
428 GstVideoCodecState *state;
430 guint size, min, max;
431 GstStructure *config;
433 if (!GST_VIDEO_DECODER_CLASS (gst_openh264dec_parent_class)->decide_allocation
437 state = gst_video_decoder_get_output_state (decoder);
439 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
441 config = gst_buffer_pool_get_config (pool);
442 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
443 gst_buffer_pool_config_add_option (config,
444 GST_BUFFER_POOL_OPTION_VIDEO_META);
447 gst_buffer_pool_set_config (pool, config);
449 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
451 gst_object_unref (pool);
452 gst_video_codec_state_unref (state);
458 openh264dec_element_init (GstPlugin * plugin)
460 GST_DEBUG_CATEGORY_INIT (gst_openh264dec_debug_category, "openh264dec", 0,
461 "debug category for openh264dec element");
462 if (openh264_element_init (plugin))
463 return gst_element_register (plugin, "openh264dec", GST_RANK_MARGINAL,
464 GST_TYPE_OPENH264DEC);
466 GST_ERROR ("Incorrect library version loaded, expecting %s", g_strCodecVer);