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.

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