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.

2200 lines
82KB

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