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.

2592 lines
97KB

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