Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / sys / vdpau / h264 / gsth264parser.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <string.h>
22
23 #include "gstnalreader.h"
24
25 #include "gsth264parser.h"
26
27 /* default scaling_lists according to Table 7-2 */
28 const guint8 default_4x4_intra[16] =
29     { 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
30   32, 37, 37, 42
31 };
32
33 const guint8 default_4x4_inter[16] =
34     { 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
35   27, 30, 30, 34
36 };
37
38 const guint8 default_8x8_intra[64] =
39     { 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
40   18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
41   27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
42   33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
43 };
44
45 const guint8 default_8x8_inter[64] =
46     { 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
47   19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
48   24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
49   28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
50 };
51
52 const guint8 zigzag_8x8[64] = {
53   0, 1, 8, 16, 9, 2, 3, 10,
54   17, 24, 32, 25, 18, 11, 4, 5,
55   12, 19, 26, 33, 40, 48, 41, 34,
56   27, 20, 13, 6, 7, 14, 21, 28,
57   35, 42, 49, 56, 57, 50, 43, 36,
58   29, 22, 15, 23, 30, 37, 44, 51,
59   58, 59, 52, 45, 38, 31, 39, 46,
60   53, 60, 61, 54, 47, 55, 62, 63
61 };
62
63 const guint8 zigzag_4x4[16] = {
64   0, 1, 4, 8,
65   5, 2, 3, 6,
66   9, 12, 13, 10,
67   7, 11, 14, 15,
68 };
69
70 #define CHECK_ALLOWED(val, min, max) { \
71   if (val < min || val > max) { \
72     GST_WARNING ("value not in allowed range. value: %d, range %d-%d", \
73                      val, min, max); \
74     goto error; \
75   } \
76 }
77
78 #define READ_UINT8(reader, val, nbits) { \
79   if (!gst_nal_reader_get_bits_uint8 (reader, &val, nbits)) { \
80     GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
81     goto error; \
82   } \
83 }
84
85 #define READ_UINT16(reader, val, nbits) { \
86   if (!gst_nal_reader_get_bits_uint16 (reader, &val, nbits)) { \
87   GST_WARNING ("failed to read uint16, nbits: %d", nbits); \
88     goto error; \
89   } \
90 }
91
92 #define READ_UINT32(reader, val, nbits) { \
93   if (!gst_nal_reader_get_bits_uint32 (reader, &val, nbits)) { \
94   GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
95     goto error; \
96   } \
97 }
98
99 #define READ_UINT64(reader, val, nbits) { \
100   if (!gst_nal_reader_get_bits_uint64 (reader, &val, nbits)) { \
101     GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
102     goto error; \
103   } \
104 }
105
106 #define READ_UE(reader, val) { \
107   if (!gst_nal_reader_get_ue (reader, &val)) { \
108     GST_WARNING ("failed to read UE"); \
109     goto error; \
110   } \
111 }
112
113 #define READ_UE_ALLOWED(reader, val, min, max) { \
114   guint32 tmp; \
115   READ_UE (reader, tmp); \
116   CHECK_ALLOWED (tmp, min, max); \
117   val = tmp; \
118 }
119
120 #define READ_SE(reader, val) { \
121   if (!gst_nal_reader_get_se (reader, &val)) { \
122     GST_WARNING ("failed to read SE"); \
123     goto error; \
124   } \
125 }
126
127 #define READ_SE_ALLOWED(reader, val, min, max) { \
128   gint32 tmp; \
129   READ_SE (reader, tmp); \
130   CHECK_ALLOWED (tmp, min, max); \
131   val = tmp; \
132 }
133
134 GST_DEBUG_CATEGORY_STATIC (h264parser_debug);
135 #define GST_CAT_DEFAULT h264parser_debug
136
137 #define _do_init \
138     GST_DEBUG_CATEGORY_INIT (h264parser_debug, "h264parser", 0, \
139     "H264 parser");
140
141 G_DEFINE_TYPE_WITH_CODE (GstH264Parser, gst_h264_parser, G_TYPE_OBJECT,
142     _do_init);
143
144 static void
145 gst_h264_sequence_free (void *data)
146 {
147   g_slice_free (GstH264Sequence, data);
148 }
149
150 static gboolean
151 gst_h264_parse_hrd_parameters (GstH264HRDParameters * hrd,
152     GstNalReader * reader)
153 {
154   guint SchedSelIdx;
155
156   GST_DEBUG ("parsing \"HRD Parameters\"");
157
158   READ_UE_ALLOWED (reader, hrd->cpb_cnt_minus1, 0, 31);
159   READ_UINT8 (reader, hrd->bit_rate_scale, 4);
160   READ_UINT8 (reader, hrd->cpb_size_scale, 4);
161
162   for (SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
163     READ_UE (reader, hrd->bit_rate_value_minus1[SchedSelIdx]);
164     READ_UE (reader, hrd->cpb_size_value_minus1[SchedSelIdx]);
165   }
166
167   READ_UINT8 (reader, hrd->initial_cpb_removal_delay_length_minus1, 5);
168   READ_UINT8 (reader, hrd->cpb_removal_delay_length_minus1, 5);
169   READ_UINT8 (reader, hrd->dpb_output_delay_length_minus1, 5);
170   READ_UINT8 (reader, hrd->time_offset_length, 5);
171
172   return TRUE;
173
174 error:
175   GST_WARNING ("error parsing \"HRD Parameters\"");
176   return FALSE;
177
178 }
179
180 static gboolean
181 gst_h264_parse_vui_parameters (GstH264VUIParameters * vui,
182     GstNalReader * reader)
183 {
184   guint8 aspect_ratio_info_present_flag;
185   guint8 video_signal_type_present_flag;
186   guint8 chroma_loc_info_present_flag;
187
188   GST_DEBUG ("parsing \"VUI Parameters\"");
189
190   /* set default values for fields that might not be present in the bitstream
191      and have valid defaults */
192   vui->aspect_ratio_idc = 0;
193   vui->video_format = 5;
194   vui->video_full_range_flag = 0;
195   vui->colour_primaries = 2;
196   vui->transfer_characteristics = 2;
197   vui->matrix_coefficients = 2;
198   vui->chroma_sample_loc_type_top_field = 0;
199   vui->chroma_sample_loc_type_bottom_field = 0;
200   vui->low_delay_hrd_flag = 0;
201
202   READ_UINT8 (reader, aspect_ratio_info_present_flag, 1);
203   if (aspect_ratio_info_present_flag) {
204     READ_UINT8 (reader, vui->aspect_ratio_idc, 8);
205     if (vui->aspect_ratio_idc == 255) {
206       READ_UINT16 (reader, vui->sar_width, 16);
207       READ_UINT16 (reader, vui->sar_height, 16);
208     }
209   }
210
211   READ_UINT8 (reader, vui->overscan_info_present_flag, 1);
212   if (vui->overscan_info_present_flag)
213     READ_UINT8 (reader, vui->overscan_appropriate_flag, 1);
214
215   READ_UINT8 (reader, video_signal_type_present_flag, 1);
216   if (video_signal_type_present_flag) {
217     guint8 colour_description_present_flag;
218
219     READ_UINT8 (reader, vui->video_format, 3);
220     READ_UINT8 (reader, vui->video_full_range_flag, 1);
221     READ_UINT8 (reader, colour_description_present_flag, 1);
222     if (colour_description_present_flag) {
223       READ_UINT8 (reader, vui->colour_primaries, 8);
224       READ_UINT8 (reader, vui->transfer_characteristics, 8);
225       READ_UINT8 (reader, vui->matrix_coefficients, 8);
226     }
227   }
228
229   READ_UINT8 (reader, chroma_loc_info_present_flag, 1);
230   if (chroma_loc_info_present_flag) {
231     READ_UE_ALLOWED (reader, vui->chroma_sample_loc_type_top_field, 0, 5);
232     READ_UE_ALLOWED (reader, vui->chroma_sample_loc_type_bottom_field, 0, 5);
233   }
234
235   READ_UINT8 (reader, vui->timing_info_present_flag, 1);
236   if (vui->timing_info_present_flag) {
237     READ_UINT32 (reader, vui->num_units_in_tick, 32);
238     if (vui->num_units_in_tick == 0)
239       GST_WARNING
240           ("num_units_in_tick = 0 detected in stream (incompliant to H.264 E.2.1).");
241
242     READ_UINT32 (reader, vui->time_scale, 32);
243     if (vui->time_scale == 0)
244       GST_WARNING
245           ("time_scale = 0 detected in stream (incompliant to H.264 E.2.1).");
246
247     READ_UINT8 (reader, vui->fixed_frame_rate_flag, 1);
248   }
249
250   READ_UINT8 (reader, vui->nal_hrd_parameters_present_flag, 1);
251   if (vui->nal_hrd_parameters_present_flag) {
252     if (!gst_h264_parse_hrd_parameters (&vui->nal_hrd_parameters, reader))
253       goto error;
254   }
255
256   READ_UINT8 (reader, vui->vcl_hrd_parameters_present_flag, 1);
257   if (vui->vcl_hrd_parameters_present_flag) {
258     if (!gst_h264_parse_hrd_parameters (&vui->vcl_hrd_parameters, reader))
259       goto error;
260   }
261
262   if (vui->nal_hrd_parameters_present_flag ||
263       vui->vcl_hrd_parameters_present_flag)
264     READ_UINT8 (reader, vui->low_delay_hrd_flag, 1);
265
266   READ_UINT8 (reader, vui->pic_struct_present_flag, 1);
267
268   return TRUE;
269
270 error:
271   GST_WARNING ("error parsing \"VUI Parameters\"");
272   return FALSE;
273 }
274
275 static gboolean
276 gst_h264_parser_parse_scaling_list (GstNalReader * reader,
277     guint8 scaling_lists_4x4[6][16], guint8 scaling_lists_8x8[6][64],
278     const guint8 fallback_4x4_inter[16], const guint8 fallback_4x4_intra[16],
279     const guint8 fallback_8x8_inter[64], const guint8 fallback_8x8_intra[64],
280     guint8 n_lists)
281 {
282   guint i;
283
284   GST_DEBUG ("parsing scaling lists");
285
286   for (i = 0; i < 12; i++) {
287     gboolean use_default = FALSE;
288
289     if (i < n_lists) {
290       guint8 scaling_list_present_flag;
291
292       READ_UINT8 (reader, scaling_list_present_flag, 1);
293       if (scaling_list_present_flag) {
294         guint8 *scaling_list;
295         const guint8 *scan;
296         guint size;
297         guint j;
298         guint8 last_scale, next_scale;
299
300         if (i < 6) {
301           scaling_list = scaling_lists_4x4[i];
302           scan = zigzag_4x4;
303           size = 16;
304         } else {
305           scaling_list = scaling_lists_8x8[i - 6];
306           scan = zigzag_8x8;
307           size = 64;
308         }
309
310         last_scale = 8;
311         next_scale = 8;
312         for (j = 0; j < size; j++) {
313           if (next_scale != 0) {
314             gint32 delta_scale;
315
316             READ_SE (reader, delta_scale);
317             next_scale = (last_scale + delta_scale) & 0xff;
318           }
319           if (j == 0 && next_scale == 0) {
320             use_default = TRUE;
321             break;
322           }
323           last_scale = scaling_list[scan[j]] =
324               (next_scale == 0) ? last_scale : next_scale;
325         }
326       } else
327         use_default = TRUE;
328     } else
329       use_default = TRUE;
330
331     if (use_default) {
332       switch (i) {
333         case 0:
334           memcpy (scaling_lists_4x4[0], fallback_4x4_intra, 16);
335           break;
336         case 1:
337           memcpy (scaling_lists_4x4[1], scaling_lists_4x4[0], 16);
338           break;
339         case 2:
340           memcpy (scaling_lists_4x4[2], scaling_lists_4x4[1], 16);
341           break;
342         case 3:
343           memcpy (scaling_lists_4x4[3], fallback_4x4_inter, 16);
344           break;
345         case 4:
346           memcpy (scaling_lists_4x4[4], scaling_lists_4x4[3], 16);
347           break;
348         case 5:
349           memcpy (scaling_lists_4x4[5], scaling_lists_4x4[4], 16);
350           break;
351         case 6:
352           memcpy (scaling_lists_8x8[0], fallback_8x8_intra, 64);
353           break;
354         case 7:
355           memcpy (scaling_lists_8x8[1], fallback_8x8_inter, 64);
356           break;
357         case 8:
358           memcpy (scaling_lists_8x8[2], scaling_lists_8x8[0], 64);
359           break;
360         case 9:
361           memcpy (scaling_lists_8x8[3], scaling_lists_8x8[1], 64);
362           break;
363         case 10:
364           memcpy (scaling_lists_8x8[4], scaling_lists_8x8[2], 64);
365           break;
366         case 11:
367           memcpy (scaling_lists_8x8[5], scaling_lists_8x8[3], 64);
368           break;
369
370         default:
371           break;
372       }
373     }
374   }
375
376   return TRUE;
377
378 error:
379
380   GST_WARNING ("error parsing scaling lists");
381   return FALSE;
382 }
383
384 GstH264Sequence *
385 gst_h264_parser_parse_sequence (GstH264Parser * parser, guint8 * data,
386     guint size)
387 {
388   GstNalReader reader = GST_NAL_READER_INIT (data, size);
389   GstH264Sequence *seq;
390   guint8 frame_cropping_flag;
391
392   g_return_val_if_fail (GST_IS_H264_PARSER (parser), NULL);
393   g_return_val_if_fail (data != NULL, NULL);
394   g_return_val_if_fail (size > 0, NULL);
395
396   GST_DEBUG ("parsing \"Sequence parameter set\"");
397
398   seq = g_slice_new (GstH264Sequence);
399
400   /* set default values for fields that might not be present in the bitstream
401      and have valid defaults */
402   seq->chroma_format_idc = 1;
403   seq->separate_colour_plane_flag = 0;
404   seq->bit_depth_luma_minus8 = 0;
405   seq->bit_depth_chroma_minus8 = 0;
406   memset (seq->scaling_lists_4x4, 16, 96);
407   memset (seq->scaling_lists_8x8, 16, 384);
408   seq->mb_adaptive_frame_field_flag = 0;
409   seq->frame_crop_left_offset = 0;
410   seq->frame_crop_right_offset = 0;
411   seq->frame_crop_top_offset = 0;
412   seq->frame_crop_bottom_offset = 0;
413
414   READ_UINT8 (&reader, seq->profile_idc, 8);
415   READ_UINT8 (&reader, seq->constraint_set0_flag, 1);
416   READ_UINT8 (&reader, seq->constraint_set1_flag, 1);
417   READ_UINT8 (&reader, seq->constraint_set2_flag, 1);
418   READ_UINT8 (&reader, seq->constraint_set3_flag, 1);
419
420   /* skip reserved_zero_4bits */
421   if (!gst_nal_reader_skip (&reader, 4))
422     goto error;
423
424   READ_UINT8 (&reader, seq->level_idc, 8);
425
426   READ_UE_ALLOWED (&reader, seq->id, 0, 31);
427
428   if (seq->profile_idc == 100 || seq->profile_idc == 110 ||
429       seq->profile_idc == 122 || seq->profile_idc == 244 ||
430       seq->profile_idc == 44 || seq->profile_idc == 83 ||
431       seq->profile_idc == 86) {
432     READ_UE_ALLOWED (&reader, seq->chroma_format_idc, 0, 3);
433     if (seq->chroma_format_idc == 3)
434       READ_UINT8 (&reader, seq->separate_colour_plane_flag, 1);
435
436     READ_UE_ALLOWED (&reader, seq->bit_depth_luma_minus8, 0, 6);
437     READ_UE_ALLOWED (&reader, seq->bit_depth_chroma_minus8, 0, 6);
438     READ_UINT8 (&reader, seq->qpprime_y_zero_transform_bypass_flag, 1);
439
440     READ_UINT8 (&reader, seq->scaling_matrix_present_flag, 1);
441     if (seq->scaling_matrix_present_flag) {
442       guint8 n_lists;
443
444       n_lists = (seq->chroma_format_idc != 3) ? 8 : 12;
445       if (!gst_h264_parser_parse_scaling_list (&reader,
446               seq->scaling_lists_4x4, seq->scaling_lists_8x8,
447               default_4x4_inter, default_4x4_intra,
448               default_8x8_inter, default_8x8_intra, n_lists))
449         goto error;
450     }
451   }
452
453   READ_UE_ALLOWED (&reader, seq->log2_max_frame_num_minus4, 0, 12);
454   /* calculate MaxFrameNum */
455   seq->MaxFrameNum = 1 << (seq->log2_max_frame_num_minus4 + 4);
456
457   READ_UE_ALLOWED (&reader, seq->pic_order_cnt_type, 0, 2);
458   if (seq->pic_order_cnt_type == 0) {
459     READ_UE_ALLOWED (&reader, seq->log2_max_pic_order_cnt_lsb_minus4, 0, 12);
460   } else if (seq->pic_order_cnt_type == 1) {
461     guint i;
462
463     READ_UINT8 (&reader, seq->delta_pic_order_always_zero_flag, 1);
464     READ_SE (&reader, seq->offset_for_non_ref_pic);
465     READ_SE (&reader, seq->offset_for_top_to_bottom_field);
466     READ_UE_ALLOWED (&reader, seq->num_ref_frames_in_pic_order_cnt_cycle, 0,
467         255);
468     for (i = 0; i < seq->num_ref_frames_in_pic_order_cnt_cycle; i++)
469       READ_SE (&reader, seq->offset_for_ref_frame[i]);
470   }
471
472   READ_UE (&reader, seq->num_ref_frames);
473   READ_UINT8 (&reader, seq->gaps_in_frame_num_value_allowed_flag, 1);
474   READ_UE (&reader, seq->pic_width_in_mbs_minus1);
475   READ_UE (&reader, seq->pic_height_in_map_units_minus1);
476   READ_UINT8 (&reader, seq->frame_mbs_only_flag, 1);
477
478   if (!seq->frame_mbs_only_flag)
479     READ_UINT8 (&reader, seq->mb_adaptive_frame_field_flag, 1);
480
481   READ_UINT8 (&reader, seq->direct_8x8_inference_flag, 1);
482   READ_UINT8 (&reader, frame_cropping_flag, 1);
483   if (frame_cropping_flag) {
484     READ_UE (&reader, seq->frame_crop_left_offset);
485     READ_UE (&reader, seq->frame_crop_right_offset);
486     READ_UE (&reader, seq->frame_crop_top_offset);
487     READ_UE (&reader, seq->frame_crop_bottom_offset);
488   }
489
490   READ_UINT8 (&reader, seq->vui_parameters_present_flag, 1);
491   if (seq->vui_parameters_present_flag) {
492     if (!gst_h264_parse_vui_parameters (&seq->vui_parameters, &reader))
493       goto error;
494   }
495
496   /* calculate ChromaArrayType */
497   if (seq->separate_colour_plane_flag)
498     seq->ChromaArrayType = 0;
499   else
500     seq->ChromaArrayType = seq->chroma_format_idc;
501
502   GST_DEBUG ("adding sequence parameter set with id: %d to hash table",
503       seq->id);
504   g_hash_table_replace (parser->sequences, &seq->id, seq);
505   return seq;
506
507 error:
508   GST_WARNING ("error parsing \"Sequence parameter set\"");
509
510   gst_h264_sequence_free (seq);
511   return NULL;
512 }
513
514 static void
515 gst_h264_picture_free (void *data)
516 {
517   GstH264Picture *pic = (GstH264Picture *) data;
518
519   if (pic->slice_group_id)
520     g_free (pic->slice_group_id);
521
522   g_slice_free (GstH264Picture, data);
523 }
524
525 static gboolean
526 gst_h264_parser_more_data (GstNalReader * reader)
527 {
528   guint remaining;
529
530   remaining = gst_nal_reader_get_remaining (reader);
531   if (remaining == 0)
532     return FALSE;
533
534   if (remaining <= 8) {
535     guint8 rbsp_stop_one_bit;
536
537     if (!gst_nal_reader_peek_bits_uint8 (reader, &rbsp_stop_one_bit, 1))
538       return FALSE;
539
540     if (rbsp_stop_one_bit == 1) {
541       guint8 zero_bits;
542
543       if (remaining == 1)
544         return FALSE;
545
546       if (!gst_nal_reader_peek_bits_uint8 (reader, &zero_bits, remaining))
547         return FALSE;
548
549       if ((zero_bits - (1 << (remaining - 1))) == 0)
550         return FALSE;
551     }
552   }
553
554   return TRUE;
555 }
556
557 GstH264Picture *
558 gst_h264_parser_parse_picture (GstH264Parser * parser, guint8 * data,
559     guint size)
560 {
561   GstNalReader reader = GST_NAL_READER_INIT (data, size);
562   GstH264Picture *pic;
563   gint seq_parameter_set_id;
564   GstH264Sequence *seq;
565   guint8 pic_scaling_matrix_present_flag;
566
567   g_return_val_if_fail (GST_IS_H264_PARSER (parser), NULL);
568   g_return_val_if_fail (data != NULL, NULL);
569   g_return_val_if_fail (size > 0, NULL);
570
571   GST_DEBUG ("parsing \"Picture parameter set\"");
572
573   pic = g_slice_new (GstH264Picture);
574
575   READ_UE_ALLOWED (&reader, pic->id, 0, 255);
576   READ_UE_ALLOWED (&reader, seq_parameter_set_id, 0, 31);
577   seq = g_hash_table_lookup (parser->sequences, &seq_parameter_set_id);
578   if (!seq) {
579     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
580         seq_parameter_set_id);
581     goto error;
582   }
583   pic->sequence = seq;
584
585   /* set default values for fields that might not be present in the bitstream
586      and have valid defaults */
587   pic->slice_group_id = NULL;
588   pic->transform_8x8_mode_flag = 0;
589   memcpy (&pic->scaling_lists_4x4, &seq->scaling_lists_4x4, 96);
590   memcpy (&pic->scaling_lists_8x8, &seq->scaling_lists_8x8, 384);
591
592   READ_UINT8 (&reader, pic->entropy_coding_mode_flag, 1);
593   READ_UINT8 (&reader, pic->pic_order_present_flag, 1);
594   READ_UE_ALLOWED (&reader, pic->num_slice_groups_minus1, 0, 7);
595   if (pic->num_slice_groups_minus1 > 0) {
596     READ_UE_ALLOWED (&reader, pic->slice_group_map_type, 0, 6);
597     if (pic->slice_group_map_type == 0) {
598       gint i;
599
600       for (i = 0; i <= pic->num_slice_groups_minus1; i++)
601         READ_UE (&reader, pic->run_length_minus1[i]);
602     } else if (pic->slice_group_map_type == 2) {
603       gint i;
604
605       for (i = 0; i <= pic->num_slice_groups_minus1; i++) {
606         READ_UE (&reader, pic->top_left[i]);
607         READ_UE (&reader, pic->bottom_right[i]);
608       }
609     } else if (pic->slice_group_map_type >= 3 && pic->slice_group_map_type <= 5) {
610       READ_UINT8 (&reader, pic->slice_group_change_direction_flag, 1);
611       READ_UE (&reader, pic->slice_group_change_rate_minus1);
612     } else if (pic->slice_group_map_type == 6) {
613       gint bits;
614       gint i;
615
616       READ_UE (&reader, pic->pic_size_in_map_units_minus1);
617       bits = g_bit_storage (pic->num_slice_groups_minus1);
618
619       pic->slice_group_id =
620           g_new (guint8, pic->pic_size_in_map_units_minus1 + 1);
621       for (i = 0; i <= pic->pic_size_in_map_units_minus1; i++)
622         READ_UINT8 (&reader, pic->slice_group_id[i], bits);
623     }
624   }
625
626   READ_UE_ALLOWED (&reader, pic->num_ref_idx_l0_active_minus1, 0, 31);
627   READ_UE_ALLOWED (&reader, pic->num_ref_idx_l1_active_minus1, 0, 31);
628   READ_UINT8 (&reader, pic->weighted_pred_flag, 1);
629   READ_UINT8 (&reader, pic->weighted_bipred_idc, 2);
630   READ_SE_ALLOWED (&reader, pic->pic_init_qp_minus26, -26, 25);
631   READ_SE_ALLOWED (&reader, pic->pic_init_qs_minus26, -26, 25);
632   READ_SE_ALLOWED (&reader, pic->chroma_qp_index_offset, -12, 12);
633   pic->second_chroma_qp_index_offset = pic->chroma_qp_index_offset;
634   READ_UINT8 (&reader, pic->deblocking_filter_control_present_flag, 1);
635   READ_UINT8 (&reader, pic->constrained_intra_pred_flag, 1);
636   READ_UINT8 (&reader, pic->redundant_pic_cnt_present_flag, 1);
637
638   if (!gst_h264_parser_more_data (&reader))
639     goto done;
640
641   READ_UINT8 (&reader, pic->transform_8x8_mode_flag, 1);
642
643   READ_UINT8 (&reader, pic_scaling_matrix_present_flag, 1);
644   if (pic_scaling_matrix_present_flag) {
645     guint8 n_lists;
646
647     n_lists = 6 + ((seq->chroma_format_idc != 3) ? 2 : 6) *
648         pic->transform_8x8_mode_flag;
649
650     if (seq->scaling_matrix_present_flag) {
651       if (!gst_h264_parser_parse_scaling_list (&reader,
652               pic->scaling_lists_4x4, pic->scaling_lists_8x8,
653               seq->scaling_lists_4x4[0], seq->scaling_lists_4x4[3],
654               seq->scaling_lists_8x8[0], seq->scaling_lists_8x8[3], n_lists))
655         goto error;
656     } else {
657       if (!gst_h264_parser_parse_scaling_list (&reader,
658               pic->scaling_lists_4x4, pic->scaling_lists_8x8,
659               default_4x4_inter, default_4x4_intra,
660               default_8x8_inter, default_8x8_intra, n_lists))
661         goto error;
662     }
663   }
664
665   READ_SE_ALLOWED (&reader, pic->second_chroma_qp_index_offset, -12, 12);
666
667 done:
668   GST_DEBUG ("adding picture parameter set with id: %d to hash table", pic->id);
669   g_hash_table_replace (parser->pictures, &pic->id, pic);
670   return pic;
671
672 error:
673   GST_WARNING ("error parsing \"Picture parameter set\"");
674
675   gst_h264_picture_free (pic);
676   return NULL;
677 }
678
679 static gboolean
680 gst_h264_slice_parse_pred_weight_table (GstH264Slice * slice,
681     GstNalReader * reader,
682     const GstH264Sequence * seq, const GstH264Picture * pic)
683 {
684   GstH264PredWeightTable *p;
685   gint i;
686
687   GST_DEBUG ("parsing \"Prediction weight table\"");
688
689   p = &slice->pred_weight_table;
690
691   READ_UE_ALLOWED (reader, p->luma_log2_weight_denom, 0, 7);
692   /* set default values */
693   memset (p->luma_weight_l0, 1 << p->luma_log2_weight_denom, 32);
694   memset (p->luma_offset_l0, 0, 32);
695
696   if (seq->ChromaArrayType != 0) {
697     READ_UE_ALLOWED (reader, p->chroma_log2_weight_denom, 0, 7);
698     /* set default values */
699     memset (p->chroma_weight_l0, 1 << p->chroma_log2_weight_denom, 64);
700     memset (p->chroma_offset_l0, 0, 64);
701   }
702
703   for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
704     guint8 luma_weight_l0_flag;
705
706     READ_UINT8 (reader, luma_weight_l0_flag, 1);
707     if (luma_weight_l0_flag) {
708       READ_SE_ALLOWED (reader, p->luma_weight_l0[i], -128, 127);
709       READ_SE_ALLOWED (reader, p->luma_offset_l0[i], -128, 127);
710     }
711     if (seq->ChromaArrayType != 0) {
712       guint8 chroma_weight_l0_flag;
713       gint j;
714
715       READ_UINT8 (reader, chroma_weight_l0_flag, 1);
716       if (chroma_weight_l0_flag) {
717         for (j = 0; j < 2; j++) {
718           READ_SE_ALLOWED (reader, p->chroma_weight_l0[i][j], -128, 127);
719           READ_SE_ALLOWED (reader, p->chroma_offset_l0[i][j], -128, 127);
720         }
721       }
722     }
723   }
724
725   if (GST_H264_IS_B_SLICE (slice->type)) {
726     for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
727       guint8 luma_weight_l1_flag;
728
729       READ_UINT8 (reader, luma_weight_l1_flag, 1);
730       if (luma_weight_l1_flag) {
731         READ_SE_ALLOWED (reader, p->luma_weight_l1[i], -128, 127);
732         READ_SE_ALLOWED (reader, p->luma_offset_l1[i], -128, 127);
733       }
734       if (seq->ChromaArrayType != 0) {
735         guint8 chroma_weight_l1_flag;
736         gint j;
737
738         READ_UINT8 (reader, chroma_weight_l1_flag, 1);
739         if (chroma_weight_l1_flag) {
740           for (j = 0; j < 2; j++) {
741             READ_SE_ALLOWED (reader, p->chroma_weight_l1[i][j], -128, 127);
742             READ_SE_ALLOWED (reader, p->chroma_offset_l1[i][j], -128, 127);
743           }
744         }
745       }
746     }
747   }
748
749   return TRUE;
750
751 error:
752   GST_WARNING ("error parsing \"Prediction weight table\"");
753   return FALSE;
754 }
755
756 static gboolean
757 gst_h264_slice_parse_ref_pic_list_reordering (GstH264Slice * slice,
758     GstNalReader * reader)
759 {
760   GST_DEBUG ("parsing \"Reference picture list reordering\"");
761
762   if (!GST_H264_IS_I_SLICE (slice->type) && !GST_H264_IS_SI_SLICE (slice->type)) {
763     guint8 ref_pic_list_reordering_flag_l0;
764     guint8 reordering_of_pic_nums_idc;
765
766     READ_UINT8 (reader, ref_pic_list_reordering_flag_l0, 1);
767     if (ref_pic_list_reordering_flag_l0)
768       do {
769         READ_UE_ALLOWED (reader, reordering_of_pic_nums_idc, 0, 3);
770         if (reordering_of_pic_nums_idc == 0 || reordering_of_pic_nums_idc == 1) {
771           guint32 abs_diff_pic_num_minus1 G_GNUC_UNUSED;
772
773           READ_UE_ALLOWED (reader, abs_diff_pic_num_minus1, 0,
774               slice->MaxPicNum - 1);
775         } else if (reordering_of_pic_nums_idc == 2) {
776           guint32 long_term_pic_num;
777
778           READ_UE (reader, long_term_pic_num);
779         }
780       } while (reordering_of_pic_nums_idc != 3);
781   }
782
783   if (GST_H264_IS_B_SLICE (slice->type)) {
784     guint8 ref_pic_list_reordering_flag_l1;
785     guint8 reordering_of_pic_nums_idc;
786
787     READ_UINT8 (reader, ref_pic_list_reordering_flag_l1, 1);
788     if (ref_pic_list_reordering_flag_l1)
789       do {
790         READ_UE_ALLOWED (reader, reordering_of_pic_nums_idc, 0, 3);
791         if (reordering_of_pic_nums_idc == 0 || reordering_of_pic_nums_idc == 1) {
792           guint32 abs_diff_num_minus1;
793           READ_UE (reader, abs_diff_num_minus1);
794         } else if (reordering_of_pic_nums_idc == 2) {
795           guint32 long_term_pic_num;
796
797           READ_UE (reader, long_term_pic_num);
798         }
799       } while (reordering_of_pic_nums_idc != 3);
800   }
801
802   return TRUE;
803
804 error:
805   GST_WARNING ("error parsing \"Reference picture list reordering\"");
806   return FALSE;
807 }
808
809 static gboolean
810 gst_h264_slice_parse_dec_ref_pic_marking (GstH264Slice * slice,
811     GstNalReader * reader)
812 {
813   GstH264DecRefPicMarking *m;
814
815   GST_DEBUG ("parsing \"Decoded reference picture marking\"");
816
817   m = &slice->dec_ref_pic_marking;
818
819   if (slice->nal_unit.IdrPicFlag) {
820     READ_UINT8 (reader, m->no_output_of_prior_pics_flag, 1);
821     READ_UINT8 (reader, m->long_term_reference_flag, 1);
822   } else {
823     READ_UINT8 (reader, m->adaptive_ref_pic_marking_mode_flag, 1);
824     if (m->adaptive_ref_pic_marking_mode_flag) {
825       guint8 memory_management_control_operation;
826
827       m->n_ref_pic_marking = 0;
828       while (1) {
829         READ_UE_ALLOWED (reader, memory_management_control_operation, 0, 6);
830         if (memory_management_control_operation == 0)
831           break;
832
833         m->ref_pic_marking[m->
834             n_ref_pic_marking].memory_management_control_operation =
835             memory_management_control_operation;
836
837         if (memory_management_control_operation == 1 ||
838             memory_management_control_operation == 3)
839           READ_UE (reader,
840               m->ref_pic_marking[m->
841                   n_ref_pic_marking].difference_of_pic_nums_minus1);
842
843         if (memory_management_control_operation == 2)
844           READ_UE (reader,
845               m->ref_pic_marking[m->n_ref_pic_marking].long_term_pic_num);
846
847         if (memory_management_control_operation == 3 ||
848             memory_management_control_operation == 6)
849           READ_UE (reader,
850               m->ref_pic_marking[m->n_ref_pic_marking].long_term_frame_idx);
851
852         if (memory_management_control_operation == 4)
853           READ_UE (reader,
854               m->ref_pic_marking[m->
855                   n_ref_pic_marking].max_long_term_frame_idx_plus1);
856
857         m->n_ref_pic_marking++;
858       }
859     }
860   }
861
862   return TRUE;
863
864 error:
865   GST_WARNING ("error parsing \"Decoded reference picture marking\"");
866   return FALSE;
867 }
868
869 gboolean
870 gst_h264_parser_parse_slice_header (GstH264Parser * parser,
871     GstH264Slice * slice, guint8 * data, guint size, GstNalUnit nal_unit)
872 {
873   GstNalReader reader = GST_NAL_READER_INIT (data, size);
874   gint pic_parameter_set_id;
875   GstH264Picture *pic;
876   GstH264Sequence *seq;
877
878   g_return_val_if_fail (GST_IS_H264_PARSER (parser), FALSE);
879   g_return_val_if_fail (slice != NULL, FALSE);
880   g_return_val_if_fail (data != NULL, FALSE);
881   g_return_val_if_fail (size > 0, FALSE);
882
883   GST_DEBUG ("parsing \"Slice header\"");
884
885   memcpy (&slice->nal_unit, &nal_unit, sizeof (GstNalUnit));
886
887   READ_UE (&reader, slice->first_mb_in_slice);
888   READ_UE (&reader, slice->type);
889
890   READ_UE_ALLOWED (&reader, pic_parameter_set_id, 0, 255);
891   pic = g_hash_table_lookup (parser->pictures, &pic_parameter_set_id);
892   if (!pic) {
893     GST_WARNING ("couldn't find associated picture parameter set with id: %d",
894         pic_parameter_set_id);
895     goto error;
896   }
897   slice->picture = pic;
898   seq = pic->sequence;
899
900   /* set default values for fields that might not be present in the bitstream
901      and have valid defaults */
902   slice->field_pic_flag = 0;
903   slice->bottom_field_flag = 0;
904   slice->delta_pic_order_cnt_bottom = 0;
905   slice->delta_pic_order_cnt[0] = 0;
906   slice->delta_pic_order_cnt[1] = 0;
907   slice->redundant_pic_cnt = 0;
908   slice->num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
909   slice->num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
910
911   if (seq->separate_colour_plane_flag)
912     READ_UINT8 (&reader, slice->colour_plane_id, 2);
913
914   READ_UINT16 (&reader, slice->frame_num, seq->log2_max_frame_num_minus4 + 4);
915
916   if (!seq->frame_mbs_only_flag) {
917     READ_UINT8 (&reader, slice->field_pic_flag, 1);
918     if (slice->field_pic_flag)
919       READ_UINT8 (&reader, slice->bottom_field_flag, 1);
920   }
921
922   /* calculate MaxPicNum */
923   if (slice->field_pic_flag)
924     slice->MaxPicNum = seq->MaxFrameNum;
925   else
926     slice->MaxPicNum = 2 * seq->MaxFrameNum;
927
928   if (nal_unit.type == 5)
929     READ_UE_ALLOWED (&reader, slice->idr_pic_id, 0, 65535);
930
931   if (seq->pic_order_cnt_type == 0) {
932     READ_UINT16 (&reader, slice->pic_order_cnt_lsb,
933         seq->log2_max_pic_order_cnt_lsb_minus4 + 4);
934     if (pic->pic_order_present_flag && !slice->field_pic_flag)
935       READ_SE (&reader, slice->delta_pic_order_cnt_bottom);
936   }
937
938   if (seq->pic_order_cnt_type == 1 && !seq->delta_pic_order_always_zero_flag) {
939     READ_SE (&reader, slice->delta_pic_order_cnt[0]);
940     if (pic->pic_order_present_flag && !slice->field_pic_flag)
941       READ_SE (&reader, slice->delta_pic_order_cnt[1]);
942   }
943
944   if (pic->redundant_pic_cnt_present_flag)
945     READ_UE_ALLOWED (&reader, slice->redundant_pic_cnt, 0, 127);
946
947   if (GST_H264_IS_B_SLICE (slice->type))
948     READ_UINT8 (&reader, slice->direct_spatial_mv_pred_flag, 1);
949
950   if (GST_H264_IS_P_SLICE (slice->type) ||
951       GST_H264_IS_SP_SLICE (slice->type) || GST_H264_IS_B_SLICE (slice->type)) {
952     guint8 num_ref_idx_active_override_flag;
953
954     READ_UINT8 (&reader, num_ref_idx_active_override_flag, 1);
955     if (num_ref_idx_active_override_flag) {
956       READ_UE_ALLOWED (&reader, slice->num_ref_idx_l0_active_minus1, 0, 31);
957
958       if (GST_H264_IS_B_SLICE (slice->type))
959         READ_UE_ALLOWED (&reader, slice->num_ref_idx_l1_active_minus1, 0, 31);
960     }
961   }
962
963   if (!gst_h264_slice_parse_ref_pic_list_reordering (slice, &reader))
964     return FALSE;
965
966   if ((pic->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice->type) ||
967               GST_H264_IS_SP_SLICE (slice->type)))
968       || (pic->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice->type))) {
969     if (!gst_h264_slice_parse_pred_weight_table (slice, &reader, seq, pic))
970       return FALSE;
971   }
972
973   if (nal_unit.ref_idc != 0) {
974     if (!gst_h264_slice_parse_dec_ref_pic_marking (slice, &reader))
975       return FALSE;
976   }
977
978   return TRUE;
979
980 error:
981   GST_WARNING ("error parsing \"Slice header\"");
982   return FALSE;
983 }
984
985 static gboolean
986 gst_h264_parser_parse_buffering_period (GstH264Parser * parser,
987     GstH264BufferingPeriod * per, guint8 * data, guint size)
988 {
989   GstNalReader reader = GST_NAL_READER_INIT (data, size);
990
991   GstH264Sequence *seq;
992   guint8 seq_parameter_set_id;
993
994   GST_DEBUG ("parsing \"Buffering period\"");
995
996   READ_UE_ALLOWED (&reader, seq_parameter_set_id, 0, 31);
997   seq = g_hash_table_lookup (parser->sequences, &seq_parameter_set_id);
998   if (!seq) {
999     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1000         seq_parameter_set_id);
1001     goto error;
1002   }
1003   per->seq = seq;
1004
1005   if (seq->vui_parameters_present_flag) {
1006     GstH264VUIParameters *vui = &seq->vui_parameters;
1007
1008     if (vui->nal_hrd_parameters_present_flag) {
1009       GstH264HRDParameters *hrd = &vui->nal_hrd_parameters;
1010       guint8 SchedSelIdx;
1011
1012       for (SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
1013         READ_UINT8 (&reader, per->nal_initial_cpb_removal_delay[SchedSelIdx],
1014             5);
1015         READ_UINT8 (&reader,
1016             per->nal_initial_cpb_removal_delay_offset[SchedSelIdx], 5);
1017       }
1018     }
1019
1020     if (vui->vcl_hrd_parameters_present_flag) {
1021       GstH264HRDParameters *hrd = &vui->vcl_hrd_parameters;
1022       guint8 SchedSelIdx;
1023
1024       for (SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
1025         READ_UINT8 (&reader, per->vcl_initial_cpb_removal_delay[SchedSelIdx],
1026             5);
1027         READ_UINT8 (&reader,
1028             per->vcl_initial_cpb_removal_delay_offset[SchedSelIdx], 5);
1029       }
1030     }
1031   }
1032
1033   return TRUE;
1034
1035 error:
1036   GST_WARNING ("error parsing \"Buffering period\"");
1037   return FALSE;
1038 }
1039
1040 static gboolean
1041 gst_h264_parse_clock_timestamp (GstH264ClockTimestamp * tim,
1042     GstH264VUIParameters * vui, GstNalReader * reader)
1043 {
1044   guint8 full_timestamp_flag;
1045   guint8 time_offset_length;
1046
1047   GST_DEBUG ("parsing \"Clock timestamp\"");
1048
1049   /* defalt values */
1050   tim->time_offset = 0;
1051
1052   READ_UINT8 (reader, tim->ct_type, 2);
1053   READ_UINT8 (reader, tim->nuit_field_based_flag, 1);
1054   READ_UINT8 (reader, tim->counting_type, 5);
1055   READ_UINT8 (reader, full_timestamp_flag, 1);
1056   READ_UINT8 (reader, tim->discontinuity_flag, 1);
1057   READ_UINT8 (reader, tim->cnt_dropped_flag, 1);
1058   READ_UINT8 (reader, tim->n_frames, 8);
1059
1060   if (full_timestamp_flag) {
1061     tim->seconds_flag = TRUE;
1062     READ_UINT8 (reader, tim->seconds_value, 6);
1063
1064     tim->minutes_flag = TRUE;
1065     READ_UINT8 (reader, tim->minutes_value, 6);
1066
1067     tim->hours_flag = TRUE;
1068     READ_UINT8 (reader, tim->hours_value, 5);
1069   } else {
1070     READ_UINT8 (reader, tim->seconds_flag, 1);
1071     if (tim->seconds_flag) {
1072       READ_UINT8 (reader, tim->seconds_value, 6);
1073       READ_UINT8 (reader, tim->minutes_flag, 1);
1074       if (tim->minutes_flag) {
1075         READ_UINT8 (reader, tim->minutes_value, 6);
1076         READ_UINT8 (reader, tim->hours_flag, 1);
1077         if (tim->hours_flag)
1078           READ_UINT8 (reader, tim->hours_value, 5);
1079       }
1080     }
1081   }
1082
1083   time_offset_length = 0;
1084   if (vui->nal_hrd_parameters_present_flag)
1085     time_offset_length = vui->nal_hrd_parameters.time_offset_length;
1086   else if (vui->vcl_hrd_parameters_present_flag)
1087     time_offset_length = vui->vcl_hrd_parameters.time_offset_length;
1088
1089   if (time_offset_length > 0)
1090     READ_UINT32 (reader, tim->time_offset, time_offset_length);
1091
1092 error:
1093   GST_WARNING ("error parsing \"Clock timestamp\"");
1094   return FALSE;
1095 }
1096
1097 static gboolean
1098 gst_h264_parser_parse_pic_timing (GstH264Parser * parser, GstH264Sequence * seq,
1099     GstH264PicTiming * tim, guint8 * data, guint size)
1100 {
1101   GstNalReader reader = GST_NAL_READER_INIT (data, size);
1102
1103   GST_DEBUG ("parsing \"Picture timing\"");
1104
1105   if (!seq) {
1106     GST_WARNING ("didn't get the associated sequence paramater set for the "
1107         "current access unit");
1108     goto error;
1109   }
1110
1111   /* default values */
1112   memset (tim->clock_timestamp_flag, 0, 3);
1113
1114   if (seq->vui_parameters_present_flag) {
1115     GstH264VUIParameters *vui = &seq->vui_parameters;
1116
1117     if (vui->nal_hrd_parameters_present_flag) {
1118       READ_UINT8 (&reader, tim->cpb_removal_delay,
1119           vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1120       READ_UINT8 (&reader, tim->dpb_output_delay,
1121           vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1122     } else if (vui->nal_hrd_parameters_present_flag) {
1123       READ_UINT8 (&reader, tim->cpb_removal_delay,
1124           vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1125       READ_UINT8 (&reader, tim->dpb_output_delay,
1126           vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1127     }
1128
1129     if (vui->pic_struct_present_flag) {
1130       const guint8 num_clock_ts_table[9] = {
1131         1, 1, 1, 2, 2, 3, 3, 2, 3
1132       };
1133       guint8 NumClockTs;
1134       guint i;
1135
1136       READ_UINT8 (&reader, tim->pic_struct, 4);
1137       CHECK_ALLOWED (tim->pic_struct, 0, 8);
1138
1139       NumClockTs = num_clock_ts_table[tim->pic_struct];
1140       for (i = 0; i < NumClockTs; i++) {
1141         READ_UINT8 (&reader, tim->clock_timestamp_flag[i], 1);
1142         if (tim->clock_timestamp_flag[i]) {
1143           if (!gst_h264_parse_clock_timestamp (&tim->clock_timestamp[i], vui,
1144                   &reader))
1145             goto error;
1146         }
1147       }
1148     }
1149   }
1150
1151   return TRUE;
1152
1153 error:
1154   GST_WARNING ("error parsing \"Picture timing\"");
1155   return FALSE;
1156 }
1157
1158 gboolean
1159 gst_h264_parser_parse_sei_message (GstH264Parser * parser,
1160     GstH264Sequence * seq, GstH264SEIMessage * sei, guint8 * data, guint size)
1161 {
1162   GstNalReader reader;
1163
1164   guint32 payloadSize;
1165   guint8 payload_type_byte, payload_size_byte;
1166
1167   guint8 *payload_data;
1168   guint remaining, payload_size;
1169   gboolean res;
1170
1171   g_return_val_if_fail (GST_IS_H264_PARSER (parser), FALSE);
1172   g_return_val_if_fail (sei != NULL, FALSE);
1173   g_return_val_if_fail (data != NULL, FALSE);
1174   g_return_val_if_fail (size > 0, FALSE);
1175
1176   GST_DEBUG ("parsing \"Sei message\"");
1177
1178   gst_nal_reader_init (&reader, data, size);
1179
1180   sei->payloadType = 0;
1181   do {
1182     READ_UINT8 (&reader, payload_type_byte, 8);
1183     sei->payloadType += payload_type_byte;
1184   }
1185   while (payload_type_byte == 0xff);
1186
1187   payloadSize = 0;
1188   do {
1189     READ_UINT8 (&reader, payload_size_byte, 8);
1190     payloadSize += payload_size_byte;
1191   }
1192   while (payload_size_byte == 0xff);
1193
1194   payload_data = data + gst_nal_reader_get_pos (&reader) * 8;
1195   remaining = gst_nal_reader_get_remaining (&reader) * 8;
1196   payload_size = payloadSize < remaining ? payloadSize : remaining;
1197
1198   if (sei->payloadType == 0)
1199     res =
1200         gst_h264_parser_parse_buffering_period (parser,
1201         &sei->buffering_period, payload_data, payload_size);
1202   else if (sei->payloadType == 1)
1203     res = gst_h264_parser_parse_pic_timing (parser, seq, &sei->pic_timing,
1204         payload_data, payload_size);
1205   else
1206     res = TRUE;
1207
1208   return res;
1209
1210 error:
1211   GST_WARNING ("error parsing \"Sei message\"");
1212   return FALSE;
1213 }
1214
1215 #undef CHECK_ALLOWED
1216 #undef READ_UINT8
1217 #undef READ_UINT16
1218 #undef READ_UINT32
1219 #undef READ_UINT64
1220 #undef READ_UE
1221 #undef READ_UE_ALLOWED
1222 #undef READ_SE
1223 #undef READ_SE_ALLOWED
1224
1225 static void
1226 gst_h264_parser_init (GstH264Parser * object)
1227 {
1228   GstH264Parser *parser = GST_H264_PARSER (object);
1229
1230   parser->sequences = g_hash_table_new_full (g_int_hash, g_int_equal, NULL,
1231       gst_h264_sequence_free);
1232   parser->pictures = g_hash_table_new_full (g_int_hash, g_int_equal, NULL,
1233       gst_h264_picture_free);
1234 }
1235
1236 static void
1237 gst_h264_parser_finalize (GObject * object)
1238 {
1239   GstH264Parser *parser = GST_H264_PARSER (object);
1240
1241   g_hash_table_destroy (parser->sequences);
1242   g_hash_table_destroy (parser->pictures);
1243
1244   G_OBJECT_CLASS (gst_h264_parser_parent_class)->finalize (object);
1245 }
1246
1247 static void
1248 gst_h264_parser_class_init (GstH264ParserClass * klass)
1249 {
1250   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1251
1252   object_class->finalize = gst_h264_parser_finalize;
1253 }