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.

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