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 pps_id;
  792. int ret;
  793. unsigned int slice_type, tmp, i;
  794. int last_pic_structure, last_pic_droppable;
  795. int needs_reinit = 0;
  796. int field_pic_flag, bottom_field_flag;
  797. int frame_num, droppable, picture_structure;
  798. int mb_aff_frame = 0;
  799. sl->first_mb_addr = get_ue_golomb(&sl->gb);
  800. if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
  801. if (h->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
  802. ff_h264_field_end(h, sl, 1);
  803. }
  804. h->current_slice = 0;
  805. if (!h->first_field) {
  806. if (h->cur_pic_ptr && !h->droppable) {
  807. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  808. h->picture_structure == PICT_BOTTOM_FIELD);
  809. }
  810. h->cur_pic_ptr = NULL;
  811. }
  812. }
  813. slice_type = get_ue_golomb_31(&sl->gb);
  814. if (slice_type > 9) {
  815. av_log(h->avctx, AV_LOG_ERROR,
  816. "slice type %d too large at %d\n",
  817. slice_type, sl->first_mb_addr);
  818. return AVERROR_INVALIDDATA;
  819. }
  820. if (slice_type > 4) {
  821. slice_type -= 5;
  822. sl->slice_type_fixed = 1;
  823. } else
  824. sl->slice_type_fixed = 0;
  825. slice_type = ff_h264_golomb_to_pict_type[slice_type];
  826. sl->slice_type = slice_type;
  827. sl->slice_type_nos = slice_type & 3;
  828. if (h->nal_unit_type == NAL_IDR_SLICE &&
  829. sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  830. av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
  831. return AVERROR_INVALIDDATA;
  832. }
  833. pps_id = get_ue_golomb(&sl->gb);
  834. if (pps_id >= MAX_PPS_COUNT) {
  835. av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
  836. return AVERROR_INVALIDDATA;
  837. }
  838. if (!h->ps.pps_list[pps_id]) {
  839. av_log(h->avctx, AV_LOG_ERROR,
  840. "non-existing PPS %u referenced\n",
  841. pps_id);
  842. return AVERROR_INVALIDDATA;
  843. }
  844. if (!h->setup_finished) {
  845. h->ps.pps = (const PPS*)h->ps.pps_list[pps_id]->data;
  846. } else if (h->ps.pps != (const PPS*)h->ps.pps_list[pps_id]->data) {
  847. av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
  848. return AVERROR_INVALIDDATA;
  849. }
  850. if (!h->ps.sps_list[h->ps.pps->sps_id]) {
  851. av_log(h->avctx, AV_LOG_ERROR,
  852. "non-existing SPS %u referenced\n",
  853. h->ps.pps->sps_id);
  854. return AVERROR_INVALIDDATA;
  855. }
  856. if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
  857. h->ps.sps = (SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data;
  858. if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
  859. h->chroma_format_idc != h->ps.sps->chroma_format_idc)
  860. needs_reinit = 1;
  861. }
  862. pps = h->ps.pps;
  863. sps = h->ps.sps;
  864. if (!h->setup_finished) {
  865. h->avctx->profile = ff_h264_get_profile(sps);
  866. h->avctx->level = sps->level_idc;
  867. h->avctx->refs = sps->ref_frame_count;
  868. if (h->mb_width != sps->mb_width ||
  869. h->mb_height != sps->mb_height * (2 - sps->frame_mbs_only_flag))
  870. needs_reinit = 1;
  871. h->mb_width = sps->mb_width;
  872. h->mb_height = sps->mb_height * (2 - sps->frame_mbs_only_flag);
  873. h->mb_num = h->mb_width * h->mb_height;
  874. h->mb_stride = h->mb_width + 1;
  875. h->b_stride = h->mb_width * 4;
  876. h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
  877. h->width = 16 * h->mb_width;
  878. h->height = 16 * h->mb_height;
  879. ret = init_dimensions(h);
  880. if (ret < 0)
  881. return ret;
  882. if (sps->video_signal_type_present_flag) {
  883. h->avctx->color_range = sps->full_range ? AVCOL_RANGE_JPEG
  884. : AVCOL_RANGE_MPEG;
  885. if (sps->colour_description_present_flag) {
  886. if (h->avctx->colorspace != sps->colorspace)
  887. needs_reinit = 1;
  888. h->avctx->color_primaries = sps->color_primaries;
  889. h->avctx->color_trc = sps->color_trc;
  890. h->avctx->colorspace = sps->colorspace;
  891. }
  892. }
  893. }
  894. if (h->context_initialized && needs_reinit) {
  895. h->context_initialized = 0;
  896. if (sl != h->slice_ctx) {
  897. av_log(h->avctx, AV_LOG_ERROR,
  898. "changing width %d -> %d / height %d -> %d on "
  899. "slice %d\n",
  900. h->width, h->avctx->coded_width,
  901. h->height, h->avctx->coded_height,
  902. h->current_slice + 1);
  903. return AVERROR_INVALIDDATA;
  904. }
  905. ff_h264_flush_change(h);
  906. if ((ret = get_pixel_format(h)) < 0)
  907. return ret;
  908. h->avctx->pix_fmt = ret;
  909. av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
  910. "pix_fmt: %d\n", h->width, h->height, h->avctx->pix_fmt);
  911. if ((ret = h264_slice_header_init(h)) < 0) {
  912. av_log(h->avctx, AV_LOG_ERROR,
  913. "h264_slice_header_init() failed\n");
  914. return ret;
  915. }
  916. }
  917. if (!h->context_initialized) {
  918. if (sl != h->slice_ctx) {
  919. av_log(h->avctx, AV_LOG_ERROR,
  920. "Cannot (re-)initialize context during parallel decoding.\n");
  921. return AVERROR_PATCHWELCOME;
  922. }
  923. if ((ret = get_pixel_format(h)) < 0)
  924. return ret;
  925. h->avctx->pix_fmt = ret;
  926. if ((ret = h264_slice_header_init(h)) < 0) {
  927. av_log(h->avctx, AV_LOG_ERROR,
  928. "h264_slice_header_init() failed\n");
  929. return ret;
  930. }
  931. }
  932. frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
  933. if (!h->setup_finished)
  934. h->poc.frame_num = frame_num;
  935. sl->mb_mbaff = 0;
  936. last_pic_structure = h->picture_structure;
  937. last_pic_droppable = h->droppable;
  938. droppable = h->nal_ref_idc == 0;
  939. if (sps->frame_mbs_only_flag) {
  940. picture_structure = PICT_FRAME;
  941. } else {
  942. field_pic_flag = get_bits1(&sl->gb);
  943. if (field_pic_flag) {
  944. bottom_field_flag = get_bits1(&sl->gb);
  945. picture_structure = PICT_TOP_FIELD + bottom_field_flag;
  946. } else {
  947. picture_structure = PICT_FRAME;
  948. mb_aff_frame = sps->mb_aff;
  949. }
  950. }
  951. if (!h->setup_finished) {
  952. h->droppable = droppable;
  953. h->picture_structure = picture_structure;
  954. h->mb_aff_frame = mb_aff_frame;
  955. }
  956. sl->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
  957. if (h->current_slice != 0) {
  958. if (last_pic_structure != picture_structure ||
  959. last_pic_droppable != droppable) {
  960. av_log(h->avctx, AV_LOG_ERROR,
  961. "Changing field mode (%d -> %d) between slices is not allowed\n",
  962. last_pic_structure, h->picture_structure);
  963. return AVERROR_INVALIDDATA;
  964. } else if (!h->cur_pic_ptr) {
  965. av_log(h->avctx, AV_LOG_ERROR,
  966. "unset cur_pic_ptr on slice %d\n",
  967. h->current_slice + 1);
  968. return AVERROR_INVALIDDATA;
  969. }
  970. } else {
  971. /* Shorten frame num gaps so we don't have to allocate reference
  972. * frames just to throw them away */
  973. if (h->poc.frame_num != h->poc.prev_frame_num) {
  974. int unwrap_prev_frame_num = h->poc.prev_frame_num;
  975. int max_frame_num = 1 << sps->log2_max_frame_num;
  976. if (unwrap_prev_frame_num > h->poc.frame_num)
  977. unwrap_prev_frame_num -= max_frame_num;
  978. if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
  979. unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
  980. if (unwrap_prev_frame_num < 0)
  981. unwrap_prev_frame_num += max_frame_num;
  982. h->poc.prev_frame_num = unwrap_prev_frame_num;
  983. }
  984. }
  985. /* See if we have a decoded first field looking for a pair...
  986. * Here, we're using that to see if we should mark previously
  987. * decode frames as "finished".
  988. * We have to do that before the "dummy" in-between frame allocation,
  989. * since that can modify s->current_picture_ptr. */
  990. if (h->first_field) {
  991. assert(h->cur_pic_ptr);
  992. assert(h->cur_pic_ptr->f->buf[0]);
  993. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  994. /* figure out if we have a complementary field pair */
  995. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  996. /* Previous field is unmatched. Don't display it, but let it
  997. * remain for reference if marked as such. */
  998. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  999. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1000. last_pic_structure == PICT_TOP_FIELD);
  1001. }
  1002. } else {
  1003. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1004. /* This and previous field were reference, but had
  1005. * different frame_nums. Consider this field first in
  1006. * pair. Throw away previous field except for reference
  1007. * purposes. */
  1008. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  1009. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1010. last_pic_structure == PICT_TOP_FIELD);
  1011. }
  1012. } else {
  1013. /* Second field in complementary pair */
  1014. if (!((last_pic_structure == PICT_TOP_FIELD &&
  1015. h->picture_structure == PICT_BOTTOM_FIELD) ||
  1016. (last_pic_structure == PICT_BOTTOM_FIELD &&
  1017. h->picture_structure == PICT_TOP_FIELD))) {
  1018. av_log(h->avctx, AV_LOG_ERROR,
  1019. "Invalid field mode combination %d/%d\n",
  1020. last_pic_structure, h->picture_structure);
  1021. h->picture_structure = last_pic_structure;
  1022. h->droppable = last_pic_droppable;
  1023. return AVERROR_INVALIDDATA;
  1024. } else if (last_pic_droppable != h->droppable) {
  1025. avpriv_request_sample(h->avctx,
  1026. "Found reference and non-reference fields in the same frame, which");
  1027. h->picture_structure = last_pic_structure;
  1028. h->droppable = last_pic_droppable;
  1029. return AVERROR_PATCHWELCOME;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. while (h->poc.frame_num != h->poc.prev_frame_num &&
  1035. h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
  1036. H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1037. av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  1038. h->poc.frame_num, h->poc.prev_frame_num);
  1039. ret = initialize_cur_frame(h);
  1040. if (ret < 0) {
  1041. h->first_field = 0;
  1042. return ret;
  1043. }
  1044. h->poc.prev_frame_num++;
  1045. h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
  1046. h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
  1047. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  1048. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  1049. ret = ff_generate_sliding_window_mmcos(h, 1);
  1050. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1051. return ret;
  1052. ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1053. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1054. return ret;
  1055. /* Error concealment: If a ref is missing, copy the previous ref
  1056. * in its place.
  1057. * FIXME: Avoiding a memcpy would be nice, but ref handling makes
  1058. * many assumptions about there being no actual duplicates.
  1059. * FIXME: This does not copy padding for out-of-frame motion
  1060. * vectors. Given we are concealing a lost frame, this probably
  1061. * is not noticeable by comparison, but it should be fixed. */
  1062. if (h->short_ref_count) {
  1063. if (prev &&
  1064. h->short_ref[0]->f->width == prev->f->width &&
  1065. h->short_ref[0]->f->height == prev->f->height &&
  1066. h->short_ref[0]->f->format == prev->f->format) {
  1067. av_image_copy(h->short_ref[0]->f->data,
  1068. h->short_ref[0]->f->linesize,
  1069. (const uint8_t **)prev->f->data,
  1070. prev->f->linesize,
  1071. prev->f->format,
  1072. h->mb_width * 16,
  1073. h->mb_height * 16);
  1074. h->short_ref[0]->poc = prev->poc + 2;
  1075. }
  1076. h->short_ref[0]->frame_num = h->poc.prev_frame_num;
  1077. }
  1078. }
  1079. /* See if we have a decoded first field looking for a pair...
  1080. * We're using that to see whether to continue decoding in that
  1081. * frame, or to allocate a new one. */
  1082. if (h->first_field) {
  1083. assert(h->cur_pic_ptr);
  1084. assert(h->cur_pic_ptr->f->buf[0]);
  1085. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1086. /* figure out if we have a complementary field pair */
  1087. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1088. /* Previous field is unmatched. Don't display it, but let it
  1089. * remain for reference if marked as such. */
  1090. h->cur_pic_ptr = NULL;
  1091. h->first_field = FIELD_PICTURE(h);
  1092. } else {
  1093. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1094. /* This and the previous field had different frame_nums.
  1095. * Consider this field first in pair. Throw away previous
  1096. * one except for reference purposes. */
  1097. h->first_field = 1;
  1098. h->cur_pic_ptr = NULL;
  1099. } else {
  1100. /* Second field in complementary pair */
  1101. h->first_field = 0;
  1102. }
  1103. }
  1104. } else {
  1105. /* Frame or first field in a potentially complementary pair */
  1106. h->first_field = FIELD_PICTURE(h);
  1107. }
  1108. if (!FIELD_PICTURE(h) || h->first_field) {
  1109. if (h264_frame_start(h) < 0) {
  1110. h->first_field = 0;
  1111. return AVERROR_INVALIDDATA;
  1112. }
  1113. } else {
  1114. release_unused_pictures(h, 0);
  1115. }
  1116. }
  1117. if (h->picture_structure == PICT_FRAME) {
  1118. h->curr_pic_num = h->poc.frame_num;
  1119. h->max_pic_num = 1 << sps->log2_max_frame_num;
  1120. } else {
  1121. h->curr_pic_num = 2 * h->poc.frame_num + 1;
  1122. h->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
  1123. }
  1124. if (h->nal_unit_type == NAL_IDR_SLICE)
  1125. get_ue_golomb(&sl->gb); /* idr_pic_id */
  1126. if (sps->poc_type == 0) {
  1127. int poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
  1128. if (!h->setup_finished)
  1129. h->poc.poc_lsb = poc_lsb;
  1130. if (pps->pic_order_present == 1 && h->picture_structure == PICT_FRAME) {
  1131. int delta_poc_bottom = get_se_golomb(&sl->gb);
  1132. if (!h->setup_finished)
  1133. h->poc.delta_poc_bottom = delta_poc_bottom;
  1134. }
  1135. }
  1136. if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
  1137. int delta_poc = get_se_golomb(&sl->gb);
  1138. if (!h->setup_finished)
  1139. h->poc.delta_poc[0] = delta_poc;
  1140. if (pps->pic_order_present == 1 && h->picture_structure == PICT_FRAME) {
  1141. delta_poc = get_se_golomb(&sl->gb);
  1142. if (!h->setup_finished)
  1143. h->poc.delta_poc[1] = delta_poc;
  1144. }
  1145. }
  1146. if (pps->redundant_pic_cnt_present)
  1147. sl->redundant_pic_count = get_ue_golomb(&sl->gb);
  1148. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1149. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1150. ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
  1151. &sl->gb, pps, sl->slice_type_nos,
  1152. h->picture_structure);
  1153. if (ret < 0)
  1154. return ret;
  1155. if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1156. ret = ff_h264_decode_ref_pic_list_reordering(h, sl);
  1157. if (ret < 0) {
  1158. sl->ref_count[1] = sl->ref_count[0] = 0;
  1159. return ret;
  1160. }
  1161. }
  1162. sl->pwt.use_weight = 0;
  1163. for (i = 0; i < 2; i++) {
  1164. sl->pwt.luma_weight_flag[i] = 0;
  1165. sl->pwt.chroma_weight_flag[i] = 0;
  1166. }
  1167. if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
  1168. (pps->weighted_bipred_idc == 1 &&
  1169. sl->slice_type_nos == AV_PICTURE_TYPE_B))
  1170. ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
  1171. sl->slice_type_nos, &sl->pwt);
  1172. // If frame-mt is enabled, only update mmco tables for the first slice
  1173. // in a field. Subsequent slices can temporarily clobber h->mmco_index
  1174. // or h->mmco, which will cause ref list mix-ups and decoding errors
  1175. // further down the line. This may break decoding if the first slice is
  1176. // corrupt, thus we only do this if frame-mt is enabled.
  1177. if (h->nal_ref_idc) {
  1178. ret = ff_h264_decode_ref_pic_marking(h, &sl->gb,
  1179. !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
  1180. h->current_slice == 0);
  1181. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1182. return AVERROR_INVALIDDATA;
  1183. }
  1184. if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
  1185. tmp = get_ue_golomb_31(&sl->gb);
  1186. if (tmp > 2) {
  1187. av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
  1188. return AVERROR_INVALIDDATA;
  1189. }
  1190. sl->cabac_init_idc = tmp;
  1191. }
  1192. sl->last_qscale_diff = 0;
  1193. tmp = pps->init_qp + get_se_golomb(&sl->gb);
  1194. if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
  1195. av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1196. return AVERROR_INVALIDDATA;
  1197. }
  1198. sl->qscale = tmp;
  1199. sl->chroma_qp[0] = get_chroma_qp(h, 0, sl->qscale);
  1200. sl->chroma_qp[1] = get_chroma_qp(h, 1, sl->qscale);
  1201. // FIXME qscale / qp ... stuff
  1202. if (sl->slice_type == AV_PICTURE_TYPE_SP)
  1203. get_bits1(&sl->gb); /* sp_for_switch_flag */
  1204. if (sl->slice_type == AV_PICTURE_TYPE_SP ||
  1205. sl->slice_type == AV_PICTURE_TYPE_SI)
  1206. get_se_golomb(&sl->gb); /* slice_qs_delta */
  1207. sl->deblocking_filter = 1;
  1208. sl->slice_alpha_c0_offset = 0;
  1209. sl->slice_beta_offset = 0;
  1210. if (pps->deblocking_filter_parameters_present) {
  1211. tmp = get_ue_golomb_31(&sl->gb);
  1212. if (tmp > 2) {
  1213. av_log(h->avctx, AV_LOG_ERROR,
  1214. "deblocking_filter_idc %u out of range\n", tmp);
  1215. return AVERROR_INVALIDDATA;
  1216. }
  1217. sl->deblocking_filter = tmp;
  1218. if (sl->deblocking_filter < 2)
  1219. sl->deblocking_filter ^= 1; // 1<->0
  1220. if (sl->deblocking_filter) {
  1221. sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
  1222. sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;
  1223. if (sl->slice_alpha_c0_offset > 12 ||
  1224. sl->slice_alpha_c0_offset < -12 ||
  1225. sl->slice_beta_offset > 12 ||
  1226. sl->slice_beta_offset < -12) {
  1227. av_log(h->avctx, AV_LOG_ERROR,
  1228. "deblocking filter parameters %d %d out of range\n",
  1229. sl->slice_alpha_c0_offset, sl->slice_beta_offset);
  1230. return AVERROR_INVALIDDATA;
  1231. }
  1232. }
  1233. }
  1234. return 0;
  1235. }
  1236. /**
  1237. * Decode a slice header.
  1238. * This will (re)initialize the decoder and call h264_frame_start() as needed.
  1239. *
  1240. * @param h h264context
  1241. *
  1242. * @return 0 if okay, <0 if an error occurred
  1243. */
  1244. int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl)
  1245. {
  1246. int i, j, ret = 0;
  1247. ret = h264_slice_header_parse(h, sl);
  1248. if (ret < 0)
  1249. return ret;
  1250. assert(h->mb_num == h->mb_width * h->mb_height);
  1251. if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  1252. sl->first_mb_addr >= h->mb_num) {
  1253. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1254. return AVERROR_INVALIDDATA;
  1255. }
  1256. sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
  1257. sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
  1258. FIELD_OR_MBAFF_PICTURE(h);
  1259. if (h->picture_structure == PICT_BOTTOM_FIELD)
  1260. sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
  1261. assert(sl->mb_y < h->mb_height);
  1262. if (!h->setup_finished)
  1263. ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
  1264. h->ps.sps, &h->poc, h->picture_structure, h->nal_ref_idc);
  1265. ret = ff_h264_build_ref_list(h, sl);
  1266. if (ret < 0)
  1267. return ret;
  1268. if (h->ps.pps->weighted_bipred_idc == 2 &&
  1269. sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1270. implicit_weight_table(h, sl, -1);
  1271. if (FRAME_MBAFF(h)) {
  1272. implicit_weight_table(h, sl, 0);
  1273. implicit_weight_table(h, sl, 1);
  1274. }
  1275. }
  1276. if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
  1277. ff_h264_direct_dist_scale_factor(h, sl);
  1278. ff_h264_direct_ref_list_init(h, sl);
  1279. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  1280. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  1281. sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1282. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  1283. sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1284. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  1285. h->nal_ref_idc == 0))
  1286. sl->deblocking_filter = 0;
  1287. if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) {
  1288. if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  1289. /* Cheat slightly for speed:
  1290. * Do not bother to deblock across slices. */
  1291. sl->deblocking_filter = 2;
  1292. } else {
  1293. h->postpone_filter = 1;
  1294. }
  1295. }
  1296. sl->qp_thresh = 15 -
  1297. FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
  1298. FFMAX3(0,
  1299. h->ps.pps->chroma_qp_index_offset[0],
  1300. h->ps.pps->chroma_qp_index_offset[1]) +
  1301. 6 * (h->ps.sps->bit_depth_luma - 8);
  1302. sl->slice_num = ++h->current_slice;
  1303. if (sl->slice_num >= MAX_SLICES) {
  1304. av_log(h->avctx, AV_LOG_ERROR,
  1305. "Too many slices, increase MAX_SLICES and recompile\n");
  1306. }
  1307. for (j = 0; j < 2; j++) {
  1308. int id_list[16];
  1309. int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
  1310. for (i = 0; i < 16; i++) {
  1311. id_list[i] = 60;
  1312. if (j < sl->list_count && i < sl->ref_count[j] &&
  1313. sl->ref_list[j][i].parent->f->buf[0]) {
  1314. int k;
  1315. AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
  1316. for (k = 0; k < h->short_ref_count; k++)
  1317. if (h->short_ref[k]->f->buf[0]->buffer == buf) {
  1318. id_list[i] = k;
  1319. break;
  1320. }
  1321. for (k = 0; k < h->long_ref_count; k++)
  1322. if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
  1323. id_list[i] = h->short_ref_count + k;
  1324. break;
  1325. }
  1326. }
  1327. }
  1328. ref2frm[0] =
  1329. ref2frm[1] = -1;
  1330. for (i = 0; i < 16; i++)
  1331. ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
  1332. ref2frm[18 + 0] =
  1333. ref2frm[18 + 1] = -1;
  1334. for (i = 16; i < 48; i++)
  1335. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  1336. (sl->ref_list[j][i].reference & 3);
  1337. }
  1338. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  1339. av_log(h->avctx, AV_LOG_DEBUG,
  1340. "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",
  1341. sl->slice_num,
  1342. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  1343. sl->mb_y * h->mb_width + sl->mb_x,
  1344. av_get_picture_type_char(sl->slice_type),
  1345. sl->slice_type_fixed ? " fix" : "",
  1346. h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  1347. h->poc.frame_num,
  1348. h->cur_pic_ptr->field_poc[0],
  1349. h->cur_pic_ptr->field_poc[1],
  1350. sl->ref_count[0], sl->ref_count[1],
  1351. sl->qscale,
  1352. sl->deblocking_filter,
  1353. sl->slice_alpha_c0_offset, sl->slice_beta_offset,
  1354. sl->pwt.use_weight,
  1355. sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
  1356. sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  1357. }
  1358. return 0;
  1359. }
  1360. int ff_h264_get_slice_type(const H264SliceContext *sl)
  1361. {
  1362. switch (sl->slice_type) {
  1363. case AV_PICTURE_TYPE_P:
  1364. return 0;
  1365. case AV_PICTURE_TYPE_B:
  1366. return 1;
  1367. case AV_PICTURE_TYPE_I:
  1368. return 2;
  1369. case AV_PICTURE_TYPE_SP:
  1370. return 3;
  1371. case AV_PICTURE_TYPE_SI:
  1372. return 4;
  1373. default:
  1374. return AVERROR_INVALIDDATA;
  1375. }
  1376. }
  1377. static av_always_inline void fill_filter_caches_inter(const H264Context *h,
  1378. H264SliceContext *sl,
  1379. int mb_type, int top_xy,
  1380. int left_xy[LEFT_MBS],
  1381. int top_type,
  1382. int left_type[LEFT_MBS],
  1383. int mb_xy, int list)
  1384. {
  1385. int b_stride = h->b_stride;
  1386. int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
  1387. int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
  1388. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  1389. if (USES_LIST(top_type, list)) {
  1390. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  1391. const int b8_xy = 4 * top_xy + 2;
  1392. int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(sl) ? 20 : 2);
  1393. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  1394. ref_cache[0 - 1 * 8] =
  1395. ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
  1396. ref_cache[2 - 1 * 8] =
  1397. ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
  1398. } else {
  1399. AV_ZERO128(mv_dst - 1 * 8);
  1400. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1401. }
  1402. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  1403. if (USES_LIST(left_type[LTOP], list)) {
  1404. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  1405. const int b8_xy = 4 * left_xy[LTOP] + 1;
  1406. int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(sl) ? 20 : 2);
  1407. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  1408. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  1409. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  1410. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  1411. ref_cache[-1 + 0] =
  1412. ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  1413. ref_cache[-1 + 16] =
  1414. ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  1415. } else {
  1416. AV_ZERO32(mv_dst - 1 + 0);
  1417. AV_ZERO32(mv_dst - 1 + 8);
  1418. AV_ZERO32(mv_dst - 1 + 16);
  1419. AV_ZERO32(mv_dst - 1 + 24);
  1420. ref_cache[-1 + 0] =
  1421. ref_cache[-1 + 8] =
  1422. ref_cache[-1 + 16] =
  1423. ref_cache[-1 + 24] = LIST_NOT_USED;
  1424. }
  1425. }
  1426. }
  1427. if (!USES_LIST(mb_type, list)) {
  1428. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  1429. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1430. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1431. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1432. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1433. return;
  1434. }
  1435. {
  1436. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  1437. int (*ref2frm)[64] = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(sl) ? 20 : 2);
  1438. uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
  1439. uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
  1440. AV_WN32A(&ref_cache[0 * 8], ref01);
  1441. AV_WN32A(&ref_cache[1 * 8], ref01);
  1442. AV_WN32A(&ref_cache[2 * 8], ref23);
  1443. AV_WN32A(&ref_cache[3 * 8], ref23);
  1444. }
  1445. {
  1446. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
  1447. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  1448. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  1449. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  1450. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  1451. }
  1452. }
  1453. /**
  1454. * @return non zero if the loop filter can be skipped
  1455. */
  1456. static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
  1457. {
  1458. const int mb_xy = sl->mb_xy;
  1459. int top_xy, left_xy[LEFT_MBS];
  1460. int top_type, left_type[LEFT_MBS];
  1461. uint8_t *nnz;
  1462. uint8_t *nnz_cache;
  1463. top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
  1464. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  1465. if (FRAME_MBAFF(h)) {
  1466. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  1467. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  1468. if (sl->mb_y & 1) {
  1469. if (left_mb_field_flag != curr_mb_field_flag)
  1470. left_xy[LTOP] -= h->mb_stride;
  1471. } else {
  1472. if (curr_mb_field_flag)
  1473. top_xy += h->mb_stride &
  1474. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  1475. if (left_mb_field_flag != curr_mb_field_flag)
  1476. left_xy[LBOT] += h->mb_stride;
  1477. }
  1478. }
  1479. sl->top_mb_xy = top_xy;
  1480. sl->left_mb_xy[LTOP] = left_xy[LTOP];
  1481. sl->left_mb_xy[LBOT] = left_xy[LBOT];
  1482. {
  1483. /* For sufficiently low qp, filtering wouldn't do anything.
  1484. * This is a conservative estimate: could also check beta_offset
  1485. * and more accurate chroma_qp. */
  1486. int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  1487. int qp = h->cur_pic.qscale_table[mb_xy];
  1488. if (qp <= qp_thresh &&
  1489. (left_xy[LTOP] < 0 ||
  1490. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  1491. (top_xy < 0 ||
  1492. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  1493. if (!FRAME_MBAFF(h))
  1494. return 1;
  1495. if ((left_xy[LTOP] < 0 ||
  1496. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  1497. (top_xy < h->mb_stride ||
  1498. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  1499. return 1;
  1500. }
  1501. }
  1502. top_type = h->cur_pic.mb_type[top_xy];
  1503. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  1504. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  1505. if (sl->deblocking_filter == 2) {
  1506. if (h->slice_table[top_xy] != sl->slice_num)
  1507. top_type = 0;
  1508. if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
  1509. left_type[LTOP] = left_type[LBOT] = 0;
  1510. } else {
  1511. if (h->slice_table[top_xy] == 0xFFFF)
  1512. top_type = 0;
  1513. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  1514. left_type[LTOP] = left_type[LBOT] = 0;
  1515. }
  1516. sl->top_type = top_type;
  1517. sl->left_type[LTOP] = left_type[LTOP];
  1518. sl->left_type[LBOT] = left_type[LBOT];
  1519. if (IS_INTRA(mb_type))
  1520. return 0;
  1521. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1522. top_type, left_type, mb_xy, 0);
  1523. if (sl->list_count == 2)
  1524. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1525. top_type, left_type, mb_xy, 1);
  1526. nnz = h->non_zero_count[mb_xy];
  1527. nnz_cache = sl->non_zero_count_cache;
  1528. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  1529. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  1530. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  1531. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  1532. sl->cbp = h->cbp_table[mb_xy];
  1533. if (top_type) {
  1534. nnz = h->non_zero_count[top_xy];
  1535. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  1536. }
  1537. if (left_type[LTOP]) {
  1538. nnz = h->non_zero_count[left_xy[LTOP]];
  1539. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  1540. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  1541. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  1542. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  1543. }
  1544. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  1545. * from what the loop filter needs */
  1546. if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
  1547. if (IS_8x8DCT(top_type)) {
  1548. nnz_cache[4 + 8 * 0] =
  1549. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  1550. nnz_cache[6 + 8 * 0] =
  1551. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  1552. }
  1553. if (IS_8x8DCT(left_type[LTOP])) {
  1554. nnz_cache[3 + 8 * 1] =
  1555. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  1556. }
  1557. if (IS_8x8DCT(left_type[LBOT])) {
  1558. nnz_cache[3 + 8 * 3] =
  1559. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  1560. }
  1561. if (IS_8x8DCT(mb_type)) {
  1562. nnz_cache[scan8[0]] =
  1563. nnz_cache[scan8[1]] =
  1564. nnz_cache[scan8[2]] =
  1565. nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
  1566. nnz_cache[scan8[0 + 4]] =
  1567. nnz_cache[scan8[1 + 4]] =
  1568. nnz_cache[scan8[2 + 4]] =
  1569. nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
  1570. nnz_cache[scan8[0 + 8]] =
  1571. nnz_cache[scan8[1 + 8]] =
  1572. nnz_cache[scan8[2 + 8]] =
  1573. nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
  1574. nnz_cache[scan8[0 + 12]] =
  1575. nnz_cache[scan8[1 + 12]] =
  1576. nnz_cache[scan8[2 + 12]] =
  1577. nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
  1578. }
  1579. }
  1580. return 0;
  1581. }
  1582. static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
  1583. {
  1584. uint8_t *dest_y, *dest_cb, *dest_cr;
  1585. int linesize, uvlinesize, mb_x, mb_y;
  1586. const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
  1587. const int old_slice_type = sl->slice_type;
  1588. const int pixel_shift = h->pixel_shift;
  1589. const int block_h = 16 >> h->chroma_y_shift;
  1590. if (h->postpone_filter)
  1591. return;
  1592. if (sl->deblocking_filter) {
  1593. for (mb_x = start_x; mb_x < end_x; mb_x++)
  1594. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  1595. int mb_xy, mb_type;
  1596. mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
  1597. mb_type = h->cur_pic.mb_type[mb_xy];
  1598. if (FRAME_MBAFF(h))
  1599. sl->mb_mbaff =
  1600. sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  1601. sl->mb_x = mb_x;
  1602. sl->mb_y = mb_y;
  1603. dest_y = h->cur_pic.f->data[0] +
  1604. ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
  1605. dest_cb = h->cur_pic.f->data[1] +
  1606. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1607. mb_y * sl->uvlinesize * block_h;
  1608. dest_cr = h->cur_pic.f->data[2] +
  1609. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1610. mb_y * sl->uvlinesize * block_h;
  1611. // FIXME simplify above
  1612. if (MB_FIELD(sl)) {
  1613. linesize = sl->mb_linesize = sl->linesize * 2;
  1614. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
  1615. if (mb_y & 1) { // FIXME move out of this function?
  1616. dest_y -= sl->linesize * 15;
  1617. dest_cb -= sl->uvlinesize * (block_h - 1);
  1618. dest_cr -= sl->uvlinesize * (block_h - 1);
  1619. }
  1620. } else {
  1621. linesize = sl->mb_linesize = sl->linesize;
  1622. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
  1623. }
  1624. backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
  1625. uvlinesize, 0);
  1626. if (fill_filter_caches(h, sl, mb_type))
  1627. continue;
  1628. sl->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
  1629. sl->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
  1630. if (FRAME_MBAFF(h)) {
  1631. ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  1632. linesize, uvlinesize);
  1633. } else {
  1634. ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
  1635. dest_cr, linesize, uvlinesize);
  1636. }
  1637. }
  1638. }
  1639. sl->slice_type = old_slice_type;
  1640. sl->mb_x = end_x;
  1641. sl->mb_y = end_mb_y - FRAME_MBAFF(h);
  1642. sl->chroma_qp[0] = get_chroma_qp(h, 0, sl->qscale);
  1643. sl->chroma_qp[1] = get_chroma_qp(h, 1, sl->qscale);
  1644. }
  1645. static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
  1646. {
  1647. const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
  1648. int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
  1649. h->cur_pic.mb_type[mb_xy - 1] :
  1650. (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
  1651. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  1652. sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  1653. }
  1654. /**
  1655. * Draw edges and report progress for the last MB row.
  1656. */
  1657. static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
  1658. {
  1659. int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
  1660. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  1661. int height = 16 << FRAME_MBAFF(h);
  1662. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  1663. if (sl->deblocking_filter) {
  1664. if ((top + height) >= pic_height)
  1665. height += deblock_border;
  1666. top -= deblock_border;
  1667. }
  1668. if (top >= pic_height || (top + height) < 0)
  1669. return;
  1670. height = FFMIN(height, pic_height - top);
  1671. if (top < 0) {
  1672. height = top + height;
  1673. top = 0;
  1674. }
  1675. ff_h264_draw_horiz_band(h, sl, top, height);
  1676. if (h->droppable)
  1677. return;
  1678. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  1679. h->picture_structure == PICT_BOTTOM_FIELD);
  1680. }
  1681. static void er_add_slice(H264SliceContext *sl,
  1682. int startx, int starty,
  1683. int endx, int endy, int status)
  1684. {
  1685. #if CONFIG_ERROR_RESILIENCE
  1686. ERContext *er = &sl->er;
  1687. if (!sl->h264->enable_er)
  1688. return;
  1689. er->ref_count = sl->ref_count[0];
  1690. ff_er_add_slice(er, startx, starty, endx, endy, status);
  1691. #endif
  1692. }
  1693. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  1694. {
  1695. H264SliceContext *sl = arg;
  1696. const H264Context *h = sl->h264;
  1697. int lf_x_start = sl->mb_x;
  1698. int orig_deblock = sl->deblocking_filter;
  1699. int ret;
  1700. sl->linesize = h->cur_pic_ptr->f->linesize[0];
  1701. sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
  1702. ret = alloc_scratch_buffers(sl, sl->linesize);
  1703. if (ret < 0)
  1704. return ret;
  1705. sl->mb_skip_run = -1;
  1706. if (h->postpone_filter)
  1707. sl->deblocking_filter = 0;
  1708. sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  1709. avctx->codec_id != AV_CODEC_ID_H264 ||
  1710. (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
  1711. if (h->ps.pps->cabac) {
  1712. /* realign */
  1713. align_get_bits(&sl->gb);
  1714. /* init cabac */
  1715. ff_init_cabac_decoder(&sl->cabac,
  1716. sl->gb.buffer + get_bits_count(&sl->gb) / 8,
  1717. (get_bits_left(&sl->gb) + 7) / 8);
  1718. ff_h264_init_cabac_states(h, sl);
  1719. for (;;) {
  1720. // START_TIMER
  1721. int ret, eos;
  1722. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  1723. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  1724. sl->next_slice_idx);
  1725. return AVERROR_INVALIDDATA;
  1726. }
  1727. ret = ff_h264_decode_mb_cabac(h, sl);
  1728. // STOP_TIMER("decode_mb_cabac")
  1729. if (ret >= 0)
  1730. ff_h264_hl_decode_mb(h, sl);
  1731. // FIXME optimal? or let mb_decode decode 16x32 ?
  1732. if (ret >= 0 && FRAME_MBAFF(h)) {
  1733. sl->mb_y++;
  1734. ret = ff_h264_decode_mb_cabac(h, sl);
  1735. if (ret >= 0)
  1736. ff_h264_hl_decode_mb(h, sl);
  1737. sl->mb_y--;
  1738. }
  1739. eos = get_cabac_terminate(&sl->cabac);
  1740. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  1741. sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  1742. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  1743. sl->mb_y, ER_MB_END);
  1744. if (sl->mb_x >= lf_x_start)
  1745. loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
  1746. goto finish;
  1747. }
  1748. if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  1749. av_log(h->avctx, AV_LOG_ERROR,
  1750. "error while decoding MB %d %d, bytestream %td\n",
  1751. sl->mb_x, sl->mb_y,
  1752. sl->cabac.bytestream_end - sl->cabac.bytestream);
  1753. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  1754. sl->mb_y, ER_MB_ERROR);
  1755. return AVERROR_INVALIDDATA;
  1756. }
  1757. if (++sl->mb_x >= h->mb_width) {
  1758. loop_filter(h, sl, lf_x_start, sl->mb_x);
  1759. sl->mb_x = lf_x_start = 0;
  1760. decode_finish_row(h, sl);
  1761. ++sl->mb_y;
  1762. if (FIELD_OR_MBAFF_PICTURE(h)) {
  1763. ++sl->mb_y;
  1764. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  1765. predict_field_decoding_flag(h, sl);
  1766. }
  1767. }
  1768. if (eos || sl->mb_y >= h->mb_height) {
  1769. ff_tlog(h->avctx, "slice end %d %d\n",
  1770. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  1771. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  1772. sl->mb_y, ER_MB_END);
  1773. if (sl->mb_x > lf_x_start)
  1774. loop_filter(h, sl, lf_x_start, sl->mb_x);
  1775. goto finish;
  1776. }
  1777. }
  1778. } else {
  1779. for (;;) {
  1780. int ret;
  1781. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  1782. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  1783. sl->next_slice_idx);
  1784. return AVERROR_INVALIDDATA;
  1785. }
  1786. ret = ff_h264_decode_mb_cavlc(h, sl);
  1787. if (ret >= 0)
  1788. ff_h264_hl_decode_mb(h, sl);
  1789. // FIXME optimal? or let mb_decode decode 16x32 ?
  1790. if (ret >= 0 && FRAME_MBAFF(h)) {
  1791. sl->mb_y++;
  1792. ret = ff_h264_decode_mb_cavlc(h, sl);
  1793. if (ret >= 0)
  1794. ff_h264_hl_decode_mb(h, sl);
  1795. sl->mb_y--;
  1796. }
  1797. if (ret < 0) {
  1798. av_log(h->avctx, AV_LOG_ERROR,
  1799. "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
  1800. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  1801. sl->mb_y, ER_MB_ERROR);
  1802. return ret;
  1803. }
  1804. if (++sl->mb_x >= h->mb_width) {
  1805. loop_filter(h, sl, lf_x_start, sl->mb_x);
  1806. sl->mb_x = lf_x_start = 0;
  1807. decode_finish_row(h, sl);
  1808. ++sl->mb_y;
  1809. if (FIELD_OR_MBAFF_PICTURE(h)) {
  1810. ++sl->mb_y;
  1811. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  1812. predict_field_decoding_flag(h, sl);
  1813. }
  1814. if (sl->mb_y >= h->mb_height) {
  1815. ff_tlog(h->avctx, "slice end %d %d\n",
  1816. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  1817. if (get_bits_left(&sl->gb) == 0) {
  1818. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  1819. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  1820. goto finish;
  1821. } else {
  1822. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  1823. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  1824. return AVERROR_INVALIDDATA;
  1825. }
  1826. }
  1827. }
  1828. if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
  1829. ff_tlog(h->avctx, "slice end %d %d\n",
  1830. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  1831. if (get_bits_left(&sl->gb) == 0) {
  1832. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  1833. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  1834. if (sl->mb_x > lf_x_start)
  1835. loop_filter(h, sl, lf_x_start, sl->mb_x);
  1836. goto finish;
  1837. } else {
  1838. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  1839. sl->mb_y, ER_MB_ERROR);
  1840. return AVERROR_INVALIDDATA;
  1841. }
  1842. }
  1843. }
  1844. }
  1845. finish:
  1846. sl->deblocking_filter = orig_deblock;
  1847. return 0;
  1848. }
  1849. /**
  1850. * Call decode_slice() for each context.
  1851. *
  1852. * @param h h264 master context
  1853. * @param context_count number of contexts to execute
  1854. */
  1855. int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
  1856. {
  1857. AVCodecContext *const avctx = h->avctx;
  1858. H264SliceContext *sl;
  1859. int i, j;
  1860. if (h->avctx->hwaccel)
  1861. return 0;
  1862. if (context_count == 1) {
  1863. int ret;
  1864. h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
  1865. h->postpone_filter = 0;
  1866. ret = decode_slice(avctx, &h->slice_ctx[0]);
  1867. h->mb_y = h->slice_ctx[0].mb_y;
  1868. return ret;
  1869. } else {
  1870. for (i = 0; i < context_count; i++) {
  1871. int next_slice_idx = h->mb_width * h->mb_height;
  1872. int slice_idx;
  1873. sl = &h->slice_ctx[i];
  1874. sl->er.error_count = 0;
  1875. /* make sure none of those slices overlap */
  1876. slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
  1877. for (j = 0; j < context_count; j++) {
  1878. H264SliceContext *sl2 = &h->slice_ctx[j];
  1879. int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
  1880. if (i == j || slice_idx2 < slice_idx)
  1881. continue;
  1882. next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
  1883. }
  1884. sl->next_slice_idx = next_slice_idx;
  1885. }
  1886. avctx->execute(avctx, decode_slice, h->slice_ctx,
  1887. NULL, context_count, sizeof(h->slice_ctx[0]));
  1888. /* pull back stuff from slices to master context */
  1889. sl = &h->slice_ctx[context_count - 1];
  1890. h->mb_y = sl->mb_y;
  1891. for (i = 1; i < context_count; i++)
  1892. h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count;
  1893. if (h->postpone_filter) {
  1894. h->postpone_filter = 0;
  1895. for (i = 0; i < context_count; i++) {
  1896. int y_end, x_end;
  1897. sl = &h->slice_ctx[i];
  1898. y_end = FFMIN(sl->mb_y + 1, h->mb_height);
  1899. x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
  1900. for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
  1901. sl->mb_y = j;
  1902. loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
  1903. j == y_end - 1 ? x_end : h->mb_width);
  1904. }
  1905. }
  1906. }
  1907. }
  1908. return 0;
  1909. }