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.

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