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.

2596 lines
98KB

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