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.

2588 lines
97KB

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