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.

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