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.

548 lines
19KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "bytestream.h"
  19. #include "get_bits.h"
  20. #include "golomb.h"
  21. #include "h264.h"
  22. #include "h264dec.h"
  23. #include "h264_parse.h"
  24. #include "h264_ps.h"
  25. int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps,
  26. const int *ref_count, int slice_type_nos,
  27. H264PredWeightTable *pwt,
  28. int picture_structure, void *logctx)
  29. {
  30. int list, i, j;
  31. int luma_def, chroma_def;
  32. pwt->use_weight = 0;
  33. pwt->use_weight_chroma = 0;
  34. pwt->luma_log2_weight_denom = get_ue_golomb(gb);
  35. if (pwt->luma_log2_weight_denom > 7U) {
  36. av_log(logctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is out of range\n", pwt->luma_log2_weight_denom);
  37. pwt->luma_log2_weight_denom = 0;
  38. }
  39. luma_def = 1 << pwt->luma_log2_weight_denom;
  40. if (sps->chroma_format_idc) {
  41. pwt->chroma_log2_weight_denom = get_ue_golomb(gb);
  42. if (pwt->chroma_log2_weight_denom > 7U) {
  43. av_log(logctx, AV_LOG_ERROR, "chroma_log2_weight_denom %d is out of range\n", pwt->chroma_log2_weight_denom);
  44. pwt->chroma_log2_weight_denom = 0;
  45. }
  46. chroma_def = 1 << pwt->chroma_log2_weight_denom;
  47. }
  48. for (list = 0; list < 2; list++) {
  49. pwt->luma_weight_flag[list] = 0;
  50. pwt->chroma_weight_flag[list] = 0;
  51. for (i = 0; i < ref_count[list]; i++) {
  52. int luma_weight_flag, chroma_weight_flag;
  53. luma_weight_flag = get_bits1(gb);
  54. if (luma_weight_flag) {
  55. pwt->luma_weight[i][list][0] = get_se_golomb(gb);
  56. pwt->luma_weight[i][list][1] = get_se_golomb(gb);
  57. if ((int8_t)pwt->luma_weight[i][list][0] != pwt->luma_weight[i][list][0] ||
  58. (int8_t)pwt->luma_weight[i][list][1] != pwt->luma_weight[i][list][1])
  59. goto out_range_weight;
  60. if (pwt->luma_weight[i][list][0] != luma_def ||
  61. pwt->luma_weight[i][list][1] != 0) {
  62. pwt->use_weight = 1;
  63. pwt->luma_weight_flag[list] = 1;
  64. }
  65. } else {
  66. pwt->luma_weight[i][list][0] = luma_def;
  67. pwt->luma_weight[i][list][1] = 0;
  68. }
  69. if (sps->chroma_format_idc) {
  70. chroma_weight_flag = get_bits1(gb);
  71. if (chroma_weight_flag) {
  72. int j;
  73. for (j = 0; j < 2; j++) {
  74. pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb);
  75. pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb);
  76. if ((int8_t)pwt->chroma_weight[i][list][j][0] != pwt->chroma_weight[i][list][j][0] ||
  77. (int8_t)pwt->chroma_weight[i][list][j][1] != pwt->chroma_weight[i][list][j][1]) {
  78. pwt->chroma_weight[i][list][j][0] = chroma_def;
  79. pwt->chroma_weight[i][list][j][1] = 0;
  80. goto out_range_weight;
  81. }
  82. if (pwt->chroma_weight[i][list][j][0] != chroma_def ||
  83. pwt->chroma_weight[i][list][j][1] != 0) {
  84. pwt->use_weight_chroma = 1;
  85. pwt->chroma_weight_flag[list] = 1;
  86. }
  87. }
  88. } else {
  89. int j;
  90. for (j = 0; j < 2; j++) {
  91. pwt->chroma_weight[i][list][j][0] = chroma_def;
  92. pwt->chroma_weight[i][list][j][1] = 0;
  93. }
  94. }
  95. }
  96. // for MBAFF
  97. if (picture_structure == PICT_FRAME) {
  98. pwt->luma_weight[16 + 2 * i][list][0] = pwt->luma_weight[16 + 2 * i + 1][list][0] = pwt->luma_weight[i][list][0];
  99. pwt->luma_weight[16 + 2 * i][list][1] = pwt->luma_weight[16 + 2 * i + 1][list][1] = pwt->luma_weight[i][list][1];
  100. if (sps->chroma_format_idc) {
  101. for (j = 0; j < 2; j++) {
  102. pwt->chroma_weight[16 + 2 * i][list][j][0] = pwt->chroma_weight[16 + 2 * i + 1][list][j][0] = pwt->chroma_weight[i][list][j][0];
  103. pwt->chroma_weight[16 + 2 * i][list][j][1] = pwt->chroma_weight[16 + 2 * i + 1][list][j][1] = pwt->chroma_weight[i][list][j][1];
  104. }
  105. }
  106. }
  107. }
  108. if (slice_type_nos != AV_PICTURE_TYPE_B)
  109. break;
  110. }
  111. pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
  112. return 0;
  113. out_range_weight:
  114. avpriv_request_sample(logctx, "Out of range weight");
  115. return AVERROR_INVALIDDATA;
  116. }
  117. /**
  118. * Check if the top & left blocks are available if needed and
  119. * change the dc mode so it only uses the available blocks.
  120. */
  121. int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx,
  122. int top_samples_available, int left_samples_available)
  123. {
  124. static const int8_t top[12] = {
  125. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  126. };
  127. static const int8_t left[12] = {
  128. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  129. };
  130. int i;
  131. if (!(top_samples_available & 0x8000)) {
  132. for (i = 0; i < 4; i++) {
  133. int status = top[pred_mode_cache[scan8[0] + i]];
  134. if (status < 0) {
  135. av_log(logctx, AV_LOG_ERROR,
  136. "top block unavailable for requested intra mode %d\n",
  137. status);
  138. return AVERROR_INVALIDDATA;
  139. } else if (status) {
  140. pred_mode_cache[scan8[0] + i] = status;
  141. }
  142. }
  143. }
  144. if ((left_samples_available & 0x8888) != 0x8888) {
  145. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  146. for (i = 0; i < 4; i++)
  147. if (!(left_samples_available & mask[i])) {
  148. int status = left[pred_mode_cache[scan8[0] + 8 * i]];
  149. if (status < 0) {
  150. av_log(logctx, AV_LOG_ERROR,
  151. "left block unavailable for requested intra4x4 mode %d\n",
  152. status);
  153. return AVERROR_INVALIDDATA;
  154. } else if (status) {
  155. pred_mode_cache[scan8[0] + 8 * i] = status;
  156. }
  157. }
  158. }
  159. return 0;
  160. }
  161. /**
  162. * Check if the top & left blocks are available if needed and
  163. * change the dc mode so it only uses the available blocks.
  164. */
  165. int ff_h264_check_intra_pred_mode(void *logctx, int top_samples_available,
  166. int left_samples_available,
  167. int mode, int is_chroma)
  168. {
  169. static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  170. static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  171. if (mode > 3U) {
  172. av_log(logctx, AV_LOG_ERROR,
  173. "out of range intra chroma pred mode\n");
  174. return AVERROR_INVALIDDATA;
  175. }
  176. if (!(top_samples_available & 0x8000)) {
  177. mode = top[mode];
  178. if (mode < 0) {
  179. av_log(logctx, AV_LOG_ERROR,
  180. "top block unavailable for requested intra mode\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. }
  184. if ((left_samples_available & 0x8080) != 0x8080) {
  185. mode = left[mode];
  186. if (mode < 0) {
  187. av_log(logctx, AV_LOG_ERROR,
  188. "left block unavailable for requested intra mode\n");
  189. return AVERROR_INVALIDDATA;
  190. }
  191. if (is_chroma && (left_samples_available & 0x8080)) {
  192. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  193. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  194. (!(left_samples_available & 0x8000)) +
  195. 2 * (mode == DC_128_PRED8x8);
  196. }
  197. }
  198. return mode;
  199. }
  200. int ff_h264_parse_ref_count(int *plist_count, int ref_count[2],
  201. GetBitContext *gb, const PPS *pps,
  202. int slice_type_nos, int picture_structure, void *logctx)
  203. {
  204. int list_count;
  205. int num_ref_idx_active_override_flag;
  206. // set defaults, might be overridden a few lines later
  207. ref_count[0] = pps->ref_count[0];
  208. ref_count[1] = pps->ref_count[1];
  209. if (slice_type_nos != AV_PICTURE_TYPE_I) {
  210. unsigned max[2];
  211. max[0] = max[1] = picture_structure == PICT_FRAME ? 15 : 31;
  212. num_ref_idx_active_override_flag = get_bits1(gb);
  213. if (num_ref_idx_active_override_flag) {
  214. ref_count[0] = get_ue_golomb(gb) + 1;
  215. if (slice_type_nos == AV_PICTURE_TYPE_B) {
  216. ref_count[1] = get_ue_golomb(gb) + 1;
  217. } else
  218. // full range is spec-ok in this case, even for frames
  219. ref_count[1] = 1;
  220. }
  221. if (slice_type_nos == AV_PICTURE_TYPE_B)
  222. list_count = 2;
  223. else
  224. list_count = 1;
  225. if (ref_count[0] - 1 > max[0] || (list_count == 2 && (ref_count[1] - 1 > max[1]))) {
  226. av_log(logctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n",
  227. ref_count[0] - 1, max[0], ref_count[1] - 1, max[1]);
  228. ref_count[0] = ref_count[1] = 0;
  229. *plist_count = 0;
  230. goto fail;
  231. } else if (ref_count[1] - 1 > max[1]) {
  232. av_log(logctx, AV_LOG_DEBUG, "reference overflow %u > %u \n",
  233. ref_count[1] - 1, max[1]);
  234. ref_count[1] = 0;
  235. }
  236. } else {
  237. list_count = 0;
  238. ref_count[0] = ref_count[1] = 0;
  239. }
  240. *plist_count = list_count;
  241. return 0;
  242. fail:
  243. *plist_count = 0;
  244. ref_count[0] = 0;
  245. ref_count[1] = 0;
  246. return AVERROR_INVALIDDATA;
  247. }
  248. int ff_h264_init_poc(int pic_field_poc[2], int *pic_poc,
  249. const SPS *sps, H264POCContext *pc,
  250. int picture_structure, int nal_ref_idc)
  251. {
  252. const int max_frame_num = 1 << sps->log2_max_frame_num;
  253. int64_t field_poc[2];
  254. pc->frame_num_offset = pc->prev_frame_num_offset;
  255. if (pc->frame_num < pc->prev_frame_num)
  256. pc->frame_num_offset += max_frame_num;
  257. if (sps->poc_type == 0) {
  258. const int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
  259. if (pc->prev_poc_lsb < 0)
  260. pc->prev_poc_lsb = pc->poc_lsb;
  261. if (pc->poc_lsb < pc->prev_poc_lsb &&
  262. pc->prev_poc_lsb - pc->poc_lsb >= max_poc_lsb / 2)
  263. pc->poc_msb = pc->prev_poc_msb + max_poc_lsb;
  264. else if (pc->poc_lsb > pc->prev_poc_lsb &&
  265. pc->prev_poc_lsb - pc->poc_lsb < -max_poc_lsb / 2)
  266. pc->poc_msb = pc->prev_poc_msb - max_poc_lsb;
  267. else
  268. pc->poc_msb = pc->prev_poc_msb;
  269. field_poc[0] =
  270. field_poc[1] = pc->poc_msb + pc->poc_lsb;
  271. if (picture_structure == PICT_FRAME)
  272. field_poc[1] += pc->delta_poc_bottom;
  273. } else if (sps->poc_type == 1) {
  274. int abs_frame_num;
  275. int64_t expected_delta_per_poc_cycle, expectedpoc;
  276. int i;
  277. if (sps->poc_cycle_length != 0)
  278. abs_frame_num = pc->frame_num_offset + pc->frame_num;
  279. else
  280. abs_frame_num = 0;
  281. if (nal_ref_idc == 0 && abs_frame_num > 0)
  282. abs_frame_num--;
  283. expected_delta_per_poc_cycle = 0;
  284. for (i = 0; i < sps->poc_cycle_length; i++)
  285. // FIXME integrate during sps parse
  286. expected_delta_per_poc_cycle += sps->offset_for_ref_frame[i];
  287. if (abs_frame_num > 0) {
  288. int poc_cycle_cnt = (abs_frame_num - 1) / sps->poc_cycle_length;
  289. int frame_num_in_poc_cycle = (abs_frame_num - 1) % sps->poc_cycle_length;
  290. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  291. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  292. expectedpoc = expectedpoc + sps->offset_for_ref_frame[i];
  293. } else
  294. expectedpoc = 0;
  295. if (nal_ref_idc == 0)
  296. expectedpoc = expectedpoc + sps->offset_for_non_ref_pic;
  297. field_poc[0] = expectedpoc + pc->delta_poc[0];
  298. field_poc[1] = field_poc[0] + sps->offset_for_top_to_bottom_field;
  299. if (picture_structure == PICT_FRAME)
  300. field_poc[1] += pc->delta_poc[1];
  301. } else {
  302. int poc = 2 * (pc->frame_num_offset + pc->frame_num);
  303. if (!nal_ref_idc)
  304. poc--;
  305. field_poc[0] = poc;
  306. field_poc[1] = poc;
  307. }
  308. if ( field_poc[0] != (int)field_poc[0]
  309. || field_poc[1] != (int)field_poc[1])
  310. return AVERROR_INVALIDDATA;
  311. if (picture_structure != PICT_BOTTOM_FIELD)
  312. pic_field_poc[0] = field_poc[0];
  313. if (picture_structure != PICT_TOP_FIELD)
  314. pic_field_poc[1] = field_poc[1];
  315. *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
  316. return 0;
  317. }
  318. static int decode_extradata_ps(const uint8_t *data, int size, H264ParamSets *ps,
  319. int is_avc, void *logctx)
  320. {
  321. H2645Packet pkt = { 0 };
  322. int i, ret = 0;
  323. ret = ff_h2645_packet_split(&pkt, data, size, logctx, is_avc, 2, AV_CODEC_ID_H264, 1, 0);
  324. if (ret < 0) {
  325. ret = 0;
  326. goto fail;
  327. }
  328. for (i = 0; i < pkt.nb_nals; i++) {
  329. H2645NAL *nal = &pkt.nals[i];
  330. switch (nal->type) {
  331. case H264_NAL_SPS: {
  332. GetBitContext tmp_gb = nal->gb;
  333. ret = ff_h264_decode_seq_parameter_set(&tmp_gb, logctx, ps, 0);
  334. if (ret >= 0)
  335. break;
  336. av_log(logctx, AV_LOG_DEBUG,
  337. "SPS decoding failure, trying again with the complete NAL\n");
  338. init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
  339. ret = ff_h264_decode_seq_parameter_set(&tmp_gb, logctx, ps, 0);
  340. if (ret >= 0)
  341. break;
  342. ret = ff_h264_decode_seq_parameter_set(&nal->gb, logctx, ps, 1);
  343. if (ret < 0)
  344. goto fail;
  345. break;
  346. }
  347. case H264_NAL_PPS:
  348. ret = ff_h264_decode_picture_parameter_set(&nal->gb, logctx, ps,
  349. nal->size_bits);
  350. if (ret < 0)
  351. goto fail;
  352. break;
  353. default:
  354. av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n",
  355. nal->type);
  356. break;
  357. }
  358. }
  359. fail:
  360. ff_h2645_packet_uninit(&pkt);
  361. return ret;
  362. }
  363. /* There are (invalid) samples in the wild with mp4-style extradata, where the
  364. * parameter sets are stored unescaped (i.e. as RBSP).
  365. * This function catches the parameter set decoding failure and tries again
  366. * after escaping it */
  367. static int decode_extradata_ps_mp4(const uint8_t *buf, int buf_size, H264ParamSets *ps,
  368. int err_recognition, void *logctx)
  369. {
  370. int ret;
  371. ret = decode_extradata_ps(buf, buf_size, ps, 1, logctx);
  372. if (ret < 0 && !(err_recognition & AV_EF_EXPLODE)) {
  373. GetByteContext gbc;
  374. PutByteContext pbc;
  375. uint8_t *escaped_buf;
  376. int escaped_buf_size;
  377. av_log(logctx, AV_LOG_WARNING,
  378. "SPS decoding failure, trying again after escaping the NAL\n");
  379. if (buf_size / 2 >= (INT16_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 3)
  380. return AVERROR(ERANGE);
  381. escaped_buf_size = buf_size * 3 / 2 + AV_INPUT_BUFFER_PADDING_SIZE;
  382. escaped_buf = av_mallocz(escaped_buf_size);
  383. if (!escaped_buf)
  384. return AVERROR(ENOMEM);
  385. bytestream2_init(&gbc, buf, buf_size);
  386. bytestream2_init_writer(&pbc, escaped_buf, escaped_buf_size);
  387. while (bytestream2_get_bytes_left(&gbc)) {
  388. if (bytestream2_get_bytes_left(&gbc) >= 3 &&
  389. bytestream2_peek_be24(&gbc) <= 3) {
  390. bytestream2_put_be24(&pbc, 3);
  391. bytestream2_skip(&gbc, 2);
  392. } else
  393. bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
  394. }
  395. escaped_buf_size = bytestream2_tell_p(&pbc);
  396. AV_WB16(escaped_buf, escaped_buf_size - 2);
  397. (void)decode_extradata_ps(escaped_buf, escaped_buf_size, ps, 1, logctx);
  398. // lorex.mp4 decodes ok even with extradata decoding failing
  399. av_freep(&escaped_buf);
  400. }
  401. return 0;
  402. }
  403. int ff_h264_decode_extradata(const uint8_t *data, int size, H264ParamSets *ps,
  404. int *is_avc, int *nal_length_size,
  405. int err_recognition, void *logctx)
  406. {
  407. int ret;
  408. if (!data || size <= 0)
  409. return -1;
  410. if (data[0] == 1) {
  411. int i, cnt, nalsize;
  412. const uint8_t *p = data;
  413. *is_avc = 1;
  414. if (size < 7) {
  415. av_log(logctx, AV_LOG_ERROR, "avcC %d too short\n", size);
  416. return AVERROR_INVALIDDATA;
  417. }
  418. // Decode sps from avcC
  419. cnt = *(p + 5) & 0x1f; // Number of sps
  420. p += 6;
  421. for (i = 0; i < cnt; i++) {
  422. nalsize = AV_RB16(p) + 2;
  423. if (nalsize > size - (p - data))
  424. return AVERROR_INVALIDDATA;
  425. ret = decode_extradata_ps_mp4(p, nalsize, ps, err_recognition, logctx);
  426. if (ret < 0) {
  427. av_log(logctx, AV_LOG_ERROR,
  428. "Decoding sps %d from avcC failed\n", i);
  429. return ret;
  430. }
  431. p += nalsize;
  432. }
  433. // Decode pps from avcC
  434. cnt = *(p++); // Number of pps
  435. for (i = 0; i < cnt; i++) {
  436. nalsize = AV_RB16(p) + 2;
  437. if (nalsize > size - (p - data))
  438. return AVERROR_INVALIDDATA;
  439. ret = decode_extradata_ps_mp4(p, nalsize, ps, err_recognition, logctx);
  440. if (ret < 0) {
  441. av_log(logctx, AV_LOG_ERROR,
  442. "Decoding pps %d from avcC failed\n", i);
  443. return ret;
  444. }
  445. p += nalsize;
  446. }
  447. // Store right nal length size that will be used to parse all other nals
  448. *nal_length_size = (data[4] & 0x03) + 1;
  449. } else {
  450. *is_avc = 0;
  451. ret = decode_extradata_ps(data, size, ps, 0, logctx);
  452. if (ret < 0)
  453. return ret;
  454. }
  455. return size;
  456. }
  457. /**
  458. * Compute profile from profile_idc and constraint_set?_flags.
  459. *
  460. * @param sps SPS
  461. *
  462. * @return profile as defined by FF_PROFILE_H264_*
  463. */
  464. int ff_h264_get_profile(const SPS *sps)
  465. {
  466. int profile = sps->profile_idc;
  467. switch (sps->profile_idc) {
  468. case FF_PROFILE_H264_BASELINE:
  469. // constraint_set1_flag set to 1
  470. profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  471. break;
  472. case FF_PROFILE_H264_HIGH_10:
  473. case FF_PROFILE_H264_HIGH_422:
  474. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  475. // constraint_set3_flag set to 1
  476. profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
  477. break;
  478. }
  479. return profile;
  480. }