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.

2483 lines
95KB

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