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.

2608 lines
98KB

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