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.

2621 lines
99KB

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