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.

2379 lines
87KB

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