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.

781 lines
29KB

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