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.

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