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.

1852 lines
62KB

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