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
98KB

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