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.

5340 lines
198KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/stereo3d.h"
  31. #include "internal.h"
  32. #include "cabac.h"
  33. #include "cabac_functions.h"
  34. #include "dsputil.h"
  35. #include "error_resilience.h"
  36. #include "avcodec.h"
  37. #include "mpegvideo.h"
  38. #include "h264.h"
  39. #include "h264data.h"
  40. #include "h264chroma.h"
  41. #include "h264_mvpred.h"
  42. #include "golomb.h"
  43. #include "mathops.h"
  44. #include "rectangle.h"
  45. #include "svq3.h"
  46. #include "thread.h"
  47. #include "vdpau_internal.h"
  48. #include <assert.h>
  49. static void flush_change(H264Context *h);
  50. const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
  51. static const uint8_t rem6[QP_MAX_NUM + 1] = {
  52. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
  53. 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  54. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
  55. 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  56. 0, 1, 2, 3,
  57. };
  58. static const uint8_t div6[QP_MAX_NUM + 1] = {
  59. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
  60. 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
  61. 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10,
  62. 10,10,10,11,11,11,11,11,11,12,12,12,12,12,12,13,13,13, 13, 13, 13,
  63. 14,14,14,14,
  64. };
  65. static const uint8_t field_scan[16+1] = {
  66. 0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
  67. 0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
  68. 2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
  69. 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
  70. };
  71. static const uint8_t field_scan8x8[64+1] = {
  72. 0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
  73. 1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
  74. 2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
  75. 0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
  76. 2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
  77. 2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
  78. 2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
  79. 3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
  80. 3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
  81. 4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
  82. 4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
  83. 5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
  84. 5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
  85. 7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
  86. 6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
  87. 7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
  88. };
  89. static const uint8_t field_scan8x8_cavlc[64+1] = {
  90. 0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
  91. 2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
  92. 3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
  93. 5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
  94. 0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
  95. 1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
  96. 3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
  97. 5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
  98. 0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
  99. 1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
  100. 3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
  101. 5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
  102. 1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
  103. 1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
  104. 3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
  105. 6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
  106. };
  107. // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
  108. static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
  109. 0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
  110. 4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
  111. 3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
  112. 2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
  113. 1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
  114. 3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
  115. 2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
  116. 3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
  117. 0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
  118. 2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
  119. 1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
  120. 4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
  121. 0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
  122. 1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
  123. 0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
  124. 5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
  125. };
  126. static const uint8_t dequant4_coeff_init[6][3] = {
  127. { 10, 13, 16 },
  128. { 11, 14, 18 },
  129. { 13, 16, 20 },
  130. { 14, 18, 23 },
  131. { 16, 20, 25 },
  132. { 18, 23, 29 },
  133. };
  134. static const uint8_t dequant8_coeff_init_scan[16] = {
  135. 0, 3, 4, 3, 3, 1, 5, 1, 4, 5, 2, 5, 3, 1, 5, 1
  136. };
  137. static const uint8_t dequant8_coeff_init[6][6] = {
  138. { 20, 18, 32, 19, 25, 24 },
  139. { 22, 19, 35, 21, 28, 26 },
  140. { 26, 23, 42, 24, 33, 31 },
  141. { 28, 25, 45, 26, 35, 33 },
  142. { 32, 28, 51, 30, 40, 38 },
  143. { 36, 32, 58, 34, 46, 43 },
  144. };
  145. static const enum AVPixelFormat h264_hwaccel_pixfmt_list_420[] = {
  146. #if CONFIG_H264_DXVA2_HWACCEL
  147. AV_PIX_FMT_DXVA2_VLD,
  148. #endif
  149. #if CONFIG_H264_VAAPI_HWACCEL
  150. AV_PIX_FMT_VAAPI_VLD,
  151. #endif
  152. #if CONFIG_H264_VDA_HWACCEL
  153. AV_PIX_FMT_VDA_VLD,
  154. #endif
  155. #if CONFIG_H264_VDPAU_HWACCEL
  156. AV_PIX_FMT_VDPAU,
  157. #endif
  158. AV_PIX_FMT_YUV420P,
  159. AV_PIX_FMT_NONE
  160. };
  161. static const enum AVPixelFormat h264_hwaccel_pixfmt_list_jpeg_420[] = {
  162. #if CONFIG_H264_DXVA2_HWACCEL
  163. AV_PIX_FMT_DXVA2_VLD,
  164. #endif
  165. #if CONFIG_H264_VAAPI_HWACCEL
  166. AV_PIX_FMT_VAAPI_VLD,
  167. #endif
  168. #if CONFIG_H264_VDA_HWACCEL
  169. AV_PIX_FMT_VDA_VLD,
  170. #endif
  171. #if CONFIG_H264_VDPAU_HWACCEL
  172. AV_PIX_FMT_VDPAU,
  173. #endif
  174. AV_PIX_FMT_YUVJ420P,
  175. AV_PIX_FMT_NONE
  176. };
  177. int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
  178. {
  179. H264Context *h = avctx->priv_data;
  180. return h ? h->sps.num_reorder_frames : 0;
  181. }
  182. static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  183. int (*mv)[2][4][2],
  184. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  185. {
  186. H264Context *h = opaque;
  187. h->mb_x = mb_x;
  188. h->mb_y = mb_y;
  189. h->mb_xy = mb_x + mb_y * h->mb_stride;
  190. memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
  191. av_assert1(ref >= 0);
  192. /* FIXME: It is possible albeit uncommon that slice references
  193. * differ between slices. We take the easy approach and ignore
  194. * it for now. If this turns out to have any relevance in
  195. * practice then correct remapping should be added. */
  196. if (ref >= h->ref_count[0])
  197. ref = 0;
  198. if (!h->ref_list[0][ref].f.data[0]) {
  199. av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
  200. ref = 0;
  201. }
  202. if ((h->ref_list[0][ref].reference&3) != 3) {
  203. av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
  204. return;
  205. }
  206. fill_rectangle(&h->cur_pic.ref_index[0][4 * h->mb_xy],
  207. 2, 2, 2, ref, 1);
  208. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  209. fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
  210. pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
  211. h->mb_mbaff =
  212. h->mb_field_decoding_flag = 0;
  213. ff_h264_hl_decode_mb(h);
  214. }
  215. void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
  216. {
  217. AVCodecContext *avctx = h->avctx;
  218. Picture *cur = &h->cur_pic;
  219. Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL;
  220. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  221. int vshift = desc->log2_chroma_h;
  222. const int field_pic = h->picture_structure != PICT_FRAME;
  223. if (field_pic) {
  224. height <<= 1;
  225. y <<= 1;
  226. }
  227. height = FFMIN(height, avctx->height - y);
  228. if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
  229. return;
  230. if (avctx->draw_horiz_band) {
  231. AVFrame *src;
  232. int offset[AV_NUM_DATA_POINTERS];
  233. int i;
  234. if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
  235. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  236. src = &cur->f;
  237. else if (last)
  238. src = &last->f;
  239. else
  240. return;
  241. offset[0] = y * src->linesize[0];
  242. offset[1] =
  243. offset[2] = (y >> vshift) * src->linesize[1];
  244. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  245. offset[i] = 0;
  246. emms_c();
  247. avctx->draw_horiz_band(avctx, src, offset,
  248. y, h->picture_structure, height);
  249. }
  250. }
  251. static void unref_picture(H264Context *h, Picture *pic)
  252. {
  253. int off = offsetof(Picture, tf) + sizeof(pic->tf);
  254. int i;
  255. if (!pic->f.buf[0])
  256. return;
  257. ff_thread_release_buffer(h->avctx, &pic->tf);
  258. av_buffer_unref(&pic->hwaccel_priv_buf);
  259. av_buffer_unref(&pic->qscale_table_buf);
  260. av_buffer_unref(&pic->mb_type_buf);
  261. for (i = 0; i < 2; i++) {
  262. av_buffer_unref(&pic->motion_val_buf[i]);
  263. av_buffer_unref(&pic->ref_index_buf[i]);
  264. }
  265. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  266. }
  267. static void release_unused_pictures(H264Context *h, int remove_current)
  268. {
  269. int i;
  270. /* release non reference frames */
  271. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  272. if (h->DPB[i].f.buf[0] && !h->DPB[i].reference &&
  273. (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
  274. unref_picture(h, &h->DPB[i]);
  275. }
  276. }
  277. }
  278. static int ref_picture(H264Context *h, Picture *dst, Picture *src)
  279. {
  280. int ret, i;
  281. av_assert0(!dst->f.buf[0]);
  282. av_assert0(src->f.buf[0]);
  283. src->tf.f = &src->f;
  284. dst->tf.f = &dst->f;
  285. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  286. if (ret < 0)
  287. goto fail;
  288. dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
  289. dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
  290. if (!dst->qscale_table_buf || !dst->mb_type_buf)
  291. goto fail;
  292. dst->qscale_table = src->qscale_table;
  293. dst->mb_type = src->mb_type;
  294. for (i = 0; i < 2; i++) {
  295. dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
  296. dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
  297. if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
  298. goto fail;
  299. dst->motion_val[i] = src->motion_val[i];
  300. dst->ref_index[i] = src->ref_index[i];
  301. }
  302. if (src->hwaccel_picture_private) {
  303. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  304. if (!dst->hwaccel_priv_buf)
  305. goto fail;
  306. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  307. }
  308. for (i = 0; i < 2; i++)
  309. dst->field_poc[i] = src->field_poc[i];
  310. memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
  311. memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
  312. dst->poc = src->poc;
  313. dst->frame_num = src->frame_num;
  314. dst->mmco_reset = src->mmco_reset;
  315. dst->pic_id = src->pic_id;
  316. dst->long_ref = src->long_ref;
  317. dst->mbaff = src->mbaff;
  318. dst->field_picture = src->field_picture;
  319. dst->needs_realloc = src->needs_realloc;
  320. dst->reference = src->reference;
  321. dst->crop = src->crop;
  322. dst->crop_left = src->crop_left;
  323. dst->crop_top = src->crop_top;
  324. dst->recovered = src->recovered;
  325. return 0;
  326. fail:
  327. unref_picture(h, dst);
  328. return ret;
  329. }
  330. static int alloc_scratch_buffers(H264Context *h, int linesize)
  331. {
  332. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  333. if (h->bipred_scratchpad)
  334. return 0;
  335. h->bipred_scratchpad = av_malloc(16 * 6 * alloc_size);
  336. // edge emu needs blocksize + filter length - 1
  337. // (= 21x21 for h264)
  338. h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 21);
  339. h->me.scratchpad = av_mallocz(alloc_size * 2 * 16 * 2);
  340. if (!h->bipred_scratchpad || !h->edge_emu_buffer || !h->me.scratchpad) {
  341. av_freep(&h->bipred_scratchpad);
  342. av_freep(&h->edge_emu_buffer);
  343. av_freep(&h->me.scratchpad);
  344. return AVERROR(ENOMEM);
  345. }
  346. h->me.temp = h->me.scratchpad;
  347. return 0;
  348. }
  349. static int init_table_pools(H264Context *h)
  350. {
  351. const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
  352. const int mb_array_size = h->mb_stride * h->mb_height;
  353. const int b4_stride = h->mb_width * 4 + 1;
  354. const int b4_array_size = b4_stride * h->mb_height * 4;
  355. h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
  356. av_buffer_allocz);
  357. h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
  358. sizeof(uint32_t), av_buffer_allocz);
  359. h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
  360. sizeof(int16_t), av_buffer_allocz);
  361. h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
  362. if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
  363. !h->ref_index_pool) {
  364. av_buffer_pool_uninit(&h->qscale_table_pool);
  365. av_buffer_pool_uninit(&h->mb_type_pool);
  366. av_buffer_pool_uninit(&h->motion_val_pool);
  367. av_buffer_pool_uninit(&h->ref_index_pool);
  368. return AVERROR(ENOMEM);
  369. }
  370. return 0;
  371. }
  372. static int alloc_picture(H264Context *h, Picture *pic)
  373. {
  374. int i, ret = 0;
  375. av_assert0(!pic->f.data[0]);
  376. pic->tf.f = &pic->f;
  377. ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
  378. AV_GET_BUFFER_FLAG_REF : 0);
  379. if (ret < 0)
  380. goto fail;
  381. h->linesize = pic->f.linesize[0];
  382. h->uvlinesize = pic->f.linesize[1];
  383. pic->crop = h->sps.crop;
  384. pic->crop_top = h->sps.crop_top;
  385. pic->crop_left= h->sps.crop_left;
  386. if (h->avctx->hwaccel) {
  387. const AVHWAccel *hwaccel = h->avctx->hwaccel;
  388. av_assert0(!pic->hwaccel_picture_private);
  389. if (hwaccel->priv_data_size) {
  390. pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->priv_data_size);
  391. if (!pic->hwaccel_priv_buf)
  392. return AVERROR(ENOMEM);
  393. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  394. }
  395. }
  396. if (!h->qscale_table_pool) {
  397. ret = init_table_pools(h);
  398. if (ret < 0)
  399. goto fail;
  400. }
  401. pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
  402. pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
  403. if (!pic->qscale_table_buf || !pic->mb_type_buf)
  404. goto fail;
  405. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
  406. pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
  407. for (i = 0; i < 2; i++) {
  408. pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
  409. pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
  410. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  411. goto fail;
  412. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  413. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  414. }
  415. return 0;
  416. fail:
  417. unref_picture(h, pic);
  418. return (ret < 0) ? ret : AVERROR(ENOMEM);
  419. }
  420. static inline int pic_is_unused(H264Context *h, Picture *pic)
  421. {
  422. if (!pic->f.buf[0])
  423. return 1;
  424. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  425. return 1;
  426. return 0;
  427. }
  428. static int find_unused_picture(H264Context *h)
  429. {
  430. int i;
  431. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  432. if (pic_is_unused(h, &h->DPB[i]))
  433. break;
  434. }
  435. if (i == MAX_PICTURE_COUNT)
  436. return AVERROR_INVALIDDATA;
  437. if (h->DPB[i].needs_realloc) {
  438. h->DPB[i].needs_realloc = 0;
  439. unref_picture(h, &h->DPB[i]);
  440. }
  441. return i;
  442. }
  443. /**
  444. * Check if the top & left blocks are available if needed and
  445. * change the dc mode so it only uses the available blocks.
  446. */
  447. int ff_h264_check_intra4x4_pred_mode(H264Context *h)
  448. {
  449. static const int8_t top[12] = {
  450. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  451. };
  452. static const int8_t left[12] = {
  453. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  454. };
  455. int i;
  456. if (!(h->top_samples_available & 0x8000)) {
  457. for (i = 0; i < 4; i++) {
  458. int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
  459. if (status < 0) {
  460. av_log(h->avctx, AV_LOG_ERROR,
  461. "top block unavailable for requested intra4x4 mode %d at %d %d\n",
  462. status, h->mb_x, h->mb_y);
  463. return AVERROR_INVALIDDATA;
  464. } else if (status) {
  465. h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
  466. }
  467. }
  468. }
  469. if ((h->left_samples_available & 0x8888) != 0x8888) {
  470. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  471. for (i = 0; i < 4; i++)
  472. if (!(h->left_samples_available & mask[i])) {
  473. int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
  474. if (status < 0) {
  475. av_log(h->avctx, AV_LOG_ERROR,
  476. "left block unavailable for requested intra4x4 mode %d at %d %d\n",
  477. status, h->mb_x, h->mb_y);
  478. return AVERROR_INVALIDDATA;
  479. } else if (status) {
  480. h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
  481. }
  482. }
  483. }
  484. return 0;
  485. } // FIXME cleanup like ff_h264_check_intra_pred_mode
  486. /**
  487. * Check if the top & left blocks are available if needed and
  488. * change the dc mode so it only uses the available blocks.
  489. */
  490. int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
  491. {
  492. static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  493. static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  494. if (mode > 3U) {
  495. av_log(h->avctx, AV_LOG_ERROR,
  496. "out of range intra chroma pred mode at %d %d\n",
  497. h->mb_x, h->mb_y);
  498. return AVERROR_INVALIDDATA;
  499. }
  500. if (!(h->top_samples_available & 0x8000)) {
  501. mode = top[mode];
  502. if (mode < 0) {
  503. av_log(h->avctx, AV_LOG_ERROR,
  504. "top block unavailable for requested intra mode at %d %d\n",
  505. h->mb_x, h->mb_y);
  506. return AVERROR_INVALIDDATA;
  507. }
  508. }
  509. if ((h->left_samples_available & 0x8080) != 0x8080) {
  510. mode = left[mode];
  511. if (is_chroma && (h->left_samples_available & 0x8080)) {
  512. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  513. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  514. (!(h->left_samples_available & 0x8000)) +
  515. 2 * (mode == DC_128_PRED8x8);
  516. }
  517. if (mode < 0) {
  518. av_log(h->avctx, AV_LOG_ERROR,
  519. "left block unavailable for requested intra mode at %d %d\n",
  520. h->mb_x, h->mb_y);
  521. return AVERROR_INVALIDDATA;
  522. }
  523. }
  524. return mode;
  525. }
  526. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
  527. int *dst_length, int *consumed, int length)
  528. {
  529. int i, si, di;
  530. uint8_t *dst;
  531. int bufidx;
  532. // src[0]&0x80; // forbidden bit
  533. h->nal_ref_idc = src[0] >> 5;
  534. h->nal_unit_type = src[0] & 0x1F;
  535. src++;
  536. length--;
  537. #define STARTCODE_TEST \
  538. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  539. if (src[i + 2] != 3) { \
  540. /* startcode, so we must be past the end */ \
  541. length = i; \
  542. } \
  543. break; \
  544. }
  545. #if HAVE_FAST_UNALIGNED
  546. #define FIND_FIRST_ZERO \
  547. if (i > 0 && !src[i]) \
  548. i--; \
  549. while (src[i]) \
  550. i++
  551. #if HAVE_FAST_64BIT
  552. for (i = 0; i + 1 < length; i += 9) {
  553. if (!((~AV_RN64A(src + i) &
  554. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  555. 0x8000800080008080ULL))
  556. continue;
  557. FIND_FIRST_ZERO;
  558. STARTCODE_TEST;
  559. i -= 7;
  560. }
  561. #else
  562. for (i = 0; i + 1 < length; i += 5) {
  563. if (!((~AV_RN32A(src + i) &
  564. (AV_RN32A(src + i) - 0x01000101U)) &
  565. 0x80008080U))
  566. continue;
  567. FIND_FIRST_ZERO;
  568. STARTCODE_TEST;
  569. i -= 3;
  570. }
  571. #endif
  572. #else
  573. for (i = 0; i + 1 < length; i += 2) {
  574. if (src[i])
  575. continue;
  576. if (i > 0 && src[i - 1] == 0)
  577. i--;
  578. STARTCODE_TEST;
  579. }
  580. #endif
  581. // use second escape buffer for inter data
  582. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
  583. si = h->rbsp_buffer_size[bufidx];
  584. av_fast_padded_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+MAX_MBPAIR_SIZE);
  585. dst = h->rbsp_buffer[bufidx];
  586. if (dst == NULL)
  587. return NULL;
  588. if(i>=length-1){ //no escaped 0
  589. *dst_length= length;
  590. *consumed= length+1; //+1 for the header
  591. if(h->avctx->flags2 & CODEC_FLAG2_FAST){
  592. return src;
  593. }else{
  594. memcpy(dst, src, length);
  595. return dst;
  596. }
  597. }
  598. memcpy(dst, src, i);
  599. si = di = i;
  600. while (si + 2 < length) {
  601. // remove escapes (very rare 1:2^22)
  602. if (src[si + 2] > 3) {
  603. dst[di++] = src[si++];
  604. dst[di++] = src[si++];
  605. } else if (src[si] == 0 && src[si + 1] == 0) {
  606. if (src[si + 2] == 3) { // escape
  607. dst[di++] = 0;
  608. dst[di++] = 0;
  609. si += 3;
  610. continue;
  611. } else // next start code
  612. goto nsc;
  613. }
  614. dst[di++] = src[si++];
  615. }
  616. while (si < length)
  617. dst[di++] = src[si++];
  618. nsc:
  619. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  620. *dst_length = di;
  621. *consumed = si + 1; // +1 for the header
  622. /* FIXME store exact number of bits in the getbitcontext
  623. * (it is needed for decoding) */
  624. return dst;
  625. }
  626. /**
  627. * Identify the exact end of the bitstream
  628. * @return the length of the trailing, or 0 if damaged
  629. */
  630. static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
  631. {
  632. int v = *src;
  633. int r;
  634. tprintf(h->avctx, "rbsp trailing %X\n", v);
  635. for (r = 1; r < 9; r++) {
  636. if (v & 1)
  637. return r;
  638. v >>= 1;
  639. }
  640. return 0;
  641. }
  642. static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
  643. int height, int y_offset, int list)
  644. {
  645. int raw_my = h->mv_cache[list][scan8[n]][1];
  646. int filter_height_down = (raw_my & 3) ? 3 : 0;
  647. int full_my = (raw_my >> 2) + y_offset;
  648. int bottom = full_my + filter_height_down + height;
  649. av_assert2(height >= 0);
  650. return FFMAX(0, bottom);
  651. }
  652. static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
  653. int height, int y_offset, int list0,
  654. int list1, int *nrefs)
  655. {
  656. int my;
  657. y_offset += 16 * (h->mb_y >> MB_FIELD(h));
  658. if (list0) {
  659. int ref_n = h->ref_cache[0][scan8[n]];
  660. Picture *ref = &h->ref_list[0][ref_n];
  661. // Error resilience puts the current picture in the ref list.
  662. // Don't try to wait on these as it will cause a deadlock.
  663. // Fields can wait on each other, though.
  664. if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
  665. (ref->reference & 3) != h->picture_structure) {
  666. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
  667. if (refs[0][ref_n] < 0)
  668. nrefs[0] += 1;
  669. refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
  670. }
  671. }
  672. if (list1) {
  673. int ref_n = h->ref_cache[1][scan8[n]];
  674. Picture *ref = &h->ref_list[1][ref_n];
  675. if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
  676. (ref->reference & 3) != h->picture_structure) {
  677. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
  678. if (refs[1][ref_n] < 0)
  679. nrefs[1] += 1;
  680. refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
  681. }
  682. }
  683. }
  684. /**
  685. * Wait until all reference frames are available for MC operations.
  686. *
  687. * @param h the H264 context
  688. */
  689. static void await_references(H264Context *h)
  690. {
  691. const int mb_xy = h->mb_xy;
  692. const int mb_type = h->cur_pic.mb_type[mb_xy];
  693. int refs[2][48];
  694. int nrefs[2] = { 0 };
  695. int ref, list;
  696. memset(refs, -1, sizeof(refs));
  697. if (IS_16X16(mb_type)) {
  698. get_lowest_part_y(h, refs, 0, 16, 0,
  699. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  700. } else if (IS_16X8(mb_type)) {
  701. get_lowest_part_y(h, refs, 0, 8, 0,
  702. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  703. get_lowest_part_y(h, refs, 8, 8, 8,
  704. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  705. } else if (IS_8X16(mb_type)) {
  706. get_lowest_part_y(h, refs, 0, 16, 0,
  707. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  708. get_lowest_part_y(h, refs, 4, 16, 0,
  709. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  710. } else {
  711. int i;
  712. av_assert2(IS_8X8(mb_type));
  713. for (i = 0; i < 4; i++) {
  714. const int sub_mb_type = h->sub_mb_type[i];
  715. const int n = 4 * i;
  716. int y_offset = (i & 2) << 2;
  717. if (IS_SUB_8X8(sub_mb_type)) {
  718. get_lowest_part_y(h, refs, n, 8, y_offset,
  719. IS_DIR(sub_mb_type, 0, 0),
  720. IS_DIR(sub_mb_type, 0, 1),
  721. nrefs);
  722. } else if (IS_SUB_8X4(sub_mb_type)) {
  723. get_lowest_part_y(h, refs, n, 4, y_offset,
  724. IS_DIR(sub_mb_type, 0, 0),
  725. IS_DIR(sub_mb_type, 0, 1),
  726. nrefs);
  727. get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
  728. IS_DIR(sub_mb_type, 0, 0),
  729. IS_DIR(sub_mb_type, 0, 1),
  730. nrefs);
  731. } else if (IS_SUB_4X8(sub_mb_type)) {
  732. get_lowest_part_y(h, refs, n, 8, y_offset,
  733. IS_DIR(sub_mb_type, 0, 0),
  734. IS_DIR(sub_mb_type, 0, 1),
  735. nrefs);
  736. get_lowest_part_y(h, refs, n + 1, 8, y_offset,
  737. IS_DIR(sub_mb_type, 0, 0),
  738. IS_DIR(sub_mb_type, 0, 1),
  739. nrefs);
  740. } else {
  741. int j;
  742. av_assert2(IS_SUB_4X4(sub_mb_type));
  743. for (j = 0; j < 4; j++) {
  744. int sub_y_offset = y_offset + 2 * (j & 2);
  745. get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
  746. IS_DIR(sub_mb_type, 0, 0),
  747. IS_DIR(sub_mb_type, 0, 1),
  748. nrefs);
  749. }
  750. }
  751. }
  752. }
  753. for (list = h->list_count - 1; list >= 0; list--)
  754. for (ref = 0; ref < 48 && nrefs[list]; ref++) {
  755. int row = refs[list][ref];
  756. if (row >= 0) {
  757. Picture *ref_pic = &h->ref_list[list][ref];
  758. int ref_field = ref_pic->reference - 1;
  759. int ref_field_picture = ref_pic->field_picture;
  760. int pic_height = 16 * h->mb_height >> ref_field_picture;
  761. row <<= MB_MBAFF(h);
  762. nrefs[list]--;
  763. if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
  764. ff_thread_await_progress(&ref_pic->tf,
  765. FFMIN((row >> 1) - !(row & 1),
  766. pic_height - 1),
  767. 1);
  768. ff_thread_await_progress(&ref_pic->tf,
  769. FFMIN((row >> 1), pic_height - 1),
  770. 0);
  771. } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
  772. ff_thread_await_progress(&ref_pic->tf,
  773. FFMIN(row * 2 + ref_field,
  774. pic_height - 1),
  775. 0);
  776. } else if (FIELD_PICTURE(h)) {
  777. ff_thread_await_progress(&ref_pic->tf,
  778. FFMIN(row, pic_height - 1),
  779. ref_field);
  780. } else {
  781. ff_thread_await_progress(&ref_pic->tf,
  782. FFMIN(row, pic_height - 1),
  783. 0);
  784. }
  785. }
  786. }
  787. }
  788. static av_always_inline void mc_dir_part(H264Context *h, Picture *pic,
  789. int n, int square, int height,
  790. int delta, int list,
  791. uint8_t *dest_y, uint8_t *dest_cb,
  792. uint8_t *dest_cr,
  793. int src_x_offset, int src_y_offset,
  794. qpel_mc_func *qpix_op,
  795. h264_chroma_mc_func chroma_op,
  796. int pixel_shift, int chroma_idc)
  797. {
  798. const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
  799. int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
  800. const int luma_xy = (mx & 3) + ((my & 3) << 2);
  801. ptrdiff_t offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
  802. uint8_t *src_y = pic->f.data[0] + offset;
  803. uint8_t *src_cb, *src_cr;
  804. int extra_width = 0;
  805. int extra_height = 0;
  806. int emu = 0;
  807. const int full_mx = mx >> 2;
  808. const int full_my = my >> 2;
  809. const int pic_width = 16 * h->mb_width;
  810. const int pic_height = 16 * h->mb_height >> MB_FIELD(h);
  811. int ysh;
  812. if (mx & 7)
  813. extra_width -= 3;
  814. if (my & 7)
  815. extra_height -= 3;
  816. if (full_mx < 0 - extra_width ||
  817. full_my < 0 - extra_height ||
  818. full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
  819. full_my + 16 /*FIXME*/ > pic_height + extra_height) {
  820. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  821. src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
  822. h->mb_linesize, h->mb_linesize,
  823. 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
  824. full_my - 2, pic_width, pic_height);
  825. src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  826. emu = 1;
  827. }
  828. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
  829. if (!square)
  830. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  831. if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
  832. return;
  833. if (chroma_idc == 3 /* yuv444 */) {
  834. src_cb = pic->f.data[1] + offset;
  835. if (emu) {
  836. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  837. src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
  838. h->mb_linesize, h->mb_linesize,
  839. 16 + 5, 16 + 5 /*FIXME*/,
  840. full_mx - 2, full_my - 2,
  841. pic_width, pic_height);
  842. src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  843. }
  844. qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
  845. if (!square)
  846. qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
  847. src_cr = pic->f.data[2] + offset;
  848. if (emu) {
  849. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  850. src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
  851. h->mb_linesize, h->mb_linesize,
  852. 16 + 5, 16 + 5 /*FIXME*/,
  853. full_mx - 2, full_my - 2,
  854. pic_width, pic_height);
  855. src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  856. }
  857. qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
  858. if (!square)
  859. qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
  860. return;
  861. }
  862. ysh = 3 - (chroma_idc == 2 /* yuv422 */);
  863. if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) {
  864. // chroma offset when predicting from a field of opposite parity
  865. my += 2 * ((h->mb_y & 1) - (pic->reference - 1));
  866. emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
  867. }
  868. src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
  869. (my >> ysh) * h->mb_uvlinesize;
  870. src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
  871. (my >> ysh) * h->mb_uvlinesize;
  872. if (emu) {
  873. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb,
  874. h->mb_uvlinesize, h->mb_uvlinesize,
  875. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  876. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  877. src_cb = h->edge_emu_buffer;
  878. }
  879. chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
  880. height >> (chroma_idc == 1 /* yuv420 */),
  881. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  882. if (emu) {
  883. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr,
  884. h->mb_uvlinesize, h->mb_uvlinesize,
  885. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  886. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  887. src_cr = h->edge_emu_buffer;
  888. }
  889. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
  890. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  891. }
  892. static av_always_inline void mc_part_std(H264Context *h, int n, int square,
  893. int height, int delta,
  894. uint8_t *dest_y, uint8_t *dest_cb,
  895. uint8_t *dest_cr,
  896. int x_offset, int y_offset,
  897. qpel_mc_func *qpix_put,
  898. h264_chroma_mc_func chroma_put,
  899. qpel_mc_func *qpix_avg,
  900. h264_chroma_mc_func chroma_avg,
  901. int list0, int list1,
  902. int pixel_shift, int chroma_idc)
  903. {
  904. qpel_mc_func *qpix_op = qpix_put;
  905. h264_chroma_mc_func chroma_op = chroma_put;
  906. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  907. if (chroma_idc == 3 /* yuv444 */) {
  908. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  909. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  910. } else if (chroma_idc == 2 /* yuv422 */) {
  911. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  912. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  913. } else { /* yuv420 */
  914. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  915. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  916. }
  917. x_offset += 8 * h->mb_x;
  918. y_offset += 8 * (h->mb_y >> MB_FIELD(h));
  919. if (list0) {
  920. Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
  921. mc_dir_part(h, ref, n, square, height, delta, 0,
  922. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  923. qpix_op, chroma_op, pixel_shift, chroma_idc);
  924. qpix_op = qpix_avg;
  925. chroma_op = chroma_avg;
  926. }
  927. if (list1) {
  928. Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
  929. mc_dir_part(h, ref, n, square, height, delta, 1,
  930. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  931. qpix_op, chroma_op, pixel_shift, chroma_idc);
  932. }
  933. }
  934. static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
  935. int height, int delta,
  936. uint8_t *dest_y, uint8_t *dest_cb,
  937. uint8_t *dest_cr,
  938. int x_offset, int y_offset,
  939. qpel_mc_func *qpix_put,
  940. h264_chroma_mc_func chroma_put,
  941. h264_weight_func luma_weight_op,
  942. h264_weight_func chroma_weight_op,
  943. h264_biweight_func luma_weight_avg,
  944. h264_biweight_func chroma_weight_avg,
  945. int list0, int list1,
  946. int pixel_shift, int chroma_idc)
  947. {
  948. int chroma_height;
  949. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  950. if (chroma_idc == 3 /* yuv444 */) {
  951. chroma_height = height;
  952. chroma_weight_avg = luma_weight_avg;
  953. chroma_weight_op = luma_weight_op;
  954. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  955. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  956. } else if (chroma_idc == 2 /* yuv422 */) {
  957. chroma_height = height;
  958. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  959. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  960. } else { /* yuv420 */
  961. chroma_height = height >> 1;
  962. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  963. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  964. }
  965. x_offset += 8 * h->mb_x;
  966. y_offset += 8 * (h->mb_y >> MB_FIELD(h));
  967. if (list0 && list1) {
  968. /* don't optimize for luma-only case, since B-frames usually
  969. * use implicit weights => chroma too. */
  970. uint8_t *tmp_cb = h->bipred_scratchpad;
  971. uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
  972. uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
  973. int refn0 = h->ref_cache[0][scan8[n]];
  974. int refn1 = h->ref_cache[1][scan8[n]];
  975. mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
  976. dest_y, dest_cb, dest_cr,
  977. x_offset, y_offset, qpix_put, chroma_put,
  978. pixel_shift, chroma_idc);
  979. mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
  980. tmp_y, tmp_cb, tmp_cr,
  981. x_offset, y_offset, qpix_put, chroma_put,
  982. pixel_shift, chroma_idc);
  983. if (h->use_weight == 2) {
  984. int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1];
  985. int weight1 = 64 - weight0;
  986. luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
  987. height, 5, weight0, weight1, 0);
  988. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
  989. chroma_height, 5, weight0, weight1, 0);
  990. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
  991. chroma_height, 5, weight0, weight1, 0);
  992. } else {
  993. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
  994. h->luma_log2_weight_denom,
  995. h->luma_weight[refn0][0][0],
  996. h->luma_weight[refn1][1][0],
  997. h->luma_weight[refn0][0][1] +
  998. h->luma_weight[refn1][1][1]);
  999. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
  1000. h->chroma_log2_weight_denom,
  1001. h->chroma_weight[refn0][0][0][0],
  1002. h->chroma_weight[refn1][1][0][0],
  1003. h->chroma_weight[refn0][0][0][1] +
  1004. h->chroma_weight[refn1][1][0][1]);
  1005. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
  1006. h->chroma_log2_weight_denom,
  1007. h->chroma_weight[refn0][0][1][0],
  1008. h->chroma_weight[refn1][1][1][0],
  1009. h->chroma_weight[refn0][0][1][1] +
  1010. h->chroma_weight[refn1][1][1][1]);
  1011. }
  1012. } else {
  1013. int list = list1 ? 1 : 0;
  1014. int refn = h->ref_cache[list][scan8[n]];
  1015. Picture *ref = &h->ref_list[list][refn];
  1016. mc_dir_part(h, ref, n, square, height, delta, list,
  1017. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1018. qpix_put, chroma_put, pixel_shift, chroma_idc);
  1019. luma_weight_op(dest_y, h->mb_linesize, height,
  1020. h->luma_log2_weight_denom,
  1021. h->luma_weight[refn][list][0],
  1022. h->luma_weight[refn][list][1]);
  1023. if (h->use_weight_chroma) {
  1024. chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
  1025. h->chroma_log2_weight_denom,
  1026. h->chroma_weight[refn][list][0][0],
  1027. h->chroma_weight[refn][list][0][1]);
  1028. chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
  1029. h->chroma_log2_weight_denom,
  1030. h->chroma_weight[refn][list][1][0],
  1031. h->chroma_weight[refn][list][1][1]);
  1032. }
  1033. }
  1034. }
  1035. static av_always_inline void prefetch_motion(H264Context *h, int list,
  1036. int pixel_shift, int chroma_idc)
  1037. {
  1038. /* fetch pixels for estimated mv 4 macroblocks ahead
  1039. * optimized for 64byte cache lines */
  1040. const int refn = h->ref_cache[list][scan8[0]];
  1041. if (refn >= 0) {
  1042. const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8;
  1043. const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y;
  1044. uint8_t **src = h->ref_list[list][refn].f.data;
  1045. int off = (mx << pixel_shift) +
  1046. (my + (h->mb_x & 3) * 4) * h->mb_linesize +
  1047. (64 << pixel_shift);
  1048. h->vdsp.prefetch(src[0] + off, h->linesize, 4);
  1049. if (chroma_idc == 3 /* yuv444 */) {
  1050. h->vdsp.prefetch(src[1] + off, h->linesize, 4);
  1051. h->vdsp.prefetch(src[2] + off, h->linesize, 4);
  1052. } else {
  1053. off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (h->mb_x&7))*h->uvlinesize;
  1054. h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
  1055. }
  1056. }
  1057. }
  1058. static void free_tables(H264Context *h, int free_rbsp)
  1059. {
  1060. int i;
  1061. H264Context *hx;
  1062. av_freep(&h->intra4x4_pred_mode);
  1063. av_freep(&h->chroma_pred_mode_table);
  1064. av_freep(&h->cbp_table);
  1065. av_freep(&h->mvd_table[0]);
  1066. av_freep(&h->mvd_table[1]);
  1067. av_freep(&h->direct_table);
  1068. av_freep(&h->non_zero_count);
  1069. av_freep(&h->slice_table_base);
  1070. h->slice_table = NULL;
  1071. av_freep(&h->list_counts);
  1072. av_freep(&h->mb2b_xy);
  1073. av_freep(&h->mb2br_xy);
  1074. av_buffer_pool_uninit(&h->qscale_table_pool);
  1075. av_buffer_pool_uninit(&h->mb_type_pool);
  1076. av_buffer_pool_uninit(&h->motion_val_pool);
  1077. av_buffer_pool_uninit(&h->ref_index_pool);
  1078. if (free_rbsp && h->DPB) {
  1079. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  1080. unref_picture(h, &h->DPB[i]);
  1081. av_freep(&h->DPB);
  1082. } else if (h->DPB) {
  1083. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  1084. h->DPB[i].needs_realloc = 1;
  1085. }
  1086. h->cur_pic_ptr = NULL;
  1087. for (i = 0; i < MAX_THREADS; i++) {
  1088. hx = h->thread_context[i];
  1089. if (!hx)
  1090. continue;
  1091. av_freep(&hx->top_borders[1]);
  1092. av_freep(&hx->top_borders[0]);
  1093. av_freep(&hx->bipred_scratchpad);
  1094. av_freep(&hx->edge_emu_buffer);
  1095. av_freep(&hx->dc_val_base);
  1096. av_freep(&hx->me.scratchpad);
  1097. av_freep(&hx->er.mb_index2xy);
  1098. av_freep(&hx->er.error_status_table);
  1099. av_freep(&hx->er.er_temp_buffer);
  1100. av_freep(&hx->er.mbintra_table);
  1101. av_freep(&hx->er.mbskip_table);
  1102. if (free_rbsp) {
  1103. av_freep(&hx->rbsp_buffer[1]);
  1104. av_freep(&hx->rbsp_buffer[0]);
  1105. hx->rbsp_buffer_size[0] = 0;
  1106. hx->rbsp_buffer_size[1] = 0;
  1107. }
  1108. if (i)
  1109. av_freep(&h->thread_context[i]);
  1110. }
  1111. }
  1112. static void init_dequant8_coeff_table(H264Context *h)
  1113. {
  1114. int i, j, q, x;
  1115. const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
  1116. for (i = 0; i < 6; i++) {
  1117. h->dequant8_coeff[i] = h->dequant8_buffer[i];
  1118. for (j = 0; j < i; j++)
  1119. if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
  1120. 64 * sizeof(uint8_t))) {
  1121. h->dequant8_coeff[i] = h->dequant8_buffer[j];
  1122. break;
  1123. }
  1124. if (j < i)
  1125. continue;
  1126. for (q = 0; q < max_qp + 1; q++) {
  1127. int shift = div6[q];
  1128. int idx = rem6[q];
  1129. for (x = 0; x < 64; x++)
  1130. h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
  1131. ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
  1132. h->pps.scaling_matrix8[i][x]) << shift;
  1133. }
  1134. }
  1135. }
  1136. static void init_dequant4_coeff_table(H264Context *h)
  1137. {
  1138. int i, j, q, x;
  1139. const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
  1140. for (i = 0; i < 6; i++) {
  1141. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  1142. for (j = 0; j < i; j++)
  1143. if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
  1144. 16 * sizeof(uint8_t))) {
  1145. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  1146. break;
  1147. }
  1148. if (j < i)
  1149. continue;
  1150. for (q = 0; q < max_qp + 1; q++) {
  1151. int shift = div6[q] + 2;
  1152. int idx = rem6[q];
  1153. for (x = 0; x < 16; x++)
  1154. h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
  1155. ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
  1156. h->pps.scaling_matrix4[i][x]) << shift;
  1157. }
  1158. }
  1159. }
  1160. static void init_dequant_tables(H264Context *h)
  1161. {
  1162. int i, x;
  1163. init_dequant4_coeff_table(h);
  1164. if (h->pps.transform_8x8_mode)
  1165. init_dequant8_coeff_table(h);
  1166. if (h->sps.transform_bypass) {
  1167. for (i = 0; i < 6; i++)
  1168. for (x = 0; x < 16; x++)
  1169. h->dequant4_coeff[i][0][x] = 1 << 6;
  1170. if (h->pps.transform_8x8_mode)
  1171. for (i = 0; i < 6; i++)
  1172. for (x = 0; x < 64; x++)
  1173. h->dequant8_coeff[i][0][x] = 1 << 6;
  1174. }
  1175. }
  1176. int ff_h264_alloc_tables(H264Context *h)
  1177. {
  1178. const int big_mb_num = h->mb_stride * (h->mb_height + 1);
  1179. const int row_mb_num = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
  1180. int x, y, i;
  1181. FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
  1182. row_mb_num * 8 * sizeof(uint8_t), fail)
  1183. FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
  1184. big_mb_num * 48 * sizeof(uint8_t), fail)
  1185. FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
  1186. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
  1187. FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
  1188. big_mb_num * sizeof(uint16_t), fail)
  1189. FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
  1190. big_mb_num * sizeof(uint8_t), fail)
  1191. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
  1192. 16 * row_mb_num * sizeof(uint8_t), fail);
  1193. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
  1194. 16 * row_mb_num * sizeof(uint8_t), fail);
  1195. FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
  1196. 4 * big_mb_num * sizeof(uint8_t), fail);
  1197. FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
  1198. big_mb_num * sizeof(uint8_t), fail)
  1199. memset(h->slice_table_base, -1,
  1200. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
  1201. h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
  1202. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
  1203. big_mb_num * sizeof(uint32_t), fail);
  1204. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
  1205. big_mb_num * sizeof(uint32_t), fail);
  1206. for (y = 0; y < h->mb_height; y++)
  1207. for (x = 0; x < h->mb_width; x++) {
  1208. const int mb_xy = x + y * h->mb_stride;
  1209. const int b_xy = 4 * x + 4 * y * h->b_stride;
  1210. h->mb2b_xy[mb_xy] = b_xy;
  1211. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
  1212. }
  1213. if (!h->dequant4_coeff[0])
  1214. init_dequant_tables(h);
  1215. if (!h->DPB) {
  1216. h->DPB = av_mallocz_array(MAX_PICTURE_COUNT, sizeof(*h->DPB));
  1217. if (!h->DPB)
  1218. return AVERROR(ENOMEM);
  1219. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  1220. av_frame_unref(&h->DPB[i].f);
  1221. av_frame_unref(&h->cur_pic.f);
  1222. }
  1223. return 0;
  1224. fail:
  1225. free_tables(h, 1);
  1226. return AVERROR(ENOMEM);
  1227. }
  1228. /**
  1229. * Mimic alloc_tables(), but for every context thread.
  1230. */
  1231. static void clone_tables(H264Context *dst, H264Context *src, int i)
  1232. {
  1233. dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * src->mb_stride;
  1234. dst->non_zero_count = src->non_zero_count;
  1235. dst->slice_table = src->slice_table;
  1236. dst->cbp_table = src->cbp_table;
  1237. dst->mb2b_xy = src->mb2b_xy;
  1238. dst->mb2br_xy = src->mb2br_xy;
  1239. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  1240. dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * src->mb_stride;
  1241. dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * src->mb_stride;
  1242. dst->direct_table = src->direct_table;
  1243. dst->list_counts = src->list_counts;
  1244. dst->DPB = src->DPB;
  1245. dst->cur_pic_ptr = src->cur_pic_ptr;
  1246. dst->cur_pic = src->cur_pic;
  1247. dst->bipred_scratchpad = NULL;
  1248. dst->edge_emu_buffer = NULL;
  1249. dst->me.scratchpad = NULL;
  1250. ff_h264_pred_init(&dst->hpc, src->avctx->codec_id, src->sps.bit_depth_luma,
  1251. src->sps.chroma_format_idc);
  1252. }
  1253. /**
  1254. * Init context
  1255. * Allocate buffers which are not shared amongst multiple threads.
  1256. */
  1257. static int context_init(H264Context *h)
  1258. {
  1259. ERContext *er = &h->er;
  1260. int mb_array_size = h->mb_height * h->mb_stride;
  1261. int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
  1262. int c_size = h->mb_stride * (h->mb_height + 1);
  1263. int yc_size = y_size + 2 * c_size;
  1264. int x, y, i;
  1265. FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[0],
  1266. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
  1267. FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[1],
  1268. h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
  1269. h->ref_cache[0][scan8[5] + 1] =
  1270. h->ref_cache[0][scan8[7] + 1] =
  1271. h->ref_cache[0][scan8[13] + 1] =
  1272. h->ref_cache[1][scan8[5] + 1] =
  1273. h->ref_cache[1][scan8[7] + 1] =
  1274. h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  1275. if (CONFIG_ERROR_RESILIENCE) {
  1276. /* init ER */
  1277. er->avctx = h->avctx;
  1278. er->dsp = &h->dsp;
  1279. er->decode_mb = h264_er_decode_mb;
  1280. er->opaque = h;
  1281. er->quarter_sample = 1;
  1282. er->mb_num = h->mb_num;
  1283. er->mb_width = h->mb_width;
  1284. er->mb_height = h->mb_height;
  1285. er->mb_stride = h->mb_stride;
  1286. er->b8_stride = h->mb_width * 2 + 1;
  1287. FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
  1288. fail); // error ressilience code looks cleaner with this
  1289. for (y = 0; y < h->mb_height; y++)
  1290. for (x = 0; x < h->mb_width; x++)
  1291. er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
  1292. er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
  1293. h->mb_stride + h->mb_width;
  1294. FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
  1295. mb_array_size * sizeof(uint8_t), fail);
  1296. FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
  1297. memset(er->mbintra_table, 1, mb_array_size);
  1298. FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
  1299. FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride,
  1300. fail);
  1301. FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
  1302. er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
  1303. er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
  1304. er->dc_val[2] = er->dc_val[1] + c_size;
  1305. for (i = 0; i < yc_size; i++)
  1306. h->dc_val_base[i] = 1024;
  1307. }
  1308. return 0;
  1309. fail:
  1310. return AVERROR(ENOMEM); // free_tables will clean up for us
  1311. }
  1312. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  1313. int parse_extradata);
  1314. int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
  1315. {
  1316. AVCodecContext *avctx = h->avctx;
  1317. int ret;
  1318. if (!buf || size <= 0)
  1319. return -1;
  1320. if (buf[0] == 1) {
  1321. int i, cnt, nalsize;
  1322. const unsigned char *p = buf;
  1323. h->is_avc = 1;
  1324. if (size < 7) {
  1325. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  1326. return AVERROR_INVALIDDATA;
  1327. }
  1328. /* sps and pps in the avcC always have length coded with 2 bytes,
  1329. * so put a fake nal_length_size = 2 while parsing them */
  1330. h->nal_length_size = 2;
  1331. // Decode sps from avcC
  1332. cnt = *(p + 5) & 0x1f; // Number of sps
  1333. p += 6;
  1334. for (i = 0; i < cnt; i++) {
  1335. nalsize = AV_RB16(p) + 2;
  1336. if(nalsize > size - (p-buf))
  1337. return AVERROR_INVALIDDATA;
  1338. ret = decode_nal_units(h, p, nalsize, 1);
  1339. if (ret < 0) {
  1340. av_log(avctx, AV_LOG_ERROR,
  1341. "Decoding sps %d from avcC failed\n", i);
  1342. return ret;
  1343. }
  1344. p += nalsize;
  1345. }
  1346. // Decode pps from avcC
  1347. cnt = *(p++); // Number of pps
  1348. for (i = 0; i < cnt; i++) {
  1349. nalsize = AV_RB16(p) + 2;
  1350. if(nalsize > size - (p-buf))
  1351. return AVERROR_INVALIDDATA;
  1352. ret = decode_nal_units(h, p, nalsize, 1);
  1353. if (ret < 0) {
  1354. av_log(avctx, AV_LOG_ERROR,
  1355. "Decoding pps %d from avcC failed\n", i);
  1356. return ret;
  1357. }
  1358. p += nalsize;
  1359. }
  1360. // Now store right nal length size, that will be used to parse all other nals
  1361. h->nal_length_size = (buf[4] & 0x03) + 1;
  1362. } else {
  1363. h->is_avc = 0;
  1364. ret = decode_nal_units(h, buf, size, 1);
  1365. if (ret < 0)
  1366. return ret;
  1367. }
  1368. return size;
  1369. }
  1370. av_cold int ff_h264_decode_init(AVCodecContext *avctx)
  1371. {
  1372. H264Context *h = avctx->priv_data;
  1373. int i;
  1374. int ret;
  1375. h->avctx = avctx;
  1376. h->bit_depth_luma = 8;
  1377. h->chroma_format_idc = 1;
  1378. h->avctx->bits_per_raw_sample = 8;
  1379. h->cur_chroma_format_idc = 1;
  1380. ff_h264dsp_init(&h->h264dsp, 8, 1);
  1381. av_assert0(h->sps.bit_depth_chroma == 0);
  1382. ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
  1383. ff_h264qpel_init(&h->h264qpel, 8);
  1384. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
  1385. h->dequant_coeff_pps = -1;
  1386. h->current_sps_id = -1;
  1387. /* needed so that IDCT permutation is known early */
  1388. if (CONFIG_ERROR_RESILIENCE)
  1389. ff_dsputil_init(&h->dsp, h->avctx);
  1390. ff_videodsp_init(&h->vdsp, 8);
  1391. memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
  1392. memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
  1393. h->picture_structure = PICT_FRAME;
  1394. h->slice_context_count = 1;
  1395. h->workaround_bugs = avctx->workaround_bugs;
  1396. h->flags = avctx->flags;
  1397. /* set defaults */
  1398. // s->decode_mb = ff_h263_decode_mb;
  1399. if (!avctx->has_b_frames)
  1400. h->low_delay = 1;
  1401. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1402. ff_h264_decode_init_vlc();
  1403. ff_init_cabac_states();
  1404. h->pixel_shift = 0;
  1405. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  1406. h->thread_context[0] = h;
  1407. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  1408. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  1409. h->last_pocs[i] = INT_MIN;
  1410. h->prev_poc_msb = 1 << 16;
  1411. h->prev_frame_num = -1;
  1412. h->x264_build = -1;
  1413. h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
  1414. ff_h264_reset_sei(h);
  1415. if (avctx->codec_id == AV_CODEC_ID_H264) {
  1416. if (avctx->ticks_per_frame == 1) {
  1417. if(h->avctx->time_base.den < INT_MAX/2) {
  1418. h->avctx->time_base.den *= 2;
  1419. } else
  1420. h->avctx->time_base.num /= 2;
  1421. }
  1422. avctx->ticks_per_frame = 2;
  1423. }
  1424. if (avctx->extradata_size > 0 && avctx->extradata) {
  1425. ret = ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
  1426. if (ret < 0) {
  1427. ff_h264_free_context(h);
  1428. return ret;
  1429. }
  1430. }
  1431. if (h->sps.bitstream_restriction_flag &&
  1432. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  1433. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  1434. h->low_delay = 0;
  1435. }
  1436. avctx->internal->allocate_progress = 1;
  1437. flush_change(h);
  1438. return 0;
  1439. }
  1440. #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
  1441. #undef REBASE_PICTURE
  1442. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  1443. ((pic && pic >= old_ctx->DPB && \
  1444. pic < old_ctx->DPB + MAX_PICTURE_COUNT) ? \
  1445. &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
  1446. static void copy_picture_range(Picture **to, Picture **from, int count,
  1447. H264Context *new_base,
  1448. H264Context *old_base)
  1449. {
  1450. int i;
  1451. for (i = 0; i < count; i++) {
  1452. assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
  1453. IN_RANGE(from[i], old_base->DPB,
  1454. sizeof(Picture) * MAX_PICTURE_COUNT) ||
  1455. !from[i]));
  1456. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  1457. }
  1458. }
  1459. static int copy_parameter_set(void **to, void **from, int count, int size)
  1460. {
  1461. int i;
  1462. for (i = 0; i < count; i++) {
  1463. if (to[i] && !from[i]) {
  1464. av_freep(&to[i]);
  1465. } else if (from[i] && !to[i]) {
  1466. to[i] = av_malloc(size);
  1467. if (!to[i])
  1468. return AVERROR(ENOMEM);
  1469. }
  1470. if (from[i])
  1471. memcpy(to[i], from[i], size);
  1472. }
  1473. return 0;
  1474. }
  1475. static int decode_init_thread_copy(AVCodecContext *avctx)
  1476. {
  1477. H264Context *h = avctx->priv_data;
  1478. if (!avctx->internal->is_copy)
  1479. return 0;
  1480. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1481. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1482. h->rbsp_buffer[0] = NULL;
  1483. h->rbsp_buffer[1] = NULL;
  1484. h->rbsp_buffer_size[0] = 0;
  1485. h->rbsp_buffer_size[1] = 0;
  1486. h->context_initialized = 0;
  1487. return 0;
  1488. }
  1489. #define copy_fields(to, from, start_field, end_field) \
  1490. memcpy(&to->start_field, &from->start_field, \
  1491. (char *)&to->end_field - (char *)&to->start_field)
  1492. static int h264_slice_header_init(H264Context *, int);
  1493. static int h264_set_parameter_from_sps(H264Context *h);
  1494. static int decode_update_thread_context(AVCodecContext *dst,
  1495. const AVCodecContext *src)
  1496. {
  1497. H264Context *h = dst->priv_data, *h1 = src->priv_data;
  1498. int inited = h->context_initialized, err = 0;
  1499. int context_reinitialized = 0;
  1500. int i, ret;
  1501. if (dst == src)
  1502. return 0;
  1503. if (inited &&
  1504. (h->width != h1->width ||
  1505. h->height != h1->height ||
  1506. h->mb_width != h1->mb_width ||
  1507. h->mb_height != h1->mb_height ||
  1508. h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
  1509. h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
  1510. h->sps.colorspace != h1->sps.colorspace)) {
  1511. /* set bits_per_raw_sample to the previous value. the check for changed
  1512. * bit depth in h264_set_parameter_from_sps() uses it and sets it to
  1513. * the current value */
  1514. h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  1515. av_freep(&h->bipred_scratchpad);
  1516. h->width = h1->width;
  1517. h->height = h1->height;
  1518. h->mb_height = h1->mb_height;
  1519. h->mb_width = h1->mb_width;
  1520. h->mb_num = h1->mb_num;
  1521. h->mb_stride = h1->mb_stride;
  1522. h->b_stride = h1->b_stride;
  1523. // SPS/PPS
  1524. if ((ret = copy_parameter_set((void **)h->sps_buffers,
  1525. (void **)h1->sps_buffers,
  1526. MAX_SPS_COUNT, sizeof(SPS))) < 0)
  1527. return ret;
  1528. h->sps = h1->sps;
  1529. if ((ret = copy_parameter_set((void **)h->pps_buffers,
  1530. (void **)h1->pps_buffers,
  1531. MAX_PPS_COUNT, sizeof(PPS))) < 0)
  1532. return ret;
  1533. h->pps = h1->pps;
  1534. if ((err = h264_slice_header_init(h, 1)) < 0) {
  1535. av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
  1536. return err;
  1537. }
  1538. context_reinitialized = 1;
  1539. #if 0
  1540. h264_set_parameter_from_sps(h);
  1541. //Note we set context_reinitialized which will cause h264_set_parameter_from_sps to be reexecuted
  1542. h->cur_chroma_format_idc = h1->cur_chroma_format_idc;
  1543. #endif
  1544. }
  1545. /* update linesize on resize for h264. The h264 decoder doesn't
  1546. * necessarily call ff_MPV_frame_start in the new thread */
  1547. h->linesize = h1->linesize;
  1548. h->uvlinesize = h1->uvlinesize;
  1549. /* copy block_offset since frame_start may not be called */
  1550. memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
  1551. if (!inited) {
  1552. for (i = 0; i < MAX_SPS_COUNT; i++)
  1553. av_freep(h->sps_buffers + i);
  1554. for (i = 0; i < MAX_PPS_COUNT; i++)
  1555. av_freep(h->pps_buffers + i);
  1556. av_freep(&h->rbsp_buffer[0]);
  1557. av_freep(&h->rbsp_buffer[1]);
  1558. memcpy(h, h1, offsetof(H264Context, intra_pcm_ptr));
  1559. memcpy(&h->cabac, &h1->cabac,
  1560. sizeof(H264Context) - offsetof(H264Context, cabac));
  1561. av_assert0((void*)&h->cabac == &h->mb_padding + 1);
  1562. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1563. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1564. memset(&h->er, 0, sizeof(h->er));
  1565. memset(&h->me, 0, sizeof(h->me));
  1566. memset(&h->mb, 0, sizeof(h->mb));
  1567. memset(&h->mb_luma_dc, 0, sizeof(h->mb_luma_dc));
  1568. memset(&h->mb_padding, 0, sizeof(h->mb_padding));
  1569. h->avctx = dst;
  1570. h->DPB = NULL;
  1571. h->qscale_table_pool = NULL;
  1572. h->mb_type_pool = NULL;
  1573. h->ref_index_pool = NULL;
  1574. h->motion_val_pool = NULL;
  1575. for (i = 0; i < 2; i++) {
  1576. h->rbsp_buffer[i] = NULL;
  1577. h->rbsp_buffer_size[i] = 0;
  1578. }
  1579. if (h1->context_initialized) {
  1580. h->context_initialized = 0;
  1581. memset(&h->cur_pic, 0, sizeof(h->cur_pic));
  1582. av_frame_unref(&h->cur_pic.f);
  1583. h->cur_pic.tf.f = &h->cur_pic.f;
  1584. ret = ff_h264_alloc_tables(h);
  1585. if (ret < 0) {
  1586. av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
  1587. return ret;
  1588. }
  1589. ret = context_init(h);
  1590. if (ret < 0) {
  1591. av_log(dst, AV_LOG_ERROR, "context_init() failed.\n");
  1592. return ret;
  1593. }
  1594. }
  1595. h->bipred_scratchpad = NULL;
  1596. h->edge_emu_buffer = NULL;
  1597. h->thread_context[0] = h;
  1598. h->context_initialized = h1->context_initialized;
  1599. }
  1600. h->avctx->coded_height = h1->avctx->coded_height;
  1601. h->avctx->coded_width = h1->avctx->coded_width;
  1602. h->avctx->width = h1->avctx->width;
  1603. h->avctx->height = h1->avctx->height;
  1604. h->coded_picture_number = h1->coded_picture_number;
  1605. h->first_field = h1->first_field;
  1606. h->picture_structure = h1->picture_structure;
  1607. h->qscale = h1->qscale;
  1608. h->droppable = h1->droppable;
  1609. h->data_partitioning = h1->data_partitioning;
  1610. h->low_delay = h1->low_delay;
  1611. for (i = 0; h->DPB && i < MAX_PICTURE_COUNT; i++) {
  1612. unref_picture(h, &h->DPB[i]);
  1613. if (h1->DPB && h1->DPB[i].f.buf[0] &&
  1614. (ret = ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
  1615. return ret;
  1616. }
  1617. h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
  1618. unref_picture(h, &h->cur_pic);
  1619. if (h1->cur_pic.f.buf[0] && (ret = ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0)
  1620. return ret;
  1621. h->workaround_bugs = h1->workaround_bugs;
  1622. h->low_delay = h1->low_delay;
  1623. h->droppable = h1->droppable;
  1624. // extradata/NAL handling
  1625. h->is_avc = h1->is_avc;
  1626. // SPS/PPS
  1627. if ((ret = copy_parameter_set((void **)h->sps_buffers,
  1628. (void **)h1->sps_buffers,
  1629. MAX_SPS_COUNT, sizeof(SPS))) < 0)
  1630. return ret;
  1631. h->sps = h1->sps;
  1632. if ((ret = copy_parameter_set((void **)h->pps_buffers,
  1633. (void **)h1->pps_buffers,
  1634. MAX_PPS_COUNT, sizeof(PPS))) < 0)
  1635. return ret;
  1636. h->pps = h1->pps;
  1637. // Dequantization matrices
  1638. // FIXME these are big - can they be only copied when PPS changes?
  1639. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  1640. for (i = 0; i < 6; i++)
  1641. h->dequant4_coeff[i] = h->dequant4_buffer[0] +
  1642. (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  1643. for (i = 0; i < 6; i++)
  1644. h->dequant8_coeff[i] = h->dequant8_buffer[0] +
  1645. (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  1646. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  1647. // POC timing
  1648. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  1649. // reference lists
  1650. copy_fields(h, h1, short_ref, cabac_init_idc);
  1651. copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
  1652. copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
  1653. copy_picture_range(h->delayed_pic, h1->delayed_pic,
  1654. MAX_DELAYED_PIC_COUNT + 2, h, h1);
  1655. h->frame_recovered = h1->frame_recovered;
  1656. if (context_reinitialized)
  1657. h264_set_parameter_from_sps(h);
  1658. if (!h->cur_pic_ptr)
  1659. return 0;
  1660. if (!h->droppable) {
  1661. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1662. h->prev_poc_msb = h->poc_msb;
  1663. h->prev_poc_lsb = h->poc_lsb;
  1664. }
  1665. h->prev_frame_num_offset = h->frame_num_offset;
  1666. h->prev_frame_num = h->frame_num;
  1667. h->outputed_poc = h->next_outputed_poc;
  1668. h->recovery_frame = h1->recovery_frame;
  1669. return err;
  1670. }
  1671. static int h264_frame_start(H264Context *h)
  1672. {
  1673. Picture *pic;
  1674. int i, ret;
  1675. const int pixel_shift = h->pixel_shift;
  1676. int c[4] = {
  1677. 1<<(h->sps.bit_depth_luma-1),
  1678. 1<<(h->sps.bit_depth_chroma-1),
  1679. 1<<(h->sps.bit_depth_chroma-1),
  1680. -1
  1681. };
  1682. if (!ff_thread_can_start_frame(h->avctx)) {
  1683. av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1684. return -1;
  1685. }
  1686. release_unused_pictures(h, 1);
  1687. h->cur_pic_ptr = NULL;
  1688. i = find_unused_picture(h);
  1689. if (i < 0) {
  1690. av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1691. return i;
  1692. }
  1693. pic = &h->DPB[i];
  1694. pic->reference = h->droppable ? 0 : h->picture_structure;
  1695. pic->f.coded_picture_number = h->coded_picture_number++;
  1696. pic->field_picture = h->picture_structure != PICT_FRAME;
  1697. /*
  1698. * Zero key_frame here; IDR markings per slice in frame or fields are ORed
  1699. * in later.
  1700. * See decode_nal_units().
  1701. */
  1702. pic->f.key_frame = 0;
  1703. pic->mmco_reset = 0;
  1704. pic->recovered = 0;
  1705. if ((ret = alloc_picture(h, pic)) < 0)
  1706. return ret;
  1707. if(!h->frame_recovered && !h->avctx->hwaccel &&
  1708. !(h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU))
  1709. avpriv_color_frame(&pic->f, c);
  1710. h->cur_pic_ptr = pic;
  1711. unref_picture(h, &h->cur_pic);
  1712. if (CONFIG_ERROR_RESILIENCE) {
  1713. h->er.cur_pic = NULL;
  1714. }
  1715. if ((ret = ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
  1716. return ret;
  1717. if (CONFIG_ERROR_RESILIENCE) {
  1718. ff_er_frame_start(&h->er);
  1719. h->er.last_pic =
  1720. h->er.next_pic = NULL;
  1721. }
  1722. assert(h->linesize && h->uvlinesize);
  1723. for (i = 0; i < 16; i++) {
  1724. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  1725. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  1726. }
  1727. for (i = 0; i < 16; i++) {
  1728. h->block_offset[16 + i] =
  1729. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1730. h->block_offset[48 + 16 + i] =
  1731. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1732. }
  1733. // s->decode = (h->flags & CODEC_FLAG_PSNR) || !s->encoding ||
  1734. // h->cur_pic.reference /* || h->contains_intra */ || 1;
  1735. /* We mark the current picture as non-reference after allocating it, so
  1736. * that if we break out due to an error it can be released automatically
  1737. * in the next ff_MPV_frame_start().
  1738. */
  1739. h->cur_pic_ptr->reference = 0;
  1740. h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
  1741. h->next_output_pic = NULL;
  1742. assert(h->cur_pic_ptr->long_ref == 0);
  1743. return 0;
  1744. }
  1745. /**
  1746. * Run setup operations that must be run after slice header decoding.
  1747. * This includes finding the next displayed frame.
  1748. *
  1749. * @param h h264 master context
  1750. * @param setup_finished enough NALs have been read that we can call
  1751. * ff_thread_finish_setup()
  1752. */
  1753. static void decode_postinit(H264Context *h, int setup_finished)
  1754. {
  1755. Picture *out = h->cur_pic_ptr;
  1756. Picture *cur = h->cur_pic_ptr;
  1757. int i, pics, out_of_order, out_idx;
  1758. h->cur_pic_ptr->f.pict_type = h->pict_type;
  1759. if (h->next_output_pic)
  1760. return;
  1761. if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
  1762. /* FIXME: if we have two PAFF fields in one packet, we can't start
  1763. * the next thread here. If we have one field per packet, we can.
  1764. * The check in decode_nal_units() is not good enough to find this
  1765. * yet, so we assume the worst for now. */
  1766. // if (setup_finished)
  1767. // ff_thread_finish_setup(h->avctx);
  1768. return;
  1769. }
  1770. cur->f.interlaced_frame = 0;
  1771. cur->f.repeat_pict = 0;
  1772. /* Signal interlacing information externally. */
  1773. /* Prioritize picture timing SEI information over used
  1774. * decoding process if it exists. */
  1775. if (h->sps.pic_struct_present_flag) {
  1776. switch (h->sei_pic_struct) {
  1777. case SEI_PIC_STRUCT_FRAME:
  1778. break;
  1779. case SEI_PIC_STRUCT_TOP_FIELD:
  1780. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  1781. cur->f.interlaced_frame = 1;
  1782. break;
  1783. case SEI_PIC_STRUCT_TOP_BOTTOM:
  1784. case SEI_PIC_STRUCT_BOTTOM_TOP:
  1785. if (FIELD_OR_MBAFF_PICTURE(h))
  1786. cur->f.interlaced_frame = 1;
  1787. else
  1788. // try to flag soft telecine progressive
  1789. cur->f.interlaced_frame = h->prev_interlaced_frame;
  1790. break;
  1791. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  1792. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  1793. /* Signal the possibility of telecined film externally
  1794. * (pic_struct 5,6). From these hints, let the applications
  1795. * decide if they apply deinterlacing. */
  1796. cur->f.repeat_pict = 1;
  1797. break;
  1798. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  1799. cur->f.repeat_pict = 2;
  1800. break;
  1801. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  1802. cur->f.repeat_pict = 4;
  1803. break;
  1804. }
  1805. if ((h->sei_ct_type & 3) &&
  1806. h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  1807. cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
  1808. } else {
  1809. /* Derive interlacing flag from used decoding process. */
  1810. cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
  1811. }
  1812. h->prev_interlaced_frame = cur->f.interlaced_frame;
  1813. if (cur->field_poc[0] != cur->field_poc[1]) {
  1814. /* Derive top_field_first from field pocs. */
  1815. cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
  1816. } else {
  1817. if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
  1818. /* Use picture timing SEI information. Even if it is a
  1819. * information of a past frame, better than nothing. */
  1820. if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
  1821. h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  1822. cur->f.top_field_first = 1;
  1823. else
  1824. cur->f.top_field_first = 0;
  1825. } else {
  1826. /* Most likely progressive */
  1827. cur->f.top_field_first = 0;
  1828. }
  1829. }
  1830. if (h->sei_frame_packing_present &&
  1831. h->frame_packing_arrangement_type >= 0 &&
  1832. h->frame_packing_arrangement_type <= 6 &&
  1833. h->content_interpretation_type > 0 &&
  1834. h->content_interpretation_type < 3) {
  1835. AVStereo3D *stereo = av_stereo3d_create_side_data(&cur->f);
  1836. if (!stereo)
  1837. return;
  1838. switch (h->frame_packing_arrangement_type) {
  1839. case 0:
  1840. stereo->type = AV_STEREO3D_CHECKERBOARD;
  1841. break;
  1842. case 1:
  1843. stereo->type = AV_STEREO3D_LINES;
  1844. break;
  1845. case 2:
  1846. stereo->type = AV_STEREO3D_COLUMNS;
  1847. break;
  1848. case 3:
  1849. if (h->quincunx_subsampling)
  1850. stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
  1851. else
  1852. stereo->type = AV_STEREO3D_SIDEBYSIDE;
  1853. break;
  1854. case 4:
  1855. stereo->type = AV_STEREO3D_TOPBOTTOM;
  1856. break;
  1857. case 5:
  1858. stereo->type = AV_STEREO3D_FRAMESEQUENCE;
  1859. break;
  1860. case 6:
  1861. stereo->type = AV_STEREO3D_2D;
  1862. break;
  1863. }
  1864. if (h->content_interpretation_type == 2)
  1865. stereo->flags = AV_STEREO3D_FLAG_INVERT;
  1866. }
  1867. cur->mmco_reset = h->mmco_reset;
  1868. h->mmco_reset = 0;
  1869. // FIXME do something with unavailable reference frames
  1870. /* Sort B-frames into display order */
  1871. if (h->sps.bitstream_restriction_flag &&
  1872. h->avctx->has_b_frames < h->sps.num_reorder_frames) {
  1873. h->avctx->has_b_frames = h->sps.num_reorder_frames;
  1874. h->low_delay = 0;
  1875. }
  1876. if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
  1877. !h->sps.bitstream_restriction_flag) {
  1878. h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
  1879. h->low_delay = 0;
  1880. }
  1881. for (i = 0; 1; i++) {
  1882. if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
  1883. if(i)
  1884. h->last_pocs[i-1] = cur->poc;
  1885. break;
  1886. } else if(i) {
  1887. h->last_pocs[i-1]= h->last_pocs[i];
  1888. }
  1889. }
  1890. out_of_order = MAX_DELAYED_PIC_COUNT - i;
  1891. if( cur->f.pict_type == AV_PICTURE_TYPE_B
  1892. || (h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > INT_MIN && h->last_pocs[MAX_DELAYED_PIC_COUNT-1] - h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > 2))
  1893. out_of_order = FFMAX(out_of_order, 1);
  1894. if (out_of_order == MAX_DELAYED_PIC_COUNT) {
  1895. av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
  1896. for (i = 1; i < MAX_DELAYED_PIC_COUNT; i++)
  1897. h->last_pocs[i] = INT_MIN;
  1898. h->last_pocs[0] = cur->poc;
  1899. cur->mmco_reset = 1;
  1900. } else if(h->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
  1901. av_log(h->avctx, AV_LOG_VERBOSE, "Increasing reorder buffer to %d\n", out_of_order);
  1902. h->avctx->has_b_frames = out_of_order;
  1903. h->low_delay = 0;
  1904. }
  1905. pics = 0;
  1906. while (h->delayed_pic[pics])
  1907. pics++;
  1908. av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
  1909. h->delayed_pic[pics++] = cur;
  1910. if (cur->reference == 0)
  1911. cur->reference = DELAYED_PIC_REF;
  1912. out = h->delayed_pic[0];
  1913. out_idx = 0;
  1914. for (i = 1; h->delayed_pic[i] &&
  1915. !h->delayed_pic[i]->f.key_frame &&
  1916. !h->delayed_pic[i]->mmco_reset;
  1917. i++)
  1918. if (h->delayed_pic[i]->poc < out->poc) {
  1919. out = h->delayed_pic[i];
  1920. out_idx = i;
  1921. }
  1922. if (h->avctx->has_b_frames == 0 &&
  1923. (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
  1924. h->next_outputed_poc = INT_MIN;
  1925. out_of_order = out->poc < h->next_outputed_poc;
  1926. if (out_of_order || pics > h->avctx->has_b_frames) {
  1927. out->reference &= ~DELAYED_PIC_REF;
  1928. // for frame threading, the owner must be the second field's thread or
  1929. // else the first thread can release the picture and reuse it unsafely
  1930. for (i = out_idx; h->delayed_pic[i]; i++)
  1931. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1932. }
  1933. if (!out_of_order && pics > h->avctx->has_b_frames) {
  1934. h->next_output_pic = out;
  1935. if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
  1936. h->next_outputed_poc = INT_MIN;
  1937. } else
  1938. h->next_outputed_poc = out->poc;
  1939. } else {
  1940. av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
  1941. }
  1942. if (h->next_output_pic) {
  1943. if (h->next_output_pic->recovered) {
  1944. // We have reached an recovery point and all frames after it in
  1945. // display order are "recovered".
  1946. h->frame_recovered |= FRAME_RECOVERED_SEI;
  1947. }
  1948. h->next_output_pic->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
  1949. }
  1950. if (setup_finished && !h->avctx->hwaccel)
  1951. ff_thread_finish_setup(h->avctx);
  1952. }
  1953. static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
  1954. uint8_t *src_cb, uint8_t *src_cr,
  1955. int linesize, int uvlinesize,
  1956. int simple)
  1957. {
  1958. uint8_t *top_border;
  1959. int top_idx = 1;
  1960. const int pixel_shift = h->pixel_shift;
  1961. int chroma444 = CHROMA444(h);
  1962. int chroma422 = CHROMA422(h);
  1963. src_y -= linesize;
  1964. src_cb -= uvlinesize;
  1965. src_cr -= uvlinesize;
  1966. if (!simple && FRAME_MBAFF(h)) {
  1967. if (h->mb_y & 1) {
  1968. if (!MB_MBAFF(h)) {
  1969. top_border = h->top_borders[0][h->mb_x];
  1970. AV_COPY128(top_border, src_y + 15 * linesize);
  1971. if (pixel_shift)
  1972. AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
  1973. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  1974. if (chroma444) {
  1975. if (pixel_shift) {
  1976. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1977. AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
  1978. AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
  1979. AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
  1980. } else {
  1981. AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
  1982. AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
  1983. }
  1984. } else if (chroma422) {
  1985. if (pixel_shift) {
  1986. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1987. AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
  1988. } else {
  1989. AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
  1990. AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
  1991. }
  1992. } else {
  1993. if (pixel_shift) {
  1994. AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
  1995. AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
  1996. } else {
  1997. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  1998. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  1999. }
  2000. }
  2001. }
  2002. }
  2003. } else if (MB_MBAFF(h)) {
  2004. top_idx = 0;
  2005. } else
  2006. return;
  2007. }
  2008. top_border = h->top_borders[top_idx][h->mb_x];
  2009. /* There are two lines saved, the line above the top macroblock
  2010. * of a pair, and the line above the bottom macroblock. */
  2011. AV_COPY128(top_border, src_y + 16 * linesize);
  2012. if (pixel_shift)
  2013. AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
  2014. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  2015. if (chroma444) {
  2016. if (pixel_shift) {
  2017. AV_COPY128(top_border + 32, src_cb + 16 * linesize);
  2018. AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
  2019. AV_COPY128(top_border + 64, src_cr + 16 * linesize);
  2020. AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
  2021. } else {
  2022. AV_COPY128(top_border + 16, src_cb + 16 * linesize);
  2023. AV_COPY128(top_border + 32, src_cr + 16 * linesize);
  2024. }
  2025. } else if (chroma422) {
  2026. if (pixel_shift) {
  2027. AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
  2028. AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
  2029. } else {
  2030. AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
  2031. AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
  2032. }
  2033. } else {
  2034. if (pixel_shift) {
  2035. AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
  2036. AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
  2037. } else {
  2038. AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
  2039. AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
  2040. }
  2041. }
  2042. }
  2043. }
  2044. static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  2045. uint8_t *src_cb, uint8_t *src_cr,
  2046. int linesize, int uvlinesize,
  2047. int xchg, int chroma444,
  2048. int simple, int pixel_shift)
  2049. {
  2050. int deblock_topleft;
  2051. int deblock_top;
  2052. int top_idx = 1;
  2053. uint8_t *top_border_m1;
  2054. uint8_t *top_border;
  2055. if (!simple && FRAME_MBAFF(h)) {
  2056. if (h->mb_y & 1) {
  2057. if (!MB_MBAFF(h))
  2058. return;
  2059. } else {
  2060. top_idx = MB_MBAFF(h) ? 0 : 1;
  2061. }
  2062. }
  2063. if (h->deblocking_filter == 2) {
  2064. deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
  2065. deblock_top = h->top_type;
  2066. } else {
  2067. deblock_topleft = (h->mb_x > 0);
  2068. deblock_top = (h->mb_y > !!MB_FIELD(h));
  2069. }
  2070. src_y -= linesize + 1 + pixel_shift;
  2071. src_cb -= uvlinesize + 1 + pixel_shift;
  2072. src_cr -= uvlinesize + 1 + pixel_shift;
  2073. top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
  2074. top_border = h->top_borders[top_idx][h->mb_x];
  2075. #define XCHG(a, b, xchg) \
  2076. if (pixel_shift) { \
  2077. if (xchg) { \
  2078. AV_SWAP64(b + 0, a + 0); \
  2079. AV_SWAP64(b + 8, a + 8); \
  2080. } else { \
  2081. AV_COPY128(b, a); \
  2082. } \
  2083. } else if (xchg) \
  2084. AV_SWAP64(b, a); \
  2085. else \
  2086. AV_COPY64(b, a);
  2087. if (deblock_top) {
  2088. if (deblock_topleft) {
  2089. XCHG(top_border_m1 + (8 << pixel_shift),
  2090. src_y - (7 << pixel_shift), 1);
  2091. }
  2092. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  2093. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  2094. if (h->mb_x + 1 < h->mb_width) {
  2095. XCHG(h->top_borders[top_idx][h->mb_x + 1],
  2096. src_y + (17 << pixel_shift), 1);
  2097. }
  2098. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  2099. if (chroma444) {
  2100. if (deblock_topleft) {
  2101. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  2102. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  2103. }
  2104. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  2105. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  2106. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  2107. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  2108. if (h->mb_x + 1 < h->mb_width) {
  2109. XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  2110. XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  2111. }
  2112. } else {
  2113. if (deblock_topleft) {
  2114. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  2115. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  2116. }
  2117. XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
  2118. XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
  2119. }
  2120. }
  2121. }
  2122. }
  2123. static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
  2124. int index)
  2125. {
  2126. if (high_bit_depth) {
  2127. return AV_RN32A(((int32_t *)mb) + index);
  2128. } else
  2129. return AV_RN16A(mb + index);
  2130. }
  2131. static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
  2132. int index, int value)
  2133. {
  2134. if (high_bit_depth) {
  2135. AV_WN32A(((int32_t *)mb) + index, value);
  2136. } else
  2137. AV_WN16A(mb + index, value);
  2138. }
  2139. static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
  2140. int mb_type, int is_h264,
  2141. int simple,
  2142. int transform_bypass,
  2143. int pixel_shift,
  2144. int *block_offset,
  2145. int linesize,
  2146. uint8_t *dest_y, int p)
  2147. {
  2148. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  2149. void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
  2150. int i;
  2151. int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
  2152. block_offset += 16 * p;
  2153. if (IS_INTRA4x4(mb_type)) {
  2154. if (IS_8x8DCT(mb_type)) {
  2155. if (transform_bypass) {
  2156. idct_dc_add =
  2157. idct_add = h->h264dsp.h264_add_pixels8_clear;
  2158. } else {
  2159. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  2160. idct_add = h->h264dsp.h264_idct8_add;
  2161. }
  2162. for (i = 0; i < 16; i += 4) {
  2163. uint8_t *const ptr = dest_y + block_offset[i];
  2164. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  2165. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  2166. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2167. } else {
  2168. const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  2169. h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
  2170. (h->topright_samples_available << i) & 0x4000, linesize);
  2171. if (nnz) {
  2172. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  2173. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2174. else
  2175. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2176. }
  2177. }
  2178. }
  2179. } else {
  2180. if (transform_bypass) {
  2181. idct_dc_add =
  2182. idct_add = h->h264dsp.h264_add_pixels4_clear;
  2183. } else {
  2184. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  2185. idct_add = h->h264dsp.h264_idct_add;
  2186. }
  2187. for (i = 0; i < 16; i++) {
  2188. uint8_t *const ptr = dest_y + block_offset[i];
  2189. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  2190. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  2191. h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2192. } else {
  2193. uint8_t *topright;
  2194. int nnz, tr;
  2195. uint64_t tr_high;
  2196. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  2197. const int topright_avail = (h->topright_samples_available << i) & 0x8000;
  2198. av_assert2(h->mb_y || linesize <= block_offset[i]);
  2199. if (!topright_avail) {
  2200. if (pixel_shift) {
  2201. tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
  2202. topright = (uint8_t *)&tr_high;
  2203. } else {
  2204. tr = ptr[3 - linesize] * 0x01010101u;
  2205. topright = (uint8_t *)&tr;
  2206. }
  2207. } else
  2208. topright = ptr + (4 << pixel_shift) - linesize;
  2209. } else
  2210. topright = NULL;
  2211. h->hpc.pred4x4[dir](ptr, topright, linesize);
  2212. nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  2213. if (nnz) {
  2214. if (is_h264) {
  2215. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  2216. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2217. else
  2218. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  2219. } else if (CONFIG_SVQ3_DECODER)
  2220. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
  2221. }
  2222. }
  2223. }
  2224. }
  2225. } else {
  2226. h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
  2227. if (is_h264) {
  2228. if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
  2229. if (!transform_bypass)
  2230. h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
  2231. h->mb_luma_dc[p],
  2232. h->dequant4_coeff[p][qscale][0]);
  2233. else {
  2234. static const uint8_t dc_mapping[16] = {
  2235. 0 * 16, 1 * 16, 4 * 16, 5 * 16,
  2236. 2 * 16, 3 * 16, 6 * 16, 7 * 16,
  2237. 8 * 16, 9 * 16, 12 * 16, 13 * 16,
  2238. 10 * 16, 11 * 16, 14 * 16, 15 * 16
  2239. };
  2240. for (i = 0; i < 16; i++)
  2241. dctcoef_set(h->mb + (p * 256 << pixel_shift),
  2242. pixel_shift, dc_mapping[i],
  2243. dctcoef_get(h->mb_luma_dc[p],
  2244. pixel_shift, i));
  2245. }
  2246. }
  2247. } else if (CONFIG_SVQ3_DECODER)
  2248. ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
  2249. h->mb_luma_dc[p], qscale);
  2250. }
  2251. }
  2252. static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
  2253. int is_h264, int simple,
  2254. int transform_bypass,
  2255. int pixel_shift,
  2256. int *block_offset,
  2257. int linesize,
  2258. uint8_t *dest_y, int p)
  2259. {
  2260. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  2261. int i;
  2262. block_offset += 16 * p;
  2263. if (!IS_INTRA4x4(mb_type)) {
  2264. if (is_h264) {
  2265. if (IS_INTRA16x16(mb_type)) {
  2266. if (transform_bypass) {
  2267. if (h->sps.profile_idc == 244 &&
  2268. (h->intra16x16_pred_mode == VERT_PRED8x8 ||
  2269. h->intra16x16_pred_mode == HOR_PRED8x8)) {
  2270. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
  2271. h->mb + (p * 256 << pixel_shift),
  2272. linesize);
  2273. } else {
  2274. for (i = 0; i < 16; i++)
  2275. if (h->non_zero_count_cache[scan8[i + p * 16]] ||
  2276. dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  2277. h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
  2278. h->mb + (i * 16 + p * 256 << pixel_shift),
  2279. linesize);
  2280. }
  2281. } else {
  2282. h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
  2283. h->mb + (p * 256 << pixel_shift),
  2284. linesize,
  2285. h->non_zero_count_cache + p * 5 * 8);
  2286. }
  2287. } else if (h->cbp & 15) {
  2288. if (transform_bypass) {
  2289. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  2290. idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
  2291. : h->h264dsp.h264_add_pixels4_clear;
  2292. for (i = 0; i < 16; i += di)
  2293. if (h->non_zero_count_cache[scan8[i + p * 16]])
  2294. idct_add(dest_y + block_offset[i],
  2295. h->mb + (i * 16 + p * 256 << pixel_shift),
  2296. linesize);
  2297. } else {
  2298. if (IS_8x8DCT(mb_type))
  2299. h->h264dsp.h264_idct8_add4(dest_y, block_offset,
  2300. h->mb + (p * 256 << pixel_shift),
  2301. linesize,
  2302. h->non_zero_count_cache + p * 5 * 8);
  2303. else
  2304. h->h264dsp.h264_idct_add16(dest_y, block_offset,
  2305. h->mb + (p * 256 << pixel_shift),
  2306. linesize,
  2307. h->non_zero_count_cache + p * 5 * 8);
  2308. }
  2309. }
  2310. } else if (CONFIG_SVQ3_DECODER) {
  2311. for (i = 0; i < 16; i++)
  2312. if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
  2313. // FIXME benchmark weird rule, & below
  2314. uint8_t *const ptr = dest_y + block_offset[i];
  2315. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
  2316. h->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2317. }
  2318. }
  2319. }
  2320. }
  2321. #define BITS 8
  2322. #define SIMPLE 1
  2323. #include "h264_mb_template.c"
  2324. #undef BITS
  2325. #define BITS 16
  2326. #include "h264_mb_template.c"
  2327. #undef SIMPLE
  2328. #define SIMPLE 0
  2329. #include "h264_mb_template.c"
  2330. void ff_h264_hl_decode_mb(H264Context *h)
  2331. {
  2332. const int mb_xy = h->mb_xy;
  2333. const int mb_type = h->cur_pic.mb_type[mb_xy];
  2334. int is_complex = CONFIG_SMALL || h->is_complex ||
  2335. IS_INTRA_PCM(mb_type) || h->qscale == 0;
  2336. if (CHROMA444(h)) {
  2337. if (is_complex || h->pixel_shift)
  2338. hl_decode_mb_444_complex(h);
  2339. else
  2340. hl_decode_mb_444_simple_8(h);
  2341. } else if (is_complex) {
  2342. hl_decode_mb_complex(h);
  2343. } else if (h->pixel_shift) {
  2344. hl_decode_mb_simple_16(h);
  2345. } else
  2346. hl_decode_mb_simple_8(h);
  2347. }
  2348. int ff_pred_weight_table(H264Context *h)
  2349. {
  2350. int list, i;
  2351. int luma_def, chroma_def;
  2352. h->use_weight = 0;
  2353. h->use_weight_chroma = 0;
  2354. h->luma_log2_weight_denom = get_ue_golomb(&h->gb);
  2355. if (h->sps.chroma_format_idc)
  2356. h->chroma_log2_weight_denom = get_ue_golomb(&h->gb);
  2357. luma_def = 1 << h->luma_log2_weight_denom;
  2358. chroma_def = 1 << h->chroma_log2_weight_denom;
  2359. for (list = 0; list < 2; list++) {
  2360. h->luma_weight_flag[list] = 0;
  2361. h->chroma_weight_flag[list] = 0;
  2362. for (i = 0; i < h->ref_count[list]; i++) {
  2363. int luma_weight_flag, chroma_weight_flag;
  2364. luma_weight_flag = get_bits1(&h->gb);
  2365. if (luma_weight_flag) {
  2366. h->luma_weight[i][list][0] = get_se_golomb(&h->gb);
  2367. h->luma_weight[i][list][1] = get_se_golomb(&h->gb);
  2368. if (h->luma_weight[i][list][0] != luma_def ||
  2369. h->luma_weight[i][list][1] != 0) {
  2370. h->use_weight = 1;
  2371. h->luma_weight_flag[list] = 1;
  2372. }
  2373. } else {
  2374. h->luma_weight[i][list][0] = luma_def;
  2375. h->luma_weight[i][list][1] = 0;
  2376. }
  2377. if (h->sps.chroma_format_idc) {
  2378. chroma_weight_flag = get_bits1(&h->gb);
  2379. if (chroma_weight_flag) {
  2380. int j;
  2381. for (j = 0; j < 2; j++) {
  2382. h->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb);
  2383. h->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb);
  2384. if (h->chroma_weight[i][list][j][0] != chroma_def ||
  2385. h->chroma_weight[i][list][j][1] != 0) {
  2386. h->use_weight_chroma = 1;
  2387. h->chroma_weight_flag[list] = 1;
  2388. }
  2389. }
  2390. } else {
  2391. int j;
  2392. for (j = 0; j < 2; j++) {
  2393. h->chroma_weight[i][list][j][0] = chroma_def;
  2394. h->chroma_weight[i][list][j][1] = 0;
  2395. }
  2396. }
  2397. }
  2398. }
  2399. if (h->slice_type_nos != AV_PICTURE_TYPE_B)
  2400. break;
  2401. }
  2402. h->use_weight = h->use_weight || h->use_weight_chroma;
  2403. return 0;
  2404. }
  2405. /**
  2406. * Initialize implicit_weight table.
  2407. * @param field 0/1 initialize the weight for interlaced MBAFF
  2408. * -1 initializes the rest
  2409. */
  2410. static void implicit_weight_table(H264Context *h, int field)
  2411. {
  2412. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  2413. for (i = 0; i < 2; i++) {
  2414. h->luma_weight_flag[i] = 0;
  2415. h->chroma_weight_flag[i] = 0;
  2416. }
  2417. if (field < 0) {
  2418. if (h->picture_structure == PICT_FRAME) {
  2419. cur_poc = h->cur_pic_ptr->poc;
  2420. } else {
  2421. cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
  2422. }
  2423. if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
  2424. h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
  2425. h->use_weight = 0;
  2426. h->use_weight_chroma = 0;
  2427. return;
  2428. }
  2429. ref_start = 0;
  2430. ref_count0 = h->ref_count[0];
  2431. ref_count1 = h->ref_count[1];
  2432. } else {
  2433. cur_poc = h->cur_pic_ptr->field_poc[field];
  2434. ref_start = 16;
  2435. ref_count0 = 16 + 2 * h->ref_count[0];
  2436. ref_count1 = 16 + 2 * h->ref_count[1];
  2437. }
  2438. h->use_weight = 2;
  2439. h->use_weight_chroma = 2;
  2440. h->luma_log2_weight_denom = 5;
  2441. h->chroma_log2_weight_denom = 5;
  2442. for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
  2443. int poc0 = h->ref_list[0][ref0].poc;
  2444. for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
  2445. int w = 32;
  2446. if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
  2447. int poc1 = h->ref_list[1][ref1].poc;
  2448. int td = av_clip(poc1 - poc0, -128, 127);
  2449. if (td) {
  2450. int tb = av_clip(cur_poc - poc0, -128, 127);
  2451. int tx = (16384 + (FFABS(td) >> 1)) / td;
  2452. int dist_scale_factor = (tb * tx + 32) >> 8;
  2453. if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
  2454. w = 64 - dist_scale_factor;
  2455. }
  2456. }
  2457. if (field < 0) {
  2458. h->implicit_weight[ref0][ref1][0] =
  2459. h->implicit_weight[ref0][ref1][1] = w;
  2460. } else {
  2461. h->implicit_weight[ref0][ref1][field] = w;
  2462. }
  2463. }
  2464. }
  2465. }
  2466. /**
  2467. * instantaneous decoder refresh.
  2468. */
  2469. static void idr(H264Context *h)
  2470. {
  2471. int i;
  2472. ff_h264_remove_all_refs(h);
  2473. h->prev_frame_num = 0;
  2474. h->prev_frame_num_offset = 0;
  2475. h->prev_poc_msb = 1<<16;
  2476. h->prev_poc_lsb = 0;
  2477. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  2478. h->last_pocs[i] = INT_MIN;
  2479. }
  2480. /* forget old pics after a seek */
  2481. static void flush_change(H264Context *h)
  2482. {
  2483. int i, j;
  2484. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  2485. h->prev_interlaced_frame = 1;
  2486. idr(h);
  2487. h->prev_frame_num = -1;
  2488. if (h->cur_pic_ptr) {
  2489. h->cur_pic_ptr->reference = 0;
  2490. for (j=i=0; h->delayed_pic[i]; i++)
  2491. if (h->delayed_pic[i] != h->cur_pic_ptr)
  2492. h->delayed_pic[j++] = h->delayed_pic[i];
  2493. h->delayed_pic[j] = NULL;
  2494. }
  2495. h->first_field = 0;
  2496. memset(h->ref_list[0], 0, sizeof(h->ref_list[0]));
  2497. memset(h->ref_list[1], 0, sizeof(h->ref_list[1]));
  2498. memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0]));
  2499. memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1]));
  2500. ff_h264_reset_sei(h);
  2501. h->recovery_frame = -1;
  2502. h->frame_recovered = 0;
  2503. h->list_count = 0;
  2504. h->current_slice = 0;
  2505. h->mmco_reset = 1;
  2506. }
  2507. /* forget old pics after a seek */
  2508. static void flush_dpb(AVCodecContext *avctx)
  2509. {
  2510. H264Context *h = avctx->priv_data;
  2511. int i;
  2512. for (i = 0; i <= MAX_DELAYED_PIC_COUNT; i++) {
  2513. if (h->delayed_pic[i])
  2514. h->delayed_pic[i]->reference = 0;
  2515. h->delayed_pic[i] = NULL;
  2516. }
  2517. flush_change(h);
  2518. if (h->DPB)
  2519. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2520. unref_picture(h, &h->DPB[i]);
  2521. h->cur_pic_ptr = NULL;
  2522. unref_picture(h, &h->cur_pic);
  2523. h->mb_x = h->mb_y = 0;
  2524. h->parse_context.state = -1;
  2525. h->parse_context.frame_start_found = 0;
  2526. h->parse_context.overread = 0;
  2527. h->parse_context.overread_index = 0;
  2528. h->parse_context.index = 0;
  2529. h->parse_context.last_index = 0;
  2530. free_tables(h, 1);
  2531. h->context_initialized = 0;
  2532. }
  2533. int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
  2534. {
  2535. const int max_frame_num = 1 << h->sps.log2_max_frame_num;
  2536. int field_poc[2];
  2537. h->frame_num_offset = h->prev_frame_num_offset;
  2538. if (h->frame_num < h->prev_frame_num)
  2539. h->frame_num_offset += max_frame_num;
  2540. if (h->sps.poc_type == 0) {
  2541. const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
  2542. if (h->poc_lsb < h->prev_poc_lsb &&
  2543. h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
  2544. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2545. else if (h->poc_lsb > h->prev_poc_lsb &&
  2546. h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
  2547. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2548. else
  2549. h->poc_msb = h->prev_poc_msb;
  2550. field_poc[0] =
  2551. field_poc[1] = h->poc_msb + h->poc_lsb;
  2552. if (h->picture_structure == PICT_FRAME)
  2553. field_poc[1] += h->delta_poc_bottom;
  2554. } else if (h->sps.poc_type == 1) {
  2555. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2556. int i;
  2557. if (h->sps.poc_cycle_length != 0)
  2558. abs_frame_num = h->frame_num_offset + h->frame_num;
  2559. else
  2560. abs_frame_num = 0;
  2561. if (h->nal_ref_idc == 0 && abs_frame_num > 0)
  2562. abs_frame_num--;
  2563. expected_delta_per_poc_cycle = 0;
  2564. for (i = 0; i < h->sps.poc_cycle_length; i++)
  2565. // FIXME integrate during sps parse
  2566. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
  2567. if (abs_frame_num > 0) {
  2568. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2569. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2570. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2571. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  2572. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
  2573. } else
  2574. expectedpoc = 0;
  2575. if (h->nal_ref_idc == 0)
  2576. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2577. field_poc[0] = expectedpoc + h->delta_poc[0];
  2578. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2579. if (h->picture_structure == PICT_FRAME)
  2580. field_poc[1] += h->delta_poc[1];
  2581. } else {
  2582. int poc = 2 * (h->frame_num_offset + h->frame_num);
  2583. if (!h->nal_ref_idc)
  2584. poc--;
  2585. field_poc[0] = poc;
  2586. field_poc[1] = poc;
  2587. }
  2588. if (h->picture_structure != PICT_BOTTOM_FIELD)
  2589. pic_field_poc[0] = field_poc[0];
  2590. if (h->picture_structure != PICT_TOP_FIELD)
  2591. pic_field_poc[1] = field_poc[1];
  2592. *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
  2593. return 0;
  2594. }
  2595. /**
  2596. * initialize scan tables
  2597. */
  2598. static void init_scan_tables(H264Context *h)
  2599. {
  2600. int i;
  2601. for (i = 0; i < 16; i++) {
  2602. #define T(x) (x >> 2) | ((x << 2) & 0xF)
  2603. h->zigzag_scan[i] = T(zigzag_scan[i]);
  2604. h->field_scan[i] = T(field_scan[i]);
  2605. #undef T
  2606. }
  2607. for (i = 0; i < 64; i++) {
  2608. #define T(x) (x >> 3) | ((x & 7) << 3)
  2609. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  2610. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  2611. h->field_scan8x8[i] = T(field_scan8x8[i]);
  2612. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  2613. #undef T
  2614. }
  2615. if (h->sps.transform_bypass) { // FIXME same ugly
  2616. memcpy(h->zigzag_scan_q0 , zigzag_scan , sizeof(h->zigzag_scan_q0 ));
  2617. memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
  2618. memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
  2619. memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
  2620. memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
  2621. memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
  2622. } else {
  2623. memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
  2624. memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
  2625. memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
  2626. memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
  2627. memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
  2628. memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
  2629. }
  2630. }
  2631. static int field_end(H264Context *h, int in_setup)
  2632. {
  2633. AVCodecContext *const avctx = h->avctx;
  2634. int err = 0;
  2635. h->mb_y = 0;
  2636. if (CONFIG_H264_VDPAU_DECODER &&
  2637. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2638. ff_vdpau_h264_set_reference_frames(h);
  2639. if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  2640. if (!h->droppable) {
  2641. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2642. h->prev_poc_msb = h->poc_msb;
  2643. h->prev_poc_lsb = h->poc_lsb;
  2644. }
  2645. h->prev_frame_num_offset = h->frame_num_offset;
  2646. h->prev_frame_num = h->frame_num;
  2647. h->outputed_poc = h->next_outputed_poc;
  2648. }
  2649. if (avctx->hwaccel) {
  2650. if (avctx->hwaccel->end_frame(avctx) < 0)
  2651. av_log(avctx, AV_LOG_ERROR,
  2652. "hardware accelerator failed to decode picture\n");
  2653. }
  2654. if (CONFIG_H264_VDPAU_DECODER &&
  2655. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2656. ff_vdpau_h264_picture_complete(h);
  2657. /*
  2658. * FIXME: Error handling code does not seem to support interlaced
  2659. * when slices span multiple rows
  2660. * The ff_er_add_slice calls don't work right for bottom
  2661. * fields; they cause massive erroneous error concealing
  2662. * Error marking covers both fields (top and bottom).
  2663. * This causes a mismatched s->error_count
  2664. * and a bad error table. Further, the error count goes to
  2665. * INT_MAX when called for bottom field, because mb_y is
  2666. * past end by one (callers fault) and resync_mb_y != 0
  2667. * causes problems for the first MB line, too.
  2668. */
  2669. if (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE(h) && h->current_slice && !h->sps.new) {
  2670. h->er.cur_pic = h->cur_pic_ptr;
  2671. ff_er_frame_end(&h->er);
  2672. }
  2673. if (!in_setup && !h->droppable)
  2674. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  2675. h->picture_structure == PICT_BOTTOM_FIELD);
  2676. emms_c();
  2677. h->current_slice = 0;
  2678. return err;
  2679. }
  2680. /**
  2681. * Replicate H264 "master" context to thread contexts.
  2682. */
  2683. static int clone_slice(H264Context *dst, H264Context *src)
  2684. {
  2685. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2686. dst->cur_pic_ptr = src->cur_pic_ptr;
  2687. dst->cur_pic = src->cur_pic;
  2688. dst->linesize = src->linesize;
  2689. dst->uvlinesize = src->uvlinesize;
  2690. dst->first_field = src->first_field;
  2691. dst->prev_poc_msb = src->prev_poc_msb;
  2692. dst->prev_poc_lsb = src->prev_poc_lsb;
  2693. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2694. dst->prev_frame_num = src->prev_frame_num;
  2695. dst->short_ref_count = src->short_ref_count;
  2696. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2697. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2698. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2699. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2700. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2701. return 0;
  2702. }
  2703. /**
  2704. * Compute profile from profile_idc and constraint_set?_flags.
  2705. *
  2706. * @param sps SPS
  2707. *
  2708. * @return profile as defined by FF_PROFILE_H264_*
  2709. */
  2710. int ff_h264_get_profile(SPS *sps)
  2711. {
  2712. int profile = sps->profile_idc;
  2713. switch (sps->profile_idc) {
  2714. case FF_PROFILE_H264_BASELINE:
  2715. // constraint_set1_flag set to 1
  2716. profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  2717. break;
  2718. case FF_PROFILE_H264_HIGH_10:
  2719. case FF_PROFILE_H264_HIGH_422:
  2720. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  2721. // constraint_set3_flag set to 1
  2722. profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
  2723. break;
  2724. }
  2725. return profile;
  2726. }
  2727. static int h264_set_parameter_from_sps(H264Context *h)
  2728. {
  2729. if (h->flags & CODEC_FLAG_LOW_DELAY ||
  2730. (h->sps.bitstream_restriction_flag &&
  2731. !h->sps.num_reorder_frames)) {
  2732. if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
  2733. av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
  2734. "Reenabling low delay requires a codec flush.\n");
  2735. else
  2736. h->low_delay = 1;
  2737. }
  2738. if (h->avctx->has_b_frames < 2)
  2739. h->avctx->has_b_frames = !h->low_delay;
  2740. if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
  2741. h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
  2742. if (h->avctx->codec &&
  2743. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU &&
  2744. (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
  2745. av_log(h->avctx, AV_LOG_ERROR,
  2746. "VDPAU decoding does not support video colorspace.\n");
  2747. return AVERROR_INVALIDDATA;
  2748. }
  2749. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 14 &&
  2750. h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13) {
  2751. h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2752. h->cur_chroma_format_idc = h->sps.chroma_format_idc;
  2753. h->pixel_shift = h->sps.bit_depth_luma > 8;
  2754. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma,
  2755. h->sps.chroma_format_idc);
  2756. ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
  2757. ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
  2758. ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
  2759. h->sps.chroma_format_idc);
  2760. if (CONFIG_ERROR_RESILIENCE)
  2761. ff_dsputil_init(&h->dsp, h->avctx);
  2762. ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
  2763. } else {
  2764. av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
  2765. h->sps.bit_depth_luma);
  2766. return AVERROR_INVALIDDATA;
  2767. }
  2768. }
  2769. return 0;
  2770. }
  2771. static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
  2772. {
  2773. switch (h->sps.bit_depth_luma) {
  2774. case 9:
  2775. if (CHROMA444(h)) {
  2776. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2777. return AV_PIX_FMT_GBRP9;
  2778. } else
  2779. return AV_PIX_FMT_YUV444P9;
  2780. } else if (CHROMA422(h))
  2781. return AV_PIX_FMT_YUV422P9;
  2782. else
  2783. return AV_PIX_FMT_YUV420P9;
  2784. break;
  2785. case 10:
  2786. if (CHROMA444(h)) {
  2787. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2788. return AV_PIX_FMT_GBRP10;
  2789. } else
  2790. return AV_PIX_FMT_YUV444P10;
  2791. } else if (CHROMA422(h))
  2792. return AV_PIX_FMT_YUV422P10;
  2793. else
  2794. return AV_PIX_FMT_YUV420P10;
  2795. break;
  2796. case 12:
  2797. if (CHROMA444(h)) {
  2798. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2799. return AV_PIX_FMT_GBRP12;
  2800. } else
  2801. return AV_PIX_FMT_YUV444P12;
  2802. } else if (CHROMA422(h))
  2803. return AV_PIX_FMT_YUV422P12;
  2804. else
  2805. return AV_PIX_FMT_YUV420P12;
  2806. break;
  2807. case 14:
  2808. if (CHROMA444(h)) {
  2809. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2810. return AV_PIX_FMT_GBRP14;
  2811. } else
  2812. return AV_PIX_FMT_YUV444P14;
  2813. } else if (CHROMA422(h))
  2814. return AV_PIX_FMT_YUV422P14;
  2815. else
  2816. return AV_PIX_FMT_YUV420P14;
  2817. break;
  2818. case 8:
  2819. if (CHROMA444(h)) {
  2820. if (h->avctx->colorspace == AVCOL_SPC_RGB) {
  2821. av_log(h->avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
  2822. return AV_PIX_FMT_GBR24P;
  2823. } else if (h->avctx->colorspace == AVCOL_SPC_YCGCO) {
  2824. av_log(h->avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
  2825. }
  2826. return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ444P
  2827. : AV_PIX_FMT_YUV444P;
  2828. } else if (CHROMA422(h)) {
  2829. return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ422P
  2830. : AV_PIX_FMT_YUV422P;
  2831. } else {
  2832. int i;
  2833. const enum AVPixelFormat * fmt = h->avctx->codec->pix_fmts ?
  2834. h->avctx->codec->pix_fmts :
  2835. h->avctx->color_range == AVCOL_RANGE_JPEG ?
  2836. h264_hwaccel_pixfmt_list_jpeg_420 :
  2837. h264_hwaccel_pixfmt_list_420;
  2838. for (i=0; fmt[i] != AV_PIX_FMT_NONE; i++)
  2839. if (fmt[i] == h->avctx->pix_fmt && !force_callback)
  2840. return fmt[i];
  2841. return ff_thread_get_format(h->avctx, fmt);
  2842. }
  2843. break;
  2844. default:
  2845. av_log(h->avctx, AV_LOG_ERROR,
  2846. "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2847. return AVERROR_INVALIDDATA;
  2848. }
  2849. }
  2850. /* export coded and cropped frame dimensions to AVCodecContext */
  2851. static int init_dimensions(H264Context *h)
  2852. {
  2853. int width = h->width - (h->sps.crop_right + h->sps.crop_left);
  2854. int height = h->height - (h->sps.crop_top + h->sps.crop_bottom);
  2855. av_assert0(h->sps.crop_right + h->sps.crop_left < (unsigned)h->width);
  2856. av_assert0(h->sps.crop_top + h->sps.crop_bottom < (unsigned)h->height);
  2857. /* handle container cropping */
  2858. if (!h->sps.crop &&
  2859. FFALIGN(h->avctx->width, 16) == h->width &&
  2860. FFALIGN(h->avctx->height, 16) == h->height) {
  2861. width = h->avctx->width;
  2862. height = h->avctx->height;
  2863. }
  2864. if (width <= 0 || height <= 0) {
  2865. av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
  2866. width, height);
  2867. if (h->avctx->err_recognition & AV_EF_EXPLODE)
  2868. return AVERROR_INVALIDDATA;
  2869. av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
  2870. h->sps.crop_bottom = h->sps.crop_top = h->sps.crop_right = h->sps.crop_left = 0;
  2871. h->sps.crop = 0;
  2872. width = h->width;
  2873. height = h->height;
  2874. }
  2875. h->avctx->coded_width = h->width;
  2876. h->avctx->coded_height = h->height;
  2877. h->avctx->width = width;
  2878. h->avctx->height = height;
  2879. return 0;
  2880. }
  2881. static int h264_slice_header_init(H264Context *h, int reinit)
  2882. {
  2883. int nb_slices = (HAVE_THREADS &&
  2884. h->avctx->active_thread_type & FF_THREAD_SLICE) ?
  2885. h->avctx->thread_count : 1;
  2886. int i, ret;
  2887. h->avctx->sample_aspect_ratio = h->sps.sar;
  2888. av_assert0(h->avctx->sample_aspect_ratio.den);
  2889. av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
  2890. &h->chroma_x_shift, &h->chroma_y_shift);
  2891. if (h->sps.timing_info_present_flag) {
  2892. int64_t den = h->sps.time_scale;
  2893. if (h->x264_build < 44U)
  2894. den *= 2;
  2895. av_reduce(&h->avctx->time_base.num, &h->avctx->time_base.den,
  2896. h->sps.num_units_in_tick, den, 1 << 30);
  2897. }
  2898. h->avctx->hwaccel = ff_find_hwaccel(h->avctx);
  2899. if (reinit)
  2900. free_tables(h, 0);
  2901. h->first_field = 0;
  2902. h->prev_interlaced_frame = 1;
  2903. init_scan_tables(h);
  2904. ret = ff_h264_alloc_tables(h);
  2905. if (ret < 0) {
  2906. av_log(h->avctx, AV_LOG_ERROR,
  2907. "Could not allocate memory for h264\n");
  2908. return ret;
  2909. }
  2910. if (nb_slices > MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) {
  2911. int max_slices;
  2912. if (h->mb_height)
  2913. max_slices = FFMIN(MAX_THREADS, h->mb_height);
  2914. else
  2915. max_slices = MAX_THREADS;
  2916. av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  2917. " reducing to %d\n", nb_slices, max_slices);
  2918. nb_slices = max_slices;
  2919. }
  2920. h->slice_context_count = nb_slices;
  2921. if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
  2922. ret = context_init(h);
  2923. if (ret < 0) {
  2924. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2925. return ret;
  2926. }
  2927. } else {
  2928. for (i = 1; i < h->slice_context_count; i++) {
  2929. H264Context *c;
  2930. c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
  2931. if (!c)
  2932. return AVERROR(ENOMEM);
  2933. c->avctx = h->avctx;
  2934. if (CONFIG_ERROR_RESILIENCE) {
  2935. c->dsp = h->dsp;
  2936. }
  2937. c->vdsp = h->vdsp;
  2938. c->h264dsp = h->h264dsp;
  2939. c->h264qpel = h->h264qpel;
  2940. c->h264chroma = h->h264chroma;
  2941. c->sps = h->sps;
  2942. c->pps = h->pps;
  2943. c->pixel_shift = h->pixel_shift;
  2944. c->cur_chroma_format_idc = h->cur_chroma_format_idc;
  2945. c->width = h->width;
  2946. c->height = h->height;
  2947. c->linesize = h->linesize;
  2948. c->uvlinesize = h->uvlinesize;
  2949. c->chroma_x_shift = h->chroma_x_shift;
  2950. c->chroma_y_shift = h->chroma_y_shift;
  2951. c->qscale = h->qscale;
  2952. c->droppable = h->droppable;
  2953. c->data_partitioning = h->data_partitioning;
  2954. c->low_delay = h->low_delay;
  2955. c->mb_width = h->mb_width;
  2956. c->mb_height = h->mb_height;
  2957. c->mb_stride = h->mb_stride;
  2958. c->mb_num = h->mb_num;
  2959. c->flags = h->flags;
  2960. c->workaround_bugs = h->workaround_bugs;
  2961. c->pict_type = h->pict_type;
  2962. init_scan_tables(c);
  2963. clone_tables(c, h, i);
  2964. c->context_initialized = 1;
  2965. }
  2966. for (i = 0; i < h->slice_context_count; i++)
  2967. if ((ret = context_init(h->thread_context[i])) < 0) {
  2968. av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2969. return ret;
  2970. }
  2971. }
  2972. h->context_initialized = 1;
  2973. return 0;
  2974. }
  2975. int ff_set_ref_count(H264Context *h)
  2976. {
  2977. int ref_count[2], list_count;
  2978. int num_ref_idx_active_override_flag;
  2979. // set defaults, might be overridden a few lines later
  2980. ref_count[0] = h->pps.ref_count[0];
  2981. ref_count[1] = h->pps.ref_count[1];
  2982. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  2983. unsigned max[2];
  2984. max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
  2985. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  2986. h->direct_spatial_mv_pred = get_bits1(&h->gb);
  2987. num_ref_idx_active_override_flag = get_bits1(&h->gb);
  2988. if (num_ref_idx_active_override_flag) {
  2989. ref_count[0] = get_ue_golomb(&h->gb) + 1;
  2990. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  2991. ref_count[1] = get_ue_golomb(&h->gb) + 1;
  2992. } else
  2993. // full range is spec-ok in this case, even for frames
  2994. ref_count[1] = 1;
  2995. }
  2996. if (ref_count[0]-1 > max[0] || ref_count[1]-1 > max[1]){
  2997. av_log(h->avctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n", ref_count[0]-1, max[0], ref_count[1]-1, max[1]);
  2998. h->ref_count[0] = h->ref_count[1] = 0;
  2999. h->list_count = 0;
  3000. return AVERROR_INVALIDDATA;
  3001. }
  3002. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  3003. list_count = 2;
  3004. else
  3005. list_count = 1;
  3006. } else {
  3007. list_count = 0;
  3008. ref_count[0] = ref_count[1] = 0;
  3009. }
  3010. if (list_count != h->list_count ||
  3011. ref_count[0] != h->ref_count[0] ||
  3012. ref_count[1] != h->ref_count[1]) {
  3013. h->ref_count[0] = ref_count[0];
  3014. h->ref_count[1] = ref_count[1];
  3015. h->list_count = list_count;
  3016. return 1;
  3017. }
  3018. return 0;
  3019. }
  3020. /**
  3021. * Decode a slice header.
  3022. * This will also call ff_MPV_common_init() and frame_start() as needed.
  3023. *
  3024. * @param h h264context
  3025. * @param h0 h264 master context (differs from 'h' when doing sliced based
  3026. * parallel decoding)
  3027. *
  3028. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  3029. */
  3030. static int decode_slice_header(H264Context *h, H264Context *h0)
  3031. {
  3032. unsigned int first_mb_in_slice;
  3033. unsigned int pps_id;
  3034. int ret;
  3035. unsigned int slice_type, tmp, i, j;
  3036. int last_pic_structure, last_pic_droppable;
  3037. int must_reinit;
  3038. int needs_reinit = 0;
  3039. int field_pic_flag, bottom_field_flag;
  3040. h->me.qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
  3041. h->me.qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
  3042. first_mb_in_slice = get_ue_golomb_long(&h->gb);
  3043. if (first_mb_in_slice == 0) { // FIXME better field boundary detection
  3044. if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
  3045. field_end(h, 1);
  3046. }
  3047. h0->current_slice = 0;
  3048. if (!h0->first_field) {
  3049. if (h->cur_pic_ptr && !h->droppable) {
  3050. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  3051. h->picture_structure == PICT_BOTTOM_FIELD);
  3052. }
  3053. h->cur_pic_ptr = NULL;
  3054. }
  3055. }
  3056. slice_type = get_ue_golomb_31(&h->gb);
  3057. if (slice_type > 9) {
  3058. av_log(h->avctx, AV_LOG_ERROR,
  3059. "slice type too large (%d) at %d %d\n",
  3060. slice_type, h->mb_x, h->mb_y);
  3061. return AVERROR_INVALIDDATA;
  3062. }
  3063. if (slice_type > 4) {
  3064. slice_type -= 5;
  3065. h->slice_type_fixed = 1;
  3066. } else
  3067. h->slice_type_fixed = 0;
  3068. slice_type = golomb_to_pict_type[slice_type];
  3069. h->slice_type = slice_type;
  3070. h->slice_type_nos = slice_type & 3;
  3071. // to make a few old functions happy, it's wrong though
  3072. h->pict_type = h->slice_type;
  3073. pps_id = get_ue_golomb(&h->gb);
  3074. if (pps_id >= MAX_PPS_COUNT) {
  3075. av_log(h->avctx, AV_LOG_ERROR, "pps_id %d out of range\n", pps_id);
  3076. return AVERROR_INVALIDDATA;
  3077. }
  3078. if (!h0->pps_buffers[pps_id]) {
  3079. av_log(h->avctx, AV_LOG_ERROR,
  3080. "non-existing PPS %u referenced\n",
  3081. pps_id);
  3082. return AVERROR_INVALIDDATA;
  3083. }
  3084. h->pps = *h0->pps_buffers[pps_id];
  3085. if (!h0->sps_buffers[h->pps.sps_id]) {
  3086. av_log(h->avctx, AV_LOG_ERROR,
  3087. "non-existing SPS %u referenced\n",
  3088. h->pps.sps_id);
  3089. return AVERROR_INVALIDDATA;
  3090. }
  3091. if (h->pps.sps_id != h->current_sps_id ||
  3092. h0->sps_buffers[h->pps.sps_id]->new) {
  3093. h0->sps_buffers[h->pps.sps_id]->new = 0;
  3094. h->current_sps_id = h->pps.sps_id;
  3095. h->sps = *h0->sps_buffers[h->pps.sps_id];
  3096. if (h->mb_width != h->sps.mb_width ||
  3097. h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||
  3098. h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
  3099. h->cur_chroma_format_idc != h->sps.chroma_format_idc
  3100. )
  3101. needs_reinit = 1;
  3102. if (h->bit_depth_luma != h->sps.bit_depth_luma ||
  3103. h->chroma_format_idc != h->sps.chroma_format_idc) {
  3104. h->bit_depth_luma = h->sps.bit_depth_luma;
  3105. h->chroma_format_idc = h->sps.chroma_format_idc;
  3106. needs_reinit = 1;
  3107. }
  3108. if ((ret = h264_set_parameter_from_sps(h)) < 0)
  3109. return ret;
  3110. }
  3111. h->avctx->profile = ff_h264_get_profile(&h->sps);
  3112. h->avctx->level = h->sps.level_idc;
  3113. h->avctx->refs = h->sps.ref_frame_count;
  3114. must_reinit = (h->context_initialized &&
  3115. ( 16*h->sps.mb_width != h->avctx->coded_width
  3116. || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
  3117. || h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
  3118. || h->cur_chroma_format_idc != h->sps.chroma_format_idc
  3119. || av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio)
  3120. || h->mb_width != h->sps.mb_width
  3121. || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)
  3122. ));
  3123. if (h0->avctx->pix_fmt != get_pixel_format(h0, 0))
  3124. must_reinit = 1;
  3125. h->mb_width = h->sps.mb_width;
  3126. h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3127. h->mb_num = h->mb_width * h->mb_height;
  3128. h->mb_stride = h->mb_width + 1;
  3129. h->b_stride = h->mb_width * 4;
  3130. h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
  3131. h->width = 16 * h->mb_width;
  3132. h->height = 16 * h->mb_height;
  3133. ret = init_dimensions(h);
  3134. if (ret < 0)
  3135. return ret;
  3136. if (h->sps.video_signal_type_present_flag) {
  3137. h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
  3138. : AVCOL_RANGE_MPEG;
  3139. if (h->sps.colour_description_present_flag) {
  3140. if (h->avctx->colorspace != h->sps.colorspace)
  3141. needs_reinit = 1;
  3142. h->avctx->color_primaries = h->sps.color_primaries;
  3143. h->avctx->color_trc = h->sps.color_trc;
  3144. h->avctx->colorspace = h->sps.colorspace;
  3145. }
  3146. }
  3147. if (h->context_initialized &&
  3148. (h->width != h->avctx->coded_width ||
  3149. h->height != h->avctx->coded_height ||
  3150. must_reinit ||
  3151. needs_reinit)) {
  3152. if (h != h0) {
  3153. av_log(h->avctx, AV_LOG_ERROR, "changing width/height on "
  3154. "slice %d\n", h0->current_slice + 1);
  3155. return AVERROR_INVALIDDATA;
  3156. }
  3157. flush_change(h);
  3158. if ((ret = get_pixel_format(h, 1)) < 0)
  3159. return ret;
  3160. h->avctx->pix_fmt = ret;
  3161. av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
  3162. "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
  3163. if ((ret = h264_slice_header_init(h, 1)) < 0) {
  3164. av_log(h->avctx, AV_LOG_ERROR,
  3165. "h264_slice_header_init() failed\n");
  3166. return ret;
  3167. }
  3168. }
  3169. if (!h->context_initialized) {
  3170. if (h != h0) {
  3171. av_log(h->avctx, AV_LOG_ERROR,
  3172. "Cannot (re-)initialize context during parallel decoding.\n");
  3173. return AVERROR_PATCHWELCOME;
  3174. }
  3175. if ((ret = get_pixel_format(h, 1)) < 0)
  3176. return ret;
  3177. h->avctx->pix_fmt = ret;
  3178. if ((ret = h264_slice_header_init(h, 0)) < 0) {
  3179. av_log(h->avctx, AV_LOG_ERROR,
  3180. "h264_slice_header_init() failed\n");
  3181. return ret;
  3182. }
  3183. }
  3184. if (h == h0 && h->dequant_coeff_pps != pps_id) {
  3185. h->dequant_coeff_pps = pps_id;
  3186. init_dequant_tables(h);
  3187. }
  3188. h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
  3189. h->mb_mbaff = 0;
  3190. h->mb_aff_frame = 0;
  3191. last_pic_structure = h0->picture_structure;
  3192. last_pic_droppable = h0->droppable;
  3193. h->droppable = h->nal_ref_idc == 0;
  3194. if (h->sps.frame_mbs_only_flag) {
  3195. h->picture_structure = PICT_FRAME;
  3196. } else {
  3197. if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
  3198. av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
  3199. return -1;
  3200. }
  3201. field_pic_flag = get_bits1(&h->gb);
  3202. if (field_pic_flag) {
  3203. bottom_field_flag = get_bits1(&h->gb);
  3204. h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;
  3205. } else {
  3206. h->picture_structure = PICT_FRAME;
  3207. h->mb_aff_frame = h->sps.mb_aff;
  3208. }
  3209. }
  3210. h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
  3211. if (h0->current_slice != 0) {
  3212. if (last_pic_structure != h->picture_structure ||
  3213. last_pic_droppable != h->droppable) {
  3214. av_log(h->avctx, AV_LOG_ERROR,
  3215. "Changing field mode (%d -> %d) between slices is not allowed\n",
  3216. last_pic_structure, h->picture_structure);
  3217. h->picture_structure = last_pic_structure;
  3218. h->droppable = last_pic_droppable;
  3219. return AVERROR_INVALIDDATA;
  3220. } else if (!h0->cur_pic_ptr) {
  3221. av_log(h->avctx, AV_LOG_ERROR,
  3222. "unset cur_pic_ptr on %d. slice\n",
  3223. h0->current_slice + 1);
  3224. return AVERROR_INVALIDDATA;
  3225. }
  3226. } else {
  3227. /* Shorten frame num gaps so we don't have to allocate reference
  3228. * frames just to throw them away */
  3229. if (h->frame_num != h->prev_frame_num) {
  3230. int unwrap_prev_frame_num = h->prev_frame_num;
  3231. int max_frame_num = 1 << h->sps.log2_max_frame_num;
  3232. if (unwrap_prev_frame_num > h->frame_num)
  3233. unwrap_prev_frame_num -= max_frame_num;
  3234. if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
  3235. unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
  3236. if (unwrap_prev_frame_num < 0)
  3237. unwrap_prev_frame_num += max_frame_num;
  3238. h->prev_frame_num = unwrap_prev_frame_num;
  3239. }
  3240. }
  3241. /* See if we have a decoded first field looking for a pair...
  3242. * Here, we're using that to see if we should mark previously
  3243. * decode frames as "finished".
  3244. * We have to do that before the "dummy" in-between frame allocation,
  3245. * since that can modify h->cur_pic_ptr. */
  3246. if (h0->first_field) {
  3247. assert(h0->cur_pic_ptr);
  3248. assert(h0->cur_pic_ptr->f.buf[0]);
  3249. assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
  3250. /* Mark old field/frame as completed */
  3251. if (h0->cur_pic_ptr->tf.owner == h0->avctx) {
  3252. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  3253. last_pic_structure == PICT_BOTTOM_FIELD);
  3254. }
  3255. /* figure out if we have a complementary field pair */
  3256. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  3257. /* Previous field is unmatched. Don't display it, but let it
  3258. * remain for reference if marked as such. */
  3259. if (last_pic_structure != PICT_FRAME) {
  3260. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  3261. last_pic_structure == PICT_TOP_FIELD);
  3262. }
  3263. } else {
  3264. if (h0->cur_pic_ptr->frame_num != h->frame_num) {
  3265. /* This and previous field were reference, but had
  3266. * different frame_nums. Consider this field first in
  3267. * pair. Throw away previous field except for reference
  3268. * purposes. */
  3269. if (last_pic_structure != PICT_FRAME) {
  3270. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  3271. last_pic_structure == PICT_TOP_FIELD);
  3272. }
  3273. } else {
  3274. /* Second field in complementary pair */
  3275. if (!((last_pic_structure == PICT_TOP_FIELD &&
  3276. h->picture_structure == PICT_BOTTOM_FIELD) ||
  3277. (last_pic_structure == PICT_BOTTOM_FIELD &&
  3278. h->picture_structure == PICT_TOP_FIELD))) {
  3279. av_log(h->avctx, AV_LOG_ERROR,
  3280. "Invalid field mode combination %d/%d\n",
  3281. last_pic_structure, h->picture_structure);
  3282. h->picture_structure = last_pic_structure;
  3283. h->droppable = last_pic_droppable;
  3284. return AVERROR_INVALIDDATA;
  3285. } else if (last_pic_droppable != h->droppable) {
  3286. avpriv_request_sample(h->avctx,
  3287. "Found reference and non-reference fields in the same frame, which");
  3288. h->picture_structure = last_pic_structure;
  3289. h->droppable = last_pic_droppable;
  3290. return AVERROR_PATCHWELCOME;
  3291. }
  3292. }
  3293. }
  3294. }
  3295. while (h->frame_num != h->prev_frame_num && !h0->first_field &&
  3296. h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
  3297. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  3298. av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  3299. h->frame_num, h->prev_frame_num);
  3300. if (!h->sps.gaps_in_frame_num_allowed_flag)
  3301. for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
  3302. h->last_pocs[i] = INT_MIN;
  3303. ret = h264_frame_start(h);
  3304. if (ret < 0) {
  3305. h0->first_field = 0;
  3306. return ret;
  3307. }
  3308. h->prev_frame_num++;
  3309. h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
  3310. h->cur_pic_ptr->frame_num = h->prev_frame_num;
  3311. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
  3312. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
  3313. ret = ff_generate_sliding_window_mmcos(h, 1);
  3314. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  3315. return ret;
  3316. ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  3317. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  3318. return ret;
  3319. /* Error concealment: If a ref is missing, copy the previous ref
  3320. * in its place.
  3321. * FIXME: Avoiding a memcpy would be nice, but ref handling makes
  3322. * many assumptions about there being no actual duplicates.
  3323. * FIXME: This does not copy padding for out-of-frame motion
  3324. * vectors. Given we are concealing a lost frame, this probably
  3325. * is not noticeable by comparison, but it should be fixed. */
  3326. if (h->short_ref_count) {
  3327. if (prev) {
  3328. av_image_copy(h->short_ref[0]->f.data,
  3329. h->short_ref[0]->f.linesize,
  3330. (const uint8_t **)prev->f.data,
  3331. prev->f.linesize,
  3332. h->avctx->pix_fmt,
  3333. h->mb_width * 16,
  3334. h->mb_height * 16);
  3335. h->short_ref[0]->poc = prev->poc + 2;
  3336. }
  3337. h->short_ref[0]->frame_num = h->prev_frame_num;
  3338. }
  3339. }
  3340. /* See if we have a decoded first field looking for a pair...
  3341. * We're using that to see whether to continue decoding in that
  3342. * frame, or to allocate a new one. */
  3343. if (h0->first_field) {
  3344. assert(h0->cur_pic_ptr);
  3345. assert(h0->cur_pic_ptr->f.buf[0]);
  3346. assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
  3347. /* figure out if we have a complementary field pair */
  3348. if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
  3349. /* Previous field is unmatched. Don't display it, but let it
  3350. * remain for reference if marked as such. */
  3351. h0->cur_pic_ptr = NULL;
  3352. h0->first_field = FIELD_PICTURE(h);
  3353. } else {
  3354. if (h0->cur_pic_ptr->frame_num != h->frame_num) {
  3355. ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
  3356. h0->picture_structure==PICT_BOTTOM_FIELD);
  3357. /* This and the previous field had different frame_nums.
  3358. * Consider this field first in pair. Throw away previous
  3359. * one except for reference purposes. */
  3360. h0->first_field = 1;
  3361. h0->cur_pic_ptr = NULL;
  3362. } else {
  3363. /* Second field in complementary pair */
  3364. h0->first_field = 0;
  3365. }
  3366. }
  3367. } else {
  3368. /* Frame or first field in a potentially complementary pair */
  3369. h0->first_field = FIELD_PICTURE(h);
  3370. }
  3371. if (!FIELD_PICTURE(h) || h0->first_field) {
  3372. if (h264_frame_start(h) < 0) {
  3373. h0->first_field = 0;
  3374. return AVERROR_INVALIDDATA;
  3375. }
  3376. } else {
  3377. release_unused_pictures(h, 0);
  3378. }
  3379. /* Some macroblocks can be accessed before they're available in case
  3380. * of lost slices, MBAFF or threading. */
  3381. if (FIELD_PICTURE(h)) {
  3382. for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
  3383. memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
  3384. } else {
  3385. memset(h->slice_table, -1,
  3386. (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
  3387. }
  3388. h0->last_slice_type = -1;
  3389. }
  3390. if (h != h0 && (ret = clone_slice(h, h0)) < 0)
  3391. return ret;
  3392. /* can't be in alloc_tables because linesize isn't known there.
  3393. * FIXME: redo bipred weight to not require extra buffer? */
  3394. for (i = 0; i < h->slice_context_count; i++)
  3395. if (h->thread_context[i]) {
  3396. ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);
  3397. if (ret < 0)
  3398. return ret;
  3399. }
  3400. h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
  3401. av_assert1(h->mb_num == h->mb_width * h->mb_height);
  3402. if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
  3403. first_mb_in_slice >= h->mb_num) {
  3404. av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  3405. return AVERROR_INVALIDDATA;
  3406. }
  3407. h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
  3408. h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
  3409. FIELD_OR_MBAFF_PICTURE(h);
  3410. if (h->picture_structure == PICT_BOTTOM_FIELD)
  3411. h->resync_mb_y = h->mb_y = h->mb_y + 1;
  3412. av_assert1(h->mb_y < h->mb_height);
  3413. if (h->picture_structure == PICT_FRAME) {
  3414. h->curr_pic_num = h->frame_num;
  3415. h->max_pic_num = 1 << h->sps.log2_max_frame_num;
  3416. } else {
  3417. h->curr_pic_num = 2 * h->frame_num + 1;
  3418. h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
  3419. }
  3420. if (h->nal_unit_type == NAL_IDR_SLICE)
  3421. get_ue_golomb(&h->gb); /* idr_pic_id */
  3422. if (h->sps.poc_type == 0) {
  3423. h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
  3424. if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
  3425. h->delta_poc_bottom = get_se_golomb(&h->gb);
  3426. }
  3427. if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
  3428. h->delta_poc[0] = get_se_golomb(&h->gb);
  3429. if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
  3430. h->delta_poc[1] = get_se_golomb(&h->gb);
  3431. }
  3432. ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);
  3433. if (h->pps.redundant_pic_cnt_present)
  3434. h->redundant_pic_count = get_ue_golomb(&h->gb);
  3435. ret = ff_set_ref_count(h);
  3436. if (ret < 0)
  3437. return ret;
  3438. if (slice_type != AV_PICTURE_TYPE_I &&
  3439. (h0->current_slice == 0 ||
  3440. slice_type != h0->last_slice_type ||
  3441. memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
  3442. ff_h264_fill_default_ref_list(h);
  3443. }
  3444. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  3445. ret = ff_h264_decode_ref_pic_list_reordering(h);
  3446. if (ret < 0) {
  3447. h->ref_count[1] = h->ref_count[0] = 0;
  3448. return ret;
  3449. }
  3450. }
  3451. if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
  3452. (h->pps.weighted_bipred_idc == 1 &&
  3453. h->slice_type_nos == AV_PICTURE_TYPE_B))
  3454. ff_pred_weight_table(h);
  3455. else if (h->pps.weighted_bipred_idc == 2 &&
  3456. h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3457. implicit_weight_table(h, -1);
  3458. } else {
  3459. h->use_weight = 0;
  3460. for (i = 0; i < 2; i++) {
  3461. h->luma_weight_flag[i] = 0;
  3462. h->chroma_weight_flag[i] = 0;
  3463. }
  3464. }
  3465. // If frame-mt is enabled, only update mmco tables for the first slice
  3466. // in a field. Subsequent slices can temporarily clobber h->mmco_index
  3467. // or h->mmco, which will cause ref list mix-ups and decoding errors
  3468. // further down the line. This may break decoding if the first slice is
  3469. // corrupt, thus we only do this if frame-mt is enabled.
  3470. if (h->nal_ref_idc) {
  3471. ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,
  3472. !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
  3473. h0->current_slice == 0);
  3474. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  3475. return AVERROR_INVALIDDATA;
  3476. }
  3477. if (FRAME_MBAFF(h)) {
  3478. ff_h264_fill_mbaff_ref_list(h);
  3479. if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3480. implicit_weight_table(h, 0);
  3481. implicit_weight_table(h, 1);
  3482. }
  3483. }
  3484. if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  3485. ff_h264_direct_dist_scale_factor(h);
  3486. ff_h264_direct_ref_list_init(h);
  3487. if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
  3488. tmp = get_ue_golomb_31(&h->gb);
  3489. if (tmp > 2) {
  3490. av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3491. return AVERROR_INVALIDDATA;
  3492. }
  3493. h->cabac_init_idc = tmp;
  3494. }
  3495. h->last_qscale_diff = 0;
  3496. tmp = h->pps.init_qp + get_se_golomb(&h->gb);
  3497. if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
  3498. av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3499. return AVERROR_INVALIDDATA;
  3500. }
  3501. h->qscale = tmp;
  3502. h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
  3503. h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
  3504. // FIXME qscale / qp ... stuff
  3505. if (h->slice_type == AV_PICTURE_TYPE_SP)
  3506. get_bits1(&h->gb); /* sp_for_switch_flag */
  3507. if (h->slice_type == AV_PICTURE_TYPE_SP ||
  3508. h->slice_type == AV_PICTURE_TYPE_SI)
  3509. get_se_golomb(&h->gb); /* slice_qs_delta */
  3510. h->deblocking_filter = 1;
  3511. h->slice_alpha_c0_offset = 52;
  3512. h->slice_beta_offset = 52;
  3513. if (h->pps.deblocking_filter_parameters_present) {
  3514. tmp = get_ue_golomb_31(&h->gb);
  3515. if (tmp > 2) {
  3516. av_log(h->avctx, AV_LOG_ERROR,
  3517. "deblocking_filter_idc %u out of range\n", tmp);
  3518. return AVERROR_INVALIDDATA;
  3519. }
  3520. h->deblocking_filter = tmp;
  3521. if (h->deblocking_filter < 2)
  3522. h->deblocking_filter ^= 1; // 1<->0
  3523. if (h->deblocking_filter) {
  3524. h->slice_alpha_c0_offset += get_se_golomb(&h->gb) << 1;
  3525. h->slice_beta_offset += get_se_golomb(&h->gb) << 1;
  3526. if (h->slice_alpha_c0_offset > 104U ||
  3527. h->slice_beta_offset > 104U) {
  3528. av_log(h->avctx, AV_LOG_ERROR,
  3529. "deblocking filter parameters %d %d out of range\n",
  3530. h->slice_alpha_c0_offset, h->slice_beta_offset);
  3531. return AVERROR_INVALIDDATA;
  3532. }
  3533. }
  3534. }
  3535. if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  3536. (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  3537. h->slice_type_nos != AV_PICTURE_TYPE_I) ||
  3538. (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  3539. h->slice_type_nos == AV_PICTURE_TYPE_B) ||
  3540. (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  3541. h->nal_ref_idc == 0))
  3542. h->deblocking_filter = 0;
  3543. if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
  3544. if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
  3545. /* Cheat slightly for speed:
  3546. * Do not bother to deblock across slices. */
  3547. h->deblocking_filter = 2;
  3548. } else {
  3549. h0->max_contexts = 1;
  3550. if (!h0->single_decode_warning) {
  3551. av_log(h->avctx, AV_LOG_INFO,
  3552. "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  3553. h0->single_decode_warning = 1;
  3554. }
  3555. if (h != h0) {
  3556. av_log(h->avctx, AV_LOG_ERROR,
  3557. "Deblocking switched inside frame.\n");
  3558. return 1;
  3559. }
  3560. }
  3561. }
  3562. h->qp_thresh = 15 + 52 -
  3563. FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
  3564. FFMAX3(0,
  3565. h->pps.chroma_qp_index_offset[0],
  3566. h->pps.chroma_qp_index_offset[1]) +
  3567. 6 * (h->sps.bit_depth_luma - 8);
  3568. h0->last_slice_type = slice_type;
  3569. memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));
  3570. h->slice_num = ++h0->current_slice;
  3571. if (h->slice_num)
  3572. h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;
  3573. if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y
  3574. && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y
  3575. && h->slice_num >= MAX_SLICES) {
  3576. //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
  3577. av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
  3578. }
  3579. for (j = 0; j < 2; j++) {
  3580. int id_list[16];
  3581. int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
  3582. for (i = 0; i < 16; i++) {
  3583. id_list[i] = 60;
  3584. if (j < h->list_count && i < h->ref_count[j] &&
  3585. h->ref_list[j][i].f.buf[0]) {
  3586. int k;
  3587. AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
  3588. for (k = 0; k < h->short_ref_count; k++)
  3589. if (h->short_ref[k]->f.buf[0]->buffer == buf) {
  3590. id_list[i] = k;
  3591. break;
  3592. }
  3593. for (k = 0; k < h->long_ref_count; k++)
  3594. if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
  3595. id_list[i] = h->short_ref_count + k;
  3596. break;
  3597. }
  3598. }
  3599. }
  3600. ref2frm[0] =
  3601. ref2frm[1] = -1;
  3602. for (i = 0; i < 16; i++)
  3603. ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);
  3604. ref2frm[18 + 0] =
  3605. ref2frm[18 + 1] = -1;
  3606. for (i = 16; i < 48; i++)
  3607. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  3608. (h->ref_list[j][i].reference & 3);
  3609. }
  3610. if (h->ref_count[0]) h->er.last_pic = &h->ref_list[0][0];
  3611. if (h->ref_count[1]) h->er.next_pic = &h->ref_list[1][0];
  3612. h->er.ref_count = h->ref_count[0];
  3613. if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
  3614. av_log(h->avctx, AV_LOG_DEBUG,
  3615. "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",
  3616. h->slice_num,
  3617. (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  3618. first_mb_in_slice,
  3619. av_get_picture_type_char(h->slice_type),
  3620. h->slice_type_fixed ? " fix" : "",
  3621. h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  3622. pps_id, h->frame_num,
  3623. h->cur_pic_ptr->field_poc[0],
  3624. h->cur_pic_ptr->field_poc[1],
  3625. h->ref_count[0], h->ref_count[1],
  3626. h->qscale,
  3627. h->deblocking_filter,
  3628. h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
  3629. h->use_weight,
  3630. h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
  3631. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  3632. }
  3633. return 0;
  3634. }
  3635. int ff_h264_get_slice_type(const H264Context *h)
  3636. {
  3637. switch (h->slice_type) {
  3638. case AV_PICTURE_TYPE_P:
  3639. return 0;
  3640. case AV_PICTURE_TYPE_B:
  3641. return 1;
  3642. case AV_PICTURE_TYPE_I:
  3643. return 2;
  3644. case AV_PICTURE_TYPE_SP:
  3645. return 3;
  3646. case AV_PICTURE_TYPE_SI:
  3647. return 4;
  3648. default:
  3649. return AVERROR_INVALIDDATA;
  3650. }
  3651. }
  3652. static av_always_inline void fill_filter_caches_inter(H264Context *h,
  3653. int mb_type, int top_xy,
  3654. int left_xy[LEFT_MBS],
  3655. int top_type,
  3656. int left_type[LEFT_MBS],
  3657. int mb_xy, int list)
  3658. {
  3659. int b_stride = h->b_stride;
  3660. int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
  3661. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  3662. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  3663. if (USES_LIST(top_type, list)) {
  3664. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  3665. const int b8_xy = 4 * top_xy + 2;
  3666. int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
  3667. AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
  3668. ref_cache[0 - 1 * 8] =
  3669. ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
  3670. ref_cache[2 - 1 * 8] =
  3671. ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
  3672. } else {
  3673. AV_ZERO128(mv_dst - 1 * 8);
  3674. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3675. }
  3676. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  3677. if (USES_LIST(left_type[LTOP], list)) {
  3678. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  3679. const int b8_xy = 4 * left_xy[LTOP] + 1;
  3680. int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
  3681. AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
  3682. AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
  3683. AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
  3684. AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
  3685. ref_cache[-1 + 0] =
  3686. ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
  3687. ref_cache[-1 + 16] =
  3688. ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
  3689. } else {
  3690. AV_ZERO32(mv_dst - 1 + 0);
  3691. AV_ZERO32(mv_dst - 1 + 8);
  3692. AV_ZERO32(mv_dst - 1 + 16);
  3693. AV_ZERO32(mv_dst - 1 + 24);
  3694. ref_cache[-1 + 0] =
  3695. ref_cache[-1 + 8] =
  3696. ref_cache[-1 + 16] =
  3697. ref_cache[-1 + 24] = LIST_NOT_USED;
  3698. }
  3699. }
  3700. }
  3701. if (!USES_LIST(mb_type, list)) {
  3702. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  3703. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3704. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3705. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3706. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3707. return;
  3708. }
  3709. {
  3710. int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
  3711. int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
  3712. uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
  3713. uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
  3714. AV_WN32A(&ref_cache[0 * 8], ref01);
  3715. AV_WN32A(&ref_cache[1 * 8], ref01);
  3716. AV_WN32A(&ref_cache[2 * 8], ref23);
  3717. AV_WN32A(&ref_cache[3 * 8], ref23);
  3718. }
  3719. {
  3720. int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
  3721. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  3722. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  3723. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  3724. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  3725. }
  3726. }
  3727. /**
  3728. *
  3729. * @return non zero if the loop filter can be skipped
  3730. */
  3731. static int fill_filter_caches(H264Context *h, int mb_type)
  3732. {
  3733. const int mb_xy = h->mb_xy;
  3734. int top_xy, left_xy[LEFT_MBS];
  3735. int top_type, left_type[LEFT_MBS];
  3736. uint8_t *nnz;
  3737. uint8_t *nnz_cache;
  3738. top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
  3739. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  3740. * stuff, I can't imagine that these complex rules are worth it. */
  3741. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  3742. if (FRAME_MBAFF(h)) {
  3743. const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
  3744. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  3745. if (h->mb_y & 1) {
  3746. if (left_mb_field_flag != curr_mb_field_flag)
  3747. left_xy[LTOP] -= h->mb_stride;
  3748. } else {
  3749. if (curr_mb_field_flag)
  3750. top_xy += h->mb_stride &
  3751. (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
  3752. if (left_mb_field_flag != curr_mb_field_flag)
  3753. left_xy[LBOT] += h->mb_stride;
  3754. }
  3755. }
  3756. h->top_mb_xy = top_xy;
  3757. h->left_mb_xy[LTOP] = left_xy[LTOP];
  3758. h->left_mb_xy[LBOT] = left_xy[LBOT];
  3759. {
  3760. /* For sufficiently low qp, filtering wouldn't do anything.
  3761. * This is a conservative estimate: could also check beta_offset
  3762. * and more accurate chroma_qp. */
  3763. int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  3764. int qp = h->cur_pic.qscale_table[mb_xy];
  3765. if (qp <= qp_thresh &&
  3766. (left_xy[LTOP] < 0 ||
  3767. ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  3768. (top_xy < 0 ||
  3769. ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  3770. if (!FRAME_MBAFF(h))
  3771. return 1;
  3772. if ((left_xy[LTOP] < 0 ||
  3773. ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  3774. (top_xy < h->mb_stride ||
  3775. ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
  3776. return 1;
  3777. }
  3778. }
  3779. top_type = h->cur_pic.mb_type[top_xy];
  3780. left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
  3781. left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
  3782. if (h->deblocking_filter == 2) {
  3783. if (h->slice_table[top_xy] != h->slice_num)
  3784. top_type = 0;
  3785. if (h->slice_table[left_xy[LBOT]] != h->slice_num)
  3786. left_type[LTOP] = left_type[LBOT] = 0;
  3787. } else {
  3788. if (h->slice_table[top_xy] == 0xFFFF)
  3789. top_type = 0;
  3790. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  3791. left_type[LTOP] = left_type[LBOT] = 0;
  3792. }
  3793. h->top_type = top_type;
  3794. h->left_type[LTOP] = left_type[LTOP];
  3795. h->left_type[LBOT] = left_type[LBOT];
  3796. if (IS_INTRA(mb_type))
  3797. return 0;
  3798. fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
  3799. top_type, left_type, mb_xy, 0);
  3800. if (h->list_count == 2)
  3801. fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
  3802. top_type, left_type, mb_xy, 1);
  3803. nnz = h->non_zero_count[mb_xy];
  3804. nnz_cache = h->non_zero_count_cache;
  3805. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  3806. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  3807. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  3808. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  3809. h->cbp = h->cbp_table[mb_xy];
  3810. if (top_type) {
  3811. nnz = h->non_zero_count[top_xy];
  3812. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  3813. }
  3814. if (left_type[LTOP]) {
  3815. nnz = h->non_zero_count[left_xy[LTOP]];
  3816. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  3817. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  3818. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  3819. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  3820. }
  3821. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  3822. * from what the loop filter needs */
  3823. if (!CABAC(h) && h->pps.transform_8x8_mode) {
  3824. if (IS_8x8DCT(top_type)) {
  3825. nnz_cache[4 + 8 * 0] =
  3826. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  3827. nnz_cache[6 + 8 * 0] =
  3828. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  3829. }
  3830. if (IS_8x8DCT(left_type[LTOP])) {
  3831. nnz_cache[3 + 8 * 1] =
  3832. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  3833. }
  3834. if (IS_8x8DCT(left_type[LBOT])) {
  3835. nnz_cache[3 + 8 * 3] =
  3836. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  3837. }
  3838. if (IS_8x8DCT(mb_type)) {
  3839. nnz_cache[scan8[0]] =
  3840. nnz_cache[scan8[1]] =
  3841. nnz_cache[scan8[2]] =
  3842. nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
  3843. nnz_cache[scan8[0 + 4]] =
  3844. nnz_cache[scan8[1 + 4]] =
  3845. nnz_cache[scan8[2 + 4]] =
  3846. nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
  3847. nnz_cache[scan8[0 + 8]] =
  3848. nnz_cache[scan8[1 + 8]] =
  3849. nnz_cache[scan8[2 + 8]] =
  3850. nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
  3851. nnz_cache[scan8[0 + 12]] =
  3852. nnz_cache[scan8[1 + 12]] =
  3853. nnz_cache[scan8[2 + 12]] =
  3854. nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
  3855. }
  3856. }
  3857. return 0;
  3858. }
  3859. static void loop_filter(H264Context *h, int start_x, int end_x)
  3860. {
  3861. uint8_t *dest_y, *dest_cb, *dest_cr;
  3862. int linesize, uvlinesize, mb_x, mb_y;
  3863. const int end_mb_y = h->mb_y + FRAME_MBAFF(h);
  3864. const int old_slice_type = h->slice_type;
  3865. const int pixel_shift = h->pixel_shift;
  3866. const int block_h = 16 >> h->chroma_y_shift;
  3867. if (h->deblocking_filter) {
  3868. for (mb_x = start_x; mb_x < end_x; mb_x++)
  3869. for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
  3870. int mb_xy, mb_type;
  3871. mb_xy = h->mb_xy = mb_x + mb_y * h->mb_stride;
  3872. h->slice_num = h->slice_table[mb_xy];
  3873. mb_type = h->cur_pic.mb_type[mb_xy];
  3874. h->list_count = h->list_counts[mb_xy];
  3875. if (FRAME_MBAFF(h))
  3876. h->mb_mbaff =
  3877. h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  3878. h->mb_x = mb_x;
  3879. h->mb_y = mb_y;
  3880. dest_y = h->cur_pic.f.data[0] +
  3881. ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
  3882. dest_cb = h->cur_pic.f.data[1] +
  3883. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  3884. mb_y * h->uvlinesize * block_h;
  3885. dest_cr = h->cur_pic.f.data[2] +
  3886. (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
  3887. mb_y * h->uvlinesize * block_h;
  3888. // FIXME simplify above
  3889. if (MB_FIELD(h)) {
  3890. linesize = h->mb_linesize = h->linesize * 2;
  3891. uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
  3892. if (mb_y & 1) { // FIXME move out of this function?
  3893. dest_y -= h->linesize * 15;
  3894. dest_cb -= h->uvlinesize * (block_h - 1);
  3895. dest_cr -= h->uvlinesize * (block_h - 1);
  3896. }
  3897. } else {
  3898. linesize = h->mb_linesize = h->linesize;
  3899. uvlinesize = h->mb_uvlinesize = h->uvlinesize;
  3900. }
  3901. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  3902. uvlinesize, 0);
  3903. if (fill_filter_caches(h, mb_type))
  3904. continue;
  3905. h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
  3906. h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
  3907. if (FRAME_MBAFF(h)) {
  3908. ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  3909. linesize, uvlinesize);
  3910. } else {
  3911. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
  3912. dest_cr, linesize, uvlinesize);
  3913. }
  3914. }
  3915. }
  3916. h->slice_type = old_slice_type;
  3917. h->mb_x = end_x;
  3918. h->mb_y = end_mb_y - FRAME_MBAFF(h);
  3919. h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
  3920. h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
  3921. }
  3922. static void predict_field_decoding_flag(H264Context *h)
  3923. {
  3924. const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
  3925. int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
  3926. h->cur_pic.mb_type[mb_xy - 1] :
  3927. (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
  3928. h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
  3929. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  3930. }
  3931. /**
  3932. * Draw edges and report progress for the last MB row.
  3933. */
  3934. static void decode_finish_row(H264Context *h)
  3935. {
  3936. int top = 16 * (h->mb_y >> FIELD_PICTURE(h));
  3937. int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
  3938. int height = 16 << FRAME_MBAFF(h);
  3939. int deblock_border = (16 + 4) << FRAME_MBAFF(h);
  3940. if (h->deblocking_filter) {
  3941. if ((top + height) >= pic_height)
  3942. height += deblock_border;
  3943. top -= deblock_border;
  3944. }
  3945. if (top >= pic_height || (top + height) < 0)
  3946. return;
  3947. height = FFMIN(height, pic_height - top);
  3948. if (top < 0) {
  3949. height = top + height;
  3950. top = 0;
  3951. }
  3952. ff_h264_draw_horiz_band(h, top, height);
  3953. if (h->droppable || h->er.error_occurred)
  3954. return;
  3955. ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
  3956. h->picture_structure == PICT_BOTTOM_FIELD);
  3957. }
  3958. static void er_add_slice(H264Context *h, int startx, int starty,
  3959. int endx, int endy, int status)
  3960. {
  3961. if (CONFIG_ERROR_RESILIENCE) {
  3962. ERContext *er = &h->er;
  3963. ff_er_add_slice(er, startx, starty, endx, endy, status);
  3964. }
  3965. }
  3966. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  3967. {
  3968. H264Context *h = *(void **)arg;
  3969. int lf_x_start = h->mb_x;
  3970. h->mb_skip_run = -1;
  3971. av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * h->linesize * ((scan8[15] - scan8[0]) >> 3));
  3972. h->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
  3973. avctx->codec_id != AV_CODEC_ID_H264 ||
  3974. (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
  3975. if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && h->er.error_status_table) {
  3976. const int start_i = av_clip(h->resync_mb_x + h->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
  3977. if (start_i) {
  3978. int prev_status = h->er.error_status_table[h->er.mb_index2xy[start_i - 1]];
  3979. prev_status &= ~ VP_START;
  3980. if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
  3981. h->er.error_occurred = 1;
  3982. }
  3983. }
  3984. if (h->pps.cabac) {
  3985. /* realign */
  3986. align_get_bits(&h->gb);
  3987. /* init cabac */
  3988. ff_init_cabac_decoder(&h->cabac,
  3989. h->gb.buffer + get_bits_count(&h->gb) / 8,
  3990. (get_bits_left(&h->gb) + 7) / 8);
  3991. ff_h264_init_cabac_states(h);
  3992. for (;;) {
  3993. // START_TIMER
  3994. int ret = ff_h264_decode_mb_cabac(h);
  3995. int eos;
  3996. // STOP_TIMER("decode_mb_cabac")
  3997. if (ret >= 0)
  3998. ff_h264_hl_decode_mb(h);
  3999. // FIXME optimal? or let mb_decode decode 16x32 ?
  4000. if (ret >= 0 && FRAME_MBAFF(h)) {
  4001. h->mb_y++;
  4002. ret = ff_h264_decode_mb_cabac(h);
  4003. if (ret >= 0)
  4004. ff_h264_hl_decode_mb(h);
  4005. h->mb_y--;
  4006. }
  4007. eos = get_cabac_terminate(&h->cabac);
  4008. if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
  4009. h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  4010. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
  4011. h->mb_y, ER_MB_END);
  4012. if (h->mb_x >= lf_x_start)
  4013. loop_filter(h, lf_x_start, h->mb_x + 1);
  4014. return 0;
  4015. }
  4016. if (h->cabac.bytestream > h->cabac.bytestream_end + 2 )
  4017. av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %td\n", h->cabac.bytestream_end - h->cabac.bytestream);
  4018. if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 4) {
  4019. av_log(h->avctx, AV_LOG_ERROR,
  4020. "error while decoding MB %d %d, bytestream (%td)\n",
  4021. h->mb_x, h->mb_y,
  4022. h->cabac.bytestream_end - h->cabac.bytestream);
  4023. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  4024. h->mb_y, ER_MB_ERROR);
  4025. return AVERROR_INVALIDDATA;
  4026. }
  4027. if (++h->mb_x >= h->mb_width) {
  4028. loop_filter(h, lf_x_start, h->mb_x);
  4029. h->mb_x = lf_x_start = 0;
  4030. decode_finish_row(h);
  4031. ++h->mb_y;
  4032. if (FIELD_OR_MBAFF_PICTURE(h)) {
  4033. ++h->mb_y;
  4034. if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
  4035. predict_field_decoding_flag(h);
  4036. }
  4037. }
  4038. if (eos || h->mb_y >= h->mb_height) {
  4039. tprintf(h->avctx, "slice end %d %d\n",
  4040. get_bits_count(&h->gb), h->gb.size_in_bits);
  4041. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
  4042. h->mb_y, ER_MB_END);
  4043. if (h->mb_x > lf_x_start)
  4044. loop_filter(h, lf_x_start, h->mb_x);
  4045. return 0;
  4046. }
  4047. }
  4048. } else {
  4049. for (;;) {
  4050. int ret = ff_h264_decode_mb_cavlc(h);
  4051. if (ret >= 0)
  4052. ff_h264_hl_decode_mb(h);
  4053. // FIXME optimal? or let mb_decode decode 16x32 ?
  4054. if (ret >= 0 && FRAME_MBAFF(h)) {
  4055. h->mb_y++;
  4056. ret = ff_h264_decode_mb_cavlc(h);
  4057. if (ret >= 0)
  4058. ff_h264_hl_decode_mb(h);
  4059. h->mb_y--;
  4060. }
  4061. if (ret < 0) {
  4062. av_log(h->avctx, AV_LOG_ERROR,
  4063. "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
  4064. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  4065. h->mb_y, ER_MB_ERROR);
  4066. return ret;
  4067. }
  4068. if (++h->mb_x >= h->mb_width) {
  4069. loop_filter(h, lf_x_start, h->mb_x);
  4070. h->mb_x = lf_x_start = 0;
  4071. decode_finish_row(h);
  4072. ++h->mb_y;
  4073. if (FIELD_OR_MBAFF_PICTURE(h)) {
  4074. ++h->mb_y;
  4075. if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
  4076. predict_field_decoding_flag(h);
  4077. }
  4078. if (h->mb_y >= h->mb_height) {
  4079. tprintf(h->avctx, "slice end %d %d\n",
  4080. get_bits_count(&h->gb), h->gb.size_in_bits);
  4081. if ( get_bits_left(&h->gb) == 0
  4082. || get_bits_left(&h->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
  4083. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  4084. h->mb_x - 1, h->mb_y,
  4085. ER_MB_END);
  4086. return 0;
  4087. } else {
  4088. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  4089. h->mb_x, h->mb_y,
  4090. ER_MB_END);
  4091. return AVERROR_INVALIDDATA;
  4092. }
  4093. }
  4094. }
  4095. if (get_bits_left(&h->gb) <= 0 && h->mb_skip_run <= 0) {
  4096. tprintf(h->avctx, "slice end %d %d\n",
  4097. get_bits_count(&h->gb), h->gb.size_in_bits);
  4098. if (get_bits_left(&h->gb) == 0) {
  4099. er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
  4100. h->mb_x - 1, h->mb_y,
  4101. ER_MB_END);
  4102. if (h->mb_x > lf_x_start)
  4103. loop_filter(h, lf_x_start, h->mb_x);
  4104. return 0;
  4105. } else {
  4106. er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
  4107. h->mb_y, ER_MB_ERROR);
  4108. return AVERROR_INVALIDDATA;
  4109. }
  4110. }
  4111. }
  4112. }
  4113. }
  4114. /**
  4115. * Call decode_slice() for each context.
  4116. *
  4117. * @param h h264 master context
  4118. * @param context_count number of contexts to execute
  4119. */
  4120. static int execute_decode_slices(H264Context *h, int context_count)
  4121. {
  4122. AVCodecContext *const avctx = h->avctx;
  4123. H264Context *hx;
  4124. int i;
  4125. av_assert0(h->mb_y < h->mb_height);
  4126. if (h->avctx->hwaccel ||
  4127. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  4128. return 0;
  4129. if (context_count == 1) {
  4130. return decode_slice(avctx, &h);
  4131. } else {
  4132. av_assert0(context_count > 0);
  4133. for (i = 1; i < context_count; i++) {
  4134. hx = h->thread_context[i];
  4135. if (CONFIG_ERROR_RESILIENCE) {
  4136. hx->er.error_count = 0;
  4137. }
  4138. hx->x264_build = h->x264_build;
  4139. }
  4140. avctx->execute(avctx, decode_slice, h->thread_context,
  4141. NULL, context_count, sizeof(void *));
  4142. /* pull back stuff from slices to master context */
  4143. hx = h->thread_context[context_count - 1];
  4144. h->mb_x = hx->mb_x;
  4145. h->mb_y = hx->mb_y;
  4146. h->droppable = hx->droppable;
  4147. h->picture_structure = hx->picture_structure;
  4148. if (CONFIG_ERROR_RESILIENCE) {
  4149. for (i = 1; i < context_count; i++)
  4150. h->er.error_count += h->thread_context[i]->er.error_count;
  4151. }
  4152. }
  4153. return 0;
  4154. }
  4155. static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
  4156. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
  4157. int parse_extradata)
  4158. {
  4159. AVCodecContext *const avctx = h->avctx;
  4160. H264Context *hx; ///< thread context
  4161. int buf_index;
  4162. int context_count;
  4163. int next_avc;
  4164. int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
  4165. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  4166. int nal_index;
  4167. int idr_cleared=0;
  4168. int first_slice = 0;
  4169. int ret = 0;
  4170. h->nal_unit_type= 0;
  4171. if(!h->slice_context_count)
  4172. h->slice_context_count= 1;
  4173. h->max_contexts = h->slice_context_count;
  4174. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
  4175. h->current_slice = 0;
  4176. if (!h->first_field)
  4177. h->cur_pic_ptr = NULL;
  4178. ff_h264_reset_sei(h);
  4179. }
  4180. if (h->nal_length_size == 4) {
  4181. if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
  4182. h->is_avc = 0;
  4183. }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
  4184. h->is_avc = 1;
  4185. }
  4186. for (; pass <= 1; pass++) {
  4187. buf_index = 0;
  4188. context_count = 0;
  4189. next_avc = h->is_avc ? 0 : buf_size;
  4190. nal_index = 0;
  4191. for (;;) {
  4192. int consumed;
  4193. int dst_length;
  4194. int bit_length;
  4195. const uint8_t *ptr;
  4196. int i, nalsize = 0;
  4197. int err;
  4198. if (buf_index >= next_avc) {
  4199. if (buf_index >= buf_size - h->nal_length_size)
  4200. break;
  4201. nalsize = 0;
  4202. for (i = 0; i < h->nal_length_size; i++)
  4203. nalsize = (nalsize << 8) | buf[buf_index++];
  4204. if (nalsize <= 0 || nalsize > buf_size - buf_index) {
  4205. av_log(h->avctx, AV_LOG_ERROR,
  4206. "AVC: nal size %d\n", nalsize);
  4207. break;
  4208. }
  4209. next_avc = buf_index + nalsize;
  4210. } else {
  4211. // start code prefix search
  4212. for (; buf_index + 3 < next_avc; buf_index++)
  4213. // This should always succeed in the first iteration.
  4214. if (buf[buf_index] == 0 &&
  4215. buf[buf_index + 1] == 0 &&
  4216. buf[buf_index + 2] == 1)
  4217. break;
  4218. if (buf_index + 3 >= buf_size) {
  4219. buf_index = buf_size;
  4220. break;
  4221. }
  4222. buf_index += 3;
  4223. if (buf_index >= next_avc)
  4224. continue;
  4225. }
  4226. hx = h->thread_context[context_count];
  4227. ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
  4228. &consumed, next_avc - buf_index);
  4229. if (ptr == NULL || dst_length < 0) {
  4230. ret = -1;
  4231. goto end;
  4232. }
  4233. i = buf_index + consumed;
  4234. if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
  4235. buf[i] == 0x00 && buf[i + 1] == 0x00 &&
  4236. buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
  4237. h->workaround_bugs |= FF_BUG_TRUNCATED;
  4238. if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
  4239. while (dst_length > 0 && ptr[dst_length - 1] == 0)
  4240. dst_length--;
  4241. bit_length = !dst_length ? 0
  4242. : (8 * dst_length -
  4243. decode_rbsp_trailing(h, ptr + dst_length - 1));
  4244. if (h->avctx->debug & FF_DEBUG_STARTCODE)
  4245. av_log(h->avctx, AV_LOG_DEBUG,
  4246. "NAL %d/%d at %d/%d length %d pass %d\n",
  4247. hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length, pass);
  4248. if (h->is_avc && (nalsize != consumed) && nalsize)
  4249. av_log(h->avctx, AV_LOG_DEBUG,
  4250. "AVC: Consumed only %d bytes instead of %d\n",
  4251. consumed, nalsize);
  4252. buf_index += consumed;
  4253. nal_index++;
  4254. if (pass == 0) {
  4255. /* packets can sometimes contain multiple PPS/SPS,
  4256. * e.g. two PAFF field pictures in one packet, or a demuxer
  4257. * which splits NALs strangely if so, when frame threading we
  4258. * can't start the next thread until we've read all of them */
  4259. switch (hx->nal_unit_type) {
  4260. case NAL_SPS:
  4261. case NAL_PPS:
  4262. nals_needed = nal_index;
  4263. break;
  4264. case NAL_DPA:
  4265. case NAL_IDR_SLICE:
  4266. case NAL_SLICE:
  4267. init_get_bits(&hx->gb, ptr, bit_length);
  4268. if (!get_ue_golomb(&hx->gb) || !first_slice)
  4269. nals_needed = nal_index;
  4270. if (!first_slice)
  4271. first_slice = hx->nal_unit_type;
  4272. }
  4273. continue;
  4274. }
  4275. if (!first_slice)
  4276. switch (hx->nal_unit_type) {
  4277. case NAL_DPA:
  4278. case NAL_IDR_SLICE:
  4279. case NAL_SLICE:
  4280. first_slice = hx->nal_unit_type;
  4281. }
  4282. if (avctx->skip_frame >= AVDISCARD_NONREF &&
  4283. h->nal_ref_idc == 0 &&
  4284. h->nal_unit_type != NAL_SEI)
  4285. continue;
  4286. again:
  4287. /* Ignore per frame NAL unit type during extradata
  4288. * parsing. Decoding slices is not possible in codec init
  4289. * with frame-mt */
  4290. if (parse_extradata) {
  4291. switch (hx->nal_unit_type) {
  4292. case NAL_IDR_SLICE:
  4293. case NAL_SLICE:
  4294. case NAL_DPA:
  4295. case NAL_DPB:
  4296. case NAL_DPC:
  4297. av_log(h->avctx, AV_LOG_WARNING,
  4298. "Ignoring NAL %d in global header/extradata\n",
  4299. hx->nal_unit_type);
  4300. // fall through to next case
  4301. case NAL_AUXILIARY_SLICE:
  4302. hx->nal_unit_type = NAL_FF_IGNORE;
  4303. }
  4304. }
  4305. err = 0;
  4306. switch (hx->nal_unit_type) {
  4307. case NAL_IDR_SLICE:
  4308. if (first_slice != NAL_IDR_SLICE) {
  4309. av_log(h->avctx, AV_LOG_ERROR,
  4310. "Invalid mix of idr and non-idr slices\n");
  4311. ret = -1;
  4312. goto end;
  4313. }
  4314. if(!idr_cleared)
  4315. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  4316. idr_cleared = 1;
  4317. case NAL_SLICE:
  4318. init_get_bits(&hx->gb, ptr, bit_length);
  4319. hx->intra_gb_ptr =
  4320. hx->inter_gb_ptr = &hx->gb;
  4321. hx->data_partitioning = 0;
  4322. if ((err = decode_slice_header(hx, h)))
  4323. break;
  4324. if (h->sei_recovery_frame_cnt >= 0) {
  4325. if (h->frame_num != h->sei_recovery_frame_cnt || hx->slice_type_nos != AV_PICTURE_TYPE_I)
  4326. h->valid_recovery_point = 1;
  4327. if ( h->recovery_frame < 0
  4328. || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt) {
  4329. h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
  4330. ((1 << h->sps.log2_max_frame_num) - 1);
  4331. if (!h->valid_recovery_point)
  4332. h->recovery_frame = h->frame_num;
  4333. }
  4334. }
  4335. h->cur_pic_ptr->f.key_frame |=
  4336. (hx->nal_unit_type == NAL_IDR_SLICE);
  4337. if (hx->nal_unit_type == NAL_IDR_SLICE ||
  4338. h->recovery_frame == h->frame_num) {
  4339. h->recovery_frame = -1;
  4340. h->cur_pic_ptr->recovered = 1;
  4341. }
  4342. // If we have an IDR, all frames after it in decoded order are
  4343. // "recovered".
  4344. if (hx->nal_unit_type == NAL_IDR_SLICE)
  4345. h->frame_recovered |= FRAME_RECOVERED_IDR;
  4346. h->frame_recovered |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
  4347. h->frame_recovered |= 3*!!(avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT);
  4348. #if 1
  4349. h->cur_pic_ptr->recovered |= h->frame_recovered;
  4350. #else
  4351. h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
  4352. #endif
  4353. if (h->current_slice == 1) {
  4354. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
  4355. decode_postinit(h, nal_index >= nals_needed);
  4356. if (h->avctx->hwaccel &&
  4357. (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
  4358. return ret;
  4359. if (CONFIG_H264_VDPAU_DECODER &&
  4360. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  4361. ff_vdpau_h264_picture_start(h);
  4362. }
  4363. if (hx->redundant_pic_count == 0 &&
  4364. (avctx->skip_frame < AVDISCARD_NONREF ||
  4365. hx->nal_ref_idc) &&
  4366. (avctx->skip_frame < AVDISCARD_BIDIR ||
  4367. hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
  4368. (avctx->skip_frame < AVDISCARD_NONKEY ||
  4369. hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
  4370. avctx->skip_frame < AVDISCARD_ALL) {
  4371. if (avctx->hwaccel) {
  4372. ret = avctx->hwaccel->decode_slice(avctx,
  4373. &buf[buf_index - consumed],
  4374. consumed);
  4375. if (ret < 0)
  4376. return ret;
  4377. } else if (CONFIG_H264_VDPAU_DECODER &&
  4378. h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
  4379. ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
  4380. start_code,
  4381. sizeof(start_code));
  4382. ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
  4383. &buf[buf_index - consumed],
  4384. consumed);
  4385. } else
  4386. context_count++;
  4387. }
  4388. break;
  4389. case NAL_DPA:
  4390. init_get_bits(&hx->gb, ptr, bit_length);
  4391. hx->intra_gb_ptr =
  4392. hx->inter_gb_ptr = NULL;
  4393. if ((err = decode_slice_header(hx, h)) < 0) {
  4394. /* make sure data_partitioning is cleared if it was set
  4395. * before, so we don't try decoding a slice without a valid
  4396. * slice header later */
  4397. h->data_partitioning = 0;
  4398. break;
  4399. }
  4400. hx->data_partitioning = 1;
  4401. break;
  4402. case NAL_DPB:
  4403. init_get_bits(&hx->intra_gb, ptr, bit_length);
  4404. hx->intra_gb_ptr = &hx->intra_gb;
  4405. break;
  4406. case NAL_DPC:
  4407. init_get_bits(&hx->inter_gb, ptr, bit_length);
  4408. hx->inter_gb_ptr = &hx->inter_gb;
  4409. av_log(h->avctx, AV_LOG_ERROR, "Partitioned H.264 support is incomplete\n");
  4410. break;
  4411. if (hx->redundant_pic_count == 0 &&
  4412. hx->intra_gb_ptr &&
  4413. hx->data_partitioning &&
  4414. h->cur_pic_ptr && h->context_initialized &&
  4415. (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
  4416. (avctx->skip_frame < AVDISCARD_BIDIR ||
  4417. hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
  4418. (avctx->skip_frame < AVDISCARD_NONKEY ||
  4419. hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
  4420. avctx->skip_frame < AVDISCARD_ALL)
  4421. context_count++;
  4422. break;
  4423. case NAL_SEI:
  4424. init_get_bits(&h->gb, ptr, bit_length);
  4425. ff_h264_decode_sei(h);
  4426. break;
  4427. case NAL_SPS:
  4428. init_get_bits(&h->gb, ptr, bit_length);
  4429. if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? nalsize : 1)) {
  4430. av_log(h->avctx, AV_LOG_DEBUG,
  4431. "SPS decoding failure, trying again with the complete NAL\n");
  4432. if (h->is_avc)
  4433. av_assert0(next_avc - buf_index + consumed == nalsize);
  4434. if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
  4435. break;
  4436. init_get_bits(&h->gb, &buf[buf_index + 1 - consumed],
  4437. 8*(next_avc - buf_index + consumed - 1));
  4438. ff_h264_decode_seq_parameter_set(h);
  4439. }
  4440. break;
  4441. case NAL_PPS:
  4442. init_get_bits(&h->gb, ptr, bit_length);
  4443. ff_h264_decode_picture_parameter_set(h, bit_length);
  4444. break;
  4445. case NAL_AUD:
  4446. case NAL_END_SEQUENCE:
  4447. case NAL_END_STREAM:
  4448. case NAL_FILLER_DATA:
  4449. case NAL_SPS_EXT:
  4450. case NAL_AUXILIARY_SLICE:
  4451. break;
  4452. case NAL_FF_IGNORE:
  4453. break;
  4454. default:
  4455. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  4456. hx->nal_unit_type, bit_length);
  4457. }
  4458. if (context_count == h->max_contexts) {
  4459. execute_decode_slices(h, context_count);
  4460. context_count = 0;
  4461. }
  4462. if (err < 0) {
  4463. av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  4464. h->ref_count[0] = h->ref_count[1] = h->list_count = 0;
  4465. } else if (err == 1) {
  4466. /* Slice could not be decoded in parallel mode, copy down
  4467. * NAL unit stuff to context 0 and restart. Note that
  4468. * rbsp_buffer is not transferred, but since we no longer
  4469. * run in parallel mode this should not be an issue. */
  4470. h->nal_unit_type = hx->nal_unit_type;
  4471. h->nal_ref_idc = hx->nal_ref_idc;
  4472. hx = h;
  4473. goto again;
  4474. }
  4475. }
  4476. }
  4477. if (context_count)
  4478. execute_decode_slices(h, context_count);
  4479. end:
  4480. /* clean up */
  4481. if (h->cur_pic_ptr && !h->droppable) {
  4482. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  4483. h->picture_structure == PICT_BOTTOM_FIELD);
  4484. }
  4485. return (ret < 0) ? ret : buf_index;
  4486. }
  4487. /**
  4488. * Return the number of bytes consumed for building the current frame.
  4489. */
  4490. static int get_consumed_bytes(int pos, int buf_size)
  4491. {
  4492. if (pos == 0)
  4493. pos = 1; // avoid infinite loops (i doubt that is needed but ...)
  4494. if (pos + 10 > buf_size)
  4495. pos = buf_size; // oops ;)
  4496. return pos;
  4497. }
  4498. static int output_frame(H264Context *h, AVFrame *dst, Picture *srcp)
  4499. {
  4500. AVFrame *src = &srcp->f;
  4501. int i;
  4502. int ret = av_frame_ref(dst, src);
  4503. if (ret < 0)
  4504. return ret;
  4505. av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);
  4506. if (!srcp->crop)
  4507. return 0;
  4508. for (i = 0; i < 3; i++) {
  4509. int hshift = (i > 0) ? h->chroma_x_shift : 0;
  4510. int vshift = (i > 0) ? h->chroma_y_shift : 0;
  4511. int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
  4512. (srcp->crop_top >> vshift) * dst->linesize[i];
  4513. dst->data[i] += off;
  4514. }
  4515. return 0;
  4516. }
  4517. static int h264_decode_frame(AVCodecContext *avctx, void *data,
  4518. int *got_frame, AVPacket *avpkt)
  4519. {
  4520. const uint8_t *buf = avpkt->data;
  4521. int buf_size = avpkt->size;
  4522. H264Context *h = avctx->priv_data;
  4523. AVFrame *pict = data;
  4524. int buf_index = 0;
  4525. Picture *out;
  4526. int i, out_idx;
  4527. int ret;
  4528. h->flags = avctx->flags;
  4529. /* end of stream, output what is still in the buffers */
  4530. if (buf_size == 0) {
  4531. out:
  4532. h->cur_pic_ptr = NULL;
  4533. h->first_field = 0;
  4534. // FIXME factorize this with the output code below
  4535. out = h->delayed_pic[0];
  4536. out_idx = 0;
  4537. for (i = 1;
  4538. h->delayed_pic[i] &&
  4539. !h->delayed_pic[i]->f.key_frame &&
  4540. !h->delayed_pic[i]->mmco_reset;
  4541. i++)
  4542. if (h->delayed_pic[i]->poc < out->poc) {
  4543. out = h->delayed_pic[i];
  4544. out_idx = i;
  4545. }
  4546. for (i = out_idx; h->delayed_pic[i]; i++)
  4547. h->delayed_pic[i] = h->delayed_pic[i + 1];
  4548. if (out) {
  4549. out->reference &= ~DELAYED_PIC_REF;
  4550. ret = output_frame(h, pict, out);
  4551. if (ret < 0)
  4552. return ret;
  4553. *got_frame = 1;
  4554. }
  4555. return buf_index;
  4556. }
  4557. if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
  4558. int cnt= buf[5]&0x1f;
  4559. const uint8_t *p= buf+6;
  4560. while(cnt--){
  4561. int nalsize= AV_RB16(p) + 2;
  4562. if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
  4563. goto not_extra;
  4564. p += nalsize;
  4565. }
  4566. cnt = *(p++);
  4567. if(!cnt)
  4568. goto not_extra;
  4569. while(cnt--){
  4570. int nalsize= AV_RB16(p) + 2;
  4571. if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
  4572. goto not_extra;
  4573. p += nalsize;
  4574. }
  4575. return ff_h264_decode_extradata(h, buf, buf_size);
  4576. }
  4577. not_extra:
  4578. buf_index = decode_nal_units(h, buf, buf_size, 0);
  4579. if (buf_index < 0)
  4580. return AVERROR_INVALIDDATA;
  4581. if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  4582. av_assert0(buf_index <= buf_size);
  4583. goto out;
  4584. }
  4585. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
  4586. if (avctx->skip_frame >= AVDISCARD_NONREF ||
  4587. buf_size >= 4 && !memcmp("Q264", buf, 4))
  4588. return buf_size;
  4589. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  4590. return AVERROR_INVALIDDATA;
  4591. }
  4592. if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
  4593. (h->mb_y >= h->mb_height && h->mb_height)) {
  4594. if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
  4595. decode_postinit(h, 1);
  4596. field_end(h, 0);
  4597. /* Wait for second field. */
  4598. *got_frame = 0;
  4599. if (h->next_output_pic && (
  4600. h->next_output_pic->recovered)) {
  4601. if (!h->next_output_pic->recovered)
  4602. h->next_output_pic->f.flags |= AV_FRAME_FLAG_CORRUPT;
  4603. ret = output_frame(h, pict, h->next_output_pic);
  4604. if (ret < 0)
  4605. return ret;
  4606. *got_frame = 1;
  4607. if (CONFIG_MPEGVIDEO) {
  4608. ff_print_debug_info2(h->avctx, h->next_output_pic, pict, h->er.mbskip_table,
  4609. &h->low_delay,
  4610. h->mb_width, h->mb_height, h->mb_stride, 1);
  4611. }
  4612. }
  4613. }
  4614. assert(pict->buf[0] || !*got_frame);
  4615. return get_consumed_bytes(buf_index, buf_size);
  4616. }
  4617. av_cold void ff_h264_free_context(H264Context *h)
  4618. {
  4619. int i;
  4620. free_tables(h, 1); // FIXME cleanup init stuff perhaps
  4621. for (i = 0; i < MAX_SPS_COUNT; i++)
  4622. av_freep(h->sps_buffers + i);
  4623. for (i = 0; i < MAX_PPS_COUNT; i++)
  4624. av_freep(h->pps_buffers + i);
  4625. }
  4626. static av_cold int h264_decode_end(AVCodecContext *avctx)
  4627. {
  4628. H264Context *h = avctx->priv_data;
  4629. ff_h264_remove_all_refs(h);
  4630. ff_h264_free_context(h);
  4631. unref_picture(h, &h->cur_pic);
  4632. return 0;
  4633. }
  4634. static const AVProfile profiles[] = {
  4635. { FF_PROFILE_H264_BASELINE, "Baseline" },
  4636. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  4637. { FF_PROFILE_H264_MAIN, "Main" },
  4638. { FF_PROFILE_H264_EXTENDED, "Extended" },
  4639. { FF_PROFILE_H264_HIGH, "High" },
  4640. { FF_PROFILE_H264_HIGH_10, "High 10" },
  4641. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  4642. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  4643. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  4644. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  4645. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  4646. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  4647. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  4648. { FF_PROFILE_UNKNOWN },
  4649. };
  4650. static const AVOption h264_options[] = {
  4651. {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
  4652. {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
  4653. {NULL}
  4654. };
  4655. static const AVClass h264_class = {
  4656. .class_name = "H264 Decoder",
  4657. .item_name = av_default_item_name,
  4658. .option = h264_options,
  4659. .version = LIBAVUTIL_VERSION_INT,
  4660. };
  4661. static const AVClass h264_vdpau_class = {
  4662. .class_name = "H264 VDPAU Decoder",
  4663. .item_name = av_default_item_name,
  4664. .option = h264_options,
  4665. .version = LIBAVUTIL_VERSION_INT,
  4666. };
  4667. AVCodec ff_h264_decoder = {
  4668. .name = "h264",
  4669. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  4670. .type = AVMEDIA_TYPE_VIDEO,
  4671. .id = AV_CODEC_ID_H264,
  4672. .priv_data_size = sizeof(H264Context),
  4673. .init = ff_h264_decode_init,
  4674. .close = h264_decode_end,
  4675. .decode = h264_decode_frame,
  4676. .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
  4677. CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
  4678. CODEC_CAP_FRAME_THREADS,
  4679. .flush = flush_dpb,
  4680. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  4681. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  4682. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  4683. .priv_class = &h264_class,
  4684. };
  4685. #if CONFIG_H264_VDPAU_DECODER
  4686. AVCodec ff_h264_vdpau_decoder = {
  4687. .name = "h264_vdpau",
  4688. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  4689. .type = AVMEDIA_TYPE_VIDEO,
  4690. .id = AV_CODEC_ID_H264,
  4691. .priv_data_size = sizeof(H264Context),
  4692. .init = ff_h264_decode_init,
  4693. .close = h264_decode_end,
  4694. .decode = h264_decode_frame,
  4695. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  4696. .flush = flush_dpb,
  4697. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
  4698. AV_PIX_FMT_NONE},
  4699. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  4700. .priv_class = &h264_vdpau_class,
  4701. };
  4702. #endif