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.

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