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.

2378 lines
87KB

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