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.

2670 lines
100KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/display.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/stereo3d.h"
  30. #include "libavutil/timer.h"
  31. #include "internal.h"
  32. #include "cabac.h"
  33. #include "cabac_functions.h"
  34. #include "error_resilience.h"
  35. #include "avcodec.h"
  36. #include "h264.h"
  37. #include "h264dec.h"
  38. #include "h264data.h"
  39. #include "h264chroma.h"
  40. #include "h264_mvpred.h"
  41. #include "h264_ps.h"
  42. #include "golomb.h"
  43. #include "mathops.h"
  44. #include "mpegutils.h"
  45. #include "rectangle.h"
  46. #include "thread.h"
  47. static const uint8_t field_scan[16+1] = {
  48. 0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
  49. 0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
  50. 2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
  51. 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
  52. };
  53. static const uint8_t field_scan8x8[64+1] = {
  54. 0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
  55. 1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
  56. 2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
  57. 0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
  58. 2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
  59. 2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
  60. 2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
  61. 3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
  62. 3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
  63. 4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
  64. 4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
  65. 5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
  66. 5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
  67. 7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
  68. 6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
  69. 7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
  70. };
  71. static const uint8_t field_scan8x8_cavlc[64+1] = {
  72. 0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
  73. 2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
  74. 3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
  75. 5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
  76. 0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
  77. 1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
  78. 3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
  79. 5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
  80. 0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
  81. 1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
  82. 3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
  83. 5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
  84. 1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
  85. 1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
  86. 3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
  87. 6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
  88. };
  89. // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
  90. static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
  91. 0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
  92. 4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
  93. 3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
  94. 2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
  95. 1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
  96. 3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
  97. 2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
  98. 3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
  99. 0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
  100. 2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
  101. 1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
  102. 4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
  103. 0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
  104. 1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
  105. 0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
  106. 5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
  107. };
  108. static void release_unused_pictures(H264Context *h, int remove_current)
  109. {
  110. int i;
  111. /* release non reference frames */
  112. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  113. if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
  114. (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
  115. ff_h264_unref_picture(h, &h->DPB[i]);
  116. }
  117. }
  118. }
  119. static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
  120. {
  121. const H264Context *h = sl->h264;
  122. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  123. av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
  124. // edge emu needs blocksize + filter length - 1
  125. // (= 21x21 for H.264)
  126. av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
  127. av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
  128. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
  129. av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
  130. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
  131. if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
  132. !sl->top_borders[0] || !sl->top_borders[1]) {
  133. av_freep(&sl->bipred_scratchpad);
  134. av_freep(&sl->edge_emu_buffer);
  135. av_freep(&sl->top_borders[0]);
  136. av_freep(&sl->top_borders[1]);
  137. sl->bipred_scratchpad_allocated = 0;
  138. sl->edge_emu_buffer_allocated = 0;
  139. sl->top_borders_allocated[0] = 0;
  140. sl->top_borders_allocated[1] = 0;
  141. return AVERROR(ENOMEM);
  142. }
  143. return 0;
  144. }
  145. static int init_table_pools(H264Context *h)
  146. {
  147. const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
  148. const int mb_array_size = h->mb_stride * h->mb_height;
  149. const int b4_stride = h->mb_width * 4 + 1;
  150. const int b4_array_size = b4_stride * h->mb_height * 4;
  151. h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
  152. av_buffer_allocz);
  153. h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
  154. sizeof(uint32_t), av_buffer_allocz);
  155. h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
  156. sizeof(int16_t), av_buffer_allocz);
  157. h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
  158. if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
  159. !h->ref_index_pool) {
  160. av_buffer_pool_uninit(&h->qscale_table_pool);
  161. av_buffer_pool_uninit(&h->mb_type_pool);
  162. av_buffer_pool_uninit(&h->motion_val_pool);
  163. av_buffer_pool_uninit(&h->ref_index_pool);
  164. return AVERROR(ENOMEM);
  165. }
  166. return 0;
  167. }
  168. static int alloc_picture(H264Context *h, H264Picture *pic)
  169. {
  170. int i, ret = 0;
  171. av_assert0(!pic->f->data[0]);
  172. pic->tf.f = pic->f;
  173. ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
  174. AV_GET_BUFFER_FLAG_REF : 0);
  175. if (ret < 0)
  176. goto fail;
  177. pic->crop = h->ps.sps->crop;
  178. pic->crop_top = h->ps.sps->crop_top;
  179. pic->crop_left= h->ps.sps->crop_left;
  180. if (h->avctx->hwaccel) {
  181. const AVHWAccel *hwaccel = h->avctx->hwaccel;
  182. av_assert0(!pic->hwaccel_picture_private);
  183. if (hwaccel->frame_priv_data_size) {
  184. pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
  185. if (!pic->hwaccel_priv_buf)
  186. return AVERROR(ENOMEM);
  187. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  188. }
  189. }
  190. if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
  191. int h_chroma_shift, v_chroma_shift;
  192. av_pix_fmt_get_chroma_sub_sample(pic->f->format,
  193. &h_chroma_shift, &v_chroma_shift);
  194. for(i=0; i<AV_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
  195. memset(pic->f->data[1] + pic->f->linesize[1]*i,
  196. 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
  197. memset(pic->f->data[2] + pic->f->linesize[2]*i,
  198. 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
  199. }
  200. }
  201. if (!h->qscale_table_pool) {
  202. ret = init_table_pools(h);
  203. if (ret < 0)
  204. goto fail;
  205. }
  206. pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
  207. pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
  208. if (!pic->qscale_table_buf || !pic->mb_type_buf)
  209. goto fail;
  210. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
  211. pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
  212. for (i = 0; i < 2; i++) {
  213. pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
  214. pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
  215. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  216. goto fail;
  217. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  218. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  219. }
  220. return 0;
  221. fail:
  222. ff_h264_unref_picture(h, pic);
  223. return (ret < 0) ? ret : AVERROR(ENOMEM);
  224. }
  225. static inline int pic_is_unused(H264Context *h, H264Picture *pic)
  226. {
  227. if (!pic->f->buf[0])
  228. return 1;
  229. return 0;
  230. }
  231. static int find_unused_picture(H264Context *h)
  232. {
  233. int i;
  234. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  235. if (pic_is_unused(h, &h->DPB[i]))
  236. break;
  237. }
  238. if (i == H264_MAX_PICTURE_COUNT)
  239. return AVERROR_INVALIDDATA;
  240. return i;
  241. }
  242. #define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
  243. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  244. (((pic) && (pic) >= (old_ctx)->DPB && \
  245. (pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
  246. &(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
  247. static void copy_picture_range(H264Picture **to, H264Picture **from, int count,
  248. H264Context *new_base,
  249. H264Context *old_base)
  250. {
  251. int i;
  252. for (i = 0; i < count; i++) {
  253. av_assert1(!from[i] ||
  254. IN_RANGE(from[i], old_base, 1) ||
  255. IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
  256. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  257. }
  258. }
  259. static int h264_slice_header_init(H264Context *h);
  260. int ff_h264_update_thread_context(AVCodecContext *dst,
  261. const AVCodecContext *src)
  262. {
  263. H264Context *h = dst->priv_data, *h1 = src->priv_data;
  264. int inited = h->context_initialized, err = 0;
  265. int need_reinit = 0;
  266. int i, ret;
  267. if (dst == src)
  268. return 0;
  269. // We can't fail if SPS isn't set at it breaks current skip_frame code
  270. //if (!h1->ps.sps)
  271. // return AVERROR_INVALIDDATA;
  272. if (inited &&
  273. (h->width != h1->width ||
  274. h->height != h1->height ||
  275. h->mb_width != h1->mb_width ||
  276. h->mb_height != h1->mb_height ||
  277. !h->ps.sps ||
  278. h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
  279. h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
  280. h->ps.sps->colorspace != h1->ps.sps->colorspace)) {
  281. need_reinit = 1;
  282. }
  283. /* copy block_offset since frame_start may not be called */
  284. memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
  285. // SPS/PPS
  286. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++) {
  287. av_buffer_unref(&h->ps.sps_list[i]);
  288. if (h1->ps.sps_list[i]) {
  289. h->ps.sps_list[i] = av_buffer_ref(h1->ps.sps_list[i]);
  290. if (!h->ps.sps_list[i])
  291. return AVERROR(ENOMEM);
  292. }
  293. }
  294. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
  295. av_buffer_unref(&h->ps.pps_list[i]);
  296. if (h1->ps.pps_list[i]) {
  297. h->ps.pps_list[i] = av_buffer_ref(h1->ps.pps_list[i]);
  298. if (!h->ps.pps_list[i])
  299. return AVERROR(ENOMEM);
  300. }
  301. }
  302. av_buffer_unref(&h->ps.pps_ref);
  303. av_buffer_unref(&h->ps.sps_ref);
  304. h->ps.pps = NULL;
  305. h->ps.sps = NULL;
  306. if (h1->ps.pps_ref) {
  307. h->ps.pps_ref = av_buffer_ref(h1->ps.pps_ref);
  308. if (!h->ps.pps_ref)
  309. return AVERROR(ENOMEM);
  310. h->ps.pps = (const PPS*)h->ps.pps_ref->data;
  311. }
  312. if (h1->ps.sps_ref) {
  313. h->ps.sps_ref = av_buffer_ref(h1->ps.sps_ref);
  314. if (!h->ps.sps_ref)
  315. return AVERROR(ENOMEM);
  316. h->ps.sps = (const SPS*)h->ps.sps_ref->data;
  317. }
  318. if (need_reinit || !inited) {
  319. h->width = h1->width;
  320. h->height = h1->height;
  321. h->mb_height = h1->mb_height;
  322. h->mb_width = h1->mb_width;
  323. h->mb_num = h1->mb_num;
  324. h->mb_stride = h1->mb_stride;
  325. h->b_stride = h1->b_stride;
  326. if (h->context_initialized || h1->context_initialized) {
  327. if ((err = h264_slice_header_init(h)) < 0) {
  328. av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
  329. return err;
  330. }
  331. }
  332. /* copy block_offset since frame_start may not be called */
  333. memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
  334. }
  335. h->avctx->coded_height = h1->avctx->coded_height;
  336. h->avctx->coded_width = h1->avctx->coded_width;
  337. h->avctx->width = h1->avctx->width;
  338. h->avctx->height = h1->avctx->height;
  339. h->coded_picture_number = h1->coded_picture_number;
  340. h->first_field = h1->first_field;
  341. h->picture_structure = h1->picture_structure;
  342. h->mb_aff_frame = h1->mb_aff_frame;
  343. h->droppable = h1->droppable;
  344. h->backup_width = h1->backup_width;
  345. h->backup_height = h1->backup_height;
  346. h->backup_pix_fmt = h1->backup_pix_fmt;
  347. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  348. ff_h264_unref_picture(h, &h->DPB[i]);
  349. if (h1->DPB[i].f->buf[0] &&
  350. (ret = ff_h264_ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
  351. return ret;
  352. }
  353. h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
  354. ff_h264_unref_picture(h, &h->cur_pic);
  355. if (h1->cur_pic.f->buf[0]) {
  356. ret = ff_h264_ref_picture(h, &h->cur_pic, &h1->cur_pic);
  357. if (ret < 0)
  358. return ret;
  359. }
  360. h->enable_er = h1->enable_er;
  361. h->workaround_bugs = h1->workaround_bugs;
  362. h->droppable = h1->droppable;
  363. // extradata/NAL handling
  364. h->is_avc = h1->is_avc;
  365. h->nal_length_size = h1->nal_length_size;
  366. h->sei.unregistered.x264_build = h1->sei.unregistered.x264_build;
  367. memcpy(&h->poc, &h1->poc, sizeof(h->poc));
  368. memcpy(h->default_ref, h1->default_ref, sizeof(h->default_ref));
  369. memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
  370. memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
  371. memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
  372. memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
  373. h->next_output_pic = h1->next_output_pic;
  374. h->next_outputed_poc = h1->next_outputed_poc;
  375. memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
  376. h->nb_mmco = h1->nb_mmco;
  377. h->mmco_reset = h1->mmco_reset;
  378. h->explicit_ref_marking = h1->explicit_ref_marking;
  379. h->long_ref_count = h1->long_ref_count;
  380. h->short_ref_count = h1->short_ref_count;
  381. copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
  382. copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
  383. copy_picture_range(h->delayed_pic, h1->delayed_pic,
  384. MAX_DELAYED_PIC_COUNT + 2, h, h1);
  385. h->frame_recovered = h1->frame_recovered;
  386. if (!h->cur_pic_ptr)
  387. return 0;
  388. if (!h->droppable) {
  389. err = ff_h264_execute_ref_pic_marking(h);
  390. h->poc.prev_poc_msb = h->poc.poc_msb;
  391. h->poc.prev_poc_lsb = h->poc.poc_lsb;
  392. }
  393. h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
  394. h->poc.prev_frame_num = h->poc.frame_num;
  395. h->recovery_frame = h1->recovery_frame;
  396. return err;
  397. }
  398. static int h264_frame_start(H264Context *h)
  399. {
  400. H264Picture *pic;
  401. int i, ret;
  402. const int pixel_shift = h->pixel_shift;
  403. int c[4] = {
  404. 1<<(h->ps.sps->bit_depth_luma-1),
  405. 1<<(h->ps.sps->bit_depth_chroma-1),
  406. 1<<(h->ps.sps->bit_depth_chroma-1),
  407. -1
  408. };
  409. if (!ff_thread_can_start_frame(h->avctx)) {
  410. av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  411. return -1;
  412. }
  413. release_unused_pictures(h, 1);
  414. h->cur_pic_ptr = NULL;
  415. i = find_unused_picture(h);
  416. if (i < 0) {
  417. av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  418. return i;
  419. }
  420. pic = &h->DPB[i];
  421. pic->reference = h->droppable ? 0 : h->picture_structure;
  422. pic->f->coded_picture_number = h->coded_picture_number++;
  423. pic->field_picture = h->picture_structure != PICT_FRAME;
  424. pic->frame_num = h->poc.frame_num;
  425. /*
  426. * Zero key_frame here; IDR markings per slice in frame or fields are ORed
  427. * in later.
  428. * See decode_nal_units().
  429. */
  430. pic->f->key_frame = 0;
  431. pic->mmco_reset = 0;
  432. pic->recovered = 0;
  433. pic->invalid_gap = 0;
  434. pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
  435. pic->f->pict_type = h->slice_ctx[0].slice_type;
  436. if ((ret = alloc_picture(h, pic)) < 0)
  437. return ret;
  438. if(!h->frame_recovered && !h->avctx->hwaccel
  439. #if FF_API_CAP_VDPAU
  440. && !(h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU)
  441. #endif
  442. )
  443. ff_color_frame(pic->f, c);
  444. h->cur_pic_ptr = pic;
  445. ff_h264_unref_picture(h, &h->cur_pic);
  446. if (CONFIG_ERROR_RESILIENCE) {
  447. ff_h264_set_erpic(&h->slice_ctx[0].er.cur_pic, NULL);
  448. }
  449. if ((ret = ff_h264_ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
  450. return ret;
  451. for (i = 0; i < h->nb_slice_ctx; i++) {
  452. h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
  453. h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
  454. }
  455. if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
  456. ff_er_frame_start(&h->slice_ctx[0].er);
  457. ff_h264_set_erpic(&h->slice_ctx[0].er.last_pic, NULL);
  458. ff_h264_set_erpic(&h->slice_ctx[0].er.next_pic, NULL);
  459. }
  460. for (i = 0; i < 16; i++) {
  461. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
  462. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
  463. }
  464. for (i = 0; i < 16; i++) {
  465. h->block_offset[16 + i] =
  466. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
  467. h->block_offset[48 + 16 + i] =
  468. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
  469. }
  470. /* We mark the current picture as non-reference after allocating it, so
  471. * that if we break out due to an error it can be released automatically
  472. * in the next ff_mpv_frame_start().
  473. */
  474. h->cur_pic_ptr->reference = 0;
  475. h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
  476. h->next_output_pic = NULL;
  477. h->postpone_filter = 0;
  478. h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
  479. assert(h->cur_pic_ptr->long_ref == 0);
  480. return 0;
  481. }
  482. static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
  483. uint8_t *src_y,
  484. uint8_t *src_cb, uint8_t *src_cr,
  485. int linesize, int uvlinesize,
  486. int simple)
  487. {
  488. uint8_t *top_border;
  489. int top_idx = 1;
  490. const int pixel_shift = h->pixel_shift;
  491. int chroma444 = CHROMA444(h);
  492. int chroma422 = CHROMA422(h);
  493. src_y -= linesize;
  494. src_cb -= uvlinesize;
  495. src_cr -= uvlinesize;
  496. if (!simple && FRAME_MBAFF(h)) {
  497. if (sl->mb_y & 1) {
  498. if (!MB_MBAFF(sl)) {
  499. top_border = sl->top_borders[0][sl->mb_x];
  500. AV_COPY128(top_border, src_y + 15 * linesize);
  501. if (pixel_shift)
  502. AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
  503. if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  504. if (chroma444) {
  505. if (pixel_shift) {
  506. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  507. AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
  508. AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
  509. AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
  510. } else {
  511. AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
  512. AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
  513. }
  514. } else if (chroma422) {
  515. if (pixel_shift) {
  516. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  517. AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
  518. } else {
  519. AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
  520. AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
  521. }
  522. } else {
  523. if (pixel_shift) {
  524. AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
  525. AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
  526. } else {
  527. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  528. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  529. }
  530. }
  531. }
  532. }
  533. } else if (MB_MBAFF(sl)) {
  534. top_idx = 0;
  535. } else
  536. return;
  537. }
  538. top_border = sl->top_borders[top_idx][sl->mb_x];
  539. /* There are two lines saved, the line above the top macroblock
  540. * of a pair, and the line above the bottom macroblock. */
  541. AV_COPY128(top_border, src_y + 16 * linesize);
  542. if (pixel_shift)
  543. AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
  544. if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  545. if (chroma444) {
  546. if (pixel_shift) {
  547. AV_COPY128(top_border + 32, src_cb + 16 * linesize);
  548. AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
  549. AV_COPY128(top_border + 64, src_cr + 16 * linesize);
  550. AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
  551. } else {
  552. AV_COPY128(top_border + 16, src_cb + 16 * linesize);
  553. AV_COPY128(top_border + 32, src_cr + 16 * linesize);
  554. }
  555. } else if (chroma422) {
  556. if (pixel_shift) {
  557. AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
  558. AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
  559. } else {
  560. AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
  561. AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
  562. }
  563. } else {
  564. if (pixel_shift) {
  565. AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
  566. AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
  567. } else {
  568. AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
  569. AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
  570. }
  571. }
  572. }
  573. }
  574. /**
  575. * Initialize implicit_weight table.
  576. * @param field 0/1 initialize the weight for interlaced MBAFF
  577. * -1 initializes the rest
  578. */
  579. static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
  580. {
  581. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  582. for (i = 0; i < 2; i++) {
  583. sl->pwt.luma_weight_flag[i] = 0;
  584. sl->pwt.chroma_weight_flag[i] = 0;
  585. }
  586. if (field < 0) {
  587. if (h->picture_structure == PICT_FRAME) {
  588. cur_poc = h->cur_pic_ptr->poc;
  589. } else {
  590. cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
  591. }
  592. if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
  593. sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2 * cur_poc) {
  594. sl->pwt.use_weight = 0;
  595. sl->pwt.use_weight_chroma = 0;
  596. return;
  597. }
  598. ref_start = 0;
  599. ref_count0 = sl->ref_count[0];
  600. ref_count1 = sl->ref_count[1];
  601. } else {
  602. cur_poc = h->cur_pic_ptr->field_poc[field];
  603. ref_start = 16;
  604. ref_count0 = 16 + 2 * sl->ref_count[0];
  605. ref_count1 = 16 + 2 * sl->ref_count[1];
  606. }
  607. sl->pwt.use_weight = 2;
  608. sl->pwt.use_weight_chroma = 2;
  609. sl->pwt.luma_log2_weight_denom = 5;
  610. sl->pwt.chroma_log2_weight_denom = 5;
  611. for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
  612. int64_t poc0 = sl->ref_list[0][ref0].poc;
  613. for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
  614. int w = 32;
  615. if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
  616. int poc1 = sl->ref_list[1][ref1].poc;
  617. int td = av_clip_int8(poc1 - poc0);
  618. if (td) {
  619. int tb = av_clip_int8(cur_poc - poc0);
  620. int tx = (16384 + (FFABS(td) >> 1)) / td;
  621. int dist_scale_factor = (tb * tx + 32) >> 8;
  622. if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
  623. w = 64 - dist_scale_factor;
  624. }
  625. }
  626. if (field < 0) {
  627. sl->pwt.implicit_weight[ref0][ref1][0] =
  628. sl->pwt.implicit_weight[ref0][ref1][1] = w;
  629. } else {
  630. sl->pwt.implicit_weight[ref0][ref1][field] = w;
  631. }
  632. }
  633. }
  634. }
  635. /**
  636. * initialize scan tables
  637. */
  638. static void init_scan_tables(H264Context *h)
  639. {
  640. int i;
  641. for (i = 0; i < 16; i++) {
  642. #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
  643. h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
  644. h->field_scan[i] = TRANSPOSE(field_scan[i]);
  645. #undef TRANSPOSE
  646. }
  647. for (i = 0; i < 64; i++) {
  648. #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
  649. h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
  650. h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
  651. h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
  652. h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
  653. #undef TRANSPOSE
  654. }
  655. if (h->ps.sps->transform_bypass) { // FIXME same ugly
  656. memcpy(h->zigzag_scan_q0 , ff_zigzag_scan , sizeof(h->zigzag_scan_q0 ));
  657. memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
  658. memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
  659. memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
  660. memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
  661. memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
  662. } else {
  663. memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
  664. memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
  665. memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
  666. memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
  667. memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
  668. memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
  669. }
  670. }
  671. static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
  672. {
  673. #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
  674. CONFIG_H264_D3D11VA_HWACCEL + \
  675. CONFIG_H264_VAAPI_HWACCEL + \
  676. (CONFIG_H264_VDA_HWACCEL * 2) + \
  677. CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
  678. CONFIG_H264_VDPAU_HWACCEL)
  679. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
  680. const enum AVPixelFormat *choices = pix_fmts;
  681. int i;
  682. switch (h->ps.sps->bit_depth_luma) {
  683. case 9:
  684. if (CHROMA444(h)) {
  685. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  686. *fmt++ = AV_PIX_FMT_GBRP9;
  687. } else
  688. *fmt++ = AV_PIX_FMT_YUV444P9;
  689. } else if (CHROMA422(h))
  690. *fmt++ = AV_PIX_FMT_YUV422P9;
  691. else
  692. *fmt++ = AV_PIX_FMT_YUV420P9;
  693. break;
  694. case 10:
  695. if (CHROMA444(h)) {
  696. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  697. *fmt++ = AV_PIX_FMT_GBRP10;
  698. } else
  699. *fmt++ = AV_PIX_FMT_YUV444P10;
  700. } else if (CHROMA422(h))
  701. *fmt++ = AV_PIX_FMT_YUV422P10;
  702. else
  703. *fmt++ = AV_PIX_FMT_YUV420P10;
  704. break;
  705. case 12:
  706. if (CHROMA444(h)) {
  707. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  708. *fmt++ = AV_PIX_FMT_GBRP12;
  709. } else
  710. *fmt++ = AV_PIX_FMT_YUV444P12;
  711. } else if (CHROMA422(h))
  712. *fmt++ = AV_PIX_FMT_YUV422P12;
  713. else
  714. *fmt++ = AV_PIX_FMT_YUV420P12;
  715. break;
  716. case 14:
  717. if (CHROMA444(h)) {
  718. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  719. *fmt++ = AV_PIX_FMT_GBRP14;
  720. } else
  721. *fmt++ = AV_PIX_FMT_YUV444P14;
  722. } else if (CHROMA422(h))
  723. *fmt++ = AV_PIX_FMT_YUV422P14;
  724. else
  725. *fmt++ = AV_PIX_FMT_YUV420P14;
  726. break;
  727. case 8:
  728. #if CONFIG_H264_VDPAU_HWACCEL
  729. *fmt++ = AV_PIX_FMT_VDPAU;
  730. #endif
  731. if (CHROMA444(h)) {
  732. if (h->avctx->colorspace == AVCOL_SPC_RGB)
  733. *fmt++ = AV_PIX_FMT_GBRP;
  734. else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
  735. *fmt++ = AV_PIX_FMT_YUVJ444P;
  736. else
  737. *fmt++ = AV_PIX_FMT_YUV444P;
  738. } else if (CHROMA422(h)) {
  739. if (h->avctx->color_range == AVCOL_RANGE_JPEG)
  740. *fmt++ = AV_PIX_FMT_YUVJ422P;
  741. else
  742. *fmt++ = AV_PIX_FMT_YUV422P;
  743. } else {
  744. #if CONFIG_H264_DXVA2_HWACCEL
  745. *fmt++ = AV_PIX_FMT_DXVA2_VLD;
  746. #endif
  747. #if CONFIG_H264_D3D11VA_HWACCEL
  748. *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
  749. #endif
  750. #if CONFIG_H264_VAAPI_HWACCEL
  751. *fmt++ = AV_PIX_FMT_VAAPI;
  752. #endif
  753. #if CONFIG_H264_VDA_HWACCEL
  754. *fmt++ = AV_PIX_FMT_VDA_VLD;
  755. *fmt++ = AV_PIX_FMT_VDA;
  756. #endif
  757. #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
  758. *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
  759. #endif
  760. if (h->avctx->codec->pix_fmts)
  761. choices = h->avctx->codec->pix_fmts;
  762. else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
  763. *fmt++ = AV_PIX_FMT_YUVJ420P;
  764. else
  765. *fmt++ = AV_PIX_FMT_YUV420P;
  766. }
  767. break;
  768. default:
  769. av_log(h->avctx, AV_LOG_ERROR,
  770. "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
  771. return AVERROR_INVALIDDATA;
  772. }
  773. *fmt = AV_PIX_FMT_NONE;
  774. for (i=0; choices[i] != AV_PIX_FMT_NONE; i++)
  775. if (choices[i] == h->avctx->pix_fmt && !force_callback)
  776. return choices[i];
  777. return ff_thread_get_format(h->avctx, choices);
  778. }
  779. /* export coded and cropped frame dimensions to AVCodecContext */
  780. static int init_dimensions(H264Context *h)
  781. {
  782. const SPS *sps = (const SPS*)h->ps.sps;
  783. int width = h->width - (sps->crop_right + sps->crop_left);
  784. int height = h->height - (sps->crop_top + sps->crop_bottom);
  785. av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
  786. av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
  787. /* handle container cropping */
  788. if (FFALIGN(h->avctx->width, 16) == FFALIGN(width, 16) &&
  789. FFALIGN(h->avctx->height, 16) == FFALIGN(height, 16) &&
  790. h->avctx->width <= width &&
  791. h->avctx->height <= height
  792. ) {
  793. width = h->avctx->width;
  794. height = h->avctx->height;
  795. }
  796. h->avctx->coded_width = h->width;
  797. h->avctx->coded_height = h->height;
  798. h->avctx->width = width;
  799. h->avctx->height = height;
  800. return 0;
  801. }
  802. static int h264_slice_header_init(H264Context *h)
  803. {
  804. const SPS *sps = h->ps.sps;
  805. int i, ret;
  806. ff_set_sar(h->avctx, sps->sar);
  807. av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
  808. &h->chroma_x_shift, &h->chroma_y_shift);
  809. if (sps->timing_info_present_flag) {
  810. int64_t den = sps->time_scale;
  811. if (h->sei.unregistered.x264_build < 44U)
  812. den *= 2;
  813. av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
  814. sps->num_units_in_tick * h->avctx->ticks_per_frame, den, 1 << 30);
  815. }
  816. ff_h264_free_tables(h);
  817. h->first_field = 0;
  818. h->prev_interlaced_frame = 1;
  819. init_scan_tables(h);
  820. ret = ff_h264_alloc_tables(h);
  821. if (ret < 0) {
  822. av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
  823. goto fail;
  824. }
  825. #if FF_API_CAP_VDPAU
  826. if (h->avctx->codec &&
  827. h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU &&
  828. (sps->bit_depth_luma != 8 || sps->chroma_format_idc > 1)) {
  829. av_log(h->avctx, AV_LOG_ERROR,
  830. "VDPAU decoding does not support video colorspace.\n");
  831. ret = AVERROR_INVALIDDATA;
  832. goto fail;
  833. }
  834. #endif
  835. if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
  836. sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
  837. ) {
  838. av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
  839. sps->bit_depth_luma);
  840. ret = AVERROR_INVALIDDATA;
  841. goto fail;
  842. }
  843. h->cur_bit_depth_luma =
  844. h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
  845. h->cur_chroma_format_idc = sps->chroma_format_idc;
  846. h->pixel_shift = sps->bit_depth_luma > 8;
  847. h->chroma_format_idc = sps->chroma_format_idc;
  848. h->bit_depth_luma = sps->bit_depth_luma;
  849. ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
  850. sps->chroma_format_idc);
  851. ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
  852. ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
  853. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, sps->bit_depth_luma,
  854. sps->chroma_format_idc);
  855. ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
  856. if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
  857. ret = ff_h264_slice_context_init(h, &h->slice_ctx[0]);
  858. if (ret < 0) {
  859. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  860. goto fail;
  861. }
  862. } else {
  863. for (i = 0; i < h->nb_slice_ctx; i++) {
  864. H264SliceContext *sl = &h->slice_ctx[i];
  865. sl->h264 = h;
  866. sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
  867. sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
  868. sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
  869. if ((ret = ff_h264_slice_context_init(h, sl)) < 0) {
  870. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  871. goto fail;
  872. }
  873. }
  874. }
  875. h->context_initialized = 1;
  876. return 0;
  877. fail:
  878. ff_h264_free_tables(h);
  879. h->context_initialized = 0;
  880. return ret;
  881. }
  882. static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
  883. {
  884. switch (a) {
  885. case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
  886. case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
  887. case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
  888. default:
  889. return a;
  890. }
  891. }
  892. static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice)
  893. {
  894. const SPS *sps;
  895. int needs_reinit = 0, must_reinit, ret;
  896. if (first_slice) {
  897. av_buffer_unref(&h->ps.pps_ref);
  898. h->ps.pps = NULL;
  899. h->ps.pps_ref = av_buffer_ref(h->ps.pps_list[sl->pps_id]);
  900. if (!h->ps.pps_ref)
  901. return AVERROR(ENOMEM);
  902. h->ps.pps = (const PPS*)h->ps.pps_ref->data;
  903. }
  904. if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
  905. av_buffer_unref(&h->ps.sps_ref);
  906. h->ps.sps = NULL;
  907. h->ps.sps_ref = av_buffer_ref(h->ps.sps_list[h->ps.pps->sps_id]);
  908. if (!h->ps.sps_ref)
  909. return AVERROR(ENOMEM);
  910. h->ps.sps = (const SPS*)h->ps.sps_ref->data;
  911. if (h->mb_width != h->ps.sps->mb_width ||
  912. h->mb_height != h->ps.sps->mb_height ||
  913. h->cur_bit_depth_luma != h->ps.sps->bit_depth_luma ||
  914. h->cur_chroma_format_idc != h->ps.sps->chroma_format_idc
  915. )
  916. needs_reinit = 1;
  917. if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
  918. h->chroma_format_idc != h->ps.sps->chroma_format_idc)
  919. needs_reinit = 1;
  920. }
  921. sps = h->ps.sps;
  922. must_reinit = (h->context_initialized &&
  923. ( 16*sps->mb_width != h->avctx->coded_width
  924. || 16*sps->mb_height != h->avctx->coded_height
  925. || h->cur_bit_depth_luma != sps->bit_depth_luma
  926. || h->cur_chroma_format_idc != sps->chroma_format_idc
  927. || h->mb_width != sps->mb_width
  928. || h->mb_height != sps->mb_height
  929. ));
  930. if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
  931. || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
  932. must_reinit = 1;
  933. if (first_slice && av_cmp_q(sps->sar, h->avctx->sample_aspect_ratio))
  934. must_reinit = 1;
  935. if (!h->setup_finished) {
  936. h->avctx->profile = ff_h264_get_profile(sps);
  937. h->avctx->level = sps->level_idc;
  938. h->avctx->refs = sps->ref_frame_count;
  939. h->mb_width = sps->mb_width;
  940. h->mb_height = sps->mb_height;
  941. h->mb_num = h->mb_width * h->mb_height;
  942. h->mb_stride = h->mb_width + 1;
  943. h->b_stride = h->mb_width * 4;
  944. h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
  945. h->width = 16 * h->mb_width;
  946. h->height = 16 * h->mb_height;
  947. ret = init_dimensions(h);
  948. if (ret < 0)
  949. return ret;
  950. if (sps->video_signal_type_present_flag) {
  951. h->avctx->color_range = sps->full_range > 0 ? AVCOL_RANGE_JPEG
  952. : AVCOL_RANGE_MPEG;
  953. if (sps->colour_description_present_flag) {
  954. if (h->avctx->colorspace != sps->colorspace)
  955. needs_reinit = 1;
  956. h->avctx->color_primaries = sps->color_primaries;
  957. h->avctx->color_trc = sps->color_trc;
  958. h->avctx->colorspace = sps->colorspace;
  959. }
  960. }
  961. }
  962. if (!h->context_initialized || must_reinit || needs_reinit) {
  963. int flush_changes = h->context_initialized;
  964. h->context_initialized = 0;
  965. if (sl != h->slice_ctx) {
  966. av_log(h->avctx, AV_LOG_ERROR,
  967. "changing width %d -> %d / height %d -> %d on "
  968. "slice %d\n",
  969. h->width, h->avctx->coded_width,
  970. h->height, h->avctx->coded_height,
  971. h->current_slice + 1);
  972. return AVERROR_INVALIDDATA;
  973. }
  974. av_assert1(first_slice);
  975. if (flush_changes)
  976. ff_h264_flush_change(h);
  977. if ((ret = get_pixel_format(h, 1)) < 0)
  978. return ret;
  979. h->avctx->pix_fmt = ret;
  980. av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
  981. "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
  982. if ((ret = h264_slice_header_init(h)) < 0) {
  983. av_log(h->avctx, AV_LOG_ERROR,
  984. "h264_slice_header_init() failed\n");
  985. return ret;
  986. }
  987. }
  988. return 0;
  989. }
  990. static int h264_export_frame_props(H264Context *h)
  991. {
  992. const SPS *sps = h->ps.sps;
  993. H264Picture *cur = h->cur_pic_ptr;
  994. cur->f->interlaced_frame = 0;
  995. cur->f->repeat_pict = 0;
  996. /* Signal interlacing information externally. */
  997. /* Prioritize picture timing SEI information over used
  998. * decoding process if it exists. */
  999. if (sps->pic_struct_present_flag) {
  1000. H264SEIPictureTiming *pt = &h->sei.picture_timing;
  1001. switch (pt->pic_struct) {
  1002. case SEI_PIC_STRUCT_FRAME:
  1003. break;
  1004. case SEI_PIC_STRUCT_TOP_FIELD:
  1005. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  1006. cur->f->interlaced_frame = 1;
  1007. break;
  1008. case SEI_PIC_STRUCT_TOP_BOTTOM:
  1009. case SEI_PIC_STRUCT_BOTTOM_TOP:
  1010. if (FIELD_OR_MBAFF_PICTURE(h))
  1011. cur->f->interlaced_frame = 1;
  1012. else
  1013. // try to flag soft telecine progressive
  1014. cur->f->interlaced_frame = h->prev_interlaced_frame;
  1015. break;
  1016. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  1017. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  1018. /* Signal the possibility of telecined film externally
  1019. * (pic_struct 5,6). From these hints, let the applications
  1020. * decide if they apply deinterlacing. */
  1021. cur->f->repeat_pict = 1;
  1022. break;
  1023. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  1024. cur->f->repeat_pict = 2;
  1025. break;
  1026. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  1027. cur->f->repeat_pict = 4;
  1028. break;
  1029. }
  1030. if ((pt->ct_type & 3) &&
  1031. pt->pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  1032. cur->f->interlaced_frame = (pt->ct_type & (1 << 1)) != 0;
  1033. } else {
  1034. /* Derive interlacing flag from used decoding process. */
  1035. cur->f->interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
  1036. }
  1037. h->prev_interlaced_frame = cur->f->interlaced_frame;
  1038. if (cur->field_poc[0] != cur->field_poc[1]) {
  1039. /* Derive top_field_first from field pocs. */
  1040. cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  1041. } else {
  1042. if (sps->pic_struct_present_flag) {
  1043. /* Use picture timing SEI information. Even if it is a
  1044. * information of a past frame, better than nothing. */
  1045. if (h->sei.picture_timing.pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
  1046. h->sei.picture_timing.pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  1047. cur->f->top_field_first = 1;
  1048. else
  1049. cur->f->top_field_first = 0;
  1050. } else if (cur->f->interlaced_frame) {
  1051. /* Default to top field first when pic_struct_present_flag
  1052. * is not set but interlaced frame detected */
  1053. cur->f->top_field_first = 1;
  1054. } else {
  1055. /* Most likely progressive */
  1056. cur->f->top_field_first = 0;
  1057. }
  1058. }
  1059. if (h->sei.frame_packing.present &&
  1060. h->sei.frame_packing.frame_packing_arrangement_type <= 6 &&
  1061. h->sei.frame_packing.content_interpretation_type > 0 &&
  1062. h->sei.frame_packing.content_interpretation_type < 3) {
  1063. H264SEIFramePacking *fp = &h->sei.frame_packing;
  1064. AVStereo3D *stereo = av_stereo3d_create_side_data(cur->f);
  1065. if (stereo) {
  1066. switch (fp->frame_packing_arrangement_type) {
  1067. case 0:
  1068. stereo->type = AV_STEREO3D_CHECKERBOARD;
  1069. break;
  1070. case 1:
  1071. stereo->type = AV_STEREO3D_COLUMNS;
  1072. break;
  1073. case 2:
  1074. stereo->type = AV_STEREO3D_LINES;
  1075. break;
  1076. case 3:
  1077. if (fp->quincunx_sampling_flag)
  1078. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  1079. else
  1080. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  1081. break;
  1082. case 4:
  1083. stereo->type = AV_STEREO3D_TOPBOTTOM;
  1084. break;
  1085. case 5:
  1086. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  1087. break;
  1088. case 6:
  1089. stereo->type = AV_STEREO3D_2D;
  1090. break;
  1091. }
  1092. if (fp->content_interpretation_type == 2)
  1093. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  1094. }
  1095. }
  1096. if (h->sei.display_orientation.present &&
  1097. (h->sei.display_orientation.anticlockwise_rotation ||
  1098. h->sei.display_orientation.hflip ||
  1099. h->sei.display_orientation.vflip)) {
  1100. H264SEIDisplayOrientation *o = &h->sei.display_orientation;
  1101. double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
  1102. AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
  1103. AV_FRAME_DATA_DISPLAYMATRIX,
  1104. sizeof(int32_t) * 9);
  1105. if (rotation) {
  1106. av_display_rotation_set((int32_t *)rotation->data, angle);
  1107. av_display_matrix_flip((int32_t *)rotation->data,
  1108. o->hflip, o->vflip);
  1109. }
  1110. }
  1111. if (h->sei.afd.present) {
  1112. AVFrameSideData *sd = av_frame_new_side_data(cur->f, AV_FRAME_DATA_AFD,
  1113. sizeof(uint8_t));
  1114. if (sd) {
  1115. *sd->data = h->sei.afd.active_format_description;
  1116. h->sei.afd.present = 0;
  1117. }
  1118. }
  1119. if (h->sei.a53_caption.a53_caption) {
  1120. H264SEIA53Caption *a53 = &h->sei.a53_caption;
  1121. AVFrameSideData *sd = av_frame_new_side_data(cur->f,
  1122. AV_FRAME_DATA_A53_CC,
  1123. a53->a53_caption_size);
  1124. if (sd)
  1125. memcpy(sd->data, a53->a53_caption, a53->a53_caption_size);
  1126. av_freep(&a53->a53_caption);
  1127. a53->a53_caption_size = 0;
  1128. h->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
  1129. }
  1130. return 0;
  1131. }
  1132. /* This function is called right after decoding the slice header for a first
  1133. * slice in a field (or a frame). It decides whether we are decoding a new frame
  1134. * or a second field in a pair and does the necessary setup.
  1135. */
  1136. static int h264_field_start(H264Context *h, const H264SliceContext *sl,
  1137. const H2645NAL *nal, int first_slice)
  1138. {
  1139. int i;
  1140. const SPS *sps;
  1141. int last_pic_structure, last_pic_droppable, ret;
  1142. ret = h264_init_ps(h, sl, first_slice);
  1143. if (ret < 0)
  1144. return ret;
  1145. sps = h->ps.sps;
  1146. last_pic_droppable = h->droppable;
  1147. last_pic_structure = h->picture_structure;
  1148. h->droppable = (nal->ref_idc == 0);
  1149. h->picture_structure = sl->picture_structure;
  1150. h->poc.frame_num = sl->frame_num;
  1151. h->poc.poc_lsb = sl->poc_lsb;
  1152. h->poc.delta_poc_bottom = sl->delta_poc_bottom;
  1153. h->poc.delta_poc[0] = sl->delta_poc[0];
  1154. h->poc.delta_poc[1] = sl->delta_poc[1];
  1155. /* Shorten frame num gaps so we don't have to allocate reference
  1156. * frames just to throw them away */
  1157. if (h->poc.frame_num != h->poc.prev_frame_num) {
  1158. int unwrap_prev_frame_num = h->poc.prev_frame_num;
  1159. int max_frame_num = 1 << sps->log2_max_frame_num;
  1160. if (unwrap_prev_frame_num > h->poc.frame_num)
  1161. unwrap_prev_frame_num -= max_frame_num;
  1162. if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
  1163. unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
  1164. if (unwrap_prev_frame_num < 0)
  1165. unwrap_prev_frame_num += max_frame_num;
  1166. h->poc.prev_frame_num = unwrap_prev_frame_num;
  1167. }
  1168. }
  1169. /* See if we have a decoded first field looking for a pair...
  1170. * Here, we're using that to see if we should mark previously
  1171. * decode frames as "finished".
  1172. * We have to do that before the "dummy" in-between frame allocation,
  1173. * since that can modify h->cur_pic_ptr. */
  1174. if (h->first_field) {
  1175. av_assert0(h->cur_pic_ptr);
  1176. av_assert0(h->cur_pic_ptr->f->buf[0]);
  1177. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1178. /* Mark old field/frame as completed */
  1179. if (h->cur_pic_ptr->tf.owner == h->avctx) {
  1180. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1181. last_pic_structure == PICT_BOTTOM_FIELD);
  1182. }
  1183. /* figure out if we have a complementary field pair */
  1184. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1185. /* Previous field is unmatched. Don't display it, but let it
  1186. * remain for reference if marked as such. */
  1187. if (last_pic_structure != PICT_FRAME) {
  1188. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1189. last_pic_structure == PICT_TOP_FIELD);
  1190. }
  1191. } else {
  1192. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1193. /* This and previous field were reference, but had
  1194. * different frame_nums. Consider this field first in
  1195. * pair. Throw away previous field except for reference
  1196. * purposes. */
  1197. if (last_pic_structure != PICT_FRAME) {
  1198. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1199. last_pic_structure == PICT_TOP_FIELD);
  1200. }
  1201. } else {
  1202. /* Second field in complementary pair */
  1203. if (!((last_pic_structure == PICT_TOP_FIELD &&
  1204. h->picture_structure == PICT_BOTTOM_FIELD) ||
  1205. (last_pic_structure == PICT_BOTTOM_FIELD &&
  1206. h->picture_structure == PICT_TOP_FIELD))) {
  1207. av_log(h->avctx, AV_LOG_ERROR,
  1208. "Invalid field mode combination %d/%d\n",
  1209. last_pic_structure, h->picture_structure);
  1210. h->picture_structure = last_pic_structure;
  1211. h->droppable = last_pic_droppable;
  1212. return AVERROR_INVALIDDATA;
  1213. } else if (last_pic_droppable != h->droppable) {
  1214. avpriv_request_sample(h->avctx,
  1215. "Found reference and non-reference fields in the same frame, which");
  1216. h->picture_structure = last_pic_structure;
  1217. h->droppable = last_pic_droppable;
  1218. return AVERROR_PATCHWELCOME;
  1219. }
  1220. }
  1221. }
  1222. }
  1223. while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&
  1224. h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
  1225. H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1226. av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  1227. h->poc.frame_num, h->poc.prev_frame_num);
  1228. if (!sps->gaps_in_frame_num_allowed_flag)
  1229. for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
  1230. h->last_pocs[i] = INT_MIN;
  1231. ret = h264_frame_start(h);
  1232. if (ret < 0) {
  1233. h->first_field = 0;
  1234. return ret;
  1235. }
  1236. h->poc.prev_frame_num++;
  1237. h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
  1238. h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
  1239. h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;
  1240. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  1241. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  1242. h->explicit_ref_marking = 0;
  1243. ret = ff_h264_execute_ref_pic_marking(h);
  1244. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1245. return ret;
  1246. /* Error concealment: If a ref is missing, copy the previous ref
  1247. * in its place.
  1248. * FIXME: Avoiding a memcpy would be nice, but ref handling makes
  1249. * many assumptions about there being no actual duplicates.
  1250. * FIXME: This does not copy padding for out-of-frame motion
  1251. * vectors. Given we are concealing a lost frame, this probably
  1252. * is not noticeable by comparison, but it should be fixed. */
  1253. if (h->short_ref_count) {
  1254. if (prev &&
  1255. h->short_ref[0]->f->width == prev->f->width &&
  1256. h->short_ref[0]->f->height == prev->f->height &&
  1257. h->short_ref[0]->f->format == prev->f->format) {
  1258. ff_thread_await_progress(&prev->tf, INT_MAX, 0);
  1259. if (prev->field_picture)
  1260. ff_thread_await_progress(&prev->tf, INT_MAX, 1);
  1261. av_image_copy(h->short_ref[0]->f->data,
  1262. h->short_ref[0]->f->linesize,
  1263. (const uint8_t **)prev->f->data,
  1264. prev->f->linesize,
  1265. prev->f->format,
  1266. prev->f->width,
  1267. prev->f->height);
  1268. h->short_ref[0]->poc = prev->poc + 2;
  1269. }
  1270. h->short_ref[0]->frame_num = h->poc.prev_frame_num;
  1271. }
  1272. }
  1273. /* See if we have a decoded first field looking for a pair...
  1274. * We're using that to see whether to continue decoding in that
  1275. * frame, or to allocate a new one. */
  1276. if (h->first_field) {
  1277. av_assert0(h->cur_pic_ptr);
  1278. av_assert0(h->cur_pic_ptr->f->buf[0]);
  1279. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1280. /* figure out if we have a complementary field pair */
  1281. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1282. /* Previous field is unmatched. Don't display it, but let it
  1283. * remain for reference if marked as such. */
  1284. h->missing_fields ++;
  1285. h->cur_pic_ptr = NULL;
  1286. h->first_field = FIELD_PICTURE(h);
  1287. } else {
  1288. h->missing_fields = 0;
  1289. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1290. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1291. h->picture_structure==PICT_BOTTOM_FIELD);
  1292. /* This and the previous field had different frame_nums.
  1293. * Consider this field first in pair. Throw away previous
  1294. * one except for reference purposes. */
  1295. h->first_field = 1;
  1296. h->cur_pic_ptr = NULL;
  1297. } else {
  1298. /* Second field in complementary pair */
  1299. h->first_field = 0;
  1300. }
  1301. }
  1302. } else {
  1303. /* Frame or first field in a potentially complementary pair */
  1304. h->first_field = FIELD_PICTURE(h);
  1305. }
  1306. if (!FIELD_PICTURE(h) || h->first_field) {
  1307. if (h264_frame_start(h) < 0) {
  1308. h->first_field = 0;
  1309. return AVERROR_INVALIDDATA;
  1310. }
  1311. } else {
  1312. release_unused_pictures(h, 0);
  1313. }
  1314. /* Some macroblocks can be accessed before they're available in case
  1315. * of lost slices, MBAFF or threading. */
  1316. if (FIELD_PICTURE(h)) {
  1317. for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
  1318. memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
  1319. } else {
  1320. memset(h->slice_table, -1,
  1321. (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
  1322. }
  1323. ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
  1324. h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
  1325. memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
  1326. h->nb_mmco = sl->nb_mmco;
  1327. h->explicit_ref_marking = sl->explicit_ref_marking;
  1328. h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
  1329. if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
  1330. const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
  1331. if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
  1332. h->valid_recovery_point = 1;
  1333. if ( h->recovery_frame < 0
  1334. || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
  1335. h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
  1336. if (!h->valid_recovery_point)
  1337. h->recovery_frame = h->poc.frame_num;
  1338. }
  1339. }
  1340. h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE);
  1341. if (nal->type == H264_NAL_IDR_SLICE ||
  1342. (h->recovery_frame == h->poc.frame_num && nal->ref_idc)) {
  1343. h->recovery_frame = -1;
  1344. h->cur_pic_ptr->recovered = 1;
  1345. }
  1346. // If we have an IDR, all frames after it in decoded order are
  1347. // "recovered".
  1348. if (nal->type == H264_NAL_IDR_SLICE)
  1349. h->frame_recovered |= FRAME_RECOVERED_IDR;
  1350. #if 1
  1351. h->cur_pic_ptr->recovered |= h->frame_recovered;
  1352. #else
  1353. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  1354. #endif
  1355. /* Set the frame properties/side data. Only done for the second field in
  1356. * field coded frames, since some SEI information is present for each field
  1357. * and is merged by the SEI parsing code. */
  1358. if (!FIELD_PICTURE(h) || !h->first_field) {
  1359. ret = h264_export_frame_props(h);
  1360. if (ret < 0)
  1361. return ret;
  1362. }
  1363. return 0;
  1364. }
  1365. static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
  1366. const H2645NAL *nal)
  1367. {
  1368. const SPS *sps;
  1369. const PPS *pps;
  1370. int ret;
  1371. unsigned int slice_type, tmp, i;
  1372. int field_pic_flag, bottom_field_flag;
  1373. int first_slice = sl == h->slice_ctx && !h->current_slice;
  1374. int picture_structure;
  1375. if (first_slice)
  1376. av_assert0(!h->setup_finished);
  1377. sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
  1378. slice_type = get_ue_golomb_31(&sl->gb);
  1379. if (slice_type > 9) {
  1380. av_log(h->avctx, AV_LOG_ERROR,
  1381. "slice type %d too large at %d\n",
  1382. slice_type, sl->first_mb_addr);
  1383. return AVERROR_INVALIDDATA;
  1384. }
  1385. if (slice_type > 4) {
  1386. slice_type -= 5;
  1387. sl->slice_type_fixed = 1;
  1388. } else
  1389. sl->slice_type_fixed = 0;
  1390. slice_type = ff_h264_golomb_to_pict_type[slice_type];
  1391. sl->slice_type = slice_type;
  1392. sl->slice_type_nos = slice_type & 3;
  1393. if (nal->type == H264_NAL_IDR_SLICE &&
  1394. sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1395. av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
  1396. return AVERROR_INVALIDDATA;
  1397. }
  1398. sl->pps_id = get_ue_golomb(&sl->gb);
  1399. if (sl->pps_id >= MAX_PPS_COUNT) {
  1400. av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
  1401. return AVERROR_INVALIDDATA;
  1402. }
  1403. if (!h->ps.pps_list[sl->pps_id]) {
  1404. av_log(h->avctx, AV_LOG_ERROR,
  1405. "non-existing PPS %u referenced\n",
  1406. sl->pps_id);
  1407. return AVERROR_INVALIDDATA;
  1408. }
  1409. pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
  1410. if (!h->ps.sps_list[pps->sps_id]) {
  1411. av_log(h->avctx, AV_LOG_ERROR,
  1412. "non-existing SPS %u referenced\n", pps->sps_id);
  1413. return AVERROR_INVALIDDATA;
  1414. }
  1415. sps = (const SPS*)h->ps.sps_list[pps->sps_id]->data;
  1416. sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
  1417. if (!first_slice) {
  1418. if (h->poc.frame_num != sl->frame_num) {
  1419. av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
  1420. h->poc.frame_num, sl->frame_num);
  1421. return AVERROR_INVALIDDATA;
  1422. }
  1423. }
  1424. sl->mb_mbaff = 0;
  1425. if (sps->frame_mbs_only_flag) {
  1426. picture_structure = PICT_FRAME;
  1427. } else {
  1428. if (!sps->direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
  1429. av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
  1430. return -1;
  1431. }
  1432. field_pic_flag = get_bits1(&sl->gb);
  1433. if (field_pic_flag) {
  1434. bottom_field_flag = get_bits1(&sl->gb);
  1435. picture_structure = PICT_TOP_FIELD + bottom_field_flag;
  1436. } else {
  1437. picture_structure = PICT_FRAME;
  1438. }
  1439. }
  1440. sl->picture_structure = picture_structure;
  1441. sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
  1442. if (picture_structure == PICT_FRAME) {
  1443. sl->curr_pic_num = sl->frame_num;
  1444. sl->max_pic_num = 1 << sps->log2_max_frame_num;
  1445. } else {
  1446. sl->curr_pic_num = 2 * sl->frame_num + 1;
  1447. sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
  1448. }
  1449. if (nal->type == H264_NAL_IDR_SLICE)
  1450. get_ue_golomb_long(&sl->gb); /* idr_pic_id */
  1451. if (sps->poc_type == 0) {
  1452. sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
  1453. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1454. sl->delta_poc_bottom = get_se_golomb(&sl->gb);
  1455. }
  1456. if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
  1457. sl->delta_poc[0] = get_se_golomb(&sl->gb);
  1458. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1459. sl->delta_poc[1] = get_se_golomb(&sl->gb);
  1460. }
  1461. sl->redundant_pic_count = 0;
  1462. if (pps->redundant_pic_cnt_present)
  1463. sl->redundant_pic_count = get_ue_golomb(&sl->gb);
  1464. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1465. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1466. ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
  1467. &sl->gb, pps, sl->slice_type_nos,
  1468. picture_structure, h->avctx);
  1469. if (ret < 0)
  1470. return ret;
  1471. if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1472. ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
  1473. if (ret < 0) {
  1474. sl->ref_count[1] = sl->ref_count[0] = 0;
  1475. return ret;
  1476. }
  1477. }
  1478. sl->pwt.use_weight = 0;
  1479. for (i = 0; i < 2; i++) {
  1480. sl->pwt.luma_weight_flag[i] = 0;
  1481. sl->pwt.chroma_weight_flag[i] = 0;
  1482. }
  1483. if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
  1484. (pps->weighted_bipred_idc == 1 &&
  1485. sl->slice_type_nos == AV_PICTURE_TYPE_B))
  1486. ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
  1487. sl->slice_type_nos, &sl->pwt, h->avctx);
  1488. sl->explicit_ref_marking = 0;
  1489. if (nal->ref_idc) {
  1490. ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
  1491. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1492. return AVERROR_INVALIDDATA;
  1493. }
  1494. if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
  1495. tmp = get_ue_golomb_31(&sl->gb);
  1496. if (tmp > 2) {
  1497. av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
  1498. return AVERROR_INVALIDDATA;
  1499. }
  1500. sl->cabac_init_idc = tmp;
  1501. }
  1502. sl->last_qscale_diff = 0;
  1503. tmp = pps->init_qp + get_se_golomb(&sl->gb);
  1504. if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
  1505. av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1506. return AVERROR_INVALIDDATA;
  1507. }
  1508. sl->qscale = tmp;
  1509. sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
  1510. sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
  1511. // FIXME qscale / qp ... stuff
  1512. if (sl->slice_type == AV_PICTURE_TYPE_SP)
  1513. get_bits1(&sl->gb); /* sp_for_switch_flag */
  1514. if (sl->slice_type == AV_PICTURE_TYPE_SP ||
  1515. sl->slice_type == AV_PICTURE_TYPE_SI)
  1516. get_se_golomb(&sl->gb); /* slice_qs_delta */
  1517. sl->deblocking_filter = 1;
  1518. sl->slice_alpha_c0_offset = 0;
  1519. sl->slice_beta_offset = 0;
  1520. if (pps->deblocking_filter_parameters_present) {
  1521. tmp = get_ue_golomb_31(&sl->gb);
  1522. if (tmp > 2) {
  1523. av_log(h->avctx, AV_LOG_ERROR,
  1524. "deblocking_filter_idc %u out of range\n", tmp);
  1525. return AVERROR_INVALIDDATA;
  1526. }
  1527. sl->deblocking_filter = tmp;
  1528. if (sl->deblocking_filter < 2)
  1529. sl->deblocking_filter ^= 1; // 1<->0
  1530. if (sl->deblocking_filter) {
  1531. sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
  1532. sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;
  1533. if (sl->slice_alpha_c0_offset > 12 ||
  1534. sl->slice_alpha_c0_offset < -12 ||
  1535. sl->slice_beta_offset > 12 ||
  1536. sl->slice_beta_offset < -12) {
  1537. av_log(h->avctx, AV_LOG_ERROR,
  1538. "deblocking filter parameters %d %d out of range\n",
  1539. sl->slice_alpha_c0_offset, sl->slice_beta_offset);
  1540. return AVERROR_INVALIDDATA;
  1541. }
  1542. }
  1543. }
  1544. return 0;
  1545. }
  1546. /**
  1547. * Decode a slice header.
  1548. * This will (re)initialize the decoder and call h264_frame_start() as needed.
  1549. *
  1550. * @param h h264context
  1551. *
  1552. * @return 0 if okay, <0 if an error occurred
  1553. */
  1554. int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl,
  1555. const H2645NAL *nal)
  1556. {
  1557. int i, j, ret = 0;
  1558. int first_slice = sl == h->slice_ctx && !h->current_slice;
  1559. ret = h264_slice_header_parse(h, sl, nal);
  1560. if (ret < 0)
  1561. return ret;
  1562. // discard redundant pictures
  1563. if (sl->redundant_pic_count > 0)
  1564. return 0;
  1565. if (sl->first_mb_addr == 0 || !h->current_slice) {
  1566. if (h->setup_finished) {
  1567. av_log(h->avctx, AV_LOG_ERROR, "Too many fields\n");
  1568. return AVERROR_INVALIDDATA;
  1569. }
  1570. }
  1571. if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
  1572. if (h->current_slice) {
  1573. if (h->max_contexts > 1) {
  1574. if (!h->single_decode_warning) {
  1575. av_log(h->avctx, AV_LOG_WARNING, "Cannot decode multiple access units as slice threads\n");
  1576. h->single_decode_warning = 1;
  1577. }
  1578. h->max_contexts = 1;
  1579. return SLICE_SINGLETHREAD;
  1580. }
  1581. if (h->cur_pic_ptr && FIELD_PICTURE(h) && h->first_field) {
  1582. ret = ff_h264_field_end(h, h->slice_ctx, 1);
  1583. if (ret < 0)
  1584. return ret;
  1585. } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type == H264_NAL_IDR_SLICE) {
  1586. av_log(h, AV_LOG_WARNING, "Broken frame packetizing\n");
  1587. ret = ff_h264_field_end(h, h->slice_ctx, 1);
  1588. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  1589. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  1590. h->cur_pic_ptr = NULL;
  1591. if (ret < 0)
  1592. return ret;
  1593. } else
  1594. return AVERROR_INVALIDDATA;
  1595. }
  1596. if (!h->first_field) {
  1597. if (h->cur_pic_ptr && !h->droppable) {
  1598. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1599. h->picture_structure == PICT_BOTTOM_FIELD);
  1600. }
  1601. h->cur_pic_ptr = NULL;
  1602. }
  1603. }
  1604. if (!h->current_slice)
  1605. av_assert0(sl == h->slice_ctx);
  1606. if (h->current_slice == 0 && !h->first_field) {
  1607. if (
  1608. (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
  1609. (h->avctx->skip_frame >= AVDISCARD_BIDIR && sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1610. (h->avctx->skip_frame >= AVDISCARD_NONINTRA && sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1611. (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != H264_NAL_IDR_SLICE && h->sei.recovery_point.recovery_frame_cnt < 0) ||
  1612. h->avctx->skip_frame >= AVDISCARD_ALL) {
  1613. return SLICE_SKIPED;
  1614. }
  1615. }
  1616. if (!first_slice) {
  1617. const PPS *pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
  1618. if (h->ps.pps->sps_id != pps->sps_id ||
  1619. h->ps.pps->transform_8x8_mode != pps->transform_8x8_mode /*||
  1620. (h->setup_finished && h->ps.pps != pps)*/) {
  1621. av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
  1622. return AVERROR_INVALIDDATA;
  1623. }
  1624. if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
  1625. av_log(h->avctx, AV_LOG_ERROR,
  1626. "SPS changed in the middle of the frame\n");
  1627. return AVERROR_INVALIDDATA;
  1628. }
  1629. }
  1630. if (h->current_slice == 0) {
  1631. ret = h264_field_start(h, sl, nal, first_slice);
  1632. if (ret < 0)
  1633. return ret;
  1634. } else {
  1635. if (h->picture_structure != sl->picture_structure ||
  1636. h->droppable != (nal->ref_idc == 0)) {
  1637. av_log(h->avctx, AV_LOG_ERROR,
  1638. "Changing field mode (%d -> %d) between slices is not allowed\n",
  1639. h->picture_structure, sl->picture_structure);
  1640. return AVERROR_INVALIDDATA;
  1641. } else if (!h->cur_pic_ptr) {
  1642. av_log(h->avctx, AV_LOG_ERROR,
  1643. "unset cur_pic_ptr on slice %d\n",
  1644. h->current_slice + 1);
  1645. return AVERROR_INVALIDDATA;
  1646. }
  1647. }
  1648. if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
  1649. av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
  1650. return AVERROR_INVALIDDATA;
  1651. }
  1652. av_assert1(h->mb_num == h->mb_width * h->mb_height);
  1653. if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  1654. sl->first_mb_addr >= h->mb_num) {
  1655. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1656. return AVERROR_INVALIDDATA;
  1657. }
  1658. sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
  1659. sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
  1660. FIELD_OR_MBAFF_PICTURE(h);
  1661. if (h->picture_structure == PICT_BOTTOM_FIELD)
  1662. sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
  1663. av_assert1(sl->mb_y < h->mb_height);
  1664. ret = ff_h264_build_ref_list(h, sl);
  1665. if (ret < 0)
  1666. return ret;
  1667. if (h->ps.pps->weighted_bipred_idc == 2 &&
  1668. sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1669. implicit_weight_table(h, sl, -1);
  1670. if (FRAME_MBAFF(h)) {
  1671. implicit_weight_table(h, sl, 0);
  1672. implicit_weight_table(h, sl, 1);
  1673. }
  1674. }
  1675. if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
  1676. ff_h264_direct_dist_scale_factor(h, sl);
  1677. ff_h264_direct_ref_list_init(h, sl);
  1678. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  1679. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  1680. h->nal_unit_type != H264_NAL_IDR_SLICE) ||
  1681. (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&
  1682. sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1683. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  1684. sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1685. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  1686. nal->ref_idc == 0))
  1687. sl->deblocking_filter = 0;
  1688. if (sl->deblocking_filter == 1 && h->max_contexts > 1) {
  1689. if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  1690. /* Cheat slightly for speed:
  1691. * Do not bother to deblock across slices. */
  1692. sl->deblocking_filter = 2;
  1693. } else {
  1694. h->postpone_filter = 1;
  1695. }
  1696. }
  1697. sl->qp_thresh = 15 -
  1698. FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
  1699. FFMAX3(0,
  1700. h->ps.pps->chroma_qp_index_offset[0],
  1701. h->ps.pps->chroma_qp_index_offset[1]) +
  1702. 6 * (h->ps.sps->bit_depth_luma - 8);
  1703. sl->slice_num = ++h->current_slice;
  1704. if (sl->slice_num)
  1705. h->slice_row[(sl->slice_num-1)&(MAX_SLICES-1)]= sl->resync_mb_y;
  1706. if ( h->slice_row[sl->slice_num&(MAX_SLICES-1)] + 3 >= sl->resync_mb_y
  1707. && h->slice_row[sl->slice_num&(MAX_SLICES-1)] <= sl->resync_mb_y
  1708. && sl->slice_num >= MAX_SLICES) {
  1709. //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
  1710. av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", sl->slice_num, MAX_SLICES);
  1711. }
  1712. for (j = 0; j < 2; j++) {
  1713. int id_list[16];
  1714. int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
  1715. for (i = 0; i < 16; i++) {
  1716. id_list[i] = 60;
  1717. if (j < sl->list_count && i < sl->ref_count[j] &&
  1718. sl->ref_list[j][i].parent->f->buf[0]) {
  1719. int k;
  1720. AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
  1721. for (k = 0; k < h->short_ref_count; k++)
  1722. if (h->short_ref[k]->f->buf[0]->buffer == buf) {
  1723. id_list[i] = k;
  1724. break;
  1725. }
  1726. for (k = 0; k < h->long_ref_count; k++)
  1727. if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
  1728. id_list[i] = h->short_ref_count + k;
  1729. break;
  1730. }
  1731. }
  1732. }
  1733. ref2frm[0] =
  1734. ref2frm[1] = -1;
  1735. for (i = 0; i < 16; i++)
  1736. ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
  1737. ref2frm[18 + 0] =
  1738. ref2frm[18 + 1] = -1;
  1739. for (i = 16; i < 48; i++)
  1740. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  1741. (sl->ref_list[j][i].reference & 3);
  1742. }
  1743. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  1744. av_log(h->avctx, AV_LOG_DEBUG,
  1745. "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",
  1746. sl->slice_num,
  1747. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  1748. sl->mb_y * h->mb_width + sl->mb_x,
  1749. av_get_picture_type_char(sl->slice_type),
  1750. sl->slice_type_fixed ? " fix" : "",
  1751. nal->type == H264_NAL_IDR_SLICE ? " IDR" : "",
  1752. h->poc.frame_num,
  1753. h->cur_pic_ptr->field_poc[0],
  1754. h->cur_pic_ptr->field_poc[1],
  1755. sl->ref_count[0], sl->ref_count[1],
  1756. sl->qscale,
  1757. sl->deblocking_filter,
  1758. sl->slice_alpha_c0_offset, sl->slice_beta_offset,
  1759. sl->pwt.use_weight,
  1760. sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
  1761. sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  1762. }
  1763. return 0;
  1764. }
  1765. int ff_h264_get_slice_type(const H264SliceContext *sl)
  1766. {
  1767. switch (sl->slice_type) {
  1768. case AV_PICTURE_TYPE_P:
  1769. return 0;
  1770. case AV_PICTURE_TYPE_B:
  1771. return 1;
  1772. case AV_PICTURE_TYPE_I:
  1773. return 2;
  1774. case AV_PICTURE_TYPE_SP:
  1775. return 3;
  1776. case AV_PICTURE_TYPE_SI:
  1777. return 4;
  1778. default:
  1779. return AVERROR_INVALIDDATA;
  1780. }
  1781. }
  1782. static av_always_inline void fill_filter_caches_inter(const H264Context *h,
  1783. H264SliceContext *sl,
  1784. int mb_type, int top_xy,
  1785. int left_xy[LEFT_MBS],
  1786. int top_type,
  1787. int left_type[LEFT_MBS],
  1788. int mb_xy, int list)
  1789. {
  1790. int b_stride = h->b_stride;
  1791. int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
  1792. int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
  1793. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  1794. if (USES_LIST(top_type, list)) {
  1795. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  1796. const int b8_xy = 4 * top_xy + 2;
  1797. const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1798. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  1799. ref_cache[0 - 1 * 8] =
  1800. ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
  1801. ref_cache[2 - 1 * 8] =
  1802. ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
  1803. } else {
  1804. AV_ZERO128(mv_dst - 1 * 8);
  1805. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1806. }
  1807. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  1808. if (USES_LIST(left_type[LTOP], list)) {
  1809. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  1810. const int b8_xy = 4 * left_xy[LTOP] + 1;
  1811. const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1812. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  1813. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  1814. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  1815. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  1816. ref_cache[-1 + 0] =
  1817. ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  1818. ref_cache[-1 + 16] =
  1819. ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  1820. } else {
  1821. AV_ZERO32(mv_dst - 1 + 0);
  1822. AV_ZERO32(mv_dst - 1 + 8);
  1823. AV_ZERO32(mv_dst - 1 + 16);
  1824. AV_ZERO32(mv_dst - 1 + 24);
  1825. ref_cache[-1 + 0] =
  1826. ref_cache[-1 + 8] =
  1827. ref_cache[-1 + 16] =
  1828. ref_cache[-1 + 24] = LIST_NOT_USED;
  1829. }
  1830. }
  1831. }
  1832. if (!USES_LIST(mb_type, list)) {
  1833. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  1834. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1835. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1836. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1837. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1838. return;
  1839. }
  1840. {
  1841. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  1842. const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1843. uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
  1844. uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
  1845. AV_WN32A(&ref_cache[0 * 8], ref01);
  1846. AV_WN32A(&ref_cache[1 * 8], ref01);
  1847. AV_WN32A(&ref_cache[2 * 8], ref23);
  1848. AV_WN32A(&ref_cache[3 * 8], ref23);
  1849. }
  1850. {
  1851. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
  1852. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  1853. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  1854. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  1855. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  1856. }
  1857. }
  1858. /**
  1859. * @return non zero if the loop filter can be skipped
  1860. */
  1861. static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
  1862. {
  1863. const int mb_xy = sl->mb_xy;
  1864. int top_xy, left_xy[LEFT_MBS];
  1865. int top_type, left_type[LEFT_MBS];
  1866. uint8_t *nnz;
  1867. uint8_t *nnz_cache;
  1868. top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
  1869. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  1870. if (FRAME_MBAFF(h)) {
  1871. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  1872. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  1873. if (sl->mb_y & 1) {
  1874. if (left_mb_field_flag != curr_mb_field_flag)
  1875. left_xy[LTOP] -= h->mb_stride;
  1876. } else {
  1877. if (curr_mb_field_flag)
  1878. top_xy += h->mb_stride &
  1879. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  1880. if (left_mb_field_flag != curr_mb_field_flag)
  1881. left_xy[LBOT] += h->mb_stride;
  1882. }
  1883. }
  1884. sl->top_mb_xy = top_xy;
  1885. sl->left_mb_xy[LTOP] = left_xy[LTOP];
  1886. sl->left_mb_xy[LBOT] = left_xy[LBOT];
  1887. {
  1888. /* For sufficiently low qp, filtering wouldn't do anything.
  1889. * This is a conservative estimate: could also check beta_offset
  1890. * and more accurate chroma_qp. */
  1891. int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  1892. int qp = h->cur_pic.qscale_table[mb_xy];
  1893. if (qp <= qp_thresh &&
  1894. (left_xy[LTOP] < 0 ||
  1895. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  1896. (top_xy < 0 ||
  1897. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  1898. if (!FRAME_MBAFF(h))
  1899. return 1;
  1900. if ((left_xy[LTOP] < 0 ||
  1901. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  1902. (top_xy < h->mb_stride ||
  1903. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  1904. return 1;
  1905. }
  1906. }
  1907. top_type = h->cur_pic.mb_type[top_xy];
  1908. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  1909. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  1910. if (sl->deblocking_filter == 2) {
  1911. if (h->slice_table[top_xy] != sl->slice_num)
  1912. top_type = 0;
  1913. if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
  1914. left_type[LTOP] = left_type[LBOT] = 0;
  1915. } else {
  1916. if (h->slice_table[top_xy] == 0xFFFF)
  1917. top_type = 0;
  1918. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  1919. left_type[LTOP] = left_type[LBOT] = 0;
  1920. }
  1921. sl->top_type = top_type;
  1922. sl->left_type[LTOP] = left_type[LTOP];
  1923. sl->left_type[LBOT] = left_type[LBOT];
  1924. if (IS_INTRA(mb_type))
  1925. return 0;
  1926. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1927. top_type, left_type, mb_xy, 0);
  1928. if (sl->list_count == 2)
  1929. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1930. top_type, left_type, mb_xy, 1);
  1931. nnz = h->non_zero_count[mb_xy];
  1932. nnz_cache = sl->non_zero_count_cache;
  1933. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  1934. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  1935. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  1936. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  1937. sl->cbp = h->cbp_table[mb_xy];
  1938. if (top_type) {
  1939. nnz = h->non_zero_count[top_xy];
  1940. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  1941. }
  1942. if (left_type[LTOP]) {
  1943. nnz = h->non_zero_count[left_xy[LTOP]];
  1944. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  1945. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  1946. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  1947. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  1948. }
  1949. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  1950. * from what the loop filter needs */
  1951. if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
  1952. if (IS_8x8DCT(top_type)) {
  1953. nnz_cache[4 + 8 * 0] =
  1954. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  1955. nnz_cache[6 + 8 * 0] =
  1956. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  1957. }
  1958. if (IS_8x8DCT(left_type[LTOP])) {
  1959. nnz_cache[3 + 8 * 1] =
  1960. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  1961. }
  1962. if (IS_8x8DCT(left_type[LBOT])) {
  1963. nnz_cache[3 + 8 * 3] =
  1964. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  1965. }
  1966. if (IS_8x8DCT(mb_type)) {
  1967. nnz_cache[scan8[0]] =
  1968. nnz_cache[scan8[1]] =
  1969. nnz_cache[scan8[2]] =
  1970. nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
  1971. nnz_cache[scan8[0 + 4]] =
  1972. nnz_cache[scan8[1 + 4]] =
  1973. nnz_cache[scan8[2 + 4]] =
  1974. nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
  1975. nnz_cache[scan8[0 + 8]] =
  1976. nnz_cache[scan8[1 + 8]] =
  1977. nnz_cache[scan8[2 + 8]] =
  1978. nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
  1979. nnz_cache[scan8[0 + 12]] =
  1980. nnz_cache[scan8[1 + 12]] =
  1981. nnz_cache[scan8[2 + 12]] =
  1982. nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
  1983. }
  1984. }
  1985. return 0;
  1986. }
  1987. static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
  1988. {
  1989. uint8_t *dest_y, *dest_cb, *dest_cr;
  1990. int linesize, uvlinesize, mb_x, mb_y;
  1991. const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
  1992. const int old_slice_type = sl->slice_type;
  1993. const int pixel_shift = h->pixel_shift;
  1994. const int block_h = 16 >> h->chroma_y_shift;
  1995. if (h->postpone_filter)
  1996. return;
  1997. if (sl->deblocking_filter) {
  1998. for (mb_x = start_x; mb_x < end_x; mb_x++)
  1999. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  2000. int mb_xy, mb_type;
  2001. mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
  2002. mb_type = h->cur_pic.mb_type[mb_xy];
  2003. if (FRAME_MBAFF(h))
  2004. sl->mb_mbaff =
  2005. sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2006. sl->mb_x = mb_x;
  2007. sl->mb_y = mb_y;
  2008. dest_y = h->cur_pic.f->data[0] +
  2009. ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
  2010. dest_cb = h->cur_pic.f->data[1] +
  2011. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  2012. mb_y * sl->uvlinesize * block_h;
  2013. dest_cr = h->cur_pic.f->data[2] +
  2014. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  2015. mb_y * sl->uvlinesize * block_h;
  2016. // FIXME simplify above
  2017. if (MB_FIELD(sl)) {
  2018. linesize = sl->mb_linesize = sl->linesize * 2;
  2019. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
  2020. if (mb_y & 1) { // FIXME move out of this function?
  2021. dest_y -= sl->linesize * 15;
  2022. dest_cb -= sl->uvlinesize * (block_h - 1);
  2023. dest_cr -= sl->uvlinesize * (block_h - 1);
  2024. }
  2025. } else {
  2026. linesize = sl->mb_linesize = sl->linesize;
  2027. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
  2028. }
  2029. backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
  2030. uvlinesize, 0);
  2031. if (fill_filter_caches(h, sl, mb_type))
  2032. continue;
  2033. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
  2034. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]);
  2035. if (FRAME_MBAFF(h)) {
  2036. ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  2037. linesize, uvlinesize);
  2038. } else {
  2039. ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
  2040. dest_cr, linesize, uvlinesize);
  2041. }
  2042. }
  2043. }
  2044. sl->slice_type = old_slice_type;
  2045. sl->mb_x = end_x;
  2046. sl->mb_y = end_mb_y - FRAME_MBAFF(h);
  2047. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
  2048. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
  2049. }
  2050. static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
  2051. {
  2052. const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
  2053. int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
  2054. h->cur_pic.mb_type[mb_xy - 1] :
  2055. (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
  2056. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  2057. sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2058. }
  2059. /**
  2060. * Draw edges and report progress for the last MB row.
  2061. */
  2062. static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
  2063. {
  2064. int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
  2065. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  2066. int height = 16 << FRAME_MBAFF(h);
  2067. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  2068. if (sl->deblocking_filter) {
  2069. if ((top + height) >= pic_height)
  2070. height += deblock_border;
  2071. top -= deblock_border;
  2072. }
  2073. if (top >= pic_height || (top + height) < 0)
  2074. return;
  2075. height = FFMIN(height, pic_height - top);
  2076. if (top < 0) {
  2077. height = top + height;
  2078. top = 0;
  2079. }
  2080. ff_h264_draw_horiz_band(h, sl, top, height);
  2081. if (h->droppable || sl->h264->slice_ctx[0].er.error_occurred)
  2082. return;
  2083. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  2084. h->picture_structure == PICT_BOTTOM_FIELD);
  2085. }
  2086. static void er_add_slice(H264SliceContext *sl,
  2087. int startx, int starty,
  2088. int endx, int endy, int status)
  2089. {
  2090. if (!sl->h264->enable_er)
  2091. return;
  2092. if (CONFIG_ERROR_RESILIENCE) {
  2093. ERContext *er = &sl->h264->slice_ctx[0].er;
  2094. ff_er_add_slice(er, startx, starty, endx, endy, status);
  2095. }
  2096. }
  2097. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  2098. {
  2099. H264SliceContext *sl = arg;
  2100. const H264Context *h = sl->h264;
  2101. int lf_x_start = sl->mb_x;
  2102. int orig_deblock = sl->deblocking_filter;
  2103. int ret;
  2104. sl->linesize = h->cur_pic_ptr->f->linesize[0];
  2105. sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
  2106. ret = alloc_scratch_buffers(sl, sl->linesize);
  2107. if (ret < 0)
  2108. return ret;
  2109. sl->mb_skip_run = -1;
  2110. av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * sl->linesize * ((scan8[15] - scan8[0]) >> 3));
  2111. if (h->postpone_filter)
  2112. sl->deblocking_filter = 0;
  2113. sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  2114. (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
  2115. if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && h->slice_ctx[0].er.error_status_table) {
  2116. const int start_i = av_clip(sl->resync_mb_x + sl->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
  2117. if (start_i) {
  2118. int prev_status = h->slice_ctx[0].er.error_status_table[h->slice_ctx[0].er.mb_index2xy[start_i - 1]];
  2119. prev_status &= ~ VP_START;
  2120. if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
  2121. h->slice_ctx[0].er.error_occurred = 1;
  2122. }
  2123. }
  2124. if (h->ps.pps->cabac) {
  2125. /* realign */
  2126. align_get_bits(&sl->gb);
  2127. /* init cabac */
  2128. ret = ff_init_cabac_decoder(&sl->cabac,
  2129. sl->gb.buffer + get_bits_count(&sl->gb) / 8,
  2130. (get_bits_left(&sl->gb) + 7) / 8);
  2131. if (ret < 0)
  2132. return ret;
  2133. ff_h264_init_cabac_states(h, sl);
  2134. for (;;) {
  2135. // START_TIMER
  2136. int ret, eos;
  2137. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2138. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2139. sl->next_slice_idx);
  2140. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2141. sl->mb_y, ER_MB_ERROR);
  2142. return AVERROR_INVALIDDATA;
  2143. }
  2144. ret = ff_h264_decode_mb_cabac(h, sl);
  2145. // STOP_TIMER("decode_mb_cabac")
  2146. if (ret >= 0)
  2147. ff_h264_hl_decode_mb(h, sl);
  2148. // FIXME optimal? or let mb_decode decode 16x32 ?
  2149. if (ret >= 0 && FRAME_MBAFF(h)) {
  2150. sl->mb_y++;
  2151. ret = ff_h264_decode_mb_cabac(h, sl);
  2152. if (ret >= 0)
  2153. ff_h264_hl_decode_mb(h, sl);
  2154. sl->mb_y--;
  2155. }
  2156. eos = get_cabac_terminate(&sl->cabac);
  2157. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  2158. sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  2159. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2160. sl->mb_y, ER_MB_END);
  2161. if (sl->mb_x >= lf_x_start)
  2162. loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
  2163. goto finish;
  2164. }
  2165. if (sl->cabac.bytestream > sl->cabac.bytestream_end + 2 )
  2166. av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %"PTRDIFF_SPECIFIER"\n", sl->cabac.bytestream_end - sl->cabac.bytestream);
  2167. if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 4) {
  2168. av_log(h->avctx, AV_LOG_ERROR,
  2169. "error while decoding MB %d %d, bytestream %"PTRDIFF_SPECIFIER"\n",
  2170. sl->mb_x, sl->mb_y,
  2171. sl->cabac.bytestream_end - sl->cabac.bytestream);
  2172. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2173. sl->mb_y, ER_MB_ERROR);
  2174. return AVERROR_INVALIDDATA;
  2175. }
  2176. if (++sl->mb_x >= h->mb_width) {
  2177. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2178. sl->mb_x = lf_x_start = 0;
  2179. decode_finish_row(h, sl);
  2180. ++sl->mb_y;
  2181. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2182. ++sl->mb_y;
  2183. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2184. predict_field_decoding_flag(h, sl);
  2185. }
  2186. }
  2187. if (eos || sl->mb_y >= h->mb_height) {
  2188. ff_tlog(h->avctx, "slice end %d %d\n",
  2189. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2190. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2191. sl->mb_y, ER_MB_END);
  2192. if (sl->mb_x > lf_x_start)
  2193. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2194. goto finish;
  2195. }
  2196. }
  2197. } else {
  2198. for (;;) {
  2199. int ret;
  2200. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2201. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2202. sl->next_slice_idx);
  2203. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2204. sl->mb_y, ER_MB_ERROR);
  2205. return AVERROR_INVALIDDATA;
  2206. }
  2207. ret = ff_h264_decode_mb_cavlc(h, sl);
  2208. if (ret >= 0)
  2209. ff_h264_hl_decode_mb(h, sl);
  2210. // FIXME optimal? or let mb_decode decode 16x32 ?
  2211. if (ret >= 0 && FRAME_MBAFF(h)) {
  2212. sl->mb_y++;
  2213. ret = ff_h264_decode_mb_cavlc(h, sl);
  2214. if (ret >= 0)
  2215. ff_h264_hl_decode_mb(h, sl);
  2216. sl->mb_y--;
  2217. }
  2218. if (ret < 0) {
  2219. av_log(h->avctx, AV_LOG_ERROR,
  2220. "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
  2221. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2222. sl->mb_y, ER_MB_ERROR);
  2223. return ret;
  2224. }
  2225. if (++sl->mb_x >= h->mb_width) {
  2226. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2227. sl->mb_x = lf_x_start = 0;
  2228. decode_finish_row(h, sl);
  2229. ++sl->mb_y;
  2230. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2231. ++sl->mb_y;
  2232. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2233. predict_field_decoding_flag(h, sl);
  2234. }
  2235. if (sl->mb_y >= h->mb_height) {
  2236. ff_tlog(h->avctx, "slice end %d %d\n",
  2237. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2238. if ( get_bits_left(&sl->gb) == 0
  2239. || get_bits_left(&sl->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
  2240. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2241. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2242. goto finish;
  2243. } else {
  2244. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2245. sl->mb_x, sl->mb_y, ER_MB_END);
  2246. return AVERROR_INVALIDDATA;
  2247. }
  2248. }
  2249. }
  2250. if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
  2251. ff_tlog(h->avctx, "slice end %d %d\n",
  2252. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2253. if (get_bits_left(&sl->gb) == 0) {
  2254. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2255. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2256. if (sl->mb_x > lf_x_start)
  2257. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2258. goto finish;
  2259. } else {
  2260. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2261. sl->mb_y, ER_MB_ERROR);
  2262. return AVERROR_INVALIDDATA;
  2263. }
  2264. }
  2265. }
  2266. }
  2267. finish:
  2268. sl->deblocking_filter = orig_deblock;
  2269. return 0;
  2270. }
  2271. /**
  2272. * Call decode_slice() for each context.
  2273. *
  2274. * @param h h264 master context
  2275. * @param context_count number of contexts to execute
  2276. */
  2277. int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
  2278. {
  2279. AVCodecContext *const avctx = h->avctx;
  2280. H264SliceContext *sl;
  2281. int i, j;
  2282. av_assert0(context_count && h->slice_ctx[context_count - 1].mb_y < h->mb_height);
  2283. h->slice_ctx[0].next_slice_idx = INT_MAX;
  2284. if (h->avctx->hwaccel
  2285. #if FF_API_CAP_VDPAU
  2286. || h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU
  2287. #endif
  2288. )
  2289. return 0;
  2290. if (context_count == 1) {
  2291. int ret;
  2292. h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
  2293. h->postpone_filter = 0;
  2294. ret = decode_slice(avctx, &h->slice_ctx[0]);
  2295. h->mb_y = h->slice_ctx[0].mb_y;
  2296. return ret;
  2297. } else {
  2298. av_assert0(context_count > 0);
  2299. for (i = 0; i < context_count; i++) {
  2300. int next_slice_idx = h->mb_width * h->mb_height;
  2301. int slice_idx;
  2302. sl = &h->slice_ctx[i];
  2303. if (CONFIG_ERROR_RESILIENCE) {
  2304. sl->er.error_count = 0;
  2305. }
  2306. /* make sure none of those slices overlap */
  2307. slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
  2308. for (j = 0; j < context_count; j++) {
  2309. H264SliceContext *sl2 = &h->slice_ctx[j];
  2310. int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
  2311. if (i == j || slice_idx2 < slice_idx)
  2312. continue;
  2313. next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
  2314. }
  2315. sl->next_slice_idx = next_slice_idx;
  2316. }
  2317. avctx->execute(avctx, decode_slice, h->slice_ctx,
  2318. NULL, context_count, sizeof(h->slice_ctx[0]));
  2319. /* pull back stuff from slices to master context */
  2320. sl = &h->slice_ctx[context_count - 1];
  2321. h->mb_y = sl->mb_y;
  2322. if (CONFIG_ERROR_RESILIENCE) {
  2323. for (i = 1; i < context_count; i++)
  2324. h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count;
  2325. }
  2326. if (h->postpone_filter) {
  2327. h->postpone_filter = 0;
  2328. for (i = 0; i < context_count; i++) {
  2329. int y_end, x_end;
  2330. sl = &h->slice_ctx[i];
  2331. y_end = FFMIN(sl->mb_y + 1, h->mb_height);
  2332. x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
  2333. for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
  2334. sl->mb_y = j;
  2335. loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
  2336. j == y_end - 1 ? x_end : h->mb_width);
  2337. }
  2338. }
  2339. }
  2340. }
  2341. return 0;
  2342. }