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.

2276 lines
84KB

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