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.

5370 lines
200KB

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