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.

2571 lines
93KB

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