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.

809 lines
30KB

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