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.

517 lines
18KB

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