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.

489 lines
17KB

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