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.

2672 lines
100KB

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