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