You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

864 lines
32KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG-4 part10 parameter set decoding.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include <inttypes.h>
  27. #include "libavutil/imgutils.h"
  28. #include "internal.h"
  29. #include "mathops.h"
  30. #include "avcodec.h"
  31. #include "h264data.h"
  32. #include "h264_ps.h"
  33. #include "golomb.h"
  34. #define MIN_LOG2_MAX_FRAME_NUM 4
  35. #define EXTENDED_SAR 255
  36. static const uint8_t default_scaling4[2][16] = {
  37. { 6, 13, 20, 28, 13, 20, 28, 32,
  38. 20, 28, 32, 37, 28, 32, 37, 42 },
  39. { 10, 14, 20, 24, 14, 20, 24, 27,
  40. 20, 24, 27, 30, 24, 27, 30, 34 }
  41. };
  42. static const uint8_t default_scaling8[2][64] = {
  43. { 6, 10, 13, 16, 18, 23, 25, 27,
  44. 10, 11, 16, 18, 23, 25, 27, 29,
  45. 13, 16, 18, 23, 25, 27, 29, 31,
  46. 16, 18, 23, 25, 27, 29, 31, 33,
  47. 18, 23, 25, 27, 29, 31, 33, 36,
  48. 23, 25, 27, 29, 31, 33, 36, 38,
  49. 25, 27, 29, 31, 33, 36, 38, 40,
  50. 27, 29, 31, 33, 36, 38, 40, 42 },
  51. { 9, 13, 15, 17, 19, 21, 22, 24,
  52. 13, 13, 17, 19, 21, 22, 24, 25,
  53. 15, 17, 19, 21, 22, 24, 25, 27,
  54. 17, 19, 21, 22, 24, 25, 27, 28,
  55. 19, 21, 22, 24, 25, 27, 28, 30,
  56. 21, 22, 24, 25, 27, 28, 30, 32,
  57. 22, 24, 25, 27, 28, 30, 32, 33,
  58. 24, 25, 27, 28, 30, 32, 33, 35 }
  59. };
  60. /* maximum number of MBs in the DPB for a given level */
  61. static const int level_max_dpb_mbs[][2] = {
  62. { 10, 396 },
  63. { 11, 900 },
  64. { 12, 2376 },
  65. { 13, 2376 },
  66. { 20, 2376 },
  67. { 21, 4752 },
  68. { 22, 8100 },
  69. { 30, 8100 },
  70. { 31, 18000 },
  71. { 32, 20480 },
  72. { 40, 32768 },
  73. { 41, 32768 },
  74. { 42, 34816 },
  75. { 50, 110400 },
  76. { 51, 184320 },
  77. { 52, 184320 },
  78. };
  79. static void remove_pps(H264ParamSets *s, int id)
  80. {
  81. av_buffer_unref(&s->pps_list[id]);
  82. }
  83. static void remove_sps(H264ParamSets *s, int id)
  84. {
  85. #if 0
  86. int i;
  87. if (s->sps_list[id]) {
  88. /* drop all PPS that depend on this SPS */
  89. for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
  90. if (s->pps_list[i] && ((PPS*)s->pps_list[i]->data)->sps_id == id)
  91. remove_pps(s, i);
  92. }
  93. #endif
  94. av_buffer_unref(&s->sps_list[id]);
  95. }
  96. static inline int decode_hrd_parameters(GetBitContext *gb, AVCodecContext *avctx,
  97. SPS *sps)
  98. {
  99. int cpb_count, i;
  100. cpb_count = get_ue_golomb_31(gb) + 1;
  101. if (cpb_count > 32U) {
  102. av_log(avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
  103. return AVERROR_INVALIDDATA;
  104. }
  105. get_bits(gb, 4); /* bit_rate_scale */
  106. get_bits(gb, 4); /* cpb_size_scale */
  107. for (i = 0; i < cpb_count; i++) {
  108. get_ue_golomb_long(gb); /* bit_rate_value_minus1 */
  109. get_ue_golomb_long(gb); /* cpb_size_value_minus1 */
  110. get_bits1(gb); /* cbr_flag */
  111. }
  112. sps->initial_cpb_removal_delay_length = get_bits(gb, 5) + 1;
  113. sps->cpb_removal_delay_length = get_bits(gb, 5) + 1;
  114. sps->dpb_output_delay_length = get_bits(gb, 5) + 1;
  115. sps->time_offset_length = get_bits(gb, 5);
  116. sps->cpb_cnt = cpb_count;
  117. return 0;
  118. }
  119. static inline int decode_vui_parameters(GetBitContext *gb, AVCodecContext *avctx,
  120. SPS *sps)
  121. {
  122. int aspect_ratio_info_present_flag;
  123. unsigned int aspect_ratio_idc;
  124. aspect_ratio_info_present_flag = get_bits1(gb);
  125. if (aspect_ratio_info_present_flag) {
  126. aspect_ratio_idc = get_bits(gb, 8);
  127. if (aspect_ratio_idc == EXTENDED_SAR) {
  128. sps->sar.num = get_bits(gb, 16);
  129. sps->sar.den = get_bits(gb, 16);
  130. } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(ff_h264_pixel_aspect)) {
  131. sps->sar = ff_h264_pixel_aspect[aspect_ratio_idc];
  132. } else {
  133. av_log(avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. } else {
  137. sps->sar.num =
  138. sps->sar.den = 0;
  139. }
  140. if (get_bits1(gb)) /* overscan_info_present_flag */
  141. get_bits1(gb); /* overscan_appropriate_flag */
  142. sps->video_signal_type_present_flag = get_bits1(gb);
  143. if (sps->video_signal_type_present_flag) {
  144. get_bits(gb, 3); /* video_format */
  145. sps->full_range = get_bits1(gb); /* video_full_range_flag */
  146. sps->colour_description_present_flag = get_bits1(gb);
  147. if (sps->colour_description_present_flag) {
  148. sps->color_primaries = get_bits(gb, 8); /* colour_primaries */
  149. sps->color_trc = get_bits(gb, 8); /* transfer_characteristics */
  150. sps->colorspace = get_bits(gb, 8); /* matrix_coefficients */
  151. // Set invalid values to "unspecified"
  152. if (!av_color_primaries_name(sps->color_primaries))
  153. sps->color_primaries = AVCOL_PRI_UNSPECIFIED;
  154. if (!av_color_transfer_name(sps->color_trc))
  155. sps->color_trc = AVCOL_TRC_UNSPECIFIED;
  156. if (!av_color_space_name(sps->colorspace))
  157. sps->colorspace = AVCOL_SPC_UNSPECIFIED;
  158. }
  159. }
  160. /* chroma_location_info_present_flag */
  161. if (get_bits1(gb)) {
  162. /* chroma_sample_location_type_top_field */
  163. avctx->chroma_sample_location = get_ue_golomb(gb) + 1;
  164. get_ue_golomb(gb); /* chroma_sample_location_type_bottom_field */
  165. }
  166. if (show_bits1(gb) && get_bits_left(gb) < 10) {
  167. av_log(avctx, AV_LOG_WARNING, "Truncated VUI\n");
  168. return 0;
  169. }
  170. sps->timing_info_present_flag = get_bits1(gb);
  171. if (sps->timing_info_present_flag) {
  172. unsigned num_units_in_tick = get_bits_long(gb, 32);
  173. unsigned time_scale = get_bits_long(gb, 32);
  174. if (!num_units_in_tick || !time_scale) {
  175. av_log(avctx, AV_LOG_ERROR,
  176. "time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n",
  177. time_scale, num_units_in_tick);
  178. sps->timing_info_present_flag = 0;
  179. } else {
  180. sps->num_units_in_tick = num_units_in_tick;
  181. sps->time_scale = time_scale;
  182. }
  183. sps->fixed_frame_rate_flag = get_bits1(gb);
  184. }
  185. sps->nal_hrd_parameters_present_flag = get_bits1(gb);
  186. if (sps->nal_hrd_parameters_present_flag)
  187. if (decode_hrd_parameters(gb, avctx, sps) < 0)
  188. return AVERROR_INVALIDDATA;
  189. sps->vcl_hrd_parameters_present_flag = get_bits1(gb);
  190. if (sps->vcl_hrd_parameters_present_flag)
  191. if (decode_hrd_parameters(gb, avctx, sps) < 0)
  192. return AVERROR_INVALIDDATA;
  193. if (sps->nal_hrd_parameters_present_flag ||
  194. sps->vcl_hrd_parameters_present_flag)
  195. get_bits1(gb); /* low_delay_hrd_flag */
  196. sps->pic_struct_present_flag = get_bits1(gb);
  197. if (!get_bits_left(gb))
  198. return 0;
  199. sps->bitstream_restriction_flag = get_bits1(gb);
  200. if (sps->bitstream_restriction_flag) {
  201. get_bits1(gb); /* motion_vectors_over_pic_boundaries_flag */
  202. get_ue_golomb(gb); /* max_bytes_per_pic_denom */
  203. get_ue_golomb(gb); /* max_bits_per_mb_denom */
  204. get_ue_golomb(gb); /* log2_max_mv_length_horizontal */
  205. get_ue_golomb(gb); /* log2_max_mv_length_vertical */
  206. sps->num_reorder_frames = get_ue_golomb(gb);
  207. get_ue_golomb(gb); /*max_dec_frame_buffering*/
  208. if (get_bits_left(gb) < 0) {
  209. sps->num_reorder_frames = 0;
  210. sps->bitstream_restriction_flag = 0;
  211. }
  212. if (sps->num_reorder_frames > 16U
  213. /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
  214. av_log(avctx, AV_LOG_ERROR,
  215. "Clipping illegal num_reorder_frames %d\n",
  216. sps->num_reorder_frames);
  217. sps->num_reorder_frames = 16;
  218. return AVERROR_INVALIDDATA;
  219. }
  220. }
  221. return 0;
  222. }
  223. static int decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size,
  224. const uint8_t *jvt_list,
  225. const uint8_t *fallback_list)
  226. {
  227. int i, last = 8, next = 8;
  228. const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct;
  229. if (!get_bits1(gb)) /* matrix not written, we use the predicted one */
  230. memcpy(factors, fallback_list, size * sizeof(uint8_t));
  231. else
  232. for (i = 0; i < size; i++) {
  233. if (next) {
  234. int v = get_se_golomb(gb);
  235. if (v < -128 || v > 127) {
  236. av_log(NULL, AV_LOG_ERROR, "delta scale %d is invalid\n", v);
  237. return AVERROR_INVALIDDATA;
  238. }
  239. next = (last + v) & 0xff;
  240. }
  241. if (!i && !next) { /* matrix not written, we use the preset one */
  242. memcpy(factors, jvt_list, size * sizeof(uint8_t));
  243. break;
  244. }
  245. last = factors[scan[i]] = next ? next : last;
  246. }
  247. return 0;
  248. }
  249. /* returns non zero if the provided SPS scaling matrix has been filled */
  250. static int decode_scaling_matrices(GetBitContext *gb, const SPS *sps,
  251. const PPS *pps, int is_sps,
  252. uint8_t(*scaling_matrix4)[16],
  253. uint8_t(*scaling_matrix8)[64])
  254. {
  255. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  256. const uint8_t *fallback[4] = {
  257. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  258. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  259. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  260. fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
  261. };
  262. int ret = 0;
  263. if (get_bits1(gb)) {
  264. ret |= decode_scaling_list(gb, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]); // Intra, Y
  265. ret |= decode_scaling_list(gb, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr
  266. ret |= decode_scaling_list(gb, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb
  267. ret |= decode_scaling_list(gb, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]); // Inter, Y
  268. ret |= decode_scaling_list(gb, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr
  269. ret |= decode_scaling_list(gb, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb
  270. if (is_sps || pps->transform_8x8_mode) {
  271. ret |= decode_scaling_list(gb, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y
  272. ret |= decode_scaling_list(gb, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y
  273. if (sps->chroma_format_idc == 3) {
  274. ret |= decode_scaling_list(gb, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr
  275. ret |= decode_scaling_list(gb, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr
  276. ret |= decode_scaling_list(gb, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb
  277. ret |= decode_scaling_list(gb, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb
  278. }
  279. }
  280. if (!ret)
  281. ret = is_sps;
  282. }
  283. return ret;
  284. }
  285. void ff_h264_ps_uninit(H264ParamSets *ps)
  286. {
  287. int i;
  288. for (i = 0; i < MAX_SPS_COUNT; i++)
  289. av_buffer_unref(&ps->sps_list[i]);
  290. for (i = 0; i < MAX_PPS_COUNT; i++)
  291. av_buffer_unref(&ps->pps_list[i]);
  292. av_buffer_unref(&ps->sps_ref);
  293. av_buffer_unref(&ps->pps_ref);
  294. ps->pps = NULL;
  295. ps->sps = NULL;
  296. }
  297. int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
  298. H264ParamSets *ps, int ignore_truncation)
  299. {
  300. AVBufferRef *sps_buf;
  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. int ret;
  306. sps_buf = av_buffer_allocz(sizeof(*sps));
  307. if (!sps_buf)
  308. return AVERROR(ENOMEM);
  309. sps = (SPS*)sps_buf->data;
  310. sps->data_size = gb->buffer_end - gb->buffer;
  311. if (sps->data_size > sizeof(sps->data)) {
  312. av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n");
  313. sps->data_size = sizeof(sps->data);
  314. }
  315. memcpy(sps->data, gb->buffer, sps->data_size);
  316. profile_idc = get_bits(gb, 8);
  317. constraint_set_flags |= get_bits1(gb) << 0; // constraint_set0_flag
  318. constraint_set_flags |= get_bits1(gb) << 1; // constraint_set1_flag
  319. constraint_set_flags |= get_bits1(gb) << 2; // constraint_set2_flag
  320. constraint_set_flags |= get_bits1(gb) << 3; // constraint_set3_flag
  321. constraint_set_flags |= get_bits1(gb) << 4; // constraint_set4_flag
  322. constraint_set_flags |= get_bits1(gb) << 5; // constraint_set5_flag
  323. skip_bits(gb, 2); // reserved_zero_2bits
  324. level_idc = get_bits(gb, 8);
  325. sps_id = get_ue_golomb_31(gb);
  326. if (sps_id >= MAX_SPS_COUNT) {
  327. av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
  328. goto fail;
  329. }
  330. sps->sps_id = sps_id;
  331. sps->time_offset_length = 24;
  332. sps->profile_idc = profile_idc;
  333. sps->constraint_set_flags = constraint_set_flags;
  334. sps->level_idc = level_idc;
  335. sps->full_range = -1;
  336. memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
  337. memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
  338. sps->scaling_matrix_present = 0;
  339. sps->colorspace = 2; //AVCOL_SPC_UNSPECIFIED
  340. if (sps->profile_idc == 100 || // High profile
  341. sps->profile_idc == 110 || // High10 profile
  342. sps->profile_idc == 122 || // High422 profile
  343. sps->profile_idc == 244 || // High444 Predictive profile
  344. sps->profile_idc == 44 || // Cavlc444 profile
  345. sps->profile_idc == 83 || // Scalable Constrained High profile (SVC)
  346. sps->profile_idc == 86 || // Scalable High Intra profile (SVC)
  347. sps->profile_idc == 118 || // Stereo High profile (MVC)
  348. sps->profile_idc == 128 || // Multiview High profile (MVC)
  349. sps->profile_idc == 138 || // Multiview Depth High profile (MVCD)
  350. sps->profile_idc == 144) { // old High444 profile
  351. sps->chroma_format_idc = get_ue_golomb_31(gb);
  352. if (sps->chroma_format_idc > 3U) {
  353. avpriv_request_sample(avctx, "chroma_format_idc %u",
  354. sps->chroma_format_idc);
  355. goto fail;
  356. } else if (sps->chroma_format_idc == 3) {
  357. sps->residual_color_transform_flag = get_bits1(gb);
  358. if (sps->residual_color_transform_flag) {
  359. av_log(avctx, AV_LOG_ERROR, "separate color planes are not supported\n");
  360. goto fail;
  361. }
  362. }
  363. sps->bit_depth_luma = get_ue_golomb(gb) + 8;
  364. sps->bit_depth_chroma = get_ue_golomb(gb) + 8;
  365. if (sps->bit_depth_chroma != sps->bit_depth_luma) {
  366. avpriv_request_sample(avctx,
  367. "Different chroma and luma bit depth");
  368. goto fail;
  369. }
  370. if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
  371. sps->bit_depth_chroma < 8 || sps->bit_depth_chroma > 14) {
  372. av_log(avctx, AV_LOG_ERROR, "illegal bit depth value (%d, %d)\n",
  373. sps->bit_depth_luma, sps->bit_depth_chroma);
  374. goto fail;
  375. }
  376. sps->transform_bypass = get_bits1(gb);
  377. ret = decode_scaling_matrices(gb, sps, NULL, 1,
  378. sps->scaling_matrix4, sps->scaling_matrix8);
  379. if (ret < 0)
  380. goto fail;
  381. sps->scaling_matrix_present |= ret;
  382. } else {
  383. sps->chroma_format_idc = 1;
  384. sps->bit_depth_luma = 8;
  385. sps->bit_depth_chroma = 8;
  386. }
  387. log2_max_frame_num_minus4 = get_ue_golomb(gb);
  388. if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||
  389. log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) {
  390. av_log(avctx, AV_LOG_ERROR,
  391. "log2_max_frame_num_minus4 out of range (0-12): %d\n",
  392. log2_max_frame_num_minus4);
  393. goto fail;
  394. }
  395. sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
  396. sps->poc_type = get_ue_golomb_31(gb);
  397. if (sps->poc_type == 0) { // FIXME #define
  398. unsigned t = get_ue_golomb(gb);
  399. if (t>12) {
  400. av_log(avctx, AV_LOG_ERROR, "log2_max_poc_lsb (%d) is out of range\n", t);
  401. goto fail;
  402. }
  403. sps->log2_max_poc_lsb = t + 4;
  404. } else if (sps->poc_type == 1) { // FIXME #define
  405. sps->delta_pic_order_always_zero_flag = get_bits1(gb);
  406. sps->offset_for_non_ref_pic = get_se_golomb_long(gb);
  407. sps->offset_for_top_to_bottom_field = get_se_golomb_long(gb);
  408. sps->poc_cycle_length = get_ue_golomb(gb);
  409. if ((unsigned)sps->poc_cycle_length >=
  410. FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) {
  411. av_log(avctx, AV_LOG_ERROR,
  412. "poc_cycle_length overflow %d\n", sps->poc_cycle_length);
  413. goto fail;
  414. }
  415. for (i = 0; i < sps->poc_cycle_length; i++)
  416. sps->offset_for_ref_frame[i] = get_se_golomb_long(gb);
  417. } else if (sps->poc_type != 2) {
  418. av_log(avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  419. goto fail;
  420. }
  421. sps->ref_frame_count = get_ue_golomb_31(gb);
  422. if (avctx->codec_tag == MKTAG('S', 'M', 'V', '2'))
  423. sps->ref_frame_count = FFMAX(2, sps->ref_frame_count);
  424. if (sps->ref_frame_count > MAX_DELAYED_PIC_COUNT) {
  425. av_log(avctx, AV_LOG_ERROR,
  426. "too many reference frames %d\n", sps->ref_frame_count);
  427. goto fail;
  428. }
  429. sps->gaps_in_frame_num_allowed_flag = get_bits1(gb);
  430. sps->mb_width = get_ue_golomb(gb) + 1;
  431. sps->mb_height = get_ue_golomb(gb) + 1;
  432. sps->frame_mbs_only_flag = get_bits1(gb);
  433. if (sps->mb_height >= INT_MAX / 2U) {
  434. av_log(avctx, AV_LOG_ERROR, "height overflow\n");
  435. goto fail;
  436. }
  437. sps->mb_height *= 2 - sps->frame_mbs_only_flag;
  438. if (!sps->frame_mbs_only_flag)
  439. sps->mb_aff = get_bits1(gb);
  440. else
  441. sps->mb_aff = 0;
  442. if ((unsigned)sps->mb_width >= INT_MAX / 16 ||
  443. (unsigned)sps->mb_height >= INT_MAX / 16 ||
  444. av_image_check_size(16 * sps->mb_width,
  445. 16 * sps->mb_height, 0, avctx)) {
  446. av_log(avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  447. goto fail;
  448. }
  449. sps->direct_8x8_inference_flag = get_bits1(gb);
  450. #ifndef ALLOW_INTERLACE
  451. if (sps->mb_aff)
  452. av_log(avctx, AV_LOG_ERROR,
  453. "MBAFF support not included; enable it at compile-time.\n");
  454. #endif
  455. sps->crop = get_bits1(gb);
  456. if (sps->crop) {
  457. unsigned int crop_left = get_ue_golomb(gb);
  458. unsigned int crop_right = get_ue_golomb(gb);
  459. unsigned int crop_top = get_ue_golomb(gb);
  460. unsigned int crop_bottom = get_ue_golomb(gb);
  461. int width = 16 * sps->mb_width;
  462. int height = 16 * sps->mb_height;
  463. if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
  464. av_log(avctx, AV_LOG_DEBUG, "discarding sps cropping, original "
  465. "values are l:%d r:%d t:%d b:%d\n",
  466. crop_left, crop_right, crop_top, crop_bottom);
  467. sps->crop_left =
  468. sps->crop_right =
  469. sps->crop_top =
  470. sps->crop_bottom = 0;
  471. } else {
  472. int vsub = (sps->chroma_format_idc == 1) ? 1 : 0;
  473. int hsub = (sps->chroma_format_idc == 1 ||
  474. sps->chroma_format_idc == 2) ? 1 : 0;
  475. int step_x = 1 << hsub;
  476. int step_y = (2 - sps->frame_mbs_only_flag) << vsub;
  477. if (crop_left > (unsigned)INT_MAX / 4 / step_x ||
  478. crop_right > (unsigned)INT_MAX / 4 / step_x ||
  479. crop_top > (unsigned)INT_MAX / 4 / step_y ||
  480. crop_bottom> (unsigned)INT_MAX / 4 / step_y ||
  481. (crop_left + crop_right ) * step_x >= width ||
  482. (crop_top + crop_bottom) * step_y >= height
  483. ) {
  484. av_log(avctx, AV_LOG_ERROR, "crop values invalid %d %d %d %d / %d %d\n", crop_left, crop_right, crop_top, crop_bottom, width, height);
  485. goto fail;
  486. }
  487. sps->crop_left = crop_left * step_x;
  488. sps->crop_right = crop_right * step_x;
  489. sps->crop_top = crop_top * step_y;
  490. sps->crop_bottom = crop_bottom * step_y;
  491. }
  492. } else {
  493. sps->crop_left =
  494. sps->crop_right =
  495. sps->crop_top =
  496. sps->crop_bottom =
  497. sps->crop = 0;
  498. }
  499. sps->vui_parameters_present_flag = get_bits1(gb);
  500. if (sps->vui_parameters_present_flag) {
  501. int ret = decode_vui_parameters(gb, avctx, sps);
  502. if (ret < 0)
  503. goto fail;
  504. }
  505. if (get_bits_left(gb) < 0) {
  506. av_log(avctx, ignore_truncation ? AV_LOG_WARNING : AV_LOG_ERROR,
  507. "Overread %s by %d bits\n", sps->vui_parameters_present_flag ? "VUI" : "SPS", -get_bits_left(gb));
  508. if (!ignore_truncation)
  509. goto fail;
  510. }
  511. /* if the maximum delay is not stored in the SPS, derive it based on the
  512. * level */
  513. if (!sps->bitstream_restriction_flag &&
  514. (sps->ref_frame_count || avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT)) {
  515. sps->num_reorder_frames = MAX_DELAYED_PIC_COUNT - 1;
  516. for (i = 0; i < FF_ARRAY_ELEMS(level_max_dpb_mbs); i++) {
  517. if (level_max_dpb_mbs[i][0] == sps->level_idc) {
  518. sps->num_reorder_frames = FFMIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height),
  519. sps->num_reorder_frames);
  520. break;
  521. }
  522. }
  523. }
  524. if (!sps->sar.den)
  525. sps->sar.den = 1;
  526. if (avctx->debug & FF_DEBUG_PICT_INFO) {
  527. static const char csp[4][5] = { "Gray", "420", "422", "444" };
  528. av_log(avctx, AV_LOG_DEBUG,
  529. "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32" b%d reo:%d\n",
  530. sps_id, sps->profile_idc, sps->level_idc,
  531. sps->poc_type,
  532. sps->ref_frame_count,
  533. sps->mb_width, sps->mb_height,
  534. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  535. sps->direct_8x8_inference_flag ? "8B8" : "",
  536. sps->crop_left, sps->crop_right,
  537. sps->crop_top, sps->crop_bottom,
  538. sps->vui_parameters_present_flag ? "VUI" : "",
  539. csp[sps->chroma_format_idc],
  540. sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
  541. sps->timing_info_present_flag ? sps->time_scale : 0,
  542. sps->bit_depth_luma,
  543. sps->bitstream_restriction_flag ? sps->num_reorder_frames : -1
  544. );
  545. }
  546. /* check if this is a repeat of an already parsed SPS, then keep the
  547. * original one.
  548. * otherwise drop all PPSes that depend on it */
  549. if (ps->sps_list[sps_id] &&
  550. !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
  551. av_buffer_unref(&sps_buf);
  552. } else {
  553. remove_sps(ps, sps_id);
  554. ps->sps_list[sps_id] = sps_buf;
  555. }
  556. return 0;
  557. fail:
  558. av_buffer_unref(&sps_buf);
  559. return AVERROR_INVALIDDATA;
  560. }
  561. static void init_dequant8_coeff_table(PPS *pps, const SPS *sps)
  562. {
  563. int i, j, q, x;
  564. const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8);
  565. for (i = 0; i < 6; i++) {
  566. pps->dequant8_coeff[i] = pps->dequant8_buffer[i];
  567. for (j = 0; j < i; j++)
  568. if (!memcmp(pps->scaling_matrix8[j], pps->scaling_matrix8[i],
  569. 64 * sizeof(uint8_t))) {
  570. pps->dequant8_coeff[i] = pps->dequant8_buffer[j];
  571. break;
  572. }
  573. if (j < i)
  574. continue;
  575. for (q = 0; q < max_qp + 1; q++) {
  576. int shift = ff_h264_quant_div6[q];
  577. int idx = ff_h264_quant_rem6[q];
  578. for (x = 0; x < 64; x++)
  579. pps->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
  580. ((uint32_t)ff_h264_dequant8_coeff_init[idx][ff_h264_dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
  581. pps->scaling_matrix8[i][x]) << shift;
  582. }
  583. }
  584. }
  585. static void init_dequant4_coeff_table(PPS *pps, const SPS *sps)
  586. {
  587. int i, j, q, x;
  588. const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8);
  589. for (i = 0; i < 6; i++) {
  590. pps->dequant4_coeff[i] = pps->dequant4_buffer[i];
  591. for (j = 0; j < i; j++)
  592. if (!memcmp(pps->scaling_matrix4[j], pps->scaling_matrix4[i],
  593. 16 * sizeof(uint8_t))) {
  594. pps->dequant4_coeff[i] = pps->dequant4_buffer[j];
  595. break;
  596. }
  597. if (j < i)
  598. continue;
  599. for (q = 0; q < max_qp + 1; q++) {
  600. int shift = ff_h264_quant_div6[q] + 2;
  601. int idx = ff_h264_quant_rem6[q];
  602. for (x = 0; x < 16; x++)
  603. pps->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
  604. ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
  605. pps->scaling_matrix4[i][x]) << shift;
  606. }
  607. }
  608. }
  609. static void init_dequant_tables(PPS *pps, const SPS *sps)
  610. {
  611. int i, x;
  612. init_dequant4_coeff_table(pps, sps);
  613. memset(pps->dequant8_coeff, 0, sizeof(pps->dequant8_coeff));
  614. if (pps->transform_8x8_mode)
  615. init_dequant8_coeff_table(pps, sps);
  616. if (sps->transform_bypass) {
  617. for (i = 0; i < 6; i++)
  618. for (x = 0; x < 16; x++)
  619. pps->dequant4_coeff[i][0][x] = 1 << 6;
  620. if (pps->transform_8x8_mode)
  621. for (i = 0; i < 6; i++)
  622. for (x = 0; x < 64; x++)
  623. pps->dequant8_coeff[i][0][x] = 1 << 6;
  624. }
  625. }
  626. static void build_qp_table(PPS *pps, int t, int index, const int depth)
  627. {
  628. int i;
  629. const int max_qp = 51 + 6 * (depth - 8);
  630. for (i = 0; i < max_qp + 1; i++)
  631. pps->chroma_qp_table[t][i] =
  632. ff_h264_chroma_qp[depth - 8][av_clip(i + index, 0, max_qp)];
  633. }
  634. static int more_rbsp_data_in_pps(const SPS *sps, void *logctx)
  635. {
  636. int profile_idc = sps->profile_idc;
  637. if ((profile_idc == 66 || profile_idc == 77 ||
  638. profile_idc == 88) && (sps->constraint_set_flags & 7)) {
  639. av_log(logctx, AV_LOG_VERBOSE,
  640. "Current profile doesn't provide more RBSP data in PPS, skipping\n");
  641. return 0;
  642. }
  643. return 1;
  644. }
  645. int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
  646. H264ParamSets *ps, int bit_length)
  647. {
  648. AVBufferRef *pps_buf;
  649. const SPS *sps;
  650. unsigned int pps_id = get_ue_golomb(gb);
  651. PPS *pps;
  652. int qp_bd_offset;
  653. int bits_left;
  654. int ret;
  655. if (pps_id >= MAX_PPS_COUNT) {
  656. av_log(avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
  657. return AVERROR_INVALIDDATA;
  658. }
  659. pps_buf = av_buffer_allocz(sizeof(*pps));
  660. if (!pps_buf)
  661. return AVERROR(ENOMEM);
  662. pps = (PPS*)pps_buf->data;
  663. pps->data_size = gb->buffer_end - gb->buffer;
  664. if (pps->data_size > sizeof(pps->data)) {
  665. av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS "
  666. "(%"SIZE_SPECIFIER" > %"SIZE_SPECIFIER")\n",
  667. pps->data_size, sizeof(pps->data));
  668. pps->data_size = sizeof(pps->data);
  669. }
  670. memcpy(pps->data, gb->buffer, pps->data_size);
  671. pps->sps_id = get_ue_golomb_31(gb);
  672. if ((unsigned)pps->sps_id >= MAX_SPS_COUNT ||
  673. !ps->sps_list[pps->sps_id]) {
  674. av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id);
  675. ret = AVERROR_INVALIDDATA;
  676. goto fail;
  677. }
  678. sps = (const SPS*)ps->sps_list[pps->sps_id]->data;
  679. if (sps->bit_depth_luma > 14) {
  680. av_log(avctx, AV_LOG_ERROR,
  681. "Invalid luma bit depth=%d\n",
  682. sps->bit_depth_luma);
  683. ret = AVERROR_INVALIDDATA;
  684. goto fail;
  685. } else if (sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13) {
  686. avpriv_report_missing_feature(avctx,
  687. "Unimplemented luma bit depth=%d",
  688. sps->bit_depth_luma);
  689. ret = AVERROR_PATCHWELCOME;
  690. goto fail;
  691. }
  692. pps->cabac = get_bits1(gb);
  693. pps->pic_order_present = get_bits1(gb);
  694. pps->slice_group_count = get_ue_golomb(gb) + 1;
  695. if (pps->slice_group_count > 1) {
  696. pps->mb_slice_group_map_type = get_ue_golomb(gb);
  697. av_log(avctx, AV_LOG_ERROR, "FMO not supported\n");
  698. }
  699. pps->ref_count[0] = get_ue_golomb(gb) + 1;
  700. pps->ref_count[1] = get_ue_golomb(gb) + 1;
  701. if (pps->ref_count[0] - 1 > 32 - 1 || pps->ref_count[1] - 1 > 32 - 1) {
  702. av_log(avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  703. ret = AVERROR_INVALIDDATA;
  704. goto fail;
  705. }
  706. qp_bd_offset = 6 * (sps->bit_depth_luma - 8);
  707. pps->weighted_pred = get_bits1(gb);
  708. pps->weighted_bipred_idc = get_bits(gb, 2);
  709. pps->init_qp = get_se_golomb(gb) + 26U + qp_bd_offset;
  710. pps->init_qs = get_se_golomb(gb) + 26U + qp_bd_offset;
  711. pps->chroma_qp_index_offset[0] = get_se_golomb(gb);
  712. if (pps->chroma_qp_index_offset[0] < -12 || pps->chroma_qp_index_offset[0] > 12) {
  713. ret = AVERROR_INVALIDDATA;
  714. goto fail;
  715. }
  716. pps->deblocking_filter_parameters_present = get_bits1(gb);
  717. pps->constrained_intra_pred = get_bits1(gb);
  718. pps->redundant_pic_cnt_present = get_bits1(gb);
  719. pps->transform_8x8_mode = 0;
  720. memcpy(pps->scaling_matrix4, sps->scaling_matrix4,
  721. sizeof(pps->scaling_matrix4));
  722. memcpy(pps->scaling_matrix8, sps->scaling_matrix8,
  723. sizeof(pps->scaling_matrix8));
  724. bits_left = bit_length - get_bits_count(gb);
  725. if (bits_left > 0 && more_rbsp_data_in_pps(sps, avctx)) {
  726. pps->transform_8x8_mode = get_bits1(gb);
  727. ret = decode_scaling_matrices(gb, sps, pps, 0,
  728. pps->scaling_matrix4, pps->scaling_matrix8);
  729. if (ret < 0)
  730. goto fail;
  731. // second_chroma_qp_index_offset
  732. pps->chroma_qp_index_offset[1] = get_se_golomb(gb);
  733. if (pps->chroma_qp_index_offset[1] < -12 || pps->chroma_qp_index_offset[1] > 12) {
  734. ret = AVERROR_INVALIDDATA;
  735. goto fail;
  736. }
  737. } else {
  738. pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0];
  739. }
  740. build_qp_table(pps, 0, pps->chroma_qp_index_offset[0],
  741. sps->bit_depth_luma);
  742. build_qp_table(pps, 1, pps->chroma_qp_index_offset[1],
  743. sps->bit_depth_luma);
  744. init_dequant_tables(pps, sps);
  745. if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
  746. pps->chroma_qp_diff = 1;
  747. if (avctx->debug & FF_DEBUG_PICT_INFO) {
  748. av_log(avctx, AV_LOG_DEBUG,
  749. "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
  750. pps_id, pps->sps_id,
  751. pps->cabac ? "CABAC" : "CAVLC",
  752. pps->slice_group_count,
  753. pps->ref_count[0], pps->ref_count[1],
  754. pps->weighted_pred ? "weighted" : "",
  755. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
  756. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  757. pps->constrained_intra_pred ? "CONSTR" : "",
  758. pps->redundant_pic_cnt_present ? "REDU" : "",
  759. pps->transform_8x8_mode ? "8x8DCT" : "");
  760. }
  761. remove_pps(ps, pps_id);
  762. ps->pps_list[pps_id] = pps_buf;
  763. return 0;
  764. fail:
  765. av_buffer_unref(&pps_buf);
  766. return ret;
  767. }