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
80KB

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