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.

720 lines
28KB

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