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.

2462 lines
92KB

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