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.

2533 lines
95KB

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