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.

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