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.

2372 lines
86KB

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