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.

665 lines
26KB

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