rtsp-server:wfd: Fix build error for gcc upgrade
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / openh264 / gstopenh264dec.cpp
1 /*
2  * Copyright (c) 2014, Ericsson AB. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
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.
13  *
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
23  * OF SUCH DAMAGE.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "gstopenh264elements.h"
31 #include "gstopenh264dec.h"
32
33 #include <wels/codec_ver.h>
34 #define OPENH264_VERSION_CHECK(maj,min) ((OPENH264_MAJOR > (maj)) || (OPENH264_MAJOR == (maj) && OPENH264_MINOR >= (min)))
35
36 #include <gst/gst.h>
37 #include <gst/video/video.h>
38 #include <gst/video/gstvideodecoder.h>
39 #include <string.h>             /* for memcpy */
40
41 #if OPENH264_VERSION_CHECK (1,9)
42 #define HAVE_OPENH264_MAIN_PROFILE 1
43 #else
44 #define HAVE_OPENH264_MAIN_PROFILE 0
45 #endif
46
47 GST_DEBUG_CATEGORY_STATIC (gst_openh264dec_debug_category);
48 #define GST_CAT_DEFAULT gst_openh264dec_debug_category
49
50 /* prototypes */
51 static gboolean gst_openh264dec_start (GstVideoDecoder * decoder);
52 static gboolean gst_openh264dec_stop (GstVideoDecoder * decoder);
53
54 static gboolean gst_openh264dec_set_format (GstVideoDecoder * decoder,
55     GstVideoCodecState * state);
56 static gboolean gst_openh264dec_reset (GstVideoDecoder * decoder,
57     gboolean hard);
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,
62     GstQuery * query);
63 static gboolean openh264dec_element_init (GstPlugin * plugin);
64
65 #if HAVE_OPENH264_MAIN_PROFILE
66 #define SUPPORTED_PROFILE_STR "profile=(string){ constrained-baseline, baseline, main, high, constrained-high, progressive-high }"
67 #else
68 #define SUPPORTED_PROFILE_STR "profile=(string){ constrained-baseline, baseline }"
69 #endif
70
71 /* pad templates */
72 static GstStaticPadTemplate gst_openh264dec_sink_template =
73 GST_STATIC_PAD_TEMPLATE ("sink",
74     GST_PAD_SINK,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS
77     ("video/x-h264, stream-format=(string)byte-stream, alignment=(string)au, "
78       SUPPORTED_PROFILE_STR
79     ));
80
81 static GstStaticPadTemplate gst_openh264dec_src_template =
82 GST_STATIC_PAD_TEMPLATE ("src",
83     GST_PAD_SRC,
84     GST_PAD_ALWAYS,
85     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")));
86
87 /* class initialization */
88
89 G_DEFINE_TYPE (GstOpenh264Dec, gst_openh264dec, GST_TYPE_VIDEO_DECODER);
90 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (openh264dec, openh264dec_element_init);
91
92 static void
93 gst_openh264dec_class_init (GstOpenh264DecClass * klass)
94 {
95   GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
96
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);
101
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");
105
106   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_openh264dec_start);
107   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_openh264dec_stop);
108
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);
117 }
118
119 static void
120 gst_openh264dec_init (GstOpenh264Dec * openh264dec)
121 {
122   openh264dec->decoder = NULL;
123
124   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (openh264dec), TRUE);
125   gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (openh264dec), TRUE);
126 }
127
128 #ifndef GST_DISABLE_GST_DEBUG
129 static void
130 openh264_trace_cb (void *ctx, int level, const char *string)
131 {
132   GObject *o = G_OBJECT (ctx);
133   GstDebugLevel lvl = GST_LEVEL_WARNING;
134
135   if (level >= WELS_LOG_DETAIL)
136     lvl = GST_LEVEL_LOG;
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;
145
146   gst_debug_log (GST_CAT_DEFAULT, lvl, "", "", 0, o, "%s", string);
147 }
148 #endif
149
150 static gboolean
151 gst_openh264dec_start (GstVideoDecoder * decoder)
152 {
153   GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
154   gint ret;
155   SDecodingParam dec_param = { 0 };
156
157   if (openh264dec->decoder != NULL) {
158     openh264dec->decoder->Uninitialize ();
159     WelsDestroyDecoder (openh264dec->decoder);
160     openh264dec->decoder = NULL;
161   }
162
163   if (WelsCreateDecoder (&(openh264dec->decoder)) != 0) {
164     GST_ELEMENT_ERROR (openh264dec, LIBRARY, INIT, (NULL),
165         ("Failed to create OpenH264 decoder."));
166     return FALSE;
167   }
168
169 #ifndef GST_DISABLE_GST_DEBUG
170   {
171     int log_level = WELS_LOG_WARNING;
172     WelsTraceCallback log_cb = openh264_trace_cb;
173
174     openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_LEVEL, &log_level);
175     openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK,
176         (void *) &log_cb);
177     openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
178         (void *) &decoder);
179   }
180 #endif
181
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;
186 #endif
187   dec_param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
188
189   ret = openh264dec->decoder->Initialize (&dec_param);
190
191   GST_DEBUG_OBJECT (openh264dec,
192       "openh264_dec_start called, openh264dec %sinitialized OK!",
193       (ret != cmResultSuccess) ? "NOT " : "");
194
195   return (ret == cmResultSuccess);
196 }
197
198 static gboolean
199 gst_openh264dec_stop (GstVideoDecoder * decoder)
200 {
201   GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
202
203   if (openh264dec->decoder) {
204     openh264dec->decoder->Uninitialize ();
205     WelsDestroyDecoder (openh264dec->decoder);
206     openh264dec->decoder = NULL;
207   }
208
209   if (openh264dec->input_state) {
210     gst_video_codec_state_unref (openh264dec->input_state);
211     openh264dec->input_state = NULL;
212   }
213   openh264dec->width = openh264dec->height = 0;
214
215   return TRUE;
216 }
217
218 static gboolean
219 gst_openh264dec_set_format (GstVideoDecoder * decoder,
220     GstVideoCodecState * state)
221 {
222   GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
223
224   GST_DEBUG_OBJECT (openh264dec, "input caps: %" GST_PTR_FORMAT, state->caps);
225
226   if (openh264dec->input_state) {
227     gst_video_codec_state_unref (openh264dec->input_state);
228     openh264dec->input_state = NULL;
229   }
230   openh264dec->input_state = gst_video_codec_state_ref (state);
231
232   return TRUE;
233 }
234
235 static gboolean
236 gst_openh264dec_reset (GstVideoDecoder * decoder, gboolean hard)
237 {
238   GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
239
240   GST_DEBUG_OBJECT (openh264dec, "reset");
241
242   return TRUE;
243 }
244
245 static GstFlowReturn
246 gst_openh264dec_handle_frame (GstVideoDecoder * decoder,
247     GstVideoCodecFrame * frame)
248 {
249   GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
250   GstMapInfo map_info;
251   GstVideoCodecState *state;
252   SBufferInfo dst_buf_info;
253   DECODING_STATE ret;
254   guint8 *yuvdata[3];
255   GstFlowReturn flow_status;
256   GstVideoFrame video_frame;
257   guint actual_width, actual_height;
258   guint i;
259   guint8 *p;
260   guint row_stride, component_width, component_height, src_width, row;
261
262   if (frame == NULL) {
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));
267
268     openh264dec->decoder->SetOption (DECODER_OPTION_END_OF_STREAM,
269         &end_of_stream);
270     ret = openh264dec->decoder->FlushFrame (yuvdata, &dst_buf_info);
271
272     if (ret != dsErrorFree || dst_buf_info.iBufferStatus != 1) {
273       GST_DEBUG_OBJECT (decoder, "No more frames to retrieve at EOS");
274       return GST_FLOW_EOS;
275     }
276 #else
277     return GST_FLOW_EOS;
278 #endif
279   } else {
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;
284     }
285
286     GST_LOG_OBJECT (openh264dec, "handle frame, 1st NAL type %d",
287         map_info.size > 4 ? map_info.data[4] & 0x1f : -1);
288
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;
293
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);
297
298     ret =
299         openh264dec->decoder->DecodeFrameNoDelay (map_info.data, map_info.size,
300         yuvdata, &dst_buf_info);
301     gst_buffer_unmap (frame->input_buffer, &map_info);
302
303     if (ret != dsErrorFree) {
304       /* Request a key unit from upstream */
305       GST_DEBUG_OBJECT (openh264dec, "Requesting a key unit");
306
307       gst_video_decoder_request_sync_point (decoder, frame,
308           (GstVideoDecoderRequestSyncPointFlags) 0);
309
310       GST_LOG_OBJECT (openh264dec, "error decoding nal, return code: %d", ret);
311       gst_video_codec_frame_unref (frame);
312
313       /* Get back the frame that was reported as errored */
314       frame =
315           gst_video_decoder_get_frame (decoder, dst_buf_info.uiOutYuvTimeStamp);
316       if (frame) {
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);
321       }
322       return GST_FLOW_OK;
323     }
324
325     gst_video_codec_frame_unref (frame);
326     frame = NULL;
327
328     /* No output available yet */
329     if (dst_buf_info.iBufferStatus != 1) {
330       GST_LOG_OBJECT (decoder, "No buffer decoded yet");
331       return GST_FLOW_OK;
332     }
333   }
334
335   GST_LOG_OBJECT (decoder, "Got back frame with frame ref %" G_GUINT64_FORMAT,
336       (guint64) dst_buf_info.uiOutYuvTimeStamp);
337
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);
341   if (!frame) {
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);
346     return GST_FLOW_OK;
347   }
348
349   actual_width = dst_buf_info.UsrData.sSystemBuffer.iWidth;
350   actual_height = dst_buf_info.UsrData.sSystemBuffer.iHeight;
351
352   if (!gst_pad_has_current_caps (GST_VIDEO_DECODER_SRC_PAD (openh264dec))
353       || actual_width != openh264dec->width
354       || actual_height != openh264dec->height) {
355     state =
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;
360
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;
367     }
368   } else {
369     state = gst_video_decoder_get_output_state (decoder);
370   }
371
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);
376     return flow_status;
377   }
378
379   if (!gst_video_frame_map (&video_frame, &state->info, frame->output_buffer,
380           GST_MAP_WRITE)) {
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;
385   }
386
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);
392     src_width =
393         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);
398       p += row_stride;
399       yuvdata[i] += src_width;
400     }
401   }
402   gst_video_codec_state_unref (state);
403   gst_video_frame_unmap (&video_frame);
404
405   return gst_video_decoder_finish_frame (decoder, frame);
406 }
407
408 static GstFlowReturn
409 gst_openh264dec_finish (GstVideoDecoder * decoder)
410 {
411   GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
412
413   GST_DEBUG_OBJECT (openh264dec, "finish");
414
415   /* Decoder not negotiated yet */
416   if (openh264dec->width == 0)
417     return GST_FLOW_OK;
418
419   /* Drain all pending frames */
420   while ((gst_openh264dec_handle_frame (decoder, NULL)) == GST_FLOW_OK);
421
422   return GST_FLOW_OK;
423 }
424
425 static gboolean
426 gst_openh264dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
427 {
428   GstVideoCodecState *state;
429   GstBufferPool *pool;
430   guint size, min, max;
431   GstStructure *config;
432
433   if (!GST_VIDEO_DECODER_CLASS (gst_openh264dec_parent_class)->decide_allocation
434       (decoder, query))
435     return FALSE;
436
437   state = gst_video_decoder_get_output_state (decoder);
438
439   gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
440
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);
445   }
446
447   gst_buffer_pool_set_config (pool, config);
448
449   gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
450
451   gst_object_unref (pool);
452   gst_video_codec_state_unref (state);
453
454   return TRUE;
455 }
456
457 static gboolean
458 openh264dec_element_init (GstPlugin * plugin)
459 {
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);
465
466   GST_ERROR ("Incorrect library version loaded, expecting %s", g_strCodecVer);
467   return FALSE;
468 }