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.

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