Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / videoparsers / gsth263parse.c
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., 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31
32 #include <gst/base/gstbytereader.h>
33 #include "gsth263parse.h"
34
35 GST_DEBUG_CATEGORY (h263_parse_debug);
36 #define GST_CAT_DEFAULT h263_parse_debug
37
38 static GstStaticPadTemplate srctemplate =
39 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("video/x-h263, variant = (string) itu, "
42         "parsed = (boolean) true, framerate=(fraction)[0/1,MAX]")
43     );
44
45 static GstStaticPadTemplate sinktemplate =
46 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/x-h263, variant = (string) itu")
49     );
50
51 GST_BOILERPLATE (GstH263Parse, gst_h263_parse, GstElement, GST_TYPE_BASE_PARSE);
52
53 static gboolean gst_h263_parse_start (GstBaseParse * parse);
54 static gboolean gst_h263_parse_stop (GstBaseParse * parse);
55 static gboolean gst_h263_parse_sink_event (GstBaseParse * parse,
56     GstEvent * event);
57 static gboolean gst_h263_parse_check_valid_frame (GstBaseParse * parse,
58     GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
59 static GstFlowReturn gst_h263_parse_parse_frame (GstBaseParse * parse,
60     GstBaseParseFrame * frame);
61 static GstCaps *gst_h263_parse_get_sink_caps (GstBaseParse * parse);
62
63 static void
64 gst_h263_parse_base_init (gpointer g_class)
65 {
66   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
67
68   gst_element_class_add_static_pad_template (gstelement_class,
69       &srctemplate);
70   gst_element_class_add_static_pad_template (gstelement_class,
71       &sinktemplate);
72   gst_element_class_set_details_simple (gstelement_class, "H.263 parser",
73       "Codec/Parser/Video",
74       "Parses H.263 streams",
75       "Arun Raghavan <arun.raghavan@collabora.co.uk>,"
76       "Edward Hervey <edward.hervey@collabora.co.uk>");
77
78   GST_DEBUG_CATEGORY_INIT (h263_parse_debug, "h263parse", 0, "h263 parser");
79 }
80
81 static void
82 gst_h263_parse_class_init (GstH263ParseClass * klass)
83 {
84   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
85
86   /* Override BaseParse vfuncs */
87   parse_class->start = GST_DEBUG_FUNCPTR (gst_h263_parse_start);
88   parse_class->stop = GST_DEBUG_FUNCPTR (gst_h263_parse_stop);
89   parse_class->event = GST_DEBUG_FUNCPTR (gst_h263_parse_sink_event);
90   parse_class->check_valid_frame =
91       GST_DEBUG_FUNCPTR (gst_h263_parse_check_valid_frame);
92   parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_h263_parse_parse_frame);
93   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_h263_parse_get_sink_caps);
94 }
95
96 static void
97 gst_h263_parse_init (GstH263Parse * h263parse, GstH263ParseClass * g_class)
98 {
99 }
100
101 static gboolean
102 gst_h263_parse_start (GstBaseParse * parse)
103 {
104   GstH263Parse *h263parse = GST_H263_PARSE (parse);
105
106   GST_DEBUG_OBJECT (h263parse, "start");
107
108   h263parse->bitrate = 0;
109   h263parse->profile = -1;
110   h263parse->level = -1;
111
112   h263parse->state = PARSING;
113
114   gst_base_parse_set_min_frame_size (parse, 4);
115
116   return TRUE;
117 }
118
119 static gboolean
120 gst_h263_parse_stop (GstBaseParse * parse)
121 {
122   GST_DEBUG_OBJECT (parse, "stop");
123
124   return TRUE;
125 }
126
127 static gboolean
128 gst_h263_parse_sink_event (GstBaseParse * parse, GstEvent * event)
129 {
130   GstH263Parse *h263parse;
131   gboolean res = FALSE;
132
133   h263parse = GST_H263_PARSE (parse);
134
135   switch (GST_EVENT_TYPE (event)) {
136     case GST_EVENT_TAG:
137     {
138       GstTagList *taglist;
139
140       gst_event_parse_tag (event, &taglist);
141
142       if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &h263parse->bitrate))
143         GST_DEBUG_OBJECT (h263parse, "got bitrate tag: %u", h263parse->bitrate);
144
145       break;
146     }
147
148     default:
149       break;
150   }
151
152   return res;
153 }
154
155 static guint
156 find_psc (GstBuffer * buffer, guint skip)
157 {
158   GstByteReader br;
159   guint psc_pos = -1, psc;
160
161   gst_byte_reader_init_from_buffer (&br, buffer);
162
163   if (!gst_byte_reader_set_pos (&br, skip))
164     goto out;
165
166   gst_byte_reader_peek_uint24_be (&br, &psc);
167
168   /* Scan for the picture start code (22 bits - 0x0020) */
169   while ((gst_byte_reader_get_remaining (&br) >= 3)) {
170     if (gst_byte_reader_peek_uint24_be (&br, &psc) &&
171         ((psc & 0xffffc0) == 0x000080)) {
172       psc_pos = gst_byte_reader_get_pos (&br);
173       break;
174     } else
175       gst_byte_reader_skip (&br, 1);
176   }
177
178 out:
179   return psc_pos;
180 }
181
182 static void
183 gst_h263_parse_set_src_caps (GstH263Parse * h263parse,
184     const H263Params * params)
185 {
186   GstStructure *st;
187   GstCaps *caps, *sink_caps;
188   gint fr_num, fr_denom;
189
190   g_assert (h263parse->state == PASSTHROUGH || h263parse->state == GOT_HEADER);
191
192   caps = GST_PAD_CAPS (GST_BASE_PARSE_SINK_PAD (h263parse));
193   if (caps) {
194     caps = gst_caps_copy (caps);
195   } else {
196     caps = gst_caps_new_simple ("video/x-h263",
197         "variant", G_TYPE_STRING, "itu", NULL);
198   }
199   gst_caps_set_simple (caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
200
201   sink_caps = GST_PAD_CAPS (GST_BASE_PARSE_SINK_PAD (h263parse));
202   if (sink_caps && (st = gst_caps_get_structure (sink_caps, 0)) &&
203       gst_structure_get_fraction (st, "framerate", &fr_num, &fr_denom)) {
204     /* Got it in caps - nothing more to do */
205     GST_DEBUG_OBJECT (h263parse, "sink caps override framerate from headers");
206   } else {
207     /* Caps didn't have the framerate - get it from params */
208     gst_h263_parse_get_framerate (params, &fr_num, &fr_denom);
209   }
210   gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fr_num, fr_denom,
211       NULL);
212
213   if (params->width && params->height)
214     gst_caps_set_simple (caps, "width", G_TYPE_INT, params->width,
215         "height", G_TYPE_INT, params->height, NULL);
216
217   if (h263parse->state == GOT_HEADER) {
218     gst_caps_set_simple (caps,
219         "annex-d", G_TYPE_BOOLEAN, (params->features & H263_OPTION_UMV_MODE),
220         "annex-e", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SAC_MODE),
221         "annex-f", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AP_MODE),
222         "annex-g", G_TYPE_BOOLEAN, (params->features & H263_OPTION_PB_MODE),
223         "annex-i", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIC_MODE),
224         "annex-j", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DF_MODE),
225         "annex-k", G_TYPE_BOOLEAN, (params->features & H263_OPTION_SS_MODE),
226         "annex-m", G_TYPE_BOOLEAN, (params->type == PICTURE_IMPROVED_PB),
227         "annex-n", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RPS_MODE),
228         "annex-q", G_TYPE_BOOLEAN, (params->features & H263_OPTION_RRU_MODE),
229         "annex-r", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ISD_MODE),
230         "annex-s", G_TYPE_BOOLEAN, (params->features & H263_OPTION_AIV_MODE),
231         "annex-t", G_TYPE_BOOLEAN, (params->features & H263_OPTION_MQ_MODE),
232         "annex-u", G_TYPE_BOOLEAN, (params->features & H263_OPTION_ERPS_MODE),
233         "annex-v", G_TYPE_BOOLEAN, (params->features & H263_OPTION_DPS_MODE),
234         NULL);
235
236     h263parse->profile = gst_h263_parse_get_profile (params);
237     if (h263parse->profile != -1)
238       gst_caps_set_simple (caps, "profile", G_TYPE_UINT, h263parse->profile,
239           NULL);
240
241     h263parse->level = gst_h263_parse_get_level (params, h263parse->profile,
242         h263parse->bitrate, fr_num, fr_denom);
243     if (h263parse->level != -1)
244       gst_caps_set_simple (caps, "level", G_TYPE_UINT, h263parse->level, NULL);
245   }
246
247   gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse)), caps);
248   gst_caps_unref (caps);
249 }
250
251 static gboolean
252 gst_h263_parse_check_valid_frame (GstBaseParse * parse,
253     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
254 {
255   GstH263Parse *h263parse;
256   GstBuffer *buffer;
257   guint psc_pos, next_psc_pos;
258
259   h263parse = GST_H263_PARSE (parse);
260   buffer = frame->buffer;
261
262   if (GST_BUFFER_SIZE (buffer) < 3)
263     return FALSE;
264
265   psc_pos = find_psc (buffer, 0);
266
267   if (psc_pos == -1) {
268     /* PSC not found, need more data */
269     if (GST_BUFFER_SIZE (buffer) > 3)
270       psc_pos = GST_BUFFER_SIZE (buffer) - 3;
271     else
272       psc_pos = 0;
273     goto more;
274   }
275
276   /* Found the start of the frame, now try to find the end */
277   next_psc_pos = psc_pos + 3;
278   next_psc_pos = find_psc (buffer, next_psc_pos);
279
280   if (next_psc_pos == -1) {
281     if (GST_BASE_PARSE_DRAINING (parse))
282       /* FLUSH/EOS, it's okay if we can't find the next frame */
283       next_psc_pos = GST_BUFFER_SIZE (buffer);
284     else
285       goto more;
286   }
287
288   /* We should now have a complete frame */
289
290   /* If this is the first frame, parse and set srcpad caps */
291   if (h263parse->state == PARSING) {
292     H263Params params = { 0, };
293     GstFlowReturn res;
294
295     res = gst_h263_parse_get_params (&params, buffer, FALSE, &h263parse->state);
296     if (res != GST_FLOW_OK || h263parse->state != GOT_HEADER) {
297       GST_WARNING ("Couldn't parse header - setting passthrough mode");
298       gst_base_parse_set_passthrough (parse, TRUE);
299     } else {
300       /* Set srcpad caps since we now have sufficient information to do so */
301       gst_h263_parse_set_src_caps (h263parse, &params);
302       gst_base_parse_set_passthrough (parse, FALSE);
303     }
304   }
305
306   *skipsize = psc_pos;
307   *framesize = next_psc_pos - psc_pos;
308
309   /* XXX: After getting a keyframe, should we adjust min_frame_size to
310    * something smaller so we don't end up collecting too many non-keyframes? */
311
312   GST_DEBUG_OBJECT (h263parse, "found a frame of size %d at pos %d",
313       *framesize, *skipsize);
314
315   return TRUE;
316
317 more:
318   /* ask for best next available */
319   *framesize = G_MAXUINT;
320
321   *skipsize = psc_pos;
322
323   return FALSE;
324 }
325
326 static GstFlowReturn
327 gst_h263_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
328 {
329   GstH263Parse *h263parse;
330   GstBuffer *buffer;
331   GstFlowReturn res;
332   H263Params params = { 0, };
333
334   h263parse = GST_H263_PARSE (parse);
335   buffer = frame->buffer;
336
337   res = gst_h263_parse_get_params (&params, buffer, TRUE, &h263parse->state);
338   if (res != GST_FLOW_OK)
339     goto out;
340
341   if (h263parse->state == PASSTHROUGH || h263parse->state == PARSING) {
342     /* There's a feature we don't support, or we didn't have enough data to
343      * parse the header, which should not be possible. Either way, go into
344      * passthrough mode and let downstream handle it if it can. */
345     GST_WARNING ("Couldn't parse header - setting passthrough mode");
346     gst_base_parse_set_passthrough (parse, TRUE);
347     goto out;
348   }
349
350   /* h263parse->state is now GOT_HEADER */
351
352 #ifdef GST_H263PARSE_MODIFICATION /* do not set caps in every frame */
353   if (buffer != NULL && GST_BUFFER_CAPS (buffer) == NULL) {
354     GST_WARNING_OBJECT (h263parse, "buffer caps is null. gst_buffer_set_caps from src_caps");
355     gst_buffer_set_caps (buffer,
356         GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse))));
357   }
358 #else
359   gst_buffer_set_caps (buffer,
360       GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (h263parse))));
361 #endif
362
363   if (gst_h263_parse_is_delta_unit (&params))
364     GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
365   else
366     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
367
368 out:
369
370   return res;
371 }
372
373 static GstCaps *
374 gst_h263_parse_get_sink_caps (GstBaseParse * parse)
375 {
376   GstCaps *peercaps;
377   GstCaps *res;
378
379   peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
380   if (peercaps) {
381     guint i, n;
382
383     /* Remove parsed field */
384     peercaps = gst_caps_make_writable (peercaps);
385     n = gst_caps_get_size (peercaps);
386     for (i = 0; i < n; i++) {
387       GstStructure *s = gst_caps_get_structure (peercaps, i);
388       gst_structure_remove_field (s, "parsed");
389     }
390
391     res =
392         gst_caps_intersect_full (peercaps,
393         gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse)),
394         GST_CAPS_INTERSECT_FIRST);
395     gst_caps_unref (peercaps);
396   } else {
397     res =
398         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
399             (parse)));
400   }
401
402   return res;
403 }