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.

2385 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->priv_data_size) {
  231. pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->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. // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding ||
  625. // s->current_picture.f.reference /* || h->contains_intra */ || 1;
  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 h->avctx->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. h->avctx->sample_aspect_ratio = h->sps.sar;
  932. av_assert0(h->avctx->sample_aspect_ratio.den);
  933. av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
  934. &h->chroma_x_shift, &h->chroma_y_shift);
  935. if (h->sps.timing_info_present_flag) {
  936. int64_t den = h->sps.time_scale;
  937. if (h->x264_build < 44U)
  938. den *= 2;
  939. av_reduce(&h->avctx->time_base.num, &h->avctx->time_base.den,
  940. h->sps.num_units_in_tick, den, 1 << 30);
  941. }
  942. h->avctx->hwaccel = ff_find_hwaccel(h->avctx);
  943. if (reinit)
  944. ff_h264_free_tables(h, 0);
  945. h->first_field = 0;
  946. h->prev_interlaced_frame = 1;
  947. init_scan_tables(h);
  948. ret = ff_h264_alloc_tables(h);
  949. if (ret < 0) {
  950. av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
  951. return ret;
  952. }
  953. if (nb_slices > H264_MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) {
  954. int max_slices;
  955. if (h->mb_height)
  956. max_slices = FFMIN(H264_MAX_THREADS, h->mb_height);
  957. else
  958. max_slices = H264_MAX_THREADS;
  959. av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices %d,"
  960. " reducing to %d\n", nb_slices, max_slices);
  961. nb_slices = max_slices;
  962. }
  963. h->slice_context_count = nb_slices;
  964. if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
  965. ret = ff_h264_context_init(h);
  966. if (ret < 0) {
  967. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  968. return ret;
  969. }
  970. } else {
  971. for (i = 1; i < h->slice_context_count; i++) {
  972. H264Context *c;
  973. c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
  974. if (!c)
  975. return AVERROR(ENOMEM);
  976. c->avctx = h->avctx;
  977. c->dsp = h->dsp;
  978. c->vdsp = h->vdsp;
  979. c->h264dsp = h->h264dsp;
  980. c->h264qpel = h->h264qpel;
  981. c->h264chroma = h->h264chroma;
  982. c->sps = h->sps;
  983. c->pps = h->pps;
  984. c->pixel_shift = h->pixel_shift;
  985. c->width = h->width;
  986. c->height = h->height;
  987. c->linesize = h->linesize;
  988. c->uvlinesize = h->uvlinesize;
  989. c->chroma_x_shift = h->chroma_x_shift;
  990. c->chroma_y_shift = h->chroma_y_shift;
  991. c->qscale = h->qscale;
  992. c->droppable = h->droppable;
  993. c->data_partitioning = h->data_partitioning;
  994. c->low_delay = h->low_delay;
  995. c->mb_width = h->mb_width;
  996. c->mb_height = h->mb_height;
  997. c->mb_stride = h->mb_stride;
  998. c->mb_num = h->mb_num;
  999. c->flags = h->flags;
  1000. c->workaround_bugs = h->workaround_bugs;
  1001. c->pict_type = h->pict_type;
  1002. init_scan_tables(c);
  1003. clone_tables(c, h, i);
  1004. c->context_initialized = 1;
  1005. }
  1006. for (i = 0; i < h->slice_context_count; i++)
  1007. if ((ret = ff_h264_context_init(h->thread_context[i])) < 0) {
  1008. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  1009. return ret;
  1010. }
  1011. }
  1012. h->context_initialized = 1;
  1013. return 0;
  1014. }
  1015. /**
  1016. * Decode a slice header.
  1017. * This will (re)intialize the decoder and call h264_frame_start() as needed.
  1018. *
  1019. * @param h h264context
  1020. * @param h0 h264 master context (differs from 'h' when doing sliced based
  1021. * parallel decoding)
  1022. *
  1023. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  1024. */
  1025. int ff_h264_decode_slice_header(H264Context *h, H264Context *h0)
  1026. {
  1027. unsigned int first_mb_in_slice;
  1028. unsigned int pps_id;
  1029. int ret;
  1030. unsigned int slice_type, tmp, i, j;
  1031. int default_ref_list_done = 0;
  1032. int last_pic_structure, last_pic_droppable;
  1033. int needs_reinit = 0;
  1034. int field_pic_flag, bottom_field_flag;
  1035. h->qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
  1036. h->qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
  1037. first_mb_in_slice = get_ue_golomb(&h->gb);
  1038. if (first_mb_in_slice == 0) { // FIXME better field boundary detection
  1039. if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
  1040. ff_h264_field_end(h, 1);
  1041. }
  1042. h0->current_slice = 0;
  1043. if (!h0->first_field) {
  1044. if (h->cur_pic_ptr && !h->droppable) {
  1045. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  1046. h->picture_structure == PICT_BOTTOM_FIELD);
  1047. }
  1048. h->cur_pic_ptr = NULL;
  1049. }
  1050. }
  1051. slice_type = get_ue_golomb_31(&h->gb);
  1052. if (slice_type > 9) {
  1053. av_log(h->avctx, AV_LOG_ERROR,
  1054. "slice type %d too large at %d %d\n",
  1055. slice_type, h->mb_x, h->mb_y);
  1056. return AVERROR_INVALIDDATA;
  1057. }
  1058. if (slice_type > 4) {
  1059. slice_type -= 5;
  1060. h->slice_type_fixed = 1;
  1061. } else
  1062. h->slice_type_fixed = 0;
  1063. slice_type = golomb_to_pict_type[slice_type];
  1064. if (slice_type == AV_PICTURE_TYPE_I ||
  1065. (h0->current_slice != 0 && slice_type == h0->last_slice_type)) {
  1066. default_ref_list_done = 1;
  1067. }
  1068. h->slice_type = slice_type;
  1069. h->slice_type_nos = slice_type & 3;
  1070. if (h->nal_unit_type == NAL_IDR_SLICE &&
  1071. h->slice_type_nos != AV_PICTURE_TYPE_I) {
  1072. av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
  1073. return AVERROR_INVALIDDATA;
  1074. }
  1075. // to make a few old functions happy, it's wrong though
  1076. h->pict_type = h->slice_type;
  1077. pps_id = get_ue_golomb(&h->gb);
  1078. if (pps_id >= MAX_PPS_COUNT) {
  1079. av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
  1080. return AVERROR_INVALIDDATA;
  1081. }
  1082. if (!h0->pps_buffers[pps_id]) {
  1083. av_log(h->avctx, AV_LOG_ERROR,
  1084. "non-existing PPS %u referenced\n",
  1085. pps_id);
  1086. return AVERROR_INVALIDDATA;
  1087. }
  1088. h->pps = *h0->pps_buffers[pps_id];
  1089. if (!h0->sps_buffers[h->pps.sps_id]) {
  1090. av_log(h->avctx, AV_LOG_ERROR,
  1091. "non-existing SPS %u referenced\n",
  1092. h->pps.sps_id);
  1093. return AVERROR_INVALIDDATA;
  1094. }
  1095. if (h->pps.sps_id != h->sps.sps_id ||
  1096. h0->sps_buffers[h->pps.sps_id]->new) {
  1097. h0->sps_buffers[h->pps.sps_id]->new = 0;
  1098. h->sps = *h0->sps_buffers[h->pps.sps_id];
  1099. if (h->bit_depth_luma != h->sps.bit_depth_luma ||
  1100. h->chroma_format_idc != h->sps.chroma_format_idc) {
  1101. h->bit_depth_luma = h->sps.bit_depth_luma;
  1102. h->chroma_format_idc = h->sps.chroma_format_idc;
  1103. needs_reinit = 1;
  1104. }
  1105. if ((ret = ff_h264_set_parameter_from_sps(h)) < 0)
  1106. return ret;
  1107. }
  1108. h->avctx->profile = ff_h264_get_profile(&h->sps);
  1109. h->avctx->level = h->sps.level_idc;
  1110. h->avctx->refs = h->sps.ref_frame_count;
  1111. if (h->mb_width != h->sps.mb_width ||
  1112. h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag))
  1113. needs_reinit = 1;
  1114. h->mb_width = h->sps.mb_width;
  1115. h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  1116. h->mb_num = h->mb_width * h->mb_height;
  1117. h->mb_stride = h->mb_width + 1;
  1118. h->b_stride = h->mb_width * 4;
  1119. h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
  1120. h->width = 16 * h->mb_width;
  1121. h->height = 16 * h->mb_height;
  1122. ret = init_dimensions(h);
  1123. if (ret < 0)
  1124. return ret;
  1125. if (h->sps.video_signal_type_present_flag) {
  1126. h->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG
  1127. : AVCOL_RANGE_MPEG;
  1128. if (h->sps.colour_description_present_flag) {
  1129. if (h->avctx->colorspace != h->sps.colorspace)
  1130. needs_reinit = 1;
  1131. h->avctx->color_primaries = h->sps.color_primaries;
  1132. h->avctx->color_trc = h->sps.color_trc;
  1133. h->avctx->colorspace = h->sps.colorspace;
  1134. }
  1135. }
  1136. if (h->context_initialized &&
  1137. (h->width != h->avctx->coded_width ||
  1138. h->height != h->avctx->coded_height ||
  1139. needs_reinit)) {
  1140. if (h != h0) {
  1141. av_log(h->avctx, AV_LOG_ERROR,
  1142. "changing width %d -> %d / height %d -> %d on "
  1143. "slice %d\n",
  1144. h->width, h->avctx->coded_width,
  1145. h->height, h->avctx->coded_height,
  1146. h0->current_slice + 1);
  1147. return AVERROR_INVALIDDATA;
  1148. }
  1149. ff_h264_flush_change(h);
  1150. if ((ret = get_pixel_format(h)) < 0)
  1151. return ret;
  1152. h->avctx->pix_fmt = ret;
  1153. av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
  1154. "pix_fmt: %d\n", h->width, h->height, h->avctx->pix_fmt);
  1155. if ((ret = h264_slice_header_init(h, 1)) < 0) {
  1156. av_log(h->avctx, AV_LOG_ERROR,
  1157. "h264_slice_header_init() failed\n");
  1158. return ret;
  1159. }
  1160. }
  1161. if (!h->context_initialized) {
  1162. if (h != h0) {
  1163. av_log(h->avctx, AV_LOG_ERROR,
  1164. "Cannot (re-)initialize context during parallel decoding.\n");
  1165. return AVERROR_PATCHWELCOME;
  1166. }
  1167. if ((ret = get_pixel_format(h)) < 0)
  1168. return ret;
  1169. h->avctx->pix_fmt = ret;
  1170. if ((ret = h264_slice_header_init(h, 0)) < 0) {
  1171. av_log(h->avctx, AV_LOG_ERROR,
  1172. "h264_slice_header_init() failed\n");
  1173. return ret;
  1174. }
  1175. }
  1176. if (h == h0 && h->dequant_coeff_pps != pps_id) {
  1177. h->dequant_coeff_pps = pps_id;
  1178. h264_init_dequant_tables(h);
  1179. }
  1180. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  1181. h->mb_mbaff = 0;
  1182. h->mb_aff_frame = 0;
  1183. last_pic_structure = h0->picture_structure;
  1184. last_pic_droppable = h0->droppable;
  1185. h->droppable = h->nal_ref_idc == 0;
  1186. if (h->sps.frame_mbs_only_flag) {
  1187. h->picture_structure = PICT_FRAME;
  1188. } else {
  1189. field_pic_flag = get_bits1(&h->gb);
  1190. if (field_pic_flag) {
  1191. bottom_field_flag = get_bits1(&h->gb);
  1192. h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;
  1193. } else {
  1194. h->picture_structure = PICT_FRAME;
  1195. h->mb_aff_frame = h->sps.mb_aff;
  1196. }
  1197. }
  1198. h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
  1199. if (h0->current_slice != 0) {
  1200. if (last_pic_structure != h->picture_structure ||
  1201. last_pic_droppable != h->droppable) {
  1202. av_log(h->avctx, AV_LOG_ERROR,
  1203. "Changing field mode (%d -> %d) between slices is not allowed\n",
  1204. last_pic_structure, h->picture_structure);
  1205. h->picture_structure = last_pic_structure;
  1206. h->droppable = last_pic_droppable;
  1207. return AVERROR_INVALIDDATA;
  1208. } else if (!h0->cur_pic_ptr) {
  1209. av_log(h->avctx, AV_LOG_ERROR,
  1210. "unset cur_pic_ptr on slice %d\n",
  1211. h0->current_slice + 1);
  1212. return AVERROR_INVALIDDATA;
  1213. }
  1214. } else {
  1215. /* Shorten frame num gaps so we don't have to allocate reference
  1216. * frames just to throw them away */
  1217. if (h->frame_num != h->prev_frame_num) {
  1218. int unwrap_prev_frame_num = h->prev_frame_num;
  1219. int max_frame_num = 1 << h->sps.log2_max_frame_num;
  1220. if (unwrap_prev_frame_num > h->frame_num)
  1221. unwrap_prev_frame_num -= max_frame_num;
  1222. if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
  1223. unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
  1224. if (unwrap_prev_frame_num < 0)
  1225. unwrap_prev_frame_num += max_frame_num;
  1226. h->prev_frame_num = unwrap_prev_frame_num;
  1227. }
  1228. }
  1229. /* See if we have a decoded first field looking for a pair...
  1230. * Here, we're using that to see if we should mark previously
  1231. * decode frames as "finished".
  1232. * We have to do that before the "dummy" in-between frame allocation,
  1233. * since that can modify s->current_picture_ptr. */
  1234. if (h0->first_field) {
  1235. assert(h0->cur_pic_ptr);
  1236. assert(h0->cur_pic_ptr->f.buf[0]);
  1237. assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1238. /* figure out if we have a complementary field pair */
  1239. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1240. /* Previous field is unmatched. Don't display it, but let it
  1241. * remain for reference if marked as such. */
  1242. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  1243. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  1244. last_pic_structure == PICT_TOP_FIELD);
  1245. }
  1246. } else {
  1247. if (h0->cur_pic_ptr->frame_num != h->frame_num) {
  1248. /* This and previous field were reference, but had
  1249. * different frame_nums. Consider this field first in
  1250. * pair. Throw away previous field except for reference
  1251. * purposes. */
  1252. if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
  1253. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  1254. last_pic_structure == PICT_TOP_FIELD);
  1255. }
  1256. } else {
  1257. /* Second field in complementary pair */
  1258. if (!((last_pic_structure == PICT_TOP_FIELD &&
  1259. h->picture_structure == PICT_BOTTOM_FIELD) ||
  1260. (last_pic_structure == PICT_BOTTOM_FIELD &&
  1261. h->picture_structure == PICT_TOP_FIELD))) {
  1262. av_log(h->avctx, AV_LOG_ERROR,
  1263. "Invalid field mode combination %d/%d\n",
  1264. last_pic_structure, h->picture_structure);
  1265. h->picture_structure = last_pic_structure;
  1266. h->droppable = last_pic_droppable;
  1267. return AVERROR_INVALIDDATA;
  1268. } else if (last_pic_droppable != h->droppable) {
  1269. avpriv_request_sample(h->avctx,
  1270. "Found reference and non-reference fields in the same frame, which");
  1271. h->picture_structure = last_pic_structure;
  1272. h->droppable = last_pic_droppable;
  1273. return AVERROR_PATCHWELCOME;
  1274. }
  1275. }
  1276. }
  1277. }
  1278. while (h->frame_num != h->prev_frame_num &&
  1279. h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
  1280. H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1281. av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  1282. h->frame_num, h->prev_frame_num);
  1283. ret = h264_frame_start(h);
  1284. if (ret < 0) {
  1285. h0->first_field = 0;
  1286. return ret;
  1287. }
  1288. h->prev_frame_num++;
  1289. h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
  1290. h->cur_pic_ptr->frame_num = h->prev_frame_num;
  1291. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  1292. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  1293. ret = ff_generate_sliding_window_mmcos(h, 1);
  1294. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1295. return ret;
  1296. ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1297. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1298. return ret;
  1299. /* Error concealment: If a ref is missing, copy the previous ref
  1300. * in its place.
  1301. * FIXME: Avoiding a memcpy would be nice, but ref handling makes
  1302. * many assumptions about there being no actual duplicates.
  1303. * FIXME: This does not copy padding for out-of-frame motion
  1304. * vectors. Given we are concealing a lost frame, this probably
  1305. * is not noticeable by comparison, but it should be fixed. */
  1306. if (h->short_ref_count) {
  1307. if (prev) {
  1308. av_image_copy(h->short_ref[0]->f.data,
  1309. h->short_ref[0]->f.linesize,
  1310. (const uint8_t **)prev->f.data,
  1311. prev->f.linesize,
  1312. h->avctx->pix_fmt,
  1313. h->mb_width * 16,
  1314. h->mb_height * 16);
  1315. h->short_ref[0]->poc = prev->poc + 2;
  1316. }
  1317. h->short_ref[0]->frame_num = h->prev_frame_num;
  1318. }
  1319. }
  1320. /* See if we have a decoded first field looking for a pair...
  1321. * We're using that to see whether to continue decoding in that
  1322. * frame, or to allocate a new one. */
  1323. if (h0->first_field) {
  1324. assert(h0->cur_pic_ptr);
  1325. assert(h0->cur_pic_ptr->f.buf[0]);
  1326. assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
  1327. /* figure out if we have a complementary field pair */
  1328. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  1329. /* Previous field is unmatched. Don't display it, but let it
  1330. * remain for reference if marked as such. */
  1331. h0->cur_pic_ptr = NULL;
  1332. h0->first_field = FIELD_PICTURE(h);
  1333. } else {
  1334. if (h0->cur_pic_ptr->frame_num != h->frame_num) {
  1335. /* This and the previous field had different frame_nums.
  1336. * Consider this field first in pair. Throw away previous
  1337. * one except for reference purposes. */
  1338. h0->first_field = 1;
  1339. h0->cur_pic_ptr = NULL;
  1340. } else {
  1341. /* Second field in complementary pair */
  1342. h0->first_field = 0;
  1343. }
  1344. }
  1345. } else {
  1346. /* Frame or first field in a potentially complementary pair */
  1347. h0->first_field = FIELD_PICTURE(h);
  1348. }
  1349. if (!FIELD_PICTURE(h) || h0->first_field) {
  1350. if (h264_frame_start(h) < 0) {
  1351. h0->first_field = 0;
  1352. return AVERROR_INVALIDDATA;
  1353. }
  1354. } else {
  1355. release_unused_pictures(h, 0);
  1356. }
  1357. }
  1358. if (h != h0 && (ret = clone_slice(h, h0)) < 0)
  1359. return ret;
  1360. h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
  1361. assert(h->mb_num == h->mb_width * h->mb_height);
  1362. if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  1363. first_mb_in_slice >= h->mb_num) {
  1364. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1365. return AVERROR_INVALIDDATA;
  1366. }
  1367. h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
  1368. h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
  1369. FIELD_OR_MBAFF_PICTURE(h);
  1370. if (h->picture_structure == PICT_BOTTOM_FIELD)
  1371. h->resync_mb_y = h->mb_y = h->mb_y + 1;
  1372. assert(h->mb_y < h->mb_height);
  1373. if (h->picture_structure == PICT_FRAME) {
  1374. h->curr_pic_num = h->frame_num;
  1375. h->max_pic_num = 1 << h->sps.log2_max_frame_num;
  1376. } else {
  1377. h->curr_pic_num = 2 * h->frame_num + 1;
  1378. h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
  1379. }
  1380. if (h->nal_unit_type == NAL_IDR_SLICE)
  1381. get_ue_golomb(&h->gb); /* idr_pic_id */
  1382. if (h->sps.poc_type == 0) {
  1383. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  1384. if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
  1385. h->delta_poc_bottom = get_se_golomb(&h->gb);
  1386. }
  1387. if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
  1388. h->delta_poc[0] = get_se_golomb(&h->gb);
  1389. if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
  1390. h->delta_poc[1] = get_se_golomb(&h->gb);
  1391. }
  1392. ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);
  1393. if (h->pps.redundant_pic_cnt_present)
  1394. h->redundant_pic_count = get_ue_golomb(&h->gb);
  1395. ret = ff_set_ref_count(h);
  1396. if (ret < 0)
  1397. return ret;
  1398. else if (ret == 1)
  1399. default_ref_list_done = 0;
  1400. if (!default_ref_list_done)
  1401. ff_h264_fill_default_ref_list(h);
  1402. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  1403. ret = ff_h264_decode_ref_pic_list_reordering(h);
  1404. if (ret < 0) {
  1405. h->ref_count[1] = h->ref_count[0] = 0;
  1406. return ret;
  1407. }
  1408. }
  1409. if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
  1410. (h->pps.weighted_bipred_idc == 1 &&
  1411. h->slice_type_nos == AV_PICTURE_TYPE_B))
  1412. ff_pred_weight_table(h);
  1413. else if (h->pps.weighted_bipred_idc == 2 &&
  1414. h->slice_type_nos == AV_PICTURE_TYPE_B) {
  1415. implicit_weight_table(h, -1);
  1416. } else {
  1417. h->use_weight = 0;
  1418. for (i = 0; i < 2; i++) {
  1419. h->luma_weight_flag[i] = 0;
  1420. h->chroma_weight_flag[i] = 0;
  1421. }
  1422. }
  1423. // If frame-mt is enabled, only update mmco tables for the first slice
  1424. // in a field. Subsequent slices can temporarily clobber h->mmco_index
  1425. // or h->mmco, which will cause ref list mix-ups and decoding errors
  1426. // further down the line. This may break decoding if the first slice is
  1427. // corrupt, thus we only do this if frame-mt is enabled.
  1428. if (h->nal_ref_idc) {
  1429. ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,
  1430. !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
  1431. h0->current_slice == 0);
  1432. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  1433. return AVERROR_INVALIDDATA;
  1434. }
  1435. if (FRAME_MBAFF(h)) {
  1436. ff_h264_fill_mbaff_ref_list(h);
  1437. if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
  1438. implicit_weight_table(h, 0);
  1439. implicit_weight_table(h, 1);
  1440. }
  1441. }
  1442. if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  1443. ff_h264_direct_dist_scale_factor(h);
  1444. ff_h264_direct_ref_list_init(h);
  1445. if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
  1446. tmp = get_ue_golomb_31(&h->gb);
  1447. if (tmp > 2) {
  1448. av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
  1449. return AVERROR_INVALIDDATA;
  1450. }
  1451. h->cabac_init_idc = tmp;
  1452. }
  1453. h->last_qscale_diff = 0;
  1454. tmp = h->pps.init_qp + get_se_golomb(&h->gb);
  1455. if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
  1456. av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1457. return AVERROR_INVALIDDATA;
  1458. }
  1459. h->qscale = tmp;
  1460. h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
  1461. h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
  1462. // FIXME qscale / qp ... stuff
  1463. if (h->slice_type == AV_PICTURE_TYPE_SP)
  1464. get_bits1(&h->gb); /* sp_for_switch_flag */
  1465. if (h->slice_type == AV_PICTURE_TYPE_SP ||
  1466. h->slice_type == AV_PICTURE_TYPE_SI)
  1467. get_se_golomb(&h->gb); /* slice_qs_delta */
  1468. h->deblocking_filter = 1;
  1469. h->slice_alpha_c0_offset = 0;
  1470. h->slice_beta_offset = 0;
  1471. if (h->pps.deblocking_filter_parameters_present) {
  1472. tmp = get_ue_golomb_31(&h->gb);
  1473. if (tmp > 2) {
  1474. av_log(h->avctx, AV_LOG_ERROR,
  1475. "deblocking_filter_idc %u out of range\n", tmp);
  1476. return AVERROR_INVALIDDATA;
  1477. }
  1478. h->deblocking_filter = tmp;
  1479. if (h->deblocking_filter < 2)
  1480. h->deblocking_filter ^= 1; // 1<->0
  1481. if (h->deblocking_filter) {
  1482. h->slice_alpha_c0_offset = get_se_golomb(&h->gb) * 2;
  1483. h->slice_beta_offset = get_se_golomb(&h->gb) * 2;
  1484. if (h->slice_alpha_c0_offset > 12 ||
  1485. h->slice_alpha_c0_offset < -12 ||
  1486. h->slice_beta_offset > 12 ||
  1487. h->slice_beta_offset < -12) {
  1488. av_log(h->avctx, AV_LOG_ERROR,
  1489. "deblocking filter parameters %d %d out of range\n",
  1490. h->slice_alpha_c0_offset, h->slice_beta_offset);
  1491. return AVERROR_INVALIDDATA;
  1492. }
  1493. }
  1494. }
  1495. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  1496. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  1497. h->slice_type_nos != AV_PICTURE_TYPE_I) ||
  1498. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  1499. h->slice_type_nos == AV_PICTURE_TYPE_B) ||
  1500. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  1501. h->nal_ref_idc == 0))
  1502. h->deblocking_filter = 0;
  1503. if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
  1504. if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
  1505. /* Cheat slightly for speed:
  1506. * Do not bother to deblock across slices. */
  1507. h->deblocking_filter = 2;
  1508. } else {
  1509. h0->max_contexts = 1;
  1510. if (!h0->single_decode_warning) {
  1511. av_log(h->avctx, AV_LOG_INFO,
  1512. "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  1513. h0->single_decode_warning = 1;
  1514. }
  1515. if (h != h0) {
  1516. av_log(h->avctx, AV_LOG_ERROR,
  1517. "Deblocking switched inside frame.\n");
  1518. return 1;
  1519. }
  1520. }
  1521. }
  1522. h->qp_thresh = 15 -
  1523. FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
  1524. FFMAX3(0,
  1525. h->pps.chroma_qp_index_offset[0],
  1526. h->pps.chroma_qp_index_offset[1]) +
  1527. 6 * (h->sps.bit_depth_luma - 8);
  1528. h0->last_slice_type = slice_type;
  1529. h->slice_num = ++h0->current_slice;
  1530. if (h->slice_num >= MAX_SLICES) {
  1531. av_log(h->avctx, AV_LOG_ERROR,
  1532. "Too many slices, increase MAX_SLICES and recompile\n");
  1533. }
  1534. for (j = 0; j < 2; j++) {
  1535. int id_list[16];
  1536. int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
  1537. for (i = 0; i < 16; i++) {
  1538. id_list[i] = 60;
  1539. if (j < h->list_count && i < h->ref_count[j] &&
  1540. h->ref_list[j][i].f.buf[0]) {
  1541. int k;
  1542. AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
  1543. for (k = 0; k < h->short_ref_count; k++)
  1544. if (h->short_ref[k]->f.buf[0]->buffer == buf) {
  1545. id_list[i] = k;
  1546. break;
  1547. }
  1548. for (k = 0; k < h->long_ref_count; k++)
  1549. if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
  1550. id_list[i] = h->short_ref_count + k;
  1551. break;
  1552. }
  1553. }
  1554. }
  1555. ref2frm[0] =
  1556. ref2frm[1] = -1;
  1557. for (i = 0; i < 16; i++)
  1558. ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);
  1559. ref2frm[18 + 0] =
  1560. ref2frm[18 + 1] = -1;
  1561. for (i = 16; i < 48; i++)
  1562. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  1563. (h->ref_list[j][i].reference & 3);
  1564. }
  1565. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  1566. av_log(h->avctx, AV_LOG_DEBUG,
  1567. "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",
  1568. h->slice_num,
  1569. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  1570. first_mb_in_slice,
  1571. av_get_picture_type_char(h->slice_type),
  1572. h->slice_type_fixed ? " fix" : "",
  1573. h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  1574. pps_id, h->frame_num,
  1575. h->cur_pic_ptr->field_poc[0],
  1576. h->cur_pic_ptr->field_poc[1],
  1577. h->ref_count[0], h->ref_count[1],
  1578. h->qscale,
  1579. h->deblocking_filter,
  1580. h->slice_alpha_c0_offset, h->slice_beta_offset,
  1581. h->use_weight,
  1582. h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
  1583. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  1584. }
  1585. return 0;
  1586. }
  1587. int ff_h264_get_slice_type(const H264Context *h)
  1588. {
  1589. switch (h->slice_type) {
  1590. case AV_PICTURE_TYPE_P:
  1591. return 0;
  1592. case AV_PICTURE_TYPE_B:
  1593. return 1;
  1594. case AV_PICTURE_TYPE_I:
  1595. return 2;
  1596. case AV_PICTURE_TYPE_SP:
  1597. return 3;
  1598. case AV_PICTURE_TYPE_SI:
  1599. return 4;
  1600. default:
  1601. return AVERROR_INVALIDDATA;
  1602. }
  1603. }
  1604. static av_always_inline void fill_filter_caches_inter(H264Context *h,
  1605. int mb_type, int top_xy,
  1606. int left_xy[LEFT_MBS],
  1607. int top_type,
  1608. int left_type[LEFT_MBS],
  1609. int mb_xy, int list)
  1610. {
  1611. int b_stride = h->b_stride;
  1612. int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
  1613. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  1614. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  1615. if (USES_LIST(top_type, list)) {
  1616. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  1617. const int b8_xy = 4 * top_xy + 2;
  1618. int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);
  1619. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  1620. ref_cache[0 - 1 * 8] =
  1621. ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
  1622. ref_cache[2 - 1 * 8] =
  1623. ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
  1624. } else {
  1625. AV_ZERO128(mv_dst - 1 * 8);
  1626. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1627. }
  1628. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  1629. if (USES_LIST(left_type[LTOP], list)) {
  1630. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  1631. const int b8_xy = 4 * left_xy[LTOP] + 1;
  1632. int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);
  1633. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  1634. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  1635. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  1636. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  1637. ref_cache[-1 + 0] =
  1638. ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  1639. ref_cache[-1 + 16] =
  1640. ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  1641. } else {
  1642. AV_ZERO32(mv_dst - 1 + 0);
  1643. AV_ZERO32(mv_dst - 1 + 8);
  1644. AV_ZERO32(mv_dst - 1 + 16);
  1645. AV_ZERO32(mv_dst - 1 + 24);
  1646. ref_cache[-1 + 0] =
  1647. ref_cache[-1 + 8] =
  1648. ref_cache[-1 + 16] =
  1649. ref_cache[-1 + 24] = LIST_NOT_USED;
  1650. }
  1651. }
  1652. }
  1653. if (!USES_LIST(mb_type, list)) {
  1654. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  1655. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1656. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1657. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1658. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  1659. return;
  1660. }
  1661. {
  1662. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  1663. int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);
  1664. uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
  1665. uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
  1666. AV_WN32A(&ref_cache[0 * 8], ref01);
  1667. AV_WN32A(&ref_cache[1 * 8], ref01);
  1668. AV_WN32A(&ref_cache[2 * 8], ref23);
  1669. AV_WN32A(&ref_cache[3 * 8], ref23);
  1670. }
  1671. {
  1672. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
  1673. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  1674. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  1675. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  1676. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  1677. }
  1678. }
  1679. /**
  1680. *
  1681. * @return non zero if the loop filter can be skipped
  1682. */
  1683. static int fill_filter_caches(H264Context *h, int mb_type)
  1684. {
  1685. const int mb_xy = h->mb_xy;
  1686. int top_xy, left_xy[LEFT_MBS];
  1687. int top_type, left_type[LEFT_MBS];
  1688. uint8_t *nnz;
  1689. uint8_t *nnz_cache;
  1690. top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
  1691. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  1692. * stuff, I can't imagine that these complex rules are worth it. */
  1693. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  1694. if (FRAME_MBAFF(h)) {
  1695. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  1696. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  1697. if (h->mb_y & 1) {
  1698. if (left_mb_field_flag != curr_mb_field_flag)
  1699. left_xy[LTOP] -= h->mb_stride;
  1700. } else {
  1701. if (curr_mb_field_flag)
  1702. top_xy += h->mb_stride &
  1703. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  1704. if (left_mb_field_flag != curr_mb_field_flag)
  1705. left_xy[LBOT] += h->mb_stride;
  1706. }
  1707. }
  1708. h->top_mb_xy = top_xy;
  1709. h->left_mb_xy[LTOP] = left_xy[LTOP];
  1710. h->left_mb_xy[LBOT] = left_xy[LBOT];
  1711. {
  1712. /* For sufficiently low qp, filtering wouldn't do anything.
  1713. * This is a conservative estimate: could also check beta_offset
  1714. * and more accurate chroma_qp. */
  1715. int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  1716. int qp = h->cur_pic.qscale_table[mb_xy];
  1717. if (qp <= qp_thresh &&
  1718. (left_xy[LTOP] < 0 ||
  1719. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  1720. (top_xy < 0 ||
  1721. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  1722. if (!FRAME_MBAFF(h))
  1723. return 1;
  1724. if ((left_xy[LTOP] < 0 ||
  1725. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  1726. (top_xy < h->mb_stride ||
  1727. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  1728. return 1;
  1729. }
  1730. }
  1731. top_type = h->cur_pic.mb_type[top_xy];
  1732. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  1733. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  1734. if (h->deblocking_filter == 2) {
  1735. if (h->slice_table[top_xy] != h->slice_num)
  1736. top_type = 0;
  1737. if (h->slice_table[left_xy[LBOT]] != h->slice_num)
  1738. left_type[LTOP] = left_type[LBOT] = 0;
  1739. } else {
  1740. if (h->slice_table[top_xy] == 0xFFFF)
  1741. top_type = 0;
  1742. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  1743. left_type[LTOP] = left_type[LBOT] = 0;
  1744. }
  1745. h->top_type = top_type;
  1746. h->left_type[LTOP] = left_type[LTOP];
  1747. h->left_type[LBOT] = left_type[LBOT];
  1748. if (IS_INTRA(mb_type))
  1749. return 0;
  1750. fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
  1751. top_type, left_type, mb_xy, 0);
  1752. if (h->list_count == 2)
  1753. fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
  1754. top_type, left_type, mb_xy, 1);
  1755. nnz = h->non_zero_count[mb_xy];
  1756. nnz_cache = h->non_zero_count_cache;
  1757. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  1758. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  1759. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  1760. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  1761. h->cbp = h->cbp_table[mb_xy];
  1762. if (top_type) {
  1763. nnz = h->non_zero_count[top_xy];
  1764. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  1765. }
  1766. if (left_type[LTOP]) {
  1767. nnz = h->non_zero_count[left_xy[LTOP]];
  1768. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  1769. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  1770. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  1771. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  1772. }
  1773. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  1774. * from what the loop filter needs */
  1775. if (!CABAC(h) && h->pps.transform_8x8_mode) {
  1776. if (IS_8x8DCT(top_type)) {
  1777. nnz_cache[4 + 8 * 0] =
  1778. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  1779. nnz_cache[6 + 8 * 0] =
  1780. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  1781. }
  1782. if (IS_8x8DCT(left_type[LTOP])) {
  1783. nnz_cache[3 + 8 * 1] =
  1784. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  1785. }
  1786. if (IS_8x8DCT(left_type[LBOT])) {
  1787. nnz_cache[3 + 8 * 3] =
  1788. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  1789. }
  1790. if (IS_8x8DCT(mb_type)) {
  1791. nnz_cache[scan8[0]] =
  1792. nnz_cache[scan8[1]] =
  1793. nnz_cache[scan8[2]] =
  1794. nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
  1795. nnz_cache[scan8[0 + 4]] =
  1796. nnz_cache[scan8[1 + 4]] =
  1797. nnz_cache[scan8[2 + 4]] =
  1798. nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
  1799. nnz_cache[scan8[0 + 8]] =
  1800. nnz_cache[scan8[1 + 8]] =
  1801. nnz_cache[scan8[2 + 8]] =
  1802. nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
  1803. nnz_cache[scan8[0 + 12]] =
  1804. nnz_cache[scan8[1 + 12]] =
  1805. nnz_cache[scan8[2 + 12]] =
  1806. nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
  1807. }
  1808. }
  1809. return 0;
  1810. }
  1811. static void loop_filter(H264Context *h, int start_x, int end_x)
  1812. {
  1813. uint8_t *dest_y, *dest_cb, *dest_cr;
  1814. int linesize, uvlinesize, mb_x, mb_y;
  1815. const int end_mb_y = h->mb_y + FRAME_MBAFF(h);
  1816. const int old_slice_type = h->slice_type;
  1817. const int pixel_shift = h->pixel_shift;
  1818. const int block_h = 16 >> h->chroma_y_shift;
  1819. if (h->deblocking_filter) {
  1820. for (mb_x = start_x; mb_x < end_x; mb_x++)
  1821. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  1822. int mb_xy, mb_type;
  1823. mb_xy = h->mb_xy = mb_x + mb_y * h->mb_stride;
  1824. h->slice_num = h->slice_table[mb_xy];
  1825. mb_type = h->cur_pic.mb_type[mb_xy];
  1826. h->list_count = h->list_counts[mb_xy];
  1827. if (FRAME_MBAFF(h))
  1828. h->mb_mbaff =
  1829. h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  1830. h->mb_x = mb_x;
  1831. h->mb_y = mb_y;
  1832. dest_y = h->cur_pic.f.data[0] +
  1833. ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
  1834. dest_cb = h->cur_pic.f.data[1] +
  1835. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1836. mb_y * h->uvlinesize * block_h;
  1837. dest_cr = h->cur_pic.f.data[2] +
  1838. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  1839. mb_y * h->uvlinesize * block_h;
  1840. // FIXME simplify above
  1841. if (MB_FIELD(h)) {
  1842. linesize = h->mb_linesize = h->linesize * 2;
  1843. uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
  1844. if (mb_y & 1) { // FIXME move out of this function?
  1845. dest_y -= h->linesize * 15;
  1846. dest_cb -= h->uvlinesize * (block_h - 1);
  1847. dest_cr -= h->uvlinesize * (block_h - 1);
  1848. }
  1849. } else {
  1850. linesize = h->mb_linesize = h->linesize;
  1851. uvlinesize = h->mb_uvlinesize = h->uvlinesize;
  1852. }
  1853. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  1854. uvlinesize, 0);
  1855. if (fill_filter_caches(h, mb_type))
  1856. continue;
  1857. h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
  1858. h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
  1859. if (FRAME_MBAFF(h)) {
  1860. ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  1861. linesize, uvlinesize);
  1862. } else {
  1863. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
  1864. dest_cr, linesize, uvlinesize);
  1865. }
  1866. }
  1867. }
  1868. h->slice_type = old_slice_type;
  1869. h->mb_x = end_x;
  1870. h->mb_y = end_mb_y - FRAME_MBAFF(h);
  1871. h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
  1872. h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
  1873. }
  1874. static void predict_field_decoding_flag(H264Context *h)
  1875. {
  1876. const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
  1877. int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
  1878. h->cur_pic.mb_type[mb_xy - 1] :
  1879. (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
  1880. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  1881. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  1882. }
  1883. /**
  1884. * Draw edges and report progress for the last MB row.
  1885. */
  1886. static void decode_finish_row(H264Context *h)
  1887. {
  1888. int top = 16 * (h->mb_y >> FIELD_PICTURE(h));
  1889. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  1890. int height = 16 << FRAME_MBAFF(h);
  1891. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  1892. if (h->deblocking_filter) {
  1893. if ((top + height) >= pic_height)
  1894. height += deblock_border;
  1895. top -= deblock_border;
  1896. }
  1897. if (top >= pic_height || (top + height) < 0)
  1898. return;
  1899. height = FFMIN(height, pic_height - top);
  1900. if (top < 0) {
  1901. height = top + height;
  1902. top = 0;
  1903. }
  1904. ff_h264_draw_horiz_band(h, top, height);
  1905. if (h->droppable)
  1906. return;
  1907. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  1908. h->picture_structure == PICT_BOTTOM_FIELD);
  1909. }
  1910. static void er_add_slice(H264Context *h, int startx, int starty,
  1911. int endx, int endy, int status)
  1912. {
  1913. #if CONFIG_ERROR_RESILIENCE
  1914. ERContext *er = &h->er;
  1915. er->ref_count = h->ref_count[0];
  1916. ff_er_add_slice(er, startx, starty, endx, endy, status);
  1917. #endif
  1918. }
  1919. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  1920. {
  1921. H264Context *h = *(void **)arg;
  1922. int lf_x_start = h->mb_x;
  1923. h->mb_skip_run = -1;
  1924. h->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  1925. avctx->codec_id != AV_CODEC_ID_H264 ||
  1926. (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
  1927. if (h->pps.cabac) {
  1928. /* realign */
  1929. align_get_bits(&h->gb);
  1930. /* init cabac */
  1931. ff_init_cabac_decoder(&h->cabac,
  1932. h->gb.buffer + get_bits_count(&h->gb) / 8,
  1933. (get_bits_left(&h->gb) + 7) / 8);
  1934. ff_h264_init_cabac_states(h);
  1935. for (;;) {
  1936. // START_TIMER
  1937. int ret = ff_h264_decode_mb_cabac(h);
  1938. int eos;
  1939. // STOP_TIMER("decode_mb_cabac")
  1940. if (ret >= 0)
  1941. ff_h264_hl_decode_mb(h);
  1942. // FIXME optimal? or let mb_decode decode 16x32 ?
  1943. if (ret >= 0 && FRAME_MBAFF(h)) {
  1944. h->mb_y++;
  1945. ret = ff_h264_decode_mb_cabac(h);
  1946. if (ret >= 0)
  1947. ff_h264_hl_decode_mb(h);
  1948. h->mb_y--;
  1949. }
  1950. eos = get_cabac_terminate(&h->cabac);
  1951. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  1952. h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  1953. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
  1954. h->mb_y, ER_MB_END);
  1955. if (h->mb_x >= lf_x_start)
  1956. loop_filter(h, lf_x_start, h->mb_x + 1);
  1957. return 0;
  1958. }
  1959. if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  1960. av_log(h->avctx, AV_LOG_ERROR,
  1961. "error while decoding MB %d %d, bytestream %td\n",
  1962. h->mb_x, h->mb_y,
  1963. h->cabac.bytestream_end - h->cabac.bytestream);
  1964. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  1965. h->mb_y, ER_MB_ERROR);
  1966. return AVERROR_INVALIDDATA;
  1967. }
  1968. if (++h->mb_x >= h->mb_width) {
  1969. loop_filter(h, lf_x_start, h->mb_x);
  1970. h->mb_x = lf_x_start = 0;
  1971. decode_finish_row(h);
  1972. ++h->mb_y;
  1973. if (FIELD_OR_MBAFF_PICTURE(h)) {
  1974. ++h->mb_y;
  1975. if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
  1976. predict_field_decoding_flag(h);
  1977. }
  1978. }
  1979. if (eos || h->mb_y >= h->mb_height) {
  1980. tprintf(h->avctx, "slice end %d %d\n",
  1981. get_bits_count(&h->gb), h->gb.size_in_bits);
  1982. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
  1983. h->mb_y, ER_MB_END);
  1984. if (h->mb_x > lf_x_start)
  1985. loop_filter(h, lf_x_start, h->mb_x);
  1986. return 0;
  1987. }
  1988. }
  1989. } else {
  1990. for (;;) {
  1991. int ret = ff_h264_decode_mb_cavlc(h);
  1992. if (ret >= 0)
  1993. ff_h264_hl_decode_mb(h);
  1994. // FIXME optimal? or let mb_decode decode 16x32 ?
  1995. if (ret >= 0 && FRAME_MBAFF(h)) {
  1996. h->mb_y++;
  1997. ret = ff_h264_decode_mb_cavlc(h);
  1998. if (ret >= 0)
  1999. ff_h264_hl_decode_mb(h);
  2000. h->mb_y--;
  2001. }
  2002. if (ret < 0) {
  2003. av_log(h->avctx, AV_LOG_ERROR,
  2004. "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
  2005. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  2006. h->mb_y, ER_MB_ERROR);
  2007. return ret;
  2008. }
  2009. if (++h->mb_x >= h->mb_width) {
  2010. loop_filter(h, lf_x_start, h->mb_x);
  2011. h->mb_x = lf_x_start = 0;
  2012. decode_finish_row(h);
  2013. ++h->mb_y;
  2014. if (FIELD_OR_MBAFF_PICTURE(h)) {
  2015. ++h->mb_y;
  2016. if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
  2017. predict_field_decoding_flag(h);
  2018. }
  2019. if (h->mb_y >= h->mb_height) {
  2020. tprintf(h->avctx, "slice end %d %d\n",
  2021. get_bits_count(&h->gb), h->gb.size_in_bits);
  2022. if (get_bits_left(&h->gb) == 0) {
  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 0;
  2027. } else {
  2028. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  2029. h->mb_x - 1, h->mb_y,
  2030. ER_MB_END);
  2031. return AVERROR_INVALIDDATA;
  2032. }
  2033. }
  2034. }
  2035. if (get_bits_left(&h->gb) <= 0 && h->mb_skip_run <= 0) {
  2036. tprintf(h->avctx, "slice end %d %d\n",
  2037. get_bits_count(&h->gb), h->gb.size_in_bits);
  2038. if (get_bits_left(&h->gb) == 0) {
  2039. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  2040. h->mb_x - 1, h->mb_y,
  2041. ER_MB_END);
  2042. if (h->mb_x > lf_x_start)
  2043. loop_filter(h, lf_x_start, h->mb_x);
  2044. return 0;
  2045. } else {
  2046. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  2047. h->mb_y, ER_MB_ERROR);
  2048. return AVERROR_INVALIDDATA;
  2049. }
  2050. }
  2051. }
  2052. }
  2053. }
  2054. /**
  2055. * Call decode_slice() for each context.
  2056. *
  2057. * @param h h264 master context
  2058. * @param context_count number of contexts to execute
  2059. */
  2060. int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
  2061. {
  2062. AVCodecContext *const avctx = h->avctx;
  2063. H264Context *hx;
  2064. int i;
  2065. if (h->mb_y >= h->mb_height) {
  2066. av_log(h->avctx, AV_LOG_ERROR,
  2067. "Input contains more MB rows than the frame height.\n");
  2068. return AVERROR_INVALIDDATA;
  2069. }
  2070. if (h->avctx->hwaccel)
  2071. return 0;
  2072. if (context_count == 1) {
  2073. return decode_slice(avctx, &h);
  2074. } else {
  2075. for (i = 1; i < context_count; i++) {
  2076. hx = h->thread_context[i];
  2077. hx->er.error_count = 0;
  2078. }
  2079. avctx->execute(avctx, decode_slice, h->thread_context,
  2080. NULL, context_count, sizeof(void *));
  2081. /* pull back stuff from slices to master context */
  2082. hx = h->thread_context[context_count - 1];
  2083. h->mb_x = hx->mb_x;
  2084. h->mb_y = hx->mb_y;
  2085. h->droppable = hx->droppable;
  2086. h->picture_structure = hx->picture_structure;
  2087. for (i = 1; i < context_count; i++)
  2088. h->er.error_count += h->thread_context[i]->er.error_count;
  2089. }
  2090. return 0;
  2091. }