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.

1817 lines
60KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/display.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/stereo3d.h"
  31. #include "libavutil/timer.h"
  32. #include "internal.h"
  33. #include "cabac.h"
  34. #include "cabac_functions.h"
  35. #include "error_resilience.h"
  36. #include "avcodec.h"
  37. #include "h264.h"
  38. #include "h264data.h"
  39. #include "h264chroma.h"
  40. #include "h264_mvpred.h"
  41. #include "golomb.h"
  42. #include "mathops.h"
  43. #include "me_cmp.h"
  44. #include "mpegutils.h"
  45. #include "rectangle.h"
  46. #include "svq3.h"
  47. #include "thread.h"
  48. #include <assert.h>
  49. const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
  50. static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  51. int (*mv)[2][4][2],
  52. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  53. {
  54. H264Context *h = opaque;
  55. H264SliceContext *sl = &h->slice_ctx[0];
  56. sl->mb_x = mb_x;
  57. sl->mb_y = mb_y;
  58. sl->mb_xy = mb_x + mb_y * h->mb_stride;
  59. memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
  60. assert(ref >= 0);
  61. /* FIXME: It is possible albeit uncommon that slice references
  62. * differ between slices. We take the easy approach and ignore
  63. * it for now. If this turns out to have any relevance in
  64. * practice then correct remapping should be added. */
  65. if (ref >= sl->ref_count[0])
  66. ref = 0;
  67. fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
  68. 2, 2, 2, ref, 1);
  69. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  70. fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
  71. pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
  72. assert(!FRAME_MBAFF(h));
  73. ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
  74. }
  75. void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
  76. int y, int height)
  77. {
  78. AVCodecContext *avctx = h->avctx;
  79. const AVFrame *src = h->cur_pic.f;
  80. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  81. int vshift = desc->log2_chroma_h;
  82. const int field_pic = h->picture_structure != PICT_FRAME;
  83. if (field_pic) {
  84. height <<= 1;
  85. y <<= 1;
  86. }
  87. height = FFMIN(height, avctx->height - y);
  88. if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
  89. return;
  90. if (avctx->draw_horiz_band) {
  91. int offset[AV_NUM_DATA_POINTERS];
  92. int i;
  93. offset[0] = y * src->linesize[0];
  94. offset[1] =
  95. offset[2] = (y >> vshift) * src->linesize[1];
  96. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  97. offset[i] = 0;
  98. emms_c();
  99. avctx->draw_horiz_band(avctx, src, offset,
  100. y, h->picture_structure, height);
  101. }
  102. }
  103. /**
  104. * Check if the top & left blocks are available if needed and
  105. * change the dc mode so it only uses the available blocks.
  106. */
  107. int ff_h264_check_intra4x4_pred_mode(const H264Context *h, H264SliceContext *sl)
  108. {
  109. static const int8_t top[12] = {
  110. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  111. };
  112. static const int8_t left[12] = {
  113. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  114. };
  115. int i;
  116. if (!(sl->top_samples_available & 0x8000)) {
  117. for (i = 0; i < 4; i++) {
  118. int status = top[sl->intra4x4_pred_mode_cache[scan8[0] + i]];
  119. if (status < 0) {
  120. av_log(h->avctx, AV_LOG_ERROR,
  121. "top block unavailable for requested intra4x4 mode %d at %d %d\n",
  122. status, sl->mb_x, sl->mb_y);
  123. return AVERROR_INVALIDDATA;
  124. } else if (status) {
  125. sl->intra4x4_pred_mode_cache[scan8[0] + i] = status;
  126. }
  127. }
  128. }
  129. if ((sl->left_samples_available & 0x8888) != 0x8888) {
  130. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  131. for (i = 0; i < 4; i++)
  132. if (!(sl->left_samples_available & mask[i])) {
  133. int status = left[sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
  134. if (status < 0) {
  135. av_log(h->avctx, AV_LOG_ERROR,
  136. "left block unavailable for requested intra4x4 mode %d at %d %d\n",
  137. status, sl->mb_x, sl->mb_y);
  138. return AVERROR_INVALIDDATA;
  139. } else if (status) {
  140. sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
  141. }
  142. }
  143. }
  144. return 0;
  145. } // FIXME cleanup like ff_h264_check_intra_pred_mode
  146. /**
  147. * Check if the top & left blocks are available if needed and
  148. * change the dc mode so it only uses the available blocks.
  149. */
  150. int ff_h264_check_intra_pred_mode(const H264Context *h, H264SliceContext *sl,
  151. int mode, int is_chroma)
  152. {
  153. static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  154. static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  155. if (mode > 3U) {
  156. av_log(h->avctx, AV_LOG_ERROR,
  157. "out of range intra chroma pred mode at %d %d\n",
  158. sl->mb_x, sl->mb_y);
  159. return AVERROR_INVALIDDATA;
  160. }
  161. if (!(sl->top_samples_available & 0x8000)) {
  162. mode = top[mode];
  163. if (mode < 0) {
  164. av_log(h->avctx, AV_LOG_ERROR,
  165. "top block unavailable for requested intra mode at %d %d\n",
  166. sl->mb_x, sl->mb_y);
  167. return AVERROR_INVALIDDATA;
  168. }
  169. }
  170. if ((sl->left_samples_available & 0x8080) != 0x8080) {
  171. mode = left[mode];
  172. if (is_chroma && (sl->left_samples_available & 0x8080)) {
  173. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  174. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  175. (!(sl->left_samples_available & 0x8000)) +
  176. 2 * (mode == DC_128_PRED8x8);
  177. }
  178. if (mode < 0) {
  179. av_log(h->avctx, AV_LOG_ERROR,
  180. "left block unavailable for requested intra mode at %d %d\n",
  181. sl->mb_x, sl->mb_y);
  182. return AVERROR_INVALIDDATA;
  183. }
  184. }
  185. return mode;
  186. }
  187. const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl,
  188. const uint8_t *src,
  189. int *dst_length, int *consumed, int length)
  190. {
  191. int i, si, di;
  192. uint8_t *dst;
  193. // src[0]&0x80; // forbidden bit
  194. h->nal_ref_idc = src[0] >> 5;
  195. h->nal_unit_type = src[0] & 0x1F;
  196. src++;
  197. length--;
  198. #define STARTCODE_TEST \
  199. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  200. if (src[i + 2] != 3) { \
  201. /* startcode, so we must be past the end */ \
  202. length = i; \
  203. } \
  204. break; \
  205. }
  206. #if HAVE_FAST_UNALIGNED
  207. #define FIND_FIRST_ZERO \
  208. if (i > 0 && !src[i]) \
  209. i--; \
  210. while (src[i]) \
  211. i++
  212. #if HAVE_FAST_64BIT
  213. for (i = 0; i + 1 < length; i += 9) {
  214. if (!((~AV_RN64A(src + i) &
  215. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  216. 0x8000800080008080ULL))
  217. continue;
  218. FIND_FIRST_ZERO;
  219. STARTCODE_TEST;
  220. i -= 7;
  221. }
  222. #else
  223. for (i = 0; i + 1 < length; i += 5) {
  224. if (!((~AV_RN32A(src + i) &
  225. (AV_RN32A(src + i) - 0x01000101U)) &
  226. 0x80008080U))
  227. continue;
  228. FIND_FIRST_ZERO;
  229. STARTCODE_TEST;
  230. i -= 3;
  231. }
  232. #endif
  233. #else
  234. for (i = 0; i + 1 < length; i += 2) {
  235. if (src[i])
  236. continue;
  237. if (i > 0 && src[i - 1] == 0)
  238. i--;
  239. STARTCODE_TEST;
  240. }
  241. #endif
  242. if (i >= length - 1) { // no escaped 0
  243. *dst_length = length;
  244. *consumed = length + 1; // +1 for the header
  245. return src;
  246. }
  247. av_fast_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size,
  248. length + AV_INPUT_BUFFER_PADDING_SIZE);
  249. dst = sl->rbsp_buffer;
  250. if (!dst)
  251. return NULL;
  252. memcpy(dst, src, i);
  253. si = di = i;
  254. while (si + 2 < length) {
  255. // remove escapes (very rare 1:2^22)
  256. if (src[si + 2] > 3) {
  257. dst[di++] = src[si++];
  258. dst[di++] = src[si++];
  259. } else if (src[si] == 0 && src[si + 1] == 0) {
  260. if (src[si + 2] == 3) { // escape
  261. dst[di++] = 0;
  262. dst[di++] = 0;
  263. si += 3;
  264. continue;
  265. } else // next start code
  266. goto nsc;
  267. }
  268. dst[di++] = src[si++];
  269. }
  270. while (si < length)
  271. dst[di++] = src[si++];
  272. nsc:
  273. memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  274. *dst_length = di;
  275. *consumed = si + 1; // +1 for the header
  276. /* FIXME store exact number of bits in the getbitcontext
  277. * (it is needed for decoding) */
  278. return dst;
  279. }
  280. /**
  281. * Identify the exact end of the bitstream
  282. * @return the length of the trailing, or 0 if damaged
  283. */
  284. static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
  285. {
  286. int v = *src;
  287. int r;
  288. ff_tlog(h->avctx, "rbsp trailing %X\n", v);
  289. for (r = 1; r < 9; r++) {
  290. if (v & 1)
  291. return r;
  292. v >>= 1;
  293. }
  294. return 0;
  295. }
  296. void ff_h264_free_tables(H264Context *h)
  297. {
  298. int i;
  299. av_freep(&h->intra4x4_pred_mode);
  300. av_freep(&h->chroma_pred_mode_table);
  301. av_freep(&h->cbp_table);
  302. av_freep(&h->mvd_table[0]);
  303. av_freep(&h->mvd_table[1]);
  304. av_freep(&h->direct_table);
  305. av_freep(&h->non_zero_count);
  306. av_freep(&h->slice_table_base);
  307. h->slice_table = NULL;
  308. av_freep(&h->list_counts);
  309. av_freep(&h->mb2b_xy);
  310. av_freep(&h->mb2br_xy);
  311. av_buffer_pool_uninit(&h->qscale_table_pool);
  312. av_buffer_pool_uninit(&h->mb_type_pool);
  313. av_buffer_pool_uninit(&h->motion_val_pool);
  314. av_buffer_pool_uninit(&h->ref_index_pool);
  315. for (i = 0; i < h->nb_slice_ctx; i++) {
  316. H264SliceContext *sl = &h->slice_ctx[i];
  317. av_freep(&sl->dc_val_base);
  318. av_freep(&sl->er.mb_index2xy);
  319. av_freep(&sl->er.error_status_table);
  320. av_freep(&sl->er.er_temp_buffer);
  321. av_freep(&sl->bipred_scratchpad);
  322. av_freep(&sl->edge_emu_buffer);
  323. av_freep(&sl->top_borders[0]);
  324. av_freep(&sl->top_borders[1]);
  325. sl->bipred_scratchpad_allocated = 0;
  326. sl->edge_emu_buffer_allocated = 0;
  327. sl->top_borders_allocated[0] = 0;
  328. sl->top_borders_allocated[1] = 0;
  329. }
  330. }
  331. int ff_h264_alloc_tables(H264Context *h)
  332. {
  333. const int big_mb_num = h->mb_stride * (h->mb_height + 1);
  334. const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count;
  335. int x, y;
  336. FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
  337. row_mb_num * 8 * sizeof(uint8_t), fail)
  338. h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
  339. FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
  340. big_mb_num * 48 * sizeof(uint8_t), fail)
  341. FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
  342. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
  343. FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
  344. big_mb_num * sizeof(uint16_t), fail)
  345. FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
  346. big_mb_num * sizeof(uint8_t), fail)
  347. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
  348. 16 * row_mb_num * sizeof(uint8_t), fail);
  349. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
  350. 16 * row_mb_num * sizeof(uint8_t), fail);
  351. h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
  352. h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
  353. FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
  354. 4 * big_mb_num * sizeof(uint8_t), fail);
  355. FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
  356. big_mb_num * sizeof(uint8_t), fail)
  357. memset(h->slice_table_base, -1,
  358. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
  359. h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
  360. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
  361. big_mb_num * sizeof(uint32_t), fail);
  362. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
  363. big_mb_num * sizeof(uint32_t), fail);
  364. for (y = 0; y < h->mb_height; y++)
  365. for (x = 0; x < h->mb_width; x++) {
  366. const int mb_xy = x + y * h->mb_stride;
  367. const int b_xy = 4 * x + 4 * y * h->b_stride;
  368. h->mb2b_xy[mb_xy] = b_xy;
  369. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
  370. }
  371. if (!h->dequant4_coeff[0])
  372. h264_init_dequant_tables(h);
  373. return 0;
  374. fail:
  375. ff_h264_free_tables(h);
  376. return AVERROR(ENOMEM);
  377. }
  378. /**
  379. * Init context
  380. * Allocate buffers which are not shared amongst multiple threads.
  381. */
  382. int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
  383. {
  384. ERContext *er = &sl->er;
  385. int mb_array_size = h->mb_height * h->mb_stride;
  386. int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
  387. int c_size = h->mb_stride * (h->mb_height + 1);
  388. int yc_size = y_size + 2 * c_size;
  389. int x, y, i;
  390. sl->ref_cache[0][scan8[5] + 1] =
  391. sl->ref_cache[0][scan8[7] + 1] =
  392. sl->ref_cache[0][scan8[13] + 1] =
  393. sl->ref_cache[1][scan8[5] + 1] =
  394. sl->ref_cache[1][scan8[7] + 1] =
  395. sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  396. if (CONFIG_ERROR_RESILIENCE) {
  397. /* init ER */
  398. er->avctx = h->avctx;
  399. er->decode_mb = h264_er_decode_mb;
  400. er->opaque = h;
  401. er->quarter_sample = 1;
  402. er->mb_num = h->mb_num;
  403. er->mb_width = h->mb_width;
  404. er->mb_height = h->mb_height;
  405. er->mb_stride = h->mb_stride;
  406. er->b8_stride = h->mb_width * 2 + 1;
  407. // error resilience code looks cleaner with this
  408. FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
  409. (h->mb_num + 1) * sizeof(int), fail);
  410. for (y = 0; y < h->mb_height; y++)
  411. for (x = 0; x < h->mb_width; x++)
  412. er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
  413. er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
  414. h->mb_stride + h->mb_width;
  415. FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
  416. mb_array_size * sizeof(uint8_t), fail);
  417. FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
  418. h->mb_height * h->mb_stride, fail);
  419. FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
  420. yc_size * sizeof(int16_t), fail);
  421. er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
  422. er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
  423. er->dc_val[2] = er->dc_val[1] + c_size;
  424. for (i = 0; i < yc_size; i++)
  425. sl->dc_val_base[i] = 1024;
  426. }
  427. return 0;
  428. fail:
  429. return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
  430. }
  431. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  432. int parse_extradata);
  433. int ff_h264_decode_extradata(H264Context *h)
  434. {
  435. AVCodecContext *avctx = h->avctx;
  436. int ret;
  437. if (avctx->extradata[0] == 1) {
  438. int i, cnt, nalsize;
  439. unsigned char *p = avctx->extradata;
  440. h->is_avc = 1;
  441. if (avctx->extradata_size < 7) {
  442. av_log(avctx, AV_LOG_ERROR,
  443. "avcC %d too short\n", avctx->extradata_size);
  444. return AVERROR_INVALIDDATA;
  445. }
  446. /* sps and pps in the avcC always have length coded with 2 bytes,
  447. * so put a fake nal_length_size = 2 while parsing them */
  448. h->nal_length_size = 2;
  449. // Decode sps from avcC
  450. cnt = *(p + 5) & 0x1f; // Number of sps
  451. p += 6;
  452. for (i = 0; i < cnt; i++) {
  453. nalsize = AV_RB16(p) + 2;
  454. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  455. return AVERROR_INVALIDDATA;
  456. ret = decode_nal_units(h, p, nalsize, 1);
  457. if (ret < 0) {
  458. av_log(avctx, AV_LOG_ERROR,
  459. "Decoding sps %d from avcC failed\n", i);
  460. return ret;
  461. }
  462. p += nalsize;
  463. }
  464. // Decode pps from avcC
  465. cnt = *(p++); // Number of pps
  466. for (i = 0; i < cnt; i++) {
  467. nalsize = AV_RB16(p) + 2;
  468. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  469. return AVERROR_INVALIDDATA;
  470. ret = decode_nal_units(h, p, nalsize, 1);
  471. if (ret < 0) {
  472. av_log(avctx, AV_LOG_ERROR,
  473. "Decoding pps %d from avcC failed\n", i);
  474. return ret;
  475. }
  476. p += nalsize;
  477. }
  478. // Store right nal length size that will be used to parse all other nals
  479. h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;
  480. } else {
  481. h->is_avc = 0;
  482. ret = decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1);
  483. if (ret < 0)
  484. return ret;
  485. }
  486. return 0;
  487. }
  488. static int h264_init_context(AVCodecContext *avctx, H264Context *h)
  489. {
  490. int i;
  491. h->avctx = avctx;
  492. h->dequant_coeff_pps = -1;
  493. h->picture_structure = PICT_FRAME;
  494. h->slice_context_count = 1;
  495. h->workaround_bugs = avctx->workaround_bugs;
  496. h->flags = avctx->flags;
  497. h->prev_poc_msb = 1 << 16;
  498. h->x264_build = -1;
  499. h->recovery_frame = -1;
  500. h->frame_recovered = 0;
  501. h->next_outputed_poc = INT_MIN;
  502. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  503. h->last_pocs[i] = INT_MIN;
  504. ff_h264_reset_sei(h);
  505. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  506. h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? H264_MAX_THREADS : 1;
  507. h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
  508. if (!h->slice_ctx) {
  509. h->nb_slice_ctx = 0;
  510. return AVERROR(ENOMEM);
  511. }
  512. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  513. h->DPB[i].f = av_frame_alloc();
  514. if (!h->DPB[i].f)
  515. return AVERROR(ENOMEM);
  516. }
  517. h->cur_pic.f = av_frame_alloc();
  518. if (!h->cur_pic.f)
  519. return AVERROR(ENOMEM);
  520. for (i = 0; i < h->nb_slice_ctx; i++)
  521. h->slice_ctx[i].h264 = h;
  522. return 0;
  523. }
  524. static AVOnce h264_vlc_init = AV_ONCE_INIT;
  525. av_cold int ff_h264_decode_init(AVCodecContext *avctx)
  526. {
  527. H264Context *h = avctx->priv_data;
  528. int ret;
  529. ret = h264_init_context(avctx, h);
  530. if (ret < 0)
  531. return ret;
  532. /* set defaults */
  533. if (!avctx->has_b_frames)
  534. h->low_delay = 1;
  535. ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
  536. if (ret != 0) {
  537. av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
  538. return AVERROR_UNKNOWN;
  539. }
  540. if (avctx->codec_id == AV_CODEC_ID_H264) {
  541. if (avctx->ticks_per_frame == 1)
  542. h->avctx->framerate.num *= 2;
  543. avctx->ticks_per_frame = 2;
  544. }
  545. if (avctx->extradata_size > 0 && avctx->extradata) {
  546. ret = ff_h264_decode_extradata(h);
  547. if (ret < 0) {
  548. ff_h264_free_context(h);
  549. return ret;
  550. }
  551. }
  552. if (h->sps.bitstream_restriction_flag &&
  553. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  554. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  555. h->low_delay = 0;
  556. }
  557. avctx->internal->allocate_progress = 1;
  558. if (h->enable_er) {
  559. av_log(avctx, AV_LOG_WARNING,
  560. "Error resilience is enabled. It is unsafe and unsupported and may crash. "
  561. "Use it at your own risk\n");
  562. }
  563. return 0;
  564. }
  565. static int decode_init_thread_copy(AVCodecContext *avctx)
  566. {
  567. H264Context *h = avctx->priv_data;
  568. int ret;
  569. if (!avctx->internal->is_copy)
  570. return 0;
  571. memset(h, 0, sizeof(*h));
  572. ret = h264_init_context(avctx, h);
  573. if (ret < 0)
  574. return ret;
  575. h->context_initialized = 0;
  576. return 0;
  577. }
  578. /**
  579. * Run setup operations that must be run after slice header decoding.
  580. * This includes finding the next displayed frame.
  581. *
  582. * @param h h264 master context
  583. * @param setup_finished enough NALs have been read that we can call
  584. * ff_thread_finish_setup()
  585. */
  586. static void decode_postinit(H264Context *h, int setup_finished)
  587. {
  588. H264Picture *out = h->cur_pic_ptr;
  589. H264Picture *cur = h->cur_pic_ptr;
  590. int i, pics, out_of_order, out_idx;
  591. int invalid = 0, cnt = 0;
  592. h->cur_pic_ptr->f->pict_type = h->pict_type;
  593. if (h->next_output_pic)
  594. return;
  595. if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
  596. /* FIXME: if we have two PAFF fields in one packet, we can't start
  597. * the next thread here. If we have one field per packet, we can.
  598. * The check in decode_nal_units() is not good enough to find this
  599. * yet, so we assume the worst for now. */
  600. // if (setup_finished)
  601. // ff_thread_finish_setup(h->avctx);
  602. return;
  603. }
  604. cur->f->interlaced_frame = 0;
  605. cur->f->repeat_pict = 0;
  606. /* Signal interlacing information externally. */
  607. /* Prioritize picture timing SEI information over used
  608. * decoding process if it exists. */
  609. if (h->sps.pic_struct_present_flag) {
  610. switch (h->sei_pic_struct) {
  611. case SEI_PIC_STRUCT_FRAME:
  612. break;
  613. case SEI_PIC_STRUCT_TOP_FIELD:
  614. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  615. cur->f->interlaced_frame = 1;
  616. break;
  617. case SEI_PIC_STRUCT_TOP_BOTTOM:
  618. case SEI_PIC_STRUCT_BOTTOM_TOP:
  619. if (FIELD_OR_MBAFF_PICTURE(h))
  620. cur->f->interlaced_frame = 1;
  621. else
  622. // try to flag soft telecine progressive
  623. cur->f->interlaced_frame = h->prev_interlaced_frame;
  624. break;
  625. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  626. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  627. /* Signal the possibility of telecined film externally
  628. * (pic_struct 5,6). From these hints, let the applications
  629. * decide if they apply deinterlacing. */
  630. cur->f->repeat_pict = 1;
  631. break;
  632. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  633. cur->f->repeat_pict = 2;
  634. break;
  635. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  636. cur->f->repeat_pict = 4;
  637. break;
  638. }
  639. if ((h->sei_ct_type & 3) &&
  640. h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  641. cur->f->interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
  642. } else {
  643. /* Derive interlacing flag from used decoding process. */
  644. cur->f->interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
  645. }
  646. h->prev_interlaced_frame = cur->f->interlaced_frame;
  647. if (cur->field_poc[0] != cur->field_poc[1]) {
  648. /* Derive top_field_first from field pocs. */
  649. cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  650. } else {
  651. if (cur->f->interlaced_frame || h->sps.pic_struct_present_flag) {
  652. /* Use picture timing SEI information. Even if it is a
  653. * information of a past frame, better than nothing. */
  654. if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
  655. h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  656. cur->f->top_field_first = 1;
  657. else
  658. cur->f->top_field_first = 0;
  659. } else {
  660. /* Most likely progressive */
  661. cur->f->top_field_first = 0;
  662. }
  663. }
  664. if (h->sei_frame_packing_present &&
  665. h->frame_packing_arrangement_type >= 0 &&
  666. h->frame_packing_arrangement_type <= 6 &&
  667. h->content_interpretation_type > 0 &&
  668. h->content_interpretation_type < 3) {
  669. AVStereo3D *stereo = av_stereo3d_create_side_data(cur->f);
  670. if (!stereo)
  671. return;
  672. switch (h->frame_packing_arrangement_type) {
  673. case 0:
  674. stereo->type = AV_STEREO3D_CHECKERBOARD;
  675. break;
  676. case 1:
  677. stereo->type = AV_STEREO3D_COLUMNS;
  678. break;
  679. case 2:
  680. stereo->type = AV_STEREO3D_LINES;
  681. break;
  682. case 3:
  683. if (h->quincunx_subsampling)
  684. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  685. else
  686. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  687. break;
  688. case 4:
  689. stereo->type = AV_STEREO3D_TOPBOTTOM;
  690. break;
  691. case 5:
  692. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  693. break;
  694. case 6:
  695. stereo->type = AV_STEREO3D_2D;
  696. break;
  697. }
  698. if (h->content_interpretation_type == 2)
  699. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  700. }
  701. if (h->sei_display_orientation_present &&
  702. (h->sei_anticlockwise_rotation || h->sei_hflip || h->sei_vflip)) {
  703. double angle = h->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
  704. AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
  705. AV_FRAME_DATA_DISPLAYMATRIX,
  706. sizeof(int32_t) * 9);
  707. if (!rotation)
  708. return;
  709. av_display_rotation_set((int32_t *)rotation->data, angle);
  710. av_display_matrix_flip((int32_t *)rotation->data,
  711. h->sei_hflip, h->sei_vflip);
  712. }
  713. if (h->sei_reguserdata_afd_present) {
  714. AVFrameSideData *sd = av_frame_new_side_data(cur->f, AV_FRAME_DATA_AFD,
  715. sizeof(uint8_t));
  716. if (!sd)
  717. return;
  718. *sd->data = h->active_format_description;
  719. h->sei_reguserdata_afd_present = 0;
  720. }
  721. if (h->a53_caption) {
  722. AVFrameSideData *sd = av_frame_new_side_data(cur->f,
  723. AV_FRAME_DATA_A53_CC,
  724. h->a53_caption_size);
  725. if (!sd)
  726. return;
  727. memcpy(sd->data, h->a53_caption, h->a53_caption_size);
  728. av_freep(&h->a53_caption);
  729. h->a53_caption_size = 0;
  730. }
  731. // FIXME do something with unavailable reference frames
  732. /* Sort B-frames into display order */
  733. if (h->sps.bitstream_restriction_flag &&
  734. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  735. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  736. h->low_delay = 0;
  737. }
  738. if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
  739. !h->sps.bitstream_restriction_flag) {
  740. h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
  741. h->low_delay = 0;
  742. }
  743. pics = 0;
  744. while (h->delayed_pic[pics])
  745. pics++;
  746. assert(pics <= MAX_DELAYED_PIC_COUNT);
  747. h->delayed_pic[pics++] = cur;
  748. if (cur->reference == 0)
  749. cur->reference = DELAYED_PIC_REF;
  750. /* Frame reordering. This code takes pictures from coding order and sorts
  751. * them by their incremental POC value into display order. It supports POC
  752. * gaps, MMCO reset codes and random resets.
  753. * A "display group" can start either with a IDR frame (f.key_frame = 1),
  754. * and/or can be closed down with a MMCO reset code. In sequences where
  755. * there is no delay, we can't detect that (since the frame was already
  756. * output to the user), so we also set h->mmco_reset to detect the MMCO
  757. * reset code.
  758. * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
  759. * we increase the delay between input and output. All frames affected by
  760. * the lag (e.g. those that should have been output before another frame
  761. * that we already returned to the user) will be dropped. This is a bug
  762. * that we will fix later. */
  763. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
  764. cnt += out->poc < h->last_pocs[i];
  765. invalid += out->poc == INT_MIN;
  766. }
  767. if (!h->mmco_reset && !cur->f->key_frame &&
  768. cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
  769. h->mmco_reset = 2;
  770. if (pics > 1)
  771. h->delayed_pic[pics - 2]->mmco_reset = 2;
  772. }
  773. if (h->mmco_reset || cur->f->key_frame) {
  774. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  775. h->last_pocs[i] = INT_MIN;
  776. cnt = 0;
  777. invalid = MAX_DELAYED_PIC_COUNT;
  778. }
  779. out = h->delayed_pic[0];
  780. out_idx = 0;
  781. for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
  782. h->delayed_pic[i] &&
  783. !h->delayed_pic[i - 1]->mmco_reset &&
  784. !h->delayed_pic[i]->f->key_frame;
  785. i++)
  786. if (h->delayed_pic[i]->poc < out->poc) {
  787. out = h->delayed_pic[i];
  788. out_idx = i;
  789. }
  790. if (h->avctx->has_b_frames == 0 &&
  791. (h->delayed_pic[0]->f->key_frame || h->mmco_reset))
  792. h->next_outputed_poc = INT_MIN;
  793. out_of_order = !out->f->key_frame && !h->mmco_reset &&
  794. (out->poc < h->next_outputed_poc);
  795. if (h->sps.bitstream_restriction_flag &&
  796. h->avctx->has_b_frames >= h->sps.num_reorder_frames) {
  797. } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
  798. h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
  799. if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
  800. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
  801. }
  802. h->low_delay = 0;
  803. } else if (h->low_delay &&
  804. ((h->next_outputed_poc != INT_MIN &&
  805. out->poc > h->next_outputed_poc + 2) ||
  806. cur->f->pict_type == AV_PICTURE_TYPE_B)) {
  807. h->low_delay = 0;
  808. h->avctx->has_b_frames++;
  809. }
  810. if (pics > h->avctx->has_b_frames) {
  811. out->reference &= ~DELAYED_PIC_REF;
  812. // for frame threading, the owner must be the second field's thread or
  813. // else the first thread can release the picture and reuse it unsafely
  814. for (i = out_idx; h->delayed_pic[i]; i++)
  815. h->delayed_pic[i] = h->delayed_pic[i + 1];
  816. }
  817. memmove(h->last_pocs, &h->last_pocs[1],
  818. sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
  819. h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
  820. if (!out_of_order && pics > h->avctx->has_b_frames) {
  821. h->next_output_pic = out;
  822. if (out->mmco_reset) {
  823. if (out_idx > 0) {
  824. h->next_outputed_poc = out->poc;
  825. h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
  826. } else {
  827. h->next_outputed_poc = INT_MIN;
  828. }
  829. } else {
  830. if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f->key_frame) {
  831. h->next_outputed_poc = INT_MIN;
  832. } else {
  833. h->next_outputed_poc = out->poc;
  834. }
  835. }
  836. h->mmco_reset = 0;
  837. } else {
  838. av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
  839. }
  840. if (h->next_output_pic) {
  841. if (h->next_output_pic->recovered) {
  842. // We have reached an recovery point and all frames after it in
  843. // display order are "recovered".
  844. h->frame_recovered |= FRAME_RECOVERED_SEI;
  845. }
  846. h->next_output_pic->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
  847. }
  848. if (setup_finished && !h->avctx->hwaccel) {
  849. ff_thread_finish_setup(h->avctx);
  850. if (h->avctx->active_thread_type & FF_THREAD_FRAME)
  851. h->setup_finished = 1;
  852. }
  853. }
  854. int ff_pred_weight_table(H264Context *h, H264SliceContext *sl)
  855. {
  856. int list, i;
  857. int luma_def, chroma_def;
  858. sl->use_weight = 0;
  859. sl->use_weight_chroma = 0;
  860. sl->luma_log2_weight_denom = get_ue_golomb(&sl->gb);
  861. if (h->sps.chroma_format_idc)
  862. sl->chroma_log2_weight_denom = get_ue_golomb(&sl->gb);
  863. luma_def = 1 << sl->luma_log2_weight_denom;
  864. chroma_def = 1 << sl->chroma_log2_weight_denom;
  865. for (list = 0; list < 2; list++) {
  866. sl->luma_weight_flag[list] = 0;
  867. sl->chroma_weight_flag[list] = 0;
  868. for (i = 0; i < sl->ref_count[list]; i++) {
  869. int luma_weight_flag, chroma_weight_flag;
  870. luma_weight_flag = get_bits1(&sl->gb);
  871. if (luma_weight_flag) {
  872. sl->luma_weight[i][list][0] = get_se_golomb(&sl->gb);
  873. sl->luma_weight[i][list][1] = get_se_golomb(&sl->gb);
  874. if (sl->luma_weight[i][list][0] != luma_def ||
  875. sl->luma_weight[i][list][1] != 0) {
  876. sl->use_weight = 1;
  877. sl->luma_weight_flag[list] = 1;
  878. }
  879. } else {
  880. sl->luma_weight[i][list][0] = luma_def;
  881. sl->luma_weight[i][list][1] = 0;
  882. }
  883. if (h->sps.chroma_format_idc) {
  884. chroma_weight_flag = get_bits1(&sl->gb);
  885. if (chroma_weight_flag) {
  886. int j;
  887. for (j = 0; j < 2; j++) {
  888. sl->chroma_weight[i][list][j][0] = get_se_golomb(&sl->gb);
  889. sl->chroma_weight[i][list][j][1] = get_se_golomb(&sl->gb);
  890. if (sl->chroma_weight[i][list][j][0] != chroma_def ||
  891. sl->chroma_weight[i][list][j][1] != 0) {
  892. sl->use_weight_chroma = 1;
  893. sl->chroma_weight_flag[list] = 1;
  894. }
  895. }
  896. } else {
  897. int j;
  898. for (j = 0; j < 2; j++) {
  899. sl->chroma_weight[i][list][j][0] = chroma_def;
  900. sl->chroma_weight[i][list][j][1] = 0;
  901. }
  902. }
  903. }
  904. }
  905. if (sl->slice_type_nos != AV_PICTURE_TYPE_B)
  906. break;
  907. }
  908. sl->use_weight = sl->use_weight || sl->use_weight_chroma;
  909. return 0;
  910. }
  911. /**
  912. * instantaneous decoder refresh.
  913. */
  914. static void idr(H264Context *h)
  915. {
  916. ff_h264_remove_all_refs(h);
  917. h->prev_frame_num =
  918. h->prev_frame_num_offset =
  919. h->prev_poc_msb =
  920. h->prev_poc_lsb = 0;
  921. }
  922. /* forget old pics after a seek */
  923. void ff_h264_flush_change(H264Context *h)
  924. {
  925. int i;
  926. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  927. h->last_pocs[i] = INT_MIN;
  928. h->next_outputed_poc = INT_MIN;
  929. h->prev_interlaced_frame = 1;
  930. idr(h);
  931. if (h->cur_pic_ptr)
  932. h->cur_pic_ptr->reference = 0;
  933. h->first_field = 0;
  934. ff_h264_reset_sei(h);
  935. h->recovery_frame = -1;
  936. h->frame_recovered = 0;
  937. }
  938. /* forget old pics after a seek */
  939. static void flush_dpb(AVCodecContext *avctx)
  940. {
  941. H264Context *h = avctx->priv_data;
  942. int i;
  943. memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
  944. ff_h264_flush_change(h);
  945. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  946. ff_h264_unref_picture(h, &h->DPB[i]);
  947. h->cur_pic_ptr = NULL;
  948. ff_h264_unref_picture(h, &h->cur_pic);
  949. h->mb_y = 0;
  950. ff_h264_free_tables(h);
  951. h->context_initialized = 0;
  952. }
  953. int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
  954. {
  955. const int max_frame_num = 1 << h->sps.log2_max_frame_num;
  956. int field_poc[2];
  957. h->frame_num_offset = h->prev_frame_num_offset;
  958. if (h->frame_num < h->prev_frame_num)
  959. h->frame_num_offset += max_frame_num;
  960. if (h->sps.poc_type == 0) {
  961. const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
  962. if (h->poc_lsb < h->prev_poc_lsb &&
  963. h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
  964. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  965. else if (h->poc_lsb > h->prev_poc_lsb &&
  966. h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
  967. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  968. else
  969. h->poc_msb = h->prev_poc_msb;
  970. field_poc[0] =
  971. field_poc[1] = h->poc_msb + h->poc_lsb;
  972. if (h->picture_structure == PICT_FRAME)
  973. field_poc[1] += h->delta_poc_bottom;
  974. } else if (h->sps.poc_type == 1) {
  975. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  976. int i;
  977. if (h->sps.poc_cycle_length != 0)
  978. abs_frame_num = h->frame_num_offset + h->frame_num;
  979. else
  980. abs_frame_num = 0;
  981. if (h->nal_ref_idc == 0 && abs_frame_num > 0)
  982. abs_frame_num--;
  983. expected_delta_per_poc_cycle = 0;
  984. for (i = 0; i < h->sps.poc_cycle_length; i++)
  985. // FIXME integrate during sps parse
  986. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
  987. if (abs_frame_num > 0) {
  988. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  989. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  990. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  991. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  992. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
  993. } else
  994. expectedpoc = 0;
  995. if (h->nal_ref_idc == 0)
  996. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  997. field_poc[0] = expectedpoc + h->delta_poc[0];
  998. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  999. if (h->picture_structure == PICT_FRAME)
  1000. field_poc[1] += h->delta_poc[1];
  1001. } else {
  1002. int poc = 2 * (h->frame_num_offset + h->frame_num);
  1003. if (!h->nal_ref_idc)
  1004. poc--;
  1005. field_poc[0] = poc;
  1006. field_poc[1] = poc;
  1007. }
  1008. if (h->picture_structure != PICT_BOTTOM_FIELD)
  1009. pic_field_poc[0] = field_poc[0];
  1010. if (h->picture_structure != PICT_TOP_FIELD)
  1011. pic_field_poc[1] = field_poc[1];
  1012. *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
  1013. return 0;
  1014. }
  1015. /**
  1016. * Compute profile from profile_idc and constraint_set?_flags.
  1017. *
  1018. * @param sps SPS
  1019. *
  1020. * @return profile as defined by FF_PROFILE_H264_*
  1021. */
  1022. int ff_h264_get_profile(SPS *sps)
  1023. {
  1024. int profile = sps->profile_idc;
  1025. switch (sps->profile_idc) {
  1026. case FF_PROFILE_H264_BASELINE:
  1027. // constraint_set1_flag set to 1
  1028. profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  1029. break;
  1030. case FF_PROFILE_H264_HIGH_10:
  1031. case FF_PROFILE_H264_HIGH_422:
  1032. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  1033. // constraint_set3_flag set to 1
  1034. profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
  1035. break;
  1036. }
  1037. return profile;
  1038. }
  1039. int ff_set_ref_count(H264Context *h, H264SliceContext *sl)
  1040. {
  1041. int ref_count[2], list_count;
  1042. int num_ref_idx_active_override_flag, max_refs;
  1043. // set defaults, might be overridden a few lines later
  1044. ref_count[0] = h->pps.ref_count[0];
  1045. ref_count[1] = h->pps.ref_count[1];
  1046. if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1047. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1048. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1049. num_ref_idx_active_override_flag = get_bits1(&sl->gb);
  1050. if (num_ref_idx_active_override_flag) {
  1051. ref_count[0] = get_ue_golomb(&sl->gb) + 1;
  1052. if (ref_count[0] < 1)
  1053. return AVERROR_INVALIDDATA;
  1054. if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1055. ref_count[1] = get_ue_golomb(&sl->gb) + 1;
  1056. if (ref_count[1] < 1)
  1057. return AVERROR_INVALIDDATA;
  1058. }
  1059. }
  1060. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1061. list_count = 2;
  1062. else
  1063. list_count = 1;
  1064. } else {
  1065. list_count = 0;
  1066. ref_count[0] = ref_count[1] = 0;
  1067. }
  1068. max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
  1069. if (ref_count[0] > max_refs || ref_count[1] > max_refs) {
  1070. av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
  1071. sl->ref_count[0] = sl->ref_count[1] = 0;
  1072. return AVERROR_INVALIDDATA;
  1073. }
  1074. if (list_count != sl->list_count ||
  1075. ref_count[0] != sl->ref_count[0] ||
  1076. ref_count[1] != sl->ref_count[1]) {
  1077. sl->ref_count[0] = ref_count[0];
  1078. sl->ref_count[1] = ref_count[1];
  1079. sl->list_count = list_count;
  1080. return 1;
  1081. }
  1082. return 0;
  1083. }
  1084. static int find_start_code(const uint8_t *buf, int buf_size,
  1085. int buf_index, int next_avc)
  1086. {
  1087. // start code prefix search
  1088. for (; buf_index + 3 < next_avc; buf_index++)
  1089. // This should always succeed in the first iteration.
  1090. if (buf[buf_index] == 0 &&
  1091. buf[buf_index + 1] == 0 &&
  1092. buf[buf_index + 2] == 1)
  1093. break;
  1094. if (buf_index + 3 >= buf_size)
  1095. return buf_size;
  1096. return buf_index + 3;
  1097. }
  1098. static int get_avc_nalsize(H264Context *h, const uint8_t *buf,
  1099. int buf_size, int *buf_index)
  1100. {
  1101. int i, nalsize = 0;
  1102. if (*buf_index >= buf_size - h->nal_length_size) {
  1103. // the end of the buffer is reached, refill it.
  1104. return AVERROR(EAGAIN);
  1105. }
  1106. for (i = 0; i < h->nal_length_size; i++)
  1107. nalsize = (nalsize << 8) | buf[(*buf_index)++];
  1108. if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
  1109. av_log(h->avctx, AV_LOG_ERROR,
  1110. "AVC: nal size %d\n", nalsize);
  1111. return AVERROR_INVALIDDATA;
  1112. }
  1113. return nalsize;
  1114. }
  1115. static int get_bit_length(H264Context *h, const uint8_t *buf,
  1116. const uint8_t *ptr, int dst_length,
  1117. int i, int next_avc)
  1118. {
  1119. if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
  1120. buf[i] == 0x00 && buf[i + 1] == 0x00 &&
  1121. buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
  1122. h->workaround_bugs |= FF_BUG_TRUNCATED;
  1123. if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
  1124. while (dst_length > 0 && ptr[dst_length - 1] == 0)
  1125. dst_length--;
  1126. if (!dst_length)
  1127. return 0;
  1128. return 8 * dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1);
  1129. }
  1130. static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
  1131. {
  1132. int next_avc = h->is_avc ? 0 : buf_size;
  1133. int nal_index = 0;
  1134. int buf_index = 0;
  1135. int nals_needed = 0;
  1136. while(1) {
  1137. GetBitContext gb;
  1138. int nalsize = 0;
  1139. int dst_length, bit_length, consumed;
  1140. const uint8_t *ptr;
  1141. if (buf_index >= next_avc) {
  1142. nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
  1143. if (nalsize < 0)
  1144. break;
  1145. next_avc = buf_index + nalsize;
  1146. } else {
  1147. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  1148. if (buf_index >= buf_size)
  1149. break;
  1150. }
  1151. ptr = ff_h264_decode_nal(h, &h->slice_ctx[0], buf + buf_index, &dst_length, &consumed,
  1152. next_avc - buf_index);
  1153. if (!ptr || dst_length < 0)
  1154. return AVERROR_INVALIDDATA;
  1155. buf_index += consumed;
  1156. bit_length = get_bit_length(h, buf, ptr, dst_length,
  1157. buf_index, next_avc);
  1158. nal_index++;
  1159. /* packets can sometimes contain multiple PPS/SPS,
  1160. * e.g. two PAFF field pictures in one packet, or a demuxer
  1161. * which splits NALs strangely if so, when frame threading we
  1162. * can't start the next thread until we've read all of them */
  1163. switch (h->nal_unit_type) {
  1164. case NAL_SPS:
  1165. case NAL_PPS:
  1166. nals_needed = nal_index;
  1167. break;
  1168. case NAL_DPA:
  1169. case NAL_IDR_SLICE:
  1170. case NAL_SLICE:
  1171. init_get_bits(&gb, ptr, bit_length);
  1172. if (!get_ue_golomb(&gb))
  1173. nals_needed = nal_index;
  1174. }
  1175. }
  1176. return nals_needed;
  1177. }
  1178. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  1179. int parse_extradata)
  1180. {
  1181. AVCodecContext *const avctx = h->avctx;
  1182. H264SliceContext *sl;
  1183. int buf_index;
  1184. unsigned context_count;
  1185. int next_avc;
  1186. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  1187. int nal_index;
  1188. int ret = 0;
  1189. h->max_contexts = h->slice_context_count;
  1190. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
  1191. h->current_slice = 0;
  1192. if (!h->first_field)
  1193. h->cur_pic_ptr = NULL;
  1194. ff_h264_reset_sei(h);
  1195. }
  1196. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1197. nals_needed = get_last_needed_nal(h, buf, buf_size);
  1198. {
  1199. buf_index = 0;
  1200. context_count = 0;
  1201. next_avc = h->is_avc ? 0 : buf_size;
  1202. nal_index = 0;
  1203. for (;;) {
  1204. int consumed;
  1205. int dst_length;
  1206. int bit_length;
  1207. const uint8_t *ptr;
  1208. int nalsize = 0;
  1209. int err;
  1210. if (buf_index >= next_avc) {
  1211. nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
  1212. if (nalsize < 0)
  1213. break;
  1214. next_avc = buf_index + nalsize;
  1215. } else {
  1216. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  1217. if (buf_index >= buf_size)
  1218. break;
  1219. if (buf_index >= next_avc)
  1220. continue;
  1221. }
  1222. sl = &h->slice_ctx[context_count];
  1223. ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
  1224. &consumed, next_avc - buf_index);
  1225. if (!ptr || dst_length < 0) {
  1226. ret = -1;
  1227. goto end;
  1228. }
  1229. bit_length = get_bit_length(h, buf, ptr, dst_length,
  1230. buf_index + consumed, next_avc);
  1231. if (h->avctx->debug & FF_DEBUG_STARTCODE)
  1232. av_log(h->avctx, AV_LOG_DEBUG,
  1233. "NAL %d at %d/%d length %d\n",
  1234. h->nal_unit_type, buf_index, buf_size, dst_length);
  1235. if (h->is_avc && (nalsize != consumed) && nalsize)
  1236. av_log(h->avctx, AV_LOG_DEBUG,
  1237. "AVC: Consumed only %d bytes instead of %d\n",
  1238. consumed, nalsize);
  1239. buf_index += consumed;
  1240. nal_index++;
  1241. if (avctx->skip_frame >= AVDISCARD_NONREF &&
  1242. h->nal_ref_idc == 0 &&
  1243. h->nal_unit_type != NAL_SEI)
  1244. continue;
  1245. again:
  1246. /* Ignore every NAL unit type except PPS and SPS during extradata
  1247. * parsing. Decoding slices is not possible in codec init
  1248. * with frame-mt */
  1249. if (parse_extradata && HAVE_THREADS &&
  1250. (h->avctx->active_thread_type & FF_THREAD_FRAME) &&
  1251. (h->nal_unit_type != NAL_PPS &&
  1252. h->nal_unit_type != NAL_SPS)) {
  1253. if (h->nal_unit_type < NAL_AUD ||
  1254. h->nal_unit_type > NAL_AUXILIARY_SLICE)
  1255. av_log(avctx, AV_LOG_INFO,
  1256. "Ignoring NAL unit %d during extradata parsing\n",
  1257. h->nal_unit_type);
  1258. h->nal_unit_type = NAL_FF_IGNORE;
  1259. }
  1260. err = 0;
  1261. switch (h->nal_unit_type) {
  1262. case NAL_IDR_SLICE:
  1263. if (h->nal_unit_type != NAL_IDR_SLICE) {
  1264. av_log(h->avctx, AV_LOG_ERROR,
  1265. "Invalid mix of idr and non-idr slices\n");
  1266. ret = -1;
  1267. goto end;
  1268. }
  1269. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  1270. case NAL_SLICE:
  1271. init_get_bits(&sl->gb, ptr, bit_length);
  1272. if ((err = ff_h264_decode_slice_header(h, sl)))
  1273. break;
  1274. if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
  1275. h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
  1276. ((1 << h->sps.log2_max_frame_num) - 1);
  1277. }
  1278. h->cur_pic_ptr->f->key_frame |=
  1279. (h->nal_unit_type == NAL_IDR_SLICE) ||
  1280. (h->sei_recovery_frame_cnt >= 0);
  1281. if (h->nal_unit_type == NAL_IDR_SLICE ||
  1282. h->recovery_frame == h->frame_num) {
  1283. h->recovery_frame = -1;
  1284. h->cur_pic_ptr->recovered = 1;
  1285. }
  1286. // If we have an IDR, all frames after it in decoded order are
  1287. // "recovered".
  1288. if (h->nal_unit_type == NAL_IDR_SLICE)
  1289. h->frame_recovered |= FRAME_RECOVERED_IDR;
  1290. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  1291. if (h->current_slice == 1) {
  1292. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS))
  1293. decode_postinit(h, nal_index >= nals_needed);
  1294. if (h->avctx->hwaccel &&
  1295. (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
  1296. return ret;
  1297. }
  1298. if (sl->redundant_pic_count == 0 &&
  1299. (avctx->skip_frame < AVDISCARD_NONREF ||
  1300. h->nal_ref_idc) &&
  1301. (avctx->skip_frame < AVDISCARD_BIDIR ||
  1302. sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
  1303. (avctx->skip_frame < AVDISCARD_NONKEY ||
  1304. h->cur_pic_ptr->f->key_frame) &&
  1305. avctx->skip_frame < AVDISCARD_ALL) {
  1306. if (avctx->hwaccel) {
  1307. ret = avctx->hwaccel->decode_slice(avctx,
  1308. &buf[buf_index - consumed],
  1309. consumed);
  1310. if (ret < 0)
  1311. return ret;
  1312. } else
  1313. context_count++;
  1314. }
  1315. break;
  1316. case NAL_DPA:
  1317. case NAL_DPB:
  1318. case NAL_DPC:
  1319. avpriv_request_sample(avctx, "data partitioning");
  1320. ret = AVERROR(ENOSYS);
  1321. goto end;
  1322. break;
  1323. case NAL_SEI:
  1324. init_get_bits(&h->gb, ptr, bit_length);
  1325. ret = ff_h264_decode_sei(h);
  1326. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1327. goto end;
  1328. break;
  1329. case NAL_SPS:
  1330. init_get_bits(&h->gb, ptr, bit_length);
  1331. ret = ff_h264_decode_seq_parameter_set(h);
  1332. if (ret < 0 && h->is_avc && (nalsize != consumed) && nalsize) {
  1333. av_log(h->avctx, AV_LOG_DEBUG,
  1334. "SPS decoding failure, trying again with the complete NAL\n");
  1335. init_get_bits(&h->gb, buf + buf_index + 1 - consumed,
  1336. 8 * (nalsize - 1));
  1337. ff_h264_decode_seq_parameter_set(h);
  1338. }
  1339. break;
  1340. case NAL_PPS:
  1341. init_get_bits(&h->gb, ptr, bit_length);
  1342. ret = ff_h264_decode_picture_parameter_set(h, bit_length);
  1343. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1344. goto end;
  1345. break;
  1346. case NAL_AUD:
  1347. case NAL_END_SEQUENCE:
  1348. case NAL_END_STREAM:
  1349. case NAL_FILLER_DATA:
  1350. case NAL_SPS_EXT:
  1351. case NAL_AUXILIARY_SLICE:
  1352. break;
  1353. case NAL_FF_IGNORE:
  1354. break;
  1355. default:
  1356. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  1357. h->nal_unit_type, bit_length);
  1358. }
  1359. if (context_count == h->max_contexts) {
  1360. ret = ff_h264_execute_decode_slices(h, context_count);
  1361. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1362. goto end;
  1363. context_count = 0;
  1364. }
  1365. if (err < 0) {
  1366. av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  1367. sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
  1368. } else if (err == 1) {
  1369. /* Slice could not be decoded in parallel mode, restart. Note
  1370. * that rbsp_buffer is not transferred, but since we no longer
  1371. * run in parallel mode this should not be an issue. */
  1372. sl = &h->slice_ctx[0];
  1373. goto again;
  1374. }
  1375. }
  1376. }
  1377. if (context_count) {
  1378. ret = ff_h264_execute_decode_slices(h, context_count);
  1379. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1380. goto end;
  1381. }
  1382. ret = 0;
  1383. end:
  1384. /* clean up */
  1385. if (h->cur_pic_ptr && !h->droppable) {
  1386. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1387. h->picture_structure == PICT_BOTTOM_FIELD);
  1388. }
  1389. return (ret < 0) ? ret : buf_index;
  1390. }
  1391. /**
  1392. * Return the number of bytes consumed for building the current frame.
  1393. */
  1394. static int get_consumed_bytes(int pos, int buf_size)
  1395. {
  1396. if (pos == 0)
  1397. pos = 1; // avoid infinite loops (I doubt that is needed but...)
  1398. if (pos + 10 > buf_size)
  1399. pos = buf_size; // oops ;)
  1400. return pos;
  1401. }
  1402. static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
  1403. {
  1404. int i;
  1405. int ret = av_frame_ref(dst, src);
  1406. if (ret < 0)
  1407. return ret;
  1408. if (!h->sps.crop)
  1409. return 0;
  1410. for (i = 0; i < 3; i++) {
  1411. int hshift = (i > 0) ? h->chroma_x_shift : 0;
  1412. int vshift = (i > 0) ? h->chroma_y_shift : 0;
  1413. int off = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
  1414. (h->sps.crop_top >> vshift) * dst->linesize[i];
  1415. dst->data[i] += off;
  1416. }
  1417. return 0;
  1418. }
  1419. static int h264_decode_frame(AVCodecContext *avctx, void *data,
  1420. int *got_frame, AVPacket *avpkt)
  1421. {
  1422. const uint8_t *buf = avpkt->data;
  1423. int buf_size = avpkt->size;
  1424. H264Context *h = avctx->priv_data;
  1425. AVFrame *pict = data;
  1426. int buf_index = 0;
  1427. int ret;
  1428. h->flags = avctx->flags;
  1429. h->setup_finished = 0;
  1430. /* end of stream, output what is still in the buffers */
  1431. out:
  1432. if (buf_size == 0) {
  1433. H264Picture *out;
  1434. int i, out_idx;
  1435. h->cur_pic_ptr = NULL;
  1436. // FIXME factorize this with the output code below
  1437. out = h->delayed_pic[0];
  1438. out_idx = 0;
  1439. for (i = 1;
  1440. h->delayed_pic[i] &&
  1441. !h->delayed_pic[i]->f->key_frame &&
  1442. !h->delayed_pic[i]->mmco_reset;
  1443. i++)
  1444. if (h->delayed_pic[i]->poc < out->poc) {
  1445. out = h->delayed_pic[i];
  1446. out_idx = i;
  1447. }
  1448. for (i = out_idx; h->delayed_pic[i]; i++)
  1449. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1450. if (out) {
  1451. ret = output_frame(h, pict, out->f);
  1452. if (ret < 0)
  1453. return ret;
  1454. *got_frame = 1;
  1455. }
  1456. return buf_index;
  1457. }
  1458. buf_index = decode_nal_units(h, buf, buf_size, 0);
  1459. if (buf_index < 0)
  1460. return AVERROR_INVALIDDATA;
  1461. if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  1462. buf_size = 0;
  1463. goto out;
  1464. }
  1465. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
  1466. if (avctx->skip_frame >= AVDISCARD_NONREF)
  1467. return 0;
  1468. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  1469. return AVERROR_INVALIDDATA;
  1470. }
  1471. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
  1472. (h->mb_y >= h->mb_height && h->mb_height)) {
  1473. if (avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)
  1474. decode_postinit(h, 1);
  1475. ff_h264_field_end(h, &h->slice_ctx[0], 0);
  1476. *got_frame = 0;
  1477. if (h->next_output_pic && ((avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
  1478. h->next_output_pic->recovered)) {
  1479. if (!h->next_output_pic->recovered)
  1480. h->next_output_pic->f->flags |= AV_FRAME_FLAG_CORRUPT;
  1481. ret = output_frame(h, pict, h->next_output_pic->f);
  1482. if (ret < 0)
  1483. return ret;
  1484. *got_frame = 1;
  1485. }
  1486. }
  1487. assert(pict->buf[0] || !*got_frame);
  1488. return get_consumed_bytes(buf_index, buf_size);
  1489. }
  1490. av_cold void ff_h264_free_context(H264Context *h)
  1491. {
  1492. int i;
  1493. ff_h264_free_tables(h);
  1494. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  1495. ff_h264_unref_picture(h, &h->DPB[i]);
  1496. av_frame_free(&h->DPB[i].f);
  1497. }
  1498. h->cur_pic_ptr = NULL;
  1499. for (i = 0; i < h->nb_slice_ctx; i++)
  1500. av_freep(&h->slice_ctx[i].rbsp_buffer);
  1501. av_freep(&h->slice_ctx);
  1502. h->nb_slice_ctx = 0;
  1503. for (i = 0; i < MAX_SPS_COUNT; i++)
  1504. av_freep(h->sps_buffers + i);
  1505. for (i = 0; i < MAX_PPS_COUNT; i++)
  1506. av_freep(h->pps_buffers + i);
  1507. }
  1508. static av_cold int h264_decode_end(AVCodecContext *avctx)
  1509. {
  1510. H264Context *h = avctx->priv_data;
  1511. ff_h264_free_context(h);
  1512. ff_h264_unref_picture(h, &h->cur_pic);
  1513. av_frame_free(&h->cur_pic.f);
  1514. return 0;
  1515. }
  1516. #define OFFSET(x) offsetof(H264Context, x)
  1517. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1518. static const AVOption h264_options[] = {
  1519. { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
  1520. { NULL },
  1521. };
  1522. static const AVClass h264_class = {
  1523. .class_name = "h264",
  1524. .item_name = av_default_item_name,
  1525. .option = h264_options,
  1526. .version = LIBAVUTIL_VERSION_INT,
  1527. };
  1528. static const AVProfile profiles[] = {
  1529. { FF_PROFILE_H264_BASELINE, "Baseline" },
  1530. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  1531. { FF_PROFILE_H264_MAIN, "Main" },
  1532. { FF_PROFILE_H264_EXTENDED, "Extended" },
  1533. { FF_PROFILE_H264_HIGH, "High" },
  1534. { FF_PROFILE_H264_HIGH_10, "High 10" },
  1535. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  1536. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  1537. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  1538. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  1539. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  1540. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  1541. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  1542. { FF_PROFILE_UNKNOWN },
  1543. };
  1544. AVCodec ff_h264_decoder = {
  1545. .name = "h264",
  1546. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  1547. .type = AVMEDIA_TYPE_VIDEO,
  1548. .id = AV_CODEC_ID_H264,
  1549. .priv_data_size = sizeof(H264Context),
  1550. .init = ff_h264_decode_init,
  1551. .close = h264_decode_end,
  1552. .decode = h264_decode_frame,
  1553. .capabilities = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
  1554. AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
  1555. AV_CODEC_CAP_FRAME_THREADS,
  1556. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1557. .flush = flush_dpb,
  1558. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1559. .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
  1560. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  1561. .priv_class = &h264_class,
  1562. };