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.

1836 lines
61KB

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