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.

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