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.

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