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.

742 lines
29KB

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