• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer H.263 Parser
2  * Copyright (C) <2010> Arun Raghavan <arun.raghavan@collabora.co.uk>
3  * Copyright (C) <2010> Edward Hervey <edward.hervey@collabora.co.uk>
4  * Copyright (C) <2010> Collabora Multimedia
5  * Copyright (C) <2010> Nokia Corporation
6  *
7  * Some bits C-c,C-v'ed and s/4/3 from h264parse:
8  *           (C) 2005 Michal Benes <michal.benes@itonis.tv>
9  *           (C) 2008 Wim Taymans <wim.taymans@gmail.com>
10  *           (C) 2009 Mark Nauwelaerts <mnauw users sf net>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the
24  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25  * Boston, MA 02110-1301, USA.
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31 
32 #include <gst/base/base.h>
33 #include <gst/pbutils/pbutils.h>
34 #include "gstvideoparserselements.h"
35 #include "gsth263parse.h"
36 
37 #include <string.h>
38 
39 GST_DEBUG_CATEGORY (h263_parse_debug);
40 #define GST_CAT_DEFAULT h263_parse_debug
41 
42 static GstStaticPadTemplate srctemplate =
43 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("video/x-h263, variant = (string) itu, "
46         "parsed = (boolean) true, framerate=(fraction)[0/1,MAX]")
47     );
48 
49 static GstStaticPadTemplate sinktemplate =
50 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("video/x-h263, variant = (string) itu")
53     );
54 
55 #define parent_class gst_h263_parse_parent_class
56 G_DEFINE_TYPE (GstH263Parse, gst_h263_parse, GST_TYPE_BASE_PARSE);
57 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (h263parse, "h263parse",
58     GST_RANK_PRIMARY + 1, GST_TYPE_H263_PARSE,
59     videoparsers_element_init (plugin));
60 
61 static gboolean gst_h263_parse_start (GstBaseParse * parse);
62 static gboolean gst_h263_parse_stop (GstBaseParse * parse);
63 static gboolean gst_h263_parse_sink_event (GstBaseParse * parse,
64     GstEvent * event);
65 static GstFlowReturn gst_h263_parse_handle_frame (GstBaseParse * parse,
66     GstBaseParseFrame * frame, gint * skipsize);
67 static GstFlowReturn gst_h263_parse_pre_push_frame (GstBaseParse * parse,
68     GstBaseParseFrame * frame);
69 static GstCaps *gst_h263_parse_get_sink_caps (GstBaseParse * parse,
70     GstCaps * filter);
71 
72 static void
gst_h263_parse_class_init(GstH263ParseClass * klass)73 gst_h263_parse_class_init (GstH263ParseClass * klass)
74 {
75   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
76   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
77 
78   GST_DEBUG_CATEGORY_INIT (h263_parse_debug, "h263parse", 0, "h263 parser");
79 
80   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
81   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
82   gst_element_class_set_static_metadata (gstelement_class, "H.263 parser",
83       "Codec/Parser/Video",
84       "Parses H.263 streams",
85       "Arun Raghavan <arun.raghavan@collabora.co.uk>,"
86       "Edward Hervey <edward.hervey@collabora.co.uk>");
87 
88   /* Override BaseParse vfuncs */
89   parse_class->start = GST_DEBUG_FUNCPTR (gst_h263_parse_start);
90   parse_class->stop = GST_DEBUG_FUNCPTR (gst_h263_parse_stop);
91   parse_class->sink_event = GST_DEBUG_FUNCPTR (gst_h263_parse_sink_event);
92   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_h263_parse_handle_frame);
93   parse_class->pre_push_frame =
94       GST_DEBUG_FUNCPTR (gst_h263_parse_pre_push_frame);
95   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_h263_parse_get_sink_caps);
96 }
97 
98 static void
gst_h263_parse_init(GstH263Parse * h263parse)99 gst_h263_parse_init (GstH263Parse * h263parse)
100 {
101   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (h263parse));
102   GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (h263parse));
103 }
104 
105 static gboolean
gst_h263_parse_start(GstBaseParse * parse)106 gst_h263_parse_start (GstBaseParse * parse)
107 {
108   GstH263Parse *h263parse = GST_H263_PARSE (parse);
109 
110   GST_DEBUG_OBJECT (h263parse, "start");
111 
112   h263parse->bitrate = 0;
113   h263parse->profile = -1;
114   h263parse->level = -1;
115 
116   h263parse->state = PARSING;
117 
118   h263parse->sent_codec_tag = FALSE;
119 
120   gst_base_parse_set_min_frame_size (parse, 4);
121 
122   return TRUE;
123 }
124 
125 static gboolean
gst_h263_parse_stop(GstBaseParse * parse)126 gst_h263_parse_stop (GstBaseParse * parse)
127 {
128   GST_DEBUG_OBJECT (parse, "stop");
129 
130   return TRUE;
131 }
132 
133 static gboolean
gst_h263_parse_sink_event(GstBaseParse * parse,GstEvent * event)134 gst_h263_parse_sink_event (GstBaseParse * parse, GstEvent * event)
135 {
136   GstH263Parse *h263parse;
137 
138   h263parse = GST_H263_PARSE (parse);
139 
140   switch (GST_EVENT_TYPE (event)) {
141     case GST_EVENT_TAG:
142     {
143       GstTagList *taglist;
144 
145       gst_event_parse_tag (event, &taglist);
146 
147       if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &h263parse->bitrate))
148         GST_DEBUG_OBJECT (h263parse, "got bitrate tag: %u", h263parse->bitrate);
149 
150       break;
151     }
152     default:
153       break;
154   }
155 
156   return GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
157 }
158 
159 static guint
find_psc(GstBuffer * buffer,guint skip)160 find_psc (GstBuffer * buffer, guint skip)
161 {
162   GstMapInfo map;
163   GstByteReader br;
164   guint psc_pos = -1, psc;
165 
166   gst_buffer_map (buffer, &map, GST_MAP_READ);
167   gst_byte_reader_init (&br, map.data, map.size);
168 
169   if (!gst_byte_reader_set_pos (&br, skip))
170     goto out;
171 
172   if (gst_byte_reader_peek_uint24_be (&br, &psc) == FALSE)
173     goto out;
174 
175   /* Scan for the picture start code (22 bits - 0x0020)
176    * startcode  : 0000 0000 0000 0000 1000 00xx
177    * mask (bin) : 1111 1111 1111 1111 1111 1100
178    * mask (hex) :    f    f    f    f    f    c
179    * match      :    0    0    0    0    8    0
180    */
181   while ((gst_byte_reader_get_remaining (&br) >= 3)) {
182     if (gst_byte_reader_peek_uint24_be (&br, &psc) &&
183         ((psc & 0xfffffc) == 0x000080)) {
184       psc_pos = gst_byte_reader_get_pos (&br);
185       break;
186     } else if (gst_byte_reader_skip (&br, 1) == FALSE)
187       break;
188   }
189 
190 out:
191   gst_buffer_unmap (buffer, &map);
192   return psc_pos;
193 }
194 
195 static void
gst_h263_parse_set_src_caps(GstH263Parse * h263parse,const H263Params * params)196 gst_h263_parse_set_src_caps (GstH263Parse * h263parse,
197     const H263Params * params)
198 {
199   GstStructure *st = NULL;
200   GstCaps *caps, *sink_caps;
201   gint fr_num, fr_denom, par_num, par_denom;
202 
203   g_assert (h263parse->state == PASSTHROUGH || h263parse->state == GOT_HEADER);
204 
205   caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (h263parse));
206   if (caps) {
207     caps = gst_caps_make_writable (caps);
208   } else {
209     caps = gst_caps_new_simple ("video/x-h263",
210         "variant", G_TYPE_STRING, "itu", NULL);
211   }
212   gst_caps_set_simple (caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
213 
214   sink_caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (h263parse));
215   if (sink_caps && (st = gst_caps_get_structure (sink_caps, 0)) &&
216       gst_structure_get_fraction (st, "framerate", &fr_num, &fr_denom)) {
217     /* Got it in caps - nothing more to do */
218     GST_DEBUG_OBJECT (h263parse, "sink caps override framerate from headers");
219   } else {
220     /* Caps didn't have the framerate - get it from params */
221     gst_h263_parse_get_framerate (params, &fr_num, &fr_denom);
222   }
223   gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fr_num, fr_denom,
224       NULL);
225 
226   if (params->width && params->height)
227     gst_caps_set_simple (caps, "width", G_TYPE_INT, params->width,
228         "height", G_TYPE_INT, params->height, NULL);
229 
230   if (st != NULL
231       && gst_structure_get_fraction (st, "pixel-aspect-ratio", &par_num,
232           &par_denom)) {
233     /* Got it in caps - nothing more to do */
234     GST_DEBUG_OBJECT (h263parse, "sink caps override PAR");
235   } else {
236     /* Caps didn't have the framerate - get it from params */
237     gst_h263_parse_get_par (params, &par_num, &par_denom);
238   }
239   gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
240       par_num, par_denom, NULL);
241 
242   if (h263parse->state == GOT_HEADER) {
243     gst_caps_set_simple (caps,
244         "annex-d", G_TYPE_BOOLEAN, (params->features & H263_OPTION_UMV_MODE),
245         "annex-e", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SAC_MODE),
246         "annex-f", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AP_MODE),
247         "annex-g", G_TYPE_BOOLEAN, (params->features & H263_OPTION_PB_MODE),
248         "annex-i", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIC_MODE),
249         "annex-j", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DF_MODE),
250         "annex-k", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SS_MODE),
251         "annex-m", G_TYPE_BOOLEAN, (params->type == PICTURE_IMPROVED_PB),
252         "annex-n", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RPS_MODE),
253         "annex-q", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RRU_MODE),
254         "annex-r", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ISD_MODE),
255         "annex-s", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIV_MODE),
256         "annex-t", G_TYPE_BOOLEAN, (params->features & H263_OPTION_MQ_MODE),
257         "annex-u", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ERPS_MODE),
258         "annex-v", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DPS_MODE),
259         NULL);
260 
261     h263parse->profile = gst_h263_parse_get_profile (params);
262     if (h263parse->profile != -1) {
263       gchar *profile_str;
264 
265       profile_str = g_strdup_printf ("%u", h263parse->profile);
266       gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile_str, NULL);
267       g_free (profile_str);
268     }
269 
270     h263parse->level = gst_h263_parse_get_level (params, h263parse->profile,
271         h263parse->bitrate, fr_num, fr_denom);
272     if (h263parse->level != -1) {
273       gchar *level_str;
274 
275       level_str = g_strdup_printf ("%u", h263parse->level);
276       gst_caps_set_simple (caps, "level", G_TYPE_STRING, level_str, NULL);
277       g_free (level_str);
278     }
279   }
280 
281   gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse)), caps);
282   gst_caps_unref (caps);
283   if (sink_caps)
284     gst_caps_unref (sink_caps);
285 }
286 
287 static GstFlowReturn
gst_h263_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)288 gst_h263_parse_handle_frame (GstBaseParse * parse,
289     GstBaseParseFrame * frame, gint * skipsize)
290 {
291   GstH263Parse *h263parse;
292   GstBuffer *buffer;
293   guint psc_pos, next_psc_pos;
294   gsize size;
295   H263Params params = { 0, };
296   GstFlowReturn res = GST_FLOW_OK;
297 
298   h263parse = GST_H263_PARSE (parse);
299   buffer = frame->buffer;
300   size = gst_buffer_get_size (buffer);
301 
302   if (size < 3) {
303     *skipsize = 1;
304     return GST_FLOW_OK;
305   }
306 
307   psc_pos = find_psc (buffer, 0);
308 
309   if (psc_pos == -1) {
310     /* PSC not found, need more data */
311     if (size > 3)
312       psc_pos = size - 3;
313     else
314       psc_pos = 0;
315     goto more;
316   }
317 
318   /* need to skip */
319   if (psc_pos > 0)
320     goto more;
321 
322   /* Found the start of the frame, now try to find the end */
323   next_psc_pos = psc_pos + 3;
324   next_psc_pos = find_psc (buffer, next_psc_pos);
325 
326   if (next_psc_pos == -1) {
327     if (GST_BASE_PARSE_DRAINING (parse))
328       /* FLUSH/EOS, it's okay if we can't find the next frame */
329       next_psc_pos = size;
330     else
331       goto more;
332   }
333 
334   /* We should now have a complete frame */
335 
336   /* If this is the first frame, parse and set srcpad caps */
337   if (h263parse->state == PARSING) {
338     res = gst_h263_parse_get_params (&params, buffer, FALSE, &h263parse->state);
339     if (res != GST_FLOW_OK || h263parse->state != GOT_HEADER) {
340       GST_WARNING ("Couldn't parse header - setting passthrough mode");
341       gst_base_parse_set_passthrough (parse, TRUE);
342     } else {
343       /* Set srcpad caps since we now have sufficient information to do so */
344       gst_h263_parse_set_src_caps (h263parse, &params);
345       gst_base_parse_set_passthrough (parse, FALSE);
346     }
347     memset (&params, 0, sizeof (params));
348   }
349 
350   /* XXX: After getting a keyframe, should we adjust min_frame_size to
351    * something smaller so we don't end up collecting too many non-keyframes? */
352 
353   GST_DEBUG_OBJECT (h263parse, "found a frame of size %u at pos %u",
354       next_psc_pos, psc_pos);
355 
356   res = gst_h263_parse_get_params (&params, buffer, TRUE, &h263parse->state);
357   if (res != GST_FLOW_OK)
358     goto more;
359 
360   if (h263parse->state == PASSTHROUGH || h263parse->state == PARSING) {
361     /* There's a feature we don't support, or we didn't have enough data to
362      * parse the header, which should not be possible. Either way, go into
363      * passthrough mode and let downstream handle it if it can. */
364     GST_WARNING ("Couldn't parse header - setting passthrough mode");
365     gst_base_parse_set_passthrough (parse, TRUE);
366     goto more;
367   }
368 
369   if (gst_h263_parse_is_delta_unit (&params))
370     GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
371   else
372     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
373 
374   return gst_base_parse_finish_frame (parse, frame, next_psc_pos);
375 
376 more:
377   *skipsize = psc_pos;
378 
379   return res;
380 }
381 
382 static void
remove_fields(GstCaps * caps)383 remove_fields (GstCaps * caps)
384 {
385   guint i, n;
386 
387   n = gst_caps_get_size (caps);
388   for (i = 0; i < n; i++) {
389     GstStructure *s = gst_caps_get_structure (caps, i);
390 
391     gst_structure_remove_field (s, "parsed");
392   }
393 }
394 
395 static GstCaps *
gst_h263_parse_get_sink_caps(GstBaseParse * parse,GstCaps * filter)396 gst_h263_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
397 {
398   GstCaps *peercaps, *templ;
399   GstCaps *res;
400 
401   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
402   if (filter) {
403     GstCaps *fcopy = gst_caps_copy (filter);
404     /* Remove the fields we convert */
405     remove_fields (fcopy);
406     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
407     gst_caps_unref (fcopy);
408   } else
409     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
410 
411   if (peercaps) {
412     /* Remove parsed field */
413     peercaps = gst_caps_make_writable (peercaps);
414     remove_fields (peercaps);
415 
416     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
417     gst_caps_unref (peercaps);
418     gst_caps_unref (templ);
419   } else {
420     res = templ;
421   }
422 
423   if (filter) {
424     GstCaps *intersection;
425 
426     intersection =
427         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
428     gst_caps_unref (res);
429     res = intersection;
430   }
431 
432   return res;
433 }
434 
435 static GstFlowReturn
gst_h263_parse_pre_push_frame(GstBaseParse * parse,GstBaseParseFrame * frame)436 gst_h263_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
437 {
438   GstH263Parse *h263parse = GST_H263_PARSE (parse);
439 
440   if (!h263parse->sent_codec_tag) {
441     GstTagList *taglist;
442     GstCaps *caps;
443 
444     /* codec tag */
445     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
446     if (G_UNLIKELY (caps == NULL)) {
447       if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
448         GST_INFO_OBJECT (parse, "Src pad is flushing");
449         return GST_FLOW_FLUSHING;
450       } else {
451         GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
452         return GST_FLOW_NOT_NEGOTIATED;
453       }
454     }
455 
456     taglist = gst_tag_list_new_empty ();
457     gst_pb_utils_add_codec_description_to_tag_list (taglist,
458         GST_TAG_VIDEO_CODEC, caps);
459     gst_caps_unref (caps);
460 
461     gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
462     gst_tag_list_unref (taglist);
463 
464     /* also signals the end of first-frame processing */
465     h263parse->sent_codec_tag = TRUE;
466   }
467 
468   return GST_FLOW_OK;
469 }
470