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.

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