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.

2590 lines
97KB

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