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.

2504 lines
96KB

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