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