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.

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