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.

2683 lines
101KB

  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 == 2LL * 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 * (2 - h->ps.sps->frame_mbs_only_flag) ||
  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 * (2 - sps->frame_mbs_only_flag) != 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 * (2 - sps->frame_mbs_only_flag)
  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 * (2 - sps->frame_mbs_only_flag);
  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. av_image_copy(h->short_ref[0]->f->data,
  1259. h->short_ref[0]->f->linesize,
  1260. (const uint8_t **)prev->f->data,
  1261. prev->f->linesize,
  1262. prev->f->format,
  1263. prev->f->width,
  1264. prev->f->height);
  1265. h->short_ref[0]->poc = prev->poc + 2;
  1266. }
  1267. h->short_ref[0]->frame_num = h->poc.prev_frame_num;
  1268. }
  1269. }
  1270. /* See if we have a decoded first field looking for a pair...
  1271. * We're using that to see whether to continue decoding in that
  1272. * frame, or to allocate a new one. */
  1273. if (h->first_field) {
  1274. av_assert0(h->cur_pic_ptr);
  1275. av_assert0(h->cur_pic_ptr->f->buf[0]);
  1276. assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1277. /* figure out if we have a complementary field pair */
  1278. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1279. /* Previous field is unmatched. Don't display it, but let it
  1280. * remain for reference if marked as such. */
  1281. h->missing_fields ++;
  1282. h->cur_pic_ptr = NULL;
  1283. h->first_field = FIELD_PICTURE(h);
  1284. } else {
  1285. h->missing_fields = 0;
  1286. if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
  1287. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1288. h->picture_structure==PICT_BOTTOM_FIELD);
  1289. /* This and the previous field had different frame_nums.
  1290. * Consider this field first in pair. Throw away previous
  1291. * one except for reference purposes. */
  1292. h->first_field = 1;
  1293. h->cur_pic_ptr = NULL;
  1294. } else if (h->cur_pic_ptr->reference & DELAYED_PIC_REF) {
  1295. /* This frame was already output, we cannot draw into it
  1296. * anymore.
  1297. */
  1298. h->first_field = 1;
  1299. h->cur_pic_ptr = NULL;
  1300. } else {
  1301. /* Second field in complementary pair */
  1302. h->first_field = 0;
  1303. }
  1304. }
  1305. } else {
  1306. /* Frame or first field in a potentially complementary pair */
  1307. h->first_field = FIELD_PICTURE(h);
  1308. }
  1309. if (!FIELD_PICTURE(h) || h->first_field) {
  1310. if (h264_frame_start(h) < 0) {
  1311. h->first_field = 0;
  1312. return AVERROR_INVALIDDATA;
  1313. }
  1314. } else {
  1315. release_unused_pictures(h, 0);
  1316. }
  1317. /* Some macroblocks can be accessed before they're available in case
  1318. * of lost slices, MBAFF or threading. */
  1319. if (FIELD_PICTURE(h)) {
  1320. for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
  1321. memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
  1322. } else {
  1323. memset(h->slice_table, -1,
  1324. (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
  1325. }
  1326. ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
  1327. h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
  1328. memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
  1329. h->nb_mmco = sl->nb_mmco;
  1330. h->explicit_ref_marking = sl->explicit_ref_marking;
  1331. h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
  1332. if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
  1333. const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
  1334. if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
  1335. h->valid_recovery_point = 1;
  1336. if ( h->recovery_frame < 0
  1337. || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
  1338. h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
  1339. if (!h->valid_recovery_point)
  1340. h->recovery_frame = h->poc.frame_num;
  1341. }
  1342. }
  1343. h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE);
  1344. if (nal->type == H264_NAL_IDR_SLICE ||
  1345. (h->recovery_frame == h->poc.frame_num && nal->ref_idc)) {
  1346. h->recovery_frame = -1;
  1347. h->cur_pic_ptr->recovered = 1;
  1348. }
  1349. // If we have an IDR, all frames after it in decoded order are
  1350. // "recovered".
  1351. if (nal->type == H264_NAL_IDR_SLICE)
  1352. h->frame_recovered |= FRAME_RECOVERED_IDR;
  1353. #if 1
  1354. h->cur_pic_ptr->recovered |= h->frame_recovered;
  1355. #else
  1356. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  1357. #endif
  1358. /* Set the frame properties/side data. Only done for the second field in
  1359. * field coded frames, since some SEI information is present for each field
  1360. * and is merged by the SEI parsing code. */
  1361. if (!FIELD_PICTURE(h) || !h->first_field) {
  1362. ret = h264_export_frame_props(h);
  1363. if (ret < 0)
  1364. return ret;
  1365. }
  1366. return 0;
  1367. }
  1368. static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
  1369. const H2645NAL *nal)
  1370. {
  1371. const SPS *sps;
  1372. const PPS *pps;
  1373. int ret;
  1374. unsigned int slice_type, tmp, i;
  1375. int field_pic_flag, bottom_field_flag;
  1376. int first_slice = sl == h->slice_ctx && !h->current_slice;
  1377. int picture_structure;
  1378. if (first_slice)
  1379. av_assert0(!h->setup_finished);
  1380. sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
  1381. slice_type = get_ue_golomb_31(&sl->gb);
  1382. if (slice_type > 9) {
  1383. av_log(h->avctx, AV_LOG_ERROR,
  1384. "slice type %d too large at %d\n",
  1385. slice_type, sl->first_mb_addr);
  1386. return AVERROR_INVALIDDATA;
  1387. }
  1388. if (slice_type > 4) {
  1389. slice_type -= 5;
  1390. sl->slice_type_fixed = 1;
  1391. } else
  1392. sl->slice_type_fixed = 0;
  1393. slice_type = ff_h264_golomb_to_pict_type[slice_type];
  1394. sl->slice_type = slice_type;
  1395. sl->slice_type_nos = slice_type & 3;
  1396. if (nal->type == H264_NAL_IDR_SLICE &&
  1397. sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1398. av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
  1399. return AVERROR_INVALIDDATA;
  1400. }
  1401. sl->pps_id = get_ue_golomb(&sl->gb);
  1402. if (sl->pps_id >= MAX_PPS_COUNT) {
  1403. av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
  1404. return AVERROR_INVALIDDATA;
  1405. }
  1406. if (!h->ps.pps_list[sl->pps_id]) {
  1407. av_log(h->avctx, AV_LOG_ERROR,
  1408. "non-existing PPS %u referenced\n",
  1409. sl->pps_id);
  1410. return AVERROR_INVALIDDATA;
  1411. }
  1412. pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
  1413. if (!h->ps.sps_list[pps->sps_id]) {
  1414. av_log(h->avctx, AV_LOG_ERROR,
  1415. "non-existing SPS %u referenced\n", pps->sps_id);
  1416. return AVERROR_INVALIDDATA;
  1417. }
  1418. sps = (const SPS*)h->ps.sps_list[pps->sps_id]->data;
  1419. sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
  1420. if (!first_slice) {
  1421. if (h->poc.frame_num != sl->frame_num) {
  1422. av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
  1423. h->poc.frame_num, sl->frame_num);
  1424. return AVERROR_INVALIDDATA;
  1425. }
  1426. }
  1427. sl->mb_mbaff = 0;
  1428. if (sps->frame_mbs_only_flag) {
  1429. picture_structure = PICT_FRAME;
  1430. } else {
  1431. if (!sps->direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
  1432. av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
  1433. return -1;
  1434. }
  1435. field_pic_flag = get_bits1(&sl->gb);
  1436. if (field_pic_flag) {
  1437. bottom_field_flag = get_bits1(&sl->gb);
  1438. picture_structure = PICT_TOP_FIELD + bottom_field_flag;
  1439. } else {
  1440. picture_structure = PICT_FRAME;
  1441. }
  1442. }
  1443. sl->picture_structure = picture_structure;
  1444. sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
  1445. if (picture_structure == PICT_FRAME) {
  1446. sl->curr_pic_num = sl->frame_num;
  1447. sl->max_pic_num = 1 << sps->log2_max_frame_num;
  1448. } else {
  1449. sl->curr_pic_num = 2 * sl->frame_num + 1;
  1450. sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
  1451. }
  1452. if (nal->type == H264_NAL_IDR_SLICE)
  1453. get_ue_golomb_long(&sl->gb); /* idr_pic_id */
  1454. if (sps->poc_type == 0) {
  1455. sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
  1456. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1457. sl->delta_poc_bottom = get_se_golomb(&sl->gb);
  1458. }
  1459. if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
  1460. sl->delta_poc[0] = get_se_golomb(&sl->gb);
  1461. if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
  1462. sl->delta_poc[1] = get_se_golomb(&sl->gb);
  1463. }
  1464. sl->redundant_pic_count = 0;
  1465. if (pps->redundant_pic_cnt_present)
  1466. sl->redundant_pic_count = get_ue_golomb(&sl->gb);
  1467. if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
  1468. sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
  1469. ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
  1470. &sl->gb, pps, sl->slice_type_nos,
  1471. picture_structure, h->avctx);
  1472. if (ret < 0)
  1473. return ret;
  1474. if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
  1475. ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
  1476. if (ret < 0) {
  1477. sl->ref_count[1] = sl->ref_count[0] = 0;
  1478. return ret;
  1479. }
  1480. }
  1481. sl->pwt.use_weight = 0;
  1482. for (i = 0; i < 2; i++) {
  1483. sl->pwt.luma_weight_flag[i] = 0;
  1484. sl->pwt.chroma_weight_flag[i] = 0;
  1485. }
  1486. if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
  1487. (pps->weighted_bipred_idc == 1 &&
  1488. sl->slice_type_nos == AV_PICTURE_TYPE_B)) {
  1489. ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
  1490. sl->slice_type_nos, &sl->pwt,
  1491. picture_structure, h->avctx);
  1492. if (ret < 0)
  1493. return ret;
  1494. }
  1495. sl->explicit_ref_marking = 0;
  1496. if (nal->ref_idc) {
  1497. ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
  1498. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1499. return AVERROR_INVALIDDATA;
  1500. }
  1501. if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
  1502. tmp = get_ue_golomb_31(&sl->gb);
  1503. if (tmp > 2) {
  1504. av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
  1505. return AVERROR_INVALIDDATA;
  1506. }
  1507. sl->cabac_init_idc = tmp;
  1508. }
  1509. sl->last_qscale_diff = 0;
  1510. tmp = pps->init_qp + (unsigned)get_se_golomb(&sl->gb);
  1511. if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
  1512. av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1513. return AVERROR_INVALIDDATA;
  1514. }
  1515. sl->qscale = tmp;
  1516. sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
  1517. sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
  1518. // FIXME qscale / qp ... stuff
  1519. if (sl->slice_type == AV_PICTURE_TYPE_SP)
  1520. get_bits1(&sl->gb); /* sp_for_switch_flag */
  1521. if (sl->slice_type == AV_PICTURE_TYPE_SP ||
  1522. sl->slice_type == AV_PICTURE_TYPE_SI)
  1523. get_se_golomb(&sl->gb); /* slice_qs_delta */
  1524. sl->deblocking_filter = 1;
  1525. sl->slice_alpha_c0_offset = 0;
  1526. sl->slice_beta_offset = 0;
  1527. if (pps->deblocking_filter_parameters_present) {
  1528. tmp = get_ue_golomb_31(&sl->gb);
  1529. if (tmp > 2) {
  1530. av_log(h->avctx, AV_LOG_ERROR,
  1531. "deblocking_filter_idc %u out of range\n", tmp);
  1532. return AVERROR_INVALIDDATA;
  1533. }
  1534. sl->deblocking_filter = tmp;
  1535. if (sl->deblocking_filter < 2)
  1536. sl->deblocking_filter ^= 1; // 1<->0
  1537. if (sl->deblocking_filter) {
  1538. int slice_alpha_c0_offset_div2 = get_se_golomb(&sl->gb);
  1539. int slice_beta_offset_div2 = get_se_golomb(&sl->gb);
  1540. if (slice_alpha_c0_offset_div2 > 6 ||
  1541. slice_alpha_c0_offset_div2 < -6 ||
  1542. slice_beta_offset_div2 > 6 ||
  1543. slice_beta_offset_div2 < -6) {
  1544. av_log(h->avctx, AV_LOG_ERROR,
  1545. "deblocking filter parameters %d %d out of range\n",
  1546. slice_alpha_c0_offset_div2, slice_beta_offset_div2);
  1547. return AVERROR_INVALIDDATA;
  1548. }
  1549. sl->slice_alpha_c0_offset = slice_alpha_c0_offset_div2 * 2;
  1550. sl->slice_beta_offset = slice_beta_offset_div2 * 2;
  1551. }
  1552. }
  1553. return 0;
  1554. }
  1555. /**
  1556. * Decode a slice header.
  1557. * This will (re)initialize the decoder and call h264_frame_start() as needed.
  1558. *
  1559. * @param h h264context
  1560. *
  1561. * @return 0 if okay, <0 if an error occurred
  1562. */
  1563. int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl,
  1564. const H2645NAL *nal)
  1565. {
  1566. int i, j, ret = 0;
  1567. int first_slice = sl == h->slice_ctx && !h->current_slice;
  1568. ret = h264_slice_header_parse(h, sl, nal);
  1569. if (ret < 0)
  1570. return ret;
  1571. // discard redundant pictures
  1572. if (sl->redundant_pic_count > 0) {
  1573. sl->ref_count[0] = sl->ref_count[1] = 0;
  1574. return 0;
  1575. }
  1576. if (sl->first_mb_addr == 0 || !h->current_slice) {
  1577. if (h->setup_finished) {
  1578. av_log(h->avctx, AV_LOG_ERROR, "Too many fields\n");
  1579. return AVERROR_INVALIDDATA;
  1580. }
  1581. }
  1582. if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
  1583. if (h->current_slice) {
  1584. if (h->max_contexts > 1) {
  1585. if (!h->single_decode_warning) {
  1586. av_log(h->avctx, AV_LOG_WARNING, "Cannot decode multiple access units as slice threads\n");
  1587. h->single_decode_warning = 1;
  1588. }
  1589. h->max_contexts = 1;
  1590. return SLICE_SINGLETHREAD;
  1591. }
  1592. if (h->cur_pic_ptr && FIELD_PICTURE(h) && h->first_field) {
  1593. ret = ff_h264_field_end(h, h->slice_ctx, 1);
  1594. h->current_slice = 0;
  1595. if (ret < 0)
  1596. return ret;
  1597. } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type == H264_NAL_IDR_SLICE) {
  1598. av_log(h, AV_LOG_WARNING, "Broken frame packetizing\n");
  1599. ret = ff_h264_field_end(h, h->slice_ctx, 1);
  1600. h->current_slice = 0;
  1601. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  1602. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  1603. h->cur_pic_ptr = NULL;
  1604. if (ret < 0)
  1605. return ret;
  1606. } else
  1607. return AVERROR_INVALIDDATA;
  1608. }
  1609. if (!h->first_field) {
  1610. if (h->cur_pic_ptr && !h->droppable) {
  1611. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1612. h->picture_structure == PICT_BOTTOM_FIELD);
  1613. }
  1614. h->cur_pic_ptr = NULL;
  1615. }
  1616. }
  1617. if (!h->current_slice)
  1618. av_assert0(sl == h->slice_ctx);
  1619. if (h->current_slice == 0 && !h->first_field) {
  1620. if (
  1621. (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
  1622. (h->avctx->skip_frame >= AVDISCARD_BIDIR && sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1623. (h->avctx->skip_frame >= AVDISCARD_NONINTRA && sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1624. (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != H264_NAL_IDR_SLICE && h->sei.recovery_point.recovery_frame_cnt < 0) ||
  1625. h->avctx->skip_frame >= AVDISCARD_ALL) {
  1626. return SLICE_SKIPED;
  1627. }
  1628. }
  1629. if (!first_slice) {
  1630. const PPS *pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
  1631. if (h->ps.pps->sps_id != pps->sps_id ||
  1632. h->ps.pps->transform_8x8_mode != pps->transform_8x8_mode /*||
  1633. (h->setup_finished && h->ps.pps != pps)*/) {
  1634. av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
  1635. return AVERROR_INVALIDDATA;
  1636. }
  1637. if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
  1638. av_log(h->avctx, AV_LOG_ERROR,
  1639. "SPS changed in the middle of the frame\n");
  1640. return AVERROR_INVALIDDATA;
  1641. }
  1642. }
  1643. if (h->current_slice == 0) {
  1644. ret = h264_field_start(h, sl, nal, first_slice);
  1645. if (ret < 0)
  1646. return ret;
  1647. } else {
  1648. if (h->picture_structure != sl->picture_structure ||
  1649. h->droppable != (nal->ref_idc == 0)) {
  1650. av_log(h->avctx, AV_LOG_ERROR,
  1651. "Changing field mode (%d -> %d) between slices is not allowed\n",
  1652. h->picture_structure, sl->picture_structure);
  1653. return AVERROR_INVALIDDATA;
  1654. } else if (!h->cur_pic_ptr) {
  1655. av_log(h->avctx, AV_LOG_ERROR,
  1656. "unset cur_pic_ptr on slice %d\n",
  1657. h->current_slice + 1);
  1658. return AVERROR_INVALIDDATA;
  1659. }
  1660. }
  1661. if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
  1662. av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
  1663. return AVERROR_INVALIDDATA;
  1664. }
  1665. av_assert1(h->mb_num == h->mb_width * h->mb_height);
  1666. if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  1667. sl->first_mb_addr >= h->mb_num) {
  1668. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1669. return AVERROR_INVALIDDATA;
  1670. }
  1671. sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
  1672. sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
  1673. FIELD_OR_MBAFF_PICTURE(h);
  1674. if (h->picture_structure == PICT_BOTTOM_FIELD)
  1675. sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
  1676. av_assert1(sl->mb_y < h->mb_height);
  1677. ret = ff_h264_build_ref_list(h, sl);
  1678. if (ret < 0)
  1679. return ret;
  1680. if (h->ps.pps->weighted_bipred_idc == 2 &&
  1681. sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  1682. implicit_weight_table(h, sl, -1);
  1683. if (FRAME_MBAFF(h)) {
  1684. implicit_weight_table(h, sl, 0);
  1685. implicit_weight_table(h, sl, 1);
  1686. }
  1687. }
  1688. if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
  1689. ff_h264_direct_dist_scale_factor(h, sl);
  1690. ff_h264_direct_ref_list_init(h, sl);
  1691. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  1692. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  1693. h->nal_unit_type != H264_NAL_IDR_SLICE) ||
  1694. (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&
  1695. sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1696. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  1697. sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1698. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  1699. nal->ref_idc == 0))
  1700. sl->deblocking_filter = 0;
  1701. if (sl->deblocking_filter == 1 && h->max_contexts > 1) {
  1702. if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
  1703. /* Cheat slightly for speed:
  1704. * Do not bother to deblock across slices. */
  1705. sl->deblocking_filter = 2;
  1706. } else {
  1707. h->postpone_filter = 1;
  1708. }
  1709. }
  1710. sl->qp_thresh = 15 -
  1711. FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
  1712. FFMAX3(0,
  1713. h->ps.pps->chroma_qp_index_offset[0],
  1714. h->ps.pps->chroma_qp_index_offset[1]) +
  1715. 6 * (h->ps.sps->bit_depth_luma - 8);
  1716. sl->slice_num = ++h->current_slice;
  1717. if (sl->slice_num)
  1718. h->slice_row[(sl->slice_num-1)&(MAX_SLICES-1)]= sl->resync_mb_y;
  1719. if ( h->slice_row[sl->slice_num&(MAX_SLICES-1)] + 3 >= sl->resync_mb_y
  1720. && h->slice_row[sl->slice_num&(MAX_SLICES-1)] <= sl->resync_mb_y
  1721. && sl->slice_num >= MAX_SLICES) {
  1722. //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
  1723. 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);
  1724. }
  1725. for (j = 0; j < 2; j++) {
  1726. int id_list[16];
  1727. int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
  1728. for (i = 0; i < 16; i++) {
  1729. id_list[i] = 60;
  1730. if (j < sl->list_count && i < sl->ref_count[j] &&
  1731. sl->ref_list[j][i].parent->f->buf[0]) {
  1732. int k;
  1733. AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
  1734. for (k = 0; k < h->short_ref_count; k++)
  1735. if (h->short_ref[k]->f->buf[0]->buffer == buf) {
  1736. id_list[i] = k;
  1737. break;
  1738. }
  1739. for (k = 0; k < h->long_ref_count; k++)
  1740. if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
  1741. id_list[i] = h->short_ref_count + k;
  1742. break;
  1743. }
  1744. }
  1745. }
  1746. ref2frm[0] =
  1747. ref2frm[1] = -1;
  1748. for (i = 0; i < 16; i++)
  1749. ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
  1750. ref2frm[18 + 0] =
  1751. ref2frm[18 + 1] = -1;
  1752. for (i = 16; i < 48; i++)
  1753. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  1754. (sl->ref_list[j][i].reference & 3);
  1755. }
  1756. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  1757. av_log(h->avctx, AV_LOG_DEBUG,
  1758. "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",
  1759. sl->slice_num,
  1760. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  1761. sl->mb_y * h->mb_width + sl->mb_x,
  1762. av_get_picture_type_char(sl->slice_type),
  1763. sl->slice_type_fixed ? " fix" : "",
  1764. nal->type == H264_NAL_IDR_SLICE ? " IDR" : "",
  1765. h->poc.frame_num,
  1766. h->cur_pic_ptr->field_poc[0],
  1767. h->cur_pic_ptr->field_poc[1],
  1768. sl->ref_count[0], sl->ref_count[1],
  1769. sl->qscale,
  1770. sl->deblocking_filter,
  1771. sl->slice_alpha_c0_offset, sl->slice_beta_offset,
  1772. sl->pwt.use_weight,
  1773. sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
  1774. sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  1775. }
  1776. return 0;
  1777. }
  1778. int ff_h264_get_slice_type(const H264SliceContext *sl)
  1779. {
  1780. switch (sl->slice_type) {
  1781. case AV_PICTURE_TYPE_P:
  1782. return 0;
  1783. case AV_PICTURE_TYPE_B:
  1784. return 1;
  1785. case AV_PICTURE_TYPE_I:
  1786. return 2;
  1787. case AV_PICTURE_TYPE_SP:
  1788. return 3;
  1789. case AV_PICTURE_TYPE_SI:
  1790. return 4;
  1791. default:
  1792. return AVERROR_INVALIDDATA;
  1793. }
  1794. }
  1795. static av_always_inline void fill_filter_caches_inter(const H264Context *h,
  1796. H264SliceContext *sl,
  1797. int mb_type, int top_xy,
  1798. int left_xy[LEFT_MBS],
  1799. int top_type,
  1800. int left_type[LEFT_MBS],
  1801. int mb_xy, int list)
  1802. {
  1803. int b_stride = h->b_stride;
  1804. int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
  1805. int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
  1806. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  1807. if (USES_LIST(top_type, list)) {
  1808. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  1809. const int b8_xy = 4 * top_xy + 2;
  1810. const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1811. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  1812. ref_cache[0 - 1 * 8] =
  1813. ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
  1814. ref_cache[2 - 1 * 8] =
  1815. ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
  1816. } else {
  1817. AV_ZERO128(mv_dst - 1 * 8);
  1818. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1819. }
  1820. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  1821. if (USES_LIST(left_type[LTOP], list)) {
  1822. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  1823. const int b8_xy = 4 * left_xy[LTOP] + 1;
  1824. const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1825. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  1826. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  1827. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  1828. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  1829. ref_cache[-1 + 0] =
  1830. ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  1831. ref_cache[-1 + 16] =
  1832. ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  1833. } else {
  1834. AV_ZERO32(mv_dst - 1 + 0);
  1835. AV_ZERO32(mv_dst - 1 + 8);
  1836. AV_ZERO32(mv_dst - 1 + 16);
  1837. AV_ZERO32(mv_dst - 1 + 24);
  1838. ref_cache[-1 + 0] =
  1839. ref_cache[-1 + 8] =
  1840. ref_cache[-1 + 16] =
  1841. ref_cache[-1 + 24] = LIST_NOT_USED;
  1842. }
  1843. }
  1844. }
  1845. if (!USES_LIST(mb_type, list)) {
  1846. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  1847. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1848. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1849. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1850. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1851. return;
  1852. }
  1853. {
  1854. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  1855. const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
  1856. uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
  1857. uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
  1858. AV_WN32A(&ref_cache[0 * 8], ref01);
  1859. AV_WN32A(&ref_cache[1 * 8], ref01);
  1860. AV_WN32A(&ref_cache[2 * 8], ref23);
  1861. AV_WN32A(&ref_cache[3 * 8], ref23);
  1862. }
  1863. {
  1864. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
  1865. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  1866. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  1867. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  1868. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  1869. }
  1870. }
  1871. /**
  1872. * @return non zero if the loop filter can be skipped
  1873. */
  1874. static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
  1875. {
  1876. const int mb_xy = sl->mb_xy;
  1877. int top_xy, left_xy[LEFT_MBS];
  1878. int top_type, left_type[LEFT_MBS];
  1879. uint8_t *nnz;
  1880. uint8_t *nnz_cache;
  1881. top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
  1882. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  1883. if (FRAME_MBAFF(h)) {
  1884. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  1885. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  1886. if (sl->mb_y & 1) {
  1887. if (left_mb_field_flag != curr_mb_field_flag)
  1888. left_xy[LTOP] -= h->mb_stride;
  1889. } else {
  1890. if (curr_mb_field_flag)
  1891. top_xy += h->mb_stride &
  1892. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  1893. if (left_mb_field_flag != curr_mb_field_flag)
  1894. left_xy[LBOT] += h->mb_stride;
  1895. }
  1896. }
  1897. sl->top_mb_xy = top_xy;
  1898. sl->left_mb_xy[LTOP] = left_xy[LTOP];
  1899. sl->left_mb_xy[LBOT] = left_xy[LBOT];
  1900. {
  1901. /* For sufficiently low qp, filtering wouldn't do anything.
  1902. * This is a conservative estimate: could also check beta_offset
  1903. * and more accurate chroma_qp. */
  1904. int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  1905. int qp = h->cur_pic.qscale_table[mb_xy];
  1906. if (qp <= qp_thresh &&
  1907. (left_xy[LTOP] < 0 ||
  1908. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  1909. (top_xy < 0 ||
  1910. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  1911. if (!FRAME_MBAFF(h))
  1912. return 1;
  1913. if ((left_xy[LTOP] < 0 ||
  1914. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  1915. (top_xy < h->mb_stride ||
  1916. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  1917. return 1;
  1918. }
  1919. }
  1920. top_type = h->cur_pic.mb_type[top_xy];
  1921. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  1922. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  1923. if (sl->deblocking_filter == 2) {
  1924. if (h->slice_table[top_xy] != sl->slice_num)
  1925. top_type = 0;
  1926. if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
  1927. left_type[LTOP] = left_type[LBOT] = 0;
  1928. } else {
  1929. if (h->slice_table[top_xy] == 0xFFFF)
  1930. top_type = 0;
  1931. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  1932. left_type[LTOP] = left_type[LBOT] = 0;
  1933. }
  1934. sl->top_type = top_type;
  1935. sl->left_type[LTOP] = left_type[LTOP];
  1936. sl->left_type[LBOT] = left_type[LBOT];
  1937. if (IS_INTRA(mb_type))
  1938. return 0;
  1939. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1940. top_type, left_type, mb_xy, 0);
  1941. if (sl->list_count == 2)
  1942. fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
  1943. top_type, left_type, mb_xy, 1);
  1944. nnz = h->non_zero_count[mb_xy];
  1945. nnz_cache = sl->non_zero_count_cache;
  1946. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  1947. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  1948. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  1949. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  1950. sl->cbp = h->cbp_table[mb_xy];
  1951. if (top_type) {
  1952. nnz = h->non_zero_count[top_xy];
  1953. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  1954. }
  1955. if (left_type[LTOP]) {
  1956. nnz = h->non_zero_count[left_xy[LTOP]];
  1957. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  1958. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  1959. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  1960. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  1961. }
  1962. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  1963. * from what the loop filter needs */
  1964. if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
  1965. if (IS_8x8DCT(top_type)) {
  1966. nnz_cache[4 + 8 * 0] =
  1967. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  1968. nnz_cache[6 + 8 * 0] =
  1969. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  1970. }
  1971. if (IS_8x8DCT(left_type[LTOP])) {
  1972. nnz_cache[3 + 8 * 1] =
  1973. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  1974. }
  1975. if (IS_8x8DCT(left_type[LBOT])) {
  1976. nnz_cache[3 + 8 * 3] =
  1977. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  1978. }
  1979. if (IS_8x8DCT(mb_type)) {
  1980. nnz_cache[scan8[0]] =
  1981. nnz_cache[scan8[1]] =
  1982. nnz_cache[scan8[2]] =
  1983. nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
  1984. nnz_cache[scan8[0 + 4]] =
  1985. nnz_cache[scan8[1 + 4]] =
  1986. nnz_cache[scan8[2 + 4]] =
  1987. nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
  1988. nnz_cache[scan8[0 + 8]] =
  1989. nnz_cache[scan8[1 + 8]] =
  1990. nnz_cache[scan8[2 + 8]] =
  1991. nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
  1992. nnz_cache[scan8[0 + 12]] =
  1993. nnz_cache[scan8[1 + 12]] =
  1994. nnz_cache[scan8[2 + 12]] =
  1995. nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
  1996. }
  1997. }
  1998. return 0;
  1999. }
  2000. static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
  2001. {
  2002. uint8_t *dest_y, *dest_cb, *dest_cr;
  2003. int linesize, uvlinesize, mb_x, mb_y;
  2004. const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
  2005. const int old_slice_type = sl->slice_type;
  2006. const int pixel_shift = h->pixel_shift;
  2007. const int block_h = 16 >> h->chroma_y_shift;
  2008. if (h->postpone_filter)
  2009. return;
  2010. if (sl->deblocking_filter) {
  2011. for (mb_x = start_x; mb_x < end_x; mb_x++)
  2012. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  2013. int mb_xy, mb_type;
  2014. mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
  2015. mb_type = h->cur_pic.mb_type[mb_xy];
  2016. if (FRAME_MBAFF(h))
  2017. sl->mb_mbaff =
  2018. sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2019. sl->mb_x = mb_x;
  2020. sl->mb_y = mb_y;
  2021. dest_y = h->cur_pic.f->data[0] +
  2022. ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
  2023. dest_cb = h->cur_pic.f->data[1] +
  2024. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  2025. mb_y * sl->uvlinesize * block_h;
  2026. dest_cr = h->cur_pic.f->data[2] +
  2027. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  2028. mb_y * sl->uvlinesize * block_h;
  2029. // FIXME simplify above
  2030. if (MB_FIELD(sl)) {
  2031. linesize = sl->mb_linesize = sl->linesize * 2;
  2032. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
  2033. if (mb_y & 1) { // FIXME move out of this function?
  2034. dest_y -= sl->linesize * 15;
  2035. dest_cb -= sl->uvlinesize * (block_h - 1);
  2036. dest_cr -= sl->uvlinesize * (block_h - 1);
  2037. }
  2038. } else {
  2039. linesize = sl->mb_linesize = sl->linesize;
  2040. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
  2041. }
  2042. backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
  2043. uvlinesize, 0);
  2044. if (fill_filter_caches(h, sl, mb_type))
  2045. continue;
  2046. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
  2047. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]);
  2048. if (FRAME_MBAFF(h)) {
  2049. ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  2050. linesize, uvlinesize);
  2051. } else {
  2052. ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
  2053. dest_cr, linesize, uvlinesize);
  2054. }
  2055. }
  2056. }
  2057. sl->slice_type = old_slice_type;
  2058. sl->mb_x = end_x;
  2059. sl->mb_y = end_mb_y - FRAME_MBAFF(h);
  2060. sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
  2061. sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
  2062. }
  2063. static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
  2064. {
  2065. const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
  2066. int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
  2067. h->cur_pic.mb_type[mb_xy - 1] :
  2068. (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
  2069. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  2070. sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2071. }
  2072. /**
  2073. * Draw edges and report progress for the last MB row.
  2074. */
  2075. static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
  2076. {
  2077. int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
  2078. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  2079. int height = 16 << FRAME_MBAFF(h);
  2080. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  2081. if (sl->deblocking_filter) {
  2082. if ((top + height) >= pic_height)
  2083. height += deblock_border;
  2084. top -= deblock_border;
  2085. }
  2086. if (top >= pic_height || (top + height) < 0)
  2087. return;
  2088. height = FFMIN(height, pic_height - top);
  2089. if (top < 0) {
  2090. height = top + height;
  2091. top = 0;
  2092. }
  2093. ff_h264_draw_horiz_band(h, sl, top, height);
  2094. if (h->droppable || sl->h264->slice_ctx[0].er.error_occurred)
  2095. return;
  2096. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  2097. h->picture_structure == PICT_BOTTOM_FIELD);
  2098. }
  2099. static void er_add_slice(H264SliceContext *sl,
  2100. int startx, int starty,
  2101. int endx, int endy, int status)
  2102. {
  2103. if (!sl->h264->enable_er)
  2104. return;
  2105. if (CONFIG_ERROR_RESILIENCE) {
  2106. ERContext *er = &sl->h264->slice_ctx[0].er;
  2107. ff_er_add_slice(er, startx, starty, endx, endy, status);
  2108. }
  2109. }
  2110. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  2111. {
  2112. H264SliceContext *sl = arg;
  2113. const H264Context *h = sl->h264;
  2114. int lf_x_start = sl->mb_x;
  2115. int orig_deblock = sl->deblocking_filter;
  2116. int ret;
  2117. sl->linesize = h->cur_pic_ptr->f->linesize[0];
  2118. sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
  2119. ret = alloc_scratch_buffers(sl, sl->linesize);
  2120. if (ret < 0)
  2121. return ret;
  2122. sl->mb_skip_run = -1;
  2123. av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * sl->linesize * ((scan8[15] - scan8[0]) >> 3));
  2124. if (h->postpone_filter)
  2125. sl->deblocking_filter = 0;
  2126. sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  2127. (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
  2128. if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && h->slice_ctx[0].er.error_status_table) {
  2129. const int start_i = av_clip(sl->resync_mb_x + sl->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
  2130. if (start_i) {
  2131. int prev_status = h->slice_ctx[0].er.error_status_table[h->slice_ctx[0].er.mb_index2xy[start_i - 1]];
  2132. prev_status &= ~ VP_START;
  2133. if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
  2134. h->slice_ctx[0].er.error_occurred = 1;
  2135. }
  2136. }
  2137. if (h->ps.pps->cabac) {
  2138. /* realign */
  2139. align_get_bits(&sl->gb);
  2140. /* init cabac */
  2141. ret = ff_init_cabac_decoder(&sl->cabac,
  2142. sl->gb.buffer + get_bits_count(&sl->gb) / 8,
  2143. (get_bits_left(&sl->gb) + 7) / 8);
  2144. if (ret < 0)
  2145. return ret;
  2146. ff_h264_init_cabac_states(h, sl);
  2147. for (;;) {
  2148. // START_TIMER
  2149. int ret, eos;
  2150. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2151. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2152. sl->next_slice_idx);
  2153. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2154. sl->mb_y, ER_MB_ERROR);
  2155. return AVERROR_INVALIDDATA;
  2156. }
  2157. ret = ff_h264_decode_mb_cabac(h, sl);
  2158. // STOP_TIMER("decode_mb_cabac")
  2159. if (ret >= 0)
  2160. ff_h264_hl_decode_mb(h, sl);
  2161. // FIXME optimal? or let mb_decode decode 16x32 ?
  2162. if (ret >= 0 && FRAME_MBAFF(h)) {
  2163. sl->mb_y++;
  2164. ret = ff_h264_decode_mb_cabac(h, sl);
  2165. if (ret >= 0)
  2166. ff_h264_hl_decode_mb(h, sl);
  2167. sl->mb_y--;
  2168. }
  2169. eos = get_cabac_terminate(&sl->cabac);
  2170. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  2171. sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
  2172. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2173. sl->mb_y, ER_MB_END);
  2174. if (sl->mb_x >= lf_x_start)
  2175. loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
  2176. goto finish;
  2177. }
  2178. if (sl->cabac.bytestream > sl->cabac.bytestream_end + 2 )
  2179. av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %"PTRDIFF_SPECIFIER"\n", sl->cabac.bytestream_end - sl->cabac.bytestream);
  2180. if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 4) {
  2181. av_log(h->avctx, AV_LOG_ERROR,
  2182. "error while decoding MB %d %d, bytestream %"PTRDIFF_SPECIFIER"\n",
  2183. sl->mb_x, sl->mb_y,
  2184. sl->cabac.bytestream_end - sl->cabac.bytestream);
  2185. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2186. sl->mb_y, ER_MB_ERROR);
  2187. return AVERROR_INVALIDDATA;
  2188. }
  2189. if (++sl->mb_x >= h->mb_width) {
  2190. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2191. sl->mb_x = lf_x_start = 0;
  2192. decode_finish_row(h, sl);
  2193. ++sl->mb_y;
  2194. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2195. ++sl->mb_y;
  2196. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2197. predict_field_decoding_flag(h, sl);
  2198. }
  2199. }
  2200. if (eos || sl->mb_y >= h->mb_height) {
  2201. ff_tlog(h->avctx, "slice end %d %d\n",
  2202. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2203. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
  2204. sl->mb_y, ER_MB_END);
  2205. if (sl->mb_x > lf_x_start)
  2206. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2207. goto finish;
  2208. }
  2209. }
  2210. } else {
  2211. for (;;) {
  2212. int ret;
  2213. if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
  2214. av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
  2215. sl->next_slice_idx);
  2216. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2217. sl->mb_y, ER_MB_ERROR);
  2218. return AVERROR_INVALIDDATA;
  2219. }
  2220. ret = ff_h264_decode_mb_cavlc(h, sl);
  2221. if (ret >= 0)
  2222. ff_h264_hl_decode_mb(h, sl);
  2223. // FIXME optimal? or let mb_decode decode 16x32 ?
  2224. if (ret >= 0 && FRAME_MBAFF(h)) {
  2225. sl->mb_y++;
  2226. ret = ff_h264_decode_mb_cavlc(h, sl);
  2227. if (ret >= 0)
  2228. ff_h264_hl_decode_mb(h, sl);
  2229. sl->mb_y--;
  2230. }
  2231. if (ret < 0) {
  2232. av_log(h->avctx, AV_LOG_ERROR,
  2233. "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
  2234. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2235. sl->mb_y, ER_MB_ERROR);
  2236. return ret;
  2237. }
  2238. if (++sl->mb_x >= h->mb_width) {
  2239. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2240. sl->mb_x = lf_x_start = 0;
  2241. decode_finish_row(h, sl);
  2242. ++sl->mb_y;
  2243. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2244. ++sl->mb_y;
  2245. if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
  2246. predict_field_decoding_flag(h, sl);
  2247. }
  2248. if (sl->mb_y >= h->mb_height) {
  2249. ff_tlog(h->avctx, "slice end %d %d\n",
  2250. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2251. if ( get_bits_left(&sl->gb) == 0
  2252. || get_bits_left(&sl->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
  2253. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2254. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2255. goto finish;
  2256. } else {
  2257. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2258. sl->mb_x, sl->mb_y, ER_MB_END);
  2259. return AVERROR_INVALIDDATA;
  2260. }
  2261. }
  2262. }
  2263. if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
  2264. ff_tlog(h->avctx, "slice end %d %d\n",
  2265. get_bits_count(&sl->gb), sl->gb.size_in_bits);
  2266. if (get_bits_left(&sl->gb) == 0) {
  2267. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
  2268. sl->mb_x - 1, sl->mb_y, ER_MB_END);
  2269. if (sl->mb_x > lf_x_start)
  2270. loop_filter(h, sl, lf_x_start, sl->mb_x);
  2271. goto finish;
  2272. } else {
  2273. er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
  2274. sl->mb_y, ER_MB_ERROR);
  2275. return AVERROR_INVALIDDATA;
  2276. }
  2277. }
  2278. }
  2279. }
  2280. finish:
  2281. sl->deblocking_filter = orig_deblock;
  2282. return 0;
  2283. }
  2284. /**
  2285. * Call decode_slice() for each context.
  2286. *
  2287. * @param h h264 master context
  2288. * @param context_count number of contexts to execute
  2289. */
  2290. int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
  2291. {
  2292. AVCodecContext *const avctx = h->avctx;
  2293. H264SliceContext *sl;
  2294. int i, j;
  2295. av_assert0(context_count && h->slice_ctx[context_count - 1].mb_y < h->mb_height);
  2296. h->slice_ctx[0].next_slice_idx = INT_MAX;
  2297. if (h->avctx->hwaccel
  2298. #if FF_API_CAP_VDPAU
  2299. || h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU
  2300. #endif
  2301. )
  2302. return 0;
  2303. if (context_count == 1) {
  2304. int ret;
  2305. h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
  2306. h->postpone_filter = 0;
  2307. ret = decode_slice(avctx, &h->slice_ctx[0]);
  2308. h->mb_y = h->slice_ctx[0].mb_y;
  2309. return ret;
  2310. } else {
  2311. av_assert0(context_count > 0);
  2312. for (i = 0; i < context_count; i++) {
  2313. int next_slice_idx = h->mb_width * h->mb_height;
  2314. int slice_idx;
  2315. sl = &h->slice_ctx[i];
  2316. if (CONFIG_ERROR_RESILIENCE) {
  2317. sl->er.error_count = 0;
  2318. }
  2319. /* make sure none of those slices overlap */
  2320. slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
  2321. for (j = 0; j < context_count; j++) {
  2322. H264SliceContext *sl2 = &h->slice_ctx[j];
  2323. int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
  2324. if (i == j || slice_idx2 < slice_idx)
  2325. continue;
  2326. next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
  2327. }
  2328. sl->next_slice_idx = next_slice_idx;
  2329. }
  2330. avctx->execute(avctx, decode_slice, h->slice_ctx,
  2331. NULL, context_count, sizeof(h->slice_ctx[0]));
  2332. /* pull back stuff from slices to master context */
  2333. sl = &h->slice_ctx[context_count - 1];
  2334. h->mb_y = sl->mb_y;
  2335. if (CONFIG_ERROR_RESILIENCE) {
  2336. for (i = 1; i < context_count; i++)
  2337. h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count;
  2338. }
  2339. if (h->postpone_filter) {
  2340. h->postpone_filter = 0;
  2341. for (i = 0; i < context_count; i++) {
  2342. int y_end, x_end;
  2343. sl = &h->slice_ctx[i];
  2344. y_end = FFMIN(sl->mb_y + 1, h->mb_height);
  2345. x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
  2346. for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
  2347. sl->mb_y = j;
  2348. loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
  2349. j == y_end - 1 ? x_end : h->mb_width);
  2350. }
  2351. }
  2352. }
  2353. }
  2354. return 0;
  2355. }