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.

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