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.

1827 lines
61KB

  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 + FF_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, FF_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, int free_rbsp)
  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. if (free_rbsp) {
  330. av_freep(&sl->rbsp_buffer);
  331. sl->rbsp_buffer_size = 0;
  332. }
  333. }
  334. }
  335. int ff_h264_alloc_tables(H264Context *h)
  336. {
  337. const int big_mb_num = h->mb_stride * (h->mb_height + 1);
  338. const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count;
  339. int x, y;
  340. FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
  341. row_mb_num * 8 * sizeof(uint8_t), fail)
  342. h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
  343. FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
  344. big_mb_num * 48 * sizeof(uint8_t), fail)
  345. FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
  346. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
  347. FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
  348. big_mb_num * sizeof(uint16_t), fail)
  349. FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
  350. big_mb_num * sizeof(uint8_t), fail)
  351. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
  352. 16 * row_mb_num * sizeof(uint8_t), fail);
  353. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
  354. 16 * row_mb_num * sizeof(uint8_t), fail);
  355. h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
  356. h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
  357. FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
  358. 4 * big_mb_num * sizeof(uint8_t), fail);
  359. FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
  360. big_mb_num * sizeof(uint8_t), fail)
  361. memset(h->slice_table_base, -1,
  362. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
  363. h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
  364. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
  365. big_mb_num * sizeof(uint32_t), fail);
  366. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
  367. big_mb_num * sizeof(uint32_t), fail);
  368. for (y = 0; y < h->mb_height; y++)
  369. for (x = 0; x < h->mb_width; x++) {
  370. const int mb_xy = x + y * h->mb_stride;
  371. const int b_xy = 4 * x + 4 * y * h->b_stride;
  372. h->mb2b_xy[mb_xy] = b_xy;
  373. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
  374. }
  375. if (!h->dequant4_coeff[0])
  376. h264_init_dequant_tables(h);
  377. return 0;
  378. fail:
  379. ff_h264_free_tables(h, 1);
  380. return AVERROR(ENOMEM);
  381. }
  382. /**
  383. * Init context
  384. * Allocate buffers which are not shared amongst multiple threads.
  385. */
  386. int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
  387. {
  388. ERContext *er = &sl->er;
  389. int mb_array_size = h->mb_height * h->mb_stride;
  390. int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
  391. int c_size = h->mb_stride * (h->mb_height + 1);
  392. int yc_size = y_size + 2 * c_size;
  393. int x, y, i;
  394. sl->ref_cache[0][scan8[5] + 1] =
  395. sl->ref_cache[0][scan8[7] + 1] =
  396. sl->ref_cache[0][scan8[13] + 1] =
  397. sl->ref_cache[1][scan8[5] + 1] =
  398. sl->ref_cache[1][scan8[7] + 1] =
  399. sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  400. if (CONFIG_ERROR_RESILIENCE) {
  401. /* init ER */
  402. er->avctx = h->avctx;
  403. er->decode_mb = h264_er_decode_mb;
  404. er->opaque = h;
  405. er->quarter_sample = 1;
  406. er->mb_num = h->mb_num;
  407. er->mb_width = h->mb_width;
  408. er->mb_height = h->mb_height;
  409. er->mb_stride = h->mb_stride;
  410. er->b8_stride = h->mb_width * 2 + 1;
  411. // error resilience code looks cleaner with this
  412. FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
  413. (h->mb_num + 1) * sizeof(int), fail);
  414. for (y = 0; y < h->mb_height; y++)
  415. for (x = 0; x < h->mb_width; x++)
  416. er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
  417. er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
  418. h->mb_stride + h->mb_width;
  419. FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
  420. mb_array_size * sizeof(uint8_t), fail);
  421. FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
  422. h->mb_height * h->mb_stride, fail);
  423. FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
  424. yc_size * sizeof(int16_t), fail);
  425. er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
  426. er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
  427. er->dc_val[2] = er->dc_val[1] + c_size;
  428. for (i = 0; i < yc_size; i++)
  429. sl->dc_val_base[i] = 1024;
  430. }
  431. return 0;
  432. fail:
  433. return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
  434. }
  435. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  436. int parse_extradata);
  437. int ff_h264_decode_extradata(H264Context *h)
  438. {
  439. AVCodecContext *avctx = h->avctx;
  440. int ret;
  441. if (avctx->extradata[0] == 1) {
  442. int i, cnt, nalsize;
  443. unsigned char *p = avctx->extradata;
  444. h->is_avc = 1;
  445. if (avctx->extradata_size < 7) {
  446. av_log(avctx, AV_LOG_ERROR,
  447. "avcC %d too short\n", avctx->extradata_size);
  448. return AVERROR_INVALIDDATA;
  449. }
  450. /* sps and pps in the avcC always have length coded with 2 bytes,
  451. * so put a fake nal_length_size = 2 while parsing them */
  452. h->nal_length_size = 2;
  453. // Decode sps from avcC
  454. cnt = *(p + 5) & 0x1f; // Number of sps
  455. p += 6;
  456. for (i = 0; i < cnt; i++) {
  457. nalsize = AV_RB16(p) + 2;
  458. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  459. return AVERROR_INVALIDDATA;
  460. ret = decode_nal_units(h, p, nalsize, 1);
  461. if (ret < 0) {
  462. av_log(avctx, AV_LOG_ERROR,
  463. "Decoding sps %d from avcC failed\n", i);
  464. return ret;
  465. }
  466. p += nalsize;
  467. }
  468. // Decode pps from avcC
  469. cnt = *(p++); // Number of pps
  470. for (i = 0; i < cnt; i++) {
  471. nalsize = AV_RB16(p) + 2;
  472. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  473. return AVERROR_INVALIDDATA;
  474. ret = decode_nal_units(h, p, nalsize, 1);
  475. if (ret < 0) {
  476. av_log(avctx, AV_LOG_ERROR,
  477. "Decoding pps %d from avcC failed\n", i);
  478. return ret;
  479. }
  480. p += nalsize;
  481. }
  482. // Store right nal length size that will be used to parse all other nals
  483. h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;
  484. } else {
  485. h->is_avc = 0;
  486. ret = decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1);
  487. if (ret < 0)
  488. return ret;
  489. }
  490. return 0;
  491. }
  492. static int h264_init_context(AVCodecContext *avctx, H264Context *h)
  493. {
  494. int i;
  495. h->avctx = avctx;
  496. h->dequant_coeff_pps = -1;
  497. h->cur_chroma_format_idc = -1;
  498. h->picture_structure = PICT_FRAME;
  499. h->slice_context_count = 1;
  500. h->workaround_bugs = avctx->workaround_bugs;
  501. h->flags = avctx->flags;
  502. h->prev_poc_msb = 1 << 16;
  503. h->x264_build = -1;
  504. h->recovery_frame = -1;
  505. h->frame_recovered = 0;
  506. h->next_outputed_poc = INT_MIN;
  507. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  508. h->last_pocs[i] = INT_MIN;
  509. ff_h264_reset_sei(h);
  510. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  511. h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? H264_MAX_THREADS : 1;
  512. h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
  513. if (!h->slice_ctx) {
  514. h->nb_slice_ctx = 0;
  515. return AVERROR(ENOMEM);
  516. }
  517. h->DPB = av_mallocz_array(H264_MAX_PICTURE_COUNT, sizeof(*h->DPB));
  518. if (!h->DPB)
  519. return AVERROR(ENOMEM);
  520. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  521. av_frame_unref(&h->DPB[i].f);
  522. av_frame_unref(&h->cur_pic.f);
  523. for (i = 0; i < h->nb_slice_ctx; i++)
  524. h->slice_ctx[i].h264 = h;
  525. return 0;
  526. }
  527. av_cold int ff_h264_decode_init(AVCodecContext *avctx)
  528. {
  529. H264Context *h = avctx->priv_data;
  530. int ret;
  531. ret = h264_init_context(avctx, h);
  532. if (ret < 0)
  533. return ret;
  534. /* set defaults */
  535. if (!avctx->has_b_frames)
  536. h->low_delay = 1;
  537. ff_h264_decode_init_vlc();
  538. ff_init_cabac_states();
  539. if (avctx->codec_id == AV_CODEC_ID_H264) {
  540. if (avctx->ticks_per_frame == 1)
  541. h->avctx->framerate.num *= 2;
  542. avctx->ticks_per_frame = 2;
  543. }
  544. if (avctx->extradata_size > 0 && avctx->extradata) {
  545. ret = ff_h264_decode_extradata(h);
  546. if (ret < 0) {
  547. ff_h264_free_context(h);
  548. return ret;
  549. }
  550. }
  551. if (h->sps.bitstream_restriction_flag &&
  552. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  553. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  554. h->low_delay = 0;
  555. }
  556. avctx->internal->allocate_progress = 1;
  557. if (h->enable_er) {
  558. av_log(avctx, AV_LOG_WARNING,
  559. "Error resilience is enabled. It is unsafe and unsupported and may crash. "
  560. "Use it at your own risk\n");
  561. }
  562. return 0;
  563. }
  564. static int decode_init_thread_copy(AVCodecContext *avctx)
  565. {
  566. H264Context *h = avctx->priv_data;
  567. int ret;
  568. if (!avctx->internal->is_copy)
  569. return 0;
  570. memset(h, 0, sizeof(*h));
  571. ret = h264_init_context(avctx, h);
  572. if (ret < 0)
  573. return ret;
  574. h->context_initialized = 0;
  575. return 0;
  576. }
  577. /**
  578. * Run setup operations that must be run after slice header decoding.
  579. * This includes finding the next displayed frame.
  580. *
  581. * @param h h264 master context
  582. * @param setup_finished enough NALs have been read that we can call
  583. * ff_thread_finish_setup()
  584. */
  585. static void decode_postinit(H264Context *h, int setup_finished)
  586. {
  587. H264Picture *out = h->cur_pic_ptr;
  588. H264Picture *cur = h->cur_pic_ptr;
  589. int i, pics, out_of_order, out_idx;
  590. int invalid = 0, cnt = 0;
  591. h->cur_pic_ptr->f.pict_type = h->pict_type;
  592. if (h->next_output_pic)
  593. return;
  594. if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
  595. /* FIXME: if we have two PAFF fields in one packet, we can't start
  596. * the next thread here. If we have one field per packet, we can.
  597. * The check in decode_nal_units() is not good enough to find this
  598. * yet, so we assume the worst for now. */
  599. // if (setup_finished)
  600. // ff_thread_finish_setup(h->avctx);
  601. return;
  602. }
  603. cur->f.interlaced_frame = 0;
  604. cur->f.repeat_pict = 0;
  605. /* Signal interlacing information externally. */
  606. /* Prioritize picture timing SEI information over used
  607. * decoding process if it exists. */
  608. if (h->sps.pic_struct_present_flag) {
  609. switch (h->sei_pic_struct) {
  610. case SEI_PIC_STRUCT_FRAME:
  611. break;
  612. case SEI_PIC_STRUCT_TOP_FIELD:
  613. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  614. cur->f.interlaced_frame = 1;
  615. break;
  616. case SEI_PIC_STRUCT_TOP_BOTTOM:
  617. case SEI_PIC_STRUCT_BOTTOM_TOP:
  618. if (FIELD_OR_MBAFF_PICTURE(h))
  619. cur->f.interlaced_frame = 1;
  620. else
  621. // try to flag soft telecine progressive
  622. cur->f.interlaced_frame = h->prev_interlaced_frame;
  623. break;
  624. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  625. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  626. /* Signal the possibility of telecined film externally
  627. * (pic_struct 5,6). From these hints, let the applications
  628. * decide if they apply deinterlacing. */
  629. cur->f.repeat_pict = 1;
  630. break;
  631. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  632. cur->f.repeat_pict = 2;
  633. break;
  634. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  635. cur->f.repeat_pict = 4;
  636. break;
  637. }
  638. if ((h->sei_ct_type & 3) &&
  639. h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  640. cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
  641. } else {
  642. /* Derive interlacing flag from used decoding process. */
  643. cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
  644. }
  645. h->prev_interlaced_frame = cur->f.interlaced_frame;
  646. if (cur->field_poc[0] != cur->field_poc[1]) {
  647. /* Derive top_field_first from field pocs. */
  648. cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
  649. } else {
  650. if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
  651. /* Use picture timing SEI information. Even if it is a
  652. * information of a past frame, better than nothing. */
  653. if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
  654. h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  655. cur->f.top_field_first = 1;
  656. else
  657. cur->f.top_field_first = 0;
  658. } else {
  659. /* Most likely progressive */
  660. cur->f.top_field_first = 0;
  661. }
  662. }
  663. if (h->sei_frame_packing_present &&
  664. h->frame_packing_arrangement_type >= 0 &&
  665. h->frame_packing_arrangement_type <= 6 &&
  666. h->content_interpretation_type > 0 &&
  667. h->content_interpretation_type < 3) {
  668. AVStereo3D *stereo = av_stereo3d_create_side_data(&cur->f);
  669. if (!stereo)
  670. return;
  671. switch (h->frame_packing_arrangement_type) {
  672. case 0:
  673. stereo->type = AV_STEREO3D_CHECKERBOARD;
  674. break;
  675. case 1:
  676. stereo->type = AV_STEREO3D_COLUMNS;
  677. break;
  678. case 2:
  679. stereo->type = AV_STEREO3D_LINES;
  680. break;
  681. case 3:
  682. if (h->quincunx_subsampling)
  683. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  684. else
  685. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  686. break;
  687. case 4:
  688. stereo->type = AV_STEREO3D_TOPBOTTOM;
  689. break;
  690. case 5:
  691. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  692. break;
  693. case 6:
  694. stereo->type = AV_STEREO3D_2D;
  695. break;
  696. }
  697. if (h->content_interpretation_type == 2)
  698. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  699. }
  700. if (h->sei_display_orientation_present &&
  701. (h->sei_anticlockwise_rotation || h->sei_hflip || h->sei_vflip)) {
  702. double angle = h->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
  703. AVFrameSideData *rotation = av_frame_new_side_data(&cur->f,
  704. AV_FRAME_DATA_DISPLAYMATRIX,
  705. sizeof(int32_t) * 9);
  706. if (!rotation)
  707. return;
  708. av_display_rotation_set((int32_t *)rotation->data, angle);
  709. av_display_matrix_flip((int32_t *)rotation->data,
  710. h->sei_hflip, h->sei_vflip);
  711. }
  712. // FIXME do something with unavailable reference frames
  713. /* Sort B-frames into display order */
  714. if (h->sps.bitstream_restriction_flag &&
  715. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  716. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  717. h->low_delay = 0;
  718. }
  719. if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
  720. !h->sps.bitstream_restriction_flag) {
  721. h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
  722. h->low_delay = 0;
  723. }
  724. pics = 0;
  725. while (h->delayed_pic[pics])
  726. pics++;
  727. assert(pics <= MAX_DELAYED_PIC_COUNT);
  728. h->delayed_pic[pics++] = cur;
  729. if (cur->reference == 0)
  730. cur->reference = DELAYED_PIC_REF;
  731. /* Frame reordering. This code takes pictures from coding order and sorts
  732. * them by their incremental POC value into display order. It supports POC
  733. * gaps, MMCO reset codes and random resets.
  734. * A "display group" can start either with a IDR frame (f.key_frame = 1),
  735. * and/or can be closed down with a MMCO reset code. In sequences where
  736. * there is no delay, we can't detect that (since the frame was already
  737. * output to the user), so we also set h->mmco_reset to detect the MMCO
  738. * reset code.
  739. * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
  740. * we increase the delay between input and output. All frames affected by
  741. * the lag (e.g. those that should have been output before another frame
  742. * that we already returned to the user) will be dropped. This is a bug
  743. * that we will fix later. */
  744. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
  745. cnt += out->poc < h->last_pocs[i];
  746. invalid += out->poc == INT_MIN;
  747. }
  748. if (!h->mmco_reset && !cur->f.key_frame &&
  749. cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
  750. h->mmco_reset = 2;
  751. if (pics > 1)
  752. h->delayed_pic[pics - 2]->mmco_reset = 2;
  753. }
  754. if (h->mmco_reset || cur->f.key_frame) {
  755. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  756. h->last_pocs[i] = INT_MIN;
  757. cnt = 0;
  758. invalid = MAX_DELAYED_PIC_COUNT;
  759. }
  760. out = h->delayed_pic[0];
  761. out_idx = 0;
  762. for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
  763. h->delayed_pic[i] &&
  764. !h->delayed_pic[i - 1]->mmco_reset &&
  765. !h->delayed_pic[i]->f.key_frame;
  766. i++)
  767. if (h->delayed_pic[i]->poc < out->poc) {
  768. out = h->delayed_pic[i];
  769. out_idx = i;
  770. }
  771. if (h->avctx->has_b_frames == 0 &&
  772. (h->delayed_pic[0]->f.key_frame || h->mmco_reset))
  773. h->next_outputed_poc = INT_MIN;
  774. out_of_order = !out->f.key_frame && !h->mmco_reset &&
  775. (out->poc < h->next_outputed_poc);
  776. if (h->sps.bitstream_restriction_flag &&
  777. h->avctx->has_b_frames >= h->sps.num_reorder_frames) {
  778. } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
  779. h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
  780. if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
  781. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
  782. }
  783. h->low_delay = 0;
  784. } else if (h->low_delay &&
  785. ((h->next_outputed_poc != INT_MIN &&
  786. out->poc > h->next_outputed_poc + 2) ||
  787. cur->f.pict_type == AV_PICTURE_TYPE_B)) {
  788. h->low_delay = 0;
  789. h->avctx->has_b_frames++;
  790. }
  791. if (pics > h->avctx->has_b_frames) {
  792. out->reference &= ~DELAYED_PIC_REF;
  793. // for frame threading, the owner must be the second field's thread or
  794. // else the first thread can release the picture and reuse it unsafely
  795. for (i = out_idx; h->delayed_pic[i]; i++)
  796. h->delayed_pic[i] = h->delayed_pic[i + 1];
  797. }
  798. memmove(h->last_pocs, &h->last_pocs[1],
  799. sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
  800. h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
  801. if (!out_of_order && pics > h->avctx->has_b_frames) {
  802. h->next_output_pic = out;
  803. if (out->mmco_reset) {
  804. if (out_idx > 0) {
  805. h->next_outputed_poc = out->poc;
  806. h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
  807. } else {
  808. h->next_outputed_poc = INT_MIN;
  809. }
  810. } else {
  811. if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f.key_frame) {
  812. h->next_outputed_poc = INT_MIN;
  813. } else {
  814. h->next_outputed_poc = out->poc;
  815. }
  816. }
  817. h->mmco_reset = 0;
  818. } else {
  819. av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
  820. }
  821. if (h->next_output_pic) {
  822. if (h->next_output_pic->recovered) {
  823. // We have reached an recovery point and all frames after it in
  824. // display order are "recovered".
  825. h->frame_recovered |= FRAME_RECOVERED_SEI;
  826. }
  827. h->next_output_pic->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
  828. }
  829. if (setup_finished && !h->avctx->hwaccel)
  830. ff_thread_finish_setup(h->avctx);
  831. }
  832. int ff_pred_weight_table(H264Context *h, H264SliceContext *sl)
  833. {
  834. int list, i;
  835. int luma_def, chroma_def;
  836. sl->use_weight = 0;
  837. sl->use_weight_chroma = 0;
  838. sl->luma_log2_weight_denom = get_ue_golomb(&sl->gb);
  839. if (h->sps.chroma_format_idc)
  840. sl->chroma_log2_weight_denom = get_ue_golomb(&sl->gb);
  841. luma_def = 1 << sl->luma_log2_weight_denom;
  842. chroma_def = 1 << sl->chroma_log2_weight_denom;
  843. for (list = 0; list < 2; list++) {
  844. sl->luma_weight_flag[list] = 0;
  845. sl->chroma_weight_flag[list] = 0;
  846. for (i = 0; i < sl->ref_count[list]; i++) {
  847. int luma_weight_flag, chroma_weight_flag;
  848. luma_weight_flag = get_bits1(&sl->gb);
  849. if (luma_weight_flag) {
  850. sl->luma_weight[i][list][0] = get_se_golomb(&sl->gb);
  851. sl->luma_weight[i][list][1] = get_se_golomb(&sl->gb);
  852. if (sl->luma_weight[i][list][0] != luma_def ||
  853. sl->luma_weight[i][list][1] != 0) {
  854. sl->use_weight = 1;
  855. sl->luma_weight_flag[list] = 1;
  856. }
  857. } else {
  858. sl->luma_weight[i][list][0] = luma_def;
  859. sl->luma_weight[i][list][1] = 0;
  860. }
  861. if (h->sps.chroma_format_idc) {
  862. chroma_weight_flag = get_bits1(&sl->gb);
  863. if (chroma_weight_flag) {
  864. int j;
  865. for (j = 0; j < 2; j++) {
  866. sl->chroma_weight[i][list][j][0] = get_se_golomb(&sl->gb);
  867. sl->chroma_weight[i][list][j][1] = get_se_golomb(&sl->gb);
  868. if (sl->chroma_weight[i][list][j][0] != chroma_def ||
  869. sl->chroma_weight[i][list][j][1] != 0) {
  870. sl->use_weight_chroma = 1;
  871. sl->chroma_weight_flag[list] = 1;
  872. }
  873. }
  874. } else {
  875. int j;
  876. for (j = 0; j < 2; j++) {
  877. sl->chroma_weight[i][list][j][0] = chroma_def;
  878. sl->chroma_weight[i][list][j][1] = 0;
  879. }
  880. }
  881. }
  882. }
  883. if (sl->slice_type_nos != AV_PICTURE_TYPE_B)
  884. break;
  885. }
  886. sl->use_weight = sl->use_weight || sl->use_weight_chroma;
  887. return 0;
  888. }
  889. /**
  890. * instantaneous decoder refresh.
  891. */
  892. static void idr(H264Context *h)
  893. {
  894. ff_h264_remove_all_refs(h);
  895. h->prev_frame_num =
  896. h->prev_frame_num_offset =
  897. h->prev_poc_msb =
  898. h->prev_poc_lsb = 0;
  899. }
  900. /* forget old pics after a seek */
  901. void ff_h264_flush_change(H264Context *h)
  902. {
  903. int i;
  904. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  905. h->last_pocs[i] = INT_MIN;
  906. h->next_outputed_poc = INT_MIN;
  907. h->prev_interlaced_frame = 1;
  908. idr(h);
  909. if (h->cur_pic_ptr)
  910. h->cur_pic_ptr->reference = 0;
  911. h->first_field = 0;
  912. ff_h264_reset_sei(h);
  913. h->recovery_frame = -1;
  914. h->frame_recovered = 0;
  915. }
  916. /* forget old pics after a seek */
  917. static void flush_dpb(AVCodecContext *avctx)
  918. {
  919. H264Context *h = avctx->priv_data;
  920. int i;
  921. memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
  922. ff_h264_flush_change(h);
  923. if (h->DPB)
  924. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  925. ff_h264_unref_picture(h, &h->DPB[i]);
  926. h->cur_pic_ptr = NULL;
  927. ff_h264_unref_picture(h, &h->cur_pic);
  928. h->mb_y = 0;
  929. ff_h264_free_tables(h, 1);
  930. h->context_initialized = 0;
  931. }
  932. int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
  933. {
  934. const int max_frame_num = 1 << h->sps.log2_max_frame_num;
  935. int field_poc[2];
  936. h->frame_num_offset = h->prev_frame_num_offset;
  937. if (h->frame_num < h->prev_frame_num)
  938. h->frame_num_offset += max_frame_num;
  939. if (h->sps.poc_type == 0) {
  940. const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
  941. if (h->poc_lsb < h->prev_poc_lsb &&
  942. h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
  943. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  944. else if (h->poc_lsb > h->prev_poc_lsb &&
  945. h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
  946. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  947. else
  948. h->poc_msb = h->prev_poc_msb;
  949. field_poc[0] =
  950. field_poc[1] = h->poc_msb + h->poc_lsb;
  951. if (h->picture_structure == PICT_FRAME)
  952. field_poc[1] += h->delta_poc_bottom;
  953. } else if (h->sps.poc_type == 1) {
  954. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  955. int i;
  956. if (h->sps.poc_cycle_length != 0)
  957. abs_frame_num = h->frame_num_offset + h->frame_num;
  958. else
  959. abs_frame_num = 0;
  960. if (h->nal_ref_idc == 0 && abs_frame_num > 0)
  961. abs_frame_num--;
  962. expected_delta_per_poc_cycle = 0;
  963. for (i = 0; i < h->sps.poc_cycle_length; i++)
  964. // FIXME integrate during sps parse
  965. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
  966. if (abs_frame_num > 0) {
  967. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  968. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  969. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  970. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  971. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
  972. } else
  973. expectedpoc = 0;
  974. if (h->nal_ref_idc == 0)
  975. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  976. field_poc[0] = expectedpoc + h->delta_poc[0];
  977. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  978. if (h->picture_structure == PICT_FRAME)
  979. field_poc[1] += h->delta_poc[1];
  980. } else {
  981. int poc = 2 * (h->frame_num_offset + h->frame_num);
  982. if (!h->nal_ref_idc)
  983. poc--;
  984. field_poc[0] = poc;
  985. field_poc[1] = poc;
  986. }
  987. if (h->picture_structure != PICT_BOTTOM_FIELD)
  988. pic_field_poc[0] = field_poc[0];
  989. if (h->picture_structure != PICT_TOP_FIELD)
  990. pic_field_poc[1] = field_poc[1];
  991. *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
  992. return 0;
  993. }
  994. /**
  995. * Compute profile from profile_idc and constraint_set?_flags.
  996. *
  997. * @param sps SPS
  998. *
  999. * @return profile as defined by FF_PROFILE_H264_*
  1000. */
  1001. int ff_h264_get_profile(SPS *sps)
  1002. {
  1003. int profile = sps->profile_idc;
  1004. switch (sps->profile_idc) {
  1005. case FF_PROFILE_H264_BASELINE:
  1006. // constraint_set1_flag set to 1
  1007. profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  1008. break;
  1009. case FF_PROFILE_H264_HIGH_10:
  1010. case FF_PROFILE_H264_HIGH_422:
  1011. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  1012. // constraint_set3_flag set to 1
  1013. profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
  1014. break;
  1015. }
  1016. return profile;
  1017. }
  1018. int ff_h264_set_parameter_from_sps(H264Context *h)
  1019. {
  1020. if (h->flags & CODEC_FLAG_LOW_DELAY ||
  1021. (h->sps.bitstream_restriction_flag &&
  1022. !h->sps.num_reorder_frames)) {
  1023. if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
  1024. av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
  1025. "Reenabling low delay requires a codec flush.\n");
  1026. else
  1027. h->low_delay = 1;
  1028. }
  1029. if (h->avctx->has_b_frames < 2)
  1030. h->avctx->has_b_frames = !h->low_delay;
  1031. if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
  1032. h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
  1033. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  1034. h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  1035. h->cur_chroma_format_idc = h->sps.chroma_format_idc;
  1036. h->pixel_shift = h->sps.bit_depth_luma > 8;
  1037. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma,
  1038. h->sps.chroma_format_idc);
  1039. ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
  1040. ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
  1041. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
  1042. h->sps.chroma_format_idc);
  1043. ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
  1044. } else {
  1045. av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
  1046. h->sps.bit_depth_luma);
  1047. return AVERROR_INVALIDDATA;
  1048. }
  1049. }
  1050. return 0;
  1051. }
  1052. int ff_set_ref_count(H264Context *h, H264SliceContext *sl)
  1053. {
  1054. int ref_count[2], list_count;
  1055. int num_ref_idx_active_override_flag, max_refs;
  1056. // set defaults, might be overridden a few lines later
  1057. ref_count[0] = h->pps.ref_count[0];
  1058. ref_count[1] = h->pps.ref_count[1];
  1059. if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1060. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1061. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1062. num_ref_idx_active_override_flag = get_bits1(&sl->gb);
  1063. if (num_ref_idx_active_override_flag) {
  1064. ref_count[0] = get_ue_golomb(&sl->gb) + 1;
  1065. if (ref_count[0] < 1)
  1066. return AVERROR_INVALIDDATA;
  1067. if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1068. ref_count[1] = get_ue_golomb(&sl->gb) + 1;
  1069. if (ref_count[1] < 1)
  1070. return AVERROR_INVALIDDATA;
  1071. }
  1072. }
  1073. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1074. list_count = 2;
  1075. else
  1076. list_count = 1;
  1077. } else {
  1078. list_count = 0;
  1079. ref_count[0] = ref_count[1] = 0;
  1080. }
  1081. max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
  1082. if (ref_count[0] > max_refs || ref_count[1] > max_refs) {
  1083. av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
  1084. sl->ref_count[0] = sl->ref_count[1] = 0;
  1085. return AVERROR_INVALIDDATA;
  1086. }
  1087. if (list_count != sl->list_count ||
  1088. ref_count[0] != sl->ref_count[0] ||
  1089. ref_count[1] != sl->ref_count[1]) {
  1090. sl->ref_count[0] = ref_count[0];
  1091. sl->ref_count[1] = ref_count[1];
  1092. sl->list_count = list_count;
  1093. return 1;
  1094. }
  1095. return 0;
  1096. }
  1097. static int find_start_code(const uint8_t *buf, int buf_size,
  1098. int buf_index, int next_avc)
  1099. {
  1100. // start code prefix search
  1101. for (; buf_index + 3 < next_avc; buf_index++)
  1102. // This should always succeed in the first iteration.
  1103. if (buf[buf_index] == 0 &&
  1104. buf[buf_index + 1] == 0 &&
  1105. buf[buf_index + 2] == 1)
  1106. break;
  1107. if (buf_index + 3 >= buf_size)
  1108. return buf_size;
  1109. return buf_index + 3;
  1110. }
  1111. static int get_avc_nalsize(H264Context *h, const uint8_t *buf,
  1112. int buf_size, int *buf_index)
  1113. {
  1114. int i, nalsize = 0;
  1115. if (*buf_index >= buf_size - h->nal_length_size)
  1116. return -1;
  1117. for (i = 0; i < h->nal_length_size; i++)
  1118. nalsize = (nalsize << 8) | buf[(*buf_index)++];
  1119. if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
  1120. av_log(h->avctx, AV_LOG_ERROR,
  1121. "AVC: nal size %d\n", nalsize);
  1122. return -1;
  1123. }
  1124. return nalsize;
  1125. }
  1126. static int get_bit_length(H264Context *h, const uint8_t *buf,
  1127. const uint8_t *ptr, int dst_length,
  1128. int i, int next_avc)
  1129. {
  1130. if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
  1131. buf[i] == 0x00 && buf[i + 1] == 0x00 &&
  1132. buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
  1133. h->workaround_bugs |= FF_BUG_TRUNCATED;
  1134. if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
  1135. while (dst_length > 0 && ptr[dst_length - 1] == 0)
  1136. dst_length--;
  1137. if (!dst_length)
  1138. return 0;
  1139. return 8 * dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1);
  1140. }
  1141. static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
  1142. {
  1143. int next_avc = h->is_avc ? 0 : buf_size;
  1144. int nal_index = 0;
  1145. int buf_index = 0;
  1146. int nals_needed = 0;
  1147. while(1) {
  1148. GetBitContext gb;
  1149. int nalsize = 0;
  1150. int dst_length, bit_length, consumed;
  1151. const uint8_t *ptr;
  1152. if (buf_index >= next_avc) {
  1153. nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
  1154. if (nalsize < 0)
  1155. break;
  1156. next_avc = buf_index + nalsize;
  1157. } else {
  1158. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  1159. if (buf_index >= buf_size)
  1160. break;
  1161. }
  1162. ptr = ff_h264_decode_nal(h, &h->slice_ctx[0], buf + buf_index, &dst_length, &consumed,
  1163. next_avc - buf_index);
  1164. if (!ptr || dst_length < 0)
  1165. return AVERROR_INVALIDDATA;
  1166. buf_index += consumed;
  1167. bit_length = get_bit_length(h, buf, ptr, dst_length,
  1168. buf_index, next_avc);
  1169. nal_index++;
  1170. /* packets can sometimes contain multiple PPS/SPS,
  1171. * e.g. two PAFF field pictures in one packet, or a demuxer
  1172. * which splits NALs strangely if so, when frame threading we
  1173. * can't start the next thread until we've read all of them */
  1174. switch (h->nal_unit_type) {
  1175. case NAL_SPS:
  1176. case NAL_PPS:
  1177. nals_needed = nal_index;
  1178. break;
  1179. case NAL_DPA:
  1180. case NAL_IDR_SLICE:
  1181. case NAL_SLICE:
  1182. init_get_bits(&gb, ptr, bit_length);
  1183. if (!get_ue_golomb(&gb))
  1184. nals_needed = nal_index;
  1185. }
  1186. }
  1187. return nals_needed;
  1188. }
  1189. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  1190. int parse_extradata)
  1191. {
  1192. AVCodecContext *const avctx = h->avctx;
  1193. H264SliceContext *sl;
  1194. int buf_index;
  1195. unsigned context_count;
  1196. int next_avc;
  1197. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  1198. int nal_index;
  1199. int ret = 0;
  1200. h->max_contexts = h->slice_context_count;
  1201. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
  1202. h->current_slice = 0;
  1203. if (!h->first_field)
  1204. h->cur_pic_ptr = NULL;
  1205. ff_h264_reset_sei(h);
  1206. }
  1207. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1208. nals_needed = get_last_needed_nal(h, buf, buf_size);
  1209. {
  1210. buf_index = 0;
  1211. context_count = 0;
  1212. next_avc = h->is_avc ? 0 : buf_size;
  1213. nal_index = 0;
  1214. for (;;) {
  1215. int consumed;
  1216. int dst_length;
  1217. int bit_length;
  1218. const uint8_t *ptr;
  1219. int nalsize = 0;
  1220. int err;
  1221. if (buf_index >= next_avc) {
  1222. nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
  1223. if (nalsize < 0)
  1224. break;
  1225. next_avc = buf_index + nalsize;
  1226. } else {
  1227. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  1228. if (buf_index >= buf_size)
  1229. break;
  1230. if (buf_index >= next_avc)
  1231. continue;
  1232. }
  1233. sl = &h->slice_ctx[context_count];
  1234. ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
  1235. &consumed, next_avc - buf_index);
  1236. if (!ptr || dst_length < 0) {
  1237. ret = -1;
  1238. goto end;
  1239. }
  1240. bit_length = get_bit_length(h, buf, ptr, dst_length,
  1241. buf_index + consumed, next_avc);
  1242. if (h->avctx->debug & FF_DEBUG_STARTCODE)
  1243. av_log(h->avctx, AV_LOG_DEBUG,
  1244. "NAL %d at %d/%d length %d\n",
  1245. h->nal_unit_type, buf_index, buf_size, dst_length);
  1246. if (h->is_avc && (nalsize != consumed) && nalsize)
  1247. av_log(h->avctx, AV_LOG_DEBUG,
  1248. "AVC: Consumed only %d bytes instead of %d\n",
  1249. consumed, nalsize);
  1250. buf_index += consumed;
  1251. nal_index++;
  1252. if (avctx->skip_frame >= AVDISCARD_NONREF &&
  1253. h->nal_ref_idc == 0 &&
  1254. h->nal_unit_type != NAL_SEI)
  1255. continue;
  1256. again:
  1257. /* Ignore every NAL unit type except PPS and SPS during extradata
  1258. * parsing. Decoding slices is not possible in codec init
  1259. * with frame-mt */
  1260. if (parse_extradata && HAVE_THREADS &&
  1261. (h->avctx->active_thread_type & FF_THREAD_FRAME) &&
  1262. (h->nal_unit_type != NAL_PPS &&
  1263. h->nal_unit_type != NAL_SPS)) {
  1264. if (h->nal_unit_type < NAL_AUD ||
  1265. h->nal_unit_type > NAL_AUXILIARY_SLICE)
  1266. av_log(avctx, AV_LOG_INFO,
  1267. "Ignoring NAL unit %d during extradata parsing\n",
  1268. h->nal_unit_type);
  1269. h->nal_unit_type = NAL_FF_IGNORE;
  1270. }
  1271. err = 0;
  1272. switch (h->nal_unit_type) {
  1273. case NAL_IDR_SLICE:
  1274. if (h->nal_unit_type != NAL_IDR_SLICE) {
  1275. av_log(h->avctx, AV_LOG_ERROR,
  1276. "Invalid mix of idr and non-idr slices\n");
  1277. ret = -1;
  1278. goto end;
  1279. }
  1280. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  1281. case NAL_SLICE:
  1282. init_get_bits(&sl->gb, ptr, bit_length);
  1283. if ((err = ff_h264_decode_slice_header(h, sl)))
  1284. break;
  1285. if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
  1286. h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
  1287. ((1 << h->sps.log2_max_frame_num) - 1);
  1288. }
  1289. h->cur_pic_ptr->f.key_frame |=
  1290. (h->nal_unit_type == NAL_IDR_SLICE) ||
  1291. (h->sei_recovery_frame_cnt >= 0);
  1292. if (h->nal_unit_type == NAL_IDR_SLICE ||
  1293. h->recovery_frame == h->frame_num) {
  1294. h->recovery_frame = -1;
  1295. h->cur_pic_ptr->recovered = 1;
  1296. }
  1297. // If we have an IDR, all frames after it in decoded order are
  1298. // "recovered".
  1299. if (h->nal_unit_type == NAL_IDR_SLICE)
  1300. h->frame_recovered |= FRAME_RECOVERED_IDR;
  1301. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  1302. if (h->current_slice == 1) {
  1303. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
  1304. decode_postinit(h, nal_index >= nals_needed);
  1305. if (h->avctx->hwaccel &&
  1306. (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
  1307. return ret;
  1308. }
  1309. if (sl->redundant_pic_count == 0 &&
  1310. (avctx->skip_frame < AVDISCARD_NONREF ||
  1311. h->nal_ref_idc) &&
  1312. (avctx->skip_frame < AVDISCARD_BIDIR ||
  1313. sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
  1314. (avctx->skip_frame < AVDISCARD_NONKEY ||
  1315. sl->slice_type_nos == AV_PICTURE_TYPE_I) &&
  1316. avctx->skip_frame < AVDISCARD_ALL) {
  1317. if (avctx->hwaccel) {
  1318. ret = avctx->hwaccel->decode_slice(avctx,
  1319. &buf[buf_index - consumed],
  1320. consumed);
  1321. if (ret < 0)
  1322. return ret;
  1323. } else
  1324. context_count++;
  1325. }
  1326. break;
  1327. case NAL_DPA:
  1328. case NAL_DPB:
  1329. case NAL_DPC:
  1330. avpriv_request_sample(avctx, "data partitioning");
  1331. ret = AVERROR(ENOSYS);
  1332. goto end;
  1333. break;
  1334. case NAL_SEI:
  1335. init_get_bits(&h->gb, ptr, bit_length);
  1336. ret = ff_h264_decode_sei(h);
  1337. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1338. goto end;
  1339. break;
  1340. case NAL_SPS:
  1341. init_get_bits(&h->gb, ptr, bit_length);
  1342. ret = ff_h264_decode_seq_parameter_set(h);
  1343. if (ret < 0 && h->is_avc && (nalsize != consumed) && nalsize) {
  1344. av_log(h->avctx, AV_LOG_DEBUG,
  1345. "SPS decoding failure, trying again with the complete NAL\n");
  1346. init_get_bits(&h->gb, buf + buf_index + 1 - consumed,
  1347. 8 * (nalsize - 1));
  1348. ff_h264_decode_seq_parameter_set(h);
  1349. }
  1350. ret = ff_h264_set_parameter_from_sps(h);
  1351. if (ret < 0)
  1352. goto end;
  1353. break;
  1354. case NAL_PPS:
  1355. init_get_bits(&h->gb, ptr, bit_length);
  1356. ret = ff_h264_decode_picture_parameter_set(h, bit_length);
  1357. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1358. goto end;
  1359. break;
  1360. case NAL_AUD:
  1361. case NAL_END_SEQUENCE:
  1362. case NAL_END_STREAM:
  1363. case NAL_FILLER_DATA:
  1364. case NAL_SPS_EXT:
  1365. case NAL_AUXILIARY_SLICE:
  1366. break;
  1367. case NAL_FF_IGNORE:
  1368. break;
  1369. default:
  1370. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  1371. h->nal_unit_type, bit_length);
  1372. }
  1373. if (context_count == h->max_contexts) {
  1374. ret = ff_h264_execute_decode_slices(h, context_count);
  1375. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1376. goto end;
  1377. context_count = 0;
  1378. }
  1379. if (err < 0) {
  1380. av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  1381. sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
  1382. } else if (err == 1) {
  1383. /* Slice could not be decoded in parallel mode, restart. Note
  1384. * that rbsp_buffer is not transferred, but since we no longer
  1385. * run in parallel mode this should not be an issue. */
  1386. sl = &h->slice_ctx[0];
  1387. goto again;
  1388. }
  1389. }
  1390. }
  1391. if (context_count) {
  1392. ret = ff_h264_execute_decode_slices(h, context_count);
  1393. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1394. goto end;
  1395. }
  1396. ret = 0;
  1397. end:
  1398. /* clean up */
  1399. if (h->cur_pic_ptr && !h->droppable) {
  1400. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1401. h->picture_structure == PICT_BOTTOM_FIELD);
  1402. }
  1403. return (ret < 0) ? ret : buf_index;
  1404. }
  1405. /**
  1406. * Return the number of bytes consumed for building the current frame.
  1407. */
  1408. static int get_consumed_bytes(int pos, int buf_size)
  1409. {
  1410. if (pos == 0)
  1411. pos = 1; // avoid infinite loops (I doubt that is needed but...)
  1412. if (pos + 10 > buf_size)
  1413. pos = buf_size; // oops ;)
  1414. return pos;
  1415. }
  1416. static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
  1417. {
  1418. int i;
  1419. int ret = av_frame_ref(dst, src);
  1420. if (ret < 0)
  1421. return ret;
  1422. if (!h->sps.crop)
  1423. return 0;
  1424. for (i = 0; i < 3; i++) {
  1425. int hshift = (i > 0) ? h->chroma_x_shift : 0;
  1426. int vshift = (i > 0) ? h->chroma_y_shift : 0;
  1427. int off = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
  1428. (h->sps.crop_top >> vshift) * dst->linesize[i];
  1429. dst->data[i] += off;
  1430. }
  1431. return 0;
  1432. }
  1433. static int h264_decode_frame(AVCodecContext *avctx, void *data,
  1434. int *got_frame, AVPacket *avpkt)
  1435. {
  1436. const uint8_t *buf = avpkt->data;
  1437. int buf_size = avpkt->size;
  1438. H264Context *h = avctx->priv_data;
  1439. AVFrame *pict = data;
  1440. int buf_index = 0;
  1441. int ret;
  1442. h->flags = avctx->flags;
  1443. /* end of stream, output what is still in the buffers */
  1444. out:
  1445. if (buf_size == 0) {
  1446. H264Picture *out;
  1447. int i, out_idx;
  1448. h->cur_pic_ptr = NULL;
  1449. // FIXME factorize this with the output code below
  1450. out = h->delayed_pic[0];
  1451. out_idx = 0;
  1452. for (i = 1;
  1453. h->delayed_pic[i] &&
  1454. !h->delayed_pic[i]->f.key_frame &&
  1455. !h->delayed_pic[i]->mmco_reset;
  1456. i++)
  1457. if (h->delayed_pic[i]->poc < out->poc) {
  1458. out = h->delayed_pic[i];
  1459. out_idx = i;
  1460. }
  1461. for (i = out_idx; h->delayed_pic[i]; i++)
  1462. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1463. if (out) {
  1464. ret = output_frame(h, pict, &out->f);
  1465. if (ret < 0)
  1466. return ret;
  1467. *got_frame = 1;
  1468. }
  1469. return buf_index;
  1470. }
  1471. buf_index = decode_nal_units(h, buf, buf_size, 0);
  1472. if (buf_index < 0)
  1473. return AVERROR_INVALIDDATA;
  1474. if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  1475. buf_size = 0;
  1476. goto out;
  1477. }
  1478. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
  1479. if (avctx->skip_frame >= AVDISCARD_NONREF)
  1480. return 0;
  1481. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  1482. return AVERROR_INVALIDDATA;
  1483. }
  1484. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
  1485. (h->mb_y >= h->mb_height && h->mb_height)) {
  1486. if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
  1487. decode_postinit(h, 1);
  1488. ff_h264_field_end(h, &h->slice_ctx[0], 0);
  1489. *got_frame = 0;
  1490. if (h->next_output_pic && ((avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT) ||
  1491. h->next_output_pic->recovered)) {
  1492. if (!h->next_output_pic->recovered)
  1493. h->next_output_pic->f.flags |= AV_FRAME_FLAG_CORRUPT;
  1494. ret = output_frame(h, pict, &h->next_output_pic->f);
  1495. if (ret < 0)
  1496. return ret;
  1497. *got_frame = 1;
  1498. }
  1499. }
  1500. assert(pict->buf[0] || !*got_frame);
  1501. return get_consumed_bytes(buf_index, buf_size);
  1502. }
  1503. av_cold void ff_h264_free_context(H264Context *h)
  1504. {
  1505. int i;
  1506. ff_h264_free_tables(h, 1); // FIXME cleanup init stuff perhaps
  1507. if (h->DPB) {
  1508. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  1509. ff_h264_unref_picture(h, &h->DPB[i]);
  1510. av_freep(&h->DPB);
  1511. }
  1512. h->cur_pic_ptr = NULL;
  1513. av_freep(&h->slice_ctx);
  1514. h->nb_slice_ctx = 0;
  1515. for (i = 0; i < MAX_SPS_COUNT; i++)
  1516. av_freep(h->sps_buffers + i);
  1517. for (i = 0; i < MAX_PPS_COUNT; i++)
  1518. av_freep(h->pps_buffers + i);
  1519. }
  1520. static av_cold int h264_decode_end(AVCodecContext *avctx)
  1521. {
  1522. H264Context *h = avctx->priv_data;
  1523. ff_h264_free_context(h);
  1524. ff_h264_unref_picture(h, &h->cur_pic);
  1525. return 0;
  1526. }
  1527. #define OFFSET(x) offsetof(H264Context, x)
  1528. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1529. static const AVOption h264_options[] = {
  1530. { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
  1531. { NULL },
  1532. };
  1533. static const AVClass h264_class = {
  1534. .class_name = "h264",
  1535. .item_name = av_default_item_name,
  1536. .option = h264_options,
  1537. .version = LIBAVUTIL_VERSION_INT,
  1538. };
  1539. static const AVProfile profiles[] = {
  1540. { FF_PROFILE_H264_BASELINE, "Baseline" },
  1541. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  1542. { FF_PROFILE_H264_MAIN, "Main" },
  1543. { FF_PROFILE_H264_EXTENDED, "Extended" },
  1544. { FF_PROFILE_H264_HIGH, "High" },
  1545. { FF_PROFILE_H264_HIGH_10, "High 10" },
  1546. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  1547. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  1548. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  1549. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  1550. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  1551. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  1552. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  1553. { FF_PROFILE_UNKNOWN },
  1554. };
  1555. AVCodec ff_h264_decoder = {
  1556. .name = "h264",
  1557. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  1558. .type = AVMEDIA_TYPE_VIDEO,
  1559. .id = AV_CODEC_ID_H264,
  1560. .priv_data_size = sizeof(H264Context),
  1561. .init = ff_h264_decode_init,
  1562. .close = h264_decode_end,
  1563. .decode = h264_decode_frame,
  1564. .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
  1565. CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
  1566. CODEC_CAP_FRAME_THREADS,
  1567. .flush = flush_dpb,
  1568. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1569. .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
  1570. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  1571. .priv_class = &h264_class,
  1572. };