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.

5215 lines
195KB

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