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_WITH_CODE (GstOpenh264Dec, gst_openh264dec,
90 GST_TYPE_VIDEO_DECODER,
91 GST_DEBUG_CATEGORY_INIT (gst_openh264dec_debug_category, "openh264dec", 0,
92 "debug category for openh264dec element"));
93 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (openh264dec, openh264dec_element_init);
94
95 static void
gst_openh264dec_class_init(GstOpenh264DecClass * klass)96 gst_openh264dec_class_init (GstOpenh264DecClass * klass)
97 {
98 GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
99
100 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
101 &gst_openh264dec_sink_template);
102 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
103 &gst_openh264dec_src_template);
104
105 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
106 "OpenH264 video decoder", "Decoder/Video", "OpenH264 video decoder",
107 "Ericsson AB, http://www.ericsson.com");
108
109 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_openh264dec_start);
110 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_openh264dec_stop);
111
112 video_decoder_class->set_format =
113 GST_DEBUG_FUNCPTR (gst_openh264dec_set_format);
114 video_decoder_class->reset = GST_DEBUG_FUNCPTR (gst_openh264dec_reset);
115 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_openh264dec_finish);
116 video_decoder_class->handle_frame =
117 GST_DEBUG_FUNCPTR (gst_openh264dec_handle_frame);
118 video_decoder_class->decide_allocation =
119 GST_DEBUG_FUNCPTR (gst_openh264dec_decide_allocation);
120 }
121
122 static void
gst_openh264dec_init(GstOpenh264Dec * openh264dec)123 gst_openh264dec_init (GstOpenh264Dec * openh264dec)
124 {
125 openh264dec->decoder = NULL;
126
127 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (openh264dec), TRUE);
128 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (openh264dec), TRUE);
129 }
130
131 #ifndef GST_DISABLE_GST_DEBUG
132 static void
openh264_trace_cb(void * ctx,int level,const char * string)133 openh264_trace_cb (void *ctx, int level, const char *string)
134 {
135 GObject *o = G_OBJECT (ctx);
136 GstDebugLevel lvl = GST_LEVEL_WARNING;
137
138 if (level >= WELS_LOG_DETAIL)
139 lvl = GST_LEVEL_LOG;
140 else if (level >= WELS_LOG_DEBUG)
141 lvl = GST_LEVEL_DEBUG;
142 else if (level >= WELS_LOG_INFO)
143 lvl = GST_LEVEL_INFO;
144 else if (level >= WELS_LOG_WARNING)
145 lvl = GST_LEVEL_WARNING;
146 else if (level >= WELS_LOG_ERROR)
147 lvl = GST_LEVEL_ERROR;
148
149 gst_debug_log (GST_CAT_DEFAULT, lvl, "", "", 0, o, "%s", string);
150 }
151 #endif
152
153 static gboolean
gst_openh264dec_start(GstVideoDecoder * decoder)154 gst_openh264dec_start (GstVideoDecoder * decoder)
155 {
156 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
157 gint ret;
158 SDecodingParam dec_param = { 0 };
159
160 if (openh264dec->decoder != NULL) {
161 openh264dec->decoder->Uninitialize ();
162 WelsDestroyDecoder (openh264dec->decoder);
163 openh264dec->decoder = NULL;
164 }
165 WelsCreateDecoder (&(openh264dec->decoder));
166
167 #ifndef GST_DISABLE_GST_DEBUG
168 {
169 int log_level = WELS_LOG_WARNING;
170 WelsTraceCallback log_cb = openh264_trace_cb;
171
172 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_LEVEL, &log_level);
173 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK,
174 (void *) &log_cb);
175 openh264dec->decoder->SetOption (DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
176 (void *) &decoder);
177 }
178 #endif
179
180 dec_param.uiTargetDqLayer = 255;
181 dec_param.eEcActiveIdc = ERROR_CON_FRAME_COPY;
182 #if OPENH264_MAJOR == 1 && OPENH264_MINOR < 6
183 dec_param.eOutputColorFormat = videoFormatI420;
184 #endif
185 dec_param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
186
187 ret = openh264dec->decoder->Initialize (&dec_param);
188
189 GST_DEBUG_OBJECT (openh264dec,
190 "openh264_dec_start called, openh264dec %sinitialized OK!",
191 (ret != cmResultSuccess) ? "NOT " : "");
192
193 return (ret == cmResultSuccess);
194 }
195
196 static gboolean
gst_openh264dec_stop(GstVideoDecoder * decoder)197 gst_openh264dec_stop (GstVideoDecoder * decoder)
198 {
199 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
200
201 if (openh264dec->decoder) {
202 openh264dec->decoder->Uninitialize ();
203 WelsDestroyDecoder (openh264dec->decoder);
204 openh264dec->decoder = NULL;
205 }
206
207 if (openh264dec->input_state) {
208 gst_video_codec_state_unref (openh264dec->input_state);
209 openh264dec->input_state = NULL;
210 }
211 openh264dec->width = openh264dec->height = 0;
212
213 return TRUE;
214 }
215
216 static gboolean
gst_openh264dec_set_format(GstVideoDecoder * decoder,GstVideoCodecState * state)217 gst_openh264dec_set_format (GstVideoDecoder * decoder,
218 GstVideoCodecState * state)
219 {
220 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
221
222 GST_DEBUG_OBJECT (openh264dec, "input caps: %" GST_PTR_FORMAT, state->caps);
223
224 if (openh264dec->input_state) {
225 gst_video_codec_state_unref (openh264dec->input_state);
226 openh264dec->input_state = NULL;
227 }
228 openh264dec->input_state = gst_video_codec_state_ref (state);
229
230 return TRUE;
231 }
232
233 static gboolean
gst_openh264dec_reset(GstVideoDecoder * decoder,gboolean hard)234 gst_openh264dec_reset (GstVideoDecoder * decoder, gboolean hard)
235 {
236 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
237
238 GST_DEBUG_OBJECT (openh264dec, "reset");
239
240 return TRUE;
241 }
242
243 static GstFlowReturn
gst_openh264dec_handle_frame(GstVideoDecoder * decoder,GstVideoCodecFrame * frame)244 gst_openh264dec_handle_frame (GstVideoDecoder * decoder,
245 GstVideoCodecFrame * frame)
246 {
247 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
248 GstMapInfo map_info;
249 GstVideoCodecState *state;
250 SBufferInfo dst_buf_info;
251 DECODING_STATE ret;
252 guint8 *yuvdata[3];
253 GstFlowReturn flow_status;
254 GstVideoFrame video_frame;
255 guint actual_width, actual_height;
256 guint i;
257 guint8 *p;
258 guint row_stride, component_width, component_height, src_width, row;
259
260 if (frame == NULL) {
261 #if OPENH264_VERSION_CHECK (1,9)
262 /* Called with no videoframe for EOS logic. Drain out */
263 int end_of_stream = 1;
264 memset (&dst_buf_info, 0, sizeof (SBufferInfo));
265
266 openh264dec->decoder->SetOption (DECODER_OPTION_END_OF_STREAM,
267 &end_of_stream);
268 ret = openh264dec->decoder->FlushFrame (yuvdata, &dst_buf_info);
269
270 if (ret != dsErrorFree || dst_buf_info.iBufferStatus != 1) {
271 GST_DEBUG_OBJECT (decoder, "No more frames to retrieve at EOS");
272 return GST_FLOW_EOS;
273 }
274 #else
275 return GST_FLOW_EOS;
276 #endif
277 } else {
278 if (!gst_buffer_map (frame->input_buffer, &map_info, GST_MAP_READ)) {
279 GST_ERROR_OBJECT (openh264dec, "Cannot map input buffer!");
280 gst_video_codec_frame_unref (frame);
281 return GST_FLOW_ERROR;
282 }
283
284 GST_LOG_OBJECT (openh264dec, "handle frame, 1st NAL type %d",
285 map_info.size > 4 ? map_info.data[4] & 0x1f : -1);
286
287 memset (&dst_buf_info, 0, sizeof (SBufferInfo));
288 /* Use the unsigned long long OpenH264 timestamp to store the system_frame_number
289 * to track the original frame through any OpenH264 reordering */
290 dst_buf_info.uiInBsTimeStamp = frame->system_frame_number;
291
292 GST_LOG_OBJECT (decoder, "Submitting frame with PTS %" GST_TIME_FORMAT
293 " and frame ref %" G_GUINT64_FORMAT,
294 GST_TIME_ARGS (frame->pts), (guint64) frame->system_frame_number);
295
296 ret =
297 openh264dec->decoder->DecodeFrameNoDelay (map_info.data, map_info.size,
298 yuvdata, &dst_buf_info);
299 gst_buffer_unmap (frame->input_buffer, &map_info);
300
301 if (ret != dsErrorFree) {
302 /* Request a key unit from upstream */
303 GST_DEBUG_OBJECT (openh264dec, "Requesting a key unit");
304
305 gst_video_decoder_request_sync_point (decoder, frame,
306 (GstVideoDecoderRequestSyncPointFlags) 0);
307
308 GST_LOG_OBJECT (openh264dec, "error decoding nal, return code: %d", ret);
309 gst_video_codec_frame_unref (frame);
310
311 /* Get back the frame that was reported as errored */
312 frame =
313 gst_video_decoder_get_frame (decoder, dst_buf_info.uiOutYuvTimeStamp);
314 if (frame) {
315 GST_LOG_OBJECT (decoder,
316 "Dropping errored frame ref %" G_GUINT64_FORMAT,
317 (guint64) dst_buf_info.uiOutYuvTimeStamp);
318 return gst_video_decoder_drop_frame (decoder, frame);
319 }
320 return GST_FLOW_OK;
321 }
322
323 gst_video_codec_frame_unref (frame);
324 frame = NULL;
325
326 /* No output available yet */
327 if (dst_buf_info.iBufferStatus != 1) {
328 GST_LOG_OBJECT (decoder, "No buffer decoded yet");
329 return GST_FLOW_OK;
330 }
331 }
332
333 GST_LOG_OBJECT (decoder, "Got back frame with frame ref %" G_GUINT64_FORMAT,
334 (guint64) dst_buf_info.uiOutYuvTimeStamp);
335
336 /* OpenH264 lets us pass an int reference through
337 * so we can retrieve the input frame now */
338 frame = gst_video_decoder_get_frame (decoder, dst_buf_info.uiOutYuvTimeStamp);
339 if (!frame) {
340 /* Where did our frame go? This is a reference tracking error. */
341 GST_WARNING_OBJECT (decoder,
342 "Failed to look up frame ref %" G_GUINT64_FORMAT,
343 (guint64) dst_buf_info.uiOutYuvTimeStamp);
344 return GST_FLOW_OK;
345 }
346
347 actual_width = dst_buf_info.UsrData.sSystemBuffer.iWidth;
348 actual_height = dst_buf_info.UsrData.sSystemBuffer.iHeight;
349
350 if (!gst_pad_has_current_caps (GST_VIDEO_DECODER_SRC_PAD (openh264dec))
351 || actual_width != openh264dec->width
352 || actual_height != openh264dec->height) {
353 state =
354 gst_video_decoder_set_output_state (decoder, GST_VIDEO_FORMAT_I420,
355 actual_width, actual_height, openh264dec->input_state);
356 openh264dec->width = actual_width;
357 openh264dec->height = actual_height;
358
359 if (!gst_video_decoder_negotiate (decoder)) {
360 GST_ERROR_OBJECT (openh264dec,
361 "Failed to negotiate with downstream elements");
362 gst_video_codec_state_unref (state);
363 gst_video_codec_frame_unref (frame);
364 return GST_FLOW_NOT_NEGOTIATED;
365 }
366 } else {
367 state = gst_video_decoder_get_output_state (decoder);
368 }
369
370 flow_status = gst_video_decoder_allocate_output_frame (decoder, frame);
371 if (flow_status != GST_FLOW_OK) {
372 gst_video_codec_state_unref (state);
373 gst_video_codec_frame_unref (frame);
374 return flow_status;
375 }
376
377 if (!gst_video_frame_map (&video_frame, &state->info, frame->output_buffer,
378 GST_MAP_WRITE)) {
379 GST_ERROR_OBJECT (openh264dec, "Cannot map output buffer!");
380 gst_video_codec_state_unref (state);
381 gst_video_codec_frame_unref (frame);
382 return GST_FLOW_ERROR;
383 }
384
385 for (i = 0; i < 3; i++) {
386 p = GST_VIDEO_FRAME_COMP_DATA (&video_frame, i);
387 row_stride = GST_VIDEO_FRAME_COMP_STRIDE (&video_frame, i);
388 component_width = GST_VIDEO_FRAME_COMP_WIDTH (&video_frame, i);
389 component_height = GST_VIDEO_FRAME_COMP_HEIGHT (&video_frame, i);
390 src_width =
391 i <
392 1 ? dst_buf_info.UsrData.sSystemBuffer.
393 iStride[0] : dst_buf_info.UsrData.sSystemBuffer.iStride[1];
394 for (row = 0; row < component_height; row++) {
395 memcpy (p, yuvdata[i], component_width);
396 p += row_stride;
397 yuvdata[i] += src_width;
398 }
399 }
400 gst_video_codec_state_unref (state);
401 gst_video_frame_unmap (&video_frame);
402
403 return gst_video_decoder_finish_frame (decoder, frame);
404 }
405
406 static GstFlowReturn
gst_openh264dec_finish(GstVideoDecoder * decoder)407 gst_openh264dec_finish (GstVideoDecoder * decoder)
408 {
409 GstOpenh264Dec *openh264dec = GST_OPENH264DEC (decoder);
410
411 GST_DEBUG_OBJECT (openh264dec, "finish");
412
413 /* Decoder not negotiated yet */
414 if (openh264dec->width == 0)
415 return GST_FLOW_OK;
416
417 /* Drain all pending frames */
418 while ((gst_openh264dec_handle_frame (decoder, NULL)) == GST_FLOW_OK);
419
420 return GST_FLOW_OK;
421 }
422
423 static gboolean
gst_openh264dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)424 gst_openh264dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
425 {
426 GstVideoCodecState *state;
427 GstBufferPool *pool;
428 guint size, min, max;
429 GstStructure *config;
430
431 if (!GST_VIDEO_DECODER_CLASS (gst_openh264dec_parent_class)->decide_allocation
432 (decoder, query))
433 return FALSE;
434
435 state = gst_video_decoder_get_output_state (decoder);
436
437 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
438
439 config = gst_buffer_pool_get_config (pool);
440 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
441 gst_buffer_pool_config_add_option (config,
442 GST_BUFFER_POOL_OPTION_VIDEO_META);
443 }
444
445 gst_buffer_pool_set_config (pool, config);
446
447 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
448
449 gst_object_unref (pool);
450 gst_video_codec_state_unref (state);
451
452 return TRUE;
453 }
454
455 static gboolean
openh264dec_element_init(GstPlugin * plugin)456 openh264dec_element_init (GstPlugin * plugin)
457 {
458 if (openh264_element_init (plugin))
459 return gst_element_register (plugin, "openh264dec", GST_RANK_MARGINAL,
460 GST_TYPE_OPENH264DEC);
461
462 GST_ERROR ("Incorrect library version loaded, expecting %s", g_strCodecVer);
463 return FALSE;
464 }
465