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.

2585 lines
97KB

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