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.

2370 lines
87KB

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