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.

2311 lines
85KB

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