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.

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