Libav
h264_ps.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... parameter set decoding
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include <inttypes.h>
29 
30 #include "libavutil/imgutils.h"
31 #include "internal.h"
32 #include "avcodec.h"
33 #include "h264.h"
34 #include "h264data.h" //FIXME FIXME FIXME (just for zigzag_scan)
35 #include "golomb.h"
36 
37 #define MAX_LOG2_MAX_FRAME_NUM (12 + 4)
38 #define MIN_LOG2_MAX_FRAME_NUM 4
39 
40 static const AVRational pixel_aspect[17] = {
41  { 0, 1 },
42  { 1, 1 },
43  { 12, 11 },
44  { 10, 11 },
45  { 16, 11 },
46  { 40, 33 },
47  { 24, 11 },
48  { 20, 11 },
49  { 32, 11 },
50  { 80, 33 },
51  { 18, 11 },
52  { 15, 11 },
53  { 64, 33 },
54  { 160, 99 },
55  { 4, 3 },
56  { 3, 2 },
57  { 2, 1 },
58 };
59 
60 #define QP(qP, depth) ((qP) + 6 * ((depth) - 8))
61 
62 #define CHROMA_QP_TABLE_END(d) \
63  QP(0, d), QP(1, d), QP(2, d), QP(3, d), QP(4, d), QP(5, d), \
64  QP(6, d), QP(7, d), QP(8, d), QP(9, d), QP(10, d), QP(11, d), \
65  QP(12, d), QP(13, d), QP(14, d), QP(15, d), QP(16, d), QP(17, d), \
66  QP(18, d), QP(19, d), QP(20, d), QP(21, d), QP(22, d), QP(23, d), \
67  QP(24, d), QP(25, d), QP(26, d), QP(27, d), QP(28, d), QP(29, d), \
68  QP(29, d), QP(30, d), QP(31, d), QP(32, d), QP(32, d), QP(33, d), \
69  QP(34, d), QP(34, d), QP(35, d), QP(35, d), QP(36, d), QP(36, d), \
70  QP(37, d), QP(37, d), QP(37, d), QP(38, d), QP(38, d), QP(38, d), \
71  QP(39, d), QP(39, d), QP(39, d), QP(39, d)
72 
74  { CHROMA_QP_TABLE_END(8) },
75  { 0, 1, 2, 3, 4, 5,
77  { 0, 1, 2, 3, 4, 5,
78  6, 7, 8, 9, 10, 11,
79  CHROMA_QP_TABLE_END(10) },
80 };
81 
82 static const uint8_t default_scaling4[2][16] = {
83  { 6, 13, 20, 28, 13, 20, 28, 32,
84  20, 28, 32, 37, 28, 32, 37, 42 },
85  { 10, 14, 20, 24, 14, 20, 24, 27,
86  20, 24, 27, 30, 24, 27, 30, 34 }
87 };
88 
89 static const uint8_t default_scaling8[2][64] = {
90  { 6, 10, 13, 16, 18, 23, 25, 27,
91  10, 11, 16, 18, 23, 25, 27, 29,
92  13, 16, 18, 23, 25, 27, 29, 31,
93  16, 18, 23, 25, 27, 29, 31, 33,
94  18, 23, 25, 27, 29, 31, 33, 36,
95  23, 25, 27, 29, 31, 33, 36, 38,
96  25, 27, 29, 31, 33, 36, 38, 40,
97  27, 29, 31, 33, 36, 38, 40, 42 },
98  { 9, 13, 15, 17, 19, 21, 22, 24,
99  13, 13, 17, 19, 21, 22, 24, 25,
100  15, 17, 19, 21, 22, 24, 25, 27,
101  17, 19, 21, 22, 24, 25, 27, 28,
102  19, 21, 22, 24, 25, 27, 28, 30,
103  21, 22, 24, 25, 27, 28, 30, 32,
104  22, 24, 25, 27, 28, 30, 32, 33,
105  24, 25, 27, 28, 30, 32, 33, 35 }
106 };
107 
108 static inline int decode_hrd_parameters(H264Context *h, SPS *sps)
109 {
110  int cpb_count, i;
111  cpb_count = get_ue_golomb_31(&h->gb) + 1;
112 
113  if (cpb_count > 32U) {
114  av_log(h->avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
115  return AVERROR_INVALIDDATA;
116  }
117 
118  get_bits(&h->gb, 4); /* bit_rate_scale */
119  get_bits(&h->gb, 4); /* cpb_size_scale */
120  for (i = 0; i < cpb_count; i++) {
121  get_ue_golomb_long(&h->gb); /* bit_rate_value_minus1 */
122  get_ue_golomb_long(&h->gb); /* cpb_size_value_minus1 */
123  get_bits1(&h->gb); /* cbr_flag */
124  }
125  sps->initial_cpb_removal_delay_length = get_bits(&h->gb, 5) + 1;
126  sps->cpb_removal_delay_length = get_bits(&h->gb, 5) + 1;
127  sps->dpb_output_delay_length = get_bits(&h->gb, 5) + 1;
128  sps->time_offset_length = get_bits(&h->gb, 5);
129  sps->cpb_cnt = cpb_count;
130  return 0;
131 }
132 
133 static inline int decode_vui_parameters(H264Context *h, SPS *sps)
134 {
135  int aspect_ratio_info_present_flag;
136  unsigned int aspect_ratio_idc;
137 
138  aspect_ratio_info_present_flag = get_bits1(&h->gb);
139 
140  if (aspect_ratio_info_present_flag) {
141  aspect_ratio_idc = get_bits(&h->gb, 8);
142  if (aspect_ratio_idc == EXTENDED_SAR) {
143  sps->sar.num = get_bits(&h->gb, 16);
144  sps->sar.den = get_bits(&h->gb, 16);
145  } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(pixel_aspect)) {
146  sps->sar = pixel_aspect[aspect_ratio_idc];
147  } else {
148  av_log(h->avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
149  return AVERROR_INVALIDDATA;
150  }
151  } else {
152  sps->sar.num =
153  sps->sar.den = 0;
154  }
155 
156  if (get_bits1(&h->gb)) /* overscan_info_present_flag */
157  get_bits1(&h->gb); /* overscan_appropriate_flag */
158 
161  get_bits(&h->gb, 3); /* video_format */
162  sps->full_range = get_bits1(&h->gb); /* video_full_range_flag */
163 
166  sps->color_primaries = get_bits(&h->gb, 8); /* colour_primaries */
167  sps->color_trc = get_bits(&h->gb, 8); /* transfer_characteristics */
168  sps->colorspace = get_bits(&h->gb, 8); /* matrix_coefficients */
169  if (sps->color_primaries >= AVCOL_PRI_NB)
171  if (sps->color_trc >= AVCOL_TRC_NB)
173  if (sps->colorspace >= AVCOL_SPC_NB)
175  }
176  }
177 
178  /* chroma_location_info_present_flag */
179  if (get_bits1(&h->gb)) {
180  /* chroma_sample_location_type_top_field */
182  get_ue_golomb(&h->gb); /* chroma_sample_location_type_bottom_field */
183  }
184 
186  if (sps->timing_info_present_flag) {
187  sps->num_units_in_tick = get_bits_long(&h->gb, 32);
188  sps->time_scale = get_bits_long(&h->gb, 32);
189  if (!sps->num_units_in_tick || !sps->time_scale) {
191  "time_scale/num_units_in_tick invalid or unsupported (%"PRIu32"/%"PRIu32")\n",
192  sps->time_scale, sps->num_units_in_tick);
193  return AVERROR_INVALIDDATA;
194  }
195  sps->fixed_frame_rate_flag = get_bits1(&h->gb);
196  }
197 
200  if (decode_hrd_parameters(h, sps) < 0)
201  return AVERROR_INVALIDDATA;
204  if (decode_hrd_parameters(h, sps) < 0)
205  return AVERROR_INVALIDDATA;
208  get_bits1(&h->gb); /* low_delay_hrd_flag */
210 
212  if (sps->bitstream_restriction_flag) {
213  get_bits1(&h->gb); /* motion_vectors_over_pic_boundaries_flag */
214  get_ue_golomb(&h->gb); /* max_bytes_per_pic_denom */
215  get_ue_golomb(&h->gb); /* max_bits_per_mb_denom */
216  get_ue_golomb(&h->gb); /* log2_max_mv_length_horizontal */
217  get_ue_golomb(&h->gb); /* log2_max_mv_length_vertical */
218  sps->num_reorder_frames = get_ue_golomb(&h->gb);
219  get_ue_golomb(&h->gb); /*max_dec_frame_buffering*/
220 
221  if (get_bits_left(&h->gb) < 0) {
222  sps->num_reorder_frames = 0;
224  }
225 
226  if (sps->num_reorder_frames > 16U
227  /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
229  "Clipping illegal num_reorder_frames %d\n",
230  sps->num_reorder_frames);
231  sps->num_reorder_frames = 16;
232  return AVERROR_INVALIDDATA;
233  }
234  }
235  if (get_bits_left(&h->gb) < 0) {
237  "Overread VUI by %d bits\n", -get_bits_left(&h->gb));
238  return AVERROR_INVALIDDATA;
239  }
240 
241  return 0;
242 }
243 
244 static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
245  const uint8_t *jvt_list,
246  const uint8_t *fallback_list)
247 {
248  int i, last = 8, next = 8;
249  const uint8_t *scan = size == 16 ? zigzag_scan : ff_zigzag_direct;
250  if (!get_bits1(&h->gb)) /* matrix not written, we use the predicted one */
251  memcpy(factors, fallback_list, size * sizeof(uint8_t));
252  else
253  for (i = 0; i < size; i++) {
254  if (next)
255  next = (last + get_se_golomb(&h->gb)) & 0xff;
256  if (!i && !next) { /* matrix not written, we use the preset one */
257  memcpy(factors, jvt_list, size * sizeof(uint8_t));
258  break;
259  }
260  last = factors[scan[i]] = next ? next : last;
261  }
262 }
263 
265  PPS *pps, int is_sps,
266  uint8_t(*scaling_matrix4)[16],
267  uint8_t(*scaling_matrix8)[64])
268 {
269  int fallback_sps = !is_sps && sps->scaling_matrix_present;
270  const uint8_t *fallback[4] = {
271  fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
272  fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
273  fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
274  fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
275  };
276  if (get_bits1(&h->gb)) {
277  sps->scaling_matrix_present |= is_sps;
278  decode_scaling_list(h, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]); // Intra, Y
279  decode_scaling_list(h, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr
280  decode_scaling_list(h, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb
281  decode_scaling_list(h, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]); // Inter, Y
282  decode_scaling_list(h, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr
283  decode_scaling_list(h, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb
284  if (is_sps || pps->transform_8x8_mode) {
285  decode_scaling_list(h, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y
286  if (sps->chroma_format_idc == 3) {
287  decode_scaling_list(h, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr
288  decode_scaling_list(h, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb
289  }
290  decode_scaling_list(h, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y
291  if (sps->chroma_format_idc == 3) {
292  decode_scaling_list(h, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr
293  decode_scaling_list(h, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb
294  }
295  }
296  }
297 }
298 
300 {
301  int profile_idc, level_idc, constraint_set_flags = 0;
302  unsigned int sps_id;
303  int i, log2_max_frame_num_minus4;
304  SPS *sps;
305 
306  profile_idc = get_bits(&h->gb, 8);
307  constraint_set_flags |= get_bits1(&h->gb) << 0; // constraint_set0_flag
308  constraint_set_flags |= get_bits1(&h->gb) << 1; // constraint_set1_flag
309  constraint_set_flags |= get_bits1(&h->gb) << 2; // constraint_set2_flag
310  constraint_set_flags |= get_bits1(&h->gb) << 3; // constraint_set3_flag
311  constraint_set_flags |= get_bits1(&h->gb) << 4; // constraint_set4_flag
312  constraint_set_flags |= get_bits1(&h->gb) << 5; // constraint_set5_flag
313  skip_bits(&h->gb, 2); // reserved_zero_2bits
314  level_idc = get_bits(&h->gb, 8);
315  sps_id = get_ue_golomb_31(&h->gb);
316 
317  if (sps_id >= MAX_SPS_COUNT) {
318  av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
319  return AVERROR_INVALIDDATA;
320  }
321  sps = av_mallocz(sizeof(SPS));
322  if (!sps)
323  return AVERROR(ENOMEM);
324 
325  sps->sps_id = sps_id;
326  sps->time_offset_length = 24;
327  sps->profile_idc = profile_idc;
328  sps->constraint_set_flags = constraint_set_flags;
329  sps->level_idc = level_idc;
330 
331  memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
332  memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
333  sps->scaling_matrix_present = 0;
334 
335  if (sps->profile_idc == 100 || // High profile
336  sps->profile_idc == 110 || // High10 profile
337  sps->profile_idc == 122 || // High422 profile
338  sps->profile_idc == 244 || // High444 Predictive profile
339  sps->profile_idc == 44 || // Cavlc444 profile
340  sps->profile_idc == 83 || // Scalable Constrained High profile (SVC)
341  sps->profile_idc == 86 || // Scalable High Intra profile (SVC)
342  sps->profile_idc == 118 || // Stereo High profile (MVC)
343  sps->profile_idc == 128 || // Multiview High profile (MVC)
344  sps->profile_idc == 138 || // Multiview Depth High profile (MVCD)
345  sps->profile_idc == 144) { // old High444 profile
347  if (sps->chroma_format_idc > 3) {
348  avpriv_request_sample(h->avctx, "chroma_format_idc %u",
349  sps->chroma_format_idc);
350  goto fail;
351  } else if (sps->chroma_format_idc == 3) {
353  }
354  sps->bit_depth_luma = get_ue_golomb(&h->gb) + 8;
355  sps->bit_depth_chroma = get_ue_golomb(&h->gb) + 8;
356  if (sps->bit_depth_chroma != sps->bit_depth_luma) {
358  "Different chroma and luma bit depth");
359  goto fail;
360  }
361  sps->transform_bypass = get_bits1(&h->gb);
362  decode_scaling_matrices(h, sps, NULL, 1,
363  sps->scaling_matrix4, sps->scaling_matrix8);
364  } else {
365  sps->chroma_format_idc = 1;
366  sps->bit_depth_luma = 8;
367  sps->bit_depth_chroma = 8;
368  }
369 
370  log2_max_frame_num_minus4 = get_ue_golomb(&h->gb);
371  if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||
372  log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) {
374  "log2_max_frame_num_minus4 out of range (0-12): %d\n",
375  log2_max_frame_num_minus4);
376  goto fail;
377  }
378  sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
379 
380  sps->poc_type = get_ue_golomb_31(&h->gb);
381 
382  if (sps->poc_type == 0) { // FIXME #define
383  sps->log2_max_poc_lsb = get_ue_golomb(&h->gb) + 4;
384  } else if (sps->poc_type == 1) { // FIXME #define
388  sps->poc_cycle_length = get_ue_golomb(&h->gb);
389 
390  if ((unsigned)sps->poc_cycle_length >=
393  "poc_cycle_length overflow %d\n", sps->poc_cycle_length);
394  goto fail;
395  }
396 
397  for (i = 0; i < sps->poc_cycle_length; i++)
398  sps->offset_for_ref_frame[i] = get_se_golomb(&h->gb);
399  } else if (sps->poc_type != 2) {
400  av_log(h->avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
401  goto fail;
402  }
403 
404  sps->ref_frame_count = get_ue_golomb_31(&h->gb);
405  if (sps->ref_frame_count > H264_MAX_PICTURE_COUNT - 2 ||
406  sps->ref_frame_count >= 32U) {
408  "too many reference frames %d\n", sps->ref_frame_count);
409  goto fail;
410  }
412  sps->mb_width = get_ue_golomb(&h->gb) + 1;
413  sps->mb_height = get_ue_golomb(&h->gb) + 1;
414  if ((unsigned)sps->mb_width >= INT_MAX / 16 ||
415  (unsigned)sps->mb_height >= INT_MAX / 16 ||
416  av_image_check_size(16 * sps->mb_width,
417  16 * sps->mb_height, 0, h->avctx)) {
418  av_log(h->avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
419  goto fail;
420  }
421 
422  sps->frame_mbs_only_flag = get_bits1(&h->gb);
423  if (!sps->frame_mbs_only_flag)
424  sps->mb_aff = get_bits1(&h->gb);
425  else
426  sps->mb_aff = 0;
427 
429  if (!sps->frame_mbs_only_flag && !sps->direct_8x8_inference_flag) {
431  "This stream was generated by a broken encoder, invalid 8x8 inference\n");
432  goto fail;
433  }
434 
435 #ifndef ALLOW_INTERLACE
436  if (sps->mb_aff)
438  "MBAFF support not included; enable it at compile-time.\n");
439 #endif
440  sps->crop = get_bits1(&h->gb);
441  if (sps->crop) {
442  int crop_left = get_ue_golomb(&h->gb);
443  int crop_right = get_ue_golomb(&h->gb);
444  int crop_top = get_ue_golomb(&h->gb);
445  int crop_bottom = get_ue_golomb(&h->gb);
446 
448  av_log(h->avctx, AV_LOG_DEBUG, "discarding sps cropping, original "
449  "values are l:%d r:%d t:%d b:%d\n",
450  crop_left, crop_right, crop_top, crop_bottom);
451 
452  sps->crop_left =
453  sps->crop_right =
454  sps->crop_top =
455  sps->crop_bottom = 0;
456  } else {
457  int vsub = (sps->chroma_format_idc == 1) ? 1 : 0;
458  int hsub = (sps->chroma_format_idc == 1 ||
459  sps->chroma_format_idc == 2) ? 1 : 0;
460  int step_x = 1 << hsub;
461  int step_y = (2 - sps->frame_mbs_only_flag) << vsub;
462 
463  if (crop_left & (0x1F >> (sps->bit_depth_luma > 8)) &&
464  !(h->avctx->flags & CODEC_FLAG_UNALIGNED)) {
465  crop_left &= ~(0x1F >> (sps->bit_depth_luma > 8));
467  "Reducing left cropping to %d "
468  "chroma samples to preserve alignment.\n",
469  crop_left);
470  }
471 
472  sps->crop_left = crop_left * step_x;
473  sps->crop_right = crop_right * step_x;
474  sps->crop_top = crop_top * step_y;
475  sps->crop_bottom = crop_bottom * step_y;
476  }
477  } else {
478  sps->crop_left =
479  sps->crop_right =
480  sps->crop_top =
481  sps->crop_bottom =
482  sps->crop = 0;
483  }
484 
486  if (sps->vui_parameters_present_flag) {
487  int ret = decode_vui_parameters(h, sps);
488  if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
489  goto fail;
490  }
491 
492  if (!sps->sar.den)
493  sps->sar.den = 1;
494 
495  if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
496  static const char csp[4][5] = { "Gray", "420", "422", "444" };
498  "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32"\n",
499  sps_id, sps->profile_idc, sps->level_idc,
500  sps->poc_type,
501  sps->ref_frame_count,
502  sps->mb_width, sps->mb_height,
503  sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
504  sps->direct_8x8_inference_flag ? "8B8" : "",
505  sps->crop_left, sps->crop_right,
506  sps->crop_top, sps->crop_bottom,
507  sps->vui_parameters_present_flag ? "VUI" : "",
508  csp[sps->chroma_format_idc],
510  sps->timing_info_present_flag ? sps->time_scale : 0);
511  }
512  sps->new = 1;
513 
514  av_free(h->sps_buffers[sps_id]);
515  h->sps_buffers[sps_id] = sps;
516  h->sps = *sps;
517 
518  return 0;
519 
520 fail:
521  av_free(sps);
522  return -1;
523 }
524 
525 static void build_qp_table(PPS *pps, int t, int index, const int depth)
526 {
527  int i;
528  const int max_qp = 51 + 6 * (depth - 8);
529  for (i = 0; i < max_qp + 1; i++)
530  pps->chroma_qp_table[t][i] =
531  ff_h264_chroma_qp[depth - 8][av_clip(i + index, 0, max_qp)];
532 }
533 
535 {
536  unsigned int pps_id = get_ue_golomb(&h->gb);
537  PPS *pps;
538  const int qp_bd_offset = 6 * (h->sps.bit_depth_luma - 8);
539  int bits_left;
540 
541  if (pps_id >= MAX_PPS_COUNT) {
542  av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
543  return AVERROR_INVALIDDATA;
544  } else if (h->sps.bit_depth_luma > 10) {
546  "Unimplemented luma bit depth=%d (max=10)\n",
547  h->sps.bit_depth_luma);
548  return AVERROR_PATCHWELCOME;
549  }
550 
551  pps = av_mallocz(sizeof(PPS));
552  if (!pps)
553  return AVERROR(ENOMEM);
554  pps->sps_id = get_ue_golomb_31(&h->gb);
555  if ((unsigned)pps->sps_id >= MAX_SPS_COUNT ||
556  !h->sps_buffers[pps->sps_id]) {
557  av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id);
558  goto fail;
559  }
560 
561  pps->cabac = get_bits1(&h->gb);
562  pps->pic_order_present = get_bits1(&h->gb);
563  pps->slice_group_count = get_ue_golomb(&h->gb) + 1;
564  if (pps->slice_group_count > 1) {
566  av_log(h->avctx, AV_LOG_ERROR, "FMO not supported\n");
567  switch (pps->mb_slice_group_map_type) {
568  case 0:
569 #if 0
570  | for (i = 0; i <= num_slice_groups_minus1; i++) | | |
571  | run_length[i] |1 |ue(v) |
572 #endif
573  break;
574  case 2:
575 #if 0
576  | for (i = 0; i < num_slice_groups_minus1; i++) { | | |
577  | top_left_mb[i] |1 |ue(v) |
578  | bottom_right_mb[i] |1 |ue(v) |
579  | } | | |
580 #endif
581  break;
582  case 3:
583  case 4:
584  case 5:
585 #if 0
586  | slice_group_change_direction_flag |1 |u(1) |
587  | slice_group_change_rate_minus1 |1 |ue(v) |
588 #endif
589  break;
590  case 6:
591 #if 0
592  | slice_group_id_cnt_minus1 |1 |ue(v) |
593  | for (i = 0; i <= slice_group_id_cnt_minus1; i++)| | |
594  | slice_group_id[i] |1 |u(v) |
595 #endif
596  break;
597  }
598  }
599  pps->ref_count[0] = get_ue_golomb(&h->gb) + 1;
600  pps->ref_count[1] = get_ue_golomb(&h->gb) + 1;
601  if (pps->ref_count[0] - 1 > 32 - 1 || pps->ref_count[1] - 1 > 32 - 1) {
602  av_log(h->avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
603  goto fail;
604  }
605 
606  pps->weighted_pred = get_bits1(&h->gb);
607  pps->weighted_bipred_idc = get_bits(&h->gb, 2);
608  pps->init_qp = get_se_golomb(&h->gb) + 26 + qp_bd_offset;
609  pps->init_qs = get_se_golomb(&h->gb) + 26 + qp_bd_offset;
610  pps->chroma_qp_index_offset[0] = get_se_golomb(&h->gb);
612  pps->constrained_intra_pred = get_bits1(&h->gb);
614 
615  pps->transform_8x8_mode = 0;
616  // contents of sps/pps can change even if id doesn't, so reinit
617  h->dequant_coeff_pps = -1;
618  memcpy(pps->scaling_matrix4, h->sps_buffers[pps->sps_id]->scaling_matrix4,
619  sizeof(pps->scaling_matrix4));
620  memcpy(pps->scaling_matrix8, h->sps_buffers[pps->sps_id]->scaling_matrix8,
621  sizeof(pps->scaling_matrix8));
622 
623  bits_left = bit_length - get_bits_count(&h->gb);
624  if (bits_left && (bits_left > 8 ||
625  show_bits(&h->gb, bits_left) != 1 << (bits_left - 1))) {
626  pps->transform_8x8_mode = get_bits1(&h->gb);
627  decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0,
628  pps->scaling_matrix4, pps->scaling_matrix8);
629  // second_chroma_qp_index_offset
630  pps->chroma_qp_index_offset[1] = get_se_golomb(&h->gb);
631  } else {
633  }
634 
635  build_qp_table(pps, 0, pps->chroma_qp_index_offset[0],
636  h->sps.bit_depth_luma);
637  build_qp_table(pps, 1, pps->chroma_qp_index_offset[1],
638  h->sps.bit_depth_luma);
639  if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
640  pps->chroma_qp_diff = 1;
641 
642  if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
644  "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
645  pps_id, pps->sps_id,
646  pps->cabac ? "CABAC" : "CAVLC",
647  pps->slice_group_count,
648  pps->ref_count[0], pps->ref_count[1],
649  pps->weighted_pred ? "weighted" : "",
650  pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
651  pps->deblocking_filter_parameters_present ? "LPAR" : "",
652  pps->constrained_intra_pred ? "CONSTR" : "",
653  pps->redundant_pic_cnt_present ? "REDU" : "",
654  pps->transform_8x8_mode ? "8x8DCT" : "");
655  }
656 
657  av_free(h->pps_buffers[pps_id]);
658  h->pps_buffers[pps_id] = pps;
659  return 0;
660 
661 fail:
662  av_free(pps);
663  return -1;
664 }
int chroma_format_idc
Definition: h264.h:160
#define MAX_LOG2_MAX_FRAME_NUM
Definition: h264_ps.c:37
int video_signal_type_present_flag
Definition: h264.h:185
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
GetBitContext gb
Definition: h264.h:311
int size
#define CODEC_FLAG_UNALIGNED
Allow decoders to produce frames with data planes that are not aligned to CPU requirements (e...
Definition: avcodec.h:610
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:179
#define H264_MAX_PICTURE_COUNT
Definition: h264.h:46
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
#define CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
Definition: avcodec.h:667
int weighted_bipred_idc
Definition: h264.h:227
int chroma_qp_index_offset[2]
Definition: h264.h:230
int scaling_matrix_present
Definition: h264.h:198
uint8_t scaling_matrix4[6][16]
Definition: h264.h:199
Not part of ABI.
Definition: pixfmt.h:338
#define MAX_PPS_COUNT
Definition: h264.h:50
Sequence parameter set.
Definition: h264.h:156
int bitstream_restriction_flag
Definition: h264.h:196
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:225
int num
numerator
Definition: rational.h:44
Picture parameter set.
Definition: h264.h:219
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int frame_mbs_only_flag
Definition: h264.h:173
H264Context.
Definition: h264.h:303
int chroma_qp_diff
Definition: h264.h:238
uint32_t num_units_in_tick
Definition: h264.h:192
static const AVRational pixel_aspect[17]
Definition: h264_ps.c:40
int profile_idc
Definition: h264.h:158
static const uint8_t zigzag_scan[16]
Definition: h264data.h:54
unsigned int crop_top
frame_cropping_rect_top_offset
Definition: h264.h:181
int fixed_frame_rate_flag
Definition: h264.h:194
uint8_t scaling_matrix4[6][16]
Definition: h264.h:235
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
Definition: h264.h:231
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
int full_range
Definition: h264.h:186
unsigned int crop_left
frame_cropping_rect_left_offset
Definition: h264.h:179
int offset_for_non_ref_pic
Definition: h264.h:166
int gaps_in_frame_num_allowed_flag
Definition: h264.h:170
int bit_depth_chroma
bit_depth_chroma_minus8 + 8
Definition: h264.h:210
enum AVColorPrimaries color_primaries
Definition: h264.h:188
int cabac
entropy_coding_mode_flag
Definition: h264.h:221
unsigned int crop_right
frame_cropping_rect_right_offset
Definition: h264.h:180
int transform_bypass
qpprime_y_zero_transform_bypass_flag
Definition: h264.h:161
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:194
uint8_t chroma_qp_table[2][64]
pre-scaled (with chroma_qp_index_offset) version of qp_table
Definition: h264.h:237
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
Definition: h264.h:233
static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps, uint8_t(*scaling_matrix4)[16], uint8_t(*scaling_matrix8)[64])
Definition: h264_ps.c:264
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1773
H.264 / AVC / MPEG4 part10 codec.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:555
int mb_aff
mb_adaptive_frame_field_flag
Definition: h264.h:174
enum AVColorTransferCharacteristic color_trc
Definition: h264.h:189
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
int poc_type
pic_order_cnt_type
Definition: h264.h:163
int constrained_intra_pred
constrained_intra_pred_flag
Definition: h264.h:232
int num_reorder_frames
Definition: h264.h:197
#define AVERROR(e)
Definition: error.h:43
int time_offset_length
Definition: h264.h:204
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
#define EXTENDED_SAR
Definition: h264.h:98
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
int weighted_pred
weighted_pred_flag
Definition: h264.h:226
#define MIN_LOG2_MAX_FRAME_NUM
Definition: h264_ps.c:38
int residual_color_transform_flag
residual_colour_transform_flag
Definition: h264.h:211
Libavcodec external API header.
int delta_pic_order_always_zero_flag
Definition: h264.h:165
int new
flag to keep track if the decoder context needs re-init due to changed SPS
Definition: h264.h:213
int offset_for_top_to_bottom_field
Definition: h264.h:167
#define CHROMA_QP_TABLE_END(d)
Definition: h264_ps.c:62
int crop
frame_cropping_flag
Definition: h264.h:176
static int decode_hrd_parameters(H264Context *h, SPS *sps)
Definition: h264_ps.c:108
uint8_t scaling_matrix8[6][64]
Definition: h264.h:236
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:222
int ref_frame_count
num_ref_frames
Definition: h264.h:169
int initial_cpb_removal_delay_length
initial_cpb_removal_delay_length_minus1 + 1
Definition: h264.h:206
int poc_cycle_length
num_ref_frames_in_pic_order_cnt_cycle
Definition: h264.h:168
int colour_description_present_flag
Definition: h264.h:187
AVRational sar
Definition: h264.h:184
SPS sps
current sps
Definition: h264.h:401
PPS * pps_buffers[MAX_PPS_COUNT]
Definition: h264.h:533
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:254
#define FF_ARRAY_ELEMS(a)
Definition: common.h:61
#define MAX_SPS_COUNT
Definition: h264.h:49
int init_qp
pic_init_qp_minus26 + 26
Definition: h264.h:228
int direct_8x8_inference_flag
Definition: h264.h:175
uint8_t scaling_matrix8[6][64]
Definition: h264.h:200
Not part of ABI.
Definition: pixfmt.h:316
unsigned int sps_id
Definition: h264.h:157
int dequant_coeff_pps
reinit tables when pps changes
Definition: h264.h:535
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:81
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
int pic_order_present
pic_order_present_flag
Definition: h264.h:222
SPS * sps_buffers[MAX_SPS_COUNT]
Definition: h264.h:532
short offset_for_ref_frame[256]
Definition: h264.h:195
int timing_info_present_flag
Definition: h264.h:191
NULL
Definition: eval.c:55
int vcl_hrd_parameters_present_flag
Definition: h264.h:202
AVCodecContext * avctx
Definition: h264.h:304
H264 / AVC / MPEG4 part10 codec data table
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:96
int debug
debug
Definition: avcodec.h:2362
int dpb_output_delay_length
dpb_output_delay_length_minus1 + 1
Definition: h264.h:208
#define AV_EF_EXPLODE
Definition: avcodec.h:2417
int vui_parameters_present_flag
Definition: h264.h:183
static int decode_vui_parameters(H264Context *h, SPS *sps)
Definition: h264_ps.c:133
#define QP_MAX_NUM
Definition: h264.h:105
int ff_h264_decode_seq_parameter_set(H264Context *h)
Decode SPS.
Definition: h264_ps.c:299
int constraint_set_flags
constraint_set[0-3]_flag
Definition: h264.h:212
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
int index
Definition: gxfenc.c:72
rational number numerator/denominator
Definition: rational.h:43
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
unsigned int sps_id
Definition: h264.h:220
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264.h:164
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:304
uint32_t time_scale
Definition: h264.h:193
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2363
int transform_8x8_mode
transform_8x8_mode_flag
Definition: h264.h:234
int pic_struct_present_flag
Definition: h264.h:203
int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length)
Decode PPS.
Definition: h264_ps.c:534
Not part of ABI.
Definition: pixfmt.h:356
int mb_height
pic_height_in_map_units_minus1 + 1
Definition: h264.h:172
int init_qs
pic_init_qs_minus26 + 26
Definition: h264.h:229
common internal api header.
int nal_hrd_parameters_present_flag
Definition: h264.h:201
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:162
static const uint8_t default_scaling4[2][16]
Definition: h264_ps.c:82
int den
denominator
Definition: rational.h:45
int bit_depth_luma
bit_depth_luma_minus8 + 8
Definition: h264.h:209
Definition: vf_drawbox.c:37
const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM+1]
One chroma qp table for each supported bit depth (8, 9, 10).
Definition: h264_ps.c:73
int mb_width
pic_width_in_mbs_minus1 + 1
Definition: h264.h:171
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1151
int slice_group_count
num_slice_groups_minus1 + 1
Definition: h264.h:223
int cpb_cnt
See H.264 E.1.2.
Definition: h264.h:205
static void decode_scaling_list(H264Context *h, uint8_t *factors, int size, const uint8_t *jvt_list, const uint8_t *fallback_list)
Definition: h264_ps.c:244
static const uint8_t default_scaling8[2][64]
Definition: h264_ps.c:89
int cpb_removal_delay_length
cpb_removal_delay_length_minus1 + 1
Definition: h264.h:207
unsigned int crop_bottom
frame_cropping_rect_bottom_offset
Definition: h264.h:182
exp golomb vlc stuff
int level_idc
Definition: h264.h:159
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
static void build_qp_table(PPS *pps, int t, int index, const int depth)
Definition: h264_ps.c:525
int mb_slice_group_map_type
Definition: h264.h:224
enum AVColorSpace colorspace
Definition: h264.h:190