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.

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