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.

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