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.

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