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.

4831 lines
179KB

  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 "internal.h"
  29. #include "cabac.h"
  30. #include "cabac_functions.h"
  31. #include "dsputil.h"
  32. #include "error_resilience.h"
  33. #include "avcodec.h"
  34. #include "mpegvideo.h"
  35. #include "h264.h"
  36. #include "h264data.h"
  37. #include "h264chroma.h"
  38. #include "h264_mvpred.h"
  39. #include "golomb.h"
  40. #include "mathops.h"
  41. #include "rectangle.h"
  42. #include "svq3.h"
  43. #include "thread.h"
  44. #include "vdpau_internal.h"
  45. // #undef NDEBUG
  46. #include <assert.h>
  47. const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
  48. static const uint8_t rem6[QP_MAX_NUM + 1] = {
  49. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
  50. 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  51. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  52. };
  53. static const uint8_t div6[QP_MAX_NUM + 1] = {
  54. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
  55. 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
  56. 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
  57. };
  58. static const enum AVPixelFormat h264_hwaccel_pixfmt_list_420[] = {
  59. #if CONFIG_H264_DXVA2_HWACCEL
  60. AV_PIX_FMT_DXVA2_VLD,
  61. #endif
  62. #if CONFIG_H264_VAAPI_HWACCEL
  63. AV_PIX_FMT_VAAPI_VLD,
  64. #endif
  65. #if CONFIG_H264_VDA_HWACCEL
  66. AV_PIX_FMT_VDA_VLD,
  67. #endif
  68. #if CONFIG_H264_VDPAU_HWACCEL
  69. AV_PIX_FMT_VDPAU,
  70. #endif
  71. AV_PIX_FMT_YUV420P,
  72. AV_PIX_FMT_NONE
  73. };
  74. static const enum AVPixelFormat h264_hwaccel_pixfmt_list_jpeg_420[] = {
  75. #if CONFIG_H264_DXVA2_HWACCEL
  76. AV_PIX_FMT_DXVA2_VLD,
  77. #endif
  78. #if CONFIG_H264_VAAPI_HWACCEL
  79. AV_PIX_FMT_VAAPI_VLD,
  80. #endif
  81. #if CONFIG_H264_VDA_HWACCEL
  82. AV_PIX_FMT_VDA_VLD,
  83. #endif
  84. #if CONFIG_H264_VDPAU_HWACCEL
  85. AV_PIX_FMT_VDPAU,
  86. #endif
  87. AV_PIX_FMT_YUVJ420P,
  88. AV_PIX_FMT_NONE
  89. };
  90. static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  91. int (*mv)[2][4][2],
  92. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  93. {
  94. H264Context *h = opaque;
  95. h->mb_x = mb_x;
  96. h->mb_y = mb_y;
  97. h->mb_xy = mb_x + mb_y * h->mb_stride;
  98. memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
  99. assert(ref >= 0);
  100. /* FIXME: It is possible albeit uncommon that slice references
  101. * differ between slices. We take the easy approach and ignore
  102. * it for now. If this turns out to have any relevance in
  103. * practice then correct remapping should be added. */
  104. if (ref >= h->ref_count[0])
  105. ref = 0;
  106. fill_rectangle(&h->cur_pic.ref_index[0][4 * h->mb_xy],
  107. 2, 2, 2, ref, 1);
  108. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  109. fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
  110. pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
  111. assert(!FRAME_MBAFF(h));
  112. ff_h264_hl_decode_mb(h);
  113. }
  114. void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
  115. {
  116. AVCodecContext *avctx = h->avctx;
  117. Picture *cur = &h->cur_pic;
  118. Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL;
  119. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  120. int vshift = desc->log2_chroma_h;
  121. const int field_pic = h->picture_structure != PICT_FRAME;
  122. if (field_pic) {
  123. height <<= 1;
  124. y <<= 1;
  125. }
  126. height = FFMIN(height, avctx->height - y);
  127. if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
  128. return;
  129. if (avctx->draw_horiz_band) {
  130. AVFrame *src;
  131. int offset[AV_NUM_DATA_POINTERS];
  132. int i;
  133. if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
  134. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  135. src = &cur->f;
  136. else if (last)
  137. src = &last->f;
  138. else
  139. return;
  140. offset[0] = y * src->linesize[0];
  141. offset[1] =
  142. offset[2] = (y >> vshift) * src->linesize[1];
  143. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  144. offset[i] = 0;
  145. emms_c();
  146. avctx->draw_horiz_band(avctx, src, offset,
  147. y, h->picture_structure, height);
  148. }
  149. }
  150. static void unref_picture(H264Context *h, Picture *pic)
  151. {
  152. int off = offsetof(Picture, tf) + sizeof(pic->tf);
  153. int i;
  154. if (!pic->f.data[0])
  155. return;
  156. ff_thread_release_buffer(h->avctx, &pic->tf);
  157. av_buffer_unref(&pic->hwaccel_priv_buf);
  158. av_buffer_unref(&pic->qscale_table_buf);
  159. av_buffer_unref(&pic->mb_type_buf);
  160. for (i = 0; i < 2; i++) {
  161. av_buffer_unref(&pic->motion_val_buf[i]);
  162. av_buffer_unref(&pic->ref_index_buf[i]);
  163. }
  164. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  165. }
  166. static void release_unused_pictures(H264Context *h, int remove_current)
  167. {
  168. int i;
  169. /* release non reference frames */
  170. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  171. if (h->DPB[i].f.data[0] && !h->DPB[i].reference &&
  172. (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
  173. unref_picture(h, &h->DPB[i]);
  174. }
  175. }
  176. }
  177. static int ref_picture(H264Context *h, Picture *dst, Picture *src)
  178. {
  179. int ret, i;
  180. av_assert0(!dst->f.buf[0]);
  181. av_assert0(src->f.buf[0]);
  182. src->tf.f = &src->f;
  183. dst->tf.f = &dst->f;
  184. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  185. if (ret < 0)
  186. goto fail;
  187. dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
  188. dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
  189. if (!dst->qscale_table_buf || !dst->mb_type_buf)
  190. goto fail;
  191. dst->qscale_table = src->qscale_table;
  192. dst->mb_type = src->mb_type;
  193. for (i = 0; i < 2; i++) {
  194. dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
  195. dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
  196. if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
  197. goto fail;
  198. dst->motion_val[i] = src->motion_val[i];
  199. dst->ref_index[i] = src->ref_index[i];
  200. }
  201. if (src->hwaccel_picture_private) {
  202. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  203. if (!dst->hwaccel_priv_buf)
  204. goto fail;
  205. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  206. }
  207. for (i = 0; i < 2; i++)
  208. dst->field_poc[i] = src->field_poc[i];
  209. memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
  210. memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
  211. dst->poc = src->poc;
  212. dst->frame_num = src->frame_num;
  213. dst->mmco_reset = src->mmco_reset;
  214. dst->pic_id = src->pic_id;
  215. dst->long_ref = src->long_ref;
  216. dst->mbaff = src->mbaff;
  217. dst->field_picture = src->field_picture;
  218. dst->needs_realloc = src->needs_realloc;
  219. dst->reference = src->reference;
  220. return 0;
  221. fail:
  222. unref_picture(h, dst);
  223. return ret;
  224. }
  225. static int alloc_scratch_buffers(H264Context *h, int linesize)
  226. {
  227. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  228. if (h->bipred_scratchpad)
  229. return 0;
  230. h->bipred_scratchpad = av_malloc(16 * 6 * alloc_size);
  231. // edge emu needs blocksize + filter length - 1
  232. // (= 21x21 for h264)
  233. h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 21);
  234. h->me.scratchpad = av_mallocz(alloc_size * 2 * 16 * 2);
  235. if (!h->bipred_scratchpad || !h->edge_emu_buffer || !h->me.scratchpad) {
  236. av_freep(&h->bipred_scratchpad);
  237. av_freep(&h->edge_emu_buffer);
  238. av_freep(&h->me.scratchpad);
  239. return AVERROR(ENOMEM);
  240. }
  241. h->me.temp = h->me.scratchpad;
  242. return 0;
  243. }
  244. static int init_table_pools(H264Context *h)
  245. {
  246. const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
  247. const int mb_array_size = h->mb_stride * h->mb_height;
  248. const int b4_stride = h->mb_width * 4 + 1;
  249. const int b4_array_size = b4_stride * h->mb_height * 4;
  250. h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
  251. av_buffer_allocz);
  252. h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
  253. sizeof(uint32_t), av_buffer_allocz);
  254. h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
  255. sizeof(int16_t), av_buffer_allocz);
  256. h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
  257. if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
  258. !h->ref_index_pool) {
  259. av_buffer_pool_uninit(&h->qscale_table_pool);
  260. av_buffer_pool_uninit(&h->mb_type_pool);
  261. av_buffer_pool_uninit(&h->motion_val_pool);
  262. av_buffer_pool_uninit(&h->ref_index_pool);
  263. return AVERROR(ENOMEM);
  264. }
  265. return 0;
  266. }
  267. static int alloc_picture(H264Context *h, Picture *pic)
  268. {
  269. int i, ret = 0;
  270. av_assert0(!pic->f.data[0]);
  271. pic->tf.f = &pic->f;
  272. ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
  273. AV_GET_BUFFER_FLAG_REF : 0);
  274. if (ret < 0)
  275. goto fail;
  276. h->linesize = pic->f.linesize[0];
  277. h->uvlinesize = pic->f.linesize[1];
  278. if (h->avctx->hwaccel) {
  279. const AVHWAccel *hwaccel = h->avctx->hwaccel;
  280. av_assert0(!pic->hwaccel_picture_private);
  281. if (hwaccel->priv_data_size) {
  282. pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->priv_data_size);
  283. if (!pic->hwaccel_priv_buf)
  284. return AVERROR(ENOMEM);
  285. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  286. }
  287. }
  288. if (!h->qscale_table_pool) {
  289. ret = init_table_pools(h);
  290. if (ret < 0)
  291. goto fail;
  292. }
  293. pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
  294. pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
  295. if (!pic->qscale_table_buf || !pic->mb_type_buf)
  296. goto fail;
  297. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
  298. pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
  299. for (i = 0; i < 2; i++) {
  300. pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
  301. pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
  302. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  303. goto fail;
  304. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  305. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  306. }
  307. return 0;
  308. fail:
  309. unref_picture(h, pic);
  310. return (ret < 0) ? ret : AVERROR(ENOMEM);
  311. }
  312. static inline int pic_is_unused(H264Context *h, Picture *pic)
  313. {
  314. if (pic->f.data[0] == NULL)
  315. return 1;
  316. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  317. return 1;
  318. return 0;
  319. }
  320. static int find_unused_picture(H264Context *h)
  321. {
  322. int i;
  323. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  324. if (pic_is_unused(h, &h->DPB[i]))
  325. break;
  326. }
  327. if (i == MAX_PICTURE_COUNT)
  328. return AVERROR_INVALIDDATA;
  329. if (h->DPB[i].needs_realloc) {
  330. h->DPB[i].needs_realloc = 0;
  331. unref_picture(h, &h->DPB[i]);
  332. }
  333. return i;
  334. }
  335. /**
  336. * Check if the top & left blocks are available if needed and
  337. * change the dc mode so it only uses the available blocks.
  338. */
  339. int ff_h264_check_intra4x4_pred_mode(H264Context *h)
  340. {
  341. static const int8_t top[12] = {
  342. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  343. };
  344. static const int8_t left[12] = {
  345. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  346. };
  347. int i;
  348. if (!(h->top_samples_available & 0x8000)) {
  349. for (i = 0; i < 4; i++) {
  350. int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
  351. if (status < 0) {
  352. av_log(h->avctx, AV_LOG_ERROR,
  353. "top block unavailable for requested intra4x4 mode %d at %d %d\n",
  354. status, h->mb_x, h->mb_y);
  355. return -1;
  356. } else if (status) {
  357. h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
  358. }
  359. }
  360. }
  361. if ((h->left_samples_available & 0x8888) != 0x8888) {
  362. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  363. for (i = 0; i < 4; i++)
  364. if (!(h->left_samples_available & mask[i])) {
  365. int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
  366. if (status < 0) {
  367. av_log(h->avctx, AV_LOG_ERROR,
  368. "left block unavailable for requested intra4x4 mode %d at %d %d\n",
  369. status, h->mb_x, h->mb_y);
  370. return -1;
  371. } else if (status) {
  372. h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
  373. }
  374. }
  375. }
  376. return 0;
  377. } // FIXME cleanup like ff_h264_check_intra_pred_mode
  378. /**
  379. * Check if the top & left blocks are available if needed and
  380. * change the dc mode so it only uses the available blocks.
  381. */
  382. int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
  383. {
  384. static const int8_t top[7] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  385. static const int8_t left[7] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  386. if (mode > 6U) {
  387. av_log(h->avctx, AV_LOG_ERROR,
  388. "out of range intra chroma pred mode at %d %d\n",
  389. h->mb_x, h->mb_y);
  390. return -1;
  391. }
  392. if (!(h->top_samples_available & 0x8000)) {
  393. mode = top[mode];
  394. if (mode < 0) {
  395. av_log(h->avctx, AV_LOG_ERROR,
  396. "top block unavailable for requested intra mode at %d %d\n",
  397. h->mb_x, h->mb_y);
  398. return -1;
  399. }
  400. }
  401. if ((h->left_samples_available & 0x8080) != 0x8080) {
  402. mode = left[mode];
  403. if (is_chroma && (h->left_samples_available & 0x8080)) {
  404. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  405. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  406. (!(h->left_samples_available & 0x8000)) +
  407. 2 * (mode == DC_128_PRED8x8);
  408. }
  409. if (mode < 0) {
  410. av_log(h->avctx, AV_LOG_ERROR,
  411. "left block unavailable for requested intra mode at %d %d\n",
  412. h->mb_x, h->mb_y);
  413. return -1;
  414. }
  415. }
  416. return mode;
  417. }
  418. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
  419. int *dst_length, int *consumed, int length)
  420. {
  421. int i, si, di;
  422. uint8_t *dst;
  423. int bufidx;
  424. // src[0]&0x80; // forbidden bit
  425. h->nal_ref_idc = src[0] >> 5;
  426. h->nal_unit_type = src[0] & 0x1F;
  427. src++;
  428. length--;
  429. #define STARTCODE_TEST \
  430. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  431. if (src[i + 2] != 3) { \
  432. /* startcode, so we must be past the end */ \
  433. length = i; \
  434. } \
  435. break; \
  436. }
  437. #if HAVE_FAST_UNALIGNED
  438. #define FIND_FIRST_ZERO \
  439. if (i > 0 && !src[i]) \
  440. i--; \
  441. while (src[i]) \
  442. i++
  443. #if HAVE_FAST_64BIT
  444. for (i = 0; i + 1 < length; i += 9) {
  445. if (!((~AV_RN64A(src + i) &
  446. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  447. 0x8000800080008080ULL))
  448. continue;
  449. FIND_FIRST_ZERO;
  450. STARTCODE_TEST;
  451. i -= 7;
  452. }
  453. #else
  454. for (i = 0; i + 1 < length; i += 5) {
  455. if (!((~AV_RN32A(src + i) &
  456. (AV_RN32A(src + i) - 0x01000101U)) &
  457. 0x80008080U))
  458. continue;
  459. FIND_FIRST_ZERO;
  460. STARTCODE_TEST;
  461. i -= 3;
  462. }
  463. #endif
  464. #else
  465. for (i = 0; i + 1 < length; i += 2) {
  466. if (src[i])
  467. continue;
  468. if (i > 0 && src[i - 1] == 0)
  469. i--;
  470. STARTCODE_TEST;
  471. }
  472. #endif
  473. if (i >= length - 1) { // no escaped 0
  474. *dst_length = length;
  475. *consumed = length + 1; // +1 for the header
  476. return src;
  477. }
  478. // use second escape buffer for inter data
  479. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
  480. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx],
  481. length + FF_INPUT_BUFFER_PADDING_SIZE);
  482. dst = h->rbsp_buffer[bufidx];
  483. if (dst == NULL)
  484. return NULL;
  485. memcpy(dst, src, i);
  486. si = di = i;
  487. while (si + 2 < length) {
  488. // remove escapes (very rare 1:2^22)
  489. if (src[si + 2] > 3) {
  490. dst[di++] = src[si++];
  491. dst[di++] = src[si++];
  492. } else if (src[si] == 0 && src[si + 1] == 0) {
  493. if (src[si + 2] == 3) { // escape
  494. dst[di++] = 0;
  495. dst[di++] = 0;
  496. si += 3;
  497. continue;
  498. } else // next start code
  499. goto nsc;
  500. }
  501. dst[di++] = src[si++];
  502. }
  503. while (si < length)
  504. dst[di++] = src[si++];
  505. nsc:
  506. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  507. *dst_length = di;
  508. *consumed = si + 1; // +1 for the header
  509. /* FIXME store exact number of bits in the getbitcontext
  510. * (it is needed for decoding) */
  511. return dst;
  512. }
  513. /**
  514. * Identify the exact end of the bitstream
  515. * @return the length of the trailing, or 0 if damaged
  516. */
  517. static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
  518. {
  519. int v = *src;
  520. int r;
  521. tprintf(h->avctx, "rbsp trailing %X\n", v);
  522. for (r = 1; r < 9; r++) {
  523. if (v & 1)
  524. return r;
  525. v >>= 1;
  526. }
  527. return 0;
  528. }
  529. static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
  530. int height, int y_offset, int list)
  531. {
  532. int raw_my = h->mv_cache[list][scan8[n]][1];
  533. int filter_height_up = (raw_my & 3) ? 2 : 0;
  534. int filter_height_down = (raw_my & 3) ? 3 : 0;
  535. int full_my = (raw_my >> 2) + y_offset;
  536. int top = full_my - filter_height_up;
  537. int bottom = full_my + filter_height_down + height;
  538. return FFMAX(abs(top), bottom);
  539. }
  540. static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
  541. int height, int y_offset, int list0,
  542. int list1, int *nrefs)
  543. {
  544. int my;
  545. y_offset += 16 * (h->mb_y >> MB_FIELD(h));
  546. if (list0) {
  547. int ref_n = h->ref_cache[0][scan8[n]];
  548. Picture *ref = &h->ref_list[0][ref_n];
  549. // Error resilience puts the current picture in the ref list.
  550. // Don't try to wait on these as it will cause a deadlock.
  551. // Fields can wait on each other, though.
  552. if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
  553. (ref->reference & 3) != h->picture_structure) {
  554. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
  555. if (refs[0][ref_n] < 0)
  556. nrefs[0] += 1;
  557. refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
  558. }
  559. }
  560. if (list1) {
  561. int ref_n = h->ref_cache[1][scan8[n]];
  562. Picture *ref = &h->ref_list[1][ref_n];
  563. if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
  564. (ref->reference & 3) != h->picture_structure) {
  565. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
  566. if (refs[1][ref_n] < 0)
  567. nrefs[1] += 1;
  568. refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
  569. }
  570. }
  571. }
  572. /**
  573. * Wait until all reference frames are available for MC operations.
  574. *
  575. * @param h the H264 context
  576. */
  577. static void await_references(H264Context *h)
  578. {
  579. const int mb_xy = h->mb_xy;
  580. const int mb_type = h->cur_pic.mb_type[mb_xy];
  581. int refs[2][48];
  582. int nrefs[2] = { 0 };
  583. int ref, list;
  584. memset(refs, -1, sizeof(refs));
  585. if (IS_16X16(mb_type)) {
  586. get_lowest_part_y(h, refs, 0, 16, 0,
  587. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  588. } else if (IS_16X8(mb_type)) {
  589. get_lowest_part_y(h, refs, 0, 8, 0,
  590. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  591. get_lowest_part_y(h, refs, 8, 8, 8,
  592. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  593. } else if (IS_8X16(mb_type)) {
  594. get_lowest_part_y(h, refs, 0, 16, 0,
  595. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  596. get_lowest_part_y(h, refs, 4, 16, 0,
  597. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  598. } else {
  599. int i;
  600. assert(IS_8X8(mb_type));
  601. for (i = 0; i < 4; i++) {
  602. const int sub_mb_type = h->sub_mb_type[i];
  603. const int n = 4 * i;
  604. int y_offset = (i & 2) << 2;
  605. if (IS_SUB_8X8(sub_mb_type)) {
  606. get_lowest_part_y(h, refs, n, 8, y_offset,
  607. IS_DIR(sub_mb_type, 0, 0),
  608. IS_DIR(sub_mb_type, 0, 1),
  609. nrefs);
  610. } else if (IS_SUB_8X4(sub_mb_type)) {
  611. get_lowest_part_y(h, refs, n, 4, y_offset,
  612. IS_DIR(sub_mb_type, 0, 0),
  613. IS_DIR(sub_mb_type, 0, 1),
  614. nrefs);
  615. get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
  616. IS_DIR(sub_mb_type, 0, 0),
  617. IS_DIR(sub_mb_type, 0, 1),
  618. nrefs);
  619. } else if (IS_SUB_4X8(sub_mb_type)) {
  620. get_lowest_part_y(h, refs, n, 8, y_offset,
  621. IS_DIR(sub_mb_type, 0, 0),
  622. IS_DIR(sub_mb_type, 0, 1),
  623. nrefs);
  624. get_lowest_part_y(h, refs, n + 1, 8, y_offset,
  625. IS_DIR(sub_mb_type, 0, 0),
  626. IS_DIR(sub_mb_type, 0, 1),
  627. nrefs);
  628. } else {
  629. int j;
  630. assert(IS_SUB_4X4(sub_mb_type));
  631. for (j = 0; j < 4; j++) {
  632. int sub_y_offset = y_offset + 2 * (j & 2);
  633. get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
  634. IS_DIR(sub_mb_type, 0, 0),
  635. IS_DIR(sub_mb_type, 0, 1),
  636. nrefs);
  637. }
  638. }
  639. }
  640. }
  641. for (list = h->list_count - 1; list >= 0; list--)
  642. for (ref = 0; ref < 48 && nrefs[list]; ref++) {
  643. int row = refs[list][ref];
  644. if (row >= 0) {
  645. Picture *ref_pic = &h->ref_list[list][ref];
  646. int ref_field = ref_pic->reference - 1;
  647. int ref_field_picture = ref_pic->field_picture;
  648. int pic_height = 16 * h->mb_height >> ref_field_picture;
  649. row <<= MB_MBAFF(h);
  650. nrefs[list]--;
  651. if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
  652. ff_thread_await_progress(&ref_pic->tf,
  653. FFMIN((row >> 1) - !(row & 1),
  654. pic_height - 1),
  655. 1);
  656. ff_thread_await_progress(&ref_pic->tf,
  657. FFMIN((row >> 1), pic_height - 1),
  658. 0);
  659. } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
  660. ff_thread_await_progress(&ref_pic->tf,
  661. FFMIN(row * 2 + ref_field,
  662. pic_height - 1),
  663. 0);
  664. } else if (FIELD_PICTURE(h)) {
  665. ff_thread_await_progress(&ref_pic->tf,
  666. FFMIN(row, pic_height - 1),
  667. ref_field);
  668. } else {
  669. ff_thread_await_progress(&ref_pic->tf,
  670. FFMIN(row, pic_height - 1),
  671. 0);
  672. }
  673. }
  674. }
  675. }
  676. static av_always_inline void mc_dir_part(H264Context *h, Picture *pic,
  677. int n, int square, int height,
  678. int delta, int list,
  679. uint8_t *dest_y, uint8_t *dest_cb,
  680. uint8_t *dest_cr,
  681. int src_x_offset, int src_y_offset,
  682. qpel_mc_func *qpix_op,
  683. h264_chroma_mc_func chroma_op,
  684. int pixel_shift, int chroma_idc)
  685. {
  686. const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
  687. int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
  688. const int luma_xy = (mx & 3) + ((my & 3) << 2);
  689. int offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
  690. uint8_t *src_y = pic->f.data[0] + offset;
  691. uint8_t *src_cb, *src_cr;
  692. int extra_width = 0;
  693. int extra_height = 0;
  694. int emu = 0;
  695. const int full_mx = mx >> 2;
  696. const int full_my = my >> 2;
  697. const int pic_width = 16 * h->mb_width;
  698. const int pic_height = 16 * h->mb_height >> MB_FIELD(h);
  699. int ysh;
  700. if (mx & 7)
  701. extra_width -= 3;
  702. if (my & 7)
  703. extra_height -= 3;
  704. if (full_mx < 0 - extra_width ||
  705. full_my < 0 - extra_height ||
  706. full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
  707. full_my + 16 /*FIXME*/ > pic_height + extra_height) {
  708. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  709. src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
  710. h->mb_linesize,
  711. 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
  712. full_my - 2, pic_width, pic_height);
  713. src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  714. emu = 1;
  715. }
  716. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
  717. if (!square)
  718. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  719. if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
  720. return;
  721. if (chroma_idc == 3 /* yuv444 */) {
  722. src_cb = pic->f.data[1] + offset;
  723. if (emu) {
  724. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  725. src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
  726. h->mb_linesize,
  727. 16 + 5, 16 + 5 /*FIXME*/,
  728. full_mx - 2, full_my - 2,
  729. pic_width, pic_height);
  730. src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  731. }
  732. qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
  733. if (!square)
  734. qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
  735. src_cr = pic->f.data[2] + offset;
  736. if (emu) {
  737. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  738. src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
  739. h->mb_linesize,
  740. 16 + 5, 16 + 5 /*FIXME*/,
  741. full_mx - 2, full_my - 2,
  742. pic_width, pic_height);
  743. src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  744. }
  745. qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
  746. if (!square)
  747. qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
  748. return;
  749. }
  750. ysh = 3 - (chroma_idc == 2 /* yuv422 */);
  751. if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) {
  752. // chroma offset when predicting from a field of opposite parity
  753. my += 2 * ((h->mb_y & 1) - (pic->reference - 1));
  754. emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
  755. }
  756. src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
  757. (my >> ysh) * h->mb_uvlinesize;
  758. src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
  759. (my >> ysh) * h->mb_uvlinesize;
  760. if (emu) {
  761. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb, h->mb_uvlinesize,
  762. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  763. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  764. src_cb = h->edge_emu_buffer;
  765. }
  766. chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
  767. height >> (chroma_idc == 1 /* yuv420 */),
  768. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  769. if (emu) {
  770. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr, h->mb_uvlinesize,
  771. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  772. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  773. src_cr = h->edge_emu_buffer;
  774. }
  775. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
  776. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  777. }
  778. static av_always_inline void mc_part_std(H264Context *h, int n, int square,
  779. int height, int delta,
  780. uint8_t *dest_y, uint8_t *dest_cb,
  781. uint8_t *dest_cr,
  782. int x_offset, int y_offset,
  783. qpel_mc_func *qpix_put,
  784. h264_chroma_mc_func chroma_put,
  785. qpel_mc_func *qpix_avg,
  786. h264_chroma_mc_func chroma_avg,
  787. int list0, int list1,
  788. int pixel_shift, int chroma_idc)
  789. {
  790. qpel_mc_func *qpix_op = qpix_put;
  791. h264_chroma_mc_func chroma_op = chroma_put;
  792. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  793. if (chroma_idc == 3 /* yuv444 */) {
  794. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  795. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  796. } else if (chroma_idc == 2 /* yuv422 */) {
  797. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  798. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  799. } else { /* yuv420 */
  800. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  801. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  802. }
  803. x_offset += 8 * h->mb_x;
  804. y_offset += 8 * (h->mb_y >> MB_FIELD(h));
  805. if (list0) {
  806. Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
  807. mc_dir_part(h, ref, n, square, height, delta, 0,
  808. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  809. qpix_op, chroma_op, pixel_shift, chroma_idc);
  810. qpix_op = qpix_avg;
  811. chroma_op = chroma_avg;
  812. }
  813. if (list1) {
  814. Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
  815. mc_dir_part(h, ref, n, square, height, delta, 1,
  816. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  817. qpix_op, chroma_op, pixel_shift, chroma_idc);
  818. }
  819. }
  820. static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
  821. int height, int delta,
  822. uint8_t *dest_y, uint8_t *dest_cb,
  823. uint8_t *dest_cr,
  824. int x_offset, int y_offset,
  825. qpel_mc_func *qpix_put,
  826. h264_chroma_mc_func chroma_put,
  827. h264_weight_func luma_weight_op,
  828. h264_weight_func chroma_weight_op,
  829. h264_biweight_func luma_weight_avg,
  830. h264_biweight_func chroma_weight_avg,
  831. int list0, int list1,
  832. int pixel_shift, int chroma_idc)
  833. {
  834. int chroma_height;
  835. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  836. if (chroma_idc == 3 /* yuv444 */) {
  837. chroma_height = height;
  838. chroma_weight_avg = luma_weight_avg;
  839. chroma_weight_op = luma_weight_op;
  840. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  841. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  842. } else if (chroma_idc == 2 /* yuv422 */) {
  843. chroma_height = height;
  844. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  845. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  846. } else { /* yuv420 */
  847. chroma_height = height >> 1;
  848. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  849. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  850. }
  851. x_offset += 8 * h->mb_x;
  852. y_offset += 8 * (h->mb_y >> MB_FIELD(h));
  853. if (list0 && list1) {
  854. /* don't optimize for luma-only case, since B-frames usually
  855. * use implicit weights => chroma too. */
  856. uint8_t *tmp_cb = h->bipred_scratchpad;
  857. uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
  858. uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
  859. int refn0 = h->ref_cache[0][scan8[n]];
  860. int refn1 = h->ref_cache[1][scan8[n]];
  861. mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
  862. dest_y, dest_cb, dest_cr,
  863. x_offset, y_offset, qpix_put, chroma_put,
  864. pixel_shift, chroma_idc);
  865. mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
  866. tmp_y, tmp_cb, tmp_cr,
  867. x_offset, y_offset, qpix_put, chroma_put,
  868. pixel_shift, chroma_idc);
  869. if (h->use_weight == 2) {
  870. int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1];
  871. int weight1 = 64 - weight0;
  872. luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
  873. height, 5, weight0, weight1, 0);
  874. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
  875. chroma_height, 5, weight0, weight1, 0);
  876. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
  877. chroma_height, 5, weight0, weight1, 0);
  878. } else {
  879. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
  880. h->luma_log2_weight_denom,
  881. h->luma_weight[refn0][0][0],
  882. h->luma_weight[refn1][1][0],
  883. h->luma_weight[refn0][0][1] +
  884. h->luma_weight[refn1][1][1]);
  885. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
  886. h->chroma_log2_weight_denom,
  887. h->chroma_weight[refn0][0][0][0],
  888. h->chroma_weight[refn1][1][0][0],
  889. h->chroma_weight[refn0][0][0][1] +
  890. h->chroma_weight[refn1][1][0][1]);
  891. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
  892. h->chroma_log2_weight_denom,
  893. h->chroma_weight[refn0][0][1][0],
  894. h->chroma_weight[refn1][1][1][0],
  895. h->chroma_weight[refn0][0][1][1] +
  896. h->chroma_weight[refn1][1][1][1]);
  897. }
  898. } else {
  899. int list = list1 ? 1 : 0;
  900. int refn = h->ref_cache[list][scan8[n]];
  901. Picture *ref = &h->ref_list[list][refn];
  902. mc_dir_part(h, ref, n, square, height, delta, list,
  903. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  904. qpix_put, chroma_put, pixel_shift, chroma_idc);
  905. luma_weight_op(dest_y, h->mb_linesize, height,
  906. h->luma_log2_weight_denom,
  907. h->luma_weight[refn][list][0],
  908. h->luma_weight[refn][list][1]);
  909. if (h->use_weight_chroma) {
  910. chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
  911. h->chroma_log2_weight_denom,
  912. h->chroma_weight[refn][list][0][0],
  913. h->chroma_weight[refn][list][0][1]);
  914. chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
  915. h->chroma_log2_weight_denom,
  916. h->chroma_weight[refn][list][1][0],
  917. h->chroma_weight[refn][list][1][1]);
  918. }
  919. }
  920. }
  921. static av_always_inline void prefetch_motion(H264Context *h, int list,
  922. int pixel_shift, int chroma_idc)
  923. {
  924. /* fetch pixels for estimated mv 4 macroblocks ahead
  925. * optimized for 64byte cache lines */
  926. const int refn = h->ref_cache[list][scan8[0]];
  927. if (refn >= 0) {
  928. const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8;
  929. const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y;
  930. uint8_t **src = h->ref_list[list][refn].f.data;
  931. int off = (mx << pixel_shift) +
  932. (my + (h->mb_x & 3) * 4) * h->mb_linesize +
  933. (64 << pixel_shift);
  934. h->vdsp.prefetch(src[0] + off, h->linesize, 4);
  935. if (chroma_idc == 3 /* yuv444 */) {
  936. h->vdsp.prefetch(src[1] + off, h->linesize, 4);
  937. h->vdsp.prefetch(src[2] + off, h->linesize, 4);
  938. } else {
  939. off = ((mx >> 1) << pixel_shift) +
  940. ((my >> 1) + (h->mb_x & 7)) * h->uvlinesize +
  941. (64 << pixel_shift);
  942. h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
  943. }
  944. }
  945. }
  946. static void free_tables(H264Context *h, int free_rbsp)
  947. {
  948. int i;
  949. H264Context *hx;
  950. av_freep(&h->intra4x4_pred_mode);
  951. av_freep(&h->chroma_pred_mode_table);
  952. av_freep(&h->cbp_table);
  953. av_freep(&h->mvd_table[0]);
  954. av_freep(&h->mvd_table[1]);
  955. av_freep(&h->direct_table);
  956. av_freep(&h->non_zero_count);
  957. av_freep(&h->slice_table_base);
  958. h->slice_table = NULL;
  959. av_freep(&h->list_counts);
  960. av_freep(&h->mb2b_xy);
  961. av_freep(&h->mb2br_xy);
  962. av_buffer_pool_uninit(&h->qscale_table_pool);
  963. av_buffer_pool_uninit(&h->mb_type_pool);
  964. av_buffer_pool_uninit(&h->motion_val_pool);
  965. av_buffer_pool_uninit(&h->ref_index_pool);
  966. if (free_rbsp && h->DPB) {
  967. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  968. unref_picture(h, &h->DPB[i]);
  969. av_freep(&h->DPB);
  970. } else if (h->DPB) {
  971. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  972. h->DPB[i].needs_realloc = 1;
  973. }
  974. h->cur_pic_ptr = NULL;
  975. for (i = 0; i < MAX_THREADS; i++) {
  976. hx = h->thread_context[i];
  977. if (!hx)
  978. continue;
  979. av_freep(&hx->top_borders[1]);
  980. av_freep(&hx->top_borders[0]);
  981. av_freep(&hx->bipred_scratchpad);
  982. av_freep(&hx->edge_emu_buffer);
  983. av_freep(&hx->dc_val_base);
  984. av_freep(&hx->me.scratchpad);
  985. av_freep(&hx->er.mb_index2xy);
  986. av_freep(&hx->er.error_status_table);
  987. av_freep(&hx->er.er_temp_buffer);
  988. av_freep(&hx->er.mbintra_table);
  989. av_freep(&hx->er.mbskip_table);
  990. if (free_rbsp) {
  991. av_freep(&hx->rbsp_buffer[1]);
  992. av_freep(&hx->rbsp_buffer[0]);
  993. hx->rbsp_buffer_size[0] = 0;
  994. hx->rbsp_buffer_size[1] = 0;
  995. }
  996. if (i)
  997. av_freep(&h->thread_context[i]);
  998. }
  999. }
  1000. static void init_dequant8_coeff_table(H264Context *h)
  1001. {
  1002. int i, j, q, x;
  1003. const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
  1004. for (i = 0; i < 6; i++) {
  1005. h->dequant8_coeff[i] = h->dequant8_buffer[i];
  1006. for (j = 0; j < i; j++)
  1007. if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
  1008. 64 * sizeof(uint8_t))) {
  1009. h->dequant8_coeff[i] = h->dequant8_buffer[j];
  1010. break;
  1011. }
  1012. if (j < i)
  1013. continue;
  1014. for (q = 0; q < max_qp + 1; q++) {
  1015. int shift = div6[q];
  1016. int idx = rem6[q];
  1017. for (x = 0; x < 64; x++)
  1018. h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
  1019. ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
  1020. h->pps.scaling_matrix8[i][x]) << shift;
  1021. }
  1022. }
  1023. }
  1024. static void init_dequant4_coeff_table(H264Context *h)
  1025. {
  1026. int i, j, q, x;
  1027. const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
  1028. for (i = 0; i < 6; i++) {
  1029. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  1030. for (j = 0; j < i; j++)
  1031. if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
  1032. 16 * sizeof(uint8_t))) {
  1033. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  1034. break;
  1035. }
  1036. if (j < i)
  1037. continue;
  1038. for (q = 0; q < max_qp + 1; q++) {
  1039. int shift = div6[q] + 2;
  1040. int idx = rem6[q];
  1041. for (x = 0; x < 16; x++)
  1042. h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
  1043. ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
  1044. h->pps.scaling_matrix4[i][x]) << shift;
  1045. }
  1046. }
  1047. }
  1048. static void init_dequant_tables(H264Context *h)
  1049. {
  1050. int i, x;
  1051. init_dequant4_coeff_table(h);
  1052. if (h->pps.transform_8x8_mode)
  1053. init_dequant8_coeff_table(h);
  1054. if (h->sps.transform_bypass) {
  1055. for (i = 0; i < 6; i++)
  1056. for (x = 0; x < 16; x++)
  1057. h->dequant4_coeff[i][0][x] = 1 << 6;
  1058. if (h->pps.transform_8x8_mode)
  1059. for (i = 0; i < 6; i++)
  1060. for (x = 0; x < 64; x++)
  1061. h->dequant8_coeff[i][0][x] = 1 << 6;
  1062. }
  1063. }
  1064. int ff_h264_alloc_tables(H264Context *h)
  1065. {
  1066. const int big_mb_num = h->mb_stride * (h->mb_height + 1);
  1067. const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count;
  1068. int x, y, i;
  1069. FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
  1070. row_mb_num * 8 * sizeof(uint8_t), fail)
  1071. FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
  1072. big_mb_num * 48 * sizeof(uint8_t), fail)
  1073. FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
  1074. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
  1075. FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
  1076. big_mb_num * sizeof(uint16_t), fail)
  1077. FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
  1078. big_mb_num * sizeof(uint8_t), fail)
  1079. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
  1080. 16 * row_mb_num * sizeof(uint8_t), fail);
  1081. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
  1082. 16 * row_mb_num * sizeof(uint8_t), fail);
  1083. FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
  1084. 4 * big_mb_num * sizeof(uint8_t), fail);
  1085. FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
  1086. big_mb_num * sizeof(uint8_t), fail)
  1087. memset(h->slice_table_base, -1,
  1088. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
  1089. h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
  1090. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
  1091. big_mb_num * sizeof(uint32_t), fail);
  1092. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
  1093. big_mb_num * sizeof(uint32_t), fail);
  1094. for (y = 0; y < h->mb_height; y++)
  1095. for (x = 0; x < h->mb_width; x++) {
  1096. const int mb_xy = x + y * h->mb_stride;
  1097. const int b_xy = 4 * x + 4 * y * h->b_stride;
  1098. h->mb2b_xy[mb_xy] = b_xy;
  1099. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
  1100. }
  1101. if (!h->dequant4_coeff[0])
  1102. init_dequant_tables(h);
  1103. if (!h->DPB) {
  1104. h->DPB = av_mallocz_array(MAX_PICTURE_COUNT, sizeof(*h->DPB));
  1105. if (!h->DPB)
  1106. return AVERROR(ENOMEM);
  1107. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  1108. avcodec_get_frame_defaults(&h->DPB[i].f);
  1109. avcodec_get_frame_defaults(&h->cur_pic.f);
  1110. }
  1111. return 0;
  1112. fail:
  1113. free_tables(h, 1);
  1114. return -1;
  1115. }
  1116. /**
  1117. * Mimic alloc_tables(), but for every context thread.
  1118. */
  1119. static void clone_tables(H264Context *dst, H264Context *src, int i)
  1120. {
  1121. dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * src->mb_stride;
  1122. dst->non_zero_count = src->non_zero_count;
  1123. dst->slice_table = src->slice_table;
  1124. dst->cbp_table = src->cbp_table;
  1125. dst->mb2b_xy = src->mb2b_xy;
  1126. dst->mb2br_xy = src->mb2br_xy;
  1127. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  1128. dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * src->mb_stride;
  1129. dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * src->mb_stride;
  1130. dst->direct_table = src->direct_table;
  1131. dst->list_counts = src->list_counts;
  1132. dst->DPB = src->DPB;
  1133. dst->cur_pic_ptr = src->cur_pic_ptr;
  1134. dst->cur_pic = src->cur_pic;
  1135. dst->bipred_scratchpad = NULL;
  1136. dst->edge_emu_buffer = NULL;
  1137. dst->me.scratchpad = NULL;
  1138. ff_h264_pred_init(&dst->hpc, src->avctx->codec_id, src->sps.bit_depth_luma,
  1139. src->sps.chroma_format_idc);
  1140. }
  1141. /**
  1142. * Init context
  1143. * Allocate buffers which are not shared amongst multiple threads.
  1144. */
  1145. static int context_init(H264Context *h)
  1146. {
  1147. ERContext *er = &h->er;
  1148. int mb_array_size = h->mb_height * h->mb_stride;
  1149. int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
  1150. int c_size = h->mb_stride * (h->mb_height + 1);
  1151. int yc_size = y_size + 2 * c_size;
  1152. int x, y, i;
  1153. FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[0],
  1154. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
  1155. FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[1],
  1156. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
  1157. h->ref_cache[0][scan8[5] + 1] =
  1158. h->ref_cache[0][scan8[7] + 1] =
  1159. h->ref_cache[0][scan8[13] + 1] =
  1160. h->ref_cache[1][scan8[5] + 1] =
  1161. h->ref_cache[1][scan8[7] + 1] =
  1162. h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  1163. if (CONFIG_ERROR_RESILIENCE) {
  1164. /* init ER */
  1165. er->avctx = h->avctx;
  1166. er->dsp = &h->dsp;
  1167. er->decode_mb = h264_er_decode_mb;
  1168. er->opaque = h;
  1169. er->quarter_sample = 1;
  1170. er->mb_num = h->mb_num;
  1171. er->mb_width = h->mb_width;
  1172. er->mb_height = h->mb_height;
  1173. er->mb_stride = h->mb_stride;
  1174. er->b8_stride = h->mb_width * 2 + 1;
  1175. FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
  1176. fail); // error ressilience code looks cleaner with this
  1177. for (y = 0; y < h->mb_height; y++)
  1178. for (x = 0; x < h->mb_width; x++)
  1179. er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
  1180. er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
  1181. h->mb_stride + h->mb_width;
  1182. FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
  1183. mb_array_size * sizeof(uint8_t), fail);
  1184. FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
  1185. memset(er->mbintra_table, 1, mb_array_size);
  1186. FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
  1187. FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride,
  1188. fail);
  1189. FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
  1190. er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
  1191. er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
  1192. er->dc_val[2] = er->dc_val[1] + c_size;
  1193. for (i = 0; i < yc_size; i++)
  1194. h->dc_val_base[i] = 1024;
  1195. }
  1196. return 0;
  1197. fail:
  1198. return -1; // free_tables will clean up for us
  1199. }
  1200. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  1201. int parse_extradata);
  1202. int ff_h264_decode_extradata(H264Context *h)
  1203. {
  1204. AVCodecContext *avctx = h->avctx;
  1205. if (avctx->extradata[0] == 1) {
  1206. int i, cnt, nalsize;
  1207. unsigned char *p = avctx->extradata;
  1208. h->is_avc = 1;
  1209. if (avctx->extradata_size < 7) {
  1210. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  1211. return -1;
  1212. }
  1213. /* sps and pps in the avcC always have length coded with 2 bytes,
  1214. * so put a fake nal_length_size = 2 while parsing them */
  1215. h->nal_length_size = 2;
  1216. // Decode sps from avcC
  1217. cnt = *(p + 5) & 0x1f; // Number of sps
  1218. p += 6;
  1219. for (i = 0; i < cnt; i++) {
  1220. nalsize = AV_RB16(p) + 2;
  1221. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  1222. return -1;
  1223. if (decode_nal_units(h, p, nalsize, 1) < 0) {
  1224. av_log(avctx, AV_LOG_ERROR,
  1225. "Decoding sps %d from avcC failed\n", i);
  1226. return -1;
  1227. }
  1228. p += nalsize;
  1229. }
  1230. // Decode pps from avcC
  1231. cnt = *(p++); // Number of pps
  1232. for (i = 0; i < cnt; i++) {
  1233. nalsize = AV_RB16(p) + 2;
  1234. if (p - avctx->extradata + nalsize > avctx->extradata_size)
  1235. return -1;
  1236. if (decode_nal_units(h, p, nalsize, 1) < 0) {
  1237. av_log(avctx, AV_LOG_ERROR,
  1238. "Decoding pps %d from avcC failed\n", i);
  1239. return -1;
  1240. }
  1241. p += nalsize;
  1242. }
  1243. // Now store right nal length size, that will be used to parse all other nals
  1244. h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;
  1245. } else {
  1246. h->is_avc = 0;
  1247. if (decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1) < 0)
  1248. return -1;
  1249. }
  1250. return 0;
  1251. }
  1252. av_cold int ff_h264_decode_init(AVCodecContext *avctx)
  1253. {
  1254. H264Context *h = avctx->priv_data;
  1255. int i;
  1256. h->avctx = avctx;
  1257. h->bit_depth_luma = 8;
  1258. h->chroma_format_idc = 1;
  1259. ff_h264dsp_init(&h->h264dsp, 8, 1);
  1260. ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
  1261. ff_h264qpel_init(&h->h264qpel, 8);
  1262. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
  1263. h->dequant_coeff_pps = -1;
  1264. /* needed so that IDCT permutation is known early */
  1265. if (CONFIG_ERROR_RESILIENCE)
  1266. ff_dsputil_init(&h->dsp, h->avctx);
  1267. ff_videodsp_init(&h->vdsp, 8);
  1268. memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
  1269. memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
  1270. h->picture_structure = PICT_FRAME;
  1271. h->slice_context_count = 1;
  1272. h->workaround_bugs = avctx->workaround_bugs;
  1273. h->flags = avctx->flags;
  1274. /* set defaults */
  1275. // s->decode_mb = ff_h263_decode_mb;
  1276. if (!avctx->has_b_frames)
  1277. h->low_delay = 1;
  1278. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1279. ff_h264_decode_init_vlc();
  1280. h->pixel_shift = 0;
  1281. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  1282. h->thread_context[0] = h;
  1283. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  1284. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  1285. h->last_pocs[i] = INT_MIN;
  1286. h->prev_poc_msb = 1 << 16;
  1287. h->x264_build = -1;
  1288. ff_h264_reset_sei(h);
  1289. if (avctx->codec_id == AV_CODEC_ID_H264) {
  1290. if (avctx->ticks_per_frame == 1)
  1291. h->avctx->time_base.den *= 2;
  1292. avctx->ticks_per_frame = 2;
  1293. }
  1294. if (avctx->extradata_size > 0 && avctx->extradata &&
  1295. ff_h264_decode_extradata(h))
  1296. return -1;
  1297. if (h->sps.bitstream_restriction_flag &&
  1298. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  1299. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  1300. h->low_delay = 0;
  1301. }
  1302. avctx->internal->allocate_progress = 1;
  1303. return 0;
  1304. }
  1305. #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
  1306. #undef REBASE_PICTURE
  1307. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  1308. ((pic && pic >= old_ctx->DPB && \
  1309. pic < old_ctx->DPB + MAX_PICTURE_COUNT) ? \
  1310. &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
  1311. static void copy_picture_range(Picture **to, Picture **from, int count,
  1312. H264Context *new_base,
  1313. H264Context *old_base)
  1314. {
  1315. int i;
  1316. for (i = 0; i < count; i++) {
  1317. assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
  1318. IN_RANGE(from[i], old_base->DPB,
  1319. sizeof(Picture) * MAX_PICTURE_COUNT) ||
  1320. !from[i]));
  1321. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  1322. }
  1323. }
  1324. static void copy_parameter_set(void **to, void **from, int count, int size)
  1325. {
  1326. int i;
  1327. for (i = 0; i < count; i++) {
  1328. if (to[i] && !from[i])
  1329. av_freep(&to[i]);
  1330. else if (from[i] && !to[i])
  1331. to[i] = av_malloc(size);
  1332. if (from[i])
  1333. memcpy(to[i], from[i], size);
  1334. }
  1335. }
  1336. static int decode_init_thread_copy(AVCodecContext *avctx)
  1337. {
  1338. H264Context *h = avctx->priv_data;
  1339. if (!avctx->internal->is_copy)
  1340. return 0;
  1341. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1342. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1343. h->context_initialized = 0;
  1344. return 0;
  1345. }
  1346. #define copy_fields(to, from, start_field, end_field) \
  1347. memcpy(&to->start_field, &from->start_field, \
  1348. (char *)&to->end_field - (char *)&to->start_field)
  1349. static int h264_slice_header_init(H264Context *, int);
  1350. static int h264_set_parameter_from_sps(H264Context *h);
  1351. static int decode_update_thread_context(AVCodecContext *dst,
  1352. const AVCodecContext *src)
  1353. {
  1354. H264Context *h = dst->priv_data, *h1 = src->priv_data;
  1355. int inited = h->context_initialized, err = 0;
  1356. int context_reinitialized = 0;
  1357. int i, ret;
  1358. if (dst == src || !h1->context_initialized)
  1359. return 0;
  1360. if (inited &&
  1361. (h->width != h1->width ||
  1362. h->height != h1->height ||
  1363. h->mb_width != h1->mb_width ||
  1364. h->mb_height != h1->mb_height ||
  1365. h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
  1366. h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
  1367. h->sps.colorspace != h1->sps.colorspace)) {
  1368. /* set bits_per_raw_sample to the previous value. the check for changed
  1369. * bit depth in h264_set_parameter_from_sps() uses it and sets it to
  1370. * the current value */
  1371. h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  1372. av_freep(&h->bipred_scratchpad);
  1373. h->width = h1->width;
  1374. h->height = h1->height;
  1375. h->mb_height = h1->mb_height;
  1376. h->mb_width = h1->mb_width;
  1377. h->mb_num = h1->mb_num;
  1378. h->mb_stride = h1->mb_stride;
  1379. h->b_stride = h1->b_stride;
  1380. if ((err = h264_slice_header_init(h, 1)) < 0) {
  1381. av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
  1382. return err;
  1383. }
  1384. context_reinitialized = 1;
  1385. /* update linesize on resize. The decoder doesn't
  1386. * necessarily call h264_frame_start in the new thread */
  1387. h->linesize = h1->linesize;
  1388. h->uvlinesize = h1->uvlinesize;
  1389. /* copy block_offset since frame_start may not be called */
  1390. memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
  1391. }
  1392. if (!inited) {
  1393. for (i = 0; i < MAX_SPS_COUNT; i++)
  1394. av_freep(h->sps_buffers + i);
  1395. for (i = 0; i < MAX_PPS_COUNT; i++)
  1396. av_freep(h->pps_buffers + i);
  1397. memcpy(h, h1, sizeof(*h1));
  1398. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1399. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1400. memset(&h->er, 0, sizeof(h->er));
  1401. memset(&h->me, 0, sizeof(h->me));
  1402. memset(&h->mb, 0, sizeof(h->mb));
  1403. memset(&h->mb_luma_dc, 0, sizeof(h->mb_luma_dc));
  1404. memset(&h->mb_padding, 0, sizeof(h->mb_padding));
  1405. h->context_initialized = 0;
  1406. memset(&h->cur_pic, 0, sizeof(h->cur_pic));
  1407. avcodec_get_frame_defaults(&h->cur_pic.f);
  1408. h->cur_pic.tf.f = &h->cur_pic.f;
  1409. h->avctx = dst;
  1410. h->DPB = NULL;
  1411. h->qscale_table_pool = NULL;
  1412. h->mb_type_pool = NULL;
  1413. h->ref_index_pool = NULL;
  1414. h->motion_val_pool = NULL;
  1415. if (ff_h264_alloc_tables(h) < 0) {
  1416. av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
  1417. return AVERROR(ENOMEM);
  1418. }
  1419. context_init(h);
  1420. for (i = 0; i < 2; i++) {
  1421. h->rbsp_buffer[i] = NULL;
  1422. h->rbsp_buffer_size[i] = 0;
  1423. }
  1424. h->bipred_scratchpad = NULL;
  1425. h->edge_emu_buffer = NULL;
  1426. h->thread_context[0] = h;
  1427. h->context_initialized = 1;
  1428. }
  1429. h->avctx->coded_height = h1->avctx->coded_height;
  1430. h->avctx->coded_width = h1->avctx->coded_width;
  1431. h->avctx->width = h1->avctx->width;
  1432. h->avctx->height = h1->avctx->height;
  1433. h->coded_picture_number = h1->coded_picture_number;
  1434. h->first_field = h1->first_field;
  1435. h->picture_structure = h1->picture_structure;
  1436. h->qscale = h1->qscale;
  1437. h->droppable = h1->droppable;
  1438. h->data_partitioning = h1->data_partitioning;
  1439. h->low_delay = h1->low_delay;
  1440. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1441. unref_picture(h, &h->DPB[i]);
  1442. if (h1->DPB[i].f.data[0] &&
  1443. (ret = ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
  1444. return ret;
  1445. }
  1446. h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
  1447. unref_picture(h, &h->cur_pic);
  1448. if ((ret = ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0)
  1449. return ret;
  1450. h->workaround_bugs = h1->workaround_bugs;
  1451. h->low_delay = h1->low_delay;
  1452. h->droppable = h1->droppable;
  1453. /* frame_start may not be called for the next thread (if it's decoding
  1454. * a bottom field) so this has to be allocated here */
  1455. err = alloc_scratch_buffers(h, h1->linesize);
  1456. if (err < 0)
  1457. return err;
  1458. // extradata/NAL handling
  1459. h->is_avc = h1->is_avc;
  1460. // SPS/PPS
  1461. copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
  1462. MAX_SPS_COUNT, sizeof(SPS));
  1463. h->sps = h1->sps;
  1464. copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
  1465. MAX_PPS_COUNT, sizeof(PPS));
  1466. h->pps = h1->pps;
  1467. // Dequantization matrices
  1468. // FIXME these are big - can they be only copied when PPS changes?
  1469. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  1470. for (i = 0; i < 6; i++)
  1471. h->dequant4_coeff[i] = h->dequant4_buffer[0] +
  1472. (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  1473. for (i = 0; i < 6; i++)
  1474. h->dequant8_coeff[i] = h->dequant8_buffer[0] +
  1475. (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  1476. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  1477. // POC timing
  1478. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  1479. // reference lists
  1480. copy_fields(h, h1, short_ref, cabac_init_idc);
  1481. copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
  1482. copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
  1483. copy_picture_range(h->delayed_pic, h1->delayed_pic,
  1484. MAX_DELAYED_PIC_COUNT + 2, h, h1);
  1485. h->last_slice_type = h1->last_slice_type;
  1486. if (context_reinitialized)
  1487. h264_set_parameter_from_sps(h);
  1488. if (!h->cur_pic_ptr)
  1489. return 0;
  1490. if (!h->droppable) {
  1491. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1492. h->prev_poc_msb = h->poc_msb;
  1493. h->prev_poc_lsb = h->poc_lsb;
  1494. }
  1495. h->prev_frame_num_offset = h->frame_num_offset;
  1496. h->prev_frame_num = h->frame_num;
  1497. h->outputed_poc = h->next_outputed_poc;
  1498. return err;
  1499. }
  1500. static int h264_frame_start(H264Context *h)
  1501. {
  1502. Picture *pic;
  1503. int i, ret;
  1504. const int pixel_shift = h->pixel_shift;
  1505. release_unused_pictures(h, 1);
  1506. h->cur_pic_ptr = NULL;
  1507. i = find_unused_picture(h);
  1508. if (i < 0) {
  1509. av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1510. return i;
  1511. }
  1512. pic = &h->DPB[i];
  1513. pic->reference = h->droppable ? 0 : h->picture_structure;
  1514. pic->f.coded_picture_number = h->coded_picture_number++;
  1515. pic->field_picture = h->picture_structure != PICT_FRAME;
  1516. /*
  1517. * Zero key_frame here; IDR markings per slice in frame or fields are ORed
  1518. * in later.
  1519. * See decode_nal_units().
  1520. */
  1521. pic->f.key_frame = 0;
  1522. pic->mmco_reset = 0;
  1523. if ((ret = alloc_picture(h, pic)) < 0)
  1524. return ret;
  1525. h->cur_pic_ptr = pic;
  1526. unref_picture(h, &h->cur_pic);
  1527. if ((ret = ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
  1528. return ret;
  1529. if (CONFIG_ERROR_RESILIENCE)
  1530. ff_er_frame_start(&h->er);
  1531. assert(h->linesize && h->uvlinesize);
  1532. for (i = 0; i < 16; i++) {
  1533. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  1534. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  1535. }
  1536. for (i = 0; i < 16; i++) {
  1537. h->block_offset[16 + i] =
  1538. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1539. h->block_offset[48 + 16 + i] =
  1540. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1541. }
  1542. /* can't be in alloc_tables because linesize isn't known there.
  1543. * FIXME: redo bipred weight to not require extra buffer? */
  1544. for (i = 0; i < h->slice_context_count; i++)
  1545. if (h->thread_context[i]) {
  1546. ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);
  1547. if (ret < 0)
  1548. return ret;
  1549. }
  1550. /* Some macroblocks can be accessed before they're available in case
  1551. * of lost slices, MBAFF or threading. */
  1552. memset(h->slice_table, -1,
  1553. (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
  1554. // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding ||
  1555. // s->current_picture.f.reference /* || h->contains_intra */ || 1;
  1556. /* We mark the current picture as non-reference after allocating it, so
  1557. * that if we break out due to an error it can be released automatically
  1558. * in the next ff_MPV_frame_start().
  1559. */
  1560. h->cur_pic_ptr->reference = 0;
  1561. h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
  1562. h->next_output_pic = NULL;
  1563. assert(h->cur_pic_ptr->long_ref == 0);
  1564. return 0;
  1565. }
  1566. /**
  1567. * Run setup operations that must be run after slice header decoding.
  1568. * This includes finding the next displayed frame.
  1569. *
  1570. * @param h h264 master context
  1571. * @param setup_finished enough NALs have been read that we can call
  1572. * ff_thread_finish_setup()
  1573. */
  1574. static void decode_postinit(H264Context *h, int setup_finished)
  1575. {
  1576. Picture *out = h->cur_pic_ptr;
  1577. Picture *cur = h->cur_pic_ptr;
  1578. int i, pics, out_of_order, out_idx;
  1579. int invalid = 0, cnt = 0;
  1580. h->cur_pic_ptr->f.pict_type = h->pict_type;
  1581. if (h->next_output_pic)
  1582. return;
  1583. if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
  1584. /* FIXME: if we have two PAFF fields in one packet, we can't start
  1585. * the next thread here. If we have one field per packet, we can.
  1586. * The check in decode_nal_units() is not good enough to find this
  1587. * yet, so we assume the worst for now. */
  1588. // if (setup_finished)
  1589. // ff_thread_finish_setup(h->avctx);
  1590. return;
  1591. }
  1592. cur->f.interlaced_frame = 0;
  1593. cur->f.repeat_pict = 0;
  1594. /* Signal interlacing information externally. */
  1595. /* Prioritize picture timing SEI information over used
  1596. * decoding process if it exists. */
  1597. if (h->sps.pic_struct_present_flag) {
  1598. switch (h->sei_pic_struct) {
  1599. case SEI_PIC_STRUCT_FRAME:
  1600. break;
  1601. case SEI_PIC_STRUCT_TOP_FIELD:
  1602. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  1603. cur->f.interlaced_frame = 1;
  1604. break;
  1605. case SEI_PIC_STRUCT_TOP_BOTTOM:
  1606. case SEI_PIC_STRUCT_BOTTOM_TOP:
  1607. if (FIELD_OR_MBAFF_PICTURE(h))
  1608. cur->f.interlaced_frame = 1;
  1609. else
  1610. // try to flag soft telecine progressive
  1611. cur->f.interlaced_frame = h->prev_interlaced_frame;
  1612. break;
  1613. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  1614. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  1615. /* Signal the possibility of telecined film externally
  1616. * (pic_struct 5,6). From these hints, let the applications
  1617. * decide if they apply deinterlacing. */
  1618. cur->f.repeat_pict = 1;
  1619. break;
  1620. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  1621. cur->f.repeat_pict = 2;
  1622. break;
  1623. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  1624. cur->f.repeat_pict = 4;
  1625. break;
  1626. }
  1627. if ((h->sei_ct_type & 3) &&
  1628. h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  1629. cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
  1630. } else {
  1631. /* Derive interlacing flag from used decoding process. */
  1632. cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
  1633. }
  1634. h->prev_interlaced_frame = cur->f.interlaced_frame;
  1635. if (cur->field_poc[0] != cur->field_poc[1]) {
  1636. /* Derive top_field_first from field pocs. */
  1637. cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
  1638. } else {
  1639. if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
  1640. /* Use picture timing SEI information. Even if it is a
  1641. * information of a past frame, better than nothing. */
  1642. if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
  1643. h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  1644. cur->f.top_field_first = 1;
  1645. else
  1646. cur->f.top_field_first = 0;
  1647. } else {
  1648. /* Most likely progressive */
  1649. cur->f.top_field_first = 0;
  1650. }
  1651. }
  1652. // FIXME do something with unavailable reference frames
  1653. /* Sort B-frames into display order */
  1654. if (h->sps.bitstream_restriction_flag &&
  1655. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  1656. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  1657. h->low_delay = 0;
  1658. }
  1659. if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
  1660. !h->sps.bitstream_restriction_flag) {
  1661. h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
  1662. h->low_delay = 0;
  1663. }
  1664. pics = 0;
  1665. while (h->delayed_pic[pics])
  1666. pics++;
  1667. assert(pics <= MAX_DELAYED_PIC_COUNT);
  1668. h->delayed_pic[pics++] = cur;
  1669. if (cur->reference == 0)
  1670. cur->reference = DELAYED_PIC_REF;
  1671. /* Frame reordering. This code takes pictures from coding order and sorts
  1672. * them by their incremental POC value into display order. It supports POC
  1673. * gaps, MMCO reset codes and random resets.
  1674. * A "display group" can start either with a IDR frame (f.key_frame = 1),
  1675. * and/or can be closed down with a MMCO reset code. In sequences where
  1676. * there is no delay, we can't detect that (since the frame was already
  1677. * output to the user), so we also set h->mmco_reset to detect the MMCO
  1678. * reset code.
  1679. * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
  1680. * we increase the delay between input and output. All frames affected by
  1681. * the lag (e.g. those that should have been output before another frame
  1682. * that we already returned to the user) will be dropped. This is a bug
  1683. * that we will fix later. */
  1684. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
  1685. cnt += out->poc < h->last_pocs[i];
  1686. invalid += out->poc == INT_MIN;
  1687. }
  1688. if (!h->mmco_reset && !cur->f.key_frame &&
  1689. cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
  1690. h->mmco_reset = 2;
  1691. if (pics > 1)
  1692. h->delayed_pic[pics - 2]->mmco_reset = 2;
  1693. }
  1694. if (h->mmco_reset || cur->f.key_frame) {
  1695. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  1696. h->last_pocs[i] = INT_MIN;
  1697. cnt = 0;
  1698. invalid = MAX_DELAYED_PIC_COUNT;
  1699. }
  1700. out = h->delayed_pic[0];
  1701. out_idx = 0;
  1702. for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
  1703. h->delayed_pic[i] &&
  1704. !h->delayed_pic[i - 1]->mmco_reset &&
  1705. !h->delayed_pic[i]->f.key_frame;
  1706. i++)
  1707. if (h->delayed_pic[i]->poc < out->poc) {
  1708. out = h->delayed_pic[i];
  1709. out_idx = i;
  1710. }
  1711. if (h->avctx->has_b_frames == 0 &&
  1712. (h->delayed_pic[0]->f.key_frame || h->mmco_reset))
  1713. h->next_outputed_poc = INT_MIN;
  1714. out_of_order = !out->f.key_frame && !h->mmco_reset &&
  1715. (out->poc < h->next_outputed_poc);
  1716. if (h->sps.bitstream_restriction_flag &&
  1717. h->avctx->has_b_frames >= h->sps.num_reorder_frames) {
  1718. } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
  1719. h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
  1720. if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
  1721. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
  1722. }
  1723. h->low_delay = 0;
  1724. } else if (h->low_delay &&
  1725. ((h->next_outputed_poc != INT_MIN &&
  1726. out->poc > h->next_outputed_poc + 2) ||
  1727. cur->f.pict_type == AV_PICTURE_TYPE_B)) {
  1728. h->low_delay = 0;
  1729. h->avctx->has_b_frames++;
  1730. }
  1731. if (pics > h->avctx->has_b_frames) {
  1732. out->reference &= ~DELAYED_PIC_REF;
  1733. // for frame threading, the owner must be the second field's thread or
  1734. // else the first thread can release the picture and reuse it unsafely
  1735. for (i = out_idx; h->delayed_pic[i]; i++)
  1736. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1737. }
  1738. memmove(h->last_pocs, &h->last_pocs[1],
  1739. sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
  1740. h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
  1741. if (!out_of_order && pics > h->avctx->has_b_frames) {
  1742. h->next_output_pic = out;
  1743. if (out->mmco_reset) {
  1744. if (out_idx > 0) {
  1745. h->next_outputed_poc = out->poc;
  1746. h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
  1747. } else {
  1748. h->next_outputed_poc = INT_MIN;
  1749. }
  1750. } else {
  1751. if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f.key_frame) {
  1752. h->next_outputed_poc = INT_MIN;
  1753. } else {
  1754. h->next_outputed_poc = out->poc;
  1755. }
  1756. }
  1757. h->mmco_reset = 0;
  1758. } else {
  1759. av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
  1760. }
  1761. if (setup_finished && !h->avctx->hwaccel)
  1762. ff_thread_finish_setup(h->avctx);
  1763. }
  1764. static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
  1765. uint8_t *src_cb, uint8_t *src_cr,
  1766. int linesize, int uvlinesize,
  1767. int simple)
  1768. {
  1769. uint8_t *top_border;
  1770. int top_idx = 1;
  1771. const int pixel_shift = h->pixel_shift;
  1772. int chroma444 = CHROMA444(h);
  1773. int chroma422 = CHROMA422(h);
  1774. src_y -= linesize;
  1775. src_cb -= uvlinesize;
  1776. src_cr -= uvlinesize;
  1777. if (!simple && FRAME_MBAFF(h)) {
  1778. if (h->mb_y & 1) {
  1779. if (!MB_MBAFF(h)) {
  1780. top_border = h->top_borders[0][h->mb_x];
  1781. AV_COPY128(top_border, src_y + 15 * linesize);
  1782. if (pixel_shift)
  1783. AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
  1784. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  1785. if (chroma444) {
  1786. if (pixel_shift) {
  1787. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1788. AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
  1789. AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
  1790. AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
  1791. } else {
  1792. AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
  1793. AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
  1794. }
  1795. } else if (chroma422) {
  1796. if (pixel_shift) {
  1797. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1798. AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
  1799. } else {
  1800. AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
  1801. AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
  1802. }
  1803. } else {
  1804. if (pixel_shift) {
  1805. AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
  1806. AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
  1807. } else {
  1808. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  1809. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  1810. }
  1811. }
  1812. }
  1813. }
  1814. } else if (MB_MBAFF(h)) {
  1815. top_idx = 0;
  1816. } else
  1817. return;
  1818. }
  1819. top_border = h->top_borders[top_idx][h->mb_x];
  1820. /* There are two lines saved, the line above the top macroblock
  1821. * of a pair, and the line above the bottom macroblock. */
  1822. AV_COPY128(top_border, src_y + 16 * linesize);
  1823. if (pixel_shift)
  1824. AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
  1825. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  1826. if (chroma444) {
  1827. if (pixel_shift) {
  1828. AV_COPY128(top_border + 32, src_cb + 16 * linesize);
  1829. AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
  1830. AV_COPY128(top_border + 64, src_cr + 16 * linesize);
  1831. AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
  1832. } else {
  1833. AV_COPY128(top_border + 16, src_cb + 16 * linesize);
  1834. AV_COPY128(top_border + 32, src_cr + 16 * linesize);
  1835. }
  1836. } else if (chroma422) {
  1837. if (pixel_shift) {
  1838. AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
  1839. AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
  1840. } else {
  1841. AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
  1842. AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
  1843. }
  1844. } else {
  1845. if (pixel_shift) {
  1846. AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
  1847. AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
  1848. } else {
  1849. AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
  1850. AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
  1851. }
  1852. }
  1853. }
  1854. }
  1855. static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  1856. uint8_t *src_cb, uint8_t *src_cr,
  1857. int linesize, int uvlinesize,
  1858. int xchg, int chroma444,
  1859. int simple, int pixel_shift)
  1860. {
  1861. int deblock_topleft;
  1862. int deblock_top;
  1863. int top_idx = 1;
  1864. uint8_t *top_border_m1;
  1865. uint8_t *top_border;
  1866. if (!simple && FRAME_MBAFF(h)) {
  1867. if (h->mb_y & 1) {
  1868. if (!MB_MBAFF(h))
  1869. return;
  1870. } else {
  1871. top_idx = MB_MBAFF(h) ? 0 : 1;
  1872. }
  1873. }
  1874. if (h->deblocking_filter == 2) {
  1875. deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
  1876. deblock_top = h->top_type;
  1877. } else {
  1878. deblock_topleft = (h->mb_x > 0);
  1879. deblock_top = (h->mb_y > !!MB_FIELD(h));
  1880. }
  1881. src_y -= linesize + 1 + pixel_shift;
  1882. src_cb -= uvlinesize + 1 + pixel_shift;
  1883. src_cr -= uvlinesize + 1 + pixel_shift;
  1884. top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
  1885. top_border = h->top_borders[top_idx][h->mb_x];
  1886. #define XCHG(a, b, xchg) \
  1887. if (pixel_shift) { \
  1888. if (xchg) { \
  1889. AV_SWAP64(b + 0, a + 0); \
  1890. AV_SWAP64(b + 8, a + 8); \
  1891. } else { \
  1892. AV_COPY128(b, a); \
  1893. } \
  1894. } else if (xchg) \
  1895. AV_SWAP64(b, a); \
  1896. else \
  1897. AV_COPY64(b, a);
  1898. if (deblock_top) {
  1899. if (deblock_topleft) {
  1900. XCHG(top_border_m1 + (8 << pixel_shift),
  1901. src_y - (7 << pixel_shift), 1);
  1902. }
  1903. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  1904. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  1905. if (h->mb_x + 1 < h->mb_width) {
  1906. XCHG(h->top_borders[top_idx][h->mb_x + 1],
  1907. src_y + (17 << pixel_shift), 1);
  1908. }
  1909. }
  1910. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  1911. if (chroma444) {
  1912. if (deblock_topleft) {
  1913. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1914. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1915. }
  1916. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  1917. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  1918. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  1919. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  1920. if (h->mb_x + 1 < h->mb_width) {
  1921. XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  1922. XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  1923. }
  1924. } else {
  1925. if (deblock_top) {
  1926. if (deblock_topleft) {
  1927. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1928. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1929. }
  1930. XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
  1931. XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
  1932. }
  1933. }
  1934. }
  1935. }
  1936. static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
  1937. int index)
  1938. {
  1939. if (high_bit_depth) {
  1940. return AV_RN32A(((int32_t *)mb) + index);
  1941. } else
  1942. return AV_RN16A(mb + index);
  1943. }
  1944. static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
  1945. int index, int value)
  1946. {
  1947. if (high_bit_depth) {
  1948. AV_WN32A(((int32_t *)mb) + index, value);
  1949. } else
  1950. AV_WN16A(mb + index, value);
  1951. }
  1952. static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
  1953. int mb_type, int is_h264,
  1954. int simple,
  1955. int transform_bypass,
  1956. int pixel_shift,
  1957. int *block_offset,
  1958. int linesize,
  1959. uint8_t *dest_y, int p)
  1960. {
  1961. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  1962. void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
  1963. int i;
  1964. int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
  1965. block_offset += 16 * p;
  1966. if (IS_INTRA4x4(mb_type)) {
  1967. if (IS_8x8DCT(mb_type)) {
  1968. if (transform_bypass) {
  1969. idct_dc_add =
  1970. idct_add = h->h264dsp.h264_add_pixels8_clear;
  1971. } else {
  1972. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1973. idct_add = h->h264dsp.h264_idct8_add;
  1974. }
  1975. for (i = 0; i < 16; i += 4) {
  1976. uint8_t *const ptr = dest_y + block_offset[i];
  1977. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  1978. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  1979. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1980. } else {
  1981. const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  1982. h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
  1983. (h->topright_samples_available << i) & 0x4000, linesize);
  1984. if (nnz) {
  1985. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  1986. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1987. else
  1988. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1989. }
  1990. }
  1991. }
  1992. } else {
  1993. if (transform_bypass) {
  1994. idct_dc_add =
  1995. idct_add = h->h264dsp.h264_add_pixels4_clear;
  1996. } else {
  1997. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1998. idct_add = h->h264dsp.h264_idct_add;
  1999. }
  2000. for (i = 0; i < 16; i++) {
  2001. uint8_t *const ptr = dest_y + block_offset[i];
  2002. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  2003. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  2004. h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2005. } else {
  2006. uint8_t *topright;
  2007. int nnz, tr;
  2008. uint64_t tr_high;
  2009. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  2010. const int topright_avail = (h->topright_samples_available << i) & 0x8000;
  2011. assert(h->mb_y || linesize <= block_offset[i]);
  2012. if (!topright_avail) {
  2013. if (pixel_shift) {
  2014. tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
  2015. topright = (uint8_t *)&tr_high;
  2016. } else {
  2017. tr = ptr[3 - linesize] * 0x01010101u;
  2018. topright = (uint8_t *)&tr;
  2019. }
  2020. } else
  2021. topright = ptr + (4 << pixel_shift) - linesize;
  2022. } else
  2023. topright = NULL;
  2024. h->hpc.pred4x4[dir](ptr, topright, linesize);
  2025. nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  2026. if (nnz) {
  2027. if (is_h264) {
  2028. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  2029. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2030. else
  2031. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2032. } else if (CONFIG_SVQ3_DECODER)
  2033. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
  2034. }
  2035. }
  2036. }
  2037. }
  2038. } else {
  2039. h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
  2040. if (is_h264) {
  2041. if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
  2042. if (!transform_bypass)
  2043. h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
  2044. h->mb_luma_dc[p],
  2045. h->dequant4_coeff[p][qscale][0]);
  2046. else {
  2047. static const uint8_t dc_mapping[16] = {
  2048. 0 * 16, 1 * 16, 4 * 16, 5 * 16,
  2049. 2 * 16, 3 * 16, 6 * 16, 7 * 16,
  2050. 8 * 16, 9 * 16, 12 * 16, 13 * 16,
  2051. 10 * 16, 11 * 16, 14 * 16, 15 * 16
  2052. };
  2053. for (i = 0; i < 16; i++)
  2054. dctcoef_set(h->mb + (p * 256 << pixel_shift),
  2055. pixel_shift, dc_mapping[i],
  2056. dctcoef_get(h->mb_luma_dc[p],
  2057. pixel_shift, i));
  2058. }
  2059. }
  2060. } else if (CONFIG_SVQ3_DECODER)
  2061. ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
  2062. h->mb_luma_dc[p], qscale);
  2063. }
  2064. }
  2065. static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
  2066. int is_h264, int simple,
  2067. int transform_bypass,
  2068. int pixel_shift,
  2069. int *block_offset,
  2070. int linesize,
  2071. uint8_t *dest_y, int p)
  2072. {
  2073. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  2074. int i;
  2075. block_offset += 16 * p;
  2076. if (!IS_INTRA4x4(mb_type)) {
  2077. if (is_h264) {
  2078. if (IS_INTRA16x16(mb_type)) {
  2079. if (transform_bypass) {
  2080. if (h->sps.profile_idc == 244 &&
  2081. (h->intra16x16_pred_mode == VERT_PRED8x8 ||
  2082. h->intra16x16_pred_mode == HOR_PRED8x8)) {
  2083. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
  2084. h->mb + (p * 256 << pixel_shift),
  2085. linesize);
  2086. } else {
  2087. for (i = 0; i < 16; i++)
  2088. if (h->non_zero_count_cache[scan8[i + p * 16]] ||
  2089. dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  2090. h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
  2091. h->mb + (i * 16 + p * 256 << pixel_shift),
  2092. linesize);
  2093. }
  2094. } else {
  2095. h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
  2096. h->mb + (p * 256 << pixel_shift),
  2097. linesize,
  2098. h->non_zero_count_cache + p * 5 * 8);
  2099. }
  2100. } else if (h->cbp & 15) {
  2101. if (transform_bypass) {
  2102. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  2103. idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
  2104. : h->h264dsp.h264_add_pixels4_clear;
  2105. for (i = 0; i < 16; i += di)
  2106. if (h->non_zero_count_cache[scan8[i + p * 16]])
  2107. idct_add(dest_y + block_offset[i],
  2108. h->mb + (i * 16 + p * 256 << pixel_shift),
  2109. linesize);
  2110. } else {
  2111. if (IS_8x8DCT(mb_type))
  2112. h->h264dsp.h264_idct8_add4(dest_y, block_offset,
  2113. h->mb + (p * 256 << pixel_shift),
  2114. linesize,
  2115. h->non_zero_count_cache + p * 5 * 8);
  2116. else
  2117. h->h264dsp.h264_idct_add16(dest_y, block_offset,
  2118. h->mb + (p * 256 << pixel_shift),
  2119. linesize,
  2120. h->non_zero_count_cache + p * 5 * 8);
  2121. }
  2122. }
  2123. } else if (CONFIG_SVQ3_DECODER) {
  2124. for (i = 0; i < 16; i++)
  2125. if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
  2126. // FIXME benchmark weird rule, & below
  2127. uint8_t *const ptr = dest_y + block_offset[i];
  2128. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
  2129. h->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2130. }
  2131. }
  2132. }
  2133. }
  2134. #define BITS 8
  2135. #define SIMPLE 1
  2136. #include "h264_mb_template.c"
  2137. #undef BITS
  2138. #define BITS 16
  2139. #include "h264_mb_template.c"
  2140. #undef SIMPLE
  2141. #define SIMPLE 0
  2142. #include "h264_mb_template.c"
  2143. void ff_h264_hl_decode_mb(H264Context *h)
  2144. {
  2145. const int mb_xy = h->mb_xy;
  2146. const int mb_type = h->cur_pic.mb_type[mb_xy];
  2147. int is_complex = CONFIG_SMALL || h->is_complex ||
  2148. IS_INTRA_PCM(mb_type) || h->qscale == 0;
  2149. if (CHROMA444(h)) {
  2150. if (is_complex || h->pixel_shift)
  2151. hl_decode_mb_444_complex(h);
  2152. else
  2153. hl_decode_mb_444_simple_8(h);
  2154. } else if (is_complex) {
  2155. hl_decode_mb_complex(h);
  2156. } else if (h->pixel_shift) {
  2157. hl_decode_mb_simple_16(h);
  2158. } else
  2159. hl_decode_mb_simple_8(h);
  2160. }
  2161. static int pred_weight_table(H264Context *h)
  2162. {
  2163. int list, i;
  2164. int luma_def, chroma_def;
  2165. h->use_weight = 0;
  2166. h->use_weight_chroma = 0;
  2167. h->luma_log2_weight_denom = get_ue_golomb(&h->gb);
  2168. if (h->sps.chroma_format_idc)
  2169. h->chroma_log2_weight_denom = get_ue_golomb(&h->gb);
  2170. luma_def = 1 << h->luma_log2_weight_denom;
  2171. chroma_def = 1 << h->chroma_log2_weight_denom;
  2172. for (list = 0; list < 2; list++) {
  2173. h->luma_weight_flag[list] = 0;
  2174. h->chroma_weight_flag[list] = 0;
  2175. for (i = 0; i < h->ref_count[list]; i++) {
  2176. int luma_weight_flag, chroma_weight_flag;
  2177. luma_weight_flag = get_bits1(&h->gb);
  2178. if (luma_weight_flag) {
  2179. h->luma_weight[i][list][0] = get_se_golomb(&h->gb);
  2180. h->luma_weight[i][list][1] = get_se_golomb(&h->gb);
  2181. if (h->luma_weight[i][list][0] != luma_def ||
  2182. h->luma_weight[i][list][1] != 0) {
  2183. h->use_weight = 1;
  2184. h->luma_weight_flag[list] = 1;
  2185. }
  2186. } else {
  2187. h->luma_weight[i][list][0] = luma_def;
  2188. h->luma_weight[i][list][1] = 0;
  2189. }
  2190. if (h->sps.chroma_format_idc) {
  2191. chroma_weight_flag = get_bits1(&h->gb);
  2192. if (chroma_weight_flag) {
  2193. int j;
  2194. for (j = 0; j < 2; j++) {
  2195. h->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb);
  2196. h->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb);
  2197. if (h->chroma_weight[i][list][j][0] != chroma_def ||
  2198. h->chroma_weight[i][list][j][1] != 0) {
  2199. h->use_weight_chroma = 1;
  2200. h->chroma_weight_flag[list] = 1;
  2201. }
  2202. }
  2203. } else {
  2204. int j;
  2205. for (j = 0; j < 2; j++) {
  2206. h->chroma_weight[i][list][j][0] = chroma_def;
  2207. h->chroma_weight[i][list][j][1] = 0;
  2208. }
  2209. }
  2210. }
  2211. }
  2212. if (h->slice_type_nos != AV_PICTURE_TYPE_B)
  2213. break;
  2214. }
  2215. h->use_weight = h->use_weight || h->use_weight_chroma;
  2216. return 0;
  2217. }
  2218. /**
  2219. * Initialize implicit_weight table.
  2220. * @param field 0/1 initialize the weight for interlaced MBAFF
  2221. * -1 initializes the rest
  2222. */
  2223. static void implicit_weight_table(H264Context *h, int field)
  2224. {
  2225. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  2226. for (i = 0; i < 2; i++) {
  2227. h->luma_weight_flag[i] = 0;
  2228. h->chroma_weight_flag[i] = 0;
  2229. }
  2230. if (field < 0) {
  2231. if (h->picture_structure == PICT_FRAME) {
  2232. cur_poc = h->cur_pic_ptr->poc;
  2233. } else {
  2234. cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
  2235. }
  2236. if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
  2237. h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
  2238. h->use_weight = 0;
  2239. h->use_weight_chroma = 0;
  2240. return;
  2241. }
  2242. ref_start = 0;
  2243. ref_count0 = h->ref_count[0];
  2244. ref_count1 = h->ref_count[1];
  2245. } else {
  2246. cur_poc = h->cur_pic_ptr->field_poc[field];
  2247. ref_start = 16;
  2248. ref_count0 = 16 + 2 * h->ref_count[0];
  2249. ref_count1 = 16 + 2 * h->ref_count[1];
  2250. }
  2251. h->use_weight = 2;
  2252. h->use_weight_chroma = 2;
  2253. h->luma_log2_weight_denom = 5;
  2254. h->chroma_log2_weight_denom = 5;
  2255. for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
  2256. int poc0 = h->ref_list[0][ref0].poc;
  2257. for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
  2258. int w = 32;
  2259. if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
  2260. int poc1 = h->ref_list[1][ref1].poc;
  2261. int td = av_clip(poc1 - poc0, -128, 127);
  2262. if (td) {
  2263. int tb = av_clip(cur_poc - poc0, -128, 127);
  2264. int tx = (16384 + (FFABS(td) >> 1)) / td;
  2265. int dist_scale_factor = (tb * tx + 32) >> 8;
  2266. if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
  2267. w = 64 - dist_scale_factor;
  2268. }
  2269. }
  2270. if (field < 0) {
  2271. h->implicit_weight[ref0][ref1][0] =
  2272. h->implicit_weight[ref0][ref1][1] = w;
  2273. } else {
  2274. h->implicit_weight[ref0][ref1][field] = w;
  2275. }
  2276. }
  2277. }
  2278. }
  2279. /**
  2280. * instantaneous decoder refresh.
  2281. */
  2282. static void idr(H264Context *h)
  2283. {
  2284. ff_h264_remove_all_refs(h);
  2285. h->prev_frame_num = 0;
  2286. h->prev_frame_num_offset = 0;
  2287. h->prev_poc_msb =
  2288. h->prev_poc_lsb = 0;
  2289. }
  2290. /* forget old pics after a seek */
  2291. static void flush_change(H264Context *h)
  2292. {
  2293. int i;
  2294. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  2295. h->last_pocs[i] = INT_MIN;
  2296. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  2297. h->prev_interlaced_frame = 1;
  2298. idr(h);
  2299. if (h->cur_pic_ptr)
  2300. h->cur_pic_ptr->reference = 0;
  2301. h->first_field = 0;
  2302. memset(h->ref_list[0], 0, sizeof(h->ref_list[0]));
  2303. memset(h->ref_list[1], 0, sizeof(h->ref_list[1]));
  2304. memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0]));
  2305. memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1]));
  2306. ff_h264_reset_sei(h);
  2307. }
  2308. /* forget old pics after a seek */
  2309. static void flush_dpb(AVCodecContext *avctx)
  2310. {
  2311. H264Context *h = avctx->priv_data;
  2312. int i;
  2313. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
  2314. if (h->delayed_pic[i])
  2315. h->delayed_pic[i]->reference = 0;
  2316. h->delayed_pic[i] = NULL;
  2317. }
  2318. flush_change(h);
  2319. if (h->DPB)
  2320. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2321. unref_picture(h, &h->DPB[i]);
  2322. h->cur_pic_ptr = NULL;
  2323. unref_picture(h, &h->cur_pic);
  2324. h->mb_x = h->mb_y = 0;
  2325. h->parse_context.state = -1;
  2326. h->parse_context.frame_start_found = 0;
  2327. h->parse_context.overread = 0;
  2328. h->parse_context.overread_index = 0;
  2329. h->parse_context.index = 0;
  2330. h->parse_context.last_index = 0;
  2331. }
  2332. int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
  2333. {
  2334. const int max_frame_num = 1 << h->sps.log2_max_frame_num;
  2335. int field_poc[2];
  2336. h->frame_num_offset = h->prev_frame_num_offset;
  2337. if (h->frame_num < h->prev_frame_num)
  2338. h->frame_num_offset += max_frame_num;
  2339. if (h->sps.poc_type == 0) {
  2340. const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
  2341. if (h->poc_lsb < h->prev_poc_lsb &&
  2342. h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
  2343. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2344. else if (h->poc_lsb > h->prev_poc_lsb &&
  2345. h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
  2346. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2347. else
  2348. h->poc_msb = h->prev_poc_msb;
  2349. field_poc[0] =
  2350. field_poc[1] = h->poc_msb + h->poc_lsb;
  2351. if (h->picture_structure == PICT_FRAME)
  2352. field_poc[1] += h->delta_poc_bottom;
  2353. } else if (h->sps.poc_type == 1) {
  2354. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2355. int i;
  2356. if (h->sps.poc_cycle_length != 0)
  2357. abs_frame_num = h->frame_num_offset + h->frame_num;
  2358. else
  2359. abs_frame_num = 0;
  2360. if (h->nal_ref_idc == 0 && abs_frame_num > 0)
  2361. abs_frame_num--;
  2362. expected_delta_per_poc_cycle = 0;
  2363. for (i = 0; i < h->sps.poc_cycle_length; i++)
  2364. // FIXME integrate during sps parse
  2365. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
  2366. if (abs_frame_num > 0) {
  2367. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2368. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2369. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2370. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  2371. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
  2372. } else
  2373. expectedpoc = 0;
  2374. if (h->nal_ref_idc == 0)
  2375. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2376. field_poc[0] = expectedpoc + h->delta_poc[0];
  2377. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2378. if (h->picture_structure == PICT_FRAME)
  2379. field_poc[1] += h->delta_poc[1];
  2380. } else {
  2381. int poc = 2 * (h->frame_num_offset + h->frame_num);
  2382. if (!h->nal_ref_idc)
  2383. poc--;
  2384. field_poc[0] = poc;
  2385. field_poc[1] = poc;
  2386. }
  2387. if (h->picture_structure != PICT_BOTTOM_FIELD)
  2388. pic_field_poc[0] = field_poc[0];
  2389. if (h->picture_structure != PICT_TOP_FIELD)
  2390. pic_field_poc[1] = field_poc[1];
  2391. if (pic_poc)
  2392. *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
  2393. return 0;
  2394. }
  2395. /**
  2396. * initialize scan tables
  2397. */
  2398. static void init_scan_tables(H264Context *h)
  2399. {
  2400. int i;
  2401. for (i = 0; i < 16; i++) {
  2402. #define T(x) (x >> 2) | ((x << 2) & 0xF)
  2403. h->zigzag_scan[i] = T(zigzag_scan[i]);
  2404. h->field_scan[i] = T(field_scan[i]);
  2405. #undef T
  2406. }
  2407. for (i = 0; i < 64; i++) {
  2408. #define T(x) (x >> 3) | ((x & 7) << 3)
  2409. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  2410. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  2411. h->field_scan8x8[i] = T(field_scan8x8[i]);
  2412. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  2413. #undef T
  2414. }
  2415. if (h->sps.transform_bypass) { // FIXME same ugly
  2416. h->zigzag_scan_q0 = zigzag_scan;
  2417. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  2418. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  2419. h->field_scan_q0 = field_scan;
  2420. h->field_scan8x8_q0 = field_scan8x8;
  2421. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  2422. } else {
  2423. h->zigzag_scan_q0 = h->zigzag_scan;
  2424. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  2425. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  2426. h->field_scan_q0 = h->field_scan;
  2427. h->field_scan8x8_q0 = h->field_scan8x8;
  2428. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  2429. }
  2430. }
  2431. static int field_end(H264Context *h, int in_setup)
  2432. {
  2433. AVCodecContext *const avctx = h->avctx;
  2434. int err = 0;
  2435. h->mb_y = 0;
  2436. if (!in_setup && !h->droppable)
  2437. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  2438. h->picture_structure == PICT_BOTTOM_FIELD);
  2439. if (CONFIG_H264_VDPAU_DECODER &&
  2440. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2441. ff_vdpau_h264_set_reference_frames(h);
  2442. if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  2443. if (!h->droppable) {
  2444. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2445. h->prev_poc_msb = h->poc_msb;
  2446. h->prev_poc_lsb = h->poc_lsb;
  2447. }
  2448. h->prev_frame_num_offset = h->frame_num_offset;
  2449. h->prev_frame_num = h->frame_num;
  2450. h->outputed_poc = h->next_outputed_poc;
  2451. }
  2452. if (avctx->hwaccel) {
  2453. if (avctx->hwaccel->end_frame(avctx) < 0)
  2454. av_log(avctx, AV_LOG_ERROR,
  2455. "hardware accelerator failed to decode picture\n");
  2456. }
  2457. if (CONFIG_H264_VDPAU_DECODER &&
  2458. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2459. ff_vdpau_h264_picture_complete(h);
  2460. /*
  2461. * FIXME: Error handling code does not seem to support interlaced
  2462. * when slices span multiple rows
  2463. * The ff_er_add_slice calls don't work right for bottom
  2464. * fields; they cause massive erroneous error concealing
  2465. * Error marking covers both fields (top and bottom).
  2466. * This causes a mismatched s->error_count
  2467. * and a bad error table. Further, the error count goes to
  2468. * INT_MAX when called for bottom field, because mb_y is
  2469. * past end by one (callers fault) and resync_mb_y != 0
  2470. * causes problems for the first MB line, too.
  2471. */
  2472. if (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE(h)) {
  2473. h->er.cur_pic = h->cur_pic_ptr;
  2474. h->er.last_pic = h->ref_count[0] ? &h->ref_list[0][0] : NULL;
  2475. h->er.next_pic = h->ref_count[1] ? &h->ref_list[1][0] : NULL;
  2476. ff_er_frame_end(&h->er);
  2477. }
  2478. emms_c();
  2479. h->current_slice = 0;
  2480. return err;
  2481. }
  2482. /**
  2483. * Replicate H264 "master" context to thread contexts.
  2484. */
  2485. static int clone_slice(H264Context *dst, H264Context *src)
  2486. {
  2487. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2488. dst->cur_pic_ptr = src->cur_pic_ptr;
  2489. dst->cur_pic = src->cur_pic;
  2490. dst->linesize = src->linesize;
  2491. dst->uvlinesize = src->uvlinesize;
  2492. dst->first_field = src->first_field;
  2493. dst->prev_poc_msb = src->prev_poc_msb;
  2494. dst->prev_poc_lsb = src->prev_poc_lsb;
  2495. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2496. dst->prev_frame_num = src->prev_frame_num;
  2497. dst->short_ref_count = src->short_ref_count;
  2498. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2499. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2500. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2501. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2502. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2503. return 0;
  2504. }
  2505. /**
  2506. * Compute profile from profile_idc and constraint_set?_flags.
  2507. *
  2508. * @param sps SPS
  2509. *
  2510. * @return profile as defined by FF_PROFILE_H264_*
  2511. */
  2512. int ff_h264_get_profile(SPS *sps)
  2513. {
  2514. int profile = sps->profile_idc;
  2515. switch (sps->profile_idc) {
  2516. case FF_PROFILE_H264_BASELINE:
  2517. // constraint_set1_flag set to 1
  2518. profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  2519. break;
  2520. case FF_PROFILE_H264_HIGH_10:
  2521. case FF_PROFILE_H264_HIGH_422:
  2522. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  2523. // constraint_set3_flag set to 1
  2524. profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
  2525. break;
  2526. }
  2527. return profile;
  2528. }
  2529. static int h264_set_parameter_from_sps(H264Context *h)
  2530. {
  2531. if (h->flags & CODEC_FLAG_LOW_DELAY ||
  2532. (h->sps.bitstream_restriction_flag &&
  2533. !h->sps.num_reorder_frames)) {
  2534. if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
  2535. av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
  2536. "Reenabling low delay requires a codec flush.\n");
  2537. else
  2538. h->low_delay = 1;
  2539. }
  2540. if (h->avctx->has_b_frames < 2)
  2541. h->avctx->has_b_frames = !h->low_delay;
  2542. if (h->sps.bit_depth_luma != h->sps.bit_depth_chroma) {
  2543. avpriv_request_sample(h->avctx,
  2544. "Different chroma and luma bit depth");
  2545. return AVERROR_PATCHWELCOME;
  2546. }
  2547. if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
  2548. h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
  2549. if (h->avctx->codec &&
  2550. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU &&
  2551. (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
  2552. av_log(h->avctx, AV_LOG_ERROR,
  2553. "VDPAU decoding does not support video colorspace.\n");
  2554. return AVERROR_INVALIDDATA;
  2555. }
  2556. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  2557. h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2558. h->cur_chroma_format_idc = h->sps.chroma_format_idc;
  2559. h->pixel_shift = h->sps.bit_depth_luma > 8;
  2560. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma,
  2561. h->sps.chroma_format_idc);
  2562. ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
  2563. ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
  2564. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
  2565. h->sps.chroma_format_idc);
  2566. if (CONFIG_ERROR_RESILIENCE)
  2567. ff_dsputil_init(&h->dsp, h->avctx);
  2568. ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
  2569. } else {
  2570. av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
  2571. h->sps.bit_depth_luma);
  2572. return AVERROR_INVALIDDATA;
  2573. }
  2574. }
  2575. return 0;
  2576. }
  2577. static enum AVPixelFormat get_pixel_format(H264Context *h)
  2578. {
  2579. switch (h->sps.bit_depth_luma) {
  2580. case 9:
  2581. if (CHROMA444(h)) {
  2582. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2583. return AV_PIX_FMT_GBRP9;
  2584. } else
  2585. return AV_PIX_FMT_YUV444P9;
  2586. } else if (CHROMA422(h))
  2587. return AV_PIX_FMT_YUV422P9;
  2588. else
  2589. return AV_PIX_FMT_YUV420P9;
  2590. break;
  2591. case 10:
  2592. if (CHROMA444(h)) {
  2593. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2594. return AV_PIX_FMT_GBRP10;
  2595. } else
  2596. return AV_PIX_FMT_YUV444P10;
  2597. } else if (CHROMA422(h))
  2598. return AV_PIX_FMT_YUV422P10;
  2599. else
  2600. return AV_PIX_FMT_YUV420P10;
  2601. break;
  2602. case 8:
  2603. if (CHROMA444(h)) {
  2604. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2605. return AV_PIX_FMT_GBRP;
  2606. } else
  2607. return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ444P
  2608. : AV_PIX_FMT_YUV444P;
  2609. } else if (CHROMA422(h)) {
  2610. return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ422P
  2611. : AV_PIX_FMT_YUV422P;
  2612. } else {
  2613. return h->avctx->get_format(h->avctx, h->avctx->codec->pix_fmts ?
  2614. h->avctx->codec->pix_fmts :
  2615. h->avctx->color_range == AVCOL_RANGE_JPEG ?
  2616. h264_hwaccel_pixfmt_list_jpeg_420 :
  2617. h264_hwaccel_pixfmt_list_420);
  2618. }
  2619. break;
  2620. default:
  2621. av_log(h->avctx, AV_LOG_ERROR,
  2622. "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2623. return AVERROR_INVALIDDATA;
  2624. }
  2625. }
  2626. /* export coded and cropped frame dimensions to AVCodecContext */
  2627. static int init_dimensions(H264Context *h)
  2628. {
  2629. int width = h->width - (h->sps.crop_right + h->sps.crop_left);
  2630. int height = h->height - (h->sps.crop_top + h->sps.crop_bottom);
  2631. /* handle container cropping */
  2632. if (!h->sps.crop &&
  2633. FFALIGN(h->avctx->width, 16) == h->width &&
  2634. FFALIGN(h->avctx->height, 16) == h->height) {
  2635. width = h->avctx->width;
  2636. height = h->avctx->height;
  2637. }
  2638. if (width <= 0 || height <= 0) {
  2639. av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
  2640. width, height);
  2641. if (h->avctx->err_recognition & AV_EF_EXPLODE)
  2642. return AVERROR_INVALIDDATA;
  2643. av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
  2644. h->sps.crop_bottom = h->sps.crop_top = h->sps.crop_right = h->sps.crop_left = 0;
  2645. h->sps.crop = 0;
  2646. width = h->width;
  2647. height = h->height;
  2648. }
  2649. h->avctx->coded_width = h->width;
  2650. h->avctx->coded_height = h->height;
  2651. h->avctx->width = width;
  2652. h->avctx->height = height;
  2653. return 0;
  2654. }
  2655. static int h264_slice_header_init(H264Context *h, int reinit)
  2656. {
  2657. int nb_slices = (HAVE_THREADS &&
  2658. h->avctx->active_thread_type & FF_THREAD_SLICE) ?
  2659. h->avctx->thread_count : 1;
  2660. int i;
  2661. h->avctx->sample_aspect_ratio = h->sps.sar;
  2662. av_assert0(h->avctx->sample_aspect_ratio.den);
  2663. av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
  2664. &h->chroma_x_shift, &h->chroma_y_shift);
  2665. if (h->sps.timing_info_present_flag) {
  2666. int64_t den = h->sps.time_scale;
  2667. if (h->x264_build < 44U)
  2668. den *= 2;
  2669. av_reduce(&h->avctx->time_base.num, &h->avctx->time_base.den,
  2670. h->sps.num_units_in_tick, den, 1 << 30);
  2671. }
  2672. h->avctx->hwaccel = ff_find_hwaccel(h->avctx->codec->id, h->avctx->pix_fmt);
  2673. if (reinit)
  2674. free_tables(h, 0);
  2675. h->first_field = 0;
  2676. h->prev_interlaced_frame = 1;
  2677. init_scan_tables(h);
  2678. if (ff_h264_alloc_tables(h) < 0) {
  2679. av_log(h->avctx, AV_LOG_ERROR,
  2680. "Could not allocate memory for h264\n");
  2681. return AVERROR(ENOMEM);
  2682. }
  2683. if (nb_slices > MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) {
  2684. int max_slices;
  2685. if (h->mb_height)
  2686. max_slices = FFMIN(MAX_THREADS, h->mb_height);
  2687. else
  2688. max_slices = MAX_THREADS;
  2689. av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  2690. " reducing to %d\n", nb_slices, max_slices);
  2691. nb_slices = max_slices;
  2692. }
  2693. h->slice_context_count = nb_slices;
  2694. if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
  2695. if (context_init(h) < 0) {
  2696. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2697. return -1;
  2698. }
  2699. } else {
  2700. for (i = 1; i < h->slice_context_count; i++) {
  2701. H264Context *c;
  2702. c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
  2703. c->avctx = h->avctx;
  2704. c->dsp = h->dsp;
  2705. c->vdsp = h->vdsp;
  2706. c->h264dsp = h->h264dsp;
  2707. c->h264qpel = h->h264qpel;
  2708. c->h264chroma = h->h264chroma;
  2709. c->sps = h->sps;
  2710. c->pps = h->pps;
  2711. c->pixel_shift = h->pixel_shift;
  2712. c->width = h->width;
  2713. c->height = h->height;
  2714. c->linesize = h->linesize;
  2715. c->uvlinesize = h->uvlinesize;
  2716. c->chroma_x_shift = h->chroma_x_shift;
  2717. c->chroma_y_shift = h->chroma_y_shift;
  2718. c->qscale = h->qscale;
  2719. c->droppable = h->droppable;
  2720. c->data_partitioning = h->data_partitioning;
  2721. c->low_delay = h->low_delay;
  2722. c->mb_width = h->mb_width;
  2723. c->mb_height = h->mb_height;
  2724. c->mb_stride = h->mb_stride;
  2725. c->mb_num = h->mb_num;
  2726. c->flags = h->flags;
  2727. c->workaround_bugs = h->workaround_bugs;
  2728. c->pict_type = h->pict_type;
  2729. init_scan_tables(c);
  2730. clone_tables(c, h, i);
  2731. c->context_initialized = 1;
  2732. }
  2733. for (i = 0; i < h->slice_context_count; i++)
  2734. if (context_init(h->thread_context[i]) < 0) {
  2735. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2736. return -1;
  2737. }
  2738. }
  2739. h->context_initialized = 1;
  2740. return 0;
  2741. }
  2742. /**
  2743. * Decode a slice header.
  2744. * This will also call ff_MPV_common_init() and frame_start() as needed.
  2745. *
  2746. * @param h h264context
  2747. * @param h0 h264 master context (differs from 'h' when doing sliced based
  2748. * parallel decoding)
  2749. *
  2750. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  2751. */
  2752. static int decode_slice_header(H264Context *h, H264Context *h0)
  2753. {
  2754. unsigned int first_mb_in_slice;
  2755. unsigned int pps_id;
  2756. int num_ref_idx_active_override_flag, max_refs, ret;
  2757. unsigned int slice_type, tmp, i, j;
  2758. int default_ref_list_done = 0;
  2759. int last_pic_structure, last_pic_droppable;
  2760. int needs_reinit = 0;
  2761. h->me.qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
  2762. h->me.qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
  2763. first_mb_in_slice = get_ue_golomb(&h->gb);
  2764. if (first_mb_in_slice == 0) { // FIXME better field boundary detection
  2765. if (h0->current_slice && FIELD_PICTURE(h)) {
  2766. field_end(h, 1);
  2767. }
  2768. h0->current_slice = 0;
  2769. if (!h0->first_field) {
  2770. if (h->cur_pic_ptr && !h->droppable) {
  2771. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  2772. h->picture_structure == PICT_BOTTOM_FIELD);
  2773. }
  2774. h->cur_pic_ptr = NULL;
  2775. }
  2776. }
  2777. slice_type = get_ue_golomb_31(&h->gb);
  2778. if (slice_type > 9) {
  2779. av_log(h->avctx, AV_LOG_ERROR,
  2780. "slice type too large (%d) at %d %d\n",
  2781. h->slice_type, h->mb_x, h->mb_y);
  2782. return -1;
  2783. }
  2784. if (slice_type > 4) {
  2785. slice_type -= 5;
  2786. h->slice_type_fixed = 1;
  2787. } else
  2788. h->slice_type_fixed = 0;
  2789. slice_type = golomb_to_pict_type[slice_type];
  2790. if (slice_type == AV_PICTURE_TYPE_I ||
  2791. (h0->current_slice != 0 && slice_type == h0->last_slice_type)) {
  2792. default_ref_list_done = 1;
  2793. }
  2794. h->slice_type = slice_type;
  2795. h->slice_type_nos = slice_type & 3;
  2796. // to make a few old functions happy, it's wrong though
  2797. h->pict_type = h->slice_type;
  2798. pps_id = get_ue_golomb(&h->gb);
  2799. if (pps_id >= MAX_PPS_COUNT) {
  2800. av_log(h->avctx, AV_LOG_ERROR, "pps_id out of range\n");
  2801. return -1;
  2802. }
  2803. if (!h0->pps_buffers[pps_id]) {
  2804. av_log(h->avctx, AV_LOG_ERROR,
  2805. "non-existing PPS %u referenced\n",
  2806. pps_id);
  2807. return -1;
  2808. }
  2809. h->pps = *h0->pps_buffers[pps_id];
  2810. if (!h0->sps_buffers[h->pps.sps_id]) {
  2811. av_log(h->avctx, AV_LOG_ERROR,
  2812. "non-existing SPS %u referenced\n",
  2813. h->pps.sps_id);
  2814. return -1;
  2815. }
  2816. if (h->pps.sps_id != h->current_sps_id ||
  2817. h0->sps_buffers[h->pps.sps_id]->new) {
  2818. h0->sps_buffers[h->pps.sps_id]->new = 0;
  2819. h->current_sps_id = h->pps.sps_id;
  2820. h->sps = *h0->sps_buffers[h->pps.sps_id];
  2821. if (h->bit_depth_luma != h->sps.bit_depth_luma ||
  2822. h->chroma_format_idc != h->sps.chroma_format_idc) {
  2823. h->bit_depth_luma = h->sps.bit_depth_luma;
  2824. h->chroma_format_idc = h->sps.chroma_format_idc;
  2825. needs_reinit = 1;
  2826. }
  2827. if ((ret = h264_set_parameter_from_sps(h)) < 0)
  2828. return ret;
  2829. }
  2830. h->avctx->profile = ff_h264_get_profile(&h->sps);
  2831. h->avctx->level = h->sps.level_idc;
  2832. h->avctx->refs = h->sps.ref_frame_count;
  2833. if (h->mb_width != h->sps.mb_width ||
  2834. h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag))
  2835. needs_reinit = 1;
  2836. h->mb_width = h->sps.mb_width;
  2837. h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  2838. h->mb_num = h->mb_width * h->mb_height;
  2839. h->mb_stride = h->mb_width + 1;
  2840. h->b_stride = h->mb_width * 4;
  2841. h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
  2842. h->width = 16 * h->mb_width;
  2843. h->height = 16 * h->mb_height;
  2844. ret = init_dimensions(h);
  2845. if (ret < 0)
  2846. return ret;
  2847. if (h->sps.video_signal_type_present_flag) {
  2848. h->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG
  2849. : AVCOL_RANGE_MPEG;
  2850. if (h->sps.colour_description_present_flag) {
  2851. if (h->avctx->colorspace != h->sps.colorspace)
  2852. needs_reinit = 1;
  2853. h->avctx->color_primaries = h->sps.color_primaries;
  2854. h->avctx->color_trc = h->sps.color_trc;
  2855. h->avctx->colorspace = h->sps.colorspace;
  2856. }
  2857. }
  2858. if (h->context_initialized &&
  2859. (h->width != h->avctx->coded_width ||
  2860. h->height != h->avctx->coded_height ||
  2861. needs_reinit)) {
  2862. if (h != h0) {
  2863. av_log(h->avctx, AV_LOG_ERROR, "changing width/height on "
  2864. "slice %d\n", h0->current_slice + 1);
  2865. return AVERROR_INVALIDDATA;
  2866. }
  2867. flush_change(h);
  2868. if ((ret = get_pixel_format(h)) < 0)
  2869. return ret;
  2870. h->avctx->pix_fmt = ret;
  2871. av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
  2872. "pix_fmt: %d\n", h->width, h->height, h->avctx->pix_fmt);
  2873. if ((ret = h264_slice_header_init(h, 1)) < 0) {
  2874. av_log(h->avctx, AV_LOG_ERROR,
  2875. "h264_slice_header_init() failed\n");
  2876. return ret;
  2877. }
  2878. }
  2879. if (!h->context_initialized) {
  2880. if (h != h0) {
  2881. av_log(h->avctx, AV_LOG_ERROR,
  2882. "Cannot (re-)initialize context during parallel decoding.\n");
  2883. return -1;
  2884. }
  2885. if ((ret = get_pixel_format(h)) < 0)
  2886. return ret;
  2887. h->avctx->pix_fmt = ret;
  2888. if ((ret = h264_slice_header_init(h, 0)) < 0) {
  2889. av_log(h->avctx, AV_LOG_ERROR,
  2890. "h264_slice_header_init() failed\n");
  2891. return ret;
  2892. }
  2893. }
  2894. if (h == h0 && h->dequant_coeff_pps != pps_id) {
  2895. h->dequant_coeff_pps = pps_id;
  2896. init_dequant_tables(h);
  2897. }
  2898. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  2899. h->mb_mbaff = 0;
  2900. h->mb_aff_frame = 0;
  2901. last_pic_structure = h0->picture_structure;
  2902. last_pic_droppable = h0->droppable;
  2903. h->droppable = h->nal_ref_idc == 0;
  2904. if (h->sps.frame_mbs_only_flag) {
  2905. h->picture_structure = PICT_FRAME;
  2906. } else {
  2907. if (get_bits1(&h->gb)) { // field_pic_flag
  2908. h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
  2909. } else {
  2910. h->picture_structure = PICT_FRAME;
  2911. h->mb_aff_frame = h->sps.mb_aff;
  2912. }
  2913. }
  2914. h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
  2915. if (h0->current_slice != 0) {
  2916. if (last_pic_structure != h->picture_structure ||
  2917. last_pic_droppable != h->droppable) {
  2918. av_log(h->avctx, AV_LOG_ERROR,
  2919. "Changing field mode (%d -> %d) between slices is not allowed\n",
  2920. last_pic_structure, h->picture_structure);
  2921. h->picture_structure = last_pic_structure;
  2922. h->droppable = last_pic_droppable;
  2923. return AVERROR_INVALIDDATA;
  2924. } else if (!h0->cur_pic_ptr) {
  2925. av_log(h->avctx, AV_LOG_ERROR,
  2926. "unset cur_pic_ptr on %d. slice\n",
  2927. h0->current_slice + 1);
  2928. return AVERROR_INVALIDDATA;
  2929. }
  2930. } else {
  2931. /* Shorten frame num gaps so we don't have to allocate reference
  2932. * frames just to throw them away */
  2933. if (h->frame_num != h->prev_frame_num) {
  2934. int unwrap_prev_frame_num = h->prev_frame_num;
  2935. int max_frame_num = 1 << h->sps.log2_max_frame_num;
  2936. if (unwrap_prev_frame_num > h->frame_num)
  2937. unwrap_prev_frame_num -= max_frame_num;
  2938. if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
  2939. unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
  2940. if (unwrap_prev_frame_num < 0)
  2941. unwrap_prev_frame_num += max_frame_num;
  2942. h->prev_frame_num = unwrap_prev_frame_num;
  2943. }
  2944. }
  2945. /* See if we have a decoded first field looking for a pair...
  2946. * Here, we're using that to see if we should mark previously
  2947. * decode frames as "finished".
  2948. * We have to do that before the "dummy" in-between frame allocation,
  2949. * since that can modify s->current_picture_ptr. */
  2950. if (h0->first_field) {
  2951. assert(h0->cur_pic_ptr);
  2952. assert(h0->cur_pic_ptr->f.data[0]);
  2953. assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
  2954. /* figure out if we have a complementary field pair */
  2955. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  2956. /* Previous field is unmatched. Don't display it, but let it
  2957. * remain for reference if marked as such. */
  2958. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  2959. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  2960. last_pic_structure == PICT_TOP_FIELD);
  2961. }
  2962. } else {
  2963. if (h0->cur_pic_ptr->frame_num != h->frame_num) {
  2964. /* This and previous field were reference, but had
  2965. * different frame_nums. Consider this field first in
  2966. * pair. Throw away previous field except for reference
  2967. * purposes. */
  2968. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  2969. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  2970. last_pic_structure == PICT_TOP_FIELD);
  2971. }
  2972. } else {
  2973. /* Second field in complementary pair */
  2974. if (!((last_pic_structure == PICT_TOP_FIELD &&
  2975. h->picture_structure == PICT_BOTTOM_FIELD) ||
  2976. (last_pic_structure == PICT_BOTTOM_FIELD &&
  2977. h->picture_structure == PICT_TOP_FIELD))) {
  2978. av_log(h->avctx, AV_LOG_ERROR,
  2979. "Invalid field mode combination %d/%d\n",
  2980. last_pic_structure, h->picture_structure);
  2981. h->picture_structure = last_pic_structure;
  2982. h->droppable = last_pic_droppable;
  2983. return AVERROR_INVALIDDATA;
  2984. } else if (last_pic_droppable != h->droppable) {
  2985. avpriv_request_sample(h->avctx,
  2986. "Found reference and non-reference fields in the same frame, which");
  2987. h->picture_structure = last_pic_structure;
  2988. h->droppable = last_pic_droppable;
  2989. return AVERROR_PATCHWELCOME;
  2990. }
  2991. }
  2992. }
  2993. }
  2994. while (h->frame_num != h->prev_frame_num &&
  2995. h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
  2996. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  2997. av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  2998. h->frame_num, h->prev_frame_num);
  2999. if (h264_frame_start(h) < 0)
  3000. return -1;
  3001. h->prev_frame_num++;
  3002. h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
  3003. h->cur_pic_ptr->frame_num = h->prev_frame_num;
  3004. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  3005. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  3006. if ((ret = ff_generate_sliding_window_mmcos(h, 1)) < 0 &&
  3007. h->avctx->err_recognition & AV_EF_EXPLODE)
  3008. return ret;
  3009. if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
  3010. (h->avctx->err_recognition & AV_EF_EXPLODE))
  3011. return AVERROR_INVALIDDATA;
  3012. /* Error concealment: If a ref is missing, copy the previous ref
  3013. * in its place.
  3014. * FIXME: Avoiding a memcpy would be nice, but ref handling makes
  3015. * many assumptions about there being no actual duplicates.
  3016. * FIXME: This does not copy padding for out-of-frame motion
  3017. * vectors. Given we are concealing a lost frame, this probably
  3018. * is not noticeable by comparison, but it should be fixed. */
  3019. if (h->short_ref_count) {
  3020. if (prev) {
  3021. av_image_copy(h->short_ref[0]->f.data,
  3022. h->short_ref[0]->f.linesize,
  3023. (const uint8_t **)prev->f.data,
  3024. prev->f.linesize,
  3025. h->avctx->pix_fmt,
  3026. h->mb_width * 16,
  3027. h->mb_height * 16);
  3028. h->short_ref[0]->poc = prev->poc + 2;
  3029. }
  3030. h->short_ref[0]->frame_num = h->prev_frame_num;
  3031. }
  3032. }
  3033. /* See if we have a decoded first field looking for a pair...
  3034. * We're using that to see whether to continue decoding in that
  3035. * frame, or to allocate a new one. */
  3036. if (h0->first_field) {
  3037. assert(h0->cur_pic_ptr);
  3038. assert(h0->cur_pic_ptr->f.data[0]);
  3039. assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
  3040. /* figure out if we have a complementary field pair */
  3041. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  3042. /* Previous field is unmatched. Don't display it, but let it
  3043. * remain for reference if marked as such. */
  3044. h0->cur_pic_ptr = NULL;
  3045. h0->first_field = FIELD_PICTURE(h);
  3046. } else {
  3047. if (h0->cur_pic_ptr->frame_num != h->frame_num) {
  3048. /* This and the previous field had different frame_nums.
  3049. * Consider this field first in pair. Throw away previous
  3050. * one except for reference purposes. */
  3051. h0->first_field = 1;
  3052. h0->cur_pic_ptr = NULL;
  3053. } else {
  3054. /* Second field in complementary pair */
  3055. h0->first_field = 0;
  3056. }
  3057. }
  3058. } else {
  3059. /* Frame or first field in a potentially complementary pair */
  3060. h0->first_field = FIELD_PICTURE(h);
  3061. }
  3062. if (!FIELD_PICTURE(h) || h0->first_field) {
  3063. if (h264_frame_start(h) < 0) {
  3064. h0->first_field = 0;
  3065. return -1;
  3066. }
  3067. } else {
  3068. release_unused_pictures(h, 0);
  3069. }
  3070. }
  3071. if (h != h0 && (ret = clone_slice(h, h0)) < 0)
  3072. return ret;
  3073. h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
  3074. assert(h->mb_num == h->mb_width * h->mb_height);
  3075. if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  3076. first_mb_in_slice >= h->mb_num) {
  3077. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  3078. return -1;
  3079. }
  3080. h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
  3081. h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
  3082. FIELD_OR_MBAFF_PICTURE(h);
  3083. if (h->picture_structure == PICT_BOTTOM_FIELD)
  3084. h->resync_mb_y = h->mb_y = h->mb_y + 1;
  3085. assert(h->mb_y < h->mb_height);
  3086. if (h->picture_structure == PICT_FRAME) {
  3087. h->curr_pic_num = h->frame_num;
  3088. h->max_pic_num = 1 << h->sps.log2_max_frame_num;
  3089. } else {
  3090. h->curr_pic_num = 2 * h->frame_num + 1;
  3091. h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
  3092. }
  3093. if (h->nal_unit_type == NAL_IDR_SLICE)
  3094. get_ue_golomb(&h->gb); /* idr_pic_id */
  3095. if (h->sps.poc_type == 0) {
  3096. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  3097. if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
  3098. h->delta_poc_bottom = get_se_golomb(&h->gb);
  3099. }
  3100. if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
  3101. h->delta_poc[0] = get_se_golomb(&h->gb);
  3102. if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
  3103. h->delta_poc[1] = get_se_golomb(&h->gb);
  3104. }
  3105. ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);
  3106. if (h->pps.redundant_pic_cnt_present)
  3107. h->redundant_pic_count = get_ue_golomb(&h->gb);
  3108. // set defaults, might be overridden a few lines later
  3109. h->ref_count[0] = h->pps.ref_count[0];
  3110. h->ref_count[1] = h->pps.ref_count[1];
  3111. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  3112. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  3113. h->direct_spatial_mv_pred = get_bits1(&h->gb);
  3114. num_ref_idx_active_override_flag = get_bits1(&h->gb);
  3115. if (num_ref_idx_active_override_flag) {
  3116. h->ref_count[0] = get_ue_golomb(&h->gb) + 1;
  3117. if (h->ref_count[0] < 1)
  3118. return AVERROR_INVALIDDATA;
  3119. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3120. h->ref_count[1] = get_ue_golomb(&h->gb) + 1;
  3121. if (h->ref_count[1] < 1)
  3122. return AVERROR_INVALIDDATA;
  3123. }
  3124. }
  3125. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  3126. h->list_count = 2;
  3127. else
  3128. h->list_count = 1;
  3129. } else {
  3130. h->list_count = 0;
  3131. h->ref_count[0] = h->ref_count[1] = 0;
  3132. }
  3133. max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
  3134. if (h->ref_count[0] > max_refs || h->ref_count[1] > max_refs) {
  3135. av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
  3136. h->ref_count[0] = h->ref_count[1] = 0;
  3137. return AVERROR_INVALIDDATA;
  3138. }
  3139. if (!default_ref_list_done)
  3140. ff_h264_fill_default_ref_list(h);
  3141. if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
  3142. ff_h264_decode_ref_pic_list_reordering(h) < 0) {
  3143. h->ref_count[1] = h->ref_count[0] = 0;
  3144. return -1;
  3145. }
  3146. if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
  3147. (h->pps.weighted_bipred_idc == 1 &&
  3148. h->slice_type_nos == AV_PICTURE_TYPE_B))
  3149. pred_weight_table(h);
  3150. else if (h->pps.weighted_bipred_idc == 2 &&
  3151. h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3152. implicit_weight_table(h, -1);
  3153. } else {
  3154. h->use_weight = 0;
  3155. for (i = 0; i < 2; i++) {
  3156. h->luma_weight_flag[i] = 0;
  3157. h->chroma_weight_flag[i] = 0;
  3158. }
  3159. }
  3160. // If frame-mt is enabled, only update mmco tables for the first slice
  3161. // in a field. Subsequent slices can temporarily clobber h->mmco_index
  3162. // or h->mmco, which will cause ref list mix-ups and decoding errors
  3163. // further down the line. This may break decoding if the first slice is
  3164. // corrupt, thus we only do this if frame-mt is enabled.
  3165. if (h->nal_ref_idc &&
  3166. ff_h264_decode_ref_pic_marking(h0, &h->gb,
  3167. !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
  3168. h0->current_slice == 0) < 0 &&
  3169. (h->avctx->err_recognition & AV_EF_EXPLODE))
  3170. return AVERROR_INVALIDDATA;
  3171. if (FRAME_MBAFF(h)) {
  3172. ff_h264_fill_mbaff_ref_list(h);
  3173. if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3174. implicit_weight_table(h, 0);
  3175. implicit_weight_table(h, 1);
  3176. }
  3177. }
  3178. if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  3179. ff_h264_direct_dist_scale_factor(h);
  3180. ff_h264_direct_ref_list_init(h);
  3181. if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
  3182. tmp = get_ue_golomb_31(&h->gb);
  3183. if (tmp > 2) {
  3184. av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3185. return -1;
  3186. }
  3187. h->cabac_init_idc = tmp;
  3188. }
  3189. h->last_qscale_diff = 0;
  3190. tmp = h->pps.init_qp + get_se_golomb(&h->gb);
  3191. if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
  3192. av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3193. return -1;
  3194. }
  3195. h->qscale = tmp;
  3196. h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
  3197. h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
  3198. // FIXME qscale / qp ... stuff
  3199. if (h->slice_type == AV_PICTURE_TYPE_SP)
  3200. get_bits1(&h->gb); /* sp_for_switch_flag */
  3201. if (h->slice_type == AV_PICTURE_TYPE_SP ||
  3202. h->slice_type == AV_PICTURE_TYPE_SI)
  3203. get_se_golomb(&h->gb); /* slice_qs_delta */
  3204. h->deblocking_filter = 1;
  3205. h->slice_alpha_c0_offset = 52;
  3206. h->slice_beta_offset = 52;
  3207. if (h->pps.deblocking_filter_parameters_present) {
  3208. tmp = get_ue_golomb_31(&h->gb);
  3209. if (tmp > 2) {
  3210. av_log(h->avctx, AV_LOG_ERROR,
  3211. "deblocking_filter_idc %u out of range\n", tmp);
  3212. return -1;
  3213. }
  3214. h->deblocking_filter = tmp;
  3215. if (h->deblocking_filter < 2)
  3216. h->deblocking_filter ^= 1; // 1<->0
  3217. if (h->deblocking_filter) {
  3218. h->slice_alpha_c0_offset += get_se_golomb(&h->gb) << 1;
  3219. h->slice_beta_offset += get_se_golomb(&h->gb) << 1;
  3220. if (h->slice_alpha_c0_offset > 104U ||
  3221. h->slice_beta_offset > 104U) {
  3222. av_log(h->avctx, AV_LOG_ERROR,
  3223. "deblocking filter parameters %d %d out of range\n",
  3224. h->slice_alpha_c0_offset, h->slice_beta_offset);
  3225. return -1;
  3226. }
  3227. }
  3228. }
  3229. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  3230. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  3231. h->slice_type_nos != AV_PICTURE_TYPE_I) ||
  3232. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  3233. h->slice_type_nos == AV_PICTURE_TYPE_B) ||
  3234. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  3235. h->nal_ref_idc == 0))
  3236. h->deblocking_filter = 0;
  3237. if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
  3238. if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
  3239. /* Cheat slightly for speed:
  3240. * Do not bother to deblock across slices. */
  3241. h->deblocking_filter = 2;
  3242. } else {
  3243. h0->max_contexts = 1;
  3244. if (!h0->single_decode_warning) {
  3245. av_log(h->avctx, AV_LOG_INFO,
  3246. "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  3247. h0->single_decode_warning = 1;
  3248. }
  3249. if (h != h0) {
  3250. av_log(h->avctx, AV_LOG_ERROR,
  3251. "Deblocking switched inside frame.\n");
  3252. return 1;
  3253. }
  3254. }
  3255. }
  3256. h->qp_thresh = 15 + 52 -
  3257. FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
  3258. FFMAX3(0,
  3259. h->pps.chroma_qp_index_offset[0],
  3260. h->pps.chroma_qp_index_offset[1]) +
  3261. 6 * (h->sps.bit_depth_luma - 8);
  3262. h0->last_slice_type = slice_type;
  3263. h->slice_num = ++h0->current_slice;
  3264. if (h->slice_num >= MAX_SLICES) {
  3265. av_log(h->avctx, AV_LOG_ERROR,
  3266. "Too many slices, increase MAX_SLICES and recompile\n");
  3267. }
  3268. for (j = 0; j < 2; j++) {
  3269. int id_list[16];
  3270. int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
  3271. for (i = 0; i < 16; i++) {
  3272. id_list[i] = 60;
  3273. if (j < h->list_count && i < h->ref_count[j] &&
  3274. h->ref_list[j][i].f.buf[0]) {
  3275. int k;
  3276. AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
  3277. for (k = 0; k < h->short_ref_count; k++)
  3278. if (h->short_ref[k]->f.buf[0]->buffer == buf) {
  3279. id_list[i] = k;
  3280. break;
  3281. }
  3282. for (k = 0; k < h->long_ref_count; k++)
  3283. if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
  3284. id_list[i] = h->short_ref_count + k;
  3285. break;
  3286. }
  3287. }
  3288. }
  3289. ref2frm[0] =
  3290. ref2frm[1] = -1;
  3291. for (i = 0; i < 16; i++)
  3292. ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);
  3293. ref2frm[18 + 0] =
  3294. ref2frm[18 + 1] = -1;
  3295. for (i = 16; i < 48; i++)
  3296. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  3297. (h->ref_list[j][i].reference & 3);
  3298. }
  3299. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  3300. av_log(h->avctx, AV_LOG_DEBUG,
  3301. "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
  3302. h->slice_num,
  3303. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  3304. first_mb_in_slice,
  3305. av_get_picture_type_char(h->slice_type),
  3306. h->slice_type_fixed ? " fix" : "",
  3307. h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  3308. pps_id, h->frame_num,
  3309. h->cur_pic_ptr->field_poc[0],
  3310. h->cur_pic_ptr->field_poc[1],
  3311. h->ref_count[0], h->ref_count[1],
  3312. h->qscale,
  3313. h->deblocking_filter,
  3314. h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
  3315. h->use_weight,
  3316. h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
  3317. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  3318. }
  3319. return 0;
  3320. }
  3321. int ff_h264_get_slice_type(const H264Context *h)
  3322. {
  3323. switch (h->slice_type) {
  3324. case AV_PICTURE_TYPE_P:
  3325. return 0;
  3326. case AV_PICTURE_TYPE_B:
  3327. return 1;
  3328. case AV_PICTURE_TYPE_I:
  3329. return 2;
  3330. case AV_PICTURE_TYPE_SP:
  3331. return 3;
  3332. case AV_PICTURE_TYPE_SI:
  3333. return 4;
  3334. default:
  3335. return -1;
  3336. }
  3337. }
  3338. static av_always_inline void fill_filter_caches_inter(H264Context *h,
  3339. int mb_type, int top_xy,
  3340. int left_xy[LEFT_MBS],
  3341. int top_type,
  3342. int left_type[LEFT_MBS],
  3343. int mb_xy, int list)
  3344. {
  3345. int b_stride = h->b_stride;
  3346. int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
  3347. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  3348. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  3349. if (USES_LIST(top_type, list)) {
  3350. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  3351. const int b8_xy = 4 * top_xy + 2;
  3352. int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);
  3353. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  3354. ref_cache[0 - 1 * 8] =
  3355. ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
  3356. ref_cache[2 - 1 * 8] =
  3357. ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
  3358. } else {
  3359. AV_ZERO128(mv_dst - 1 * 8);
  3360. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3361. }
  3362. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  3363. if (USES_LIST(left_type[LTOP], list)) {
  3364. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  3365. const int b8_xy = 4 * left_xy[LTOP] + 1;
  3366. int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);
  3367. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  3368. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  3369. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  3370. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  3371. ref_cache[-1 + 0] =
  3372. ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  3373. ref_cache[-1 + 16] =
  3374. ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  3375. } else {
  3376. AV_ZERO32(mv_dst - 1 + 0);
  3377. AV_ZERO32(mv_dst - 1 + 8);
  3378. AV_ZERO32(mv_dst - 1 + 16);
  3379. AV_ZERO32(mv_dst - 1 + 24);
  3380. ref_cache[-1 + 0] =
  3381. ref_cache[-1 + 8] =
  3382. ref_cache[-1 + 16] =
  3383. ref_cache[-1 + 24] = LIST_NOT_USED;
  3384. }
  3385. }
  3386. }
  3387. if (!USES_LIST(mb_type, list)) {
  3388. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  3389. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3390. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3391. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3392. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3393. return;
  3394. }
  3395. {
  3396. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  3397. int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);
  3398. uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
  3399. uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
  3400. AV_WN32A(&ref_cache[0 * 8], ref01);
  3401. AV_WN32A(&ref_cache[1 * 8], ref01);
  3402. AV_WN32A(&ref_cache[2 * 8], ref23);
  3403. AV_WN32A(&ref_cache[3 * 8], ref23);
  3404. }
  3405. {
  3406. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
  3407. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  3408. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  3409. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  3410. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  3411. }
  3412. }
  3413. /**
  3414. *
  3415. * @return non zero if the loop filter can be skipped
  3416. */
  3417. static int fill_filter_caches(H264Context *h, int mb_type)
  3418. {
  3419. const int mb_xy = h->mb_xy;
  3420. int top_xy, left_xy[LEFT_MBS];
  3421. int top_type, left_type[LEFT_MBS];
  3422. uint8_t *nnz;
  3423. uint8_t *nnz_cache;
  3424. top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
  3425. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  3426. * stuff, I can't imagine that these complex rules are worth it. */
  3427. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  3428. if (FRAME_MBAFF(h)) {
  3429. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  3430. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  3431. if (h->mb_y & 1) {
  3432. if (left_mb_field_flag != curr_mb_field_flag)
  3433. left_xy[LTOP] -= h->mb_stride;
  3434. } else {
  3435. if (curr_mb_field_flag)
  3436. top_xy += h->mb_stride &
  3437. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  3438. if (left_mb_field_flag != curr_mb_field_flag)
  3439. left_xy[LBOT] += h->mb_stride;
  3440. }
  3441. }
  3442. h->top_mb_xy = top_xy;
  3443. h->left_mb_xy[LTOP] = left_xy[LTOP];
  3444. h->left_mb_xy[LBOT] = left_xy[LBOT];
  3445. {
  3446. /* For sufficiently low qp, filtering wouldn't do anything.
  3447. * This is a conservative estimate: could also check beta_offset
  3448. * and more accurate chroma_qp. */
  3449. int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  3450. int qp = h->cur_pic.qscale_table[mb_xy];
  3451. if (qp <= qp_thresh &&
  3452. (left_xy[LTOP] < 0 ||
  3453. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  3454. (top_xy < 0 ||
  3455. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  3456. if (!FRAME_MBAFF(h))
  3457. return 1;
  3458. if ((left_xy[LTOP] < 0 ||
  3459. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  3460. (top_xy < h->mb_stride ||
  3461. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  3462. return 1;
  3463. }
  3464. }
  3465. top_type = h->cur_pic.mb_type[top_xy];
  3466. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  3467. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  3468. if (h->deblocking_filter == 2) {
  3469. if (h->slice_table[top_xy] != h->slice_num)
  3470. top_type = 0;
  3471. if (h->slice_table[left_xy[LBOT]] != h->slice_num)
  3472. left_type[LTOP] = left_type[LBOT] = 0;
  3473. } else {
  3474. if (h->slice_table[top_xy] == 0xFFFF)
  3475. top_type = 0;
  3476. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  3477. left_type[LTOP] = left_type[LBOT] = 0;
  3478. }
  3479. h->top_type = top_type;
  3480. h->left_type[LTOP] = left_type[LTOP];
  3481. h->left_type[LBOT] = left_type[LBOT];
  3482. if (IS_INTRA(mb_type))
  3483. return 0;
  3484. fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
  3485. top_type, left_type, mb_xy, 0);
  3486. if (h->list_count == 2)
  3487. fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
  3488. top_type, left_type, mb_xy, 1);
  3489. nnz = h->non_zero_count[mb_xy];
  3490. nnz_cache = h->non_zero_count_cache;
  3491. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  3492. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  3493. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  3494. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  3495. h->cbp = h->cbp_table[mb_xy];
  3496. if (top_type) {
  3497. nnz = h->non_zero_count[top_xy];
  3498. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  3499. }
  3500. if (left_type[LTOP]) {
  3501. nnz = h->non_zero_count[left_xy[LTOP]];
  3502. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  3503. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  3504. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  3505. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  3506. }
  3507. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  3508. * from what the loop filter needs */
  3509. if (!CABAC(h) && h->pps.transform_8x8_mode) {
  3510. if (IS_8x8DCT(top_type)) {
  3511. nnz_cache[4 + 8 * 0] =
  3512. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  3513. nnz_cache[6 + 8 * 0] =
  3514. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  3515. }
  3516. if (IS_8x8DCT(left_type[LTOP])) {
  3517. nnz_cache[3 + 8 * 1] =
  3518. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  3519. }
  3520. if (IS_8x8DCT(left_type[LBOT])) {
  3521. nnz_cache[3 + 8 * 3] =
  3522. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  3523. }
  3524. if (IS_8x8DCT(mb_type)) {
  3525. nnz_cache[scan8[0]] =
  3526. nnz_cache[scan8[1]] =
  3527. nnz_cache[scan8[2]] =
  3528. nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
  3529. nnz_cache[scan8[0 + 4]] =
  3530. nnz_cache[scan8[1 + 4]] =
  3531. nnz_cache[scan8[2 + 4]] =
  3532. nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
  3533. nnz_cache[scan8[0 + 8]] =
  3534. nnz_cache[scan8[1 + 8]] =
  3535. nnz_cache[scan8[2 + 8]] =
  3536. nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
  3537. nnz_cache[scan8[0 + 12]] =
  3538. nnz_cache[scan8[1 + 12]] =
  3539. nnz_cache[scan8[2 + 12]] =
  3540. nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
  3541. }
  3542. }
  3543. return 0;
  3544. }
  3545. static void loop_filter(H264Context *h, int start_x, int end_x)
  3546. {
  3547. uint8_t *dest_y, *dest_cb, *dest_cr;
  3548. int linesize, uvlinesize, mb_x, mb_y;
  3549. const int end_mb_y = h->mb_y + FRAME_MBAFF(h);
  3550. const int old_slice_type = h->slice_type;
  3551. const int pixel_shift = h->pixel_shift;
  3552. const int block_h = 16 >> h->chroma_y_shift;
  3553. if (h->deblocking_filter) {
  3554. for (mb_x = start_x; mb_x < end_x; mb_x++)
  3555. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  3556. int mb_xy, mb_type;
  3557. mb_xy = h->mb_xy = mb_x + mb_y * h->mb_stride;
  3558. h->slice_num = h->slice_table[mb_xy];
  3559. mb_type = h->cur_pic.mb_type[mb_xy];
  3560. h->list_count = h->list_counts[mb_xy];
  3561. if (FRAME_MBAFF(h))
  3562. h->mb_mbaff =
  3563. h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  3564. h->mb_x = mb_x;
  3565. h->mb_y = mb_y;
  3566. dest_y = h->cur_pic.f.data[0] +
  3567. ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
  3568. dest_cb = h->cur_pic.f.data[1] +
  3569. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  3570. mb_y * h->uvlinesize * block_h;
  3571. dest_cr = h->cur_pic.f.data[2] +
  3572. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  3573. mb_y * h->uvlinesize * block_h;
  3574. // FIXME simplify above
  3575. if (MB_FIELD(h)) {
  3576. linesize = h->mb_linesize = h->linesize * 2;
  3577. uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
  3578. if (mb_y & 1) { // FIXME move out of this function?
  3579. dest_y -= h->linesize * 15;
  3580. dest_cb -= h->uvlinesize * (block_h - 1);
  3581. dest_cr -= h->uvlinesize * (block_h - 1);
  3582. }
  3583. } else {
  3584. linesize = h->mb_linesize = h->linesize;
  3585. uvlinesize = h->mb_uvlinesize = h->uvlinesize;
  3586. }
  3587. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  3588. uvlinesize, 0);
  3589. if (fill_filter_caches(h, mb_type))
  3590. continue;
  3591. h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
  3592. h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
  3593. if (FRAME_MBAFF(h)) {
  3594. ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  3595. linesize, uvlinesize);
  3596. } else {
  3597. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
  3598. dest_cr, linesize, uvlinesize);
  3599. }
  3600. }
  3601. }
  3602. h->slice_type = old_slice_type;
  3603. h->mb_x = end_x;
  3604. h->mb_y = end_mb_y - FRAME_MBAFF(h);
  3605. h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
  3606. h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
  3607. }
  3608. static void predict_field_decoding_flag(H264Context *h)
  3609. {
  3610. const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
  3611. int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
  3612. h->cur_pic.mb_type[mb_xy - 1] :
  3613. (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
  3614. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  3615. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  3616. }
  3617. /**
  3618. * Draw edges and report progress for the last MB row.
  3619. */
  3620. static void decode_finish_row(H264Context *h)
  3621. {
  3622. int top = 16 * (h->mb_y >> FIELD_PICTURE(h));
  3623. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  3624. int height = 16 << FRAME_MBAFF(h);
  3625. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  3626. if (h->deblocking_filter) {
  3627. if ((top + height) >= pic_height)
  3628. height += deblock_border;
  3629. top -= deblock_border;
  3630. }
  3631. if (top >= pic_height || (top + height) < 0)
  3632. return;
  3633. height = FFMIN(height, pic_height - top);
  3634. if (top < 0) {
  3635. height = top + height;
  3636. top = 0;
  3637. }
  3638. ff_h264_draw_horiz_band(h, top, height);
  3639. if (h->droppable)
  3640. return;
  3641. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  3642. h->picture_structure == PICT_BOTTOM_FIELD);
  3643. }
  3644. static void er_add_slice(H264Context *h, int startx, int starty,
  3645. int endx, int endy, int status)
  3646. {
  3647. #if CONFIG_ERROR_RESILIENCE
  3648. ERContext *er = &h->er;
  3649. er->ref_count = h->ref_count[0];
  3650. ff_er_add_slice(er, startx, starty, endx, endy, status);
  3651. #endif
  3652. }
  3653. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  3654. {
  3655. H264Context *h = *(void **)arg;
  3656. int lf_x_start = h->mb_x;
  3657. h->mb_skip_run = -1;
  3658. h->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  3659. avctx->codec_id != AV_CODEC_ID_H264 ||
  3660. (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
  3661. if (h->pps.cabac) {
  3662. /* realign */
  3663. align_get_bits(&h->gb);
  3664. /* init cabac */
  3665. ff_init_cabac_states();
  3666. ff_init_cabac_decoder(&h->cabac,
  3667. h->gb.buffer + get_bits_count(&h->gb) / 8,
  3668. (get_bits_left(&h->gb) + 7) / 8);
  3669. ff_h264_init_cabac_states(h);
  3670. for (;;) {
  3671. // START_TIMER
  3672. int ret = ff_h264_decode_mb_cabac(h);
  3673. int eos;
  3674. // STOP_TIMER("decode_mb_cabac")
  3675. if (ret >= 0)
  3676. ff_h264_hl_decode_mb(h);
  3677. // FIXME optimal? or let mb_decode decode 16x32 ?
  3678. if (ret >= 0 && FRAME_MBAFF(h)) {
  3679. h->mb_y++;
  3680. ret = ff_h264_decode_mb_cabac(h);
  3681. if (ret >= 0)
  3682. ff_h264_hl_decode_mb(h);
  3683. h->mb_y--;
  3684. }
  3685. eos = get_cabac_terminate(&h->cabac);
  3686. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  3687. h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  3688. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
  3689. h->mb_y, ER_MB_END);
  3690. if (h->mb_x >= lf_x_start)
  3691. loop_filter(h, lf_x_start, h->mb_x + 1);
  3692. return 0;
  3693. }
  3694. if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  3695. av_log(h->avctx, AV_LOG_ERROR,
  3696. "error while decoding MB %d %d, bytestream (%td)\n",
  3697. h->mb_x, h->mb_y,
  3698. h->cabac.bytestream_end - h->cabac.bytestream);
  3699. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  3700. h->mb_y, ER_MB_ERROR);
  3701. return -1;
  3702. }
  3703. if (++h->mb_x >= h->mb_width) {
  3704. loop_filter(h, lf_x_start, h->mb_x);
  3705. h->mb_x = lf_x_start = 0;
  3706. decode_finish_row(h);
  3707. ++h->mb_y;
  3708. if (FIELD_OR_MBAFF_PICTURE(h)) {
  3709. ++h->mb_y;
  3710. if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
  3711. predict_field_decoding_flag(h);
  3712. }
  3713. }
  3714. if (eos || h->mb_y >= h->mb_height) {
  3715. tprintf(h->avctx, "slice end %d %d\n",
  3716. get_bits_count(&h->gb), h->gb.size_in_bits);
  3717. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
  3718. h->mb_y, ER_MB_END);
  3719. if (h->mb_x > lf_x_start)
  3720. loop_filter(h, lf_x_start, h->mb_x);
  3721. return 0;
  3722. }
  3723. }
  3724. } else {
  3725. for (;;) {
  3726. int ret = ff_h264_decode_mb_cavlc(h);
  3727. if (ret >= 0)
  3728. ff_h264_hl_decode_mb(h);
  3729. // FIXME optimal? or let mb_decode decode 16x32 ?
  3730. if (ret >= 0 && FRAME_MBAFF(h)) {
  3731. h->mb_y++;
  3732. ret = ff_h264_decode_mb_cavlc(h);
  3733. if (ret >= 0)
  3734. ff_h264_hl_decode_mb(h);
  3735. h->mb_y--;
  3736. }
  3737. if (ret < 0) {
  3738. av_log(h->avctx, AV_LOG_ERROR,
  3739. "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
  3740. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  3741. h->mb_y, ER_MB_ERROR);
  3742. return -1;
  3743. }
  3744. if (++h->mb_x >= h->mb_width) {
  3745. loop_filter(h, lf_x_start, h->mb_x);
  3746. h->mb_x = lf_x_start = 0;
  3747. decode_finish_row(h);
  3748. ++h->mb_y;
  3749. if (FIELD_OR_MBAFF_PICTURE(h)) {
  3750. ++h->mb_y;
  3751. if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
  3752. predict_field_decoding_flag(h);
  3753. }
  3754. if (h->mb_y >= h->mb_height) {
  3755. tprintf(h->avctx, "slice end %d %d\n",
  3756. get_bits_count(&h->gb), h->gb.size_in_bits);
  3757. if (get_bits_left(&h->gb) == 0) {
  3758. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  3759. h->mb_x - 1, h->mb_y,
  3760. ER_MB_END);
  3761. return 0;
  3762. } else {
  3763. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  3764. h->mb_x - 1, h->mb_y,
  3765. ER_MB_END);
  3766. return -1;
  3767. }
  3768. }
  3769. }
  3770. if (get_bits_left(&h->gb) <= 0 && h->mb_skip_run <= 0) {
  3771. tprintf(h->avctx, "slice end %d %d\n",
  3772. get_bits_count(&h->gb), h->gb.size_in_bits);
  3773. if (get_bits_left(&h->gb) == 0) {
  3774. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  3775. h->mb_x - 1, h->mb_y,
  3776. ER_MB_END);
  3777. if (h->mb_x > lf_x_start)
  3778. loop_filter(h, lf_x_start, h->mb_x);
  3779. return 0;
  3780. } else {
  3781. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  3782. h->mb_y, ER_MB_ERROR);
  3783. return -1;
  3784. }
  3785. }
  3786. }
  3787. }
  3788. }
  3789. /**
  3790. * Call decode_slice() for each context.
  3791. *
  3792. * @param h h264 master context
  3793. * @param context_count number of contexts to execute
  3794. */
  3795. static int execute_decode_slices(H264Context *h, int context_count)
  3796. {
  3797. AVCodecContext *const avctx = h->avctx;
  3798. H264Context *hx;
  3799. int i;
  3800. if (h->avctx->hwaccel ||
  3801. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  3802. return 0;
  3803. if (context_count == 1) {
  3804. return decode_slice(avctx, &h);
  3805. } else {
  3806. for (i = 1; i < context_count; i++) {
  3807. hx = h->thread_context[i];
  3808. hx->er.error_count = 0;
  3809. }
  3810. avctx->execute(avctx, decode_slice, h->thread_context,
  3811. NULL, context_count, sizeof(void *));
  3812. /* pull back stuff from slices to master context */
  3813. hx = h->thread_context[context_count - 1];
  3814. h->mb_x = hx->mb_x;
  3815. h->mb_y = hx->mb_y;
  3816. h->droppable = hx->droppable;
  3817. h->picture_structure = hx->picture_structure;
  3818. for (i = 1; i < context_count; i++)
  3819. h->er.error_count += h->thread_context[i]->er.error_count;
  3820. }
  3821. return 0;
  3822. }
  3823. static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
  3824. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  3825. int parse_extradata)
  3826. {
  3827. AVCodecContext *const avctx = h->avctx;
  3828. H264Context *hx; ///< thread context
  3829. int buf_index;
  3830. int context_count;
  3831. int next_avc;
  3832. int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
  3833. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  3834. int nal_index;
  3835. h->max_contexts = h->slice_context_count;
  3836. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
  3837. h->current_slice = 0;
  3838. if (!h->first_field)
  3839. h->cur_pic_ptr = NULL;
  3840. ff_h264_reset_sei(h);
  3841. }
  3842. for (; pass <= 1; pass++) {
  3843. buf_index = 0;
  3844. context_count = 0;
  3845. next_avc = h->is_avc ? 0 : buf_size;
  3846. nal_index = 0;
  3847. for (;;) {
  3848. int consumed;
  3849. int dst_length;
  3850. int bit_length;
  3851. const uint8_t *ptr;
  3852. int i, nalsize = 0;
  3853. int err;
  3854. if (buf_index >= next_avc) {
  3855. if (buf_index >= buf_size - h->nal_length_size)
  3856. break;
  3857. nalsize = 0;
  3858. for (i = 0; i < h->nal_length_size; i++)
  3859. nalsize = (nalsize << 8) | buf[buf_index++];
  3860. if (nalsize <= 0 || nalsize > buf_size - buf_index) {
  3861. av_log(h->avctx, AV_LOG_ERROR,
  3862. "AVC: nal size %d\n", nalsize);
  3863. break;
  3864. }
  3865. next_avc = buf_index + nalsize;
  3866. } else {
  3867. // start code prefix search
  3868. for (; buf_index + 3 < next_avc; buf_index++)
  3869. // This should always succeed in the first iteration.
  3870. if (buf[buf_index] == 0 &&
  3871. buf[buf_index + 1] == 0 &&
  3872. buf[buf_index + 2] == 1)
  3873. break;
  3874. if (buf_index + 3 >= buf_size) {
  3875. buf_index = buf_size;
  3876. break;
  3877. }
  3878. buf_index += 3;
  3879. if (buf_index >= next_avc)
  3880. continue;
  3881. }
  3882. hx = h->thread_context[context_count];
  3883. ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
  3884. &consumed, next_avc - buf_index);
  3885. if (ptr == NULL || dst_length < 0) {
  3886. buf_index = -1;
  3887. goto end;
  3888. }
  3889. i = buf_index + consumed;
  3890. if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
  3891. buf[i] == 0x00 && buf[i + 1] == 0x00 &&
  3892. buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
  3893. h->workaround_bugs |= FF_BUG_TRUNCATED;
  3894. if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
  3895. while (ptr[dst_length - 1] == 0 && dst_length > 0)
  3896. dst_length--;
  3897. bit_length = !dst_length ? 0
  3898. : (8 * dst_length -
  3899. decode_rbsp_trailing(h, ptr + dst_length - 1));
  3900. if (h->avctx->debug & FF_DEBUG_STARTCODE)
  3901. av_log(h->avctx, AV_LOG_DEBUG,
  3902. "NAL %d at %d/%d length %d\n",
  3903. hx->nal_unit_type, buf_index, buf_size, dst_length);
  3904. if (h->is_avc && (nalsize != consumed) && nalsize)
  3905. av_log(h->avctx, AV_LOG_DEBUG,
  3906. "AVC: Consumed only %d bytes instead of %d\n",
  3907. consumed, nalsize);
  3908. buf_index += consumed;
  3909. nal_index++;
  3910. if (pass == 0) {
  3911. /* packets can sometimes contain multiple PPS/SPS,
  3912. * e.g. two PAFF field pictures in one packet, or a demuxer
  3913. * which splits NALs strangely if so, when frame threading we
  3914. * can't start the next thread until we've read all of them */
  3915. switch (hx->nal_unit_type) {
  3916. case NAL_SPS:
  3917. case NAL_PPS:
  3918. nals_needed = nal_index;
  3919. break;
  3920. case NAL_DPA:
  3921. case NAL_IDR_SLICE:
  3922. case NAL_SLICE:
  3923. init_get_bits(&hx->gb, ptr, bit_length);
  3924. if (!get_ue_golomb(&hx->gb))
  3925. nals_needed = nal_index;
  3926. }
  3927. continue;
  3928. }
  3929. // FIXME do not discard SEI id
  3930. if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
  3931. continue;
  3932. again:
  3933. /* Ignore every NAL unit type except PPS and SPS during extradata
  3934. * parsing. Decoding slices is not possible in codec init
  3935. * with frame-mt */
  3936. if (parse_extradata && HAVE_THREADS &&
  3937. (h->avctx->active_thread_type & FF_THREAD_FRAME) &&
  3938. (hx->nal_unit_type != NAL_PPS &&
  3939. hx->nal_unit_type != NAL_SPS)) {
  3940. av_log(avctx, AV_LOG_INFO, "Ignoring NAL unit %d during "
  3941. "extradata parsing\n", hx->nal_unit_type);
  3942. hx->nal_unit_type = NAL_FF_IGNORE;
  3943. }
  3944. err = 0;
  3945. switch (hx->nal_unit_type) {
  3946. case NAL_IDR_SLICE:
  3947. if (h->nal_unit_type != NAL_IDR_SLICE) {
  3948. av_log(h->avctx, AV_LOG_ERROR,
  3949. "Invalid mix of idr and non-idr slices\n");
  3950. buf_index = -1;
  3951. goto end;
  3952. }
  3953. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  3954. case NAL_SLICE:
  3955. init_get_bits(&hx->gb, ptr, bit_length);
  3956. hx->intra_gb_ptr =
  3957. hx->inter_gb_ptr = &hx->gb;
  3958. hx->data_partitioning = 0;
  3959. if ((err = decode_slice_header(hx, h)))
  3960. break;
  3961. h->cur_pic_ptr->f.key_frame |=
  3962. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  3963. (h->sei_recovery_frame_cnt >= 0);
  3964. if (h->current_slice == 1) {
  3965. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
  3966. decode_postinit(h, nal_index >= nals_needed);
  3967. if (h->avctx->hwaccel &&
  3968. h->avctx->hwaccel->start_frame(h->avctx, NULL, 0) < 0)
  3969. return -1;
  3970. if (CONFIG_H264_VDPAU_DECODER &&
  3971. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  3972. ff_vdpau_h264_picture_start(h);
  3973. }
  3974. if (hx->redundant_pic_count == 0 &&
  3975. (avctx->skip_frame < AVDISCARD_NONREF ||
  3976. hx->nal_ref_idc) &&
  3977. (avctx->skip_frame < AVDISCARD_BIDIR ||
  3978. hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
  3979. (avctx->skip_frame < AVDISCARD_NONKEY ||
  3980. hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
  3981. avctx->skip_frame < AVDISCARD_ALL) {
  3982. if (avctx->hwaccel) {
  3983. if (avctx->hwaccel->decode_slice(avctx,
  3984. &buf[buf_index - consumed],
  3985. consumed) < 0)
  3986. return -1;
  3987. } else if (CONFIG_H264_VDPAU_DECODER &&
  3988. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
  3989. ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
  3990. start_code,
  3991. sizeof(start_code));
  3992. ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
  3993. &buf[buf_index - consumed],
  3994. consumed);
  3995. } else
  3996. context_count++;
  3997. }
  3998. break;
  3999. case NAL_DPA:
  4000. init_get_bits(&hx->gb, ptr, bit_length);
  4001. hx->intra_gb_ptr =
  4002. hx->inter_gb_ptr = NULL;
  4003. if ((err = decode_slice_header(hx, h)) < 0)
  4004. break;
  4005. hx->data_partitioning = 1;
  4006. break;
  4007. case NAL_DPB:
  4008. init_get_bits(&hx->intra_gb, ptr, bit_length);
  4009. hx->intra_gb_ptr = &hx->intra_gb;
  4010. break;
  4011. case NAL_DPC:
  4012. init_get_bits(&hx->inter_gb, ptr, bit_length);
  4013. hx->inter_gb_ptr = &hx->inter_gb;
  4014. if (hx->redundant_pic_count == 0 &&
  4015. hx->intra_gb_ptr &&
  4016. hx->data_partitioning &&
  4017. h->cur_pic_ptr && h->context_initialized &&
  4018. (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
  4019. (avctx->skip_frame < AVDISCARD_BIDIR ||
  4020. hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
  4021. (avctx->skip_frame < AVDISCARD_NONKEY ||
  4022. hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
  4023. avctx->skip_frame < AVDISCARD_ALL)
  4024. context_count++;
  4025. break;
  4026. case NAL_SEI:
  4027. init_get_bits(&h->gb, ptr, bit_length);
  4028. ff_h264_decode_sei(h);
  4029. break;
  4030. case NAL_SPS:
  4031. init_get_bits(&h->gb, ptr, bit_length);
  4032. if (ff_h264_decode_seq_parameter_set(h) < 0 &&
  4033. h->is_avc && (nalsize != consumed) && nalsize) {
  4034. av_log(h->avctx, AV_LOG_DEBUG,
  4035. "SPS decoding failure, trying again with the complete NAL\n");
  4036. init_get_bits(&h->gb, buf + buf_index + 1 - consumed,
  4037. 8 * (nalsize - 1));
  4038. ff_h264_decode_seq_parameter_set(h);
  4039. }
  4040. if (h264_set_parameter_from_sps(h) < 0) {
  4041. buf_index = -1;
  4042. goto end;
  4043. }
  4044. break;
  4045. case NAL_PPS:
  4046. init_get_bits(&h->gb, ptr, bit_length);
  4047. ff_h264_decode_picture_parameter_set(h, bit_length);
  4048. break;
  4049. case NAL_AUD:
  4050. case NAL_END_SEQUENCE:
  4051. case NAL_END_STREAM:
  4052. case NAL_FILLER_DATA:
  4053. case NAL_SPS_EXT:
  4054. case NAL_AUXILIARY_SLICE:
  4055. break;
  4056. case NAL_FF_IGNORE:
  4057. break;
  4058. default:
  4059. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  4060. hx->nal_unit_type, bit_length);
  4061. }
  4062. if (context_count == h->max_contexts) {
  4063. execute_decode_slices(h, context_count);
  4064. context_count = 0;
  4065. }
  4066. if (err < 0)
  4067. av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  4068. else if (err == 1) {
  4069. /* Slice could not be decoded in parallel mode, copy down
  4070. * NAL unit stuff to context 0 and restart. Note that
  4071. * rbsp_buffer is not transferred, but since we no longer
  4072. * run in parallel mode this should not be an issue. */
  4073. h->nal_unit_type = hx->nal_unit_type;
  4074. h->nal_ref_idc = hx->nal_ref_idc;
  4075. hx = h;
  4076. goto again;
  4077. }
  4078. }
  4079. }
  4080. if (context_count)
  4081. execute_decode_slices(h, context_count);
  4082. end:
  4083. /* clean up */
  4084. if (h->cur_pic_ptr && !h->droppable) {
  4085. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  4086. h->picture_structure == PICT_BOTTOM_FIELD);
  4087. }
  4088. return buf_index;
  4089. }
  4090. /**
  4091. * Return the number of bytes consumed for building the current frame.
  4092. */
  4093. static int get_consumed_bytes(int pos, int buf_size)
  4094. {
  4095. if (pos == 0)
  4096. pos = 1; // avoid infinite loops (i doubt that is needed but ...)
  4097. if (pos + 10 > buf_size)
  4098. pos = buf_size; // oops ;)
  4099. return pos;
  4100. }
  4101. static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
  4102. {
  4103. int i;
  4104. int ret = av_frame_ref(dst, src);
  4105. if (ret < 0)
  4106. return ret;
  4107. if (!h->sps.crop)
  4108. return 0;
  4109. for (i = 0; i < 3; i++) {
  4110. int hshift = (i > 0) ? h->chroma_x_shift : 0;
  4111. int vshift = (i > 0) ? h->chroma_y_shift : 0;
  4112. int off = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
  4113. (h->sps.crop_top >> vshift) * dst->linesize[i];
  4114. dst->data[i] += off;
  4115. }
  4116. return 0;
  4117. }
  4118. static int decode_frame(AVCodecContext *avctx, void *data,
  4119. int *got_frame, AVPacket *avpkt)
  4120. {
  4121. const uint8_t *buf = avpkt->data;
  4122. int buf_size = avpkt->size;
  4123. H264Context *h = avctx->priv_data;
  4124. AVFrame *pict = data;
  4125. int buf_index = 0;
  4126. int ret;
  4127. h->flags = avctx->flags;
  4128. /* end of stream, output what is still in the buffers */
  4129. out:
  4130. if (buf_size == 0) {
  4131. Picture *out;
  4132. int i, out_idx;
  4133. h->cur_pic_ptr = NULL;
  4134. // FIXME factorize this with the output code below
  4135. out = h->delayed_pic[0];
  4136. out_idx = 0;
  4137. for (i = 1;
  4138. h->delayed_pic[i] &&
  4139. !h->delayed_pic[i]->f.key_frame &&
  4140. !h->delayed_pic[i]->mmco_reset;
  4141. i++)
  4142. if (h->delayed_pic[i]->poc < out->poc) {
  4143. out = h->delayed_pic[i];
  4144. out_idx = i;
  4145. }
  4146. for (i = out_idx; h->delayed_pic[i]; i++)
  4147. h->delayed_pic[i] = h->delayed_pic[i + 1];
  4148. if (out) {
  4149. ret = output_frame(h, pict, &out->f);
  4150. if (ret < 0)
  4151. return ret;
  4152. *got_frame = 1;
  4153. }
  4154. return buf_index;
  4155. }
  4156. buf_index = decode_nal_units(h, buf, buf_size, 0);
  4157. if (buf_index < 0)
  4158. return -1;
  4159. if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  4160. buf_size = 0;
  4161. goto out;
  4162. }
  4163. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
  4164. if (avctx->skip_frame >= AVDISCARD_NONREF)
  4165. return 0;
  4166. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  4167. return -1;
  4168. }
  4169. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
  4170. (h->mb_y >= h->mb_height && h->mb_height)) {
  4171. if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
  4172. decode_postinit(h, 1);
  4173. field_end(h, 0);
  4174. if (!h->next_output_pic) {
  4175. /* Wait for second field. */
  4176. *got_frame = 0;
  4177. } else {
  4178. ret = output_frame(h, pict, &h->next_output_pic->f);
  4179. if (ret < 0)
  4180. return ret;
  4181. *got_frame = 1;
  4182. }
  4183. }
  4184. assert(pict->data[0] || !*got_frame);
  4185. return get_consumed_bytes(buf_index, buf_size);
  4186. }
  4187. av_cold void ff_h264_free_context(H264Context *h)
  4188. {
  4189. int i;
  4190. free_tables(h, 1); // FIXME cleanup init stuff perhaps
  4191. for (i = 0; i < MAX_SPS_COUNT; i++)
  4192. av_freep(h->sps_buffers + i);
  4193. for (i = 0; i < MAX_PPS_COUNT; i++)
  4194. av_freep(h->pps_buffers + i);
  4195. }
  4196. static av_cold int h264_decode_end(AVCodecContext *avctx)
  4197. {
  4198. H264Context *h = avctx->priv_data;
  4199. ff_h264_free_context(h);
  4200. unref_picture(h, &h->cur_pic);
  4201. return 0;
  4202. }
  4203. static const AVProfile profiles[] = {
  4204. { FF_PROFILE_H264_BASELINE, "Baseline" },
  4205. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  4206. { FF_PROFILE_H264_MAIN, "Main" },
  4207. { FF_PROFILE_H264_EXTENDED, "Extended" },
  4208. { FF_PROFILE_H264_HIGH, "High" },
  4209. { FF_PROFILE_H264_HIGH_10, "High 10" },
  4210. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  4211. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  4212. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  4213. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  4214. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  4215. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  4216. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  4217. { FF_PROFILE_UNKNOWN },
  4218. };
  4219. AVCodec ff_h264_decoder = {
  4220. .name = "h264",
  4221. .type = AVMEDIA_TYPE_VIDEO,
  4222. .id = AV_CODEC_ID_H264,
  4223. .priv_data_size = sizeof(H264Context),
  4224. .init = ff_h264_decode_init,
  4225. .close = h264_decode_end,
  4226. .decode = decode_frame,
  4227. .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
  4228. CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
  4229. CODEC_CAP_FRAME_THREADS,
  4230. .flush = flush_dpb,
  4231. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  4232. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  4233. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  4234. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  4235. };
  4236. #if CONFIG_H264_VDPAU_DECODER
  4237. AVCodec ff_h264_vdpau_decoder = {
  4238. .name = "h264_vdpau",
  4239. .type = AVMEDIA_TYPE_VIDEO,
  4240. .id = AV_CODEC_ID_H264,
  4241. .priv_data_size = sizeof(H264Context),
  4242. .init = ff_h264_decode_init,
  4243. .close = h264_decode_end,
  4244. .decode = decode_frame,
  4245. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  4246. .flush = flush_dpb,
  4247. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  4248. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
  4249. AV_PIX_FMT_NONE},
  4250. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  4251. };
  4252. #endif