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.

5275 lines
197KB

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