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.

2616 lines
98KB

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