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.

1823 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. H264SliceContext *sl = &h->slice_ctx[0];
  55. sl->mb_x = mb_x;
  56. sl->mb_y = mb_y;
  57. sl->mb_xy = mb_x + mb_y * h->mb_stride;
  58. memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
  59. assert(ref >= 0);
  60. /* FIXME: It is possible albeit uncommon that slice references
  61. * differ between slices. We take the easy approach and ignore
  62. * it for now. If this turns out to have any relevance in
  63. * practice then correct remapping should be added. */
  64. if (ref >= sl->ref_count[0])
  65. ref = 0;
  66. fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
  67. 2, 2, 2, ref, 1);
  68. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  69. fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
  70. pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
  71. assert(!FRAME_MBAFF(h));
  72. ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
  73. }
  74. void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
  75. int y, int height)
  76. {
  77. AVCodecContext *avctx = h->avctx;
  78. const AVFrame *src = &h->cur_pic.f;
  79. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  80. int vshift = desc->log2_chroma_h;
  81. const int field_pic = h->picture_structure != PICT_FRAME;
  82. if (field_pic) {
  83. height <<= 1;
  84. y <<= 1;
  85. }
  86. height = FFMIN(height, avctx->height - y);
  87. if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
  88. return;
  89. if (avctx->draw_horiz_band) {
  90. int offset[AV_NUM_DATA_POINTERS];
  91. int i;
  92. offset[0] = y * src->linesize[0];
  93. offset[1] =
  94. offset[2] = (y >> vshift) * src->linesize[1];
  95. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  96. offset[i] = 0;
  97. emms_c();
  98. avctx->draw_horiz_band(avctx, src, offset,
  99. y, h->picture_structure, height);
  100. }
  101. }
  102. /**
  103. * Check if the top & left blocks are available if needed and
  104. * change the dc mode so it only uses the available blocks.
  105. */
  106. int ff_h264_check_intra4x4_pred_mode(const H264Context *h, H264SliceContext *sl)
  107. {
  108. static const int8_t top[12] = {
  109. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  110. };
  111. static const int8_t left[12] = {
  112. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  113. };
  114. int i;
  115. if (!(sl->top_samples_available & 0x8000)) {
  116. for (i = 0; i < 4; i++) {
  117. int status = top[sl->intra4x4_pred_mode_cache[scan8[0] + i]];
  118. if (status < 0) {
  119. av_log(h->avctx, AV_LOG_ERROR,
  120. "top block unavailable for requested intra4x4 mode %d at %d %d\n",
  121. status, sl->mb_x, sl->mb_y);
  122. return AVERROR_INVALIDDATA;
  123. } else if (status) {
  124. sl->intra4x4_pred_mode_cache[scan8[0] + i] = status;
  125. }
  126. }
  127. }
  128. if ((sl->left_samples_available & 0x8888) != 0x8888) {
  129. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  130. for (i = 0; i < 4; i++)
  131. if (!(sl->left_samples_available & mask[i])) {
  132. int status = left[sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
  133. if (status < 0) {
  134. av_log(h->avctx, AV_LOG_ERROR,
  135. "left block unavailable for requested intra4x4 mode %d at %d %d\n",
  136. status, sl->mb_x, sl->mb_y);
  137. return AVERROR_INVALIDDATA;
  138. } else if (status) {
  139. sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
  140. }
  141. }
  142. }
  143. return 0;
  144. } // FIXME cleanup like ff_h264_check_intra_pred_mode
  145. /**
  146. * Check if the top & left blocks are available if needed and
  147. * change the dc mode so it only uses the available blocks.
  148. */
  149. int ff_h264_check_intra_pred_mode(const H264Context *h, H264SliceContext *sl,
  150. int mode, int is_chroma)
  151. {
  152. static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  153. static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  154. if (mode > 3U) {
  155. av_log(h->avctx, AV_LOG_ERROR,
  156. "out of range intra chroma pred mode at %d %d\n",
  157. sl->mb_x, sl->mb_y);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. if (!(sl->top_samples_available & 0x8000)) {
  161. mode = top[mode];
  162. if (mode < 0) {
  163. av_log(h->avctx, AV_LOG_ERROR,
  164. "top block unavailable for requested intra mode at %d %d\n",
  165. sl->mb_x, sl->mb_y);
  166. return AVERROR_INVALIDDATA;
  167. }
  168. }
  169. if ((sl->left_samples_available & 0x8080) != 0x8080) {
  170. mode = left[mode];
  171. if (is_chroma && (sl->left_samples_available & 0x8080)) {
  172. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  173. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  174. (!(sl->left_samples_available & 0x8000)) +
  175. 2 * (mode == DC_128_PRED8x8);
  176. }
  177. if (mode < 0) {
  178. av_log(h->avctx, AV_LOG_ERROR,
  179. "left block unavailable for requested intra mode at %d %d\n",
  180. sl->mb_x, sl->mb_y);
  181. return AVERROR_INVALIDDATA;
  182. }
  183. }
  184. return mode;
  185. }
  186. const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl,
  187. const uint8_t *src,
  188. int *dst_length, int *consumed, int length)
  189. {
  190. int i, si, di;
  191. uint8_t *dst;
  192. // src[0]&0x80; // forbidden bit
  193. h->nal_ref_idc = src[0] >> 5;
  194. h->nal_unit_type = src[0] & 0x1F;
  195. src++;
  196. length--;
  197. #define STARTCODE_TEST \
  198. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  199. if (src[i + 2] != 3) { \
  200. /* startcode, so we must be past the end */ \
  201. length = i; \
  202. } \
  203. break; \
  204. }
  205. #if HAVE_FAST_UNALIGNED
  206. #define FIND_FIRST_ZERO \
  207. if (i > 0 && !src[i]) \
  208. i--; \
  209. while (src[i]) \
  210. i++
  211. #if HAVE_FAST_64BIT
  212. for (i = 0; i + 1 < length; i += 9) {
  213. if (!((~AV_RN64A(src + i) &
  214. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  215. 0x8000800080008080ULL))
  216. continue;
  217. FIND_FIRST_ZERO;
  218. STARTCODE_TEST;
  219. i -= 7;
  220. }
  221. #else
  222. for (i = 0; i + 1 < length; i += 5) {
  223. if (!((~AV_RN32A(src + i) &
  224. (AV_RN32A(src + i) - 0x01000101U)) &
  225. 0x80008080U))
  226. continue;
  227. FIND_FIRST_ZERO;
  228. STARTCODE_TEST;
  229. i -= 3;
  230. }
  231. #endif
  232. #else
  233. for (i = 0; i + 1 < length; i += 2) {
  234. if (src[i])
  235. continue;
  236. if (i > 0 && src[i - 1] == 0)
  237. i--;
  238. STARTCODE_TEST;
  239. }
  240. #endif
  241. if (i >= length - 1) { // no escaped 0
  242. *dst_length = length;
  243. *consumed = length + 1; // +1 for the header
  244. return src;
  245. }
  246. av_fast_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size,
  247. length + FF_INPUT_BUFFER_PADDING_SIZE);
  248. dst = sl->rbsp_buffer;
  249. if (!dst)
  250. return NULL;
  251. memcpy(dst, src, i);
  252. si = di = i;
  253. while (si + 2 < length) {
  254. // remove escapes (very rare 1:2^22)
  255. if (src[si + 2] > 3) {
  256. dst[di++] = src[si++];
  257. dst[di++] = src[si++];
  258. } else if (src[si] == 0 && src[si + 1] == 0) {
  259. if (src[si + 2] == 3) { // escape
  260. dst[di++] = 0;
  261. dst[di++] = 0;
  262. si += 3;
  263. continue;
  264. } else // next start code
  265. goto nsc;
  266. }
  267. dst[di++] = src[si++];
  268. }
  269. while (si < length)
  270. dst[di++] = src[si++];
  271. nsc:
  272. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  273. *dst_length = di;
  274. *consumed = si + 1; // +1 for the header
  275. /* FIXME store exact number of bits in the getbitcontext
  276. * (it is needed for decoding) */
  277. return dst;
  278. }
  279. /**
  280. * Identify the exact end of the bitstream
  281. * @return the length of the trailing, or 0 if damaged
  282. */
  283. static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
  284. {
  285. int v = *src;
  286. int r;
  287. tprintf(h->avctx, "rbsp trailing %X\n", v);
  288. for (r = 1; r < 9; r++) {
  289. if (v & 1)
  290. return r;
  291. v >>= 1;
  292. }
  293. return 0;
  294. }
  295. void ff_h264_free_tables(H264Context *h, int free_rbsp)
  296. {
  297. int i;
  298. av_freep(&h->intra4x4_pred_mode);
  299. av_freep(&h->chroma_pred_mode_table);
  300. av_freep(&h->cbp_table);
  301. av_freep(&h->mvd_table[0]);
  302. av_freep(&h->mvd_table[1]);
  303. av_freep(&h->direct_table);
  304. av_freep(&h->non_zero_count);
  305. av_freep(&h->slice_table_base);
  306. h->slice_table = NULL;
  307. av_freep(&h->list_counts);
  308. av_freep(&h->mb2b_xy);
  309. av_freep(&h->mb2br_xy);
  310. av_buffer_pool_uninit(&h->qscale_table_pool);
  311. av_buffer_pool_uninit(&h->mb_type_pool);
  312. av_buffer_pool_uninit(&h->motion_val_pool);
  313. av_buffer_pool_uninit(&h->ref_index_pool);
  314. if (free_rbsp && h->DPB) {
  315. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  316. ff_h264_unref_picture(h, &h->DPB[i]);
  317. av_freep(&h->DPB);
  318. } else if (h->DPB) {
  319. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  320. h->DPB[i].needs_realloc = 1;
  321. }
  322. h->cur_pic_ptr = NULL;
  323. for (i = 0; i < h->nb_slice_ctx; i++) {
  324. H264SliceContext *sl = &h->slice_ctx[i];
  325. av_freep(&sl->dc_val_base);
  326. av_freep(&sl->er.mb_index2xy);
  327. av_freep(&sl->er.error_status_table);
  328. av_freep(&sl->er.er_temp_buffer);
  329. av_freep(&sl->bipred_scratchpad);
  330. av_freep(&sl->edge_emu_buffer);
  331. av_freep(&sl->top_borders[0]);
  332. av_freep(&sl->top_borders[1]);
  333. sl->bipred_scratchpad_allocated = 0;
  334. sl->edge_emu_buffer_allocated = 0;
  335. sl->top_borders_allocated[0] = 0;
  336. sl->top_borders_allocated[1] = 0;
  337. if (free_rbsp) {
  338. av_freep(&sl->rbsp_buffer);
  339. sl->rbsp_buffer_size = 0;
  340. }
  341. }
  342. }
  343. int ff_h264_alloc_tables(H264Context *h)
  344. {
  345. const int big_mb_num = h->mb_stride * (h->mb_height + 1);
  346. const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count;
  347. int x, y, i;
  348. FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
  349. row_mb_num * 8 * sizeof(uint8_t), fail)
  350. h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
  351. FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
  352. big_mb_num * 48 * sizeof(uint8_t), fail)
  353. FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
  354. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
  355. FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
  356. big_mb_num * sizeof(uint16_t), fail)
  357. FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
  358. big_mb_num * sizeof(uint8_t), fail)
  359. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
  360. 16 * row_mb_num * sizeof(uint8_t), fail);
  361. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
  362. 16 * row_mb_num * sizeof(uint8_t), fail);
  363. h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
  364. h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
  365. FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
  366. 4 * big_mb_num * sizeof(uint8_t), fail);
  367. FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
  368. big_mb_num * sizeof(uint8_t), fail)
  369. memset(h->slice_table_base, -1,
  370. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
  371. h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
  372. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
  373. big_mb_num * sizeof(uint32_t), fail);
  374. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
  375. big_mb_num * sizeof(uint32_t), fail);
  376. for (y = 0; y < h->mb_height; y++)
  377. for (x = 0; x < h->mb_width; x++) {
  378. const int mb_xy = x + y * h->mb_stride;
  379. const int b_xy = 4 * x + 4 * y * h->b_stride;
  380. h->mb2b_xy[mb_xy] = b_xy;
  381. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
  382. }
  383. if (!h->dequant4_coeff[0])
  384. h264_init_dequant_tables(h);
  385. if (!h->DPB) {
  386. h->DPB = av_mallocz_array(H264_MAX_PICTURE_COUNT, sizeof(*h->DPB));
  387. if (!h->DPB)
  388. goto fail;
  389. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  390. av_frame_unref(&h->DPB[i].f);
  391. av_frame_unref(&h->cur_pic.f);
  392. }
  393. return 0;
  394. fail:
  395. ff_h264_free_tables(h, 1);
  396. return AVERROR(ENOMEM);
  397. }
  398. /**
  399. * Init context
  400. * Allocate buffers which are not shared amongst multiple threads.
  401. */
  402. int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
  403. {
  404. ERContext *er = &sl->er;
  405. int mb_array_size = h->mb_height * h->mb_stride;
  406. int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
  407. int c_size = h->mb_stride * (h->mb_height + 1);
  408. int yc_size = y_size + 2 * c_size;
  409. int x, y, i;
  410. sl->ref_cache[0][scan8[5] + 1] =
  411. sl->ref_cache[0][scan8[7] + 1] =
  412. sl->ref_cache[0][scan8[13] + 1] =
  413. sl->ref_cache[1][scan8[5] + 1] =
  414. sl->ref_cache[1][scan8[7] + 1] =
  415. sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  416. if (CONFIG_ERROR_RESILIENCE) {
  417. /* init ER */
  418. er->avctx = h->avctx;
  419. er->decode_mb = h264_er_decode_mb;
  420. er->opaque = h;
  421. er->quarter_sample = 1;
  422. er->mb_num = h->mb_num;
  423. er->mb_width = h->mb_width;
  424. er->mb_height = h->mb_height;
  425. er->mb_stride = h->mb_stride;
  426. er->b8_stride = h->mb_width * 2 + 1;
  427. // error resilience code looks cleaner with this
  428. FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
  429. (h->mb_num + 1) * sizeof(int), fail);
  430. for (y = 0; y < h->mb_height; y++)
  431. for (x = 0; x < h->mb_width; x++)
  432. er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
  433. er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
  434. h->mb_stride + h->mb_width;
  435. FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
  436. mb_array_size * sizeof(uint8_t), fail);
  437. FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
  438. h->mb_height * h->mb_stride, fail);
  439. FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
  440. yc_size * sizeof(int16_t), fail);
  441. er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
  442. er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
  443. er->dc_val[2] = er->dc_val[1] + c_size;
  444. for (i = 0; i < yc_size; i++)
  445. sl->dc_val_base[i] = 1024;
  446. }
  447. return 0;
  448. fail:
  449. return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
  450. }
  451. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  452. int parse_extradata);
  453. int ff_h264_decode_extradata(H264Context *h)
  454. {
  455. AVCodecContext *avctx = h->avctx;
  456. int ret;
  457. if (avctx->extradata[0] == 1) {
  458. int i, cnt, nalsize;
  459. unsigned char *p = avctx->extradata;
  460. h->is_avc = 1;
  461. if (avctx->extradata_size < 7) {
  462. av_log(avctx, AV_LOG_ERROR,
  463. "avcC %d too short\n", avctx->extradata_size);
  464. return AVERROR_INVALIDDATA;
  465. }
  466. /* sps and pps in the avcC always have length coded with 2 bytes,
  467. * so put a fake nal_length_size = 2 while parsing them */
  468. h->nal_length_size = 2;
  469. // Decode sps from avcC
  470. cnt = *(p + 5) & 0x1f; // Number of sps
  471. p += 6;
  472. for (i = 0; i < cnt; i++) {
  473. nalsize = AV_RB16(p) + 2;
  474. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  475. return AVERROR_INVALIDDATA;
  476. ret = decode_nal_units(h, p, nalsize, 1);
  477. if (ret < 0) {
  478. av_log(avctx, AV_LOG_ERROR,
  479. "Decoding sps %d from avcC failed\n", i);
  480. return ret;
  481. }
  482. p += nalsize;
  483. }
  484. // Decode pps from avcC
  485. cnt = *(p++); // Number of pps
  486. for (i = 0; i < cnt; i++) {
  487. nalsize = AV_RB16(p) + 2;
  488. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  489. return AVERROR_INVALIDDATA;
  490. ret = decode_nal_units(h, p, nalsize, 1);
  491. if (ret < 0) {
  492. av_log(avctx, AV_LOG_ERROR,
  493. "Decoding pps %d from avcC failed\n", i);
  494. return ret;
  495. }
  496. p += nalsize;
  497. }
  498. // Store right nal length size that will be used to parse all other nals
  499. h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;
  500. } else {
  501. h->is_avc = 0;
  502. ret = decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1);
  503. if (ret < 0)
  504. return ret;
  505. }
  506. return 0;
  507. }
  508. av_cold int ff_h264_decode_init(AVCodecContext *avctx)
  509. {
  510. H264Context *h = avctx->priv_data;
  511. int i;
  512. int ret;
  513. h->avctx = avctx;
  514. h->bit_depth_luma = 8;
  515. h->chroma_format_idc = 1;
  516. ff_h264dsp_init(&h->h264dsp, 8, 1);
  517. ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
  518. ff_h264qpel_init(&h->h264qpel, 8);
  519. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
  520. h->dequant_coeff_pps = -1;
  521. /* needed so that IDCT permutation is known early */
  522. ff_videodsp_init(&h->vdsp, 8);
  523. memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
  524. memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
  525. h->picture_structure = PICT_FRAME;
  526. h->slice_context_count = 1;
  527. h->workaround_bugs = avctx->workaround_bugs;
  528. h->flags = avctx->flags;
  529. /* set defaults */
  530. // s->decode_mb = ff_h263_decode_mb;
  531. if (!avctx->has_b_frames)
  532. h->low_delay = 1;
  533. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  534. ff_h264_decode_init_vlc();
  535. ff_init_cabac_states();
  536. h->pixel_shift = 0;
  537. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  538. h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? H264_MAX_THREADS : 1;
  539. h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
  540. if (!h->slice_ctx) {
  541. h->nb_slice_ctx = 0;
  542. return AVERROR(ENOMEM);
  543. }
  544. for (i = 0; i < h->nb_slice_ctx; i++)
  545. h->slice_ctx[i].h264 = h;
  546. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  547. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  548. h->last_pocs[i] = INT_MIN;
  549. h->prev_poc_msb = 1 << 16;
  550. h->x264_build = -1;
  551. ff_h264_reset_sei(h);
  552. h->recovery_frame = -1;
  553. h->frame_recovered = 0;
  554. if (avctx->codec_id == AV_CODEC_ID_H264) {
  555. if (avctx->ticks_per_frame == 1)
  556. h->avctx->framerate.num *= 2;
  557. avctx->ticks_per_frame = 2;
  558. }
  559. if (avctx->extradata_size > 0 && avctx->extradata) {
  560. ret = ff_h264_decode_extradata(h);
  561. if (ret < 0) {
  562. ff_h264_free_context(h);
  563. return ret;
  564. }
  565. }
  566. if (h->sps.bitstream_restriction_flag &&
  567. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  568. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  569. h->low_delay = 0;
  570. }
  571. avctx->internal->allocate_progress = 1;
  572. return 0;
  573. }
  574. static int decode_init_thread_copy(AVCodecContext *avctx)
  575. {
  576. H264Context *h = avctx->priv_data;
  577. int i;
  578. if (!avctx->internal->is_copy)
  579. return 0;
  580. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  581. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  582. h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? H264_MAX_THREADS : 1;
  583. h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
  584. if (!h->slice_ctx) {
  585. h->nb_slice_ctx = 0;
  586. return AVERROR(ENOMEM);
  587. }
  588. for (i = 0; i < h->nb_slice_ctx; i++)
  589. h->slice_ctx[i].h264 = h;
  590. h->avctx = avctx;
  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, H264SliceContext *sl)
  850. {
  851. int list, i;
  852. int luma_def, chroma_def;
  853. sl->use_weight = 0;
  854. sl->use_weight_chroma = 0;
  855. sl->luma_log2_weight_denom = get_ue_golomb(&sl->gb);
  856. if (h->sps.chroma_format_idc)
  857. sl->chroma_log2_weight_denom = get_ue_golomb(&sl->gb);
  858. luma_def = 1 << sl->luma_log2_weight_denom;
  859. chroma_def = 1 << sl->chroma_log2_weight_denom;
  860. for (list = 0; list < 2; list++) {
  861. sl->luma_weight_flag[list] = 0;
  862. sl->chroma_weight_flag[list] = 0;
  863. for (i = 0; i < sl->ref_count[list]; i++) {
  864. int luma_weight_flag, chroma_weight_flag;
  865. luma_weight_flag = get_bits1(&sl->gb);
  866. if (luma_weight_flag) {
  867. sl->luma_weight[i][list][0] = get_se_golomb(&sl->gb);
  868. sl->luma_weight[i][list][1] = get_se_golomb(&sl->gb);
  869. if (sl->luma_weight[i][list][0] != luma_def ||
  870. sl->luma_weight[i][list][1] != 0) {
  871. sl->use_weight = 1;
  872. sl->luma_weight_flag[list] = 1;
  873. }
  874. } else {
  875. sl->luma_weight[i][list][0] = luma_def;
  876. sl->luma_weight[i][list][1] = 0;
  877. }
  878. if (h->sps.chroma_format_idc) {
  879. chroma_weight_flag = get_bits1(&sl->gb);
  880. if (chroma_weight_flag) {
  881. int j;
  882. for (j = 0; j < 2; j++) {
  883. sl->chroma_weight[i][list][j][0] = get_se_golomb(&sl->gb);
  884. sl->chroma_weight[i][list][j][1] = get_se_golomb(&sl->gb);
  885. if (sl->chroma_weight[i][list][j][0] != chroma_def ||
  886. sl->chroma_weight[i][list][j][1] != 0) {
  887. sl->use_weight_chroma = 1;
  888. sl->chroma_weight_flag[list] = 1;
  889. }
  890. }
  891. } else {
  892. int j;
  893. for (j = 0; j < 2; j++) {
  894. sl->chroma_weight[i][list][j][0] = chroma_def;
  895. sl->chroma_weight[i][list][j][1] = 0;
  896. }
  897. }
  898. }
  899. }
  900. if (sl->slice_type_nos != AV_PICTURE_TYPE_B)
  901. break;
  902. }
  903. sl->use_weight = sl->use_weight || sl->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_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, H264SliceContext *sl)
  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 (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1077. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1078. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1079. num_ref_idx_active_override_flag = get_bits1(&sl->gb);
  1080. if (num_ref_idx_active_override_flag) {
  1081. ref_count[0] = get_ue_golomb(&sl->gb) + 1;
  1082. if (ref_count[0] < 1)
  1083. return AVERROR_INVALIDDATA;
  1084. if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1085. ref_count[1] = get_ue_golomb(&sl->gb) + 1;
  1086. if (ref_count[1] < 1)
  1087. return AVERROR_INVALIDDATA;
  1088. }
  1089. }
  1090. if (sl->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. sl->ref_count[0] = sl->ref_count[1] = 0;
  1102. return AVERROR_INVALIDDATA;
  1103. }
  1104. if (list_count != sl->list_count ||
  1105. ref_count[0] != sl->ref_count[0] ||
  1106. ref_count[1] != sl->ref_count[1]) {
  1107. sl->ref_count[0] = ref_count[0];
  1108. sl->ref_count[1] = ref_count[1];
  1109. sl->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. GetBitContext gb;
  1166. int nalsize = 0;
  1167. int dst_length, bit_length, consumed;
  1168. const uint8_t *ptr;
  1169. if (buf_index >= next_avc) {
  1170. nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
  1171. if (nalsize < 0)
  1172. break;
  1173. next_avc = buf_index + nalsize;
  1174. } else {
  1175. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  1176. if (buf_index >= buf_size)
  1177. break;
  1178. }
  1179. ptr = ff_h264_decode_nal(h, &h->slice_ctx[0], buf + buf_index, &dst_length, &consumed,
  1180. next_avc - buf_index);
  1181. if (!ptr || dst_length < 0)
  1182. return AVERROR_INVALIDDATA;
  1183. buf_index += consumed;
  1184. bit_length = get_bit_length(h, buf, ptr, dst_length,
  1185. buf_index, next_avc);
  1186. nal_index++;
  1187. /* packets can sometimes contain multiple PPS/SPS,
  1188. * e.g. two PAFF field pictures in one packet, or a demuxer
  1189. * which splits NALs strangely if so, when frame threading we
  1190. * can't start the next thread until we've read all of them */
  1191. switch (h->nal_unit_type) {
  1192. case NAL_SPS:
  1193. case NAL_PPS:
  1194. nals_needed = nal_index;
  1195. break;
  1196. case NAL_DPA:
  1197. case NAL_IDR_SLICE:
  1198. case NAL_SLICE:
  1199. init_get_bits(&gb, ptr, bit_length);
  1200. if (!get_ue_golomb(&gb))
  1201. nals_needed = nal_index;
  1202. }
  1203. }
  1204. return nals_needed;
  1205. }
  1206. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  1207. int parse_extradata)
  1208. {
  1209. AVCodecContext *const avctx = h->avctx;
  1210. H264SliceContext *sl;
  1211. int buf_index;
  1212. unsigned context_count;
  1213. int next_avc;
  1214. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  1215. int nal_index;
  1216. int ret = 0;
  1217. h->max_contexts = h->slice_context_count;
  1218. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
  1219. h->current_slice = 0;
  1220. if (!h->first_field)
  1221. h->cur_pic_ptr = NULL;
  1222. ff_h264_reset_sei(h);
  1223. }
  1224. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1225. nals_needed = get_last_needed_nal(h, buf, buf_size);
  1226. {
  1227. buf_index = 0;
  1228. context_count = 0;
  1229. next_avc = h->is_avc ? 0 : buf_size;
  1230. nal_index = 0;
  1231. for (;;) {
  1232. int consumed;
  1233. int dst_length;
  1234. int bit_length;
  1235. const uint8_t *ptr;
  1236. int nalsize = 0;
  1237. int err;
  1238. if (buf_index >= next_avc) {
  1239. nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
  1240. if (nalsize < 0)
  1241. break;
  1242. next_avc = buf_index + nalsize;
  1243. } else {
  1244. buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
  1245. if (buf_index >= buf_size)
  1246. break;
  1247. if (buf_index >= next_avc)
  1248. continue;
  1249. }
  1250. sl = &h->slice_ctx[context_count];
  1251. ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
  1252. &consumed, next_avc - buf_index);
  1253. if (!ptr || dst_length < 0) {
  1254. ret = -1;
  1255. goto end;
  1256. }
  1257. bit_length = get_bit_length(h, buf, ptr, dst_length,
  1258. buf_index + consumed, next_avc);
  1259. if (h->avctx->debug & FF_DEBUG_STARTCODE)
  1260. av_log(h->avctx, AV_LOG_DEBUG,
  1261. "NAL %d at %d/%d length %d\n",
  1262. h->nal_unit_type, buf_index, buf_size, dst_length);
  1263. if (h->is_avc && (nalsize != consumed) && nalsize)
  1264. av_log(h->avctx, AV_LOG_DEBUG,
  1265. "AVC: Consumed only %d bytes instead of %d\n",
  1266. consumed, nalsize);
  1267. buf_index += consumed;
  1268. nal_index++;
  1269. if (avctx->skip_frame >= AVDISCARD_NONREF &&
  1270. h->nal_ref_idc == 0 &&
  1271. h->nal_unit_type != NAL_SEI)
  1272. continue;
  1273. again:
  1274. /* Ignore every NAL unit type except PPS and SPS during extradata
  1275. * parsing. Decoding slices is not possible in codec init
  1276. * with frame-mt */
  1277. if (parse_extradata && HAVE_THREADS &&
  1278. (h->avctx->active_thread_type & FF_THREAD_FRAME) &&
  1279. (h->nal_unit_type != NAL_PPS &&
  1280. h->nal_unit_type != NAL_SPS)) {
  1281. if (h->nal_unit_type < NAL_AUD ||
  1282. h->nal_unit_type > NAL_AUXILIARY_SLICE)
  1283. av_log(avctx, AV_LOG_INFO,
  1284. "Ignoring NAL unit %d during extradata parsing\n",
  1285. h->nal_unit_type);
  1286. h->nal_unit_type = NAL_FF_IGNORE;
  1287. }
  1288. err = 0;
  1289. switch (h->nal_unit_type) {
  1290. case NAL_IDR_SLICE:
  1291. if (h->nal_unit_type != NAL_IDR_SLICE) {
  1292. av_log(h->avctx, AV_LOG_ERROR,
  1293. "Invalid mix of idr and non-idr slices\n");
  1294. ret = -1;
  1295. goto end;
  1296. }
  1297. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  1298. case NAL_SLICE:
  1299. init_get_bits(&sl->gb, ptr, bit_length);
  1300. if ((err = ff_h264_decode_slice_header(h, sl)))
  1301. break;
  1302. if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
  1303. h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
  1304. ((1 << h->sps.log2_max_frame_num) - 1);
  1305. }
  1306. h->cur_pic_ptr->f.key_frame |=
  1307. (h->nal_unit_type == NAL_IDR_SLICE) ||
  1308. (h->sei_recovery_frame_cnt >= 0);
  1309. if (h->nal_unit_type == NAL_IDR_SLICE ||
  1310. h->recovery_frame == h->frame_num) {
  1311. h->recovery_frame = -1;
  1312. h->cur_pic_ptr->recovered = 1;
  1313. }
  1314. // If we have an IDR, all frames after it in decoded order are
  1315. // "recovered".
  1316. if (h->nal_unit_type == NAL_IDR_SLICE)
  1317. h->frame_recovered |= FRAME_RECOVERED_IDR;
  1318. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  1319. if (h->current_slice == 1) {
  1320. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
  1321. decode_postinit(h, nal_index >= nals_needed);
  1322. if (h->avctx->hwaccel &&
  1323. (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
  1324. return ret;
  1325. }
  1326. if (sl->redundant_pic_count == 0 &&
  1327. (avctx->skip_frame < AVDISCARD_NONREF ||
  1328. h->nal_ref_idc) &&
  1329. (avctx->skip_frame < AVDISCARD_BIDIR ||
  1330. sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
  1331. (avctx->skip_frame < AVDISCARD_NONKEY ||
  1332. sl->slice_type_nos == AV_PICTURE_TYPE_I) &&
  1333. avctx->skip_frame < AVDISCARD_ALL) {
  1334. if (avctx->hwaccel) {
  1335. ret = avctx->hwaccel->decode_slice(avctx,
  1336. &buf[buf_index - consumed],
  1337. consumed);
  1338. if (ret < 0)
  1339. return ret;
  1340. } else
  1341. context_count++;
  1342. }
  1343. break;
  1344. case NAL_DPA:
  1345. case NAL_DPB:
  1346. case NAL_DPC:
  1347. avpriv_request_sample(avctx, "data partitioning");
  1348. ret = AVERROR(ENOSYS);
  1349. goto end;
  1350. break;
  1351. case NAL_SEI:
  1352. init_get_bits(&h->gb, ptr, bit_length);
  1353. ret = ff_h264_decode_sei(h);
  1354. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1355. goto end;
  1356. break;
  1357. case NAL_SPS:
  1358. init_get_bits(&h->gb, ptr, bit_length);
  1359. ret = ff_h264_decode_seq_parameter_set(h);
  1360. if (ret < 0 && h->is_avc && (nalsize != consumed) && nalsize) {
  1361. av_log(h->avctx, AV_LOG_DEBUG,
  1362. "SPS decoding failure, trying again with the complete NAL\n");
  1363. init_get_bits(&h->gb, buf + buf_index + 1 - consumed,
  1364. 8 * (nalsize - 1));
  1365. ff_h264_decode_seq_parameter_set(h);
  1366. }
  1367. ret = ff_h264_set_parameter_from_sps(h);
  1368. if (ret < 0)
  1369. goto end;
  1370. break;
  1371. case NAL_PPS:
  1372. init_get_bits(&h->gb, ptr, bit_length);
  1373. ret = ff_h264_decode_picture_parameter_set(h, bit_length);
  1374. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1375. goto end;
  1376. break;
  1377. case NAL_AUD:
  1378. case NAL_END_SEQUENCE:
  1379. case NAL_END_STREAM:
  1380. case NAL_FILLER_DATA:
  1381. case NAL_SPS_EXT:
  1382. case NAL_AUXILIARY_SLICE:
  1383. break;
  1384. case NAL_FF_IGNORE:
  1385. break;
  1386. default:
  1387. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  1388. h->nal_unit_type, bit_length);
  1389. }
  1390. if (context_count == h->max_contexts) {
  1391. ret = ff_h264_execute_decode_slices(h, context_count);
  1392. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1393. goto end;
  1394. context_count = 0;
  1395. }
  1396. if (err < 0) {
  1397. av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  1398. sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
  1399. } else if (err == 1) {
  1400. /* Slice could not be decoded in parallel mode, restart. Note
  1401. * that rbsp_buffer is not transferred, but since we no longer
  1402. * run in parallel mode this should not be an issue. */
  1403. sl = &h->slice_ctx[0];
  1404. goto again;
  1405. }
  1406. }
  1407. }
  1408. if (context_count) {
  1409. ret = ff_h264_execute_decode_slices(h, context_count);
  1410. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1411. goto end;
  1412. }
  1413. ret = 0;
  1414. end:
  1415. /* clean up */
  1416. if (h->cur_pic_ptr && !h->droppable) {
  1417. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1418. h->picture_structure == PICT_BOTTOM_FIELD);
  1419. }
  1420. return (ret < 0) ? ret : buf_index;
  1421. }
  1422. /**
  1423. * Return the number of bytes consumed for building the current frame.
  1424. */
  1425. static int get_consumed_bytes(int pos, int buf_size)
  1426. {
  1427. if (pos == 0)
  1428. pos = 1; // avoid infinite loops (I doubt that is needed but...)
  1429. if (pos + 10 > buf_size)
  1430. pos = buf_size; // oops ;)
  1431. return pos;
  1432. }
  1433. static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
  1434. {
  1435. int i;
  1436. int ret = av_frame_ref(dst, src);
  1437. if (ret < 0)
  1438. return ret;
  1439. if (!h->sps.crop)
  1440. return 0;
  1441. for (i = 0; i < 3; i++) {
  1442. int hshift = (i > 0) ? h->chroma_x_shift : 0;
  1443. int vshift = (i > 0) ? h->chroma_y_shift : 0;
  1444. int off = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
  1445. (h->sps.crop_top >> vshift) * dst->linesize[i];
  1446. dst->data[i] += off;
  1447. }
  1448. return 0;
  1449. }
  1450. static int h264_decode_frame(AVCodecContext *avctx, void *data,
  1451. int *got_frame, AVPacket *avpkt)
  1452. {
  1453. const uint8_t *buf = avpkt->data;
  1454. int buf_size = avpkt->size;
  1455. H264Context *h = avctx->priv_data;
  1456. AVFrame *pict = data;
  1457. int buf_index = 0;
  1458. int ret;
  1459. h->flags = avctx->flags;
  1460. /* end of stream, output what is still in the buffers */
  1461. out:
  1462. if (buf_size == 0) {
  1463. H264Picture *out;
  1464. int i, out_idx;
  1465. h->cur_pic_ptr = NULL;
  1466. // FIXME factorize this with the output code below
  1467. out = h->delayed_pic[0];
  1468. out_idx = 0;
  1469. for (i = 1;
  1470. h->delayed_pic[i] &&
  1471. !h->delayed_pic[i]->f.key_frame &&
  1472. !h->delayed_pic[i]->mmco_reset;
  1473. i++)
  1474. if (h->delayed_pic[i]->poc < out->poc) {
  1475. out = h->delayed_pic[i];
  1476. out_idx = i;
  1477. }
  1478. for (i = out_idx; h->delayed_pic[i]; i++)
  1479. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1480. if (out) {
  1481. ret = output_frame(h, pict, &out->f);
  1482. if (ret < 0)
  1483. return ret;
  1484. *got_frame = 1;
  1485. }
  1486. return buf_index;
  1487. }
  1488. buf_index = decode_nal_units(h, buf, buf_size, 0);
  1489. if (buf_index < 0)
  1490. return AVERROR_INVALIDDATA;
  1491. if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  1492. buf_size = 0;
  1493. goto out;
  1494. }
  1495. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
  1496. if (avctx->skip_frame >= AVDISCARD_NONREF)
  1497. return 0;
  1498. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  1499. return AVERROR_INVALIDDATA;
  1500. }
  1501. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
  1502. (h->mb_y >= h->mb_height && h->mb_height)) {
  1503. if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
  1504. decode_postinit(h, 1);
  1505. ff_h264_field_end(h, &h->slice_ctx[0], 0);
  1506. *got_frame = 0;
  1507. if (h->next_output_pic && ((avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT) ||
  1508. h->next_output_pic->recovered)) {
  1509. if (!h->next_output_pic->recovered)
  1510. h->next_output_pic->f.flags |= AV_FRAME_FLAG_CORRUPT;
  1511. ret = output_frame(h, pict, &h->next_output_pic->f);
  1512. if (ret < 0)
  1513. return ret;
  1514. *got_frame = 1;
  1515. }
  1516. }
  1517. assert(pict->buf[0] || !*got_frame);
  1518. return get_consumed_bytes(buf_index, buf_size);
  1519. }
  1520. av_cold void ff_h264_free_context(H264Context *h)
  1521. {
  1522. int i;
  1523. ff_h264_free_tables(h, 1); // FIXME cleanup init stuff perhaps
  1524. av_freep(&h->slice_ctx);
  1525. h->nb_slice_ctx = 0;
  1526. for (i = 0; i < MAX_SPS_COUNT; i++)
  1527. av_freep(h->sps_buffers + i);
  1528. for (i = 0; i < MAX_PPS_COUNT; i++)
  1529. av_freep(h->pps_buffers + i);
  1530. }
  1531. static av_cold int h264_decode_end(AVCodecContext *avctx)
  1532. {
  1533. H264Context *h = avctx->priv_data;
  1534. ff_h264_free_context(h);
  1535. ff_h264_unref_picture(h, &h->cur_pic);
  1536. return 0;
  1537. }
  1538. static const AVProfile profiles[] = {
  1539. { FF_PROFILE_H264_BASELINE, "Baseline" },
  1540. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  1541. { FF_PROFILE_H264_MAIN, "Main" },
  1542. { FF_PROFILE_H264_EXTENDED, "Extended" },
  1543. { FF_PROFILE_H264_HIGH, "High" },
  1544. { FF_PROFILE_H264_HIGH_10, "High 10" },
  1545. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  1546. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  1547. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  1548. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  1549. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  1550. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  1551. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  1552. { FF_PROFILE_UNKNOWN },
  1553. };
  1554. AVCodec ff_h264_decoder = {
  1555. .name = "h264",
  1556. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  1557. .type = AVMEDIA_TYPE_VIDEO,
  1558. .id = AV_CODEC_ID_H264,
  1559. .priv_data_size = sizeof(H264Context),
  1560. .init = ff_h264_decode_init,
  1561. .close = h264_decode_end,
  1562. .decode = h264_decode_frame,
  1563. .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
  1564. CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
  1565. CODEC_CAP_FRAME_THREADS,
  1566. .flush = flush_dpb,
  1567. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1568. .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
  1569. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  1570. };