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.

2356 lines
86KB

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