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.

811 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 / 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 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. if (sps->color_primaries >= AVCOL_PRI_NB)
  174. sps->color_primaries = AVCOL_PRI_UNSPECIFIED;
  175. if (sps->color_trc >= AVCOL_TRC_NB)
  176. sps->color_trc = AVCOL_TRC_UNSPECIFIED;
  177. if (sps->colorspace >= AVCOL_SPC_NB)
  178. sps->colorspace = AVCOL_SPC_UNSPECIFIED;
  179. }
  180. }
  181. /* chroma_location_info_present_flag */
  182. if (get_bits1(gb)) {
  183. /* chroma_sample_location_type_top_field */
  184. avctx->chroma_sample_location = get_ue_golomb(gb) + 1;
  185. get_ue_golomb(gb); /* chroma_sample_location_type_bottom_field */
  186. }
  187. sps->timing_info_present_flag = get_bits1(gb);
  188. if (sps->timing_info_present_flag) {
  189. sps->num_units_in_tick = get_bits_long(gb, 32);
  190. sps->time_scale = get_bits_long(gb, 32);
  191. if (!sps->num_units_in_tick || !sps->time_scale) {
  192. av_log(avctx, AV_LOG_ERROR,
  193. "time_scale/num_units_in_tick invalid or unsupported (%"PRIu32"/%"PRIu32")\n",
  194. sps->time_scale, sps->num_units_in_tick);
  195. return AVERROR_INVALIDDATA;
  196. }
  197. sps->fixed_frame_rate_flag = get_bits1(gb);
  198. }
  199. sps->nal_hrd_parameters_present_flag = get_bits1(gb);
  200. if (sps->nal_hrd_parameters_present_flag)
  201. if (decode_hrd_parameters(gb, avctx, sps) < 0)
  202. return AVERROR_INVALIDDATA;
  203. sps->vcl_hrd_parameters_present_flag = get_bits1(gb);
  204. if (sps->vcl_hrd_parameters_present_flag)
  205. if (decode_hrd_parameters(gb, avctx, sps) < 0)
  206. return AVERROR_INVALIDDATA;
  207. if (sps->nal_hrd_parameters_present_flag ||
  208. sps->vcl_hrd_parameters_present_flag)
  209. get_bits1(gb); /* low_delay_hrd_flag */
  210. sps->pic_struct_present_flag = get_bits1(gb);
  211. sps->bitstream_restriction_flag = get_bits1(gb);
  212. if (sps->bitstream_restriction_flag) {
  213. get_bits1(gb); /* motion_vectors_over_pic_boundaries_flag */
  214. get_ue_golomb(gb); /* max_bytes_per_pic_denom */
  215. get_ue_golomb(gb); /* max_bits_per_mb_denom */
  216. get_ue_golomb(gb); /* log2_max_mv_length_horizontal */
  217. get_ue_golomb(gb); /* log2_max_mv_length_vertical */
  218. sps->num_reorder_frames = get_ue_golomb(gb);
  219. get_ue_golomb(gb); /*max_dec_frame_buffering*/
  220. if (get_bits_left(gb) < 0) {
  221. sps->num_reorder_frames = 0;
  222. sps->bitstream_restriction_flag = 0;
  223. }
  224. if (sps->num_reorder_frames > 16U
  225. /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
  226. av_log(avctx, AV_LOG_ERROR,
  227. "Clipping illegal num_reorder_frames %d\n",
  228. sps->num_reorder_frames);
  229. sps->num_reorder_frames = 16;
  230. return AVERROR_INVALIDDATA;
  231. }
  232. }
  233. if (get_bits_left(gb) < 0) {
  234. av_log(avctx, AV_LOG_ERROR,
  235. "Overread VUI by %d bits\n", -get_bits_left(gb));
  236. return AVERROR_INVALIDDATA;
  237. }
  238. return 0;
  239. }
  240. static void decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size,
  241. const uint8_t *jvt_list,
  242. const uint8_t *fallback_list)
  243. {
  244. int i, last = 8, next = 8;
  245. const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct;
  246. if (!get_bits1(gb)) /* matrix not written, we use the predicted one */
  247. memcpy(factors, fallback_list, size * sizeof(uint8_t));
  248. else
  249. for (i = 0; i < size; i++) {
  250. if (next)
  251. next = (last + get_se_golomb(gb)) & 0xff;
  252. if (!i && !next) { /* matrix not written, we use the preset one */
  253. memcpy(factors, jvt_list, size * sizeof(uint8_t));
  254. break;
  255. }
  256. last = factors[scan[i]] = next ? next : last;
  257. }
  258. }
  259. static void decode_scaling_matrices(GetBitContext *gb, SPS *sps,
  260. PPS *pps, int is_sps,
  261. uint8_t(*scaling_matrix4)[16],
  262. uint8_t(*scaling_matrix8)[64])
  263. {
  264. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  265. const uint8_t *fallback[4] = {
  266. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  267. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  268. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  269. fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
  270. };
  271. if (get_bits1(gb)) {
  272. sps->scaling_matrix_present |= is_sps;
  273. decode_scaling_list(gb, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]); // Intra, Y
  274. decode_scaling_list(gb, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr
  275. decode_scaling_list(gb, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb
  276. decode_scaling_list(gb, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]); // Inter, Y
  277. decode_scaling_list(gb, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr
  278. decode_scaling_list(gb, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb
  279. if (is_sps || pps->transform_8x8_mode) {
  280. decode_scaling_list(gb, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y
  281. if (sps->chroma_format_idc == 3) {
  282. decode_scaling_list(gb, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr
  283. decode_scaling_list(gb, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb
  284. }
  285. decode_scaling_list(gb, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y
  286. if (sps->chroma_format_idc == 3) {
  287. decode_scaling_list(gb, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr
  288. decode_scaling_list(gb, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb
  289. }
  290. }
  291. }
  292. }
  293. int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
  294. H264ParamSets *ps)
  295. {
  296. AVBufferRef *sps_buf;
  297. int profile_idc, level_idc, constraint_set_flags = 0;
  298. unsigned int sps_id;
  299. int i, log2_max_frame_num_minus4;
  300. SPS *sps;
  301. profile_idc = get_bits(gb, 8);
  302. constraint_set_flags |= get_bits1(gb) << 0; // constraint_set0_flag
  303. constraint_set_flags |= get_bits1(gb) << 1; // constraint_set1_flag
  304. constraint_set_flags |= get_bits1(gb) << 2; // constraint_set2_flag
  305. constraint_set_flags |= get_bits1(gb) << 3; // constraint_set3_flag
  306. constraint_set_flags |= get_bits1(gb) << 4; // constraint_set4_flag
  307. constraint_set_flags |= get_bits1(gb) << 5; // constraint_set5_flag
  308. skip_bits(gb, 2); // reserved_zero_2bits
  309. level_idc = get_bits(gb, 8);
  310. sps_id = get_ue_golomb_31(gb);
  311. if (sps_id >= MAX_SPS_COUNT) {
  312. av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
  313. return AVERROR_INVALIDDATA;
  314. }
  315. sps_buf = av_buffer_allocz(sizeof(*sps));
  316. if (!sps_buf)
  317. return AVERROR(ENOMEM);
  318. sps = (SPS*)sps_buf->data;
  319. sps->sps_id = sps_id;
  320. sps->time_offset_length = 24;
  321. sps->profile_idc = profile_idc;
  322. sps->constraint_set_flags = constraint_set_flags;
  323. sps->level_idc = level_idc;
  324. memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
  325. memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
  326. sps->scaling_matrix_present = 0;
  327. if (sps->profile_idc == 100 || // High profile
  328. sps->profile_idc == 110 || // High10 profile
  329. sps->profile_idc == 122 || // High422 profile
  330. sps->profile_idc == 244 || // High444 Predictive profile
  331. sps->profile_idc == 44 || // Cavlc444 profile
  332. sps->profile_idc == 83 || // Scalable Constrained High profile (SVC)
  333. sps->profile_idc == 86 || // Scalable High Intra profile (SVC)
  334. sps->profile_idc == 118 || // Stereo High profile (MVC)
  335. sps->profile_idc == 128 || // Multiview High profile (MVC)
  336. sps->profile_idc == 138 || // Multiview Depth High profile (MVCD)
  337. sps->profile_idc == 144) { // old High444 profile
  338. sps->chroma_format_idc = get_ue_golomb_31(gb);
  339. if (sps->chroma_format_idc > 3) {
  340. avpriv_request_sample(avctx, "chroma_format_idc %u",
  341. sps->chroma_format_idc);
  342. goto fail;
  343. } else if (sps->chroma_format_idc == 3) {
  344. sps->residual_color_transform_flag = get_bits1(gb);
  345. }
  346. sps->bit_depth_luma = get_ue_golomb(gb) + 8;
  347. sps->bit_depth_chroma = get_ue_golomb(gb) + 8;
  348. if (sps->bit_depth_chroma != sps->bit_depth_luma) {
  349. avpriv_request_sample(avctx,
  350. "Different chroma and luma bit depth");
  351. goto fail;
  352. }
  353. sps->transform_bypass = get_bits1(gb);
  354. decode_scaling_matrices(gb, sps, NULL, 1,
  355. sps->scaling_matrix4, sps->scaling_matrix8);
  356. } else {
  357. sps->chroma_format_idc = 1;
  358. sps->bit_depth_luma = 8;
  359. sps->bit_depth_chroma = 8;
  360. }
  361. log2_max_frame_num_minus4 = get_ue_golomb(gb);
  362. if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||
  363. log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) {
  364. av_log(avctx, AV_LOG_ERROR,
  365. "log2_max_frame_num_minus4 out of range (0-12): %d\n",
  366. log2_max_frame_num_minus4);
  367. goto fail;
  368. }
  369. sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
  370. sps->poc_type = get_ue_golomb_31(gb);
  371. if (sps->poc_type == 0) { // FIXME #define
  372. sps->log2_max_poc_lsb = get_ue_golomb(gb) + 4;
  373. } else if (sps->poc_type == 1) { // FIXME #define
  374. sps->delta_pic_order_always_zero_flag = get_bits1(gb);
  375. sps->offset_for_non_ref_pic = get_se_golomb(gb);
  376. sps->offset_for_top_to_bottom_field = get_se_golomb(gb);
  377. sps->poc_cycle_length = get_ue_golomb(gb);
  378. if ((unsigned)sps->poc_cycle_length >=
  379. FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) {
  380. av_log(avctx, AV_LOG_ERROR,
  381. "poc_cycle_length overflow %d\n", sps->poc_cycle_length);
  382. goto fail;
  383. }
  384. for (i = 0; i < sps->poc_cycle_length; i++)
  385. sps->offset_for_ref_frame[i] = get_se_golomb(gb);
  386. } else if (sps->poc_type != 2) {
  387. av_log(avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  388. goto fail;
  389. }
  390. sps->ref_frame_count = get_ue_golomb_31(gb);
  391. if (sps->ref_frame_count > MAX_DELAYED_PIC_COUNT) {
  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->ref_frame_count || avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT)) {
  483. sps->num_reorder_frames = MAX_DELAYED_PIC_COUNT - 1;
  484. for (i = 0; i < FF_ARRAY_ELEMS(level_max_dpb_mbs); i++) {
  485. if (level_max_dpb_mbs[i][0] == sps->level_idc) {
  486. sps->num_reorder_frames = FFMIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height),
  487. sps->num_reorder_frames);
  488. break;
  489. }
  490. }
  491. }
  492. if (!sps->sar.den)
  493. sps->sar.den = 1;
  494. if (avctx->debug & FF_DEBUG_PICT_INFO) {
  495. static const char csp[4][5] = { "Gray", "420", "422", "444" };
  496. av_log(avctx, AV_LOG_DEBUG,
  497. "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32"\n",
  498. sps_id, sps->profile_idc, sps->level_idc,
  499. sps->poc_type,
  500. sps->ref_frame_count,
  501. sps->mb_width, sps->mb_height,
  502. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  503. sps->direct_8x8_inference_flag ? "8B8" : "",
  504. sps->crop_left, sps->crop_right,
  505. sps->crop_top, sps->crop_bottom,
  506. sps->vui_parameters_present_flag ? "VUI" : "",
  507. csp[sps->chroma_format_idc],
  508. sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
  509. sps->timing_info_present_flag ? sps->time_scale : 0);
  510. }
  511. /* check if this is a repeat of an already parsed SPS, then keep the
  512. * original one.
  513. * otherwise drop all PPSes that depend on it */
  514. if (ps->sps_list[sps_id] &&
  515. !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
  516. av_buffer_unref(&sps_buf);
  517. } else {
  518. remove_sps(ps, sps_id);
  519. ps->sps_list[sps_id] = sps_buf;
  520. }
  521. return 0;
  522. fail:
  523. av_buffer_unref(&sps_buf);
  524. return AVERROR_INVALIDDATA;
  525. }
  526. static void init_dequant8_coeff_table(PPS *pps, const SPS *sps)
  527. {
  528. int i, j, q, x;
  529. const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8);
  530. for (i = 0; i < 6; i++) {
  531. pps->dequant8_coeff[i] = pps->dequant8_buffer[i];
  532. for (j = 0; j < i; j++)
  533. if (!memcmp(pps->scaling_matrix8[j], pps->scaling_matrix8[i],
  534. 64 * sizeof(uint8_t))) {
  535. pps->dequant8_coeff[i] = pps->dequant8_buffer[j];
  536. break;
  537. }
  538. if (j < i)
  539. continue;
  540. for (q = 0; q < max_qp + 1; q++) {
  541. int shift = ff_h264_quant_div6[q];
  542. int idx = ff_h264_quant_rem6[q];
  543. for (x = 0; x < 64; x++)
  544. pps->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
  545. ((uint32_t)ff_h264_dequant8_coeff_init[idx][ff_h264_dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
  546. pps->scaling_matrix8[i][x]) << shift;
  547. }
  548. }
  549. }
  550. static void init_dequant4_coeff_table(PPS *pps, const SPS *sps)
  551. {
  552. int i, j, q, x;
  553. const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8);
  554. for (i = 0; i < 6; i++) {
  555. pps->dequant4_coeff[i] = pps->dequant4_buffer[i];
  556. for (j = 0; j < i; j++)
  557. if (!memcmp(pps->scaling_matrix4[j], pps->scaling_matrix4[i],
  558. 16 * sizeof(uint8_t))) {
  559. pps->dequant4_coeff[i] = pps->dequant4_buffer[j];
  560. break;
  561. }
  562. if (j < i)
  563. continue;
  564. for (q = 0; q < max_qp + 1; q++) {
  565. int shift = ff_h264_quant_div6[q] + 2;
  566. int idx = ff_h264_quant_rem6[q];
  567. for (x = 0; x < 16; x++)
  568. pps->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
  569. ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
  570. pps->scaling_matrix4[i][x]) << shift;
  571. }
  572. }
  573. }
  574. static void init_dequant_tables(PPS *pps, const SPS *sps)
  575. {
  576. int i, x;
  577. init_dequant4_coeff_table(pps, sps);
  578. if (pps->transform_8x8_mode)
  579. init_dequant8_coeff_table(pps, sps);
  580. if (sps->transform_bypass) {
  581. for (i = 0; i < 6; i++)
  582. for (x = 0; x < 16; x++)
  583. pps->dequant4_coeff[i][0][x] = 1 << 6;
  584. if (pps->transform_8x8_mode)
  585. for (i = 0; i < 6; i++)
  586. for (x = 0; x < 64; x++)
  587. pps->dequant8_coeff[i][0][x] = 1 << 6;
  588. }
  589. }
  590. static void build_qp_table(PPS *pps, int t, int index, const int depth)
  591. {
  592. int i;
  593. const int max_qp = 51 + 6 * (depth - 8);
  594. for (i = 0; i < max_qp + 1; i++)
  595. pps->chroma_qp_table[t][i] =
  596. ff_h264_chroma_qp[depth - 8][av_clip(i + index, 0, max_qp)];
  597. }
  598. int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
  599. H264ParamSets *ps, int bit_length)
  600. {
  601. AVBufferRef *pps_buf;
  602. SPS *sps;
  603. unsigned int pps_id = get_ue_golomb(gb);
  604. PPS *pps;
  605. int qp_bd_offset;
  606. int bits_left;
  607. int ret;
  608. if (pps_id >= MAX_PPS_COUNT) {
  609. av_log(avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
  610. return AVERROR_INVALIDDATA;
  611. }
  612. pps_buf = av_buffer_allocz(sizeof(*pps));
  613. if (!pps_buf)
  614. return AVERROR(ENOMEM);
  615. pps = (PPS*)pps_buf->data;
  616. pps->sps_id = get_ue_golomb_31(gb);
  617. if ((unsigned)pps->sps_id >= MAX_SPS_COUNT ||
  618. !ps->sps_list[pps->sps_id]) {
  619. av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id);
  620. ret = AVERROR_INVALIDDATA;
  621. goto fail;
  622. }
  623. sps = (SPS*)ps->sps_list[pps->sps_id]->data;
  624. if (sps->bit_depth_luma > 10) {
  625. av_log(avctx, AV_LOG_ERROR,
  626. "Unimplemented luma bit depth=%d (max=10)\n",
  627. sps->bit_depth_luma);
  628. ret = AVERROR_PATCHWELCOME;
  629. goto fail;
  630. }
  631. pps->cabac = get_bits1(gb);
  632. pps->pic_order_present = get_bits1(gb);
  633. pps->slice_group_count = get_ue_golomb(gb) + 1;
  634. if (pps->slice_group_count > 1) {
  635. pps->mb_slice_group_map_type = get_ue_golomb(gb);
  636. av_log(avctx, AV_LOG_ERROR, "FMO not supported\n");
  637. switch (pps->mb_slice_group_map_type) {
  638. case 0:
  639. #if 0
  640. | for (i = 0; i <= num_slice_groups_minus1; i++) | | |
  641. | run_length[i] |1 |ue(v) |
  642. #endif
  643. break;
  644. case 2:
  645. #if 0
  646. | for (i = 0; i < num_slice_groups_minus1; i++) { | | |
  647. | top_left_mb[i] |1 |ue(v) |
  648. | bottom_right_mb[i] |1 |ue(v) |
  649. | } | | |
  650. #endif
  651. break;
  652. case 3:
  653. case 4:
  654. case 5:
  655. #if 0
  656. | slice_group_change_direction_flag |1 |u(1) |
  657. | slice_group_change_rate_minus1 |1 |ue(v) |
  658. #endif
  659. break;
  660. case 6:
  661. #if 0
  662. | slice_group_id_cnt_minus1 |1 |ue(v) |
  663. | for (i = 0; i <= slice_group_id_cnt_minus1; i++)| | |
  664. | slice_group_id[i] |1 |u(v) |
  665. #endif
  666. break;
  667. }
  668. }
  669. pps->ref_count[0] = get_ue_golomb(gb) + 1;
  670. pps->ref_count[1] = get_ue_golomb(gb) + 1;
  671. if (pps->ref_count[0] - 1 > 32 - 1 || pps->ref_count[1] - 1 > 32 - 1) {
  672. av_log(avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  673. ret = AVERROR_INVALIDDATA;
  674. goto fail;
  675. }
  676. qp_bd_offset = 6 * (sps->bit_depth_luma - 8);
  677. pps->weighted_pred = get_bits1(gb);
  678. pps->weighted_bipred_idc = get_bits(gb, 2);
  679. pps->init_qp = get_se_golomb(gb) + 26 + qp_bd_offset;
  680. pps->init_qs = get_se_golomb(gb) + 26 + qp_bd_offset;
  681. pps->chroma_qp_index_offset[0] = get_se_golomb(gb);
  682. pps->deblocking_filter_parameters_present = get_bits1(gb);
  683. pps->constrained_intra_pred = get_bits1(gb);
  684. pps->redundant_pic_cnt_present = get_bits1(gb);
  685. pps->transform_8x8_mode = 0;
  686. memcpy(pps->scaling_matrix4, sps->scaling_matrix4,
  687. sizeof(pps->scaling_matrix4));
  688. memcpy(pps->scaling_matrix8, sps->scaling_matrix8,
  689. sizeof(pps->scaling_matrix8));
  690. bits_left = bit_length - get_bits_count(gb);
  691. if (bits_left && (bits_left > 8 ||
  692. show_bits(gb, bits_left) != 1 << (bits_left - 1))) {
  693. pps->transform_8x8_mode = get_bits1(gb);
  694. decode_scaling_matrices(gb, sps, pps, 0,
  695. pps->scaling_matrix4, pps->scaling_matrix8);
  696. // second_chroma_qp_index_offset
  697. pps->chroma_qp_index_offset[1] = get_se_golomb(gb);
  698. } else {
  699. pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0];
  700. }
  701. build_qp_table(pps, 0, pps->chroma_qp_index_offset[0],
  702. sps->bit_depth_luma);
  703. build_qp_table(pps, 1, pps->chroma_qp_index_offset[1],
  704. sps->bit_depth_luma);
  705. init_dequant_tables(pps, sps);
  706. if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
  707. pps->chroma_qp_diff = 1;
  708. if (avctx->debug & FF_DEBUG_PICT_INFO) {
  709. av_log(avctx, AV_LOG_DEBUG,
  710. "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
  711. pps_id, pps->sps_id,
  712. pps->cabac ? "CABAC" : "CAVLC",
  713. pps->slice_group_count,
  714. pps->ref_count[0], pps->ref_count[1],
  715. pps->weighted_pred ? "weighted" : "",
  716. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
  717. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  718. pps->constrained_intra_pred ? "CONSTR" : "",
  719. pps->redundant_pic_cnt_present ? "REDU" : "",
  720. pps->transform_8x8_mode ? "8x8DCT" : "");
  721. }
  722. remove_pps(ps, pps_id);
  723. ps->pps_list[pps_id] = pps_buf;
  724. return 0;
  725. fail:
  726. av_buffer_unref(&pps_buf);
  727. return ret;
  728. }