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.

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