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.

2586 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. }
  976. if (h->sei.display_orientation.present &&
  977. (h->sei.display_orientation.anticlockwise_rotation ||
  978. h->sei.display_orientation.hflip ||
  979. h->sei.display_orientation.vflip)) {
  980. H264SEIDisplayOrientation *o = &h->sei.display_orientation;
  981. double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
  982. AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
  983. AV_FRAME_DATA_DISPLAYMATRIX,
  984. sizeof(int32_t) * 9);
  985. if (!rotation)
  986. return AVERROR(ENOMEM);
  987. av_display_rotation_set((int32_t *)rotation->data, angle);
  988. av_display_matrix_flip((int32_t *)rotation->data,
  989. o->hflip, o->vflip);
  990. }
  991. if (h->sei.afd.present) {
  992. AVFrameSideData *sd = av_frame_new_side_data(cur->f, AV_FRAME_DATA_AFD,
  993. sizeof(uint8_t));
  994. if (!sd)
  995. return AVERROR(ENOMEM);
  996. *sd->data = h->sei.afd.active_format_description;
  997. h->sei.afd.present = 0;
  998. }
  999. if (h->sei.a53_caption.a53_caption) {
  1000. H264SEIA53Caption *a53 = &h->sei.a53_caption;
  1001. AVFrameSideData *sd = av_frame_new_side_data(cur->f,
  1002. AV_FRAME_DATA_A53_CC,
  1003. a53->a53_caption_size);
  1004. if (!sd)
  1005. return AVERROR(ENOMEM);
  1006. memcpy(sd->data, a53->a53_caption, a53->a53_caption_size);
  1007. av_freep(&a53->a53_caption);
  1008. a53->a53_caption_size = 0;
  1009. }
  1010. if (h->sei.alternative_transfer.present &&
  1011. av_color_transfer_name(h->sei.alternative_transfer.preferred_transfer_characteristics) &&
  1012. h->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
  1013. h->avctx->color_trc = cur->f->color_trc = h->sei.alternative_transfer.preferred_transfer_characteristics;
  1014. }
  1015. return 0;
  1016. }
  1017. static int h264_select_output_frame(H264Context *h)
  1018. {
  1019. const SPS *sps = h->ps.sps;
  1020. H264Picture *out = h->cur_pic_ptr;
  1021. H264Picture *cur = h->cur_pic_ptr;
  1022. int i, pics, out_of_order, out_idx;
  1023. int invalid = 0, cnt = 0;
  1024. int ret;
  1025. if (sps->bitstream_restriction_flag ||
  1026. h->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
  1027. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
  1028. }
  1029. pics = 0;
  1030. while (h->delayed_pic[pics])
  1031. pics++;
  1032. assert(pics <= MAX_DELAYED_PIC_COUNT);
  1033. h->delayed_pic[pics++] = cur;
  1034. if (cur->reference == 0)
  1035. cur->reference = DELAYED_PIC_REF;
  1036. /* Frame reordering. This code takes pictures from coding order and sorts
  1037. * them by their incremental POC value into display order. It supports POC
  1038. * gaps, MMCO reset codes and random resets.
  1039. * A "display group" can start either with a IDR frame (f.key_frame = 1),
  1040. * and/or can be closed down with a MMCO reset code. In sequences where
  1041. * there is no delay, we can't detect that (since the frame was already
  1042. * output to the user), so we also set h->mmco_reset to detect the MMCO
  1043. * reset code.
  1044. * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
  1045. * we increase the delay between input and output. All frames affected by
  1046. * the lag (e.g. those that should have been output before another frame
  1047. * that we already returned to the user) will be dropped. This is a bug
  1048. * that we will fix later. */
  1049. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
  1050. cnt += out->poc < h->last_pocs[i];
  1051. invalid += out->poc == INT_MIN;
  1052. }
  1053. if (!h->mmco_reset && !cur->f->key_frame &&
  1054. cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
  1055. h->mmco_reset = 2;
  1056. if (pics > 1)
  1057. h->delayed_pic[pics - 2]->mmco_reset = 2;
  1058. }
  1059. if (h->mmco_reset || cur->f->key_frame) {
  1060. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  1061. h->last_pocs[i] = INT_MIN;
  1062. cnt = 0;
  1063. invalid = MAX_DELAYED_PIC_COUNT;
  1064. }
  1065. out = h->delayed_pic[0];
  1066. out_idx = 0;
  1067. for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
  1068. h->delayed_pic[i] &&
  1069. !h->delayed_pic[i - 1]->mmco_reset &&
  1070. !h->delayed_pic[i]->f->key_frame;
  1071. i++)
  1072. if (h->delayed_pic[i]->poc < out->poc) {
  1073. out = h->delayed_pic[i];
  1074. out_idx = i;
  1075. }
  1076. if (h->avctx->has_b_frames == 0 &&
  1077. (h->delayed_pic[0]->f->key_frame || h->mmco_reset))
  1078. h->next_outputed_poc = INT_MIN;
  1079. out_of_order = !out->f->key_frame && !h->mmco_reset &&
  1080. (out->poc < h->next_outputed_poc);
  1081. if (sps->bitstream_restriction_flag &&
  1082. h->avctx->has_b_frames >= sps->num_reorder_frames) {
  1083. } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
  1084. h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
  1085. if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
  1086. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
  1087. }
  1088. } else if (!h->avctx->has_b_frames &&
  1089. ((h->next_outputed_poc != INT_MIN &&
  1090. out->poc > h->next_outputed_poc + 2) ||
  1091. cur->f->pict_type == AV_PICTURE_TYPE_B)) {
  1092. h->avctx->has_b_frames++;
  1093. }
  1094. if (pics > h->avctx->has_b_frames) {
  1095. out->reference &= ~DELAYED_PIC_REF;
  1096. for (i = out_idx; h->delayed_pic[i]; i++)
  1097. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1098. }
  1099. memmove(h->last_pocs, &h->last_pocs[1],
  1100. sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
  1101. h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
  1102. if (!out_of_order && pics > h->avctx->has_b_frames) {
  1103. av_frame_unref(h->output_frame);
  1104. ret = av_frame_ref(h->output_frame, out->f);
  1105. if (ret < 0)
  1106. return ret;
  1107. if (out->recovered) {
  1108. // We have reached an recovery point and all frames after it in
  1109. // display order are "recovered".
  1110. h->frame_recovered |= FRAME_RECOVERED_SEI;
  1111. }
  1112. out->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
  1113. if (!out->recovered) {
  1114. if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT))
  1115. av_frame_unref(h->output_frame);
  1116. else
  1117. h->output_frame->flags |= AV_FRAME_FLAG_CORRUPT;
  1118. }
  1119. if (out->mmco_reset) {
  1120. if (out_idx > 0) {
  1121. h->next_outputed_poc = out->poc;
  1122. h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
  1123. } else {
  1124. h->next_outputed_poc = INT_MIN;
  1125. }
  1126. } else {
  1127. if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f->key_frame) {
  1128. h->next_outputed_poc = INT_MIN;
  1129. } else {
  1130. h->next_outputed_poc = out->poc;
  1131. }
  1132. }
  1133. h->mmco_reset = 0;
  1134. } else {
  1135. av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
  1136. }
  1137. return 0;
  1138. }
  1139. /* This function is called right after decoding the slice header for a first
  1140. * slice in a field (or a frame). It decides whether we are decoding a new frame
  1141. * or a second field in a pair and does the necessary setup.
  1142. */
  1143. static int h264_field_start(H264Context *h, const H264SliceContext *sl,
  1144. const H2645NAL *nal)
  1145. {
  1146. const SPS *sps;
  1147. int last_pic_structure, last_pic_droppable, ret;
  1148. ret = h264_init_ps(h, sl);
  1149. if (ret < 0)
  1150. return ret;
  1151. sps = h->ps.sps;
  1152. last_pic_droppable = h->droppable;
  1153. last_pic_structure = h->picture_structure;
  1154. h->droppable = (nal->ref_idc == 0);
  1155. h->picture_structure = sl->picture_structure;
  1156. h->poc.frame_num = sl->frame_num;
  1157. h->poc.poc_lsb = sl->poc_lsb;
  1158. h->poc.delta_poc_bottom = sl->delta_poc_bottom;
  1159. h->poc.delta_poc[0] = sl->delta_poc[0];
  1160. h->poc.delta_poc[1] = sl->delta_poc[1];
  1161. /* Shorten frame num gaps so we don't have to allocate reference
  1162. * frames just to throw them away */
  1163. if (h->poc.frame_num != h->poc.prev_frame_num) {
  1164. int unwrap_prev_frame_num = h->poc.prev_frame_num;
  1165. int max_frame_num = 1 << sps->log2_max_frame_num;
  1166. if (unwrap_prev_frame_num > h->poc.frame_num)
  1167. unwrap_prev_frame_num -= max_frame_num;
  1168. if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
  1169. unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
  1170. if (unwrap_prev_frame_num < 0)
  1171. unwrap_prev_frame_num += max_frame_num;
  1172. h->poc.prev_frame_num = unwrap_prev_frame_num;
  1173. }
  1174. }
  1175. /* See if we have a decoded first field looking for a pair...
  1176. * Here, we're using that to see if we should mark previously
  1177. * decode frames as "finished".
  1178. * We have to do that before the "dummy" in-between frame allocation,
  1179. * since that can modify s->current_picture_ptr. */
  1180. if (h->first_field) {
  1181. assert(h->cur_pic_ptr);
  1182. assert(h->cur_pic_ptr->f->buf[0]);
  1183. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1184. /* figure out if we have a complementary field pair */
  1185. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1186. /* Previous field is unmatched. Don't display it, but let it
  1187. * remain for reference if marked as such. */
  1188. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  1189. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1190. last_pic_structure == PICT_TOP_FIELD);
  1191. }
  1192. } else {
  1193. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1194. /* This and previous field were reference, but had
  1195. * different frame_nums. Consider this field first in
  1196. * pair. Throw away previous field except for reference
  1197. * purposes. */
  1198. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  1199. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1200. last_pic_structure == PICT_TOP_FIELD);
  1201. }
  1202. } else {
  1203. /* Second field in complementary pair */
  1204. if (!((last_pic_structure == PICT_TOP_FIELD &&
  1205. h->picture_structure == PICT_BOTTOM_FIELD) ||
  1206. (last_pic_structure == PICT_BOTTOM_FIELD &&
  1207. h->picture_structure == PICT_TOP_FIELD))) {
  1208. av_log(h->avctx, AV_LOG_ERROR,
  1209. "Invalid field mode combination %d/%d\n",
  1210. last_pic_structure, h->picture_structure);
  1211. h->picture_structure = last_pic_structure;
  1212. h->droppable = last_pic_droppable;
  1213. return AVERROR_INVALIDDATA;
  1214. } else if (last_pic_droppable != h->droppable) {
  1215. avpriv_request_sample(h->avctx,
  1216. "Found reference and non-reference fields in the same frame, which");
  1217. h->picture_structure = last_pic_structure;
  1218. h->droppable = last_pic_droppable;
  1219. return AVERROR_PATCHWELCOME;
  1220. }
  1221. }
  1222. }
  1223. }
  1224. while (h->poc.frame_num != h->poc.prev_frame_num &&
  1225. h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
  1226. H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1227. av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  1228. h->poc.frame_num, h->poc.prev_frame_num);
  1229. ret = initialize_cur_frame(h);
  1230. if (ret < 0) {
  1231. h->first_field = 0;
  1232. return ret;
  1233. }
  1234. h->poc.prev_frame_num++;
  1235. h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
  1236. h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
  1237. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  1238. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  1239. h->explicit_ref_marking = 0;
  1240. ret = ff_h264_execute_ref_pic_marking(h);
  1241. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1242. return ret;
  1243. /* Error concealment: If a ref is missing, copy the previous ref
  1244. * in its place.
  1245. * FIXME: Avoiding a memcpy would be nice, but ref handling makes
  1246. * many assumptions about there being no actual duplicates.
  1247. * FIXME: This does not copy padding for out-of-frame motion
  1248. * vectors. Given we are concealing a lost frame, this probably
  1249. * is not noticeable by comparison, but it should be fixed. */
  1250. if (h->short_ref_count) {
  1251. if (prev &&
  1252. h->short_ref[0]->f->width == prev->f->width &&
  1253. h->short_ref[0]->f->height == prev->f->height &&
  1254. h->short_ref[0]->f->format == prev->f->format) {
  1255. ff_thread_await_progress(&prev->tf, INT_MAX, 0);
  1256. if (prev->field_picture)
  1257. ff_thread_await_progress(&prev->tf, INT_MAX, 1);
  1258. av_image_copy(h->short_ref[0]->f->data,
  1259. h->short_ref[0]->f->linesize,
  1260. (const uint8_t **)prev->f->data,
  1261. prev->f->linesize,
  1262. prev->f->format,
  1263. h->mb_width * 16,
  1264. h->mb_height * 16);
  1265. h->short_ref[0]->poc = prev->poc + 2;
  1266. }
  1267. h->short_ref[0]->frame_num = h->poc.prev_frame_num;
  1268. }
  1269. }
  1270. /* See if we have a decoded first field looking for a pair...
  1271. * We're using that to see whether to continue decoding in that
  1272. * frame, or to allocate a new one. */
  1273. if (h->first_field) {
  1274. assert(h->cur_pic_ptr);
  1275. assert(h->cur_pic_ptr->f->buf[0]);
  1276. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1277. /* figure out if we have a complementary field pair */
  1278. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1279. /* Previous field is unmatched. Don't display it, but let it
  1280. * remain for reference if marked as such. */
  1281. h->cur_pic_ptr = NULL;
  1282. h->first_field = FIELD_PICTURE(h);
  1283. } else {
  1284. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1285. /* This and the previous field had different frame_nums.
  1286. * Consider this field first in pair. Throw away previous
  1287. * one except for reference purposes. */
  1288. h->first_field = 1;
  1289. h->cur_pic_ptr = NULL;
  1290. } else {
  1291. /* Second field in complementary pair */
  1292. h->first_field = 0;
  1293. }
  1294. }
  1295. } else {
  1296. /* Frame or first field in a potentially complementary pair */
  1297. h->first_field = FIELD_PICTURE(h);
  1298. }
  1299. if (!FIELD_PICTURE(h) || h->first_field) {
  1300. if (h264_frame_start(h) < 0) {
  1301. h->first_field = 0;
  1302. return AVERROR_INVALIDDATA;
  1303. }
  1304. } else {
  1305. release_unused_pictures(h, 0);
  1306. }
  1307. ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
  1308. h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
  1309. memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
  1310. h->nb_mmco = sl->nb_mmco;
  1311. h->explicit_ref_marking = sl->explicit_ref_marking;
  1312. h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
  1313. if (h->sei.recovery_point.recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
  1314. h->recovery_frame = (h->poc.frame_num + h->sei.recovery_point.recovery_frame_cnt) &
  1315. ((1 << h->ps.sps->log2_max_frame_num) - 1);
  1316. }
  1317. h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE) ||
  1318. (h->sei.recovery_point.recovery_frame_cnt >= 0);
  1319. if (nal->type == H264_NAL_IDR_SLICE || h->recovery_frame == h->poc.frame_num) {
  1320. h->recovery_frame = -1;
  1321. h->cur_pic_ptr->recovered = 1;
  1322. }
  1323. // If we have an IDR, all frames after it in decoded order are
  1324. // "recovered".
  1325. if (nal->type == H264_NAL_IDR_SLICE)
  1326. h->frame_recovered |= FRAME_RECOVERED_IDR;
  1327. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  1328. /* Set the frame properties/side data. Only done for the second field in
  1329. * field coded frames, since some SEI information is present for each field
  1330. * and is merged by the SEI parsing code. */
  1331. if (!FIELD_PICTURE(h) || !h->first_field) {
  1332. ret = h264_export_frame_props(h);
  1333. if (ret < 0)
  1334. return ret;
  1335. ret = h264_select_output_frame(h);
  1336. if (ret < 0)
  1337. return ret;
  1338. }
  1339. if (h->avctx->hwaccel) {
  1340. ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0);
  1341. if (ret < 0)
  1342. return ret;
  1343. }
  1344. return 0;
  1345. }
  1346. static int h264_slice_header_parse(H264SliceContext *sl, const H2645NAL *nal,
  1347. const H264ParamSets *ps, AVCodecContext *avctx)
  1348. {
  1349. const SPS *sps;
  1350. const PPS *pps;
  1351. int ret;
  1352. unsigned int slice_type, tmp, i;
  1353. int field_pic_flag, bottom_field_flag, picture_structure;
  1354. sl->first_mb_addr = get_ue_golomb(&sl->gb);
  1355. slice_type = get_ue_golomb_31(&sl->gb);
  1356. if (slice_type > 9) {
  1357. av_log(avctx, AV_LOG_ERROR,
  1358. "slice type %d too large at %d\n",
  1359. slice_type, sl->first_mb_addr);
  1360. return AVERROR_INVALIDDATA;
  1361. }
  1362. if (slice_type > 4) {
  1363. slice_type -= 5;
  1364. sl->slice_type_fixed = 1;
  1365. } else
  1366. sl->slice_type_fixed = 0;
  1367. slice_type = ff_h264_golomb_to_pict_type[slice_type];
  1368. sl->slice_type = slice_type;
  1369. sl->slice_type_nos = slice_type & 3;
  1370. if (nal->type == H264_NAL_IDR_SLICE &&
  1371. sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1372. av_log(avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
  1373. return AVERROR_INVALIDDATA;
  1374. }
  1375. sl->pps_id = get_ue_golomb(&sl->gb);
  1376. if (sl->pps_id >= MAX_PPS_COUNT) {
  1377. av_log(avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
  1378. return AVERROR_INVALIDDATA;
  1379. }
  1380. if (!ps->pps_list[sl->pps_id]) {
  1381. av_log(avctx, AV_LOG_ERROR,
  1382. "non-existing PPS %u referenced\n",
  1383. sl->pps_id);
  1384. return AVERROR_INVALIDDATA;
  1385. }
  1386. pps = (const PPS*)ps->pps_list[sl->pps_id]->data;
  1387. if (!ps->sps_list[pps->sps_id]) {
  1388. av_log(avctx, AV_LOG_ERROR,
  1389. "non-existing SPS %u referenced\n", pps->sps_id);
  1390. return AVERROR_INVALIDDATA;
  1391. }
  1392. sps = (const SPS*)ps->sps_list[pps->sps_id]->data;
  1393. sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
  1394. sl->mb_mbaff = 0;
  1395. if (sps->frame_mbs_only_flag) {
  1396. picture_structure = PICT_FRAME;
  1397. } else {
  1398. field_pic_flag = get_bits1(&sl->gb);
  1399. if (field_pic_flag) {
  1400. bottom_field_flag = get_bits1(&sl->gb);
  1401. picture_structure = PICT_TOP_FIELD + bottom_field_flag;
  1402. } else {
  1403. picture_structure = PICT_FRAME;
  1404. }
  1405. }
  1406. sl->picture_structure = picture_structure;
  1407. sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
  1408. if (picture_structure == PICT_FRAME) {
  1409. sl->curr_pic_num = sl->frame_num;
  1410. sl->max_pic_num = 1 << sps->log2_max_frame_num;
  1411. } else {
  1412. sl->curr_pic_num = 2 * sl->frame_num + 1;
  1413. sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
  1414. }
  1415. if (nal->type == H264_NAL_IDR_SLICE)
  1416. get_ue_golomb(&sl->gb); /* idr_pic_id */
  1417. if (sps->poc_type == 0) {
  1418. sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
  1419. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1420. sl->delta_poc_bottom = get_se_golomb(&sl->gb);
  1421. }
  1422. if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
  1423. sl->delta_poc[0] = get_se_golomb(&sl->gb);
  1424. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1425. sl->delta_poc[1] = get_se_golomb(&sl->gb);
  1426. }
  1427. sl->redundant_pic_count = 0;
  1428. if (pps->redundant_pic_cnt_present)
  1429. sl->redundant_pic_count = get_ue_golomb(&sl->gb);
  1430. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1431. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1432. ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
  1433. &sl->gb, pps, sl->slice_type_nos,
  1434. picture_structure);
  1435. if (ret < 0)
  1436. return ret;
  1437. if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1438. ret = ff_h264_decode_ref_pic_list_reordering(sl, avctx);
  1439. if (ret < 0) {
  1440. sl->ref_count[1] = sl->ref_count[0] = 0;
  1441. return ret;
  1442. }
  1443. }
  1444. sl->pwt.use_weight = 0;
  1445. for (i = 0; i < 2; i++) {
  1446. sl->pwt.luma_weight_flag[i] = 0;
  1447. sl->pwt.chroma_weight_flag[i] = 0;
  1448. }
  1449. if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
  1450. (pps->weighted_bipred_idc == 1 &&
  1451. sl->slice_type_nos == AV_PICTURE_TYPE_B))
  1452. ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
  1453. sl->slice_type_nos, &sl->pwt);
  1454. sl->explicit_ref_marking = 0;
  1455. if (nal->ref_idc) {
  1456. ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, avctx);
  1457. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  1458. return AVERROR_INVALIDDATA;
  1459. }
  1460. if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
  1461. tmp = get_ue_golomb_31(&sl->gb);
  1462. if (tmp > 2) {
  1463. av_log(avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
  1464. return AVERROR_INVALIDDATA;
  1465. }
  1466. sl->cabac_init_idc = tmp;
  1467. }
  1468. sl->last_qscale_diff = 0;
  1469. tmp = pps->init_qp + get_se_golomb(&sl->gb);
  1470. if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
  1471. av_log(avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1472. return AVERROR_INVALIDDATA;
  1473. }
  1474. sl->qscale = tmp;
  1475. sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
  1476. sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
  1477. // FIXME qscale / qp ... stuff
  1478. if (sl->slice_type == AV_PICTURE_TYPE_SP)
  1479. get_bits1(&sl->gb); /* sp_for_switch_flag */
  1480. if (sl->slice_type == AV_PICTURE_TYPE_SP ||
  1481. sl->slice_type == AV_PICTURE_TYPE_SI)
  1482. get_se_golomb(&sl->gb); /* slice_qs_delta */
  1483. sl->deblocking_filter = 1;
  1484. sl->slice_alpha_c0_offset = 0;
  1485. sl->slice_beta_offset = 0;
  1486. if (pps->deblocking_filter_parameters_present) {
  1487. tmp = get_ue_golomb_31(&sl->gb);
  1488. if (tmp > 2) {
  1489. av_log(avctx, AV_LOG_ERROR,
  1490. "deblocking_filter_idc %u out of range\n", tmp);
  1491. return AVERROR_INVALIDDATA;
  1492. }
  1493. sl->deblocking_filter = tmp;
  1494. if (sl->deblocking_filter < 2)
  1495. sl->deblocking_filter ^= 1; // 1<->0
  1496. if (sl->deblocking_filter) {
  1497. sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
  1498. sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;
  1499. if (sl->slice_alpha_c0_offset > 12 ||
  1500. sl->slice_alpha_c0_offset < -12 ||
  1501. sl->slice_beta_offset > 12 ||
  1502. sl->slice_beta_offset < -12) {
  1503. av_log(avctx, AV_LOG_ERROR,
  1504. "deblocking filter parameters %d %d out of range\n",
  1505. sl->slice_alpha_c0_offset, sl->slice_beta_offset);
  1506. return AVERROR_INVALIDDATA;
  1507. }
  1508. }
  1509. }
  1510. return 0;
  1511. }
  1512. /* do all the per-slice initialization needed before we can start decoding the
  1513. * actual MBs */
  1514. static int h264_slice_init(H264Context *h, H264SliceContext *sl,
  1515. const H2645NAL *nal)
  1516. {
  1517. int i, j, ret = 0;
  1518. if (h->current_slice > 0) {
  1519. if (h->ps.pps != (const PPS*)h->ps.pps_list[sl->pps_id]->data) {
  1520. av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
  1521. return AVERROR_INVALIDDATA;
  1522. }
  1523. if (h->picture_structure != sl->picture_structure ||
  1524. h->droppable != (nal->ref_idc == 0)) {
  1525. av_log(h->avctx, AV_LOG_ERROR,
  1526. "Changing field mode (%d -> %d) between slices is not allowed\n",
  1527. h->picture_structure, sl->picture_structure);
  1528. return AVERROR_INVALIDDATA;
  1529. } else if (!h->cur_pic_ptr) {
  1530. av_log(h->avctx, AV_LOG_ERROR,
  1531. "unset cur_pic_ptr on slice %d\n",
  1532. h->current_slice + 1);
  1533. return AVERROR_INVALIDDATA;
  1534. }
  1535. }
  1536. if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
  1537. av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
  1538. return AVERROR_INVALIDDATA;
  1539. }
  1540. assert(h->mb_num == h->mb_width * h->mb_height);
  1541. if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  1542. sl->first_mb_addr >= h->mb_num) {
  1543. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1544. return AVERROR_INVALIDDATA;
  1545. }
  1546. sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
  1547. sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
  1548. FIELD_OR_MBAFF_PICTURE(h);
  1549. if (h->picture_structure == PICT_BOTTOM_FIELD)
  1550. sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
  1551. assert(sl->mb_y < h->mb_height);
  1552. ret = ff_h264_build_ref_list(h, sl);
  1553. if (ret < 0)
  1554. return ret;
  1555. if (h->ps.pps->weighted_bipred_idc == 2 &&
  1556. sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1557. implicit_weight_table(h, sl, -1);
  1558. if (FRAME_MBAFF(h)) {
  1559. implicit_weight_table(h, sl, 0);
  1560. implicit_weight_table(h, sl, 1);
  1561. }
  1562. }
  1563. if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
  1564. ff_h264_direct_dist_scale_factor(h, sl);
  1565. ff_h264_direct_ref_list_init(h, sl);
  1566. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  1567. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  1568. sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1569. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  1570. sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1571. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  1572. nal->ref_idc == 0))
  1573. sl->deblocking_filter = 0;
  1574. if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) {
  1575. if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  1576. /* Cheat slightly for speed:
  1577. * Do not bother to deblock across slices. */
  1578. sl->deblocking_filter = 2;
  1579. } else {
  1580. h->postpone_filter = 1;
  1581. }
  1582. }
  1583. sl->qp_thresh = 15 -
  1584. FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
  1585. FFMAX3(0,
  1586. h->ps.pps->chroma_qp_index_offset[0],
  1587. h->ps.pps->chroma_qp_index_offset[1]) +
  1588. 6 * (h->ps.sps->bit_depth_luma - 8);
  1589. sl->slice_num = ++h->current_slice;
  1590. if (sl->slice_num >= MAX_SLICES) {
  1591. av_log(h->avctx, AV_LOG_ERROR,
  1592. "Too many slices, increase MAX_SLICES and recompile\n");
  1593. }
  1594. for (j = 0; j < 2; j++) {
  1595. int id_list[16];
  1596. int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
  1597. for (i = 0; i < 16; i++) {
  1598. id_list[i] = 60;
  1599. if (j < sl->list_count && i < sl->ref_count[j] &&
  1600. sl->ref_list[j][i].parent->f->buf[0]) {
  1601. int k;
  1602. AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
  1603. for (k = 0; k < h->short_ref_count; k++)
  1604. if (h->short_ref[k]->f->buf[0]->buffer == buf) {
  1605. id_list[i] = k;
  1606. break;
  1607. }
  1608. for (k = 0; k < h->long_ref_count; k++)
  1609. if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
  1610. id_list[i] = h->short_ref_count + k;
  1611. break;
  1612. }
  1613. }
  1614. }
  1615. ref2frm[0] =
  1616. ref2frm[1] = -1;
  1617. for (i = 0; i < 16; i++)
  1618. ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
  1619. ref2frm[18 + 0] =
  1620. ref2frm[18 + 1] = -1;
  1621. for (i = 16; i < 48; i++)
  1622. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  1623. (sl->ref_list[j][i].reference & 3);
  1624. }
  1625. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  1626. av_log(h->avctx, AV_LOG_DEBUG,
  1627. "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",
  1628. sl->slice_num,
  1629. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  1630. sl->mb_y * h->mb_width + sl->mb_x,
  1631. av_get_picture_type_char(sl->slice_type),
  1632. sl->slice_type_fixed ? " fix" : "",
  1633. nal->type == H264_NAL_IDR_SLICE ? " IDR" : "",
  1634. h->poc.frame_num,
  1635. h->cur_pic_ptr->field_poc[0],
  1636. h->cur_pic_ptr->field_poc[1],
  1637. sl->ref_count[0], sl->ref_count[1],
  1638. sl->qscale,
  1639. sl->deblocking_filter,
  1640. sl->slice_alpha_c0_offset, sl->slice_beta_offset,
  1641. sl->pwt.use_weight,
  1642. sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
  1643. sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  1644. }
  1645. return 0;
  1646. }
  1647. int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal)
  1648. {
  1649. H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
  1650. int ret;
  1651. sl->gb = nal->gb;
  1652. ret = h264_slice_header_parse(sl, nal, &h->ps, h->avctx);
  1653. if (ret < 0)
  1654. return ret;
  1655. // discard redundant pictures
  1656. if (sl->redundant_pic_count > 0)
  1657. return 0;
  1658. if (!h->setup_finished) {
  1659. if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
  1660. // this slice starts a new field
  1661. // first decode any pending queued slices
  1662. if (h->nb_slice_ctx_queued) {
  1663. H264SliceContext tmp_ctx;
  1664. ret = ff_h264_execute_decode_slices(h);
  1665. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1666. return ret;
  1667. memcpy(&tmp_ctx, h->slice_ctx, sizeof(tmp_ctx));
  1668. memcpy(h->slice_ctx, sl, sizeof(tmp_ctx));
  1669. memcpy(sl, &tmp_ctx, sizeof(tmp_ctx));
  1670. sl = h->slice_ctx;
  1671. }
  1672. if (h->field_started)
  1673. ff_h264_field_end(h, sl, 1);
  1674. h->current_slice = 0;
  1675. if (!h->first_field) {
  1676. if (h->cur_pic_ptr && !h->droppable) {
  1677. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1678. h->picture_structure == PICT_BOTTOM_FIELD);
  1679. }
  1680. h->cur_pic_ptr = NULL;
  1681. }
  1682. }
  1683. if (h->current_slice == 0) {
  1684. ret = h264_field_start(h, sl, nal);
  1685. if (ret < 0)
  1686. return ret;
  1687. h->field_started = 1;
  1688. }
  1689. }
  1690. ret = h264_slice_init(h, sl, nal);
  1691. if (ret < 0)
  1692. return ret;
  1693. if ((h->avctx->skip_frame < AVDISCARD_NONREF || nal->ref_idc) &&
  1694. (h->avctx->skip_frame < AVDISCARD_BIDIR ||
  1695. sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
  1696. (h->avctx->skip_frame < AVDISCARD_NONKEY ||
  1697. h->cur_pic_ptr->f->key_frame) &&
  1698. h->avctx->skip_frame < AVDISCARD_ALL) {
  1699. h->nb_slice_ctx_queued++;
  1700. }
  1701. return 0;
  1702. }
  1703. int ff_h264_get_slice_type(const H264SliceContext *sl)
  1704. {
  1705. switch (sl->slice_type) {
  1706. case AV_PICTURE_TYPE_P:
  1707. return 0;
  1708. case AV_PICTURE_TYPE_B:
  1709. return 1;
  1710. case AV_PICTURE_TYPE_I:
  1711. return 2;
  1712. case AV_PICTURE_TYPE_SP:
  1713. return 3;
  1714. case AV_PICTURE_TYPE_SI:
  1715. return 4;
  1716. default:
  1717. return AVERROR_INVALIDDATA;
  1718. }
  1719. }
  1720. static av_always_inline void fill_filter_caches_inter(const H264Context *h,
  1721. H264SliceContext *sl,
  1722. int mb_type, int top_xy,
  1723. int left_xy[LEFT_MBS],
  1724. int top_type,
  1725. int left_type[LEFT_MBS],
  1726. int mb_xy, int list)
  1727. {
  1728. int b_stride = h->b_stride;
  1729. int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
  1730. int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
  1731. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  1732. if (USES_LIST(top_type, list)) {
  1733. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  1734. const int b8_xy = 4 * top_xy + 2;
  1735. const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1736. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  1737. ref_cache[0 - 1 * 8] =
  1738. ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
  1739. ref_cache[2 - 1 * 8] =
  1740. ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
  1741. } else {
  1742. AV_ZERO128(mv_dst - 1 * 8);
  1743. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1744. }
  1745. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  1746. if (USES_LIST(left_type[LTOP], list)) {
  1747. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  1748. const int b8_xy = 4 * left_xy[LTOP] + 1;
  1749. const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1750. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  1751. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  1752. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  1753. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  1754. ref_cache[-1 + 0] =
  1755. ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  1756. ref_cache[-1 + 16] =
  1757. ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  1758. } else {
  1759. AV_ZERO32(mv_dst - 1 + 0);
  1760. AV_ZERO32(mv_dst - 1 + 8);
  1761. AV_ZERO32(mv_dst - 1 + 16);
  1762. AV_ZERO32(mv_dst - 1 + 24);
  1763. ref_cache[-1 + 0] =
  1764. ref_cache[-1 + 8] =
  1765. ref_cache[-1 + 16] =
  1766. ref_cache[-1 + 24] = LIST_NOT_USED;
  1767. }
  1768. }
  1769. }
  1770. if (!USES_LIST(mb_type, list)) {
  1771. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  1772. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1773. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1774. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1775. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1776. return;
  1777. }
  1778. {
  1779. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  1780. const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1781. uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
  1782. uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
  1783. AV_WN32A(&ref_cache[0 * 8], ref01);
  1784. AV_WN32A(&ref_cache[1 * 8], ref01);
  1785. AV_WN32A(&ref_cache[2 * 8], ref23);
  1786. AV_WN32A(&ref_cache[3 * 8], ref23);
  1787. }
  1788. {
  1789. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
  1790. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  1791. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  1792. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  1793. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  1794. }
  1795. }
  1796. /**
  1797. * @return non zero if the loop filter can be skipped
  1798. */
  1799. static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
  1800. {
  1801. const int mb_xy = sl->mb_xy;
  1802. int top_xy, left_xy[LEFT_MBS];
  1803. int top_type, left_type[LEFT_MBS];
  1804. uint8_t *nnz;
  1805. uint8_t *nnz_cache;
  1806. top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
  1807. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  1808. if (FRAME_MBAFF(h)) {
  1809. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  1810. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  1811. if (sl->mb_y & 1) {
  1812. if (left_mb_field_flag != curr_mb_field_flag)
  1813. left_xy[LTOP] -= h->mb_stride;
  1814. } else {
  1815. if (curr_mb_field_flag)
  1816. top_xy += h->mb_stride &
  1817. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  1818. if (left_mb_field_flag != curr_mb_field_flag)
  1819. left_xy[LBOT] += h->mb_stride;
  1820. }
  1821. }
  1822. sl->top_mb_xy = top_xy;
  1823. sl->left_mb_xy[LTOP] = left_xy[LTOP];
  1824. sl->left_mb_xy[LBOT] = left_xy[LBOT];
  1825. {
  1826. /* For sufficiently low qp, filtering wouldn't do anything.
  1827. * This is a conservative estimate: could also check beta_offset
  1828. * and more accurate chroma_qp. */
  1829. int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  1830. int qp = h->cur_pic.qscale_table[mb_xy];
  1831. if (qp <= qp_thresh &&
  1832. (left_xy[LTOP] < 0 ||
  1833. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  1834. (top_xy < 0 ||
  1835. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  1836. if (!FRAME_MBAFF(h))
  1837. return 1;
  1838. if ((left_xy[LTOP] < 0 ||
  1839. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  1840. (top_xy < h->mb_stride ||
  1841. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  1842. return 1;
  1843. }
  1844. }
  1845. top_type = h->cur_pic.mb_type[top_xy];
  1846. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  1847. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  1848. if (sl->deblocking_filter == 2) {
  1849. if (h->slice_table[top_xy] != sl->slice_num)
  1850. top_type = 0;
  1851. if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
  1852. left_type[LTOP] = left_type[LBOT] = 0;
  1853. } else {
  1854. if (h->slice_table[top_xy] == 0xFFFF)
  1855. top_type = 0;
  1856. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  1857. left_type[LTOP] = left_type[LBOT] = 0;
  1858. }
  1859. sl->top_type = top_type;
  1860. sl->left_type[LTOP] = left_type[LTOP];
  1861. sl->left_type[LBOT] = left_type[LBOT];
  1862. if (IS_INTRA(mb_type))
  1863. return 0;
  1864. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1865. top_type, left_type, mb_xy, 0);
  1866. if (sl->list_count == 2)
  1867. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1868. top_type, left_type, mb_xy, 1);
  1869. nnz = h->non_zero_count[mb_xy];
  1870. nnz_cache = sl->non_zero_count_cache;
  1871. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  1872. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  1873. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  1874. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  1875. sl->cbp = h->cbp_table[mb_xy];
  1876. if (top_type) {
  1877. nnz = h->non_zero_count[top_xy];
  1878. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  1879. }
  1880. if (left_type[LTOP]) {
  1881. nnz = h->non_zero_count[left_xy[LTOP]];
  1882. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  1883. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  1884. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  1885. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  1886. }
  1887. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  1888. * from what the loop filter needs */
  1889. if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
  1890. if (IS_8x8DCT(top_type)) {
  1891. nnz_cache[4 + 8 * 0] =
  1892. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  1893. nnz_cache[6 + 8 * 0] =
  1894. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  1895. }
  1896. if (IS_8x8DCT(left_type[LTOP])) {
  1897. nnz_cache[3 + 8 * 1] =
  1898. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  1899. }
  1900. if (IS_8x8DCT(left_type[LBOT])) {
  1901. nnz_cache[3 + 8 * 3] =
  1902. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  1903. }
  1904. if (IS_8x8DCT(mb_type)) {
  1905. nnz_cache[scan8[0]] =
  1906. nnz_cache[scan8[1]] =
  1907. nnz_cache[scan8[2]] =
  1908. nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
  1909. nnz_cache[scan8[0 + 4]] =
  1910. nnz_cache[scan8[1 + 4]] =
  1911. nnz_cache[scan8[2 + 4]] =
  1912. nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
  1913. nnz_cache[scan8[0 + 8]] =
  1914. nnz_cache[scan8[1 + 8]] =
  1915. nnz_cache[scan8[2 + 8]] =
  1916. nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
  1917. nnz_cache[scan8[0 + 12]] =
  1918. nnz_cache[scan8[1 + 12]] =
  1919. nnz_cache[scan8[2 + 12]] =
  1920. nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
  1921. }
  1922. }
  1923. return 0;
  1924. }
  1925. static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
  1926. {
  1927. uint8_t *dest_y, *dest_cb, *dest_cr;
  1928. int linesize, uvlinesize, mb_x, mb_y;
  1929. const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
  1930. const int old_slice_type = sl->slice_type;
  1931. const int pixel_shift = h->pixel_shift;
  1932. const int block_h = 16 >> h->chroma_y_shift;
  1933. if (h->postpone_filter)
  1934. return;
  1935. if (sl->deblocking_filter) {
  1936. for (mb_x = start_x; mb_x < end_x; mb_x++)
  1937. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  1938. int mb_xy, mb_type;
  1939. mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
  1940. mb_type = h->cur_pic.mb_type[mb_xy];
  1941. if (FRAME_MBAFF(h))
  1942. sl->mb_mbaff =
  1943. sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  1944. sl->mb_x = mb_x;
  1945. sl->mb_y = mb_y;
  1946. dest_y = h->cur_pic.f->data[0] +
  1947. ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
  1948. dest_cb = h->cur_pic.f->data[1] +
  1949. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1950. mb_y * sl->uvlinesize * block_h;
  1951. dest_cr = h->cur_pic.f->data[2] +
  1952. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1953. mb_y * sl->uvlinesize * block_h;
  1954. // FIXME simplify above
  1955. if (MB_FIELD(sl)) {
  1956. linesize = sl->mb_linesize = sl->linesize * 2;
  1957. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
  1958. if (mb_y & 1) { // FIXME move out of this function?
  1959. dest_y -= sl->linesize * 15;
  1960. dest_cb -= sl->uvlinesize * (block_h - 1);
  1961. dest_cr -= sl->uvlinesize * (block_h - 1);
  1962. }
  1963. } else {
  1964. linesize = sl->mb_linesize = sl->linesize;
  1965. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
  1966. }
  1967. backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
  1968. uvlinesize, 0);
  1969. if (fill_filter_caches(h, sl, mb_type))
  1970. continue;
  1971. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
  1972. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]);
  1973. if (FRAME_MBAFF(h)) {
  1974. ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  1975. linesize, uvlinesize);
  1976. } else {
  1977. ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
  1978. dest_cr, linesize, uvlinesize);
  1979. }
  1980. }
  1981. }
  1982. sl->slice_type = old_slice_type;
  1983. sl->mb_x = end_x;
  1984. sl->mb_y = end_mb_y - FRAME_MBAFF(h);
  1985. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
  1986. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
  1987. }
  1988. static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
  1989. {
  1990. const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
  1991. int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
  1992. h->cur_pic.mb_type[mb_xy - 1] :
  1993. (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
  1994. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  1995. sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  1996. }
  1997. /**
  1998. * Draw edges and report progress for the last MB row.
  1999. */
  2000. static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
  2001. {
  2002. int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
  2003. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  2004. int height = 16 << FRAME_MBAFF(h);
  2005. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  2006. if (sl->deblocking_filter) {
  2007. if ((top + height) >= pic_height)
  2008. height += deblock_border;
  2009. top -= deblock_border;
  2010. }
  2011. if (top >= pic_height || (top + height) < 0)
  2012. return;
  2013. height = FFMIN(height, pic_height - top);
  2014. if (top < 0) {
  2015. height = top + height;
  2016. top = 0;
  2017. }
  2018. ff_h264_draw_horiz_band(h, sl, top, height);
  2019. if (h->droppable)
  2020. return;
  2021. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  2022. h->picture_structure == PICT_BOTTOM_FIELD);
  2023. }
  2024. static void er_add_slice(H264SliceContext *sl,
  2025. int startx, int starty,
  2026. int endx, int endy, int status)
  2027. {
  2028. #if CONFIG_ERROR_RESILIENCE
  2029. ERContext *er = &sl->er;
  2030. if (!sl->h264->enable_er)
  2031. return;
  2032. er->ref_count = sl->ref_count[0];
  2033. ff_er_add_slice(er, startx, starty, endx, endy, status);
  2034. #endif
  2035. }
  2036. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  2037. {
  2038. H264SliceContext *sl = arg;
  2039. const H264Context *h = sl->h264;
  2040. int lf_x_start = sl->mb_x;
  2041. int orig_deblock = sl->deblocking_filter;
  2042. int ret;
  2043. sl->linesize = h->cur_pic_ptr->f->linesize[0];
  2044. sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
  2045. ret = alloc_scratch_buffers(sl, sl->linesize);
  2046. if (ret < 0)
  2047. return ret;
  2048. sl->mb_skip_run = -1;
  2049. if (h->postpone_filter)
  2050. sl->deblocking_filter = 0;
  2051. sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  2052. (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
  2053. if (h->ps.pps->cabac) {
  2054. /* realign */
  2055. align_get_bits(&sl->gb);
  2056. /* init cabac */
  2057. ff_init_cabac_decoder(&sl->cabac,
  2058. sl->gb.buffer + get_bits_count(&sl->gb) / 8,
  2059. (get_bits_left(&sl->gb) + 7) / 8);
  2060. ff_h264_init_cabac_states(h, sl);
  2061. for (;;) {
  2062. // START_TIMER
  2063. int ret, eos;
  2064. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2065. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2066. sl->next_slice_idx);
  2067. return AVERROR_INVALIDDATA;
  2068. }
  2069. ret = ff_h264_decode_mb_cabac(h, sl);
  2070. // STOP_TIMER("decode_mb_cabac")
  2071. if (ret >= 0)
  2072. ff_h264_hl_decode_mb(h, sl);
  2073. // FIXME optimal? or let mb_decode decode 16x32 ?
  2074. if (ret >= 0 && FRAME_MBAFF(h)) {
  2075. sl->mb_y++;
  2076. ret = ff_h264_decode_mb_cabac(h, sl);
  2077. if (ret >= 0)
  2078. ff_h264_hl_decode_mb(h, sl);
  2079. sl->mb_y--;
  2080. }
  2081. eos = get_cabac_terminate(&sl->cabac);
  2082. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  2083. sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  2084. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2085. sl->mb_y, ER_MB_END);
  2086. if (sl->mb_x >= lf_x_start)
  2087. loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
  2088. goto finish;
  2089. }
  2090. if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  2091. av_log(h->avctx, AV_LOG_ERROR,
  2092. "error while decoding MB %d %d, bytestream %td\n",
  2093. sl->mb_x, sl->mb_y,
  2094. sl->cabac.bytestream_end - sl->cabac.bytestream);
  2095. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2096. sl->mb_y, ER_MB_ERROR);
  2097. return AVERROR_INVALIDDATA;
  2098. }
  2099. if (++sl->mb_x >= h->mb_width) {
  2100. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2101. sl->mb_x = lf_x_start = 0;
  2102. decode_finish_row(h, sl);
  2103. ++sl->mb_y;
  2104. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2105. ++sl->mb_y;
  2106. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2107. predict_field_decoding_flag(h, sl);
  2108. }
  2109. }
  2110. if (eos || sl->mb_y >= h->mb_height) {
  2111. ff_tlog(h->avctx, "slice end %d %d\n",
  2112. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2113. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2114. sl->mb_y, ER_MB_END);
  2115. if (sl->mb_x > lf_x_start)
  2116. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2117. goto finish;
  2118. }
  2119. }
  2120. } else {
  2121. for (;;) {
  2122. int ret;
  2123. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2124. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2125. sl->next_slice_idx);
  2126. return AVERROR_INVALIDDATA;
  2127. }
  2128. ret = ff_h264_decode_mb_cavlc(h, sl);
  2129. if (ret >= 0)
  2130. ff_h264_hl_decode_mb(h, sl);
  2131. // FIXME optimal? or let mb_decode decode 16x32 ?
  2132. if (ret >= 0 && FRAME_MBAFF(h)) {
  2133. sl->mb_y++;
  2134. ret = ff_h264_decode_mb_cavlc(h, sl);
  2135. if (ret >= 0)
  2136. ff_h264_hl_decode_mb(h, sl);
  2137. sl->mb_y--;
  2138. }
  2139. if (ret < 0) {
  2140. av_log(h->avctx, AV_LOG_ERROR,
  2141. "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
  2142. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2143. sl->mb_y, ER_MB_ERROR);
  2144. return ret;
  2145. }
  2146. if (++sl->mb_x >= h->mb_width) {
  2147. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2148. sl->mb_x = lf_x_start = 0;
  2149. decode_finish_row(h, sl);
  2150. ++sl->mb_y;
  2151. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2152. ++sl->mb_y;
  2153. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2154. predict_field_decoding_flag(h, sl);
  2155. }
  2156. if (sl->mb_y >= h->mb_height) {
  2157. ff_tlog(h->avctx, "slice end %d %d\n",
  2158. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2159. if (get_bits_left(&sl->gb) == 0) {
  2160. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2161. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2162. goto finish;
  2163. } else {
  2164. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2165. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2166. return AVERROR_INVALIDDATA;
  2167. }
  2168. }
  2169. }
  2170. if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
  2171. ff_tlog(h->avctx, "slice end %d %d\n",
  2172. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2173. if (get_bits_left(&sl->gb) == 0) {
  2174. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2175. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2176. if (sl->mb_x > lf_x_start)
  2177. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2178. goto finish;
  2179. } else {
  2180. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2181. sl->mb_y, ER_MB_ERROR);
  2182. return AVERROR_INVALIDDATA;
  2183. }
  2184. }
  2185. }
  2186. }
  2187. finish:
  2188. sl->deblocking_filter = orig_deblock;
  2189. return 0;
  2190. }
  2191. /**
  2192. * Call decode_slice() for each context.
  2193. *
  2194. * @param h h264 master context
  2195. */
  2196. int ff_h264_execute_decode_slices(H264Context *h)
  2197. {
  2198. AVCodecContext *const avctx = h->avctx;
  2199. H264SliceContext *sl;
  2200. int context_count = h->nb_slice_ctx_queued;
  2201. int ret = 0;
  2202. int i, j;
  2203. if (h->avctx->hwaccel || context_count < 1)
  2204. return 0;
  2205. if (context_count == 1) {
  2206. h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
  2207. h->postpone_filter = 0;
  2208. ret = decode_slice(avctx, &h->slice_ctx[0]);
  2209. h->mb_y = h->slice_ctx[0].mb_y;
  2210. if (ret < 0)
  2211. goto finish;
  2212. } else {
  2213. for (i = 0; i < context_count; i++) {
  2214. int next_slice_idx = h->mb_width * h->mb_height;
  2215. int slice_idx;
  2216. sl = &h->slice_ctx[i];
  2217. sl->er.error_count = 0;
  2218. /* make sure none of those slices overlap */
  2219. slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
  2220. for (j = 0; j < context_count; j++) {
  2221. H264SliceContext *sl2 = &h->slice_ctx[j];
  2222. int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
  2223. if (i == j || slice_idx2 < slice_idx)
  2224. continue;
  2225. next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
  2226. }
  2227. sl->next_slice_idx = next_slice_idx;
  2228. }
  2229. avctx->execute(avctx, decode_slice, h->slice_ctx,
  2230. NULL, context_count, sizeof(h->slice_ctx[0]));
  2231. /* pull back stuff from slices to master context */
  2232. sl = &h->slice_ctx[context_count - 1];
  2233. h->mb_y = sl->mb_y;
  2234. for (i = 1; i < context_count; i++)
  2235. h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count;
  2236. if (h->postpone_filter) {
  2237. h->postpone_filter = 0;
  2238. for (i = 0; i < context_count; i++) {
  2239. int y_end, x_end;
  2240. sl = &h->slice_ctx[i];
  2241. y_end = FFMIN(sl->mb_y + 1, h->mb_height);
  2242. x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
  2243. for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
  2244. sl->mb_y = j;
  2245. loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
  2246. j == y_end - 1 ? x_end : h->mb_width);
  2247. }
  2248. }
  2249. }
  2250. }
  2251. finish:
  2252. h->nb_slice_ctx_queued = 0;
  2253. return ret;
  2254. }