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.

508 lines
17KB

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