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.

2181 lines
81KB

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