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.

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