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.

2562 lines
97KB

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