798db5d0927887b4e05c545404b12d2fcd76296a
[platform/upstream/gstreamer.git] / gst-libs / gst / codecparsers / gsth264parser.c
1 /* Gstreamer
2  * Copyright (C) <2011> Intel Corporation
3  * Copyright (C) <2011> Collabora Ltd.
4  * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
5  *
6  * Some bits C-c,C-v'ed and s/4/3 from h264parse and videoparsers/h264parse.c:
7  *    Copyright (C) <2010> Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
8  *    Copyright (C) <2010> Collabora Multimedia
9  *    Copyright (C) <2010> Nokia Corporation
10  *
11  *    (C) 2005 Michal Benes <michal.benes@itonis.tv>
12  *    (C) 2008 Wim Taymans <wim.taymans@gmail.com>
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Library General Public
16  * License as published by the Free Software Foundation; either
17  * version 2 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Library General Public License for more details.
23  *
24  * You should have received a copy of the GNU Library General Public
25  * License along with this library; if not, write to the
26  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
27  * Boston, MA 02110-1301, USA.
28  */
29
30 /**
31  * SECTION:gsth264parser
32  * @title: GstH264Parser
33  * @short_description: Convenience library for h264 video
34  * bitstream parsing.
35  *
36  * It offers bitstream parsing in both AVC (length-prefixed) and Annex B
37  * (0x000001 start code prefix) format. To identify a NAL unit in a bitstream
38  * and parse its headers, first call:
39  *
40  *   * #gst_h264_parser_identify_nalu to identify a NAL unit in an Annex B type bitstream
41  *
42  *   * #gst_h264_parser_identify_nalu_avc to identify a NAL unit in an AVC type bitstream
43  *
44  * The following functions are then available for parsing the structure of the
45  * #GstH264NalUnit, depending on the #GstH264NalUnitType:
46  *
47  *   * From #GST_H264_NAL_SLICE to #GST_H264_NAL_SLICE_IDR: #gst_h264_parser_parse_slice_hdr
48  *
49  *   * #GST_H264_NAL_SEI: #gst_h264_parser_parse_sei
50  *
51  *   * #GST_H264_NAL_SPS: #gst_h264_parser_parse_sps
52  *
53  *   * #GST_H264_NAL_PPS: #gst_h264_parser_parse_pps
54  *
55  *   * Any other: #gst_h264_parser_parse_nal
56  *
57  * One of these functions *must* be called on every NAL unit in the bitstream,
58  * in order to keep the internal structures of the #GstH264NalParser up to
59  * date. It is legal to call #gst_h264_parser_parse_nal on NAL units of any
60  * type, if no special parsing of the current NAL unit is required by the
61  * application.
62  *
63  * For more details about the structures, look at the ITU-T H.264 and ISO/IEC 14496-10 â€“ MPEG-4
64  * Part 10 specifications, available at:
65  *
66  *   * ITU-T H.264: http://www.itu.int/rec/T-REC-H.264
67  *
68  *   * ISO/IEC 14496-10: http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=56538
69  *
70  */
71
72 #ifdef HAVE_CONFIG_H
73 #  include "config.h"
74 #endif
75
76 #include "nalutils.h"
77 #include "gsth264parser.h"
78
79 #include <gst/base/gstbytereader.h>
80 #include <gst/base/gstbitreader.h>
81 #include <string.h>
82
83 #ifndef GST_DISABLE_GST_DEBUG
84 #define GST_CAT_DEFAULT gst_h264_debug_category_get()
85 static GstDebugCategory *
86 gst_h264_debug_category_get (void)
87 {
88   static gsize cat_gonce = 0;
89
90   if (g_once_init_enter (&cat_gonce)) {
91     GstDebugCategory *cat = NULL;
92
93     GST_DEBUG_CATEGORY_INIT (cat, "codecparsers_h264", 0, "h264 parse library");
94
95     g_once_init_leave (&cat_gonce, (gsize) cat);
96   }
97
98   return (GstDebugCategory *) cat_gonce;
99 }
100 #endif /* GST_DISABLE_GST_DEBUG */
101
102 /**** Default scaling_lists according to Table 7-2 *****/
103 static const guint8 default_4x4_intra[16] = {
104   6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
105   32, 37, 37, 42
106 };
107
108 static const guint8 default_4x4_inter[16] = {
109   10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
110   27, 30, 30, 34
111 };
112
113 static const guint8 default_8x8_intra[64] = {
114   6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
115   18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
116   27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
117   33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
118 };
119
120 static const guint8 default_8x8_inter[64] = {
121   9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
122   19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
123   24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
124   28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
125 };
126
127 static const guint8 zigzag_8x8[64] = {
128   0, 1, 8, 16, 9, 2, 3, 10,
129   17, 24, 32, 25, 18, 11, 4, 5,
130   12, 19, 26, 33, 40, 48, 41, 34,
131   27, 20, 13, 6, 7, 14, 21, 28,
132   35, 42, 49, 56, 57, 50, 43, 36,
133   29, 22, 15, 23, 30, 37, 44, 51,
134   58, 59, 52, 45, 38, 31, 39, 46,
135   53, 60, 61, 54, 47, 55, 62, 63
136 };
137
138 static const guint8 zigzag_4x4[16] = {
139   0, 1, 4, 8,
140   5, 2, 3, 6,
141   9, 12, 13, 10,
142   7, 11, 14, 15,
143 };
144
145 typedef struct
146 {
147   guint par_n, par_d;
148 } PAR;
149
150 /* Table E-1 - Meaning of sample aspect ratio indicator (1..16) */
151 static const PAR aspect_ratios[17] = {
152   {0, 0},
153   {1, 1},
154   {12, 11},
155   {10, 11},
156   {16, 11},
157   {40, 33},
158   {24, 11},
159   {20, 11},
160   {32, 11},
161   {80, 33},
162   {18, 11},
163   {15, 11},
164   {64, 33},
165   {160, 99},
166   {4, 3},
167   {3, 2},
168   {2, 1}
169 };
170
171 /*****  Utils ****/
172 #define EXTENDED_SAR 255
173
174 static GstH264SPS *
175 gst_h264_parser_get_sps (GstH264NalParser * nalparser, guint8 sps_id)
176 {
177   GstH264SPS *sps;
178
179   sps = &nalparser->sps[sps_id];
180
181   if (sps->valid)
182     return sps;
183
184   return NULL;
185 }
186
187 static GstH264PPS *
188 gst_h264_parser_get_pps (GstH264NalParser * nalparser, guint8 pps_id)
189 {
190   GstH264PPS *pps;
191
192   pps = &nalparser->pps[pps_id];
193
194   if (pps->valid)
195     return pps;
196
197   return NULL;
198 }
199
200 static gboolean
201 gst_h264_parse_nalu_header (GstH264NalUnit * nalu)
202 {
203   guint8 *data = nalu->data + nalu->offset;
204   guint8 svc_extension_flag;
205   GstBitReader br;
206
207   if (nalu->size < 1)
208     return FALSE;
209
210   nalu->type = (data[0] & 0x1f);
211   nalu->ref_idc = (data[0] & 0x60) >> 5;
212   nalu->idr_pic_flag = (nalu->type == 5 ? 1 : 0);
213   nalu->header_bytes = 1;
214
215   nalu->extension_type = GST_H264_NAL_EXTENSION_NONE;
216
217   switch (nalu->type) {
218     case GST_H264_NAL_PREFIX_UNIT:
219     case GST_H264_NAL_SLICE_EXT:
220       if (nalu->size < 4)
221         return FALSE;
222       gst_bit_reader_init (&br, nalu->data + nalu->offset + nalu->header_bytes,
223           nalu->size - nalu->header_bytes);
224
225       svc_extension_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
226       if (svc_extension_flag) { /* SVC */
227
228         nalu->extension_type = GST_H264_NAL_EXTENSION_SVC;
229
230       } else {                  /* MVC */
231         GstH264NalUnitExtensionMVC *const mvc = &nalu->extension.mvc;
232
233         nalu->extension_type = GST_H264_NAL_EXTENSION_MVC;
234         mvc->non_idr_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
235         mvc->priority_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 6);
236         mvc->view_id = gst_bit_reader_get_bits_uint16_unchecked (&br, 10);
237         mvc->temporal_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 3);
238         mvc->anchor_pic_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
239         mvc->inter_view_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
240
241         /* Update IdrPicFlag (H.7.4.1.1) */
242         nalu->idr_pic_flag = !mvc->non_idr_flag;
243       }
244       nalu->header_bytes += 3;
245       break;
246     default:
247       break;
248   }
249
250   GST_DEBUG ("Nal type %u, ref_idc %u", nalu->type, nalu->ref_idc);
251   return TRUE;
252 }
253
254 /*
255  * gst_h264_pps_copy:
256  * @dst_pps: The destination #GstH264PPS to copy into
257  * @src_pps: The source #GstH264PPS to copy from
258  *
259  * Copies @src_pps into @dst_pps.
260  *
261  * Returns: %TRUE if everything went fine, %FALSE otherwise
262  */
263 static gboolean
264 gst_h264_pps_copy (GstH264PPS * dst_pps, const GstH264PPS * src_pps)
265 {
266   g_return_val_if_fail (dst_pps != NULL, FALSE);
267   g_return_val_if_fail (src_pps != NULL, FALSE);
268
269   gst_h264_pps_clear (dst_pps);
270
271   *dst_pps = *src_pps;
272
273   if (src_pps->slice_group_id)
274     dst_pps->slice_group_id = g_memdup (src_pps->slice_group_id,
275         src_pps->pic_size_in_map_units_minus1 + 1);
276
277   return TRUE;
278 }
279
280 /* Copy MVC-specific data for subset SPS header */
281 static gboolean
282 gst_h264_sps_mvc_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
283 {
284   GstH264SPSExtMVC *const dst_mvc = &dst_sps->extension.mvc;
285   const GstH264SPSExtMVC *const src_mvc = &src_sps->extension.mvc;
286   guint i, j, k;
287
288   g_assert (dst_sps->extension_type == GST_H264_NAL_EXTENSION_MVC);
289
290   dst_mvc->num_views_minus1 = src_mvc->num_views_minus1;
291   dst_mvc->view = g_new0 (GstH264SPSExtMVCView, dst_mvc->num_views_minus1 + 1);
292   if (!dst_mvc->view)
293     return FALSE;
294
295   dst_mvc->view[0].view_id = src_mvc->view[0].view_id;
296
297   for (i = 1; i <= dst_mvc->num_views_minus1; i++) {
298     GstH264SPSExtMVCView *const dst_view = &dst_mvc->view[i];
299     const GstH264SPSExtMVCView *const src_view = &src_mvc->view[i];
300
301     dst_view->view_id = src_view->view_id;
302
303     dst_view->num_anchor_refs_l0 = src_view->num_anchor_refs_l0;
304     for (j = 0; j < dst_view->num_anchor_refs_l0; j++)
305       dst_view->anchor_ref_l0[j] = src_view->anchor_ref_l0[j];
306
307     dst_view->num_anchor_refs_l1 = src_view->num_anchor_refs_l1;
308     for (j = 0; j < dst_view->num_anchor_refs_l1; j++)
309       dst_view->anchor_ref_l1[j] = src_view->anchor_ref_l1[j];
310
311     dst_view->num_non_anchor_refs_l0 = src_view->num_non_anchor_refs_l0;
312     for (j = 0; j < dst_view->num_non_anchor_refs_l0; j++)
313       dst_view->non_anchor_ref_l0[j] = src_view->non_anchor_ref_l0[j];
314
315     dst_view->num_non_anchor_refs_l1 = src_view->num_non_anchor_refs_l1;
316     for (j = 0; j < dst_view->num_non_anchor_refs_l1; j++)
317       dst_view->non_anchor_ref_l1[j] = src_view->non_anchor_ref_l1[j];
318   }
319
320   dst_mvc->num_level_values_signalled_minus1 =
321       src_mvc->num_level_values_signalled_minus1;
322   dst_mvc->level_value = g_new0 (GstH264SPSExtMVCLevelValue,
323       dst_mvc->num_level_values_signalled_minus1 + 1);
324   if (!dst_mvc->level_value)
325     return FALSE;
326
327   for (i = 0; i <= dst_mvc->num_level_values_signalled_minus1; i++) {
328     GstH264SPSExtMVCLevelValue *const dst_value = &dst_mvc->level_value[i];
329     const GstH264SPSExtMVCLevelValue *const src_value =
330         &src_mvc->level_value[i];
331
332     dst_value->level_idc = src_value->level_idc;
333
334     dst_value->num_applicable_ops_minus1 = src_value->num_applicable_ops_minus1;
335     dst_value->applicable_op = g_new0 (GstH264SPSExtMVCLevelValueOp,
336         dst_value->num_applicable_ops_minus1 + 1);
337     if (!dst_value->applicable_op)
338       return FALSE;
339
340     for (j = 0; j <= dst_value->num_applicable_ops_minus1; j++) {
341       GstH264SPSExtMVCLevelValueOp *const dst_op = &dst_value->applicable_op[j];
342       const GstH264SPSExtMVCLevelValueOp *const src_op =
343           &src_value->applicable_op[j];
344
345       dst_op->temporal_id = src_op->temporal_id;
346       dst_op->num_target_views_minus1 = src_op->num_target_views_minus1;
347       dst_op->target_view_id =
348           g_new (guint16, dst_op->num_target_views_minus1 + 1);
349       if (!dst_op->target_view_id)
350         return FALSE;
351
352       for (k = 0; k <= dst_op->num_target_views_minus1; k++)
353         dst_op->target_view_id[k] = src_op->target_view_id[k];
354       dst_op->num_views_minus1 = src_op->num_views_minus1;
355     }
356   }
357   return TRUE;
358 }
359
360 /*
361  * gst_h264_sps_copy:
362  * @dst_sps: The destination #GstH264SPS to copy into
363  * @src_sps: The source #GstH264SPS to copy from
364  *
365  * Copies @src_sps into @dst_sps.
366  *
367  * Returns: %TRUE if everything went fine, %FALSE otherwise
368  */
369 static gboolean
370 gst_h264_sps_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
371 {
372   g_return_val_if_fail (dst_sps != NULL, FALSE);
373   g_return_val_if_fail (src_sps != NULL, FALSE);
374
375   gst_h264_sps_clear (dst_sps);
376
377   *dst_sps = *src_sps;
378
379   switch (dst_sps->extension_type) {
380     case GST_H264_NAL_EXTENSION_MVC:
381       if (!gst_h264_sps_mvc_copy (dst_sps, src_sps))
382         return FALSE;
383       break;
384   }
385   return TRUE;
386 }
387
388 /****** Parsing functions *****/
389
390 static gboolean
391 gst_h264_parse_hrd_parameters (GstH264HRDParams * hrd, NalReader * nr)
392 {
393   guint sched_sel_idx;
394
395   GST_DEBUG ("parsing \"HRD Parameters\"");
396
397   READ_UE_MAX (nr, hrd->cpb_cnt_minus1, 31);
398   READ_UINT8 (nr, hrd->bit_rate_scale, 4);
399   READ_UINT8 (nr, hrd->cpb_size_scale, 4);
400
401   for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) {
402     READ_UE (nr, hrd->bit_rate_value_minus1[sched_sel_idx]);
403     READ_UE (nr, hrd->cpb_size_value_minus1[sched_sel_idx]);
404     READ_UINT8 (nr, hrd->cbr_flag[sched_sel_idx], 1);
405   }
406
407   READ_UINT8 (nr, hrd->initial_cpb_removal_delay_length_minus1, 5);
408   READ_UINT8 (nr, hrd->cpb_removal_delay_length_minus1, 5);
409   READ_UINT8 (nr, hrd->dpb_output_delay_length_minus1, 5);
410   READ_UINT8 (nr, hrd->time_offset_length, 5);
411
412   return TRUE;
413
414 error:
415   GST_WARNING ("error parsing \"HRD Parameters\"");
416   return FALSE;
417 }
418
419 static gboolean
420 gst_h264_parse_vui_parameters (GstH264SPS * sps, NalReader * nr)
421 {
422   GstH264VUIParams *vui = &sps->vui_parameters;
423
424   GST_DEBUG ("parsing \"VUI Parameters\"");
425
426   /* set default values for fields that might not be present in the bitstream
427      and have valid defaults */
428   vui->video_format = 5;
429   vui->colour_primaries = 2;
430   vui->transfer_characteristics = 2;
431   vui->matrix_coefficients = 2;
432
433   READ_UINT8 (nr, vui->aspect_ratio_info_present_flag, 1);
434   if (vui->aspect_ratio_info_present_flag) {
435     READ_UINT8 (nr, vui->aspect_ratio_idc, 8);
436     if (vui->aspect_ratio_idc == EXTENDED_SAR) {
437       READ_UINT16 (nr, vui->sar_width, 16);
438       READ_UINT16 (nr, vui->sar_height, 16);
439       vui->par_n = vui->sar_width;
440       vui->par_d = vui->sar_height;
441     } else if (vui->aspect_ratio_idc <= 16) {
442       vui->par_n = aspect_ratios[vui->aspect_ratio_idc].par_n;
443       vui->par_d = aspect_ratios[vui->aspect_ratio_idc].par_d;
444     }
445   }
446
447   READ_UINT8 (nr, vui->overscan_info_present_flag, 1);
448   if (vui->overscan_info_present_flag)
449     READ_UINT8 (nr, vui->overscan_appropriate_flag, 1);
450
451   READ_UINT8 (nr, vui->video_signal_type_present_flag, 1);
452   if (vui->video_signal_type_present_flag) {
453
454     READ_UINT8 (nr, vui->video_format, 3);
455     READ_UINT8 (nr, vui->video_full_range_flag, 1);
456     READ_UINT8 (nr, vui->colour_description_present_flag, 1);
457     if (vui->colour_description_present_flag) {
458       READ_UINT8 (nr, vui->colour_primaries, 8);
459       READ_UINT8 (nr, vui->transfer_characteristics, 8);
460       READ_UINT8 (nr, vui->matrix_coefficients, 8);
461     }
462   }
463
464   READ_UINT8 (nr, vui->chroma_loc_info_present_flag, 1);
465   if (vui->chroma_loc_info_present_flag) {
466     READ_UE_MAX (nr, vui->chroma_sample_loc_type_top_field, 5);
467     READ_UE_MAX (nr, vui->chroma_sample_loc_type_bottom_field, 5);
468   }
469
470   READ_UINT8 (nr, vui->timing_info_present_flag, 1);
471   if (vui->timing_info_present_flag) {
472     READ_UINT32 (nr, vui->num_units_in_tick, 32);
473     if (vui->num_units_in_tick == 0)
474       GST_WARNING ("num_units_in_tick = 0 detected in stream "
475           "(incompliant to H.264 E.2.1).");
476
477     READ_UINT32 (nr, vui->time_scale, 32);
478     if (vui->time_scale == 0)
479       GST_WARNING ("time_scale = 0 detected in stream "
480           "(incompliant to H.264 E.2.1).");
481
482     READ_UINT8 (nr, vui->fixed_frame_rate_flag, 1);
483   }
484
485   READ_UINT8 (nr, vui->nal_hrd_parameters_present_flag, 1);
486   if (vui->nal_hrd_parameters_present_flag) {
487     if (!gst_h264_parse_hrd_parameters (&vui->nal_hrd_parameters, nr))
488       goto error;
489   }
490
491   READ_UINT8 (nr, vui->vcl_hrd_parameters_present_flag, 1);
492   if (vui->vcl_hrd_parameters_present_flag) {
493     if (!gst_h264_parse_hrd_parameters (&vui->vcl_hrd_parameters, nr))
494       goto error;
495   }
496
497   if (vui->nal_hrd_parameters_present_flag ||
498       vui->vcl_hrd_parameters_present_flag)
499     READ_UINT8 (nr, vui->low_delay_hrd_flag, 1);
500
501   READ_UINT8 (nr, vui->pic_struct_present_flag, 1);
502   READ_UINT8 (nr, vui->bitstream_restriction_flag, 1);
503   if (vui->bitstream_restriction_flag) {
504     READ_UINT8 (nr, vui->motion_vectors_over_pic_boundaries_flag, 1);
505     READ_UE (nr, vui->max_bytes_per_pic_denom);
506     READ_UE_MAX (nr, vui->max_bits_per_mb_denom, 16);
507     READ_UE_MAX (nr, vui->log2_max_mv_length_horizontal, 16);
508     READ_UE_MAX (nr, vui->log2_max_mv_length_vertical, 16);
509     READ_UE (nr, vui->num_reorder_frames);
510     READ_UE (nr, vui->max_dec_frame_buffering);
511   }
512
513   return TRUE;
514
515 error:
516   GST_WARNING ("error parsing \"VUI Parameters\"");
517   return FALSE;
518 }
519
520 static gboolean
521 gst_h264_parser_parse_scaling_list (NalReader * nr,
522     guint8 scaling_lists_4x4[6][16], guint8 scaling_lists_8x8[6][64],
523     const guint8 fallback_4x4_inter[16], const guint8 fallback_4x4_intra[16],
524     const guint8 fallback_8x8_inter[64], const guint8 fallback_8x8_intra[64],
525     guint8 n_lists)
526 {
527   guint i;
528
529   static const guint8 *default_lists[12] = {
530     default_4x4_intra, default_4x4_intra, default_4x4_intra,
531     default_4x4_inter, default_4x4_inter, default_4x4_inter,
532     default_8x8_intra, default_8x8_inter,
533     default_8x8_intra, default_8x8_inter,
534     default_8x8_intra, default_8x8_inter
535   };
536
537   GST_DEBUG ("parsing scaling lists");
538
539   for (i = 0; i < 12; i++) {
540     gboolean use_default = FALSE;
541
542     if (i < n_lists) {
543       guint8 scaling_list_present_flag;
544
545       READ_UINT8 (nr, scaling_list_present_flag, 1);
546       if (scaling_list_present_flag) {
547         guint8 *scaling_list;
548         guint size;
549         guint j;
550         guint8 last_scale, next_scale;
551
552         if (i < 6) {
553           scaling_list = scaling_lists_4x4[i];
554           size = 16;
555         } else {
556           scaling_list = scaling_lists_8x8[i - 6];
557           size = 64;
558         }
559
560         last_scale = 8;
561         next_scale = 8;
562         for (j = 0; j < size; j++) {
563           if (next_scale != 0) {
564             gint32 delta_scale;
565
566             READ_SE (nr, delta_scale);
567             next_scale = (last_scale + delta_scale) & 0xff;
568           }
569           if (j == 0 && next_scale == 0) {
570             /* Use default scaling lists (7.4.2.1.1.1) */
571             memcpy (scaling_list, default_lists[i], size);
572             break;
573           }
574           last_scale = scaling_list[j] =
575               (next_scale == 0) ? last_scale : next_scale;
576         }
577       } else
578         use_default = TRUE;
579     } else
580       use_default = TRUE;
581
582     if (use_default) {
583       switch (i) {
584         case 0:
585           memcpy (scaling_lists_4x4[0], fallback_4x4_intra, 16);
586           break;
587         case 1:
588           memcpy (scaling_lists_4x4[1], scaling_lists_4x4[0], 16);
589           break;
590         case 2:
591           memcpy (scaling_lists_4x4[2], scaling_lists_4x4[1], 16);
592           break;
593         case 3:
594           memcpy (scaling_lists_4x4[3], fallback_4x4_inter, 16);
595           break;
596         case 4:
597           memcpy (scaling_lists_4x4[4], scaling_lists_4x4[3], 16);
598           break;
599         case 5:
600           memcpy (scaling_lists_4x4[5], scaling_lists_4x4[4], 16);
601           break;
602         case 6:
603           memcpy (scaling_lists_8x8[0], fallback_8x8_intra, 64);
604           break;
605         case 7:
606           memcpy (scaling_lists_8x8[1], fallback_8x8_inter, 64);
607           break;
608         case 8:
609           memcpy (scaling_lists_8x8[2], scaling_lists_8x8[0], 64);
610           break;
611         case 9:
612           memcpy (scaling_lists_8x8[3], scaling_lists_8x8[1], 64);
613           break;
614         case 10:
615           memcpy (scaling_lists_8x8[4], scaling_lists_8x8[2], 64);
616           break;
617         case 11:
618           memcpy (scaling_lists_8x8[5], scaling_lists_8x8[3], 64);
619           break;
620
621         default:
622           break;
623       }
624     }
625   }
626
627   return TRUE;
628
629 error:
630   GST_WARNING ("error parsing scaling lists");
631   return FALSE;
632 }
633
634 static gboolean
635 slice_parse_ref_pic_list_modification_1 (GstH264SliceHdr * slice,
636     NalReader * nr, guint list, gboolean is_mvc)
637 {
638   GstH264RefPicListModification *entries;
639   guint8 *ref_pic_list_modification_flag, *n_ref_pic_list_modification;
640   guint32 modification_of_pic_nums_idc;
641   gsize max_entries;
642   guint i = 0;
643
644   if (list == 0) {
645     entries = slice->ref_pic_list_modification_l0;
646     max_entries = G_N_ELEMENTS (slice->ref_pic_list_modification_l0);
647     ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l0;
648     n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l0;
649   } else {
650     entries = slice->ref_pic_list_modification_l1;
651     max_entries = G_N_ELEMENTS (slice->ref_pic_list_modification_l1);
652     ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l1;
653     n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l1;
654   }
655
656   READ_UINT8 (nr, *ref_pic_list_modification_flag, 1);
657   if (*ref_pic_list_modification_flag) {
658     while (1) {
659       READ_UE (nr, modification_of_pic_nums_idc);
660       if (modification_of_pic_nums_idc == 0 ||
661           modification_of_pic_nums_idc == 1) {
662         READ_UE_MAX (nr, entries[i].value.abs_diff_pic_num_minus1,
663             slice->max_pic_num - 1);
664       } else if (modification_of_pic_nums_idc == 2) {
665         READ_UE (nr, entries[i].value.long_term_pic_num);
666       } else if (is_mvc && (modification_of_pic_nums_idc == 4 ||
667               modification_of_pic_nums_idc == 5)) {
668         READ_UE (nr, entries[i].value.abs_diff_view_idx_minus1);
669       }
670       entries[i++].modification_of_pic_nums_idc = modification_of_pic_nums_idc;
671       if (modification_of_pic_nums_idc == 3)
672         break;
673       if (i >= max_entries)
674         goto error;
675     }
676   }
677   *n_ref_pic_list_modification = i;
678   return TRUE;
679
680 error:
681   GST_WARNING ("error parsing \"Reference picture list %u modification\"",
682       list);
683   return FALSE;
684 }
685
686 static gboolean
687 slice_parse_ref_pic_list_modification (GstH264SliceHdr * slice, NalReader * nr,
688     gboolean is_mvc)
689 {
690   if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) {
691     if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 0, is_mvc))
692       return FALSE;
693   }
694
695   if (GST_H264_IS_B_SLICE (slice)) {
696     if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 1, is_mvc))
697       return FALSE;
698   }
699   return TRUE;
700 }
701
702 static gboolean
703 gst_h264_slice_parse_dec_ref_pic_marking (GstH264SliceHdr * slice,
704     GstH264NalUnit * nalu, NalReader * nr)
705 {
706   GstH264DecRefPicMarking *dec_ref_pic_m;
707   guint start_pos;
708
709   GST_DEBUG ("parsing \"Decoded reference picture marking\"");
710
711   start_pos = nal_reader_get_pos (nr);
712
713   dec_ref_pic_m = &slice->dec_ref_pic_marking;
714
715   if (nalu->idr_pic_flag) {
716     READ_UINT8 (nr, dec_ref_pic_m->no_output_of_prior_pics_flag, 1);
717     READ_UINT8 (nr, dec_ref_pic_m->long_term_reference_flag, 1);
718   } else {
719     READ_UINT8 (nr, dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag, 1);
720     if (dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag) {
721       guint32 mem_mgmt_ctrl_op;
722       GstH264RefPicMarking *refpicmarking;
723
724       dec_ref_pic_m->n_ref_pic_marking = 0;
725       while (1) {
726         refpicmarking =
727             &dec_ref_pic_m->ref_pic_marking[dec_ref_pic_m->n_ref_pic_marking];
728
729         READ_UE (nr, mem_mgmt_ctrl_op);
730         if (mem_mgmt_ctrl_op == 0)
731           break;
732
733         refpicmarking->memory_management_control_operation = mem_mgmt_ctrl_op;
734
735         if (mem_mgmt_ctrl_op == 1 || mem_mgmt_ctrl_op == 3)
736           READ_UE (nr, refpicmarking->difference_of_pic_nums_minus1);
737
738         if (mem_mgmt_ctrl_op == 2)
739           READ_UE (nr, refpicmarking->long_term_pic_num);
740
741         if (mem_mgmt_ctrl_op == 3 || mem_mgmt_ctrl_op == 6)
742           READ_UE (nr, refpicmarking->long_term_frame_idx);
743
744         if (mem_mgmt_ctrl_op == 4)
745           READ_UE (nr, refpicmarking->max_long_term_frame_idx_plus1);
746
747         dec_ref_pic_m->n_ref_pic_marking++;
748       }
749     }
750   }
751
752   dec_ref_pic_m->bit_size = nal_reader_get_pos (nr) - start_pos;
753
754   return TRUE;
755
756 error:
757   GST_WARNING ("error parsing \"Decoded reference picture marking\"");
758   return FALSE;
759 }
760
761 static gboolean
762 gst_h264_slice_parse_pred_weight_table (GstH264SliceHdr * slice,
763     NalReader * nr, guint8 chroma_array_type)
764 {
765   GstH264PredWeightTable *p;
766   gint16 default_luma_weight, default_chroma_weight;
767   gint i;
768
769   GST_DEBUG ("parsing \"Prediction weight table\"");
770
771   p = &slice->pred_weight_table;
772
773   READ_UE_MAX (nr, p->luma_log2_weight_denom, 7);
774   /* set default values */
775   default_luma_weight = 1 << p->luma_log2_weight_denom;
776   for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l0); i++)
777     p->luma_weight_l0[i] = default_luma_weight;
778   if (GST_H264_IS_B_SLICE (slice)) {
779     for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l1); i++)
780       p->luma_weight_l1[i] = default_luma_weight;
781   }
782
783   if (chroma_array_type != 0) {
784     READ_UE_MAX (nr, p->chroma_log2_weight_denom, 7);
785     /* set default values */
786     default_chroma_weight = 1 << p->chroma_log2_weight_denom;
787     for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l0); i++) {
788       p->chroma_weight_l0[i][0] = default_chroma_weight;
789       p->chroma_weight_l0[i][1] = default_chroma_weight;
790     }
791     if (GST_H264_IS_B_SLICE (slice)) {
792       for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l1); i++) {
793         p->chroma_weight_l1[i][0] = default_chroma_weight;
794         p->chroma_weight_l1[i][1] = default_chroma_weight;
795       }
796     }
797   }
798
799   for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
800     guint8 luma_weight_l0_flag;
801
802     READ_UINT8 (nr, luma_weight_l0_flag, 1);
803     if (luma_weight_l0_flag) {
804       READ_SE_ALLOWED (nr, p->luma_weight_l0[i], -128, 127);
805       READ_SE_ALLOWED (nr, p->luma_offset_l0[i], -128, 127);
806     }
807     if (chroma_array_type != 0) {
808       guint8 chroma_weight_l0_flag;
809       gint j;
810
811       READ_UINT8 (nr, chroma_weight_l0_flag, 1);
812       if (chroma_weight_l0_flag) {
813         for (j = 0; j < 2; j++) {
814           READ_SE_ALLOWED (nr, p->chroma_weight_l0[i][j], -128, 127);
815           READ_SE_ALLOWED (nr, p->chroma_offset_l0[i][j], -128, 127);
816         }
817       }
818     }
819   }
820
821   if (GST_H264_IS_B_SLICE (slice)) {
822     for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
823       guint8 luma_weight_l1_flag;
824
825       READ_UINT8 (nr, luma_weight_l1_flag, 1);
826       if (luma_weight_l1_flag) {
827         READ_SE_ALLOWED (nr, p->luma_weight_l1[i], -128, 127);
828         READ_SE_ALLOWED (nr, p->luma_offset_l1[i], -128, 127);
829       }
830       if (chroma_array_type != 0) {
831         guint8 chroma_weight_l1_flag;
832         gint j;
833
834         READ_UINT8 (nr, chroma_weight_l1_flag, 1);
835         if (chroma_weight_l1_flag) {
836           for (j = 0; j < 2; j++) {
837             READ_SE_ALLOWED (nr, p->chroma_weight_l1[i][j], -128, 127);
838             READ_SE_ALLOWED (nr, p->chroma_offset_l1[i][j], -128, 127);
839           }
840         }
841       }
842     }
843   }
844
845   return TRUE;
846
847 error:
848   GST_WARNING ("error parsing \"Prediction weight table\"");
849   return FALSE;
850 }
851
852 static GstH264ParserResult
853 gst_h264_parser_parse_buffering_period (GstH264NalParser * nalparser,
854     GstH264BufferingPeriod * per, NalReader * nr)
855 {
856   GstH264SPS *sps;
857   guint8 sps_id;
858
859   GST_DEBUG ("parsing \"Buffering period\"");
860
861   READ_UE_MAX (nr, sps_id, GST_H264_MAX_SPS_COUNT - 1);
862   sps = gst_h264_parser_get_sps (nalparser, sps_id);
863   if (!sps) {
864     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
865         sps_id);
866     return GST_H264_PARSER_BROKEN_LINK;
867   }
868   per->sps = sps;
869
870   if (sps->vui_parameters_present_flag) {
871     GstH264VUIParams *vui = &sps->vui_parameters;
872
873     if (vui->nal_hrd_parameters_present_flag) {
874       GstH264HRDParams *hrd = &vui->nal_hrd_parameters;
875       const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
876       guint8 sched_sel_idx;
877
878       for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
879           sched_sel_idx++) {
880         READ_UINT32 (nr, per->nal_initial_cpb_removal_delay[sched_sel_idx],
881             nbits);
882         READ_UINT32 (nr,
883             per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
884       }
885     }
886
887     if (vui->vcl_hrd_parameters_present_flag) {
888       GstH264HRDParams *hrd = &vui->vcl_hrd_parameters;
889       const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
890       guint8 sched_sel_idx;
891
892       for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
893           sched_sel_idx++) {
894         READ_UINT32 (nr, per->vcl_initial_cpb_removal_delay[sched_sel_idx],
895             nbits);
896         READ_UINT32 (nr,
897             per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
898       }
899     }
900   }
901
902   return GST_H264_PARSER_OK;
903
904 error:
905   GST_WARNING ("error parsing \"Buffering period\"");
906   return GST_H264_PARSER_ERROR;
907 }
908
909 static gboolean
910 gst_h264_parse_clock_timestamp (GstH264ClockTimestamp * tim,
911     GstH264VUIParams * vui, NalReader * nr)
912 {
913   guint8 full_timestamp_flag;
914   guint8 time_offset_length;
915
916   GST_DEBUG ("parsing \"Clock timestamp\"");
917
918   /* defalt values */
919   tim->time_offset = 0;
920
921   READ_UINT8 (nr, tim->ct_type, 2);
922   READ_UINT8 (nr, tim->nuit_field_based_flag, 1);
923   READ_UINT8 (nr, tim->counting_type, 5);
924   READ_UINT8 (nr, full_timestamp_flag, 1);
925   READ_UINT8 (nr, tim->discontinuity_flag, 1);
926   READ_UINT8 (nr, tim->cnt_dropped_flag, 1);
927   READ_UINT8 (nr, tim->n_frames, 8);
928
929   if (full_timestamp_flag) {
930     tim->seconds_flag = TRUE;
931     READ_UINT8 (nr, tim->seconds_value, 6);
932
933     tim->minutes_flag = TRUE;
934     READ_UINT8 (nr, tim->minutes_value, 6);
935
936     tim->hours_flag = TRUE;
937     READ_UINT8 (nr, tim->hours_value, 5);
938   } else {
939     READ_UINT8 (nr, tim->seconds_flag, 1);
940     if (tim->seconds_flag) {
941       READ_UINT8 (nr, tim->seconds_value, 6);
942       READ_UINT8 (nr, tim->minutes_flag, 1);
943       if (tim->minutes_flag) {
944         READ_UINT8 (nr, tim->minutes_value, 6);
945         READ_UINT8 (nr, tim->hours_flag, 1);
946         if (tim->hours_flag)
947           READ_UINT8 (nr, tim->hours_value, 5);
948       }
949     }
950   }
951
952   time_offset_length = 24;
953   if (vui->nal_hrd_parameters_present_flag)
954     time_offset_length = vui->nal_hrd_parameters.time_offset_length;
955   else if (vui->vcl_hrd_parameters_present_flag)
956     time_offset_length = vui->vcl_hrd_parameters.time_offset_length;
957
958   if (time_offset_length > 0)
959     READ_UINT32 (nr, tim->time_offset, time_offset_length);
960
961   return TRUE;
962
963 error:
964   GST_WARNING ("error parsing \"Clock timestamp\"");
965   return FALSE;
966 }
967
968 static GstH264ParserResult
969 gst_h264_parser_parse_pic_timing (GstH264NalParser * nalparser,
970     GstH264PicTiming * tim, NalReader * nr)
971 {
972   GstH264ParserResult error = GST_H264_PARSER_ERROR;
973   gboolean CpbDpbDelaysPresentFlag = FALSE;
974   gboolean pic_struct_present_flag = FALSE;
975
976   GST_DEBUG ("parsing \"Picture timing\"");
977   if (!nalparser->last_sps || !nalparser->last_sps->valid) {
978     GST_WARNING ("didn't get the associated sequence parameter set for the "
979         "current access unit");
980     error = GST_H264_PARSER_BROKEN_LINK;
981     goto error;
982   }
983
984   if (nalparser->last_sps->vui_parameters_present_flag) {
985     GstH264VUIParams *vui = &nalparser->last_sps->vui_parameters;
986
987     CpbDpbDelaysPresentFlag = vui->nal_hrd_parameters_present_flag
988         || vui->vcl_hrd_parameters_present_flag;
989     tim->pic_struct_present_flag = pic_struct_present_flag =
990         vui->pic_struct_present_flag;
991
992     if (vui->nal_hrd_parameters_present_flag) {
993       READ_UINT32 (nr, tim->cpb_removal_delay,
994           vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
995       READ_UINT32 (nr, tim->dpb_output_delay,
996           vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
997     } else if (vui->vcl_hrd_parameters_present_flag) {
998       READ_UINT32 (nr, tim->cpb_removal_delay,
999           vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1000       READ_UINT32 (nr, tim->dpb_output_delay,
1001           vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1002     }
1003
1004     if (pic_struct_present_flag) {
1005       const guint8 num_clock_ts_table[9] = {
1006         1, 1, 1, 2, 2, 3, 3, 2, 3
1007       };
1008       guint8 num_clock_num_ts;
1009       guint i;
1010
1011       READ_UINT8 (nr, tim->pic_struct, 4);
1012       CHECK_ALLOWED ((gint8) tim->pic_struct, 0, 8);
1013
1014       num_clock_num_ts = num_clock_ts_table[tim->pic_struct];
1015       for (i = 0; i < num_clock_num_ts; i++) {
1016         READ_UINT8 (nr, tim->clock_timestamp_flag[i], 1);
1017         if (tim->clock_timestamp_flag[i]) {
1018           if (!gst_h264_parse_clock_timestamp (&tim->clock_timestamp[i], vui,
1019                   nr))
1020             goto error;
1021         }
1022       }
1023     }
1024   }
1025
1026   if (!CpbDpbDelaysPresentFlag && !pic_struct_present_flag) {
1027     GST_WARNING
1028         ("Invalid pic_timing SEI NAL with neither CpbDpbDelays nor pic_struct");
1029     return GST_H264_PARSER_BROKEN_DATA;
1030   }
1031
1032   return GST_H264_PARSER_OK;
1033
1034 error:
1035   GST_WARNING ("error parsing \"Picture timing\"");
1036   return error;
1037 }
1038
1039 static GstH264ParserResult
1040 gst_h264_parser_parse_registered_user_data (GstH264NalParser * nalparser,
1041     GstH264RegisteredUserData * rud, NalReader * nr, guint payload_size)
1042 {
1043   guint8 *data = NULL;
1044   guint i;
1045
1046   rud->data = NULL;
1047   rud->size = 0;
1048
1049   if (payload_size < 2)
1050     return GST_H264_PARSER_ERROR;
1051
1052   READ_UINT8 (nr, rud->country_code, 8);
1053   --payload_size;
1054
1055   if (rud->country_code == 0xFF) {
1056     READ_UINT8 (nr, rud->country_code_extension, 8);
1057     --payload_size;
1058   } else {
1059     rud->country_code_extension = 0;
1060   }
1061
1062   if (payload_size < 8)
1063     return GST_H264_PARSER_ERROR;
1064
1065   data = g_malloc (payload_size);
1066   for (i = 0; i < payload_size / 8; ++i) {
1067     READ_UINT8 (nr, data[i], 8);
1068   }
1069
1070   GST_MEMDUMP ("SEI user data", data, payload_size / 8);
1071
1072   rud->data = data;
1073   rud->size = payload_size;
1074   return GST_H264_PARSER_OK;
1075
1076 error:
1077   {
1078     GST_WARNING ("error parsing \"Registered User Data\"");
1079     g_free (data);
1080     return GST_H264_PARSER_ERROR;
1081   }
1082 }
1083
1084 static GstH264ParserResult
1085 gst_h264_parser_parse_recovery_point (GstH264NalParser * nalparser,
1086     GstH264RecoveryPoint * rp, NalReader * nr)
1087 {
1088   GstH264SPS *const sps = nalparser->last_sps;
1089
1090   GST_DEBUG ("parsing \"Recovery point\"");
1091   if (!sps || !sps->valid) {
1092     GST_WARNING ("didn't get the associated sequence paramater set for the "
1093         "current access unit");
1094     goto error;
1095   }
1096
1097   READ_UE_MAX (nr, rp->recovery_frame_cnt, sps->max_frame_num - 1);
1098   READ_UINT8 (nr, rp->exact_match_flag, 1);
1099   READ_UINT8 (nr, rp->broken_link_flag, 1);
1100   READ_UINT8 (nr, rp->changing_slice_group_idc, 2);
1101
1102   return GST_H264_PARSER_OK;
1103
1104 error:
1105   GST_WARNING ("error parsing \"Recovery point\"");
1106   return GST_H264_PARSER_ERROR;
1107 }
1108
1109 /* Parse SEI stereo_video_info() message */
1110 static GstH264ParserResult
1111 gst_h264_parser_parse_stereo_video_info (GstH264NalParser * nalparser,
1112     GstH264StereoVideoInfo * info, NalReader * nr)
1113 {
1114   GST_DEBUG ("parsing \"Stereo Video info\"");
1115
1116   READ_UINT8 (nr, info->field_views_flag, 1);
1117   if (info->field_views_flag) {
1118     READ_UINT8 (nr, info->top_field_is_left_view_flag, 1);
1119   } else {
1120     READ_UINT8 (nr, info->current_frame_is_left_view_flag, 1);
1121     READ_UINT8 (nr, info->next_frame_is_second_view_flag, 1);
1122   }
1123   READ_UINT8 (nr, info->left_view_self_contained_flag, 1);
1124   READ_UINT8 (nr, info->right_view_self_contained_flag, 1);
1125
1126   return GST_H264_PARSER_OK;
1127
1128 error:
1129   GST_WARNING ("error parsing \"Stereo Video info\"");
1130   return GST_H264_PARSER_ERROR;
1131 }
1132
1133 /* Parse SEI frame_packing_arrangement() message */
1134 static GstH264ParserResult
1135 gst_h264_parser_parse_frame_packing (GstH264NalParser * nalparser,
1136     GstH264FramePacking * frame_packing, NalReader * nr, guint payload_size)
1137 {
1138   guint8 frame_packing_extension_flag;
1139   guint start_pos;
1140
1141   GST_DEBUG ("parsing \"Frame Packing Arrangement\"");
1142
1143   start_pos = nal_reader_get_pos (nr);
1144   READ_UE (nr, frame_packing->frame_packing_id);
1145   READ_UINT8 (nr, frame_packing->frame_packing_cancel_flag, 1);
1146
1147   if (!frame_packing->frame_packing_cancel_flag) {
1148     READ_UINT8 (nr, frame_packing->frame_packing_type, 7);
1149     READ_UINT8 (nr, frame_packing->quincunx_sampling_flag, 1);
1150     READ_UINT8 (nr, frame_packing->content_interpretation_type, 6);
1151     READ_UINT8 (nr, frame_packing->spatial_flipping_flag, 1);
1152     READ_UINT8 (nr, frame_packing->frame0_flipped_flag, 1);
1153     READ_UINT8 (nr, frame_packing->field_views_flag, 1);
1154     READ_UINT8 (nr, frame_packing->current_frame_is_frame0_flag, 1);
1155     READ_UINT8 (nr, frame_packing->frame0_self_contained_flag, 1);
1156     READ_UINT8 (nr, frame_packing->frame1_self_contained_flag, 1);
1157
1158     if (!frame_packing->quincunx_sampling_flag &&
1159         frame_packing->frame_packing_type !=
1160         GST_H264_FRAME_PACKING_TEMPORAL_INTERLEAVING) {
1161       READ_UINT8 (nr, frame_packing->frame0_grid_position_x, 4);
1162       READ_UINT8 (nr, frame_packing->frame0_grid_position_y, 4);
1163       READ_UINT8 (nr, frame_packing->frame1_grid_position_x, 4);
1164       READ_UINT8 (nr, frame_packing->frame1_grid_position_y, 4);
1165     }
1166
1167     /* Skip frame_packing_arrangement_reserved_byte */
1168     if (!nal_reader_skip (nr, 8))
1169       goto error;
1170
1171     READ_UE_MAX (nr, frame_packing->frame_packing_repetition_period, 16384);
1172   }
1173
1174   READ_UINT8 (nr, frame_packing_extension_flag, 1);
1175
1176   /* All data that follows within a frame packing arrangement SEI message
1177      after the value 1 for frame_packing_arrangement_extension_flag shall
1178      be ignored (D.2.25) */
1179   if (frame_packing_extension_flag) {
1180     nal_reader_skip_long (nr,
1181         payload_size - (nal_reader_get_pos (nr) - start_pos));
1182   }
1183
1184   return GST_H264_PARSER_OK;
1185
1186 error:
1187   GST_WARNING ("error parsing \"Frame Packing Arrangement\"");
1188   return GST_H264_PARSER_ERROR;
1189 }
1190
1191 static GstH264ParserResult
1192 gst_h264_parser_parse_sei_message (GstH264NalParser * nalparser,
1193     NalReader * nr, GstH264SEIMessage * sei)
1194 {
1195   guint32 payloadSize;
1196   guint8 payload_type_byte, payload_size_byte;
1197   guint remaining, payload_size, next;
1198   GstH264ParserResult res;
1199
1200   GST_DEBUG ("parsing \"SEI message\"");
1201
1202   memset (sei, 0, sizeof (*sei));
1203
1204   do {
1205     READ_UINT8 (nr, payload_type_byte, 8);
1206     sei->payloadType += payload_type_byte;
1207   } while (payload_type_byte == 0xff);
1208
1209   payloadSize = 0;
1210   do {
1211     READ_UINT8 (nr, payload_size_byte, 8);
1212     payloadSize += payload_size_byte;
1213   }
1214   while (payload_size_byte == 0xff);
1215
1216   remaining = nal_reader_get_remaining (nr);
1217   payload_size = payloadSize * 8 < remaining ? payloadSize * 8 : remaining;
1218   next = nal_reader_get_pos (nr) + payload_size;
1219
1220   GST_DEBUG ("SEI message received: payloadType  %u, payloadSize = %u bits",
1221       sei->payloadType, payload_size);
1222
1223   switch (sei->payloadType) {
1224     case GST_H264_SEI_BUF_PERIOD:
1225       /* size not set; might depend on emulation_prevention_three_byte */
1226       res = gst_h264_parser_parse_buffering_period (nalparser,
1227           &sei->payload.buffering_period, nr);
1228       break;
1229     case GST_H264_SEI_PIC_TIMING:
1230       /* size not set; might depend on emulation_prevention_three_byte */
1231       res = gst_h264_parser_parse_pic_timing (nalparser,
1232           &sei->payload.pic_timing, nr);
1233       break;
1234     case GST_H264_SEI_REGISTERED_USER_DATA:
1235       res = gst_h264_parser_parse_registered_user_data (nalparser,
1236           &sei->payload.registered_user_data, nr, payload_size);
1237       break;
1238     case GST_H264_SEI_RECOVERY_POINT:
1239       res = gst_h264_parser_parse_recovery_point (nalparser,
1240           &sei->payload.recovery_point, nr);
1241       break;
1242     case GST_H264_SEI_STEREO_VIDEO_INFO:
1243       res = gst_h264_parser_parse_stereo_video_info (nalparser,
1244           &sei->payload.stereo_video_info, nr);
1245       break;
1246     case GST_H264_SEI_FRAME_PACKING:
1247       res = gst_h264_parser_parse_frame_packing (nalparser,
1248           &sei->payload.frame_packing, nr, payload_size);
1249       break;
1250     default:
1251       /* Just consume payloadSize bytes, which does not account for
1252          emulation prevention bytes */
1253       if (!nal_reader_skip_long (nr, payload_size))
1254         goto error;
1255       res = GST_H264_PARSER_OK;
1256       break;
1257   }
1258
1259   /* When SEI message doesn't end at byte boundary,
1260    * check remaining bits fit the specification.
1261    */
1262   if (!nal_reader_is_byte_aligned (nr)) {
1263     guint8 bit_equal_to_one;
1264     READ_UINT8 (nr, bit_equal_to_one, 1);
1265     if (!bit_equal_to_one)
1266       GST_WARNING ("Bit non equal to one.");
1267
1268     while (!nal_reader_is_byte_aligned (nr)) {
1269       guint8 bit_equal_to_zero;
1270       READ_UINT8 (nr, bit_equal_to_zero, 1);
1271       if (bit_equal_to_zero)
1272         GST_WARNING ("Bit non equal to zero.");
1273     }
1274   }
1275
1276   /* Always make sure all the advertised SEI bits
1277    * were consumed during parsing */
1278   if (next > nal_reader_get_pos (nr)) {
1279     GST_LOG ("Skipping %u unused SEI bits", next - nal_reader_get_pos (nr));
1280
1281     if (!nal_reader_skip_long (nr, next - nal_reader_get_pos (nr)))
1282       goto error;
1283   }
1284
1285   return res;
1286
1287 error:
1288   GST_WARNING ("error parsing \"Sei message\"");
1289   return GST_H264_PARSER_ERROR;
1290 }
1291
1292 /******** API *************/
1293
1294 /**
1295  * gst_h264_nal_parser_new:
1296  *
1297  * Creates a new #GstH264NalParser. It should be freed with
1298  * gst_h264_nal_parser_free after use.
1299  *
1300  * Returns: a new #GstH264NalParser
1301  */
1302 GstH264NalParser *
1303 gst_h264_nal_parser_new (void)
1304 {
1305   GstH264NalParser *nalparser;
1306
1307   nalparser = g_slice_new0 (GstH264NalParser);
1308
1309   return nalparser;
1310 }
1311
1312 /**
1313  * gst_h264_nal_parser_free:
1314  * @nalparser: the #GstH264NalParser to free
1315  *
1316  * Frees @nalparser and sets it to %NULL
1317  */
1318 void
1319 gst_h264_nal_parser_free (GstH264NalParser * nalparser)
1320 {
1321   guint i;
1322
1323   for (i = 0; i < GST_H264_MAX_SPS_COUNT; i++)
1324     gst_h264_sps_clear (&nalparser->sps[i]);
1325   for (i = 0; i < GST_H264_MAX_PPS_COUNT; i++)
1326     gst_h264_pps_clear (&nalparser->pps[i]);
1327   g_slice_free (GstH264NalParser, nalparser);
1328
1329   nalparser = NULL;
1330 }
1331
1332 /**
1333  * gst_h264_parser_identify_nalu_unchecked:
1334  * @nalparser: a #GstH264NalParser
1335  * @data: The data to parse
1336  * @offset: the offset from which to parse @data
1337  * @size: the size of @data
1338  * @nalu: The #GstH264NalUnit where to store parsed nal headers
1339  *
1340  * Parses @data and fills @nalu from the next nalu data from @data.
1341  *
1342  * This differs from @gst_h264_parser_identify_nalu in that it doesn't
1343  * check whether the packet is complete or not.
1344  *
1345  * Note: Only use this function if you already know the provided @data
1346  * is a complete NALU, else use @gst_h264_parser_identify_nalu.
1347  *
1348  * Returns: a #GstH264ParserResult
1349  */
1350 GstH264ParserResult
1351 gst_h264_parser_identify_nalu_unchecked (GstH264NalParser * nalparser,
1352     const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1353 {
1354   gint off1;
1355
1356   memset (nalu, 0, sizeof (*nalu));
1357
1358   if (size < offset + 4) {
1359     GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1360         ", offset %u", size, offset);
1361     return GST_H264_PARSER_ERROR;
1362   }
1363
1364   off1 = scan_for_start_codes (data + offset, size - offset);
1365
1366   if (off1 < 0) {
1367     GST_DEBUG ("No start code prefix in this buffer");
1368     return GST_H264_PARSER_NO_NAL;
1369   }
1370
1371   if (offset + off1 == size - 1) {
1372     GST_DEBUG ("Missing data to identify nal unit");
1373
1374     return GST_H264_PARSER_ERROR;
1375   }
1376
1377   nalu->sc_offset = offset + off1;
1378
1379
1380   nalu->offset = offset + off1 + 3;
1381   nalu->data = (guint8 *) data;
1382   nalu->size = size - nalu->offset;
1383
1384   if (!gst_h264_parse_nalu_header (nalu)) {
1385     GST_WARNING ("error parsing \"NAL unit header\"");
1386     nalu->size = 0;
1387     return GST_H264_PARSER_BROKEN_DATA;
1388   }
1389
1390   nalu->valid = TRUE;
1391
1392   /* sc might have 2 or 3 0-bytes */
1393   if (nalu->sc_offset > 0 && data[nalu->sc_offset - 1] == 00
1394       && (nalu->type == GST_H264_NAL_SPS || nalu->type == GST_H264_NAL_PPS
1395           || nalu->type == GST_H264_NAL_AU_DELIMITER))
1396     nalu->sc_offset--;
1397
1398   if (nalu->type == GST_H264_NAL_SEQ_END ||
1399       nalu->type == GST_H264_NAL_STREAM_END) {
1400     GST_DEBUG ("end-of-seq or end-of-stream nal found");
1401     nalu->size = 1;
1402     return GST_H264_PARSER_OK;
1403   }
1404
1405   return GST_H264_PARSER_OK;
1406 }
1407
1408 /**
1409  * gst_h264_parser_identify_nalu:
1410  * @nalparser: a #GstH264NalParser
1411  * @data: The data to parse, containing an Annex B coded NAL unit
1412  * @offset: the offset in @data from which to parse the NAL unit
1413  * @size: the size of @data
1414  * @nalu: The #GstH264NalUnit to store the identified NAL unit in
1415  *
1416  * Parses the headers of an Annex B coded NAL unit from @data and puts the
1417  * result into @nalu.
1418  *
1419  * Returns: a #GstH264ParserResult
1420  */
1421 GstH264ParserResult
1422 gst_h264_parser_identify_nalu (GstH264NalParser * nalparser,
1423     const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1424 {
1425   GstH264ParserResult res;
1426   gint off2;
1427
1428   res =
1429       gst_h264_parser_identify_nalu_unchecked (nalparser, data, offset, size,
1430       nalu);
1431
1432   if (res != GST_H264_PARSER_OK)
1433     goto beach;
1434
1435   /* The two NALs are exactly 1 byte size and are placed at the end of an AU,
1436    * there is no need to wait for the following */
1437   if (nalu->type == GST_H264_NAL_SEQ_END ||
1438       nalu->type == GST_H264_NAL_STREAM_END)
1439     goto beach;
1440
1441   off2 = scan_for_start_codes (data + nalu->offset, size - nalu->offset);
1442   if (off2 < 0) {
1443     GST_DEBUG ("Nal start %d, No end found", nalu->offset);
1444
1445     return GST_H264_PARSER_NO_NAL_END;
1446   }
1447
1448   /* Mini performance improvement:
1449    * We could have a way to store how many 0s were skipped to avoid
1450    * parsing them again on the next NAL */
1451   while (off2 > 0 && data[nalu->offset + off2 - 1] == 00)
1452     off2--;
1453
1454   nalu->size = off2;
1455   if (nalu->size < 2)
1456     return GST_H264_PARSER_BROKEN_DATA;
1457
1458   GST_DEBUG ("Complete nal found. Off: %d, Size: %d", nalu->offset, nalu->size);
1459
1460 beach:
1461   return res;
1462 }
1463
1464
1465 /**
1466  * gst_h264_parser_identify_nalu_avc:
1467  * @nalparser: a #GstH264NalParser
1468  * @data: The data to parse, containing an AVC coded NAL unit
1469  * @offset: the offset in @data from which to parse the NAL unit
1470  * @size: the size of @data
1471  * @nal_length_size: the size in bytes of the AVC nal length prefix.
1472  * @nalu: The #GstH264NalUnit to store the identified NAL unit in
1473  *
1474  * Parses the headers of an AVC coded NAL unit from @data and puts the result
1475  * into @nalu.
1476  *
1477  * Returns: a #GstH264ParserResult
1478  */
1479 GstH264ParserResult
1480 gst_h264_parser_identify_nalu_avc (GstH264NalParser * nalparser,
1481     const guint8 * data, guint offset, gsize size, guint8 nal_length_size,
1482     GstH264NalUnit * nalu)
1483 {
1484   GstBitReader br;
1485
1486   memset (nalu, 0, sizeof (*nalu));
1487
1488   if (size < offset + nal_length_size) {
1489     GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1490         ", offset %u", size, offset);
1491     return GST_H264_PARSER_ERROR;
1492   }
1493
1494   size = size - offset;
1495   gst_bit_reader_init (&br, data + offset, size);
1496
1497   nalu->size = gst_bit_reader_get_bits_uint32_unchecked (&br,
1498       nal_length_size * 8);
1499   nalu->sc_offset = offset;
1500   nalu->offset = offset + nal_length_size;
1501
1502   if (size < nalu->size + nal_length_size) {
1503     nalu->size = 0;
1504
1505     return GST_H264_PARSER_NO_NAL_END;
1506   }
1507
1508   nalu->data = (guint8 *) data;
1509
1510   if (!gst_h264_parse_nalu_header (nalu)) {
1511     GST_WARNING ("error parsing \"NAL unit header\"");
1512     nalu->size = 0;
1513     return GST_H264_PARSER_BROKEN_DATA;
1514   }
1515
1516   nalu->valid = TRUE;
1517
1518   return GST_H264_PARSER_OK;
1519 }
1520
1521 /**
1522  * gst_h264_parser_parse_nal:
1523  * @nalparser: a #GstH264NalParser
1524  * @nalu: The #GstH264NalUnit to parse
1525  *
1526  * This function should be called in the case one doesn't need to
1527  * parse a specific structure. It is necessary to do so to make
1528  * sure @nalparser is up to date.
1529  *
1530  * Returns: a #GstH264ParserResult
1531  */
1532 GstH264ParserResult
1533 gst_h264_parser_parse_nal (GstH264NalParser * nalparser, GstH264NalUnit * nalu)
1534 {
1535   GstH264SPS sps;
1536   GstH264PPS pps;
1537
1538   switch (nalu->type) {
1539     case GST_H264_NAL_SPS:
1540       return gst_h264_parser_parse_sps (nalparser, nalu, &sps);
1541       break;
1542     case GST_H264_NAL_PPS:
1543       return gst_h264_parser_parse_pps (nalparser, nalu, &pps);
1544   }
1545
1546   return GST_H264_PARSER_OK;
1547 }
1548
1549 /**
1550  * gst_h264_parser_parse_sps:
1551  * @nalparser: a #GstH264NalParser
1552  * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1553  * @sps: The #GstH264SPS to fill.
1554  *
1555  * Parses @nalu containing a Sequence Parameter Set, and fills @sps.
1556  *
1557  * Returns: a #GstH264ParserResult
1558  */
1559 GstH264ParserResult
1560 gst_h264_parser_parse_sps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1561     GstH264SPS * sps)
1562 {
1563   GstH264ParserResult res = gst_h264_parse_sps (nalu, sps);
1564
1565   if (res == GST_H264_PARSER_OK) {
1566     GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
1567
1568     if (!gst_h264_sps_copy (&nalparser->sps[sps->id], sps))
1569       return GST_H264_PARSER_ERROR;
1570     nalparser->last_sps = &nalparser->sps[sps->id];
1571   }
1572   return res;
1573 }
1574
1575 /* Parse seq_parameter_set_data() */
1576 static gboolean
1577 gst_h264_parse_sps_data (NalReader * nr, GstH264SPS * sps)
1578 {
1579   gint width, height;
1580   guint subwc[] = { 1, 2, 2, 1 };
1581   guint subhc[] = { 1, 2, 1, 1 };
1582
1583   memset (sps, 0, sizeof (*sps));
1584
1585   /* set default values for fields that might not be present in the bitstream
1586      and have valid defaults */
1587   sps->extension_type = GST_H264_NAL_EXTENSION_NONE;
1588   sps->chroma_format_idc = 1;
1589   memset (sps->scaling_lists_4x4, 16, 96);
1590   memset (sps->scaling_lists_8x8, 16, 384);
1591
1592   READ_UINT8 (nr, sps->profile_idc, 8);
1593   READ_UINT8 (nr, sps->constraint_set0_flag, 1);
1594   READ_UINT8 (nr, sps->constraint_set1_flag, 1);
1595   READ_UINT8 (nr, sps->constraint_set2_flag, 1);
1596   READ_UINT8 (nr, sps->constraint_set3_flag, 1);
1597   READ_UINT8 (nr, sps->constraint_set4_flag, 1);
1598   READ_UINT8 (nr, sps->constraint_set5_flag, 1);
1599
1600   /* skip reserved_zero_2bits */
1601   if (!nal_reader_skip (nr, 2))
1602     goto error;
1603
1604   READ_UINT8 (nr, sps->level_idc, 8);
1605
1606   READ_UE_MAX (nr, sps->id, GST_H264_MAX_SPS_COUNT - 1);
1607
1608   if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
1609       sps->profile_idc == 122 || sps->profile_idc == 244 ||
1610       sps->profile_idc == 44 || sps->profile_idc == 83 ||
1611       sps->profile_idc == 86 || sps->profile_idc == 118 ||
1612       sps->profile_idc == 128 || sps->profile_idc == 138 ||
1613       sps->profile_idc == 139 || sps->profile_idc == 134 ||
1614       sps->profile_idc == 135) {
1615     READ_UE_MAX (nr, sps->chroma_format_idc, 3);
1616     if (sps->chroma_format_idc == 3)
1617       READ_UINT8 (nr, sps->separate_colour_plane_flag, 1);
1618
1619     READ_UE_MAX (nr, sps->bit_depth_luma_minus8, 6);
1620     READ_UE_MAX (nr, sps->bit_depth_chroma_minus8, 6);
1621     READ_UINT8 (nr, sps->qpprime_y_zero_transform_bypass_flag, 1);
1622
1623     READ_UINT8 (nr, sps->scaling_matrix_present_flag, 1);
1624     if (sps->scaling_matrix_present_flag) {
1625       guint8 n_lists;
1626
1627       n_lists = (sps->chroma_format_idc != 3) ? 8 : 12;
1628       if (!gst_h264_parser_parse_scaling_list (nr,
1629               sps->scaling_lists_4x4, sps->scaling_lists_8x8,
1630               default_4x4_inter, default_4x4_intra,
1631               default_8x8_inter, default_8x8_intra, n_lists))
1632         goto error;
1633     }
1634   }
1635
1636   READ_UE_MAX (nr, sps->log2_max_frame_num_minus4, 12);
1637
1638   sps->max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
1639
1640   READ_UE_MAX (nr, sps->pic_order_cnt_type, 2);
1641   if (sps->pic_order_cnt_type == 0) {
1642     READ_UE_MAX (nr, sps->log2_max_pic_order_cnt_lsb_minus4, 12);
1643   } else if (sps->pic_order_cnt_type == 1) {
1644     guint i;
1645
1646     READ_UINT8 (nr, sps->delta_pic_order_always_zero_flag, 1);
1647     READ_SE (nr, sps->offset_for_non_ref_pic);
1648     READ_SE (nr, sps->offset_for_top_to_bottom_field);
1649     READ_UE_MAX (nr, sps->num_ref_frames_in_pic_order_cnt_cycle, 255);
1650
1651     for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
1652       READ_SE (nr, sps->offset_for_ref_frame[i]);
1653   }
1654
1655   READ_UE (nr, sps->num_ref_frames);
1656   READ_UINT8 (nr, sps->gaps_in_frame_num_value_allowed_flag, 1);
1657   READ_UE (nr, sps->pic_width_in_mbs_minus1);
1658   READ_UE (nr, sps->pic_height_in_map_units_minus1);
1659   READ_UINT8 (nr, sps->frame_mbs_only_flag, 1);
1660
1661   if (!sps->frame_mbs_only_flag)
1662     READ_UINT8 (nr, sps->mb_adaptive_frame_field_flag, 1);
1663
1664   READ_UINT8 (nr, sps->direct_8x8_inference_flag, 1);
1665   READ_UINT8 (nr, sps->frame_cropping_flag, 1);
1666   if (sps->frame_cropping_flag) {
1667     READ_UE (nr, sps->frame_crop_left_offset);
1668     READ_UE (nr, sps->frame_crop_right_offset);
1669     READ_UE (nr, sps->frame_crop_top_offset);
1670     READ_UE (nr, sps->frame_crop_bottom_offset);
1671   }
1672
1673   READ_UINT8 (nr, sps->vui_parameters_present_flag, 1);
1674   if (sps->vui_parameters_present_flag)
1675     if (!gst_h264_parse_vui_parameters (sps, nr))
1676       goto error;
1677
1678   /* calculate ChromaArrayType */
1679   if (!sps->separate_colour_plane_flag)
1680     sps->chroma_array_type = sps->chroma_format_idc;
1681
1682   /* Calculate  width and height */
1683   width = (sps->pic_width_in_mbs_minus1 + 1);
1684   width *= 16;
1685   height = (sps->pic_height_in_map_units_minus1 + 1);
1686   height *= 16 * (2 - sps->frame_mbs_only_flag);
1687   GST_LOG ("initial width=%d, height=%d", width, height);
1688   if (width < 0 || height < 0) {
1689     GST_WARNING ("invalid width/height in SPS");
1690     goto error;
1691   }
1692
1693   sps->width = width;
1694   sps->height = height;
1695
1696   if (sps->frame_cropping_flag) {
1697     const guint crop_unit_x = subwc[sps->chroma_format_idc];
1698     const guint crop_unit_y =
1699         subhc[sps->chroma_format_idc] * (2 - sps->frame_mbs_only_flag);
1700
1701     width -= (sps->frame_crop_left_offset + sps->frame_crop_right_offset)
1702         * crop_unit_x;
1703     height -= (sps->frame_crop_top_offset + sps->frame_crop_bottom_offset)
1704         * crop_unit_y;
1705
1706     sps->crop_rect_width = width;
1707     sps->crop_rect_height = height;
1708     sps->crop_rect_x = sps->frame_crop_left_offset * crop_unit_x;
1709     sps->crop_rect_y = sps->frame_crop_top_offset * crop_unit_y;
1710
1711     GST_LOG ("crop_rectangle x=%u y=%u width=%u, height=%u", sps->crop_rect_x,
1712         sps->crop_rect_y, width, height);
1713   }
1714
1715   sps->fps_num_removed = 0;
1716   sps->fps_den_removed = 1;
1717
1718   return TRUE;
1719
1720 error:
1721   return FALSE;
1722 }
1723
1724 /* Parse subset_seq_parameter_set() data for MVC */
1725 static gboolean
1726 gst_h264_parse_sps_mvc_data (NalReader * nr, GstH264SPS * sps)
1727 {
1728   GstH264SPSExtMVC *const mvc = &sps->extension.mvc;
1729   guint8 bit_equal_to_one;
1730   guint i, j, k;
1731
1732   READ_UINT8 (nr, bit_equal_to_one, 1);
1733   if (!bit_equal_to_one)
1734     return FALSE;
1735
1736   sps->extension_type = GST_H264_NAL_EXTENSION_MVC;
1737
1738   READ_UE_MAX (nr, mvc->num_views_minus1, GST_H264_MAX_VIEW_COUNT - 1);
1739
1740   mvc->view = g_new0 (GstH264SPSExtMVCView, mvc->num_views_minus1 + 1);
1741   if (!mvc->view)
1742     goto error_allocation_failed;
1743
1744   for (i = 0; i <= mvc->num_views_minus1; i++)
1745     READ_UE_MAX (nr, mvc->view[i].view_id, GST_H264_MAX_VIEW_ID);
1746
1747   for (i = 1; i <= mvc->num_views_minus1; i++) {
1748     /* for RefPicList0 */
1749     READ_UE_MAX (nr, mvc->view[i].num_anchor_refs_l0, 15);
1750     for (j = 0; j < mvc->view[i].num_anchor_refs_l0; j++) {
1751       READ_UE_MAX (nr, mvc->view[i].anchor_ref_l0[j], GST_H264_MAX_VIEW_ID);
1752     }
1753
1754     /* for RefPicList1 */
1755     READ_UE_MAX (nr, mvc->view[i].num_anchor_refs_l1, 15);
1756     for (j = 0; j < mvc->view[i].num_anchor_refs_l1; j++) {
1757       READ_UE_MAX (nr, mvc->view[i].anchor_ref_l1[j], GST_H264_MAX_VIEW_ID);
1758     }
1759   }
1760
1761   for (i = 1; i <= mvc->num_views_minus1; i++) {
1762     /* for RefPicList0 */
1763     READ_UE_MAX (nr, mvc->view[i].num_non_anchor_refs_l0, 15);
1764     for (j = 0; j < mvc->view[i].num_non_anchor_refs_l0; j++) {
1765       READ_UE_MAX (nr, mvc->view[i].non_anchor_ref_l0[j], GST_H264_MAX_VIEW_ID);
1766     }
1767
1768     /* for RefPicList1 */
1769     READ_UE_MAX (nr, mvc->view[i].num_non_anchor_refs_l1, 15);
1770     for (j = 0; j < mvc->view[i].num_non_anchor_refs_l1; j++) {
1771       READ_UE_MAX (nr, mvc->view[i].non_anchor_ref_l1[j], GST_H264_MAX_VIEW_ID);
1772     }
1773   }
1774
1775   READ_UE_MAX (nr, mvc->num_level_values_signalled_minus1, 63);
1776
1777   mvc->level_value =
1778       g_new0 (GstH264SPSExtMVCLevelValue,
1779       mvc->num_level_values_signalled_minus1 + 1);
1780   if (!mvc->level_value)
1781     goto error_allocation_failed;
1782
1783   for (i = 0; i <= mvc->num_level_values_signalled_minus1; i++) {
1784     GstH264SPSExtMVCLevelValue *const level_value = &mvc->level_value[i];
1785
1786     READ_UINT8 (nr, level_value->level_idc, 8);
1787
1788     READ_UE_MAX (nr, level_value->num_applicable_ops_minus1, 1023);
1789     level_value->applicable_op =
1790         g_new0 (GstH264SPSExtMVCLevelValueOp,
1791         level_value->num_applicable_ops_minus1 + 1);
1792     if (!level_value->applicable_op)
1793       goto error_allocation_failed;
1794
1795     for (j = 0; j <= level_value->num_applicable_ops_minus1; j++) {
1796       GstH264SPSExtMVCLevelValueOp *const op = &level_value->applicable_op[j];
1797
1798       READ_UINT8 (nr, op->temporal_id, 3);
1799
1800       READ_UE_MAX (nr, op->num_target_views_minus1, 1023);
1801       op->target_view_id = g_new (guint16, op->num_target_views_minus1 + 1);
1802       if (!op->target_view_id)
1803         goto error_allocation_failed;
1804
1805       for (k = 0; k <= op->num_target_views_minus1; k++)
1806         READ_UE_MAX (nr, op->target_view_id[k], GST_H264_MAX_VIEW_ID);
1807       READ_UE_MAX (nr, op->num_views_minus1, 1023);
1808     }
1809   }
1810   return TRUE;
1811
1812 error_allocation_failed:
1813   GST_WARNING ("failed to allocate memory");
1814   gst_h264_sps_clear (sps);
1815   return FALSE;
1816
1817 error:
1818   gst_h264_sps_clear (sps);
1819   return FALSE;
1820 }
1821
1822 /**
1823  * gst_h264_parse_sps:
1824  * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1825  * @sps: The #GstH264SPS to fill.
1826  *
1827  * Parses @data, and fills the @sps structure.
1828  *
1829  * Returns: a #GstH264ParserResult
1830  */
1831 GstH264ParserResult
1832 gst_h264_parse_sps (GstH264NalUnit * nalu, GstH264SPS * sps)
1833 {
1834   NalReader nr;
1835
1836   GST_DEBUG ("parsing SPS");
1837
1838   nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
1839       nalu->size - nalu->header_bytes);
1840
1841   if (!gst_h264_parse_sps_data (&nr, sps))
1842     goto error;
1843
1844   sps->valid = TRUE;
1845
1846   return GST_H264_PARSER_OK;
1847
1848 error:
1849   GST_WARNING ("error parsing \"Sequence parameter set\"");
1850   sps->valid = FALSE;
1851   return GST_H264_PARSER_ERROR;
1852 }
1853
1854 /**
1855  * gst_h264_parser_parse_subset_sps:
1856  * @nalparser: a #GstH264NalParser
1857  * @nalu: The #GST_H264_NAL_SUBSET_SPS #GstH264NalUnit to parse
1858  * @sps: The #GstH264SPS to fill.
1859  *
1860  * Parses @data, and fills in the @sps structure.
1861  *
1862  * This function fully parses @data and allocates all the necessary
1863  * data structures needed for MVC extensions. The resulting @sps
1864  * structure shall be deallocated with gst_h264_sps_clear() when it is
1865  * no longer needed.
1866  *
1867  * Note: if the caller doesn't need any of the MVC-specific data, then
1868  * gst_h264_parser_parse_sps() is more efficient because those extra
1869  * syntax elements are not parsed and no extra memory is allocated.
1870  *
1871  * Returns: a #GstH264ParserResult
1872  *
1873  * Since: 1.6
1874  */
1875 GstH264ParserResult
1876 gst_h264_parser_parse_subset_sps (GstH264NalParser * nalparser,
1877     GstH264NalUnit * nalu, GstH264SPS * sps)
1878 {
1879   GstH264ParserResult res;
1880
1881   res = gst_h264_parse_subset_sps (nalu, sps);
1882   if (res == GST_H264_PARSER_OK) {
1883     GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
1884
1885     if (!gst_h264_sps_copy (&nalparser->sps[sps->id], sps)) {
1886       gst_h264_sps_clear (sps);
1887       return GST_H264_PARSER_ERROR;
1888     }
1889     nalparser->last_sps = &nalparser->sps[sps->id];
1890   }
1891   return res;
1892 }
1893
1894 /**
1895  * gst_h264_parse_subset_sps:
1896  * @nalu: The #GST_H264_NAL_SUBSET_SPS #GstH264NalUnit to parse
1897  * @sps: The #GstH264SPS to fill.
1898  *
1899  * Parses @data, and fills in the @sps structure.
1900  *
1901  * This function fully parses @data and allocates all the necessary
1902  * data structures needed for MVC extensions. The resulting @sps
1903  * structure shall be deallocated with gst_h264_sps_clear() when it is
1904  * no longer needed.
1905  *
1906  * Note: if the caller doesn't need any of the MVC-specific data, then
1907  * gst_h264_parser_parse_sps() is more efficient because those extra
1908  * syntax elements are not parsed and no extra memory is allocated.
1909  *
1910  * Returns: a #GstH264ParserResult
1911  *
1912  * Since: 1.6
1913  */
1914 GstH264ParserResult
1915 gst_h264_parse_subset_sps (GstH264NalUnit * nalu, GstH264SPS * sps)
1916 {
1917   NalReader nr;
1918
1919   GST_DEBUG ("parsing Subset SPS");
1920
1921   nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
1922       nalu->size - nalu->header_bytes);
1923
1924   if (!gst_h264_parse_sps_data (&nr, sps))
1925     goto error;
1926
1927   if (sps->profile_idc == GST_H264_PROFILE_MULTIVIEW_HIGH ||
1928       sps->profile_idc == GST_H264_PROFILE_STEREO_HIGH) {
1929     if (!gst_h264_parse_sps_mvc_data (&nr, sps))
1930       goto error;
1931   }
1932
1933   sps->valid = TRUE;
1934   return GST_H264_PARSER_OK;
1935
1936 error:
1937   GST_WARNING ("error parsing \"Subset sequence parameter set\"");
1938   gst_h264_sps_clear (sps);
1939   sps->valid = FALSE;
1940   return GST_H264_PARSER_ERROR;
1941 }
1942
1943 /**
1944  * gst_h264_parse_pps:
1945  * @nalparser: a #GstH264NalParser
1946  * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
1947  * @pps: The #GstH264PPS to fill.
1948  *
1949  * Parses @data, and fills the @pps structure.
1950  *
1951  * The resulting @pps data structure shall be deallocated with the
1952  * gst_h264_pps_clear() function when it is no longer needed, or prior
1953  * to parsing a new PPS NAL unit.
1954  *
1955  * Returns: a #GstH264ParserResult
1956  */
1957 GstH264ParserResult
1958 gst_h264_parse_pps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1959     GstH264PPS * pps)
1960 {
1961   NalReader nr;
1962   GstH264SPS *sps;
1963   gint sps_id;
1964   gint qp_bd_offset;
1965
1966   GST_DEBUG ("parsing PPS");
1967
1968   nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
1969       nalu->size - nalu->header_bytes);
1970
1971   memset (pps, 0, sizeof (*pps));
1972
1973   READ_UE_MAX (&nr, pps->id, GST_H264_MAX_PPS_COUNT - 1);
1974   READ_UE_MAX (&nr, sps_id, GST_H264_MAX_SPS_COUNT - 1);
1975
1976   sps = gst_h264_parser_get_sps (nalparser, sps_id);
1977   if (!sps) {
1978     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1979         sps_id);
1980     return GST_H264_PARSER_BROKEN_LINK;
1981   }
1982   pps->sequence = sps;
1983   qp_bd_offset = 6 * (sps->bit_depth_luma_minus8 +
1984       sps->separate_colour_plane_flag);
1985
1986   /* set default values for fields that might not be present in the bitstream
1987      and have valid defaults */
1988   memcpy (&pps->scaling_lists_4x4, &sps->scaling_lists_4x4, 96);
1989   memcpy (&pps->scaling_lists_8x8, &sps->scaling_lists_8x8, 384);
1990
1991   READ_UINT8 (&nr, pps->entropy_coding_mode_flag, 1);
1992   READ_UINT8 (&nr, pps->pic_order_present_flag, 1);
1993   READ_UE_MAX (&nr, pps->num_slice_groups_minus1, 7);
1994   if (pps->num_slice_groups_minus1 > 0) {
1995     READ_UE_MAX (&nr, pps->slice_group_map_type, 6);
1996
1997     if (pps->slice_group_map_type == 0) {
1998       gint i;
1999
2000       for (i = 0; i <= pps->num_slice_groups_minus1; i++)
2001         READ_UE (&nr, pps->run_length_minus1[i]);
2002     } else if (pps->slice_group_map_type == 2) {
2003       gint i;
2004
2005       for (i = 0; i < pps->num_slice_groups_minus1; i++) {
2006         READ_UE (&nr, pps->top_left[i]);
2007         READ_UE (&nr, pps->bottom_right[i]);
2008       }
2009     } else if (pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
2010       READ_UINT8 (&nr, pps->slice_group_change_direction_flag, 1);
2011       READ_UE (&nr, pps->slice_group_change_rate_minus1);
2012     } else if (pps->slice_group_map_type == 6) {
2013       gint bits;
2014       gint i;
2015
2016       READ_UE (&nr, pps->pic_size_in_map_units_minus1);
2017       bits = g_bit_storage (pps->num_slice_groups_minus1);
2018
2019       pps->slice_group_id =
2020           g_new (guint8, pps->pic_size_in_map_units_minus1 + 1);
2021       for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++)
2022         READ_UINT8 (&nr, pps->slice_group_id[i], bits);
2023     }
2024   }
2025
2026   READ_UE_MAX (&nr, pps->num_ref_idx_l0_active_minus1, 31);
2027   READ_UE_MAX (&nr, pps->num_ref_idx_l1_active_minus1, 31);
2028   READ_UINT8 (&nr, pps->weighted_pred_flag, 1);
2029   READ_UINT8 (&nr, pps->weighted_bipred_idc, 2);
2030   READ_SE_ALLOWED (&nr, pps->pic_init_qp_minus26, -(26 + qp_bd_offset), 25);
2031   READ_SE_ALLOWED (&nr, pps->pic_init_qs_minus26, -26, 25);
2032   READ_SE_ALLOWED (&nr, pps->chroma_qp_index_offset, -12, 12);
2033   pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
2034   READ_UINT8 (&nr, pps->deblocking_filter_control_present_flag, 1);
2035   READ_UINT8 (&nr, pps->constrained_intra_pred_flag, 1);
2036   READ_UINT8 (&nr, pps->redundant_pic_cnt_present_flag, 1);
2037
2038   if (!nal_reader_has_more_data (&nr))
2039     goto done;
2040
2041   READ_UINT8 (&nr, pps->transform_8x8_mode_flag, 1);
2042
2043   READ_UINT8 (&nr, pps->pic_scaling_matrix_present_flag, 1);
2044   if (pps->pic_scaling_matrix_present_flag) {
2045     guint8 n_lists;
2046
2047     n_lists = 6 + ((sps->chroma_format_idc != 3) ? 2 : 6) *
2048         pps->transform_8x8_mode_flag;
2049
2050     if (sps->scaling_matrix_present_flag) {
2051       if (!gst_h264_parser_parse_scaling_list (&nr,
2052               pps->scaling_lists_4x4, pps->scaling_lists_8x8,
2053               sps->scaling_lists_4x4[3], sps->scaling_lists_4x4[0],
2054               sps->scaling_lists_8x8[3], sps->scaling_lists_8x8[0], n_lists))
2055         goto error;
2056     } else {
2057       if (!gst_h264_parser_parse_scaling_list (&nr,
2058               pps->scaling_lists_4x4, pps->scaling_lists_8x8,
2059               default_4x4_inter, default_4x4_intra,
2060               default_8x8_inter, default_8x8_intra, n_lists))
2061         goto error;
2062     }
2063   }
2064
2065   READ_SE_ALLOWED (&nr, pps->second_chroma_qp_index_offset, -12, 12);
2066
2067 done:
2068   pps->valid = TRUE;
2069   return GST_H264_PARSER_OK;
2070
2071 error:
2072   GST_WARNING ("error parsing \"Picture parameter set\"");
2073   pps->valid = FALSE;
2074   gst_h264_pps_clear (pps);
2075   return GST_H264_PARSER_ERROR;
2076 }
2077
2078 /**
2079  * gst_h264_parser_parse_pps:
2080  * @nalparser: a #GstH264NalParser
2081  * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
2082  * @pps: The #GstH264PPS to fill.
2083  *
2084  * Parses @nalu containing a Picture Parameter Set, and fills @pps.
2085  *
2086  * The resulting @pps data structure must be deallocated by the caller using
2087  * gst_h264_pps_clear().
2088  *
2089  * Returns: a #GstH264ParserResult
2090  */
2091 GstH264ParserResult
2092 gst_h264_parser_parse_pps (GstH264NalParser * nalparser,
2093     GstH264NalUnit * nalu, GstH264PPS * pps)
2094 {
2095   GstH264ParserResult res = gst_h264_parse_pps (nalparser, nalu, pps);
2096
2097   if (res == GST_H264_PARSER_OK) {
2098     GST_DEBUG ("adding picture parameter set with id: %d to array", pps->id);
2099
2100     if (!gst_h264_pps_copy (&nalparser->pps[pps->id], pps))
2101       return GST_H264_PARSER_ERROR;
2102     nalparser->last_pps = &nalparser->pps[pps->id];
2103   }
2104
2105   return res;
2106 }
2107
2108 /**
2109  * gst_h264_pps_clear:
2110  * @pps: The #GstH264PPS to free
2111  *
2112  * Clears all @pps internal resources.
2113  *
2114  * Since: 1.4
2115  */
2116 void
2117 gst_h264_pps_clear (GstH264PPS * pps)
2118 {
2119   g_return_if_fail (pps != NULL);
2120
2121   g_free (pps->slice_group_id);
2122   pps->slice_group_id = NULL;
2123 }
2124
2125 /**
2126  * gst_h264_parser_parse_slice_hdr:
2127  * @nalparser: a #GstH264NalParser
2128  * @nalu: The #GST_H264_NAL_SLICE to #GST_H264_NAL_SLICE_IDR #GstH264NalUnit to parse
2129  * @slice: The #GstH264SliceHdr to fill.
2130  * @parse_pred_weight_table: Whether to parse the pred_weight_table or not
2131  * @parse_dec_ref_pic_marking: Whether to parse the dec_ref_pic_marking or not
2132  *
2133  * Parses @nalu containing a coded slice, and fills @slice.
2134  *
2135  * Returns: a #GstH264ParserResult
2136  */
2137 GstH264ParserResult
2138 gst_h264_parser_parse_slice_hdr (GstH264NalParser * nalparser,
2139     GstH264NalUnit * nalu, GstH264SliceHdr * slice,
2140     gboolean parse_pred_weight_table, gboolean parse_dec_ref_pic_marking)
2141 {
2142   NalReader nr;
2143   gint pps_id;
2144   GstH264PPS *pps;
2145   GstH264SPS *sps;
2146   guint start_pos;
2147
2148   memset (slice, 0, sizeof (*slice));
2149
2150   if (!nalu->size) {
2151     GST_DEBUG ("Invalid Nal Unit");
2152     return GST_H264_PARSER_ERROR;
2153   }
2154
2155   nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
2156       nalu->size - nalu->header_bytes);
2157
2158   READ_UE (&nr, slice->first_mb_in_slice);
2159   READ_UE (&nr, slice->type);
2160
2161   GST_DEBUG ("parsing \"Slice header\", slice type %u", slice->type);
2162
2163   READ_UE_MAX (&nr, pps_id, GST_H264_MAX_PPS_COUNT - 1);
2164   pps = gst_h264_parser_get_pps (nalparser, pps_id);
2165
2166   if (!pps) {
2167     GST_WARNING ("couldn't find associated picture parameter set with id: %d",
2168         pps_id);
2169
2170     return GST_H264_PARSER_BROKEN_LINK;
2171   }
2172
2173   slice->pps = pps;
2174   sps = pps->sequence;
2175   if (!sps) {
2176     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
2177         pps->id);
2178     return GST_H264_PARSER_BROKEN_LINK;
2179   }
2180
2181   /* Check we can actually parse this slice (AVC, MVC headers only) */
2182   if (sps->extension_type && sps->extension_type != GST_H264_NAL_EXTENSION_MVC) {
2183     GST_WARNING ("failed to parse unsupported slice header");
2184     return GST_H264_PARSER_BROKEN_DATA;
2185   }
2186
2187   /* set default values for fields that might not be present in the bitstream
2188      and have valid defaults */
2189   slice->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_active_minus1;
2190   slice->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_active_minus1;
2191
2192   if (sps->separate_colour_plane_flag)
2193     READ_UINT8 (&nr, slice->colour_plane_id, 2);
2194
2195   READ_UINT16 (&nr, slice->frame_num, sps->log2_max_frame_num_minus4 + 4);
2196
2197   if (!sps->frame_mbs_only_flag) {
2198     READ_UINT8 (&nr, slice->field_pic_flag, 1);
2199     if (slice->field_pic_flag)
2200       READ_UINT8 (&nr, slice->bottom_field_flag, 1);
2201   }
2202
2203   /* calculate MaxPicNum */
2204   if (slice->field_pic_flag)
2205     slice->max_pic_num = 2 * sps->max_frame_num;
2206   else
2207     slice->max_pic_num = sps->max_frame_num;
2208
2209   if (nalu->idr_pic_flag)
2210     READ_UE_MAX (&nr, slice->idr_pic_id, G_MAXUINT16);
2211
2212   start_pos = nal_reader_get_pos (&nr);
2213
2214   if (sps->pic_order_cnt_type == 0) {
2215     READ_UINT16 (&nr, slice->pic_order_cnt_lsb,
2216         sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
2217
2218     if (pps->pic_order_present_flag && !slice->field_pic_flag)
2219       READ_SE (&nr, slice->delta_pic_order_cnt_bottom);
2220   }
2221
2222   if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
2223     READ_SE (&nr, slice->delta_pic_order_cnt[0]);
2224     if (pps->pic_order_present_flag && !slice->field_pic_flag)
2225       READ_SE (&nr, slice->delta_pic_order_cnt[1]);
2226   }
2227
2228   slice->pic_order_cnt_bit_size = nal_reader_get_pos (&nr) - start_pos;
2229
2230   if (pps->redundant_pic_cnt_present_flag)
2231     READ_UE_MAX (&nr, slice->redundant_pic_cnt, G_MAXINT8);
2232
2233   if (GST_H264_IS_B_SLICE (slice))
2234     READ_UINT8 (&nr, slice->direct_spatial_mv_pred_flag, 1);
2235
2236   if (GST_H264_IS_P_SLICE (slice) || GST_H264_IS_SP_SLICE (slice) ||
2237       GST_H264_IS_B_SLICE (slice)) {
2238     READ_UINT8 (&nr, slice->num_ref_idx_active_override_flag, 1);
2239     if (slice->num_ref_idx_active_override_flag) {
2240       READ_UE_MAX (&nr, slice->num_ref_idx_l0_active_minus1, 31);
2241
2242       if (GST_H264_IS_B_SLICE (slice))
2243         READ_UE_MAX (&nr, slice->num_ref_idx_l1_active_minus1, 31);
2244     }
2245   }
2246
2247   if (!slice_parse_ref_pic_list_modification (slice, &nr,
2248           GST_H264_IS_MVC_NALU (nalu)))
2249     goto error;
2250
2251   if ((pps->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice)
2252               || GST_H264_IS_SP_SLICE (slice)))
2253       || (pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice))) {
2254     if (!gst_h264_slice_parse_pred_weight_table (slice, &nr,
2255             sps->chroma_array_type))
2256       goto error;
2257   }
2258
2259   if (nalu->ref_idc != 0) {
2260     if (!gst_h264_slice_parse_dec_ref_pic_marking (slice, nalu, &nr))
2261       goto error;
2262   }
2263
2264   if (pps->entropy_coding_mode_flag && !GST_H264_IS_I_SLICE (slice) &&
2265       !GST_H264_IS_SI_SLICE (slice))
2266     READ_UE_MAX (&nr, slice->cabac_init_idc, 2);
2267
2268   READ_SE_ALLOWED (&nr, slice->slice_qp_delta, -87, 77);
2269
2270   if (GST_H264_IS_SP_SLICE (slice) || GST_H264_IS_SI_SLICE (slice)) {
2271     if (GST_H264_IS_SP_SLICE (slice))
2272       READ_UINT8 (&nr, slice->sp_for_switch_flag, 1);
2273     READ_SE_ALLOWED (&nr, slice->slice_qs_delta, -51, 51);
2274   }
2275
2276   if (pps->deblocking_filter_control_present_flag) {
2277     READ_UE_MAX (&nr, slice->disable_deblocking_filter_idc, 2);
2278     if (slice->disable_deblocking_filter_idc != 1) {
2279       READ_SE_ALLOWED (&nr, slice->slice_alpha_c0_offset_div2, -6, 6);
2280       READ_SE_ALLOWED (&nr, slice->slice_beta_offset_div2, -6, 6);
2281     }
2282   }
2283
2284   if (pps->num_slice_groups_minus1 > 0 &&
2285       pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
2286     /* Ceil(Log2(PicSizeInMapUnits / SliceGroupChangeRate + 1))  [7-33] */
2287     guint32 PicWidthInMbs = sps->pic_width_in_mbs_minus1 + 1;
2288     guint32 PicHeightInMapUnits = sps->pic_height_in_map_units_minus1 + 1;
2289     guint32 PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
2290     guint32 SliceGroupChangeRate = pps->slice_group_change_rate_minus1 + 1;
2291     const guint n = ceil_log2 (PicSizeInMapUnits / SliceGroupChangeRate + 1);
2292     READ_UINT16 (&nr, slice->slice_group_change_cycle, n);
2293   }
2294
2295   slice->header_size = nal_reader_get_pos (&nr);
2296   slice->n_emulation_prevention_bytes = nal_reader_get_epb_count (&nr);
2297
2298   return GST_H264_PARSER_OK;
2299
2300 error:
2301   GST_WARNING ("error parsing \"Slice header\"");
2302   return GST_H264_PARSER_ERROR;
2303 }
2304
2305 /* Free MVC-specific data from subset SPS header */
2306 static void
2307 gst_h264_sps_mvc_clear (GstH264SPS * sps)
2308 {
2309   GstH264SPSExtMVC *const mvc = &sps->extension.mvc;
2310   guint i, j;
2311
2312   g_assert (sps->extension_type == GST_H264_NAL_EXTENSION_MVC);
2313
2314   g_free (mvc->view);
2315   mvc->view = NULL;
2316
2317   for (i = 0; i <= mvc->num_level_values_signalled_minus1; i++) {
2318     GstH264SPSExtMVCLevelValue *const level_value = &mvc->level_value[i];
2319
2320     for (j = 0; j <= level_value->num_applicable_ops_minus1; j++) {
2321       g_free (level_value->applicable_op[j].target_view_id);
2322       level_value->applicable_op[j].target_view_id = NULL;
2323     }
2324     g_free (level_value->applicable_op);
2325     level_value->applicable_op = NULL;
2326   }
2327   g_free (mvc->level_value);
2328   mvc->level_value = NULL;
2329
2330   /* All meaningful MVC info are now gone, just pretend to be a
2331    * standard AVC struct now */
2332   sps->extension_type = GST_H264_NAL_EXTENSION_NONE;
2333 }
2334
2335 /**
2336  * gst_h264_sps_clear:
2337  * @sps: The #GstH264SPS to free
2338  *
2339  * Clears all @sps internal resources.
2340  *
2341  * Since: 1.6
2342  */
2343 void
2344 gst_h264_sps_clear (GstH264SPS * sps)
2345 {
2346   g_return_if_fail (sps != NULL);
2347
2348   switch (sps->extension_type) {
2349     case GST_H264_NAL_EXTENSION_MVC:
2350       gst_h264_sps_mvc_clear (sps);
2351       break;
2352   }
2353 }
2354
2355 static void
2356 h264_sei_message_clear (GstH264SEIMessage * sei_msg)
2357 {
2358   switch (sei_msg->payloadType) {
2359     case GST_H264_SEI_REGISTERED_USER_DATA:{
2360       GstH264RegisteredUserData *rud = &sei_msg->payload.registered_user_data;
2361
2362       g_free ((guint8 *) rud->data);
2363       rud->data = NULL;
2364       break;
2365     }
2366     default:
2367       break;
2368   }
2369 }
2370
2371 /**
2372  * gst_h264_parser_parse_sei:
2373  * @nalparser: a #GstH264NalParser
2374  * @nalu: The #GST_H264_NAL_SEI #GstH264NalUnit to parse
2375  * @messages: The GArray of #GstH264SEIMessage to fill. The caller must free it when done.
2376  *
2377  * Parses @nalu containing one or more Supplementary Enhancement Information messages,
2378  * and allocates and fills the @messages array.
2379  *
2380  * Returns: a #GstH264ParserResult
2381  */
2382 GstH264ParserResult
2383 gst_h264_parser_parse_sei (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
2384     GArray ** messages)
2385 {
2386   NalReader nr;
2387   GstH264SEIMessage sei;
2388   GstH264ParserResult res;
2389
2390   GST_DEBUG ("parsing SEI nal");
2391   nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
2392       nalu->size - nalu->header_bytes);
2393   *messages = g_array_new (FALSE, FALSE, sizeof (GstH264SEIMessage));
2394   g_array_set_clear_func (*messages, (GDestroyNotify) h264_sei_message_clear);
2395
2396   do {
2397     res = gst_h264_parser_parse_sei_message (nalparser, &nr, &sei);
2398     if (res == GST_H264_PARSER_OK)
2399       g_array_append_val (*messages, sei);
2400     else
2401       break;
2402   } while (nal_reader_has_more_data (&nr));
2403
2404   return res;
2405 }
2406
2407 /**
2408  * gst_h264_quant_matrix_8x8_get_zigzag_from_raster:
2409  * @out_quant: (out): The resulting quantization matrix
2410  * @quant: The source quantization matrix
2411  *
2412  * Converts quantization matrix @quant from raster scan order to
2413  * zigzag scan order and store the resulting factors into @out_quant.
2414  *
2415  * Note: it is an error to pass the same table in both @quant and
2416  * @out_quant arguments.
2417  *
2418  * Since: 1.4
2419  */
2420 void
2421 gst_h264_quant_matrix_8x8_get_zigzag_from_raster (guint8 out_quant[64],
2422     const guint8 quant[64])
2423 {
2424   guint i;
2425
2426   g_return_if_fail (out_quant != quant);
2427
2428   for (i = 0; i < 64; i++)
2429     out_quant[i] = quant[zigzag_8x8[i]];
2430 }
2431
2432 /**
2433  * gst_h264_quant_matrix_8x8_get_raster_from_zigzag:
2434  * @out_quant: (out): The resulting quantization matrix
2435  * @quant: The source quantization matrix
2436  *
2437  * Converts quantization matrix @quant from zigzag scan order to
2438  * raster scan order and store the resulting factors into @out_quant.
2439  *
2440  * Note: it is an error to pass the same table in both @quant and
2441  * @out_quant arguments.
2442  *
2443  * Since: 1.4
2444  */
2445 void
2446 gst_h264_quant_matrix_8x8_get_raster_from_zigzag (guint8 out_quant[64],
2447     const guint8 quant[64])
2448 {
2449   guint i;
2450
2451   g_return_if_fail (out_quant != quant);
2452
2453   for (i = 0; i < 64; i++)
2454     out_quant[zigzag_8x8[i]] = quant[i];
2455 }
2456
2457 /**
2458  * gst_h264_quant_matrix_4x4_get_zigzag_from_raster:
2459  * @out_quant: (out): The resulting quantization matrix
2460  * @quant: The source quantization matrix
2461  *
2462  * Converts quantization matrix @quant from raster scan order to
2463  * zigzag scan order and store the resulting factors into @out_quant.
2464  *
2465  * Note: it is an error to pass the same table in both @quant and
2466  * @out_quant arguments.
2467  *
2468  * Since: 1.4
2469  */
2470 void
2471 gst_h264_quant_matrix_4x4_get_zigzag_from_raster (guint8 out_quant[16],
2472     const guint8 quant[16])
2473 {
2474   guint i;
2475
2476   g_return_if_fail (out_quant != quant);
2477
2478   for (i = 0; i < 16; i++)
2479     out_quant[i] = quant[zigzag_4x4[i]];
2480 }
2481
2482 /**
2483  * gst_h264_quant_matrix_4x4_get_raster_from_zigzag:
2484  * @out_quant: (out): The resulting quantization matrix
2485  * @quant: The source quantization matrix
2486  *
2487  * Converts quantization matrix @quant from zigzag scan order to
2488  * raster scan order and store the resulting factors into @out_quant.
2489  *
2490  * Note: it is an error to pass the same table in both @quant and
2491  * @out_quant arguments.
2492  *
2493  * Since: 1.4
2494  */
2495 void
2496 gst_h264_quant_matrix_4x4_get_raster_from_zigzag (guint8 out_quant[16],
2497     const guint8 quant[16])
2498 {
2499   guint i;
2500
2501   g_return_if_fail (out_quant != quant);
2502
2503   for (i = 0; i < 16; i++)
2504     out_quant[zigzag_4x4[i]] = quant[i];
2505 }
2506
2507 /**
2508  * gst_h264_video_calculate_framerate:
2509  * @sps: Current Sequence Parameter Set
2510  * @field_pic_flag: Current @field_pic_flag, obtained from latest slice header
2511  * @pic_struct: @pic_struct value if available, 0 otherwise
2512  * @fps_num: (out): The resulting fps numerator
2513  * @fps_den: (out): The resulting fps denominator
2514  *
2515  * Calculate framerate of a video sequence using @sps VUI information,
2516  * @field_pic_flag from a slice header and @pic_struct from #GstH264PicTiming SEI
2517  * message.
2518  *
2519  * If framerate is variable or can't be determined, @fps_num will be set to 0
2520  * and @fps_den to 1.
2521  */
2522 void
2523 gst_h264_video_calculate_framerate (const GstH264SPS * sps,
2524     guint field_pic_flag, guint pic_struct, gint * fps_num, gint * fps_den)
2525 {
2526   gint num = 0;
2527   gint den = 1;
2528
2529   /* To calculate framerate, we use this formula:
2530    *          time_scale                1                         1
2531    * fps = -----------------  x  ---------------  x  ------------------------
2532    *       num_units_in_tick     DeltaTfiDivisor     (field_pic_flag ? 2 : 1)
2533    *
2534    * See H264 specification E2.1 for more details.
2535    */
2536
2537   if (sps) {
2538     if (sps->vui_parameters_present_flag) {
2539       const GstH264VUIParams *vui = &sps->vui_parameters;
2540       if (vui->timing_info_present_flag) {
2541         int delta_tfi_divisor = 1;
2542         num = vui->time_scale;
2543         den = vui->num_units_in_tick;
2544
2545         if (vui->pic_struct_present_flag) {
2546           switch (pic_struct) {
2547             case 1:
2548             case 2:
2549               delta_tfi_divisor = 1;
2550               break;
2551             case 0:
2552             case 3:
2553             case 4:
2554               delta_tfi_divisor = 2;
2555               break;
2556             case 5:
2557             case 6:
2558               delta_tfi_divisor = 3;
2559               break;
2560             case 7:
2561               delta_tfi_divisor = 4;
2562               break;
2563             case 8:
2564               delta_tfi_divisor = 6;
2565               break;
2566           }
2567         } else {
2568           delta_tfi_divisor = field_pic_flag ? 1 : 2;
2569         }
2570         den *= delta_tfi_divisor;
2571
2572         /* Picture is two fields ? */
2573         den *= (field_pic_flag ? 2 : 1);
2574       }
2575     }
2576   }
2577
2578   *fps_num = num;
2579   *fps_den = den;
2580 }