import ext/codecparsers instead of git submodule
[profile/ivi/gstreamer-vaapi.git] / ext / codecparsers / 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  * @short_description: Convenience library for h264 video
33  * bitstream parsing.
34  *
35  * It offers you bitstream parsing in AVC mode or not. To identify Nals in a bitstream and
36  * parse its headers, you should call:
37  * <itemizedlist>
38  *   <listitem>
39  *      #gst_h264_parser_identify_nalu to identify the following nalu in not AVC bitstreams
40  *   </listitem>
41  *   <listitem>
42  *      #gst_h264_parser_identify_nalu_avc to identify the nalu in AVC bitstreams
43  *   </listitem>
44  * </itemizedlist>
45  *
46  * Then, depending on the #GstH264NalUnitType of the newly parsed #GstH264NalUnit, you should
47  * call the differents functions to parse the structure:
48  * <itemizedlist>
49  *   <listitem>
50  *      From #GST_H264_NAL_SLICE to #GST_H264_NAL_SLICE_IDR: #gst_h264_parser_parse_slice_hdr
51  *   </listitem>
52  *   <listitem>
53  *      #GST_H264_NAL_SEI: #gst_h264_parser_parse_sei
54  *   </listitem>
55  *   <listitem>
56  *      #GST_H264_NAL_SPS: #gst_h264_parser_parse_sps
57  *   </listitem>
58  *   <listitem>
59  *      #GST_H264_NAL_PPS: #gst_h264_parser_parse_pps
60  *   </listitem>
61  *   <listitem>
62  *      Any other: #gst_h264_parser_parse_nal
63  *   </listitem>
64  * </itemizedlist>
65  *
66  * Note: You should always call gst_h264_parser_parse_nal if you don't actually need
67  * #GstH264NalUnitType to be parsed for your personnal use, in order to guarantee that the
68  * #GstH264NalParser is always up to date.
69  *
70  * For more details about the structures, look at the ITU-T H.264 and ISO/IEC 14496-10 – MPEG-4
71  * Part 10 specifications, you can download them from:
72  *
73  * <itemizedlist>
74  *   <listitem>
75  *     ITU-T H.264: http://www.itu.int/rec/T-REC-H.264
76  *   </listitem>
77  *   <listitem>
78  *     ISO/IEC 14496-10: http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=56538
79  *   </listitem>
80  * </itemizedlist>
81  */
82
83 #ifdef HAVE_CONFIG_H
84 #  include "config.h"
85 #endif
86
87 #include "gsth264parser.h"
88
89 #include <gst/base/gstbytereader.h>
90 #include <gst/base/gstbitreader.h>
91 #include <string.h>
92
93 GST_DEBUG_CATEGORY (h264_parser_debug);
94 #define GST_CAT_DEFAULT h264_parser_debug
95
96 /**** Default scaling_lists according to Table 7-2 *****/
97 static const guint8 default_4x4_intra[16] = {
98   6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
99   32, 37, 37, 42
100 };
101
102 static const guint8 default_4x4_inter[16] = {
103   10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
104   27, 30, 30, 34
105 };
106
107 static const guint8 default_8x8_intra[64] = {
108   6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
109   18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
110   27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
111   33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
112 };
113
114 static const guint8 default_8x8_inter[64] = {
115   9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
116   19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
117   24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
118   28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
119 };
120
121 static const guint8 zigzag_8x8[64] = {
122   0, 1, 8, 16, 9, 2, 3, 10,
123   17, 24, 32, 25, 18, 11, 4, 5,
124   12, 19, 26, 33, 40, 48, 41, 34,
125   27, 20, 13, 6, 7, 14, 21, 28,
126   35, 42, 49, 56, 57, 50, 43, 36,
127   29, 22, 15, 23, 30, 37, 44, 51,
128   58, 59, 52, 45, 38, 31, 39, 46,
129   53, 60, 61, 54, 47, 55, 62, 63
130 };
131
132 static const guint8 zigzag_4x4[16] = {
133   0, 1, 4, 8,
134   5, 2, 3, 6,
135   9, 12, 13, 10,
136   7, 11, 14, 15,
137 };
138
139 typedef struct
140 {
141   guint par_n, par_d;
142 } PAR;
143
144 /* Table E-1 - Meaning of sample aspect ratio indicator (1..16) */
145 static PAR aspect_ratios[17] = {
146   {0, 0},
147   {1, 1},
148   {12, 11},
149   {10, 11},
150   {16, 11},
151   {40, 33},
152   {24, 11},
153   {20, 11},
154   {32, 11},
155   {80, 33},
156   {18, 11},
157   {15, 11},
158   {64, 33},
159   {160, 99},
160   {4, 3},
161   {3, 2},
162   {2, 1}
163 };
164
165 /* Compute Ceil(Log2(v)) */
166 /* Derived from branchless code for integer log2(v) from:
167    <http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
168 static guint
169 ceil_log2 (guint32 v)
170 {
171   guint r, shift;
172
173   v--;
174   r = (v > 0xFFFF) << 4;
175   v >>= r;
176   shift = (v > 0xFF) << 3;
177   v >>= shift;
178   r |= shift;
179   shift = (v > 0xF) << 2;
180   v >>= shift;
181   r |= shift;
182   shift = (v > 0x3) << 1;
183   v >>= shift;
184   r |= shift;
185   r |= (v >> 1);
186   return r + 1;
187 }
188
189 /****** Nal parser ******/
190
191 typedef struct
192 {
193   const guint8 *data;
194   guint size;
195
196   guint n_epb;                  /* Number of emulation prevention bytes */
197   guint byte;                   /* Byte position */
198   guint bits_in_cache;          /* bitpos in the cache of next bit */
199   guint8 first_byte;
200   guint64 cache;                /* cached bytes */
201 } NalReader;
202
203 static void
204 nal_reader_init (NalReader * nr, const guint8 * data, guint size)
205 {
206   nr->data = data;
207   nr->size = size;
208   nr->n_epb = 0;
209
210   nr->byte = 0;
211   nr->bits_in_cache = 0;
212   /* fill with something other than 0 to detect emulation prevention bytes */
213   nr->first_byte = 0xff;
214   nr->cache = 0xff;
215 }
216
217 static inline gboolean
218 nal_reader_read (NalReader * nr, guint nbits)
219 {
220   if (G_UNLIKELY (nr->byte * 8 + (nbits - nr->bits_in_cache) > nr->size * 8)) {
221     GST_DEBUG ("Can not read %u bits, bits in cache %u, Byte * 8 %u, size in "
222         "bits %u", nbits, nr->bits_in_cache, nr->byte * 8, nr->size * 8);
223     return FALSE;
224   }
225
226   while (nr->bits_in_cache < nbits) {
227     guint8 byte;
228     gboolean check_three_byte;
229
230     check_three_byte = TRUE;
231   next_byte:
232     if (G_UNLIKELY (nr->byte >= nr->size))
233       return FALSE;
234
235     byte = nr->data[nr->byte++];
236
237     /* check if the byte is a emulation_prevention_three_byte */
238     if (check_three_byte && byte == 0x03 && nr->first_byte == 0x00 &&
239         ((nr->cache & 0xff) == 0)) {
240       /* next byte goes unconditionally to the cache, even if it's 0x03 */
241       check_three_byte = FALSE;
242       nr->n_epb++;
243       goto next_byte;
244     }
245     nr->cache = (nr->cache << 8) | nr->first_byte;
246     nr->first_byte = byte;
247     nr->bits_in_cache += 8;
248   }
249
250   return TRUE;
251 }
252
253 static inline gboolean
254 nal_reader_skip (NalReader * nr, guint nbits)
255 {
256   if (G_UNLIKELY (!nal_reader_read (nr, nbits)))
257     return FALSE;
258
259   nr->bits_in_cache -= nbits;
260
261   return TRUE;
262 }
263
264 static inline gboolean
265 nal_reader_skip_to_byte (NalReader * nr)
266 {
267   if (nr->bits_in_cache == 0) {
268     if (G_LIKELY ((nr->size - nr->byte) > 0))
269       nr->byte++;
270     else
271       return FALSE;
272   }
273
274   nr->bits_in_cache = 0;
275
276   return TRUE;
277 }
278
279 static inline guint
280 nal_reader_get_pos (const NalReader * nr)
281 {
282   return nr->byte * 8 - nr->bits_in_cache;
283 }
284
285 static inline guint
286 nal_reader_get_remaining (const NalReader * nr)
287 {
288   return (nr->size - nr->byte) * 8 + nr->bits_in_cache;
289 }
290
291 static inline guint
292 nal_reader_get_epb_count (const NalReader * nr)
293 {
294   return nr->n_epb;
295 }
296
297 #define GST_NAL_READER_READ_BITS(bits) \
298 static gboolean \
299 nal_reader_get_bits_uint##bits (NalReader *nr, guint##bits *val, guint nbits) \
300 { \
301   guint shift; \
302   \
303   if (!nal_reader_read (nr, nbits)) \
304     return FALSE; \
305   \
306   /* bring the required bits down and truncate */ \
307   shift = nr->bits_in_cache - nbits; \
308   *val = nr->first_byte >> shift; \
309   \
310   *val |= nr->cache << (8 - shift); \
311   /* mask out required bits */ \
312   if (nbits < bits) \
313     *val &= ((guint##bits)1 << nbits) - 1; \
314   \
315   nr->bits_in_cache = shift; \
316   \
317   return TRUE; \
318 } \
319
320 GST_NAL_READER_READ_BITS (8);
321 GST_NAL_READER_READ_BITS (16);
322 GST_NAL_READER_READ_BITS (32);
323
324 static gboolean
325 nal_reader_get_ue (NalReader * nr, guint32 * val)
326 {
327   guint i = 0;
328   guint8 bit;
329   guint32 value;
330
331   if (G_UNLIKELY (!nal_reader_get_bits_uint8 (nr, &bit, 1))) {
332
333     return FALSE;
334   }
335
336   while (bit == 0) {
337     i++;
338     if G_UNLIKELY
339       ((!nal_reader_get_bits_uint8 (nr, &bit, 1)))
340           return FALSE;
341   }
342
343   if (G_UNLIKELY (i > 32))
344     return FALSE;
345
346   if (G_UNLIKELY (!nal_reader_get_bits_uint32 (nr, &value, i)))
347     return FALSE;
348
349   *val = (1 << i) - 1 + value;
350
351   return TRUE;
352 }
353
354 static inline gboolean
355 nal_reader_get_se (NalReader * nr, gint32 * val)
356 {
357   guint32 value;
358
359   if (G_UNLIKELY (!nal_reader_get_ue (nr, &value)))
360     return FALSE;
361
362   if (value % 2)
363     *val = (value / 2) + 1;
364   else
365     *val = -(value / 2);
366
367   return TRUE;
368 }
369
370 #define CHECK_ALLOWED(val, min, max) { \
371   if (val < min || val > max) { \
372     GST_WARNING ("value not in allowed range. value: %d, range %d-%d", \
373                      val, min, max); \
374     goto error; \
375   } \
376 }
377
378 #define READ_UINT8(nr, val, nbits) { \
379   if (!nal_reader_get_bits_uint8 (nr, &val, nbits)) { \
380     GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
381     goto error; \
382   } \
383 }
384
385 #define READ_UINT16(nr, val, nbits) { \
386   if (!nal_reader_get_bits_uint16 (nr, &val, nbits)) { \
387   GST_WARNING ("failed to read uint16, nbits: %d", nbits); \
388     goto error; \
389   } \
390 }
391
392 #define READ_UINT32(nr, val, nbits) { \
393   if (!nal_reader_get_bits_uint32 (nr, &val, nbits)) { \
394   GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
395     goto error; \
396   } \
397 }
398
399 #define READ_UINT64(nr, val, nbits) { \
400   if (!nal_reader_get_bits_uint64 (nr, &val, nbits)) { \
401     GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
402     goto error; \
403   } \
404 }
405
406 #define READ_UE(nr, val) { \
407   if (!nal_reader_get_ue (nr, &val)) { \
408     GST_WARNING ("failed to read UE"); \
409     goto error; \
410   } \
411 }
412
413 #define READ_UE_ALLOWED(nr, val, min, max) { \
414   guint32 tmp; \
415   READ_UE (nr, tmp); \
416   CHECK_ALLOWED (tmp, min, max); \
417   val = tmp; \
418 }
419
420 #define READ_SE(nr, val) { \
421   if (!nal_reader_get_se (nr, &val)) { \
422     GST_WARNING ("failed to read SE"); \
423     goto error; \
424   } \
425 }
426
427 #define READ_SE_ALLOWED(nr, val, min, max) { \
428   gint32 tmp; \
429   READ_SE (nr, tmp); \
430   CHECK_ALLOWED (tmp, min, max); \
431   val = tmp; \
432 }
433
434 /***********  end of nal parser ***************/
435
436 /*****  Utils ****/
437 #define EXTENDED_SAR 255
438
439 static GstH264SPS *
440 gst_h264_parser_get_sps (GstH264NalParser * nalparser, guint8 sps_id)
441 {
442   GstH264SPS *sps;
443
444   sps = &nalparser->sps[sps_id];
445
446   if (sps->valid)
447     return sps;
448
449   return NULL;
450 }
451
452 static GstH264PPS *
453 gst_h264_parser_get_pps (GstH264NalParser * nalparser, guint8 pps_id)
454 {
455   GstH264PPS *pps;
456
457   pps = &nalparser->pps[pps_id];
458
459   if (pps->valid)
460     return pps;
461
462   return NULL;
463 }
464
465 static inline void
466 set_nalu_datas (GstH264NalUnit * nalu)
467 {
468   guint8 *data = nalu->data + nalu->offset;
469
470   nalu->type = (data[0] & 0x1f);
471   nalu->ref_idc = (data[0] & 0x60) >> 5;
472   nalu->idr_pic_flag = (nalu->type == 5 ? 1 : 0);
473
474   GST_DEBUG ("Nal type %u, ref_idc %u", nalu->type, nalu->ref_idc);
475 }
476
477 static inline gint
478 scan_for_start_codes (const guint8 * data, guint size)
479 {
480   GstByteReader br;
481   gst_byte_reader_init (&br, data, size);
482
483   /* NALU not empty, so we can at least expect 1 (even 2) bytes following sc */
484   return gst_byte_reader_masked_scan_uint32 (&br, 0xffffff00, 0x00000100,
485       0, size);
486 }
487
488 static gboolean
489 gst_h264_parser_more_data (NalReader * nr)
490 {
491   NalReader nr_tmp;
492   guint remaining, nbits;
493   guint8 rbsp_stop_one_bit, zero_bits;
494
495   remaining = nal_reader_get_remaining (nr);
496   if (remaining == 0)
497     return FALSE;
498
499   nr_tmp = *nr;
500   nr = &nr_tmp;
501
502   if (!nal_reader_get_bits_uint8 (nr, &rbsp_stop_one_bit, 1))
503     return FALSE;
504   if (!rbsp_stop_one_bit)
505     return TRUE;
506
507   nbits = --remaining % 8;
508   while (remaining > 0) {
509     if (!nal_reader_get_bits_uint8 (nr, &zero_bits, nbits))
510       return FALSE;
511     if (zero_bits != 0)
512       return TRUE;
513     remaining -= nbits;
514     nbits = 8;
515   }
516   return FALSE;
517 }
518
519 /****** Parsing functions *****/
520
521 static gboolean
522 gst_h264_parse_hrd_parameters (GstH264HRDParams * hrd, NalReader * nr)
523 {
524   guint sched_sel_idx;
525
526   GST_DEBUG ("parsing \"HRD Parameters\"");
527
528   READ_UE_ALLOWED (nr, hrd->cpb_cnt_minus1, 0, 31);
529   READ_UINT8 (nr, hrd->bit_rate_scale, 4);
530   READ_UINT8 (nr, hrd->cpb_size_scale, 4);
531
532   for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) {
533     READ_UE (nr, hrd->bit_rate_value_minus1[sched_sel_idx]);
534     READ_UE (nr, hrd->cpb_size_value_minus1[sched_sel_idx]);
535     READ_UINT8 (nr, hrd->cbr_flag[sched_sel_idx], 1);
536   }
537
538   READ_UINT8 (nr, hrd->initial_cpb_removal_delay_length_minus1, 5);
539   READ_UINT8 (nr, hrd->cpb_removal_delay_length_minus1, 5);
540   READ_UINT8 (nr, hrd->dpb_output_delay_length_minus1, 5);
541   READ_UINT8 (nr, hrd->time_offset_length, 5);
542
543   return TRUE;
544
545 error:
546   GST_WARNING ("error parsing \"HRD Parameters\"");
547   return FALSE;
548 }
549
550 static gboolean
551 gst_h264_parse_vui_parameters (GstH264SPS * sps, NalReader * nr)
552 {
553   GstH264VUIParams *vui = &sps->vui_parameters;
554
555   GST_DEBUG ("parsing \"VUI Parameters\"");
556
557   /* set default values for fields that might not be present in the bitstream
558      and have valid defaults */
559   vui->aspect_ratio_idc = 0;
560   vui->video_format = 5;
561   vui->video_full_range_flag = 0;
562   vui->colour_primaries = 2;
563   vui->transfer_characteristics = 2;
564   vui->matrix_coefficients = 2;
565   vui->chroma_sample_loc_type_top_field = 0;
566   vui->chroma_sample_loc_type_bottom_field = 0;
567   vui->low_delay_hrd_flag = 0;
568   vui->par_n = 0;
569   vui->par_d = 0;
570
571   READ_UINT8 (nr, vui->aspect_ratio_info_present_flag, 1);
572   if (vui->aspect_ratio_info_present_flag) {
573     READ_UINT8 (nr, vui->aspect_ratio_idc, 8);
574     if (vui->aspect_ratio_idc == EXTENDED_SAR) {
575       READ_UINT16 (nr, vui->sar_width, 16);
576       READ_UINT16 (nr, vui->sar_height, 16);
577       vui->par_n = vui->sar_width;
578       vui->par_d = vui->sar_height;
579     } else if (vui->aspect_ratio_idc <= 16) {
580       vui->par_n = aspect_ratios[vui->aspect_ratio_idc].par_n;
581       vui->par_d = aspect_ratios[vui->aspect_ratio_idc].par_d;
582     }
583   }
584
585   READ_UINT8 (nr, vui->overscan_info_present_flag, 1);
586   if (vui->overscan_info_present_flag)
587     READ_UINT8 (nr, vui->overscan_appropriate_flag, 1);
588
589   READ_UINT8 (nr, vui->video_signal_type_present_flag, 1);
590   if (vui->video_signal_type_present_flag) {
591
592     READ_UINT8 (nr, vui->video_format, 3);
593     READ_UINT8 (nr, vui->video_full_range_flag, 1);
594     READ_UINT8 (nr, vui->colour_description_present_flag, 1);
595     if (vui->colour_description_present_flag) {
596       READ_UINT8 (nr, vui->colour_primaries, 8);
597       READ_UINT8 (nr, vui->transfer_characteristics, 8);
598       READ_UINT8 (nr, vui->matrix_coefficients, 8);
599     }
600   }
601
602   READ_UINT8 (nr, vui->chroma_loc_info_present_flag, 1);
603   if (vui->chroma_loc_info_present_flag) {
604     READ_UE_ALLOWED (nr, vui->chroma_sample_loc_type_top_field, 0, 5);
605     READ_UE_ALLOWED (nr, vui->chroma_sample_loc_type_bottom_field, 0, 5);
606   }
607
608   READ_UINT8 (nr, vui->timing_info_present_flag, 1);
609   if (vui->timing_info_present_flag) {
610     READ_UINT32 (nr, vui->num_units_in_tick, 32);
611     if (vui->num_units_in_tick == 0)
612       GST_WARNING ("num_units_in_tick = 0 detected in stream "
613           "(incompliant to H.264 E.2.1).");
614
615     READ_UINT32 (nr, vui->time_scale, 32);
616     if (vui->time_scale == 0)
617       GST_WARNING ("time_scale = 0 detected in stream "
618           "(incompliant to H.264 E.2.1).");
619
620     READ_UINT8 (nr, vui->fixed_frame_rate_flag, 1);
621   }
622
623   READ_UINT8 (nr, vui->nal_hrd_parameters_present_flag, 1);
624   if (vui->nal_hrd_parameters_present_flag) {
625     if (!gst_h264_parse_hrd_parameters (&vui->nal_hrd_parameters, nr))
626       goto error;
627   }
628
629   READ_UINT8 (nr, vui->vcl_hrd_parameters_present_flag, 1);
630   if (vui->vcl_hrd_parameters_present_flag) {
631     if (!gst_h264_parse_hrd_parameters (&vui->vcl_hrd_parameters, nr))
632       goto error;
633   }
634
635   if (vui->nal_hrd_parameters_present_flag ||
636       vui->vcl_hrd_parameters_present_flag)
637     READ_UINT8 (nr, vui->low_delay_hrd_flag, 1);
638
639   READ_UINT8 (nr, vui->pic_struct_present_flag, 1);
640   READ_UINT8 (nr, vui->bitstream_restriction_flag, 1);
641   if (vui->bitstream_restriction_flag) {
642     READ_UINT8 (nr, vui->motion_vectors_over_pic_boundaries_flag, 1);
643     READ_UE (nr, vui->max_bytes_per_pic_denom);
644     READ_UE_ALLOWED (nr, vui->max_bits_per_mb_denom, 0, 16);
645     READ_UE_ALLOWED (nr, vui->log2_max_mv_length_horizontal, 0, 16);
646     READ_UE_ALLOWED (nr, vui->log2_max_mv_length_vertical, 0, 16);
647     READ_UE (nr, vui->num_reorder_frames);
648     READ_UE (nr, vui->max_dec_frame_buffering);
649   }
650
651   return TRUE;
652
653 error:
654   GST_WARNING ("error parsing \"VUI Parameters\"");
655   return FALSE;
656 }
657
658 static gboolean
659 gst_h264_parser_parse_scaling_list (NalReader * nr,
660     guint8 scaling_lists_4x4[6][16], guint8 scaling_lists_8x8[6][64],
661     const guint8 fallback_4x4_inter[16], const guint8 fallback_4x4_intra[16],
662     const guint8 fallback_8x8_inter[64], const guint8 fallback_8x8_intra[64],
663     guint8 n_lists)
664 {
665   guint i;
666
667   GST_DEBUG ("parsing scaling lists");
668
669   for (i = 0; i < 12; i++) {
670     gboolean use_default = FALSE;
671
672     if (i < n_lists) {
673       guint8 scaling_list_present_flag;
674
675       READ_UINT8 (nr, scaling_list_present_flag, 1);
676       if (scaling_list_present_flag) {
677         guint8 *scaling_list;
678         const guint8 *scan;
679         guint size;
680         guint j;
681         guint8 last_scale, next_scale;
682
683         if (i < 6) {
684           scaling_list = scaling_lists_4x4[i];
685           scan = zigzag_4x4;
686           size = 16;
687         } else {
688           scaling_list = scaling_lists_8x8[i - 6];
689           scan = zigzag_8x8;
690           size = 64;
691         }
692
693         last_scale = 8;
694         next_scale = 8;
695         for (j = 0; j < size; j++) {
696           if (next_scale != 0) {
697             gint32 delta_scale;
698
699             READ_SE (nr, delta_scale);
700             next_scale = (last_scale + delta_scale) & 0xff;
701           }
702           if (j == 0 && next_scale == 0) {
703             use_default = TRUE;
704             break;
705           }
706           last_scale = scaling_list[scan[j]] =
707               (next_scale == 0) ? last_scale : next_scale;
708         }
709       } else
710         use_default = TRUE;
711     } else
712       use_default = TRUE;
713
714     if (use_default) {
715       switch (i) {
716         case 0:
717           memcpy (scaling_lists_4x4[0], fallback_4x4_intra, 16);
718           break;
719         case 1:
720           memcpy (scaling_lists_4x4[1], scaling_lists_4x4[0], 16);
721           break;
722         case 2:
723           memcpy (scaling_lists_4x4[2], scaling_lists_4x4[1], 16);
724           break;
725         case 3:
726           memcpy (scaling_lists_4x4[3], fallback_4x4_inter, 16);
727           break;
728         case 4:
729           memcpy (scaling_lists_4x4[4], scaling_lists_4x4[3], 16);
730           break;
731         case 5:
732           memcpy (scaling_lists_4x4[5], scaling_lists_4x4[4], 16);
733           break;
734         case 6:
735           memcpy (scaling_lists_8x8[0], fallback_8x8_intra, 64);
736           break;
737         case 7:
738           memcpy (scaling_lists_8x8[1], fallback_8x8_inter, 64);
739           break;
740         case 8:
741           memcpy (scaling_lists_8x8[2], scaling_lists_8x8[0], 64);
742           break;
743         case 9:
744           memcpy (scaling_lists_8x8[3], scaling_lists_8x8[1], 64);
745           break;
746         case 10:
747           memcpy (scaling_lists_8x8[4], scaling_lists_8x8[2], 64);
748           break;
749         case 11:
750           memcpy (scaling_lists_8x8[5], scaling_lists_8x8[3], 64);
751           break;
752
753         default:
754           break;
755       }
756     }
757   }
758
759   return TRUE;
760
761 error:
762   GST_WARNING ("error parsing scaling lists");
763   return FALSE;
764 }
765
766 static gboolean
767 slice_parse_ref_pic_list_modification_1 (GstH264SliceHdr * slice,
768     NalReader * nr, guint list)
769 {
770   GstH264RefPicListModification *entries;
771   guint8 *ref_pic_list_modification_flag, *n_ref_pic_list_modification;
772   guint32 modification_of_pic_nums_idc;
773   guint i = 0;
774
775   if (list == 0) {
776     entries = slice->ref_pic_list_modification_l0;
777     ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l0;
778     n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l0;
779   } else {
780     entries = slice->ref_pic_list_modification_l1;
781     ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l1;
782     n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l1;
783   }
784
785   READ_UINT8 (nr, *ref_pic_list_modification_flag, 1);
786   if (*ref_pic_list_modification_flag) {
787     while (1) {
788       READ_UE (nr, modification_of_pic_nums_idc);
789       if (modification_of_pic_nums_idc == 3)
790         break;
791       if (modification_of_pic_nums_idc == 0 ||
792           modification_of_pic_nums_idc == 1) {
793         READ_UE_ALLOWED (nr, entries[i].value.abs_diff_pic_num_minus1, 0,
794             slice->max_pic_num - 1);
795       } else if (modification_of_pic_nums_idc == 2) {
796         READ_UE (nr, entries[i].value.long_term_pic_num);
797       }
798       entries[i++].modification_of_pic_nums_idc = modification_of_pic_nums_idc;
799     }
800   }
801   *n_ref_pic_list_modification = i;
802   return TRUE;
803
804 error:
805   GST_WARNING ("error parsing \"Reference picture list %u modification\"",
806       list);
807   return FALSE;
808 }
809
810 static gboolean
811 slice_parse_ref_pic_list_modification (GstH264SliceHdr * slice, NalReader * nr)
812 {
813   if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) {
814     if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 0))
815       return FALSE;
816   }
817
818   if (GST_H264_IS_B_SLICE (slice)) {
819     if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 1))
820       return FALSE;
821   }
822   return TRUE;
823 }
824
825 static gboolean
826 gst_h264_slice_parse_dec_ref_pic_marking (GstH264SliceHdr * slice,
827     GstH264NalUnit * nalu, NalReader * nr)
828 {
829   GstH264DecRefPicMarking *dec_ref_pic_m;
830
831   GST_DEBUG ("parsing \"Decoded reference picture marking\"");
832
833   dec_ref_pic_m = &slice->dec_ref_pic_marking;
834
835   if (nalu->idr_pic_flag) {
836     READ_UINT8 (nr, dec_ref_pic_m->no_output_of_prior_pics_flag, 1);
837     READ_UINT8 (nr, dec_ref_pic_m->long_term_reference_flag, 1);
838   } else {
839     READ_UINT8 (nr, dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag, 1);
840     if (dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag) {
841       guint32 mem_mgmt_ctrl_op;
842       GstH264RefPicMarking *refpicmarking;
843
844       dec_ref_pic_m->n_ref_pic_marking = 0;
845       while (1) {
846         refpicmarking =
847             &dec_ref_pic_m->ref_pic_marking[dec_ref_pic_m->n_ref_pic_marking];
848
849         READ_UE (nr, mem_mgmt_ctrl_op);
850         if (mem_mgmt_ctrl_op == 0)
851           break;
852
853         refpicmarking->memory_management_control_operation = mem_mgmt_ctrl_op;
854
855         if (mem_mgmt_ctrl_op == 1 || mem_mgmt_ctrl_op == 3)
856           READ_UE (nr, refpicmarking->difference_of_pic_nums_minus1);
857
858         if (mem_mgmt_ctrl_op == 2)
859           READ_UE (nr, refpicmarking->long_term_pic_num);
860
861         if (mem_mgmt_ctrl_op == 3 || mem_mgmt_ctrl_op == 6)
862           READ_UE (nr, refpicmarking->long_term_frame_idx);
863
864         if (mem_mgmt_ctrl_op == 4)
865           READ_UE (nr, refpicmarking->max_long_term_frame_idx_plus1);
866
867         dec_ref_pic_m->n_ref_pic_marking++;
868       }
869     }
870   }
871
872   return TRUE;
873
874 error:
875   GST_WARNING ("error parsing \"Decoded reference picture marking\"");
876   return FALSE;
877 }
878
879 static gboolean
880 gst_h264_slice_parse_pred_weight_table (GstH264SliceHdr * slice,
881     NalReader * nr, guint8 chroma_array_type)
882 {
883   GstH264PredWeightTable *p;
884   gint16 default_luma_weight, default_chroma_weight;
885   gint i;
886
887   GST_DEBUG ("parsing \"Prediction weight table\"");
888
889   p = &slice->pred_weight_table;
890
891   READ_UE_ALLOWED (nr, p->luma_log2_weight_denom, 0, 7);
892   /* set default values */
893   default_luma_weight = 1 << p->luma_log2_weight_denom;
894   for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l0); i++)
895     p->luma_weight_l0[i] = default_luma_weight;
896   memset (p->luma_offset_l0, 0, sizeof (p->luma_offset_l0));
897   if (GST_H264_IS_B_SLICE (slice)) {
898     for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l1); i++)
899       p->luma_weight_l1[i] = default_luma_weight;
900     memset (p->luma_offset_l1, 0, sizeof (p->luma_offset_l1));
901   }
902
903   if (chroma_array_type != 0) {
904     READ_UE_ALLOWED (nr, p->chroma_log2_weight_denom, 0, 7);
905     /* set default values */
906     default_chroma_weight = 1 << p->chroma_log2_weight_denom;
907     for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l0); i++) {
908       p->chroma_weight_l0[i][0] = default_chroma_weight;
909       p->chroma_weight_l0[i][1] = default_chroma_weight;
910     }
911     memset (p->chroma_offset_l0, 0, sizeof (p->chroma_offset_l0));
912     if (GST_H264_IS_B_SLICE (slice)) {
913       for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l1); i++) {
914         p->chroma_weight_l1[i][0] = default_chroma_weight;
915         p->chroma_weight_l1[i][1] = default_chroma_weight;
916       }
917       memset (p->chroma_offset_l1, 0, sizeof (p->chroma_offset_l1));
918     }
919   }
920
921   for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
922     guint8 luma_weight_l0_flag;
923
924     READ_UINT8 (nr, luma_weight_l0_flag, 1);
925     if (luma_weight_l0_flag) {
926       READ_SE_ALLOWED (nr, p->luma_weight_l0[i], -128, 127);
927       READ_SE_ALLOWED (nr, p->luma_offset_l0[i], -128, 127);
928     }
929     if (chroma_array_type != 0) {
930       guint8 chroma_weight_l0_flag;
931       gint j;
932
933       READ_UINT8 (nr, chroma_weight_l0_flag, 1);
934       if (chroma_weight_l0_flag) {
935         for (j = 0; j < 2; j++) {
936           READ_SE_ALLOWED (nr, p->chroma_weight_l0[i][j], -128, 127);
937           READ_SE_ALLOWED (nr, p->chroma_offset_l0[i][j], -128, 127);
938         }
939       }
940     }
941   }
942
943   if (GST_H264_IS_B_SLICE (slice)) {
944     for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
945       guint8 luma_weight_l1_flag;
946
947       READ_UINT8 (nr, luma_weight_l1_flag, 1);
948       if (luma_weight_l1_flag) {
949         READ_SE_ALLOWED (nr, p->luma_weight_l1[i], -128, 127);
950         READ_SE_ALLOWED (nr, p->luma_offset_l1[i], -128, 127);
951       }
952       if (chroma_array_type != 0) {
953         guint8 chroma_weight_l1_flag;
954         gint j;
955
956         READ_UINT8 (nr, chroma_weight_l1_flag, 1);
957         if (chroma_weight_l1_flag) {
958           for (j = 0; j < 2; j++) {
959             READ_SE_ALLOWED (nr, p->chroma_weight_l1[i][j], -128, 127);
960             READ_SE_ALLOWED (nr, p->chroma_offset_l1[i][j], -128, 127);
961           }
962         }
963       }
964     }
965   }
966
967   return TRUE;
968
969 error:
970   GST_WARNING ("error parsing \"Prediction weight table\"");
971   return FALSE;
972 }
973
974 static gboolean
975 gst_h264_parser_parse_buffering_period (GstH264NalParser * nalparser,
976     GstH264BufferingPeriod * per, NalReader * nr)
977 {
978   GstH264SPS *sps;
979   guint8 sps_id;
980
981   GST_DEBUG ("parsing \"Buffering period\"");
982
983   READ_UE_ALLOWED (nr, sps_id, 0, GST_H264_MAX_SPS_COUNT - 1);
984   sps = gst_h264_parser_get_sps (nalparser, sps_id);
985   if (!sps) {
986     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
987         sps_id);
988     return GST_H264_PARSER_BROKEN_LINK;
989   }
990   per->sps = sps;
991
992   if (sps->vui_parameters_present_flag) {
993     GstH264VUIParams *vui = &sps->vui_parameters;
994
995     if (vui->nal_hrd_parameters_present_flag) {
996       GstH264HRDParams *hrd = &vui->nal_hrd_parameters;
997       guint8 sched_sel_idx;
998
999       for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1000           sched_sel_idx++) {
1001         READ_UINT8 (nr, per->nal_initial_cpb_removal_delay[sched_sel_idx], 5);
1002         READ_UINT8 (nr,
1003             per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], 5);
1004       }
1005     }
1006
1007     if (vui->vcl_hrd_parameters_present_flag) {
1008       GstH264HRDParams *hrd = &vui->vcl_hrd_parameters;
1009       guint8 sched_sel_idx;
1010
1011       for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1012           sched_sel_idx++) {
1013         READ_UINT8 (nr, per->vcl_initial_cpb_removal_delay[sched_sel_idx], 5);
1014         READ_UINT8 (nr,
1015             per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], 5);
1016       }
1017     }
1018   }
1019
1020   return GST_H264_PARSER_OK;
1021
1022 error:
1023   GST_WARNING ("error parsing \"Buffering period\"");
1024   return GST_H264_PARSER_ERROR;
1025 }
1026
1027 static gboolean
1028 gst_h264_parse_clock_timestamp (GstH264ClockTimestamp * tim,
1029     GstH264VUIParams * vui, NalReader * nr)
1030 {
1031   guint8 full_timestamp_flag;
1032   guint8 time_offset_length;
1033
1034   GST_DEBUG ("parsing \"Clock timestamp\"");
1035
1036   /* defalt values */
1037   tim->time_offset = 0;
1038
1039   READ_UINT8 (nr, tim->ct_type, 2);
1040   READ_UINT8 (nr, tim->nuit_field_based_flag, 1);
1041   READ_UINT8 (nr, tim->counting_type, 5);
1042   READ_UINT8 (nr, full_timestamp_flag, 1);
1043   READ_UINT8 (nr, tim->discontinuity_flag, 1);
1044   READ_UINT8 (nr, tim->cnt_dropped_flag, 1);
1045   READ_UINT8 (nr, tim->n_frames, 8);
1046
1047   if (full_timestamp_flag) {
1048     tim->seconds_flag = TRUE;
1049     READ_UINT8 (nr, tim->seconds_value, 6);
1050
1051     tim->minutes_flag = TRUE;
1052     READ_UINT8 (nr, tim->minutes_value, 6);
1053
1054     tim->hours_flag = TRUE;
1055     READ_UINT8 (nr, tim->hours_value, 5);
1056   } else {
1057     READ_UINT8 (nr, tim->seconds_flag, 1);
1058     if (tim->seconds_flag) {
1059       READ_UINT8 (nr, tim->seconds_value, 6);
1060       READ_UINT8 (nr, tim->minutes_flag, 1);
1061       if (tim->minutes_flag) {
1062         READ_UINT8 (nr, tim->minutes_value, 6);
1063         READ_UINT8 (nr, tim->hours_flag, 1);
1064         if (tim->hours_flag)
1065           READ_UINT8 (nr, tim->hours_value, 5);
1066       }
1067     }
1068   }
1069
1070   time_offset_length = 0;
1071   if (vui->nal_hrd_parameters_present_flag)
1072     time_offset_length = vui->nal_hrd_parameters.time_offset_length;
1073   else if (vui->vcl_hrd_parameters_present_flag)
1074     time_offset_length = vui->vcl_hrd_parameters.time_offset_length;
1075
1076   if (time_offset_length > 0)
1077     READ_UINT32 (nr, tim->time_offset, time_offset_length);
1078
1079   return TRUE;
1080
1081 error:
1082   GST_WARNING ("error parsing \"Clock timestamp\"");
1083   return FALSE;
1084 }
1085
1086 static gboolean
1087 gst_h264_parser_parse_pic_timing (GstH264NalParser * nalparser,
1088     GstH264PicTiming * tim, NalReader * nr)
1089 {
1090   GST_DEBUG ("parsing \"Picture timing\"");
1091   if (!nalparser->last_sps || !nalparser->last_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   /* default values */
1098   memset (tim->clock_timestamp_flag, 0, 3);
1099
1100   if (nalparser->last_sps->vui_parameters_present_flag) {
1101     GstH264VUIParams *vui = &nalparser->last_sps->vui_parameters;
1102
1103     if (vui->nal_hrd_parameters_present_flag) {
1104       READ_UINT32 (nr, tim->cpb_removal_delay,
1105           vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1106       READ_UINT32 (nr, tim->dpb_output_delay,
1107           vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1108     } else if (vui->nal_hrd_parameters_present_flag) {
1109       READ_UINT32 (nr, tim->cpb_removal_delay,
1110           vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1111       READ_UINT32 (nr, tim->dpb_output_delay,
1112           vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1113     }
1114
1115     if (vui->pic_struct_present_flag) {
1116       const guint8 num_clock_ts_table[9] = {
1117         1, 1, 1, 2, 2, 3, 3, 2, 3
1118       };
1119       guint8 num_clock_num_ts;
1120       guint i;
1121
1122       tim->pic_struct_present_flag = TRUE;
1123       READ_UINT8 (nr, tim->pic_struct, 4);
1124       CHECK_ALLOWED ((gint8) tim->pic_struct, 0, 8);
1125
1126       num_clock_num_ts = num_clock_ts_table[tim->pic_struct];
1127       for (i = 0; i < num_clock_num_ts; i++) {
1128         READ_UINT8 (nr, tim->clock_timestamp_flag[i], 1);
1129         if (tim->clock_timestamp_flag[i]) {
1130           if (!gst_h264_parse_clock_timestamp (&tim->clock_timestamp[i], vui,
1131                   nr))
1132             goto error;
1133         }
1134       }
1135     }
1136   }
1137
1138   return GST_H264_PARSER_OK;
1139
1140 error:
1141   GST_WARNING ("error parsing \"Picture timing\"");
1142   return GST_H264_PARSER_ERROR;
1143 }
1144
1145 /******** API *************/
1146
1147 /**
1148  * gst_h264_nal_parser_new:
1149  *
1150  * Creates a new #GstH264NalParser. It should be freed with
1151  * gst_h264_nal_parser_free after use.
1152  *
1153  * Returns: a new #GstH264NalParser
1154  */
1155 GstH264NalParser *
1156 gst_h264_nal_parser_new (void)
1157 {
1158   GstH264NalParser *nalparser;
1159
1160   nalparser = g_slice_new0 (GstH264NalParser);
1161   GST_DEBUG_CATEGORY_INIT (h264_parser_debug, "codecparsers_h264", 0,
1162       "h264 parser library");
1163
1164   return nalparser;
1165 }
1166
1167 /**
1168  * gst_h264_nal_parser_free:
1169  * @nalparser: the #GstH264NalParser to free
1170  *
1171  * Frees @nalparser and sets it to %NULL
1172  */
1173 void
1174 gst_h264_nal_parser_free (GstH264NalParser * nalparser)
1175 {
1176   g_slice_free (GstH264NalParser, nalparser);
1177
1178   nalparser = NULL;
1179 }
1180
1181 /**
1182  * gst_h264_parser_identify_nalu_unchecked:
1183  * @nalparser: a #GstH264NalParser
1184  * @data: The data to parse
1185  * @offset: the offset from which to parse @data
1186  * @size: the size of @data
1187  * @nalu: The #GstH264NalUnit where to store parsed nal headers
1188  *
1189  * Parses @data and fills @nalu from the next nalu data from @data.
1190  *
1191  * This differs from @gst_h264_parser_identify_nalu in that it doesn't
1192  * check whether the packet is complete or not.
1193  *
1194  * Note: Only use this function if you already know the provided @data
1195  * is a complete NALU, else use @gst_h264_parser_identify_nalu.
1196  *
1197  * Returns: a #GstH264ParserResult
1198  */
1199 GstH264ParserResult
1200 gst_h264_parser_identify_nalu_unchecked (GstH264NalParser * nalparser,
1201     const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1202 {
1203   gint off1;
1204
1205   if (size < offset + 4) {
1206     GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1207         ", offset %u", size, offset);
1208     return GST_H264_PARSER_ERROR;
1209   }
1210
1211   off1 = scan_for_start_codes (data + offset, size - offset);
1212
1213   if (off1 < 0) {
1214     GST_DEBUG ("No start code prefix in this buffer");
1215     return GST_H264_PARSER_NO_NAL;
1216   }
1217
1218   if (offset + off1 == size - 1) {
1219     GST_DEBUG ("Missing data to identify nal unit");
1220
1221     return GST_H264_PARSER_ERROR;
1222   }
1223
1224   nalu->valid = TRUE;
1225   nalu->sc_offset = offset + off1;
1226
1227   /* sc might have 2 or 3 0-bytes */
1228   if (nalu->sc_offset > 0 && data[nalu->sc_offset - 1] == 00)
1229     nalu->sc_offset--;
1230
1231   nalu->offset = offset + off1 + 3;
1232   nalu->data = (guint8 *) data;
1233
1234   set_nalu_datas (nalu);
1235
1236   if (nalu->type == GST_H264_NAL_SEQ_END ||
1237       nalu->type == GST_H264_NAL_STREAM_END) {
1238     GST_DEBUG ("end-of-seq or end-of-stream nal found");
1239     nalu->size = 0;
1240     return GST_H264_PARSER_OK;
1241   }
1242
1243   nalu->size = size - nalu->offset;
1244
1245   return GST_H264_PARSER_OK;
1246 }
1247
1248 /**
1249  * gst_h264_parser_identify_nalu:
1250  * @nalparser: a #GstH264NalParser
1251  * @data: The data to parse
1252  * @offset: the offset from which to parse @data
1253  * @size: the size of @data
1254  * @nalu: The #GstH264NalUnit where to store parsed nal headers
1255  *
1256  * Parses @data and fills @nalu from the next nalu data from @data
1257  *
1258  * Returns: a #GstH264ParserResult
1259  */
1260 GstH264ParserResult
1261 gst_h264_parser_identify_nalu (GstH264NalParser * nalparser,
1262     const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1263 {
1264   GstH264ParserResult res;
1265   gint off2;
1266
1267   res =
1268       gst_h264_parser_identify_nalu_unchecked (nalparser, data, offset, size,
1269       nalu);
1270
1271   if (res != GST_H264_PARSER_OK || nalu->size == 0)
1272     goto beach;
1273
1274   off2 = scan_for_start_codes (data + nalu->offset, size - nalu->offset);
1275   if (off2 < 0) {
1276     GST_DEBUG ("Nal start %d, No end found", nalu->offset);
1277
1278     return GST_H264_PARSER_NO_NAL_END;
1279   }
1280
1281   if (off2 > 0 && data[nalu->offset + off2 - 1] == 00)
1282     off2--;
1283
1284   nalu->size = off2;
1285   if (nalu->size < 2)
1286     return GST_H264_PARSER_BROKEN_DATA;
1287
1288   GST_DEBUG ("Complete nal found. Off: %d, Size: %d", nalu->offset, nalu->size);
1289
1290 beach:
1291   return res;
1292 }
1293
1294
1295 /**
1296  * gst_h264_parser_identify_nalu_avc:
1297  * @nalparser: a #GstH264NalParser
1298  * @data: The data to parse, must be the beging of the Nal unit
1299  * @offset: the offset from which to parse @data
1300  * @size: the size of @data
1301  * @nal_length_size: the size in bytes of the AVC nal length prefix.
1302  * @nalu: The #GstH264NalUnit where to store parsed nal headers
1303  *
1304  * Parses @data and sets @nalu.
1305  *
1306  * Returns: a #GstH264ParserResult
1307  */
1308 GstH264ParserResult
1309 gst_h264_parser_identify_nalu_avc (GstH264NalParser * nalparser,
1310     const guint8 * data, guint offset, gsize size, guint8 nal_length_size,
1311     GstH264NalUnit * nalu)
1312 {
1313   GstBitReader br;
1314
1315   if (size < offset + nal_length_size) {
1316     GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1317         ", offset %u", size, offset);
1318     return GST_H264_PARSER_ERROR;
1319   }
1320
1321   size = size - offset;
1322   gst_bit_reader_init (&br, data + offset, size);
1323
1324   nalu->size = gst_bit_reader_get_bits_uint32_unchecked (&br,
1325       nal_length_size * 8);
1326   nalu->sc_offset = offset;
1327   nalu->offset = offset + nal_length_size;
1328
1329   if (size < nalu->size + nal_length_size) {
1330     nalu->size = 0;
1331
1332     return GST_H264_PARSER_NO_NAL_END;
1333   }
1334
1335   nalu->data = (guint8 *) data;
1336
1337   set_nalu_datas (nalu);
1338
1339   if (nalu->size < 2)
1340     return GST_H264_PARSER_BROKEN_DATA;
1341
1342   nalu->valid = TRUE;
1343
1344   return GST_H264_PARSER_OK;
1345 }
1346
1347 /**
1348  * gst_h264_parser_parse_nal:
1349  * @nalparser: a #GstH264NalParser
1350  * @nalu: The #GstH264NalUnit to parse
1351  *
1352  * This function should be called in the case one doesn't need to
1353  * parse a specific structure. It is necessary to do so to make
1354  * sure @nalparser is up to date.
1355  *
1356  * Returns: a #GstH264ParserResult
1357  */
1358 GstH264ParserResult
1359 gst_h264_parser_parse_nal (GstH264NalParser * nalparser, GstH264NalUnit * nalu)
1360 {
1361   GstH264SPS sps;
1362   GstH264PPS pps;
1363
1364   switch (nalu->type) {
1365     case GST_H264_NAL_SPS:
1366       return gst_h264_parser_parse_sps (nalparser, nalu, &sps, FALSE);
1367       break;
1368     case GST_H264_NAL_PPS:
1369       return gst_h264_parser_parse_pps (nalparser, nalu, &pps);
1370   }
1371
1372   return GST_H264_PARSER_OK;
1373 }
1374
1375 /**
1376  * gst_h264_parser_parse_sps:
1377  * @nalparser: a #GstH264NalParser
1378  * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1379  * @sps: The #GstH264SPS to fill.
1380  * @parse_vui_params: Whether to parse the vui_params or not
1381  *
1382  * Parses @data, and fills the @sps structure.
1383  *
1384  * Returns: a #GstH264ParserResult
1385  */
1386 GstH264ParserResult
1387 gst_h264_parser_parse_sps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1388     GstH264SPS * sps, gboolean parse_vui_params)
1389 {
1390   GstH264ParserResult res = gst_h264_parse_sps (nalu, sps, parse_vui_params);
1391
1392   if (res == GST_H264_PARSER_OK) {
1393     GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
1394
1395     nalparser->sps[sps->id] = *sps;
1396     nalparser->last_sps = &nalparser->sps[sps->id];
1397   }
1398
1399
1400
1401   return res;
1402 }
1403
1404 /**
1405  * gst_h264_parse_sps:
1406  * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1407  * @sps: The #GstH264SPS to fill.
1408  * @parse_vui_params: Whether to parse the vui_params or not
1409  *
1410  * Parses @data, and fills the @sps structure.
1411  *
1412  * Returns: a #GstH264ParserResult
1413  */
1414 GstH264ParserResult
1415 gst_h264_parse_sps (GstH264NalUnit * nalu, GstH264SPS * sps,
1416     gboolean parse_vui_params)
1417 {
1418   NalReader nr;
1419   gint width, height;
1420   guint8 frame_cropping_flag;
1421   guint subwc[] = { 1, 2, 2, 1 };
1422   guint subhc[] = { 1, 2, 1, 1 };
1423   GstH264VUIParams *vui = NULL;
1424
1425   GST_DEBUG ("parsing SPS");
1426   nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1427
1428   /* set default values for fields that might not be present in the bitstream
1429      and have valid defaults */
1430   sps->chroma_format_idc = 1;
1431   sps->separate_colour_plane_flag = 0;
1432   sps->bit_depth_luma_minus8 = 0;
1433   sps->bit_depth_chroma_minus8 = 0;
1434   memset (sps->scaling_lists_4x4, 16, 96);
1435   memset (sps->scaling_lists_8x8, 16, 384);
1436   sps->mb_adaptive_frame_field_flag = 0;
1437   sps->frame_crop_left_offset = 0;
1438   sps->frame_crop_right_offset = 0;
1439   sps->frame_crop_top_offset = 0;
1440   sps->frame_crop_bottom_offset = 0;
1441   sps->delta_pic_order_always_zero_flag = 0;
1442
1443   READ_UINT8 (&nr, sps->profile_idc, 8);
1444   READ_UINT8 (&nr, sps->constraint_set0_flag, 1);
1445   READ_UINT8 (&nr, sps->constraint_set1_flag, 1);
1446   READ_UINT8 (&nr, sps->constraint_set2_flag, 1);
1447   READ_UINT8 (&nr, sps->constraint_set3_flag, 1);
1448
1449   /* skip reserved_zero_4bits */
1450   if (!nal_reader_skip (&nr, 4))
1451     goto error;
1452
1453   READ_UINT8 (&nr, sps->level_idc, 8);
1454
1455   READ_UE_ALLOWED (&nr, sps->id, 0, GST_H264_MAX_SPS_COUNT - 1);
1456
1457   if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
1458       sps->profile_idc == 122 || sps->profile_idc == 244 ||
1459       sps->profile_idc == 44 || sps->profile_idc == 83 ||
1460       sps->profile_idc == 86) {
1461     READ_UE_ALLOWED (&nr, sps->chroma_format_idc, 0, 3);
1462     if (sps->chroma_format_idc == 3)
1463       READ_UINT8 (&nr, sps->separate_colour_plane_flag, 1);
1464
1465     READ_UE_ALLOWED (&nr, sps->bit_depth_luma_minus8, 0, 6);
1466     READ_UE_ALLOWED (&nr, sps->bit_depth_chroma_minus8, 0, 6);
1467     READ_UINT8 (&nr, sps->qpprime_y_zero_transform_bypass_flag, 1);
1468
1469     READ_UINT8 (&nr, sps->scaling_matrix_present_flag, 1);
1470     if (sps->scaling_matrix_present_flag) {
1471       guint8 n_lists;
1472
1473       n_lists = (sps->chroma_format_idc != 3) ? 8 : 12;
1474       if (!gst_h264_parser_parse_scaling_list (&nr,
1475               sps->scaling_lists_4x4, sps->scaling_lists_8x8,
1476               default_4x4_inter, default_4x4_intra,
1477               default_8x8_inter, default_8x8_intra, n_lists))
1478         goto error;
1479     }
1480   }
1481
1482   READ_UE_ALLOWED (&nr, sps->log2_max_frame_num_minus4, 0, 12);
1483
1484   sps->max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
1485
1486   READ_UE_ALLOWED (&nr, sps->pic_order_cnt_type, 0, 2);
1487   if (sps->pic_order_cnt_type == 0) {
1488     READ_UE_ALLOWED (&nr, sps->log2_max_pic_order_cnt_lsb_minus4, 0, 12);
1489   } else if (sps->pic_order_cnt_type == 1) {
1490     guint i;
1491
1492     READ_UINT8 (&nr, sps->delta_pic_order_always_zero_flag, 1);
1493     READ_SE (&nr, sps->offset_for_non_ref_pic);
1494     READ_SE (&nr, sps->offset_for_top_to_bottom_field);
1495     READ_UE_ALLOWED (&nr, sps->num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
1496
1497     for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
1498       READ_SE (&nr, sps->offset_for_ref_frame[i]);
1499   }
1500
1501   READ_UE (&nr, sps->num_ref_frames);
1502   READ_UINT8 (&nr, sps->gaps_in_frame_num_value_allowed_flag, 1);
1503   READ_UE (&nr, sps->pic_width_in_mbs_minus1);
1504   READ_UE (&nr, sps->pic_height_in_map_units_minus1);
1505   READ_UINT8 (&nr, sps->frame_mbs_only_flag, 1);
1506
1507   if (!sps->frame_mbs_only_flag)
1508     READ_UINT8 (&nr, sps->mb_adaptive_frame_field_flag, 1);
1509
1510   READ_UINT8 (&nr, sps->direct_8x8_inference_flag, 1);
1511   READ_UINT8 (&nr, frame_cropping_flag, 1);
1512   if (frame_cropping_flag) {
1513     READ_UE (&nr, sps->frame_crop_left_offset);
1514     READ_UE (&nr, sps->frame_crop_right_offset);
1515     READ_UE (&nr, sps->frame_crop_top_offset);
1516     READ_UE (&nr, sps->frame_crop_bottom_offset);
1517   }
1518
1519   READ_UINT8 (&nr, sps->vui_parameters_present_flag, 1);
1520   if (sps->vui_parameters_present_flag && parse_vui_params) {
1521     if (!gst_h264_parse_vui_parameters (sps, &nr))
1522       goto error;
1523     vui = &sps->vui_parameters;
1524   }
1525
1526   /* calculate ChromaArrayType */
1527   if (sps->separate_colour_plane_flag)
1528     sps->chroma_array_type = 0;
1529   else
1530     sps->chroma_array_type = sps->chroma_format_idc;
1531
1532   /* Calculate  width and height */
1533   width = (sps->pic_width_in_mbs_minus1 + 1);
1534   width *= 16;
1535   height = (sps->pic_height_in_map_units_minus1 + 1);
1536   height *= 16 * (2 - sps->frame_mbs_only_flag);
1537   GST_LOG ("initial width=%d, height=%d", width, height);
1538
1539   width -= (sps->frame_crop_left_offset + sps->frame_crop_right_offset)
1540       * subwc[sps->chroma_format_idc];
1541   height -= (sps->frame_crop_top_offset + sps->frame_crop_bottom_offset
1542       * subhc[sps->chroma_format_idc] * (2 - sps->frame_mbs_only_flag));
1543   if (width < 0 || height < 0) {
1544     GST_WARNING ("invalid width/height in SPS");
1545     goto error;
1546   }
1547   GST_LOG ("final width=%u, height=%u", width, height);
1548   sps->width = width;
1549   sps->height = height;
1550
1551   sps->fps_num = 0;
1552   sps->fps_den = 1;
1553
1554   if (vui && vui->timing_info_present_flag) {
1555     /* derive framerate */
1556     /* FIXME verify / also handle other cases */
1557     GST_LOG ("Framerate: %u %u %u %u", parse_vui_params,
1558         vui->fixed_frame_rate_flag, sps->frame_mbs_only_flag,
1559         vui->pic_struct_present_flag);
1560
1561     if (parse_vui_params && vui->fixed_frame_rate_flag &&
1562         sps->frame_mbs_only_flag && !vui->pic_struct_present_flag) {
1563       sps->fps_num = vui->time_scale;
1564       sps->fps_den = vui->num_units_in_tick;
1565       /* picture is a frame = 2 fields */
1566       sps->fps_den *= 2;
1567       GST_LOG ("framerate %d/%d", sps->fps_num, sps->fps_den);
1568     }
1569   } else {
1570     GST_LOG ("No VUI, unknown framerate");
1571   }
1572
1573   sps->valid = TRUE;
1574
1575   return GST_H264_PARSER_OK;
1576
1577 error:
1578   GST_WARNING ("error parsing \"Sequence parameter set\"");
1579   sps->valid = FALSE;
1580   return GST_H264_PARSER_ERROR;
1581 }
1582
1583 /**
1584  * gst_h264_parse_pps:
1585  * @nalparser: a #GstH264NalParser
1586  * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
1587  * @pps: The #GstH264PPS to fill.
1588  *
1589  * Parses @data, and fills the @pps structure.
1590  *
1591  * Returns: a #GstH264ParserResult
1592  */
1593 GstH264ParserResult
1594 gst_h264_parse_pps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1595     GstH264PPS * pps)
1596 {
1597   NalReader nr;
1598   GstH264SPS *sps;
1599   gint sps_id;
1600   guint8 pic_scaling_matrix_present_flag;
1601   gint qp_bd_offset;
1602
1603   GST_DEBUG ("parsing PPS");
1604
1605   nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1606
1607   READ_UE_ALLOWED (&nr, pps->id, 0, GST_H264_MAX_PPS_COUNT - 1);
1608   READ_UE_ALLOWED (&nr, sps_id, 0, GST_H264_MAX_SPS_COUNT - 1);
1609
1610   sps = gst_h264_parser_get_sps (nalparser, sps_id);
1611   if (!sps) {
1612     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1613         sps_id);
1614     return GST_H264_PARSER_BROKEN_LINK;
1615   }
1616   pps->sequence = sps;
1617   qp_bd_offset = 6 * (sps->bit_depth_luma_minus8 +
1618       sps->separate_colour_plane_flag);
1619
1620   /* set default values for fields that might not be present in the bitstream
1621      and have valid defaults */
1622   pps->slice_group_id = NULL;
1623   pps->transform_8x8_mode_flag = 0;
1624   memcpy (&pps->scaling_lists_4x4, &sps->scaling_lists_4x4, 96);
1625   memcpy (&pps->scaling_lists_8x8, &sps->scaling_lists_8x8, 384);
1626
1627   READ_UINT8 (&nr, pps->entropy_coding_mode_flag, 1);
1628   READ_UINT8 (&nr, pps->pic_order_present_flag, 1);
1629   READ_UE_ALLOWED (&nr, pps->num_slice_groups_minus1, 0, 7);
1630   if (pps->num_slice_groups_minus1 > 0) {
1631     READ_UE_ALLOWED (&nr, pps->slice_group_map_type, 0, 6);
1632
1633     if (pps->slice_group_map_type == 0) {
1634       gint i;
1635
1636       for (i = 0; i <= pps->num_slice_groups_minus1; i++)
1637         READ_UE (&nr, pps->run_length_minus1[i]);
1638     } else if (pps->slice_group_map_type == 2) {
1639       gint i;
1640
1641       for (i = 0; i <= pps->num_slice_groups_minus1; i++) {
1642         READ_UE (&nr, pps->top_left[i]);
1643         READ_UE (&nr, pps->bottom_right[i]);
1644       }
1645     } else if (pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
1646       READ_UINT8 (&nr, pps->slice_group_change_direction_flag, 1);
1647       READ_UE (&nr, pps->slice_group_change_rate_minus1);
1648     } else if (pps->slice_group_map_type == 6) {
1649       gint bits;
1650       gint i;
1651
1652       READ_UE (&nr, pps->pic_size_in_map_units_minus1);
1653       bits = g_bit_storage (pps->num_slice_groups_minus1);
1654
1655       pps->slice_group_id =
1656           g_new (guint8, pps->pic_size_in_map_units_minus1 + 1);
1657       for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++)
1658         READ_UINT8 (&nr, pps->slice_group_id[i], bits);
1659     }
1660   }
1661
1662   READ_UE_ALLOWED (&nr, pps->num_ref_idx_l0_active_minus1, 0, 31);
1663   READ_UE_ALLOWED (&nr, pps->num_ref_idx_l1_active_minus1, 0, 31);
1664   READ_UINT8 (&nr, pps->weighted_pred_flag, 1);
1665   READ_UINT8 (&nr, pps->weighted_bipred_idc, 2);
1666   READ_SE_ALLOWED (&nr, pps->pic_init_qp_minus26, -(26 + qp_bd_offset), 25);
1667   READ_SE_ALLOWED (&nr, pps->pic_init_qs_minus26, -26, 25);
1668   READ_SE_ALLOWED (&nr, pps->chroma_qp_index_offset, -12, 12);
1669   pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
1670   READ_UINT8 (&nr, pps->deblocking_filter_control_present_flag, 1);
1671   READ_UINT8 (&nr, pps->constrained_intra_pred_flag, 1);
1672   READ_UINT8 (&nr, pps->redundant_pic_cnt_present_flag, 1);
1673
1674   if (!gst_h264_parser_more_data (&nr))
1675     goto done;
1676
1677   READ_UINT8 (&nr, pps->transform_8x8_mode_flag, 1);
1678
1679   READ_UINT8 (&nr, pic_scaling_matrix_present_flag, 1);
1680   if (pic_scaling_matrix_present_flag) {
1681     guint8 n_lists;
1682
1683     n_lists = 6 + ((sps->chroma_format_idc != 3) ? 2 : 6) *
1684         pps->transform_8x8_mode_flag;
1685
1686     if (sps->scaling_matrix_present_flag) {
1687       if (!gst_h264_parser_parse_scaling_list (&nr,
1688               pps->scaling_lists_4x4, pps->scaling_lists_8x8,
1689               sps->scaling_lists_4x4[0], sps->scaling_lists_4x4[3],
1690               sps->scaling_lists_8x8[0], sps->scaling_lists_8x8[3], n_lists))
1691         goto error;
1692     } else {
1693       if (!gst_h264_parser_parse_scaling_list (&nr,
1694               pps->scaling_lists_4x4, pps->scaling_lists_8x8,
1695               default_4x4_inter, default_4x4_intra,
1696               default_8x8_inter, default_8x8_intra, n_lists))
1697         goto error;
1698     }
1699   }
1700
1701   READ_SE_ALLOWED (&nr, pps->second_chroma_qp_index_offset, -12, 12);
1702
1703 done:
1704   pps->valid = TRUE;
1705   return GST_H264_PARSER_OK;
1706
1707 error:
1708   GST_WARNING ("error parsing \"Picture parameter set\"");
1709   pps->valid = FALSE;
1710   return GST_H264_PARSER_ERROR;
1711 }
1712
1713 /**
1714  * gst_h264_parser_parse_pps:
1715  * @nalparser: a #GstH264NalParser
1716  * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
1717  * @pps: The #GstH264PPS to fill.
1718  *
1719  * Parses @data, and fills the @pps structure.
1720  *
1721  * Returns: a #GstH264ParserResult
1722  */
1723 GstH264ParserResult
1724 gst_h264_parser_parse_pps (GstH264NalParser * nalparser,
1725     GstH264NalUnit * nalu, GstH264PPS * pps)
1726 {
1727   GstH264ParserResult res = gst_h264_parse_pps (nalparser, nalu, pps);
1728
1729   if (res == GST_H264_PARSER_OK) {
1730     GST_DEBUG ("adding picture parameter set with id: %d to array", pps->id);
1731
1732     nalparser->pps[pps->id] = *pps;
1733     nalparser->last_pps = &nalparser->pps[pps->id];
1734   }
1735
1736   return res;
1737 }
1738
1739 /**
1740  * gst_h264_parser_parse_slice_hdr:
1741  * @nalparser: a #GstH264NalParser
1742  * @nalu: The #GST_H264_NAL_SLICE #GstH264NalUnit to parse
1743  * @slice: The #GstH264SliceHdr to fill.
1744  * @parse_pred_weight_table: Whether to parse the pred_weight_table or not
1745  * @parse_dec_ref_pic_marking: Whether to parse the dec_ref_pic_marking or not
1746  *
1747  * Parses @data, and fills the @slice structure.
1748  *
1749  * Returns: a #GstH264ParserResult
1750  */
1751 GstH264ParserResult
1752 gst_h264_parser_parse_slice_hdr (GstH264NalParser * nalparser,
1753     GstH264NalUnit * nalu, GstH264SliceHdr * slice,
1754     gboolean parse_pred_weight_table, gboolean parse_dec_ref_pic_marking)
1755 {
1756   NalReader nr;
1757   gint pps_id;
1758   GstH264PPS *pps;
1759   GstH264SPS *sps;
1760
1761   if (!nalu->size) {
1762     GST_DEBUG ("Invalid Nal Unit");
1763     return GST_H264_PARSER_ERROR;
1764   }
1765
1766
1767   nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1768
1769   READ_UE (&nr, slice->first_mb_in_slice);
1770   READ_UE (&nr, slice->type);
1771
1772   GST_DEBUG ("parsing \"Slice header\", slice type %u", slice->type);
1773
1774   READ_UE_ALLOWED (&nr, pps_id, 0, GST_H264_MAX_PPS_COUNT - 1);
1775   pps = gst_h264_parser_get_pps (nalparser, pps_id);
1776
1777   if (!pps) {
1778     GST_WARNING ("couldn't find associated picture parameter set with id: %d",
1779         pps_id);
1780
1781     return GST_H264_PARSER_BROKEN_LINK;
1782   }
1783
1784   slice->pps = pps;
1785   sps = pps->sequence;
1786   if (!sps) {
1787     GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1788         pps->id);
1789     return GST_H264_PARSER_BROKEN_LINK;
1790   }
1791
1792   /* set default values for fields that might not be present in the bitstream
1793      and have valid defaults */
1794   slice->field_pic_flag = 0;
1795   slice->bottom_field_flag = 0;
1796   slice->delta_pic_order_cnt_bottom = 0;
1797   slice->delta_pic_order_cnt[0] = 0;
1798   slice->delta_pic_order_cnt[1] = 0;
1799   slice->redundant_pic_cnt = 0;
1800   slice->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_active_minus1;
1801   slice->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_active_minus1;
1802   slice->disable_deblocking_filter_idc = 0;
1803   slice->slice_alpha_c0_offset_div2 = 0;
1804
1805   if (sps->separate_colour_plane_flag)
1806     READ_UINT8 (&nr, slice->colour_plane_id, 2);
1807
1808   READ_UINT16 (&nr, slice->frame_num, sps->log2_max_frame_num_minus4 + 4);
1809
1810   if (!sps->frame_mbs_only_flag) {
1811     READ_UINT8 (&nr, slice->field_pic_flag, 1);
1812     if (slice->field_pic_flag)
1813       READ_UINT8 (&nr, slice->bottom_field_flag, 1);
1814   }
1815
1816   /* calculate MaxPicNum */
1817   if (slice->field_pic_flag)
1818     slice->max_pic_num = sps->max_frame_num;
1819   else
1820     slice->max_pic_num = 2 * sps->max_frame_num;
1821
1822   if (nalu->type == 5)
1823     READ_UE_ALLOWED (&nr, slice->idr_pic_id, 0, G_MAXUINT16);
1824
1825   if (sps->pic_order_cnt_type == 0) {
1826     READ_UINT16 (&nr, slice->pic_order_cnt_lsb,
1827         sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1828
1829     if (pps->pic_order_present_flag && !slice->field_pic_flag)
1830       READ_SE (&nr, slice->delta_pic_order_cnt_bottom);
1831   }
1832
1833   if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1834     READ_SE (&nr, slice->delta_pic_order_cnt[0]);
1835     if (pps->pic_order_present_flag && !slice->field_pic_flag)
1836       READ_SE (&nr, slice->delta_pic_order_cnt[1]);
1837   }
1838
1839   if (pps->redundant_pic_cnt_present_flag)
1840     READ_UE_ALLOWED (&nr, slice->redundant_pic_cnt, 0, G_MAXINT8);
1841
1842   if (GST_H264_IS_B_SLICE (slice))
1843     READ_UINT8 (&nr, slice->direct_spatial_mv_pred_flag, 1);
1844
1845   if (GST_H264_IS_P_SLICE (slice) || GST_H264_IS_SP_SLICE (slice) ||
1846       GST_H264_IS_B_SLICE (slice)) {
1847     guint8 num_ref_idx_active_override_flag;
1848
1849     READ_UINT8 (&nr, num_ref_idx_active_override_flag, 1);
1850     if (num_ref_idx_active_override_flag) {
1851       READ_UE_ALLOWED (&nr, slice->num_ref_idx_l0_active_minus1, 0, 31);
1852
1853       if (GST_H264_IS_B_SLICE (slice))
1854         READ_UE_ALLOWED (&nr, slice->num_ref_idx_l1_active_minus1, 0, 31);
1855     }
1856   }
1857
1858   if (!slice_parse_ref_pic_list_modification (slice, &nr))
1859     goto error;
1860
1861   if ((pps->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice)
1862               || GST_H264_IS_SP_SLICE (slice)))
1863       || (pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice))) {
1864     if (!gst_h264_slice_parse_pred_weight_table (slice, &nr,
1865             sps->chroma_array_type))
1866       goto error;
1867   }
1868
1869   if (nalu->ref_idc != 0) {
1870     if (!gst_h264_slice_parse_dec_ref_pic_marking (slice, nalu, &nr))
1871       goto error;
1872   }
1873
1874   if (pps->entropy_coding_mode_flag && !GST_H264_IS_I_SLICE (slice) &&
1875       !GST_H264_IS_SI_SLICE (slice))
1876     READ_UE_ALLOWED (&nr, slice->cabac_init_idc, 0, 2);
1877
1878   READ_SE_ALLOWED (&nr, slice->slice_qp_delta, -87, 77);
1879
1880   if (GST_H264_IS_SP_SLICE (slice) || GST_H264_IS_SI_SLICE (slice)) {
1881     guint8 sp_for_switch_flag;
1882
1883     if (GST_H264_IS_SP_SLICE (slice))
1884       READ_UINT8 (&nr, sp_for_switch_flag, 1);
1885     READ_SE_ALLOWED (&nr, slice->slice_qs_delta, -51, 51);
1886   }
1887
1888   if (pps->deblocking_filter_control_present_flag) {
1889     READ_UE_ALLOWED (&nr, slice->disable_deblocking_filter_idc, 0, 2);
1890     if (slice->disable_deblocking_filter_idc != 1) {
1891       READ_SE_ALLOWED (&nr, slice->slice_alpha_c0_offset_div2, -6, 6);
1892       READ_SE_ALLOWED (&nr, slice->slice_beta_offset_div2, -6, 6);
1893     }
1894   }
1895
1896   if (pps->num_slice_groups_minus1 > 0 &&
1897       pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
1898     /* Ceil(Log2(PicSizeInMapUnits / SliceGroupChangeRate + 1))  [7-33] */
1899     guint32 PicWidthInMbs = sps->pic_width_in_mbs_minus1 + 1;
1900     guint32 PicHeightInMapUnits = sps->pic_height_in_map_units_minus1 + 1;
1901     guint32 PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
1902     guint32 SliceGroupChangeRate = pps->slice_group_change_rate_minus1 + 1;
1903     const guint n = ceil_log2 (PicSizeInMapUnits / SliceGroupChangeRate + 1);
1904     READ_UINT16 (&nr, slice->slice_group_change_cycle, n);
1905   }
1906
1907   slice->header_size = nal_reader_get_pos (&nr);
1908   slice->n_emulation_prevention_bytes = nal_reader_get_epb_count (&nr);
1909
1910   return GST_H264_PARSER_OK;
1911
1912 error:
1913   GST_WARNING ("error parsing \"Slice header\"");
1914   return GST_H264_PARSER_ERROR;
1915 }
1916
1917 /**
1918  * gst_h264_parser_parse_sei:
1919  * @nalparser: a #GstH264NalParser
1920  * @nalu: The #GST_H264_NAL_SEI #GstH264NalUnit to parse
1921  * @sei: The #GstH264SEIMessage to fill.
1922  *
1923  * Parses @data, and fills the @sei structures.
1924  *
1925  * Returns: a #GstH264ParserResult
1926  */
1927 GstH264ParserResult
1928 gst_h264_parser_parse_sei (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1929     GstH264SEIMessage * sei)
1930 {
1931   NalReader nr;
1932
1933   guint32 payloadSize;
1934   guint8 payload_type_byte, payload_size_byte;
1935   guint remaining, payload_size;
1936   gboolean res;
1937
1938   GST_DEBUG ("parsing \"Sei message\"");
1939
1940   nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1941
1942   /* init */
1943   memset (sei, 0, sizeof (*sei));
1944
1945   sei->payloadType = 0;
1946   do {
1947     READ_UINT8 (&nr, payload_type_byte, 8);
1948     sei->payloadType += payload_type_byte;
1949   } while (payload_type_byte == 0xff);
1950
1951   payloadSize = 0;
1952   do {
1953     READ_UINT8 (&nr, payload_size_byte, 8);
1954     payloadSize += payload_size_byte;
1955   }
1956   while (payload_size_byte == 0xff);
1957
1958   remaining = nal_reader_get_remaining (&nr) * 8;
1959   payload_size = payloadSize < remaining ? payloadSize : remaining;
1960
1961   GST_DEBUG ("SEI message received: payloadType  %u, payloadSize = %u bytes",
1962       sei->payloadType, payload_size);
1963
1964   if (sei->payloadType == GST_H264_SEI_BUF_PERIOD) {
1965     /* size not set; might depend on emulation_prevention_three_byte */
1966     res = gst_h264_parser_parse_buffering_period (nalparser,
1967         &sei->payload.buffering_period, &nr);
1968   } else if (sei->payloadType == GST_H264_SEI_PIC_TIMING) {
1969     /* size not set; might depend on emulation_prevention_three_byte */
1970     res = gst_h264_parser_parse_pic_timing (nalparser,
1971         &sei->payload.pic_timing, &nr);
1972   } else
1973     res = GST_H264_PARSER_OK;
1974
1975   return res;
1976
1977 error:
1978   GST_WARNING ("error parsing \"Sei message\"");
1979   return GST_H264_PARSER_ERROR;
1980 }