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.

2593 lines
94KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG-4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/display.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/stereo3d.h"
  30. #include "libavutil/timer.h"
  31. #include "internal.h"
  32. #include "cabac.h"
  33. #include "cabac_functions.h"
  34. #include "error_resilience.h"
  35. #include "golomb_legacy.h"
  36. #include "avcodec.h"
  37. #include "h264.h"
  38. #include "h264dec.h"
  39. #include "h264data.h"
  40. #include "h264chroma.h"
  41. #include "h264_mvpred.h"
  42. #include "h264_ps.h"
  43. #include "mathops.h"
  44. #include "mpegutils.h"
  45. #include "rectangle.h"
  46. #include "thread.h"
  47. static const uint8_t field_scan[16] = {
  48. 0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
  49. 0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
  50. 2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
  51. 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
  52. };
  53. static const uint8_t field_scan8x8[64] = {
  54. 0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
  55. 1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
  56. 2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
  57. 0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
  58. 2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
  59. 2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
  60. 2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
  61. 3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
  62. 3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
  63. 4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
  64. 4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
  65. 5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
  66. 5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
  67. 7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
  68. 6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
  69. 7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
  70. };
  71. static const uint8_t field_scan8x8_cavlc[64] = {
  72. 0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
  73. 2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
  74. 3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
  75. 5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
  76. 0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
  77. 1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
  78. 3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
  79. 5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
  80. 0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
  81. 1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
  82. 3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
  83. 5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
  84. 1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
  85. 1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
  86. 3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
  87. 6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
  88. };
  89. // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
  90. static const uint8_t zigzag_scan8x8_cavlc[64] = {
  91. 0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
  92. 4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
  93. 3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
  94. 2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
  95. 1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
  96. 3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
  97. 2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
  98. 3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
  99. 0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
  100. 2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
  101. 1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
  102. 4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
  103. 0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
  104. 1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
  105. 0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
  106. 5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
  107. };
  108. static void release_unused_pictures(H264Context *h, int remove_current)
  109. {
  110. int i;
  111. /* release non reference frames */
  112. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  113. if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
  114. (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
  115. ff_h264_unref_picture(h, &h->DPB[i]);
  116. }
  117. }
  118. }
  119. static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
  120. {
  121. const H264Context *h = sl->h264;
  122. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  123. av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
  124. // edge emu needs blocksize + filter length - 1
  125. // (= 21x21 for H.264)
  126. av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
  127. av_fast_malloc(&sl->top_borders[0], &sl->top_borders_allocated[0],
  128. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
  129. av_fast_malloc(&sl->top_borders[1], &sl->top_borders_allocated[1],
  130. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
  131. if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
  132. !sl->top_borders[0] || !sl->top_borders[1]) {
  133. av_freep(&sl->bipred_scratchpad);
  134. av_freep(&sl->edge_emu_buffer);
  135. av_freep(&sl->top_borders[0]);
  136. av_freep(&sl->top_borders[1]);
  137. sl->bipred_scratchpad_allocated = 0;
  138. sl->edge_emu_buffer_allocated = 0;
  139. sl->top_borders_allocated[0] = 0;
  140. sl->top_borders_allocated[1] = 0;
  141. return AVERROR(ENOMEM);
  142. }
  143. return 0;
  144. }
  145. static int init_table_pools(H264Context *h)
  146. {
  147. const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
  148. const int mb_array_size = h->mb_stride * h->mb_height;
  149. const int b4_stride = h->mb_width * 4 + 1;
  150. const int b4_array_size = b4_stride * h->mb_height * 4;
  151. h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
  152. av_buffer_allocz);
  153. h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
  154. sizeof(uint32_t), av_buffer_allocz);
  155. h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
  156. sizeof(int16_t), av_buffer_allocz);
  157. h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
  158. if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
  159. !h->ref_index_pool) {
  160. av_buffer_pool_uninit(&h->qscale_table_pool);
  161. av_buffer_pool_uninit(&h->mb_type_pool);
  162. av_buffer_pool_uninit(&h->motion_val_pool);
  163. av_buffer_pool_uninit(&h->ref_index_pool);
  164. return AVERROR(ENOMEM);
  165. }
  166. return 0;
  167. }
  168. static int alloc_picture(H264Context *h, H264Picture *pic)
  169. {
  170. int i, ret = 0;
  171. av_assert0(!pic->f->data[0]);
  172. pic->tf.f = pic->f;
  173. ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
  174. AV_GET_BUFFER_FLAG_REF : 0);
  175. if (ret < 0)
  176. goto fail;
  177. if (h->avctx->hwaccel) {
  178. const AVHWAccel *hwaccel = h->avctx->hwaccel;
  179. av_assert0(!pic->hwaccel_picture_private);
  180. if (hwaccel->frame_priv_data_size) {
  181. pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
  182. if (!pic->hwaccel_priv_buf)
  183. return AVERROR(ENOMEM);
  184. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  185. }
  186. }
  187. if (!h->qscale_table_pool) {
  188. ret = init_table_pools(h);
  189. if (ret < 0)
  190. goto fail;
  191. }
  192. pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
  193. pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
  194. if (!pic->qscale_table_buf || !pic->mb_type_buf)
  195. goto fail;
  196. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
  197. pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
  198. for (i = 0; i < 2; i++) {
  199. pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
  200. pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
  201. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  202. goto fail;
  203. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  204. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  205. }
  206. return 0;
  207. fail:
  208. ff_h264_unref_picture(h, pic);
  209. return (ret < 0) ? ret : AVERROR(ENOMEM);
  210. }
  211. static inline int pic_is_unused(H264Context *h, H264Picture *pic)
  212. {
  213. if (!pic->f->buf[0])
  214. return 1;
  215. return 0;
  216. }
  217. static int find_unused_picture(H264Context *h)
  218. {
  219. int i;
  220. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  221. if (pic_is_unused(h, &h->DPB[i]))
  222. break;
  223. }
  224. if (i == H264_MAX_PICTURE_COUNT)
  225. return AVERROR_INVALIDDATA;
  226. return i;
  227. }
  228. static int initialize_cur_frame(H264Context *h)
  229. {
  230. H264Picture *cur;
  231. int ret;
  232. release_unused_pictures(h, 1);
  233. ff_h264_unref_picture(h, &h->cur_pic);
  234. h->cur_pic_ptr = NULL;
  235. ret = find_unused_picture(h);
  236. if (ret < 0) {
  237. av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  238. return ret;
  239. }
  240. cur = &h->DPB[ret];
  241. ret = alloc_picture(h, cur);
  242. if (ret < 0)
  243. return ret;
  244. ret = ff_h264_ref_picture(h, &h->cur_pic, cur);
  245. if (ret < 0)
  246. return ret;
  247. h->cur_pic_ptr = cur;
  248. return 0;
  249. }
  250. #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
  251. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  252. ((pic && pic >= old_ctx->DPB && \
  253. pic < old_ctx->DPB + H264_MAX_PICTURE_COUNT) ? \
  254. &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
  255. static void copy_picture_range(H264Picture **to, H264Picture **from, int count,
  256. H264Context *new_base,
  257. H264Context *old_base)
  258. {
  259. int i;
  260. for (i = 0; i < count; i++) {
  261. assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
  262. IN_RANGE(from[i], old_base->DPB,
  263. sizeof(H264Picture) * H264_MAX_PICTURE_COUNT) ||
  264. !from[i]));
  265. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  266. }
  267. }
  268. static int h264_slice_header_init(H264Context *h);
  269. int ff_h264_update_thread_context(AVCodecContext *dst,
  270. const AVCodecContext *src)
  271. {
  272. H264Context *h = dst->priv_data, *h1 = src->priv_data;
  273. int inited = h->context_initialized, err = 0;
  274. int need_reinit = 0;
  275. int i, ret;
  276. if (dst == src || !h1->context_initialized)
  277. return 0;
  278. if (!h1->ps.sps)
  279. return AVERROR_INVALIDDATA;
  280. if (inited &&
  281. (h->width != h1->width ||
  282. h->height != h1->height ||
  283. h->mb_width != h1->mb_width ||
  284. h->mb_height != h1->mb_height ||
  285. !h->ps.sps ||
  286. h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
  287. h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
  288. h->ps.sps->colorspace != h1->ps.sps->colorspace)) {
  289. need_reinit = 1;
  290. }
  291. // SPS/PPS
  292. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++) {
  293. av_buffer_unref(&h->ps.sps_list[i]);
  294. if (h1->ps.sps_list[i]) {
  295. h->ps.sps_list[i] = av_buffer_ref(h1->ps.sps_list[i]);
  296. if (!h->ps.sps_list[i])
  297. return AVERROR(ENOMEM);
  298. }
  299. }
  300. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
  301. av_buffer_unref(&h->ps.pps_list[i]);
  302. if (h1->ps.pps_list[i]) {
  303. h->ps.pps_list[i] = av_buffer_ref(h1->ps.pps_list[i]);
  304. if (!h->ps.pps_list[i])
  305. return AVERROR(ENOMEM);
  306. }
  307. }
  308. h->ps.sps = h1->ps.sps;
  309. if (need_reinit || !inited) {
  310. h->width = h1->width;
  311. h->height = h1->height;
  312. h->mb_height = h1->mb_height;
  313. h->mb_width = h1->mb_width;
  314. h->mb_num = h1->mb_num;
  315. h->mb_stride = h1->mb_stride;
  316. h->b_stride = h1->b_stride;
  317. if ((err = h264_slice_header_init(h)) < 0) {
  318. av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
  319. return err;
  320. }
  321. /* copy block_offset since frame_start may not be called */
  322. memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
  323. }
  324. h->avctx->coded_height = h1->avctx->coded_height;
  325. h->avctx->coded_width = h1->avctx->coded_width;
  326. h->avctx->width = h1->avctx->width;
  327. h->avctx->height = h1->avctx->height;
  328. h->width_from_caller = h1->width_from_caller;
  329. h->height_from_caller = h1->height_from_caller;
  330. h->coded_picture_number = h1->coded_picture_number;
  331. h->first_field = h1->first_field;
  332. h->picture_structure = h1->picture_structure;
  333. h->mb_aff_frame = h1->mb_aff_frame;
  334. h->droppable = h1->droppable;
  335. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  336. ff_h264_unref_picture(h, &h->DPB[i]);
  337. if (h1->DPB[i].f->buf[0] &&
  338. (ret = ff_h264_ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
  339. return ret;
  340. }
  341. h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
  342. ff_h264_unref_picture(h, &h->cur_pic);
  343. if (h1->cur_pic.f->buf[0]) {
  344. ret = ff_h264_ref_picture(h, &h->cur_pic, &h1->cur_pic);
  345. if (ret < 0)
  346. return ret;
  347. }
  348. h->enable_er = h1->enable_er;
  349. h->workaround_bugs = h1->workaround_bugs;
  350. h->x264_build = h1->x264_build;
  351. h->droppable = h1->droppable;
  352. // extradata/NAL handling
  353. h->is_avc = h1->is_avc;
  354. h->nal_length_size = h1->nal_length_size;
  355. memcpy(&h->poc, &h1->poc, sizeof(h->poc));
  356. memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
  357. memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
  358. memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
  359. memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
  360. h->next_outputed_poc = h1->next_outputed_poc;
  361. memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
  362. h->nb_mmco = h1->nb_mmco;
  363. h->mmco_reset = h1->mmco_reset;
  364. h->explicit_ref_marking = h1->explicit_ref_marking;
  365. h->long_ref_count = h1->long_ref_count;
  366. h->short_ref_count = h1->short_ref_count;
  367. copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
  368. copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
  369. copy_picture_range(h->delayed_pic, h1->delayed_pic,
  370. MAX_DELAYED_PIC_COUNT + 2, h, h1);
  371. if (!h->cur_pic_ptr)
  372. return 0;
  373. if (!h->droppable) {
  374. err = ff_h264_execute_ref_pic_marking(h);
  375. h->poc.prev_poc_msb = h->poc.poc_msb;
  376. h->poc.prev_poc_lsb = h->poc.poc_lsb;
  377. }
  378. h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
  379. h->poc.prev_frame_num = h->poc.frame_num;
  380. h->recovery_frame = h1->recovery_frame;
  381. h->frame_recovered = h1->frame_recovered;
  382. return err;
  383. }
  384. static int h264_frame_start(H264Context *h)
  385. {
  386. H264Picture *pic;
  387. int i, ret;
  388. const int pixel_shift = h->pixel_shift;
  389. ret = initialize_cur_frame(h);
  390. if (ret < 0)
  391. return ret;
  392. pic = h->cur_pic_ptr;
  393. pic->reference = h->droppable ? 0 : h->picture_structure;
  394. pic->f->coded_picture_number = h->coded_picture_number++;
  395. pic->field_picture = h->picture_structure != PICT_FRAME;
  396. pic->frame_num = h->poc.frame_num;
  397. /*
  398. * Zero key_frame here; IDR markings per slice in frame or fields are ORed
  399. * in later.
  400. * See decode_nal_units().
  401. */
  402. pic->f->key_frame = 0;
  403. pic->mmco_reset = 0;
  404. pic->recovered = 0;
  405. pic->f->pict_type = h->slice_ctx[0].slice_type;
  406. pic->f->crop_left = h->crop_left;
  407. pic->f->crop_right = h->crop_right;
  408. pic->f->crop_top = h->crop_top;
  409. pic->f->crop_bottom = h->crop_bottom;
  410. if (CONFIG_ERROR_RESILIENCE && h->enable_er)
  411. ff_er_frame_start(&h->slice_ctx[0].er);
  412. for (i = 0; i < 16; i++) {
  413. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
  414. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
  415. }
  416. for (i = 0; i < 16; i++) {
  417. h->block_offset[16 + i] =
  418. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
  419. h->block_offset[48 + 16 + i] =
  420. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
  421. }
  422. /* Some macroblocks can be accessed before they're available in case
  423. * of lost slices, MBAFF or threading. */
  424. memset(h->slice_table, -1,
  425. (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
  426. /* We mark the current picture as non-reference after allocating it, so
  427. * that if we break out due to an error it can be released automatically
  428. * in the next ff_mpv_frame_start().
  429. */
  430. h->cur_pic_ptr->reference = 0;
  431. h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
  432. h->postpone_filter = 0;
  433. h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
  434. if (h->sei.unregistered.x264_build >= 0)
  435. h->x264_build = h->sei.unregistered.x264_build;
  436. assert(h->cur_pic_ptr->long_ref == 0);
  437. return 0;
  438. }
  439. static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
  440. uint8_t *src_y,
  441. uint8_t *src_cb, uint8_t *src_cr,
  442. int linesize, int uvlinesize,
  443. int simple)
  444. {
  445. uint8_t *top_border;
  446. int top_idx = 1;
  447. const int pixel_shift = h->pixel_shift;
  448. int chroma444 = CHROMA444(h);
  449. int chroma422 = CHROMA422(h);
  450. src_y -= linesize;
  451. src_cb -= uvlinesize;
  452. src_cr -= uvlinesize;
  453. if (!simple && FRAME_MBAFF(h)) {
  454. if (sl->mb_y & 1) {
  455. if (!MB_MBAFF(sl)) {
  456. top_border = sl->top_borders[0][sl->mb_x];
  457. AV_COPY128(top_border, src_y + 15 * linesize);
  458. if (pixel_shift)
  459. AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
  460. if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  461. if (chroma444) {
  462. if (pixel_shift) {
  463. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  464. AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
  465. AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
  466. AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
  467. } else {
  468. AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
  469. AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
  470. }
  471. } else if (chroma422) {
  472. if (pixel_shift) {
  473. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  474. AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
  475. } else {
  476. AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
  477. AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
  478. }
  479. } else {
  480. if (pixel_shift) {
  481. AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
  482. AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
  483. } else {
  484. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  485. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  486. }
  487. }
  488. }
  489. }
  490. } else if (MB_MBAFF(sl)) {
  491. top_idx = 0;
  492. } else
  493. return;
  494. }
  495. top_border = sl->top_borders[top_idx][sl->mb_x];
  496. /* There are two lines saved, the line above the top macroblock
  497. * of a pair, and the line above the bottom macroblock. */
  498. AV_COPY128(top_border, src_y + 16 * linesize);
  499. if (pixel_shift)
  500. AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
  501. if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  502. if (chroma444) {
  503. if (pixel_shift) {
  504. AV_COPY128(top_border + 32, src_cb + 16 * linesize);
  505. AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
  506. AV_COPY128(top_border + 64, src_cr + 16 * linesize);
  507. AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
  508. } else {
  509. AV_COPY128(top_border + 16, src_cb + 16 * linesize);
  510. AV_COPY128(top_border + 32, src_cr + 16 * linesize);
  511. }
  512. } else if (chroma422) {
  513. if (pixel_shift) {
  514. AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
  515. AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
  516. } else {
  517. AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
  518. AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
  519. }
  520. } else {
  521. if (pixel_shift) {
  522. AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
  523. AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
  524. } else {
  525. AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
  526. AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
  527. }
  528. }
  529. }
  530. }
  531. /**
  532. * Initialize implicit_weight table.
  533. * @param field 0/1 initialize the weight for interlaced MBAFF
  534. * -1 initializes the rest
  535. */
  536. static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
  537. {
  538. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  539. for (i = 0; i < 2; i++) {
  540. sl->pwt.luma_weight_flag[i] = 0;
  541. sl->pwt.chroma_weight_flag[i] = 0;
  542. }
  543. if (field < 0) {
  544. if (h->picture_structure == PICT_FRAME) {
  545. cur_poc = h->cur_pic_ptr->poc;
  546. } else {
  547. cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
  548. }
  549. if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
  550. sl->ref_list[0][0].poc + sl->ref_list[1][0].poc == 2 * cur_poc) {
  551. sl->pwt.use_weight = 0;
  552. sl->pwt.use_weight_chroma = 0;
  553. return;
  554. }
  555. ref_start = 0;
  556. ref_count0 = sl->ref_count[0];
  557. ref_count1 = sl->ref_count[1];
  558. } else {
  559. cur_poc = h->cur_pic_ptr->field_poc[field];
  560. ref_start = 16;
  561. ref_count0 = 16 + 2 * sl->ref_count[0];
  562. ref_count1 = 16 + 2 * sl->ref_count[1];
  563. }
  564. sl->pwt.use_weight = 2;
  565. sl->pwt.use_weight_chroma = 2;
  566. sl->pwt.luma_log2_weight_denom = 5;
  567. sl->pwt.chroma_log2_weight_denom = 5;
  568. for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
  569. int poc0 = sl->ref_list[0][ref0].poc;
  570. for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
  571. int w = 32;
  572. if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
  573. int poc1 = sl->ref_list[1][ref1].poc;
  574. int td = av_clip_int8(poc1 - poc0);
  575. if (td) {
  576. int tb = av_clip_int8(cur_poc - poc0);
  577. int tx = (16384 + (FFABS(td) >> 1)) / td;
  578. int dist_scale_factor = (tb * tx + 32) >> 8;
  579. if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
  580. w = 64 - dist_scale_factor;
  581. }
  582. }
  583. if (field < 0) {
  584. sl->pwt.implicit_weight[ref0][ref1][0] =
  585. sl->pwt.implicit_weight[ref0][ref1][1] = w;
  586. } else {
  587. sl->pwt.implicit_weight[ref0][ref1][field] = w;
  588. }
  589. }
  590. }
  591. }
  592. /**
  593. * initialize scan tables
  594. */
  595. static void init_scan_tables(H264Context *h)
  596. {
  597. int i;
  598. for (i = 0; i < 16; i++) {
  599. #define TRANSPOSE(x) (x >> 2) | ((x << 2) & 0xF)
  600. h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
  601. h->field_scan[i] = TRANSPOSE(field_scan[i]);
  602. #undef TRANSPOSE
  603. }
  604. for (i = 0; i < 64; i++) {
  605. #define TRANSPOSE(x) (x >> 3) | ((x & 7) << 3)
  606. h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
  607. h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
  608. h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
  609. h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
  610. #undef TRANSPOSE
  611. }
  612. if (h->ps.sps->transform_bypass) { // FIXME same ugly
  613. h->zigzag_scan_q0 = ff_zigzag_scan;
  614. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  615. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  616. h->field_scan_q0 = field_scan;
  617. h->field_scan8x8_q0 = field_scan8x8;
  618. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  619. } else {
  620. h->zigzag_scan_q0 = h->zigzag_scan;
  621. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  622. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  623. h->field_scan_q0 = h->field_scan;
  624. h->field_scan8x8_q0 = h->field_scan8x8;
  625. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  626. }
  627. }
  628. static enum AVPixelFormat get_pixel_format(H264Context *h)
  629. {
  630. #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
  631. (CONFIG_H264_D3D11VA_HWACCEL * 2) + \
  632. CONFIG_H264_VAAPI_HWACCEL + \
  633. (CONFIG_H264_VDA_HWACCEL * 2) + \
  634. CONFIG_H264_VDPAU_HWACCEL + \
  635. CONFIG_H264_CUVID_HWACCEL)
  636. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
  637. const enum AVPixelFormat *choices = pix_fmts;
  638. switch (h->ps.sps->bit_depth_luma) {
  639. case 9:
  640. if (CHROMA444(h)) {
  641. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  642. *fmt++ = AV_PIX_FMT_GBRP9;
  643. } else
  644. *fmt++ = AV_PIX_FMT_YUV444P9;
  645. } else if (CHROMA422(h))
  646. *fmt++ = AV_PIX_FMT_YUV422P9;
  647. else
  648. *fmt++ = AV_PIX_FMT_YUV420P9;
  649. break;
  650. case 10:
  651. if (CHROMA444(h)) {
  652. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  653. *fmt++ = AV_PIX_FMT_GBRP10;
  654. } else
  655. *fmt++ = AV_PIX_FMT_YUV444P10;
  656. } else if (CHROMA422(h))
  657. *fmt++ = AV_PIX_FMT_YUV422P10;
  658. else
  659. *fmt++ = AV_PIX_FMT_YUV420P10;
  660. break;
  661. case 8:
  662. #if CONFIG_H264_VDPAU_HWACCEL
  663. *fmt++ = AV_PIX_FMT_VDPAU;
  664. #endif
  665. #if CONFIG_H264_CUVID_HWACCEL
  666. *fmt++ = AV_PIX_FMT_CUDA;
  667. #endif
  668. if (CHROMA444(h)) {
  669. if (h->avctx->colorspace == AVCOL_SPC_RGB)
  670. *fmt++ = AV_PIX_FMT_GBRP;
  671. else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
  672. *fmt++ = AV_PIX_FMT_YUVJ444P;
  673. else
  674. *fmt++ = AV_PIX_FMT_YUV444P;
  675. } else if (CHROMA422(h)) {
  676. if (h->avctx->color_range == AVCOL_RANGE_JPEG)
  677. *fmt++ = AV_PIX_FMT_YUVJ422P;
  678. else
  679. *fmt++ = AV_PIX_FMT_YUV422P;
  680. } else {
  681. #if CONFIG_H264_DXVA2_HWACCEL
  682. *fmt++ = AV_PIX_FMT_DXVA2_VLD;
  683. #endif
  684. #if CONFIG_H264_D3D11VA_HWACCEL
  685. *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
  686. *fmt++ = AV_PIX_FMT_D3D11;
  687. #endif
  688. #if CONFIG_H264_VAAPI_HWACCEL
  689. *fmt++ = AV_PIX_FMT_VAAPI;
  690. #endif
  691. #if CONFIG_H264_VDA_HWACCEL
  692. *fmt++ = AV_PIX_FMT_VDA_VLD;
  693. *fmt++ = AV_PIX_FMT_VDA;
  694. #endif
  695. if (h->avctx->codec->pix_fmts)
  696. choices = h->avctx->codec->pix_fmts;
  697. else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
  698. *fmt++ = AV_PIX_FMT_YUVJ420P;
  699. else
  700. *fmt++ = AV_PIX_FMT_YUV420P;
  701. }
  702. break;
  703. default:
  704. av_log(h->avctx, AV_LOG_ERROR,
  705. "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
  706. return AVERROR_INVALIDDATA;
  707. }
  708. *fmt = AV_PIX_FMT_NONE;
  709. return ff_get_format(h->avctx, choices);
  710. }
  711. /* export coded and cropped frame dimensions to AVCodecContext */
  712. static int init_dimensions(H264Context *h)
  713. {
  714. SPS *sps = h->ps.sps;
  715. int cr = sps->crop_right;
  716. int cl = sps->crop_left;
  717. int ct = sps->crop_top;
  718. int cb = sps->crop_bottom;
  719. int width = h->width - (cr + cl);
  720. int height = h->height - (ct + cb);
  721. /* handle container cropping */
  722. if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
  723. !sps->crop_top && !sps->crop_left &&
  724. FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
  725. FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16)) {
  726. width = h->width_from_caller;
  727. height = h->height_from_caller;
  728. cl = 0;
  729. ct = 0;
  730. cr = h->width - width;
  731. cb = h->height - height;
  732. } else {
  733. h->width_from_caller = 0;
  734. h->height_from_caller = 0;
  735. }
  736. h->avctx->coded_width = h->width;
  737. h->avctx->coded_height = h->height;
  738. h->avctx->width = width;
  739. h->avctx->height = height;
  740. h->crop_right = cr;
  741. h->crop_left = cl;
  742. h->crop_top = ct;
  743. h->crop_bottom = cb;
  744. return 0;
  745. }
  746. static int h264_slice_header_init(H264Context *h)
  747. {
  748. const SPS *sps = h->ps.sps;
  749. int i, ret;
  750. ff_set_sar(h->avctx, sps->sar);
  751. av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
  752. &h->chroma_x_shift, &h->chroma_y_shift);
  753. if (sps->timing_info_present_flag) {
  754. int64_t den = sps->time_scale;
  755. if (h->x264_build < 44U)
  756. den *= 2;
  757. av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
  758. sps->num_units_in_tick, den, 1 << 30);
  759. }
  760. ff_h264_free_tables(h);
  761. h->first_field = 0;
  762. h->prev_interlaced_frame = 1;
  763. init_scan_tables(h);
  764. ret = ff_h264_alloc_tables(h);
  765. if (ret < 0) {
  766. av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
  767. return ret;
  768. }
  769. if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 10) {
  770. av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
  771. sps->bit_depth_luma);
  772. return AVERROR_INVALIDDATA;
  773. }
  774. h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
  775. h->pixel_shift = sps->bit_depth_luma > 8;
  776. h->chroma_format_idc = sps->chroma_format_idc;
  777. h->bit_depth_luma = sps->bit_depth_luma;
  778. ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
  779. sps->chroma_format_idc);
  780. ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
  781. ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
  782. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, sps->bit_depth_luma,
  783. sps->chroma_format_idc);
  784. ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
  785. if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
  786. ret = ff_h264_slice_context_init(h, &h->slice_ctx[0]);
  787. if (ret < 0) {
  788. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  789. return ret;
  790. }
  791. } else {
  792. for (i = 0; i < h->nb_slice_ctx; i++) {
  793. H264SliceContext *sl = &h->slice_ctx[i];
  794. sl->h264 = h;
  795. sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
  796. sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
  797. sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
  798. if ((ret = ff_h264_slice_context_init(h, sl)) < 0) {
  799. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  800. return ret;
  801. }
  802. }
  803. }
  804. h->context_initialized = 1;
  805. return 0;
  806. }
  807. static int h264_init_ps(H264Context *h, const H264SliceContext *sl)
  808. {
  809. const SPS *sps;
  810. int needs_reinit = 0, ret;
  811. h->ps.pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
  812. if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
  813. h->ps.sps = (SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data;
  814. if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
  815. h->chroma_format_idc != h->ps.sps->chroma_format_idc)
  816. needs_reinit = 1;
  817. }
  818. sps = h->ps.sps;
  819. h->avctx->profile = ff_h264_get_profile(sps);
  820. h->avctx->level = sps->level_idc;
  821. h->avctx->refs = sps->ref_frame_count;
  822. if (h->mb_width != sps->mb_width ||
  823. h->mb_height != sps->mb_height)
  824. needs_reinit = 1;
  825. h->mb_width = sps->mb_width;
  826. h->mb_height = sps->mb_height;
  827. h->mb_num = h->mb_width * h->mb_height;
  828. h->mb_stride = h->mb_width + 1;
  829. h->b_stride = h->mb_width * 4;
  830. h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
  831. h->width = 16 * h->mb_width;
  832. h->height = 16 * h->mb_height;
  833. ret = init_dimensions(h);
  834. if (ret < 0)
  835. return ret;
  836. if (sps->video_signal_type_present_flag) {
  837. h->avctx->color_range = sps->full_range ? AVCOL_RANGE_JPEG
  838. : AVCOL_RANGE_MPEG;
  839. if (sps->colour_description_present_flag) {
  840. if (h->avctx->colorspace != sps->colorspace)
  841. needs_reinit = 1;
  842. h->avctx->color_primaries = sps->color_primaries;
  843. h->avctx->color_trc = sps->color_trc;
  844. h->avctx->colorspace = sps->colorspace;
  845. }
  846. }
  847. if (!h->context_initialized || needs_reinit) {
  848. h->context_initialized = 0;
  849. if (sl != h->slice_ctx) {
  850. av_log(h->avctx, AV_LOG_ERROR,
  851. "changing width %d -> %d / height %d -> %d on "
  852. "slice %d\n",
  853. h->width, h->avctx->coded_width,
  854. h->height, h->avctx->coded_height,
  855. h->current_slice + 1);
  856. return AVERROR_INVALIDDATA;
  857. }
  858. ff_h264_flush_change(h);
  859. if ((ret = get_pixel_format(h)) < 0)
  860. return ret;
  861. h->avctx->pix_fmt = ret;
  862. av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
  863. "pix_fmt: %d\n", h->width, h->height, h->avctx->pix_fmt);
  864. if ((ret = h264_slice_header_init(h)) < 0) {
  865. av_log(h->avctx, AV_LOG_ERROR,
  866. "h264_slice_header_init() failed\n");
  867. return ret;
  868. }
  869. }
  870. return 0;
  871. }
  872. static int h264_export_frame_props(H264Context *h)
  873. {
  874. const SPS *sps = h->ps.sps;
  875. H264Picture *cur = h->cur_pic_ptr;
  876. cur->f->interlaced_frame = 0;
  877. cur->f->repeat_pict = 0;
  878. /* Signal interlacing information externally. */
  879. /* Prioritize picture timing SEI information over used
  880. * decoding process if it exists. */
  881. if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
  882. H264SEIPictureTiming *pt = &h->sei.picture_timing;
  883. switch (pt->pic_struct) {
  884. case H264_SEI_PIC_STRUCT_FRAME:
  885. break;
  886. case H264_SEI_PIC_STRUCT_TOP_FIELD:
  887. case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
  888. cur->f->interlaced_frame = 1;
  889. break;
  890. case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
  891. case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
  892. if (FIELD_OR_MBAFF_PICTURE(h))
  893. cur->f->interlaced_frame = 1;
  894. else
  895. // try to flag soft telecine progressive
  896. cur->f->interlaced_frame = h->prev_interlaced_frame;
  897. break;
  898. case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  899. case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  900. /* Signal the possibility of telecined film externally
  901. * (pic_struct 5,6). From these hints, let the applications
  902. * decide if they apply deinterlacing. */
  903. cur->f->repeat_pict = 1;
  904. break;
  905. case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
  906. cur->f->repeat_pict = 2;
  907. break;
  908. case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
  909. cur->f->repeat_pict = 4;
  910. break;
  911. }
  912. if ((pt->ct_type & 3) &&
  913. pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
  914. cur->f->interlaced_frame = (pt->ct_type & (1 << 1)) != 0;
  915. } else {
  916. /* Derive interlacing flag from used decoding process. */
  917. cur->f->interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
  918. }
  919. h->prev_interlaced_frame = cur->f->interlaced_frame;
  920. if (cur->field_poc[0] != cur->field_poc[1]) {
  921. /* Derive top_field_first from field pocs. */
  922. cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  923. } else {
  924. if (cur->f->interlaced_frame ||
  925. (sps->pic_struct_present_flag && h->sei.picture_timing.present)) {
  926. /* Use picture timing SEI information. Even if it is a
  927. * information of a past frame, better than nothing. */
  928. if (h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM ||
  929. h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  930. cur->f->top_field_first = 1;
  931. else
  932. cur->f->top_field_first = 0;
  933. } else {
  934. /* Most likely progressive */
  935. cur->f->top_field_first = 0;
  936. }
  937. }
  938. if (h->sei.frame_packing.present &&
  939. h->sei.frame_packing.arrangement_type >= 0 &&
  940. h->sei.frame_packing.arrangement_type <= 6 &&
  941. h->sei.frame_packing.content_interpretation_type > 0 &&
  942. h->sei.frame_packing.content_interpretation_type < 3) {
  943. H264SEIFramePacking *fp = &h->sei.frame_packing;
  944. AVStereo3D *stereo = av_stereo3d_create_side_data(cur->f);
  945. if (!stereo)
  946. return AVERROR(ENOMEM);
  947. switch (fp->arrangement_type) {
  948. case 0:
  949. stereo->type = AV_STEREO3D_CHECKERBOARD;
  950. break;
  951. case 1:
  952. stereo->type = AV_STEREO3D_COLUMNS;
  953. break;
  954. case 2:
  955. stereo->type = AV_STEREO3D_LINES;
  956. break;
  957. case 3:
  958. if (fp->quincunx_subsampling)
  959. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  960. else
  961. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  962. break;
  963. case 4:
  964. stereo->type = AV_STEREO3D_TOPBOTTOM;
  965. break;
  966. case 5:
  967. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  968. break;
  969. case 6:
  970. stereo->type = AV_STEREO3D_2D;
  971. break;
  972. }
  973. if (fp->content_interpretation_type == 2)
  974. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  975. if (fp->arrangement_type == 5) {
  976. if (fp->current_frame_is_frame0_flag)
  977. stereo->view = AV_STEREO3D_VIEW_LEFT;
  978. else
  979. stereo->view = AV_STEREO3D_VIEW_RIGHT;
  980. }
  981. }
  982. if (h->sei.display_orientation.present &&
  983. (h->sei.display_orientation.anticlockwise_rotation ||
  984. h->sei.display_orientation.hflip ||
  985. h->sei.display_orientation.vflip)) {
  986. H264SEIDisplayOrientation *o = &h->sei.display_orientation;
  987. double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
  988. AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
  989. AV_FRAME_DATA_DISPLAYMATRIX,
  990. sizeof(int32_t) * 9);
  991. if (!rotation)
  992. return AVERROR(ENOMEM);
  993. av_display_rotation_set((int32_t *)rotation->data, angle);
  994. av_display_matrix_flip((int32_t *)rotation->data,
  995. o->hflip, o->vflip);
  996. }
  997. if (h->sei.afd.present) {
  998. AVFrameSideData *sd = av_frame_new_side_data(cur->f, AV_FRAME_DATA_AFD,
  999. sizeof(uint8_t));
  1000. if (!sd)
  1001. return AVERROR(ENOMEM);
  1002. *sd->data = h->sei.afd.active_format_description;
  1003. h->sei.afd.present = 0;
  1004. }
  1005. if (h->sei.a53_caption.a53_caption) {
  1006. H264SEIA53Caption *a53 = &h->sei.a53_caption;
  1007. AVFrameSideData *sd = av_frame_new_side_data(cur->f,
  1008. AV_FRAME_DATA_A53_CC,
  1009. a53->a53_caption_size);
  1010. if (!sd)
  1011. return AVERROR(ENOMEM);
  1012. memcpy(sd->data, a53->a53_caption, a53->a53_caption_size);
  1013. av_freep(&a53->a53_caption);
  1014. a53->a53_caption_size = 0;
  1015. }
  1016. if (h->sei.alternative_transfer.present &&
  1017. av_color_transfer_name(h->sei.alternative_transfer.preferred_transfer_characteristics) &&
  1018. h->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
  1019. h->avctx->color_trc = cur->f->color_trc = h->sei.alternative_transfer.preferred_transfer_characteristics;
  1020. }
  1021. return 0;
  1022. }
  1023. static int h264_select_output_frame(H264Context *h)
  1024. {
  1025. const SPS *sps = h->ps.sps;
  1026. H264Picture *out = h->cur_pic_ptr;
  1027. H264Picture *cur = h->cur_pic_ptr;
  1028. int i, pics, out_of_order, out_idx;
  1029. int invalid = 0, cnt = 0;
  1030. int ret;
  1031. if (sps->bitstream_restriction_flag ||
  1032. h->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
  1033. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
  1034. }
  1035. pics = 0;
  1036. while (h->delayed_pic[pics])
  1037. pics++;
  1038. assert(pics <= MAX_DELAYED_PIC_COUNT);
  1039. h->delayed_pic[pics++] = cur;
  1040. if (cur->reference == 0)
  1041. cur->reference = DELAYED_PIC_REF;
  1042. /* Frame reordering. This code takes pictures from coding order and sorts
  1043. * them by their incremental POC value into display order. It supports POC
  1044. * gaps, MMCO reset codes and random resets.
  1045. * A "display group" can start either with a IDR frame (f.key_frame = 1),
  1046. * and/or can be closed down with a MMCO reset code. In sequences where
  1047. * there is no delay, we can't detect that (since the frame was already
  1048. * output to the user), so we also set h->mmco_reset to detect the MMCO
  1049. * reset code.
  1050. * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
  1051. * we increase the delay between input and output. All frames affected by
  1052. * the lag (e.g. those that should have been output before another frame
  1053. * that we already returned to the user) will be dropped. This is a bug
  1054. * that we will fix later. */
  1055. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
  1056. cnt += out->poc < h->last_pocs[i];
  1057. invalid += out->poc == INT_MIN;
  1058. }
  1059. if (!h->mmco_reset && !cur->f->key_frame &&
  1060. cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
  1061. h->mmco_reset = 2;
  1062. if (pics > 1)
  1063. h->delayed_pic[pics - 2]->mmco_reset = 2;
  1064. }
  1065. if (h->mmco_reset || cur->f->key_frame) {
  1066. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  1067. h->last_pocs[i] = INT_MIN;
  1068. cnt = 0;
  1069. invalid = MAX_DELAYED_PIC_COUNT;
  1070. }
  1071. out = h->delayed_pic[0];
  1072. out_idx = 0;
  1073. for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
  1074. h->delayed_pic[i] &&
  1075. !h->delayed_pic[i - 1]->mmco_reset &&
  1076. !h->delayed_pic[i]->f->key_frame;
  1077. i++)
  1078. if (h->delayed_pic[i]->poc < out->poc) {
  1079. out = h->delayed_pic[i];
  1080. out_idx = i;
  1081. }
  1082. if (h->avctx->has_b_frames == 0 &&
  1083. (h->delayed_pic[0]->f->key_frame || h->mmco_reset))
  1084. h->next_outputed_poc = INT_MIN;
  1085. out_of_order = !out->f->key_frame && !h->mmco_reset &&
  1086. (out->poc < h->next_outputed_poc);
  1087. if (sps->bitstream_restriction_flag &&
  1088. h->avctx->has_b_frames >= sps->num_reorder_frames) {
  1089. } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
  1090. h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
  1091. if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
  1092. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
  1093. }
  1094. } else if (!h->avctx->has_b_frames &&
  1095. ((h->next_outputed_poc != INT_MIN &&
  1096. out->poc > h->next_outputed_poc + 2) ||
  1097. cur->f->pict_type == AV_PICTURE_TYPE_B)) {
  1098. h->avctx->has_b_frames++;
  1099. }
  1100. if (pics > h->avctx->has_b_frames) {
  1101. out->reference &= ~DELAYED_PIC_REF;
  1102. for (i = out_idx; h->delayed_pic[i]; i++)
  1103. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1104. }
  1105. memmove(h->last_pocs, &h->last_pocs[1],
  1106. sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
  1107. h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
  1108. if (!out_of_order && pics > h->avctx->has_b_frames) {
  1109. av_frame_unref(h->output_frame);
  1110. ret = av_frame_ref(h->output_frame, out->f);
  1111. if (ret < 0)
  1112. return ret;
  1113. if (out->recovered) {
  1114. // We have reached an recovery point and all frames after it in
  1115. // display order are "recovered".
  1116. h->frame_recovered |= FRAME_RECOVERED_SEI;
  1117. }
  1118. out->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
  1119. if (!out->recovered) {
  1120. if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT))
  1121. av_frame_unref(h->output_frame);
  1122. else
  1123. h->output_frame->flags |= AV_FRAME_FLAG_CORRUPT;
  1124. }
  1125. if (out->mmco_reset) {
  1126. if (out_idx > 0) {
  1127. h->next_outputed_poc = out->poc;
  1128. h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
  1129. } else {
  1130. h->next_outputed_poc = INT_MIN;
  1131. }
  1132. } else {
  1133. if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f->key_frame) {
  1134. h->next_outputed_poc = INT_MIN;
  1135. } else {
  1136. h->next_outputed_poc = out->poc;
  1137. }
  1138. }
  1139. h->mmco_reset = 0;
  1140. } else {
  1141. av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
  1142. }
  1143. return 0;
  1144. }
  1145. /* This function is called right after decoding the slice header for a first
  1146. * slice in a field (or a frame). It decides whether we are decoding a new frame
  1147. * or a second field in a pair and does the necessary setup.
  1148. */
  1149. static int h264_field_start(H264Context *h, const H264SliceContext *sl,
  1150. const H2645NAL *nal)
  1151. {
  1152. const SPS *sps;
  1153. int last_pic_structure, last_pic_droppable, ret;
  1154. ret = h264_init_ps(h, sl);
  1155. if (ret < 0)
  1156. return ret;
  1157. sps = h->ps.sps;
  1158. last_pic_droppable = h->droppable;
  1159. last_pic_structure = h->picture_structure;
  1160. h->droppable = (nal->ref_idc == 0);
  1161. h->picture_structure = sl->picture_structure;
  1162. h->poc.frame_num = sl->frame_num;
  1163. h->poc.poc_lsb = sl->poc_lsb;
  1164. h->poc.delta_poc_bottom = sl->delta_poc_bottom;
  1165. h->poc.delta_poc[0] = sl->delta_poc[0];
  1166. h->poc.delta_poc[1] = sl->delta_poc[1];
  1167. /* Shorten frame num gaps so we don't have to allocate reference
  1168. * frames just to throw them away */
  1169. if (h->poc.frame_num != h->poc.prev_frame_num) {
  1170. int unwrap_prev_frame_num = h->poc.prev_frame_num;
  1171. int max_frame_num = 1 << sps->log2_max_frame_num;
  1172. if (unwrap_prev_frame_num > h->poc.frame_num)
  1173. unwrap_prev_frame_num -= max_frame_num;
  1174. if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
  1175. unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
  1176. if (unwrap_prev_frame_num < 0)
  1177. unwrap_prev_frame_num += max_frame_num;
  1178. h->poc.prev_frame_num = unwrap_prev_frame_num;
  1179. }
  1180. }
  1181. /* See if we have a decoded first field looking for a pair...
  1182. * Here, we're using that to see if we should mark previously
  1183. * decode frames as "finished".
  1184. * We have to do that before the "dummy" in-between frame allocation,
  1185. * since that can modify s->current_picture_ptr. */
  1186. if (h->first_field) {
  1187. assert(h->cur_pic_ptr);
  1188. assert(h->cur_pic_ptr->f->buf[0]);
  1189. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1190. /* figure out if we have a complementary field pair */
  1191. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1192. /* Previous field is unmatched. Don't display it, but let it
  1193. * remain for reference if marked as such. */
  1194. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  1195. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1196. last_pic_structure == PICT_TOP_FIELD);
  1197. }
  1198. } else {
  1199. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1200. /* This and previous field were reference, but had
  1201. * different frame_nums. Consider this field first in
  1202. * pair. Throw away previous field except for reference
  1203. * purposes. */
  1204. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  1205. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1206. last_pic_structure == PICT_TOP_FIELD);
  1207. }
  1208. } else {
  1209. /* Second field in complementary pair */
  1210. if (!((last_pic_structure == PICT_TOP_FIELD &&
  1211. h->picture_structure == PICT_BOTTOM_FIELD) ||
  1212. (last_pic_structure == PICT_BOTTOM_FIELD &&
  1213. h->picture_structure == PICT_TOP_FIELD))) {
  1214. av_log(h->avctx, AV_LOG_ERROR,
  1215. "Invalid field mode combination %d/%d\n",
  1216. last_pic_structure, h->picture_structure);
  1217. h->picture_structure = last_pic_structure;
  1218. h->droppable = last_pic_droppable;
  1219. return AVERROR_INVALIDDATA;
  1220. } else if (last_pic_droppable != h->droppable) {
  1221. avpriv_request_sample(h->avctx,
  1222. "Found reference and non-reference fields in the same frame, which");
  1223. h->picture_structure = last_pic_structure;
  1224. h->droppable = last_pic_droppable;
  1225. return AVERROR_PATCHWELCOME;
  1226. }
  1227. }
  1228. }
  1229. }
  1230. while (h->poc.frame_num != h->poc.prev_frame_num &&
  1231. h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
  1232. H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1233. av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  1234. h->poc.frame_num, h->poc.prev_frame_num);
  1235. ret = initialize_cur_frame(h);
  1236. if (ret < 0) {
  1237. h->first_field = 0;
  1238. return ret;
  1239. }
  1240. h->poc.prev_frame_num++;
  1241. h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
  1242. h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
  1243. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  1244. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  1245. h->explicit_ref_marking = 0;
  1246. ret = ff_h264_execute_ref_pic_marking(h);
  1247. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1248. return ret;
  1249. /* Error concealment: If a ref is missing, copy the previous ref
  1250. * in its place.
  1251. * FIXME: Avoiding a memcpy would be nice, but ref handling makes
  1252. * many assumptions about there being no actual duplicates.
  1253. * FIXME: This does not copy padding for out-of-frame motion
  1254. * vectors. Given we are concealing a lost frame, this probably
  1255. * is not noticeable by comparison, but it should be fixed. */
  1256. if (h->short_ref_count) {
  1257. if (prev &&
  1258. h->short_ref[0]->f->width == prev->f->width &&
  1259. h->short_ref[0]->f->height == prev->f->height &&
  1260. h->short_ref[0]->f->format == prev->f->format) {
  1261. ff_thread_await_progress(&prev->tf, INT_MAX, 0);
  1262. if (prev->field_picture)
  1263. ff_thread_await_progress(&prev->tf, INT_MAX, 1);
  1264. av_image_copy(h->short_ref[0]->f->data,
  1265. h->short_ref[0]->f->linesize,
  1266. (const uint8_t **)prev->f->data,
  1267. prev->f->linesize,
  1268. prev->f->format,
  1269. h->mb_width * 16,
  1270. h->mb_height * 16);
  1271. h->short_ref[0]->poc = prev->poc + 2;
  1272. }
  1273. h->short_ref[0]->frame_num = h->poc.prev_frame_num;
  1274. }
  1275. }
  1276. /* See if we have a decoded first field looking for a pair...
  1277. * We're using that to see whether to continue decoding in that
  1278. * frame, or to allocate a new one. */
  1279. if (h->first_field) {
  1280. assert(h->cur_pic_ptr);
  1281. assert(h->cur_pic_ptr->f->buf[0]);
  1282. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1283. /* figure out if we have a complementary field pair */
  1284. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1285. /* Previous field is unmatched. Don't display it, but let it
  1286. * remain for reference if marked as such. */
  1287. h->cur_pic_ptr = NULL;
  1288. h->first_field = FIELD_PICTURE(h);
  1289. } else {
  1290. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1291. /* This and the previous field had different frame_nums.
  1292. * Consider this field first in pair. Throw away previous
  1293. * one except for reference purposes. */
  1294. h->first_field = 1;
  1295. h->cur_pic_ptr = NULL;
  1296. } else {
  1297. /* Second field in complementary pair */
  1298. h->first_field = 0;
  1299. }
  1300. }
  1301. } else {
  1302. /* Frame or first field in a potentially complementary pair */
  1303. h->first_field = FIELD_PICTURE(h);
  1304. }
  1305. if (!FIELD_PICTURE(h) || h->first_field) {
  1306. if (h264_frame_start(h) < 0) {
  1307. h->first_field = 0;
  1308. return AVERROR_INVALIDDATA;
  1309. }
  1310. } else {
  1311. release_unused_pictures(h, 0);
  1312. }
  1313. ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
  1314. h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
  1315. memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
  1316. h->nb_mmco = sl->nb_mmco;
  1317. h->explicit_ref_marking = sl->explicit_ref_marking;
  1318. h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
  1319. if (h->sei.recovery_point.recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
  1320. h->recovery_frame = (h->poc.frame_num + h->sei.recovery_point.recovery_frame_cnt) &
  1321. ((1 << h->ps.sps->log2_max_frame_num) - 1);
  1322. }
  1323. h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE) ||
  1324. (h->sei.recovery_point.recovery_frame_cnt >= 0);
  1325. if (nal->type == H264_NAL_IDR_SLICE || h->recovery_frame == h->poc.frame_num) {
  1326. h->recovery_frame = -1;
  1327. h->cur_pic_ptr->recovered = 1;
  1328. }
  1329. // If we have an IDR, all frames after it in decoded order are
  1330. // "recovered".
  1331. if (nal->type == H264_NAL_IDR_SLICE)
  1332. h->frame_recovered |= FRAME_RECOVERED_IDR;
  1333. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  1334. /* Set the frame properties/side data. Only done for the second field in
  1335. * field coded frames, since some SEI information is present for each field
  1336. * and is merged by the SEI parsing code. */
  1337. if (!FIELD_PICTURE(h) || !h->first_field) {
  1338. ret = h264_export_frame_props(h);
  1339. if (ret < 0)
  1340. return ret;
  1341. ret = h264_select_output_frame(h);
  1342. if (ret < 0)
  1343. return ret;
  1344. }
  1345. if (h->avctx->hwaccel) {
  1346. ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0);
  1347. if (ret < 0)
  1348. return ret;
  1349. }
  1350. return 0;
  1351. }
  1352. static int h264_slice_header_parse(H264SliceContext *sl, const H2645NAL *nal,
  1353. const H264ParamSets *ps, AVCodecContext *avctx)
  1354. {
  1355. const SPS *sps;
  1356. const PPS *pps;
  1357. int ret;
  1358. unsigned int slice_type, tmp, i;
  1359. int field_pic_flag, bottom_field_flag, picture_structure;
  1360. sl->first_mb_addr = get_ue_golomb(&sl->gb);
  1361. slice_type = get_ue_golomb_31(&sl->gb);
  1362. if (slice_type > 9) {
  1363. av_log(avctx, AV_LOG_ERROR,
  1364. "slice type %d too large at %d\n",
  1365. slice_type, sl->first_mb_addr);
  1366. return AVERROR_INVALIDDATA;
  1367. }
  1368. if (slice_type > 4) {
  1369. slice_type -= 5;
  1370. sl->slice_type_fixed = 1;
  1371. } else
  1372. sl->slice_type_fixed = 0;
  1373. slice_type = ff_h264_golomb_to_pict_type[slice_type];
  1374. sl->slice_type = slice_type;
  1375. sl->slice_type_nos = slice_type & 3;
  1376. if (nal->type == H264_NAL_IDR_SLICE &&
  1377. sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1378. av_log(avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
  1379. return AVERROR_INVALIDDATA;
  1380. }
  1381. sl->pps_id = get_ue_golomb(&sl->gb);
  1382. if (sl->pps_id >= MAX_PPS_COUNT) {
  1383. av_log(avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
  1384. return AVERROR_INVALIDDATA;
  1385. }
  1386. if (!ps->pps_list[sl->pps_id]) {
  1387. av_log(avctx, AV_LOG_ERROR,
  1388. "non-existing PPS %u referenced\n",
  1389. sl->pps_id);
  1390. return AVERROR_INVALIDDATA;
  1391. }
  1392. pps = (const PPS*)ps->pps_list[sl->pps_id]->data;
  1393. if (!ps->sps_list[pps->sps_id]) {
  1394. av_log(avctx, AV_LOG_ERROR,
  1395. "non-existing SPS %u referenced\n", pps->sps_id);
  1396. return AVERROR_INVALIDDATA;
  1397. }
  1398. sps = (const SPS*)ps->sps_list[pps->sps_id]->data;
  1399. sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
  1400. sl->mb_mbaff = 0;
  1401. if (sps->frame_mbs_only_flag) {
  1402. picture_structure = PICT_FRAME;
  1403. } else {
  1404. field_pic_flag = get_bits1(&sl->gb);
  1405. if (field_pic_flag) {
  1406. bottom_field_flag = get_bits1(&sl->gb);
  1407. picture_structure = PICT_TOP_FIELD + bottom_field_flag;
  1408. } else {
  1409. picture_structure = PICT_FRAME;
  1410. }
  1411. }
  1412. sl->picture_structure = picture_structure;
  1413. sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
  1414. if (picture_structure == PICT_FRAME) {
  1415. sl->curr_pic_num = sl->frame_num;
  1416. sl->max_pic_num = 1 << sps->log2_max_frame_num;
  1417. } else {
  1418. sl->curr_pic_num = 2 * sl->frame_num + 1;
  1419. sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
  1420. }
  1421. if (nal->type == H264_NAL_IDR_SLICE)
  1422. get_ue_golomb(&sl->gb); /* idr_pic_id */
  1423. if (sps->poc_type == 0) {
  1424. sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
  1425. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1426. sl->delta_poc_bottom = get_se_golomb(&sl->gb);
  1427. }
  1428. if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
  1429. sl->delta_poc[0] = get_se_golomb(&sl->gb);
  1430. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1431. sl->delta_poc[1] = get_se_golomb(&sl->gb);
  1432. }
  1433. sl->redundant_pic_count = 0;
  1434. if (pps->redundant_pic_cnt_present)
  1435. sl->redundant_pic_count = get_ue_golomb(&sl->gb);
  1436. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1437. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1438. ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
  1439. &sl->gb, pps, sl->slice_type_nos,
  1440. picture_structure);
  1441. if (ret < 0)
  1442. return ret;
  1443. if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1444. ret = ff_h264_decode_ref_pic_list_reordering(sl, avctx);
  1445. if (ret < 0) {
  1446. sl->ref_count[1] = sl->ref_count[0] = 0;
  1447. return ret;
  1448. }
  1449. }
  1450. sl->pwt.use_weight = 0;
  1451. for (i = 0; i < 2; i++) {
  1452. sl->pwt.luma_weight_flag[i] = 0;
  1453. sl->pwt.chroma_weight_flag[i] = 0;
  1454. }
  1455. if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
  1456. (pps->weighted_bipred_idc == 1 &&
  1457. sl->slice_type_nos == AV_PICTURE_TYPE_B))
  1458. ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
  1459. sl->slice_type_nos, &sl->pwt);
  1460. sl->explicit_ref_marking = 0;
  1461. if (nal->ref_idc) {
  1462. ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, avctx);
  1463. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  1464. return AVERROR_INVALIDDATA;
  1465. }
  1466. if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
  1467. tmp = get_ue_golomb_31(&sl->gb);
  1468. if (tmp > 2) {
  1469. av_log(avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
  1470. return AVERROR_INVALIDDATA;
  1471. }
  1472. sl->cabac_init_idc = tmp;
  1473. }
  1474. sl->last_qscale_diff = 0;
  1475. tmp = pps->init_qp + get_se_golomb(&sl->gb);
  1476. if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
  1477. av_log(avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1478. return AVERROR_INVALIDDATA;
  1479. }
  1480. sl->qscale = tmp;
  1481. sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
  1482. sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
  1483. // FIXME qscale / qp ... stuff
  1484. if (sl->slice_type == AV_PICTURE_TYPE_SP)
  1485. get_bits1(&sl->gb); /* sp_for_switch_flag */
  1486. if (sl->slice_type == AV_PICTURE_TYPE_SP ||
  1487. sl->slice_type == AV_PICTURE_TYPE_SI)
  1488. get_se_golomb(&sl->gb); /* slice_qs_delta */
  1489. sl->deblocking_filter = 1;
  1490. sl->slice_alpha_c0_offset = 0;
  1491. sl->slice_beta_offset = 0;
  1492. if (pps->deblocking_filter_parameters_present) {
  1493. tmp = get_ue_golomb_31(&sl->gb);
  1494. if (tmp > 2) {
  1495. av_log(avctx, AV_LOG_ERROR,
  1496. "deblocking_filter_idc %u out of range\n", tmp);
  1497. return AVERROR_INVALIDDATA;
  1498. }
  1499. sl->deblocking_filter = tmp;
  1500. if (sl->deblocking_filter < 2)
  1501. sl->deblocking_filter ^= 1; // 1<->0
  1502. if (sl->deblocking_filter) {
  1503. sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
  1504. sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;
  1505. if (sl->slice_alpha_c0_offset > 12 ||
  1506. sl->slice_alpha_c0_offset < -12 ||
  1507. sl->slice_beta_offset > 12 ||
  1508. sl->slice_beta_offset < -12) {
  1509. av_log(avctx, AV_LOG_ERROR,
  1510. "deblocking filter parameters %d %d out of range\n",
  1511. sl->slice_alpha_c0_offset, sl->slice_beta_offset);
  1512. return AVERROR_INVALIDDATA;
  1513. }
  1514. }
  1515. }
  1516. return 0;
  1517. }
  1518. /* do all the per-slice initialization needed before we can start decoding the
  1519. * actual MBs */
  1520. static int h264_slice_init(H264Context *h, H264SliceContext *sl,
  1521. const H2645NAL *nal)
  1522. {
  1523. int i, j, ret = 0;
  1524. if (h->current_slice > 0) {
  1525. if (h->ps.pps != (const PPS*)h->ps.pps_list[sl->pps_id]->data) {
  1526. av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
  1527. return AVERROR_INVALIDDATA;
  1528. }
  1529. if (h->picture_structure != sl->picture_structure ||
  1530. h->droppable != (nal->ref_idc == 0)) {
  1531. av_log(h->avctx, AV_LOG_ERROR,
  1532. "Changing field mode (%d -> %d) between slices is not allowed\n",
  1533. h->picture_structure, sl->picture_structure);
  1534. return AVERROR_INVALIDDATA;
  1535. } else if (!h->cur_pic_ptr) {
  1536. av_log(h->avctx, AV_LOG_ERROR,
  1537. "unset cur_pic_ptr on slice %d\n",
  1538. h->current_slice + 1);
  1539. return AVERROR_INVALIDDATA;
  1540. }
  1541. }
  1542. if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
  1543. av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
  1544. return AVERROR_INVALIDDATA;
  1545. }
  1546. assert(h->mb_num == h->mb_width * h->mb_height);
  1547. if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  1548. sl->first_mb_addr >= h->mb_num) {
  1549. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1550. return AVERROR_INVALIDDATA;
  1551. }
  1552. sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
  1553. sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
  1554. FIELD_OR_MBAFF_PICTURE(h);
  1555. if (h->picture_structure == PICT_BOTTOM_FIELD)
  1556. sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
  1557. assert(sl->mb_y < h->mb_height);
  1558. ret = ff_h264_build_ref_list(h, sl);
  1559. if (ret < 0)
  1560. return ret;
  1561. if (h->ps.pps->weighted_bipred_idc == 2 &&
  1562. sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1563. implicit_weight_table(h, sl, -1);
  1564. if (FRAME_MBAFF(h)) {
  1565. implicit_weight_table(h, sl, 0);
  1566. implicit_weight_table(h, sl, 1);
  1567. }
  1568. }
  1569. if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
  1570. ff_h264_direct_dist_scale_factor(h, sl);
  1571. ff_h264_direct_ref_list_init(h, sl);
  1572. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  1573. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  1574. sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1575. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  1576. sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1577. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  1578. nal->ref_idc == 0))
  1579. sl->deblocking_filter = 0;
  1580. if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) {
  1581. if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  1582. /* Cheat slightly for speed:
  1583. * Do not bother to deblock across slices. */
  1584. sl->deblocking_filter = 2;
  1585. } else {
  1586. h->postpone_filter = 1;
  1587. }
  1588. }
  1589. sl->qp_thresh = 15 -
  1590. FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
  1591. FFMAX3(0,
  1592. h->ps.pps->chroma_qp_index_offset[0],
  1593. h->ps.pps->chroma_qp_index_offset[1]) +
  1594. 6 * (h->ps.sps->bit_depth_luma - 8);
  1595. sl->slice_num = ++h->current_slice;
  1596. if (sl->slice_num >= MAX_SLICES) {
  1597. av_log(h->avctx, AV_LOG_ERROR,
  1598. "Too many slices, increase MAX_SLICES and recompile\n");
  1599. }
  1600. for (j = 0; j < 2; j++) {
  1601. int id_list[16];
  1602. int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
  1603. for (i = 0; i < 16; i++) {
  1604. id_list[i] = 60;
  1605. if (j < sl->list_count && i < sl->ref_count[j] &&
  1606. sl->ref_list[j][i].parent->f->buf[0]) {
  1607. int k;
  1608. AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
  1609. for (k = 0; k < h->short_ref_count; k++)
  1610. if (h->short_ref[k]->f->buf[0]->buffer == buf) {
  1611. id_list[i] = k;
  1612. break;
  1613. }
  1614. for (k = 0; k < h->long_ref_count; k++)
  1615. if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
  1616. id_list[i] = h->short_ref_count + k;
  1617. break;
  1618. }
  1619. }
  1620. }
  1621. ref2frm[0] =
  1622. ref2frm[1] = -1;
  1623. for (i = 0; i < 16; i++)
  1624. ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
  1625. ref2frm[18 + 0] =
  1626. ref2frm[18 + 1] = -1;
  1627. for (i = 16; i < 48; i++)
  1628. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  1629. (sl->ref_list[j][i].reference & 3);
  1630. }
  1631. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  1632. av_log(h->avctx, AV_LOG_DEBUG,
  1633. "slice:%d %s mb:%d %c%s%s frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
  1634. sl->slice_num,
  1635. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  1636. sl->mb_y * h->mb_width + sl->mb_x,
  1637. av_get_picture_type_char(sl->slice_type),
  1638. sl->slice_type_fixed ? " fix" : "",
  1639. nal->type == H264_NAL_IDR_SLICE ? " IDR" : "",
  1640. h->poc.frame_num,
  1641. h->cur_pic_ptr->field_poc[0],
  1642. h->cur_pic_ptr->field_poc[1],
  1643. sl->ref_count[0], sl->ref_count[1],
  1644. sl->qscale,
  1645. sl->deblocking_filter,
  1646. sl->slice_alpha_c0_offset, sl->slice_beta_offset,
  1647. sl->pwt.use_weight,
  1648. sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
  1649. sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  1650. }
  1651. return 0;
  1652. }
  1653. int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal)
  1654. {
  1655. H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
  1656. int ret;
  1657. sl->gb = nal->gb;
  1658. ret = h264_slice_header_parse(sl, nal, &h->ps, h->avctx);
  1659. if (ret < 0)
  1660. return ret;
  1661. // discard redundant pictures
  1662. if (sl->redundant_pic_count > 0)
  1663. return 0;
  1664. if (!h->setup_finished) {
  1665. if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
  1666. // this slice starts a new field
  1667. // first decode any pending queued slices
  1668. if (h->nb_slice_ctx_queued) {
  1669. H264SliceContext tmp_ctx;
  1670. ret = ff_h264_execute_decode_slices(h);
  1671. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1672. return ret;
  1673. memcpy(&tmp_ctx, h->slice_ctx, sizeof(tmp_ctx));
  1674. memcpy(h->slice_ctx, sl, sizeof(tmp_ctx));
  1675. memcpy(sl, &tmp_ctx, sizeof(tmp_ctx));
  1676. sl = h->slice_ctx;
  1677. }
  1678. if (h->field_started)
  1679. ff_h264_field_end(h, sl, 1);
  1680. h->current_slice = 0;
  1681. if (!h->first_field) {
  1682. if (h->cur_pic_ptr && !h->droppable) {
  1683. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1684. h->picture_structure == PICT_BOTTOM_FIELD);
  1685. }
  1686. h->cur_pic_ptr = NULL;
  1687. }
  1688. }
  1689. if (h->current_slice == 0) {
  1690. ret = h264_field_start(h, sl, nal);
  1691. if (ret < 0)
  1692. return ret;
  1693. h->field_started = 1;
  1694. }
  1695. }
  1696. ret = h264_slice_init(h, sl, nal);
  1697. if (ret < 0)
  1698. return ret;
  1699. if ((h->avctx->skip_frame < AVDISCARD_NONREF || nal->ref_idc) &&
  1700. (h->avctx->skip_frame < AVDISCARD_BIDIR ||
  1701. sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
  1702. (h->avctx->skip_frame < AVDISCARD_NONKEY ||
  1703. h->cur_pic_ptr->f->key_frame) &&
  1704. h->avctx->skip_frame < AVDISCARD_ALL) {
  1705. h->nb_slice_ctx_queued++;
  1706. }
  1707. return 0;
  1708. }
  1709. int ff_h264_get_slice_type(const H264SliceContext *sl)
  1710. {
  1711. switch (sl->slice_type) {
  1712. case AV_PICTURE_TYPE_P:
  1713. return 0;
  1714. case AV_PICTURE_TYPE_B:
  1715. return 1;
  1716. case AV_PICTURE_TYPE_I:
  1717. return 2;
  1718. case AV_PICTURE_TYPE_SP:
  1719. return 3;
  1720. case AV_PICTURE_TYPE_SI:
  1721. return 4;
  1722. default:
  1723. return AVERROR_INVALIDDATA;
  1724. }
  1725. }
  1726. static av_always_inline void fill_filter_caches_inter(const H264Context *h,
  1727. H264SliceContext *sl,
  1728. int mb_type, int top_xy,
  1729. int left_xy[LEFT_MBS],
  1730. int top_type,
  1731. int left_type[LEFT_MBS],
  1732. int mb_xy, int list)
  1733. {
  1734. int b_stride = h->b_stride;
  1735. int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
  1736. int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
  1737. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  1738. if (USES_LIST(top_type, list)) {
  1739. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  1740. const int b8_xy = 4 * top_xy + 2;
  1741. const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1742. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  1743. ref_cache[0 - 1 * 8] =
  1744. ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
  1745. ref_cache[2 - 1 * 8] =
  1746. ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
  1747. } else {
  1748. AV_ZERO128(mv_dst - 1 * 8);
  1749. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1750. }
  1751. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  1752. if (USES_LIST(left_type[LTOP], list)) {
  1753. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  1754. const int b8_xy = 4 * left_xy[LTOP] + 1;
  1755. const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1756. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  1757. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  1758. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  1759. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  1760. ref_cache[-1 + 0] =
  1761. ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  1762. ref_cache[-1 + 16] =
  1763. ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  1764. } else {
  1765. AV_ZERO32(mv_dst - 1 + 0);
  1766. AV_ZERO32(mv_dst - 1 + 8);
  1767. AV_ZERO32(mv_dst - 1 + 16);
  1768. AV_ZERO32(mv_dst - 1 + 24);
  1769. ref_cache[-1 + 0] =
  1770. ref_cache[-1 + 8] =
  1771. ref_cache[-1 + 16] =
  1772. ref_cache[-1 + 24] = LIST_NOT_USED;
  1773. }
  1774. }
  1775. }
  1776. if (!USES_LIST(mb_type, list)) {
  1777. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  1778. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1779. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1780. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1781. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1782. return;
  1783. }
  1784. {
  1785. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  1786. const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1787. uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
  1788. uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
  1789. AV_WN32A(&ref_cache[0 * 8], ref01);
  1790. AV_WN32A(&ref_cache[1 * 8], ref01);
  1791. AV_WN32A(&ref_cache[2 * 8], ref23);
  1792. AV_WN32A(&ref_cache[3 * 8], ref23);
  1793. }
  1794. {
  1795. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
  1796. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  1797. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  1798. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  1799. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  1800. }
  1801. }
  1802. /**
  1803. * @return non zero if the loop filter can be skipped
  1804. */
  1805. static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
  1806. {
  1807. const int mb_xy = sl->mb_xy;
  1808. int top_xy, left_xy[LEFT_MBS];
  1809. int top_type, left_type[LEFT_MBS];
  1810. uint8_t *nnz;
  1811. uint8_t *nnz_cache;
  1812. top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
  1813. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  1814. if (FRAME_MBAFF(h)) {
  1815. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  1816. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  1817. if (sl->mb_y & 1) {
  1818. if (left_mb_field_flag != curr_mb_field_flag)
  1819. left_xy[LTOP] -= h->mb_stride;
  1820. } else {
  1821. if (curr_mb_field_flag)
  1822. top_xy += h->mb_stride &
  1823. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  1824. if (left_mb_field_flag != curr_mb_field_flag)
  1825. left_xy[LBOT] += h->mb_stride;
  1826. }
  1827. }
  1828. sl->top_mb_xy = top_xy;
  1829. sl->left_mb_xy[LTOP] = left_xy[LTOP];
  1830. sl->left_mb_xy[LBOT] = left_xy[LBOT];
  1831. {
  1832. /* For sufficiently low qp, filtering wouldn't do anything.
  1833. * This is a conservative estimate: could also check beta_offset
  1834. * and more accurate chroma_qp. */
  1835. int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  1836. int qp = h->cur_pic.qscale_table[mb_xy];
  1837. if (qp <= qp_thresh &&
  1838. (left_xy[LTOP] < 0 ||
  1839. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  1840. (top_xy < 0 ||
  1841. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  1842. if (!FRAME_MBAFF(h))
  1843. return 1;
  1844. if ((left_xy[LTOP] < 0 ||
  1845. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  1846. (top_xy < h->mb_stride ||
  1847. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  1848. return 1;
  1849. }
  1850. }
  1851. top_type = h->cur_pic.mb_type[top_xy];
  1852. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  1853. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  1854. if (sl->deblocking_filter == 2) {
  1855. if (h->slice_table[top_xy] != sl->slice_num)
  1856. top_type = 0;
  1857. if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
  1858. left_type[LTOP] = left_type[LBOT] = 0;
  1859. } else {
  1860. if (h->slice_table[top_xy] == 0xFFFF)
  1861. top_type = 0;
  1862. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  1863. left_type[LTOP] = left_type[LBOT] = 0;
  1864. }
  1865. sl->top_type = top_type;
  1866. sl->left_type[LTOP] = left_type[LTOP];
  1867. sl->left_type[LBOT] = left_type[LBOT];
  1868. if (IS_INTRA(mb_type))
  1869. return 0;
  1870. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1871. top_type, left_type, mb_xy, 0);
  1872. if (sl->list_count == 2)
  1873. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1874. top_type, left_type, mb_xy, 1);
  1875. nnz = h->non_zero_count[mb_xy];
  1876. nnz_cache = sl->non_zero_count_cache;
  1877. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  1878. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  1879. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  1880. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  1881. sl->cbp = h->cbp_table[mb_xy];
  1882. if (top_type) {
  1883. nnz = h->non_zero_count[top_xy];
  1884. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  1885. }
  1886. if (left_type[LTOP]) {
  1887. nnz = h->non_zero_count[left_xy[LTOP]];
  1888. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  1889. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  1890. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  1891. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  1892. }
  1893. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  1894. * from what the loop filter needs */
  1895. if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
  1896. if (IS_8x8DCT(top_type)) {
  1897. nnz_cache[4 + 8 * 0] =
  1898. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  1899. nnz_cache[6 + 8 * 0] =
  1900. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  1901. }
  1902. if (IS_8x8DCT(left_type[LTOP])) {
  1903. nnz_cache[3 + 8 * 1] =
  1904. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  1905. }
  1906. if (IS_8x8DCT(left_type[LBOT])) {
  1907. nnz_cache[3 + 8 * 3] =
  1908. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  1909. }
  1910. if (IS_8x8DCT(mb_type)) {
  1911. nnz_cache[scan8[0]] =
  1912. nnz_cache[scan8[1]] =
  1913. nnz_cache[scan8[2]] =
  1914. nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
  1915. nnz_cache[scan8[0 + 4]] =
  1916. nnz_cache[scan8[1 + 4]] =
  1917. nnz_cache[scan8[2 + 4]] =
  1918. nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
  1919. nnz_cache[scan8[0 + 8]] =
  1920. nnz_cache[scan8[1 + 8]] =
  1921. nnz_cache[scan8[2 + 8]] =
  1922. nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
  1923. nnz_cache[scan8[0 + 12]] =
  1924. nnz_cache[scan8[1 + 12]] =
  1925. nnz_cache[scan8[2 + 12]] =
  1926. nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
  1927. }
  1928. }
  1929. return 0;
  1930. }
  1931. static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
  1932. {
  1933. uint8_t *dest_y, *dest_cb, *dest_cr;
  1934. int linesize, uvlinesize, mb_x, mb_y;
  1935. const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
  1936. const int old_slice_type = sl->slice_type;
  1937. const int pixel_shift = h->pixel_shift;
  1938. const int block_h = 16 >> h->chroma_y_shift;
  1939. if (h->postpone_filter)
  1940. return;
  1941. if (sl->deblocking_filter) {
  1942. for (mb_x = start_x; mb_x < end_x; mb_x++)
  1943. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  1944. int mb_xy, mb_type;
  1945. mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
  1946. mb_type = h->cur_pic.mb_type[mb_xy];
  1947. if (FRAME_MBAFF(h))
  1948. sl->mb_mbaff =
  1949. sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  1950. sl->mb_x = mb_x;
  1951. sl->mb_y = mb_y;
  1952. dest_y = h->cur_pic.f->data[0] +
  1953. ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
  1954. dest_cb = h->cur_pic.f->data[1] +
  1955. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1956. mb_y * sl->uvlinesize * block_h;
  1957. dest_cr = h->cur_pic.f->data[2] +
  1958. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1959. mb_y * sl->uvlinesize * block_h;
  1960. // FIXME simplify above
  1961. if (MB_FIELD(sl)) {
  1962. linesize = sl->mb_linesize = sl->linesize * 2;
  1963. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
  1964. if (mb_y & 1) { // FIXME move out of this function?
  1965. dest_y -= sl->linesize * 15;
  1966. dest_cb -= sl->uvlinesize * (block_h - 1);
  1967. dest_cr -= sl->uvlinesize * (block_h - 1);
  1968. }
  1969. } else {
  1970. linesize = sl->mb_linesize = sl->linesize;
  1971. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
  1972. }
  1973. backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
  1974. uvlinesize, 0);
  1975. if (fill_filter_caches(h, sl, mb_type))
  1976. continue;
  1977. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
  1978. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]);
  1979. if (FRAME_MBAFF(h)) {
  1980. ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  1981. linesize, uvlinesize);
  1982. } else {
  1983. ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
  1984. dest_cr, linesize, uvlinesize);
  1985. }
  1986. }
  1987. }
  1988. sl->slice_type = old_slice_type;
  1989. sl->mb_x = end_x;
  1990. sl->mb_y = end_mb_y - FRAME_MBAFF(h);
  1991. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
  1992. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
  1993. }
  1994. static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
  1995. {
  1996. const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
  1997. int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
  1998. h->cur_pic.mb_type[mb_xy - 1] :
  1999. (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
  2000. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  2001. sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2002. }
  2003. /**
  2004. * Draw edges and report progress for the last MB row.
  2005. */
  2006. static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
  2007. {
  2008. int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
  2009. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  2010. int height = 16 << FRAME_MBAFF(h);
  2011. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  2012. if (sl->deblocking_filter) {
  2013. if ((top + height) >= pic_height)
  2014. height += deblock_border;
  2015. top -= deblock_border;
  2016. }
  2017. if (top >= pic_height || (top + height) < 0)
  2018. return;
  2019. height = FFMIN(height, pic_height - top);
  2020. if (top < 0) {
  2021. height = top + height;
  2022. top = 0;
  2023. }
  2024. ff_h264_draw_horiz_band(h, sl, top, height);
  2025. if (h->droppable)
  2026. return;
  2027. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  2028. h->picture_structure == PICT_BOTTOM_FIELD);
  2029. }
  2030. static void er_add_slice(H264SliceContext *sl,
  2031. int startx, int starty,
  2032. int endx, int endy, int status)
  2033. {
  2034. #if CONFIG_ERROR_RESILIENCE
  2035. ERContext *er = &sl->er;
  2036. if (!sl->h264->enable_er)
  2037. return;
  2038. er->ref_count = sl->ref_count[0];
  2039. ff_er_add_slice(er, startx, starty, endx, endy, status);
  2040. #endif
  2041. }
  2042. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  2043. {
  2044. H264SliceContext *sl = arg;
  2045. const H264Context *h = sl->h264;
  2046. int lf_x_start = sl->mb_x;
  2047. int orig_deblock = sl->deblocking_filter;
  2048. int ret;
  2049. sl->linesize = h->cur_pic_ptr->f->linesize[0];
  2050. sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
  2051. ret = alloc_scratch_buffers(sl, sl->linesize);
  2052. if (ret < 0)
  2053. return ret;
  2054. sl->mb_skip_run = -1;
  2055. if (h->postpone_filter)
  2056. sl->deblocking_filter = 0;
  2057. sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  2058. (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
  2059. if (h->ps.pps->cabac) {
  2060. /* realign */
  2061. align_get_bits(&sl->gb);
  2062. /* init cabac */
  2063. ff_init_cabac_decoder(&sl->cabac,
  2064. sl->gb.buffer + get_bits_count(&sl->gb) / 8,
  2065. (get_bits_left(&sl->gb) + 7) / 8);
  2066. ff_h264_init_cabac_states(h, sl);
  2067. for (;;) {
  2068. // START_TIMER
  2069. int ret, eos;
  2070. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2071. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2072. sl->next_slice_idx);
  2073. return AVERROR_INVALIDDATA;
  2074. }
  2075. ret = ff_h264_decode_mb_cabac(h, sl);
  2076. // STOP_TIMER("decode_mb_cabac")
  2077. if (ret >= 0)
  2078. ff_h264_hl_decode_mb(h, sl);
  2079. // FIXME optimal? or let mb_decode decode 16x32 ?
  2080. if (ret >= 0 && FRAME_MBAFF(h)) {
  2081. sl->mb_y++;
  2082. ret = ff_h264_decode_mb_cabac(h, sl);
  2083. if (ret >= 0)
  2084. ff_h264_hl_decode_mb(h, sl);
  2085. sl->mb_y--;
  2086. }
  2087. eos = get_cabac_terminate(&sl->cabac);
  2088. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  2089. sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  2090. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2091. sl->mb_y, ER_MB_END);
  2092. if (sl->mb_x >= lf_x_start)
  2093. loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
  2094. goto finish;
  2095. }
  2096. if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  2097. av_log(h->avctx, AV_LOG_ERROR,
  2098. "error while decoding MB %d %d, bytestream %td\n",
  2099. sl->mb_x, sl->mb_y,
  2100. sl->cabac.bytestream_end - sl->cabac.bytestream);
  2101. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2102. sl->mb_y, ER_MB_ERROR);
  2103. return AVERROR_INVALIDDATA;
  2104. }
  2105. if (++sl->mb_x >= h->mb_width) {
  2106. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2107. sl->mb_x = lf_x_start = 0;
  2108. decode_finish_row(h, sl);
  2109. ++sl->mb_y;
  2110. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2111. ++sl->mb_y;
  2112. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2113. predict_field_decoding_flag(h, sl);
  2114. }
  2115. }
  2116. if (eos || sl->mb_y >= h->mb_height) {
  2117. ff_tlog(h->avctx, "slice end %d %d\n",
  2118. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2119. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2120. sl->mb_y, ER_MB_END);
  2121. if (sl->mb_x > lf_x_start)
  2122. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2123. goto finish;
  2124. }
  2125. }
  2126. } else {
  2127. for (;;) {
  2128. int ret;
  2129. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2130. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2131. sl->next_slice_idx);
  2132. return AVERROR_INVALIDDATA;
  2133. }
  2134. ret = ff_h264_decode_mb_cavlc(h, sl);
  2135. if (ret >= 0)
  2136. ff_h264_hl_decode_mb(h, sl);
  2137. // FIXME optimal? or let mb_decode decode 16x32 ?
  2138. if (ret >= 0 && FRAME_MBAFF(h)) {
  2139. sl->mb_y++;
  2140. ret = ff_h264_decode_mb_cavlc(h, sl);
  2141. if (ret >= 0)
  2142. ff_h264_hl_decode_mb(h, sl);
  2143. sl->mb_y--;
  2144. }
  2145. if (ret < 0) {
  2146. av_log(h->avctx, AV_LOG_ERROR,
  2147. "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
  2148. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2149. sl->mb_y, ER_MB_ERROR);
  2150. return ret;
  2151. }
  2152. if (++sl->mb_x >= h->mb_width) {
  2153. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2154. sl->mb_x = lf_x_start = 0;
  2155. decode_finish_row(h, sl);
  2156. ++sl->mb_y;
  2157. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2158. ++sl->mb_y;
  2159. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2160. predict_field_decoding_flag(h, sl);
  2161. }
  2162. if (sl->mb_y >= h->mb_height) {
  2163. ff_tlog(h->avctx, "slice end %d %d\n",
  2164. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2165. if (get_bits_left(&sl->gb) == 0) {
  2166. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2167. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2168. goto finish;
  2169. } else {
  2170. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2171. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2172. return AVERROR_INVALIDDATA;
  2173. }
  2174. }
  2175. }
  2176. if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
  2177. ff_tlog(h->avctx, "slice end %d %d\n",
  2178. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2179. if (get_bits_left(&sl->gb) == 0) {
  2180. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2181. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2182. if (sl->mb_x > lf_x_start)
  2183. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2184. goto finish;
  2185. } else {
  2186. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2187. sl->mb_y, ER_MB_ERROR);
  2188. return AVERROR_INVALIDDATA;
  2189. }
  2190. }
  2191. }
  2192. }
  2193. finish:
  2194. sl->deblocking_filter = orig_deblock;
  2195. return 0;
  2196. }
  2197. /**
  2198. * Call decode_slice() for each context.
  2199. *
  2200. * @param h h264 master context
  2201. */
  2202. int ff_h264_execute_decode_slices(H264Context *h)
  2203. {
  2204. AVCodecContext *const avctx = h->avctx;
  2205. H264SliceContext *sl;
  2206. int context_count = h->nb_slice_ctx_queued;
  2207. int ret = 0;
  2208. int i, j;
  2209. if (h->avctx->hwaccel || context_count < 1)
  2210. return 0;
  2211. if (context_count == 1) {
  2212. h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
  2213. h->postpone_filter = 0;
  2214. ret = decode_slice(avctx, &h->slice_ctx[0]);
  2215. h->mb_y = h->slice_ctx[0].mb_y;
  2216. if (ret < 0)
  2217. goto finish;
  2218. } else {
  2219. for (i = 0; i < context_count; i++) {
  2220. int next_slice_idx = h->mb_width * h->mb_height;
  2221. int slice_idx;
  2222. sl = &h->slice_ctx[i];
  2223. sl->er.error_count = 0;
  2224. /* make sure none of those slices overlap */
  2225. slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
  2226. for (j = 0; j < context_count; j++) {
  2227. H264SliceContext *sl2 = &h->slice_ctx[j];
  2228. int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
  2229. if (i == j || slice_idx2 < slice_idx)
  2230. continue;
  2231. next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
  2232. }
  2233. sl->next_slice_idx = next_slice_idx;
  2234. }
  2235. avctx->execute(avctx, decode_slice, h->slice_ctx,
  2236. NULL, context_count, sizeof(h->slice_ctx[0]));
  2237. /* pull back stuff from slices to master context */
  2238. sl = &h->slice_ctx[context_count - 1];
  2239. h->mb_y = sl->mb_y;
  2240. for (i = 1; i < context_count; i++)
  2241. h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count;
  2242. if (h->postpone_filter) {
  2243. h->postpone_filter = 0;
  2244. for (i = 0; i < context_count; i++) {
  2245. int y_end, x_end;
  2246. sl = &h->slice_ctx[i];
  2247. y_end = FFMIN(sl->mb_y + 1, h->mb_height);
  2248. x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
  2249. for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
  2250. sl->mb_y = j;
  2251. loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
  2252. j == y_end - 1 ? x_end : h->mb_width);
  2253. }
  2254. }
  2255. }
  2256. }
  2257. finish:
  2258. h->nb_slice_ctx_queued = 0;
  2259. return ret;
  2260. }