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.

1022 lines
32KB

  1. /*
  2. * AV1 video decoder
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/pixdesc.h"
  21. #include "avcodec.h"
  22. #include "av1dec.h"
  23. #include "bytestream.h"
  24. #include "hwconfig.h"
  25. #include "internal.h"
  26. #include "profiles.h"
  27. static uint32_t inverse_recenter(int r, uint32_t v)
  28. {
  29. if (v > 2 * r)
  30. return v;
  31. else if (v & 1)
  32. return r - ((v + 1) >> 1);
  33. else
  34. return r + (v >> 1);
  35. }
  36. static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
  37. int mx, int r)
  38. {
  39. if ((r << 1) <= mx) {
  40. return inverse_recenter(r, sub_exp);
  41. } else {
  42. return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
  43. }
  44. }
  45. static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
  46. int high, int r)
  47. {
  48. int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
  49. return x + low;
  50. }
  51. static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
  52. {
  53. uint8_t primary_frame, prev_frame;
  54. uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
  55. int32_t r, prev_gm_param;
  56. primary_frame = s->raw_frame_header->primary_ref_frame;
  57. prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
  58. abs_bits = AV1_GM_ABS_ALPHA_BITS;
  59. prec_bits = AV1_GM_ALPHA_PREC_BITS;
  60. /* setup_past_independence() sets PrevGmParams to default values. We can
  61. * simply point to the current's frame gm_params as they will be initialized
  62. * with defaults at this point.
  63. */
  64. if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
  65. prev_gm_param = s->cur_frame.gm_params[ref][idx];
  66. else
  67. prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];
  68. if (idx < 2) {
  69. if (type == AV1_WARP_MODEL_TRANSLATION) {
  70. abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
  71. !s->raw_frame_header->allow_high_precision_mv;
  72. prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
  73. !s->raw_frame_header->allow_high_precision_mv;
  74. } else {
  75. abs_bits = AV1_GM_ABS_TRANS_BITS;
  76. prec_bits = AV1_GM_TRANS_PREC_BITS;
  77. }
  78. }
  79. round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
  80. prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
  81. sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
  82. mx = 1 << abs_bits;
  83. r = (prev_gm_param >> prec_diff) - sub;
  84. s->cur_frame.gm_params[ref][idx] =
  85. (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
  86. -mx, mx + 1, r) << prec_diff) + round;
  87. }
  88. /**
  89. * update gm type/params, since cbs already implemented part of this funcation,
  90. * so we don't need to full implement spec.
  91. */
  92. static void global_motion_params(AV1DecContext *s)
  93. {
  94. const AV1RawFrameHeader *header = s->raw_frame_header;
  95. int type, ref;
  96. for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
  97. s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
  98. for (int i = 0; i < 6; i++)
  99. s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
  100. 1 << AV1_WARPEDMODEL_PREC_BITS : 0;
  101. }
  102. if (header->frame_type == AV1_FRAME_KEY ||
  103. header->frame_type == AV1_FRAME_INTRA_ONLY)
  104. return;
  105. for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
  106. if (header->is_global[ref]) {
  107. if (header->is_rot_zoom[ref]) {
  108. type = AV1_WARP_MODEL_ROTZOOM;
  109. } else {
  110. type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
  111. : AV1_WARP_MODEL_AFFINE;
  112. }
  113. } else {
  114. type = AV1_WARP_MODEL_IDENTITY;
  115. }
  116. s->cur_frame.gm_type[ref] = type;
  117. if (type >= AV1_WARP_MODEL_ROTZOOM) {
  118. read_global_param(s, type, ref, 2);
  119. read_global_param(s, type, ref, 3);
  120. if (type == AV1_WARP_MODEL_AFFINE) {
  121. read_global_param(s, type, ref, 4);
  122. read_global_param(s, type, ref, 5);
  123. } else {
  124. s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
  125. s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
  126. }
  127. }
  128. if (type >= AV1_WARP_MODEL_TRANSLATION) {
  129. read_global_param(s, type, ref, 0);
  130. read_global_param(s, type, ref, 1);
  131. }
  132. }
  133. }
  134. static int get_relative_dist(const AV1RawSequenceHeader *seq,
  135. unsigned int a, unsigned int b)
  136. {
  137. unsigned int diff = a - b;
  138. unsigned int m = 1 << seq->order_hint_bits_minus_1;
  139. return (diff & (m - 1)) - (diff & m);
  140. }
  141. static void skip_mode_params(AV1DecContext *s)
  142. {
  143. const AV1RawFrameHeader *header = s->raw_frame_header;
  144. const AV1RawSequenceHeader *seq = s->raw_seq;
  145. int forward_idx, backward_idx;
  146. int forward_hint, backward_hint;
  147. int second_forward_idx, second_forward_hint;
  148. int ref_hint, dist, i;
  149. if (!header->skip_mode_present)
  150. return;
  151. forward_idx = -1;
  152. backward_idx = -1;
  153. for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
  154. ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
  155. dist = get_relative_dist(seq, ref_hint, header->order_hint);
  156. if (dist < 0) {
  157. if (forward_idx < 0 ||
  158. get_relative_dist(seq, ref_hint, forward_hint) > 0) {
  159. forward_idx = i;
  160. forward_hint = ref_hint;
  161. }
  162. } else if (dist > 0) {
  163. if (backward_idx < 0 ||
  164. get_relative_dist(seq, ref_hint, backward_hint) < 0) {
  165. backward_idx = i;
  166. backward_hint = ref_hint;
  167. }
  168. }
  169. }
  170. if (forward_idx < 0) {
  171. return;
  172. } else if (backward_idx >= 0) {
  173. s->cur_frame.skip_mode_frame_idx[0] =
  174. AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);
  175. s->cur_frame.skip_mode_frame_idx[1] =
  176. AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);
  177. return;
  178. }
  179. second_forward_idx = -1;
  180. for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
  181. ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
  182. if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {
  183. if (second_forward_idx < 0 ||
  184. get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {
  185. second_forward_idx = i;
  186. second_forward_hint = ref_hint;
  187. }
  188. }
  189. }
  190. if (second_forward_idx < 0)
  191. return;
  192. s->cur_frame.skip_mode_frame_idx[0] =
  193. AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);
  194. s->cur_frame.skip_mode_frame_idx[1] =
  195. AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);
  196. }
  197. static void coded_lossless_param(AV1DecContext *s)
  198. {
  199. const AV1RawFrameHeader *header = s->raw_frame_header;
  200. int i;
  201. if (header->delta_q_y_dc || header->delta_q_u_ac ||
  202. header->delta_q_u_dc || header->delta_q_v_ac ||
  203. header->delta_q_v_dc) {
  204. s->cur_frame.coded_lossless = 0;
  205. return;
  206. }
  207. s->cur_frame.coded_lossless = 1;
  208. for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
  209. int qindex;
  210. if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
  211. qindex = (header->base_q_idx +
  212. header->feature_value[i][AV1_SEG_LVL_ALT_Q]);
  213. } else {
  214. qindex = header->base_q_idx;
  215. }
  216. qindex = av_clip_uintp2(qindex, 8);
  217. if (qindex) {
  218. s->cur_frame.coded_lossless = 0;
  219. return;
  220. }
  221. }
  222. }
  223. static int init_tile_data(AV1DecContext *s)
  224. {
  225. int cur_tile_num =
  226. s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
  227. if (s->tile_num < cur_tile_num) {
  228. int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
  229. sizeof(TileGroupInfo));
  230. if (ret < 0) {
  231. s->tile_num = 0;
  232. return ret;
  233. }
  234. }
  235. s->tile_num = cur_tile_num;
  236. return 0;
  237. }
  238. static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
  239. {
  240. AV1DecContext *s = avctx->priv_data;
  241. GetByteContext gb;
  242. uint16_t tile_num, tile_row, tile_col;
  243. uint32_t size = 0, size_bytes = 0;
  244. bytestream2_init(&gb, tile_group->tile_data.data,
  245. tile_group->tile_data.data_size);
  246. s->tg_start = tile_group->tg_start;
  247. s->tg_end = tile_group->tg_end;
  248. for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
  249. tile_row = tile_num / s->raw_frame_header->tile_cols;
  250. tile_col = tile_num % s->raw_frame_header->tile_cols;
  251. if (tile_num == tile_group->tg_end) {
  252. s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
  253. s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
  254. s->tile_group_info[tile_num].tile_row = tile_row;
  255. s->tile_group_info[tile_num].tile_column = tile_col;
  256. return 0;
  257. }
  258. size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
  259. if (bytestream2_get_bytes_left(&gb) < size_bytes)
  260. return AVERROR_INVALIDDATA;
  261. size = 0;
  262. for (int i = 0; i < size_bytes; i++)
  263. size |= bytestream2_get_byteu(&gb) << 8 * i;
  264. if (bytestream2_get_bytes_left(&gb) <= size)
  265. return AVERROR_INVALIDDATA;
  266. size++;
  267. s->tile_group_info[tile_num].tile_size = size;
  268. s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
  269. s->tile_group_info[tile_num].tile_row = tile_row;
  270. s->tile_group_info[tile_num].tile_column = tile_col;
  271. bytestream2_skipu(&gb, size);
  272. }
  273. return 0;
  274. }
  275. static int get_pixel_format(AVCodecContext *avctx)
  276. {
  277. AV1DecContext *s = avctx->priv_data;
  278. const AV1RawSequenceHeader *seq = s->raw_seq;
  279. uint8_t bit_depth;
  280. int ret;
  281. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  282. #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
  283. CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
  284. CONFIG_AV1_NVDEC_HWACCEL + \
  285. CONFIG_AV1_VAAPI_HWACCEL)
  286. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
  287. if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
  288. bit_depth = seq->color_config.twelve_bit ? 12 : 10;
  289. else if (seq->seq_profile <= 2)
  290. bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
  291. else {
  292. av_log(avctx, AV_LOG_ERROR,
  293. "Unknown AV1 profile %d.\n", seq->seq_profile);
  294. return -1;
  295. }
  296. if (!seq->color_config.mono_chrome) {
  297. // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
  298. if (seq->color_config.subsampling_x == 0 &&
  299. seq->color_config.subsampling_y == 0) {
  300. if (bit_depth == 8)
  301. pix_fmt = AV_PIX_FMT_YUV444P;
  302. else if (bit_depth == 10)
  303. pix_fmt = AV_PIX_FMT_YUV444P10;
  304. else if (bit_depth == 12)
  305. pix_fmt = AV_PIX_FMT_YUV444P12;
  306. else
  307. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  308. } else if (seq->color_config.subsampling_x == 1 &&
  309. seq->color_config.subsampling_y == 0) {
  310. if (bit_depth == 8)
  311. pix_fmt = AV_PIX_FMT_YUV422P;
  312. else if (bit_depth == 10)
  313. pix_fmt = AV_PIX_FMT_YUV422P10;
  314. else if (bit_depth == 12)
  315. pix_fmt = AV_PIX_FMT_YUV422P12;
  316. else
  317. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  318. } else if (seq->color_config.subsampling_x == 1 &&
  319. seq->color_config.subsampling_y == 1) {
  320. if (bit_depth == 8)
  321. pix_fmt = AV_PIX_FMT_YUV420P;
  322. else if (bit_depth == 10)
  323. pix_fmt = AV_PIX_FMT_YUV420P10;
  324. else if (bit_depth == 12)
  325. pix_fmt = AV_PIX_FMT_YUV420P12;
  326. else
  327. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  328. }
  329. } else {
  330. if (seq->color_config.subsampling_x == 1 &&
  331. seq->color_config.subsampling_y == 1)
  332. pix_fmt = AV_PIX_FMT_YUV440P;
  333. else
  334. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  335. }
  336. av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
  337. av_get_pix_fmt_name(pix_fmt));
  338. if (pix_fmt == AV_PIX_FMT_NONE)
  339. return -1;
  340. s->pix_fmt = pix_fmt;
  341. switch (s->pix_fmt) {
  342. case AV_PIX_FMT_YUV420P:
  343. #if CONFIG_AV1_DXVA2_HWACCEL
  344. *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
  345. #endif
  346. #if CONFIG_AV1_D3D11VA_HWACCEL
  347. *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
  348. *fmtp++ = AV_PIX_FMT_D3D11;
  349. #endif
  350. #if CONFIG_AV1_NVDEC_HWACCEL
  351. *fmtp++ = AV_PIX_FMT_CUDA;
  352. #endif
  353. #if CONFIG_AV1_VAAPI_HWACCEL
  354. *fmtp++ = AV_PIX_FMT_VAAPI;
  355. #endif
  356. break;
  357. case AV_PIX_FMT_YUV420P10:
  358. #if CONFIG_AV1_DXVA2_HWACCEL
  359. *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
  360. #endif
  361. #if CONFIG_AV1_D3D11VA_HWACCEL
  362. *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
  363. *fmtp++ = AV_PIX_FMT_D3D11;
  364. #endif
  365. #if CONFIG_AV1_NVDEC_HWACCEL
  366. *fmtp++ = AV_PIX_FMT_CUDA;
  367. #endif
  368. #if CONFIG_AV1_VAAPI_HWACCEL
  369. *fmtp++ = AV_PIX_FMT_VAAPI;
  370. #endif
  371. break;
  372. }
  373. *fmtp++ = s->pix_fmt;
  374. *fmtp = AV_PIX_FMT_NONE;
  375. ret = ff_thread_get_format(avctx, pix_fmts);
  376. if (ret < 0)
  377. return ret;
  378. /**
  379. * check if the HW accel is inited correctly. If not, return un-implemented.
  380. * Since now the av1 decoder doesn't support native decode, if it will be
  381. * implemented in the future, need remove this check.
  382. */
  383. if (!avctx->hwaccel) {
  384. av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
  385. " hardware accelerated AV1 decoding.\n");
  386. return AVERROR(ENOSYS);
  387. }
  388. avctx->pix_fmt = ret;
  389. return 0;
  390. }
  391. static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
  392. {
  393. ff_thread_release_buffer(avctx, &f->tf);
  394. av_buffer_unref(&f->hwaccel_priv_buf);
  395. f->hwaccel_picture_private = NULL;
  396. av_buffer_unref(&f->header_ref);
  397. f->raw_frame_header = NULL;
  398. f->spatial_id = f->temporal_id = 0;
  399. memset(f->skip_mode_frame_idx, 0,
  400. 2 * sizeof(uint8_t));
  401. f->coded_lossless = 0;
  402. }
  403. static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
  404. {
  405. int ret;
  406. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  407. if (ret < 0)
  408. return ret;
  409. dst->header_ref = av_buffer_ref(src->header_ref);
  410. if (!dst->header_ref)
  411. goto fail;
  412. dst->raw_frame_header = src->raw_frame_header;
  413. if (src->hwaccel_picture_private) {
  414. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  415. if (!dst->hwaccel_priv_buf)
  416. goto fail;
  417. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  418. }
  419. dst->spatial_id = src->spatial_id;
  420. dst->temporal_id = src->temporal_id;
  421. memcpy(dst->gm_type,
  422. src->gm_type,
  423. AV1_NUM_REF_FRAMES * sizeof(uint8_t));
  424. memcpy(dst->gm_params,
  425. src->gm_params,
  426. AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
  427. memcpy(dst->skip_mode_frame_idx,
  428. src->skip_mode_frame_idx,
  429. 2 * sizeof(uint8_t));
  430. dst->coded_lossless = src->coded_lossless;
  431. return 0;
  432. fail:
  433. av1_frame_unref(avctx, dst);
  434. return AVERROR(ENOMEM);
  435. }
  436. static av_cold int av1_decode_free(AVCodecContext *avctx)
  437. {
  438. AV1DecContext *s = avctx->priv_data;
  439. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  440. av1_frame_unref(avctx, &s->ref[i]);
  441. av_frame_free(&s->ref[i].tf.f);
  442. }
  443. av1_frame_unref(avctx, &s->cur_frame);
  444. av_frame_free(&s->cur_frame.tf.f);
  445. av_buffer_unref(&s->seq_ref);
  446. av_buffer_unref(&s->header_ref);
  447. av_freep(&s->tile_group_info);
  448. ff_cbs_fragment_free(&s->current_obu);
  449. ff_cbs_close(&s->cbc);
  450. return 0;
  451. }
  452. static int set_context_with_sequence(AVCodecContext *avctx,
  453. const AV1RawSequenceHeader *seq)
  454. {
  455. int width = seq->max_frame_width_minus_1 + 1;
  456. int height = seq->max_frame_height_minus_1 + 1;
  457. avctx->profile = seq->seq_profile;
  458. avctx->level = seq->seq_level_idx[0];
  459. avctx->color_range =
  460. seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  461. avctx->color_primaries = seq->color_config.color_primaries;
  462. avctx->colorspace = seq->color_config.color_primaries;
  463. avctx->color_trc = seq->color_config.transfer_characteristics;
  464. switch (seq->color_config.chroma_sample_position) {
  465. case AV1_CSP_VERTICAL:
  466. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  467. break;
  468. case AV1_CSP_COLOCATED:
  469. avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  470. break;
  471. }
  472. if (avctx->width != width || avctx->height != height) {
  473. int ret = ff_set_dimensions(avctx, width, height);
  474. if (ret < 0)
  475. return ret;
  476. }
  477. avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
  478. if (seq->timing_info.num_units_in_display_tick &&
  479. seq->timing_info.time_scale) {
  480. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  481. seq->timing_info.num_units_in_display_tick,
  482. seq->timing_info.time_scale,
  483. INT_MAX);
  484. if (seq->timing_info.equal_picture_interval)
  485. avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
  486. }
  487. return 0;
  488. }
  489. static int update_context_with_frame_header(AVCodecContext *avctx,
  490. const AV1RawFrameHeader *header)
  491. {
  492. AVRational aspect_ratio;
  493. int width = header->frame_width_minus_1 + 1;
  494. int height = header->frame_height_minus_1 + 1;
  495. int r_width = header->render_width_minus_1 + 1;
  496. int r_height = header->render_height_minus_1 + 1;
  497. int ret;
  498. if (avctx->width != width || avctx->height != height) {
  499. ret = ff_set_dimensions(avctx, width, height);
  500. if (ret < 0)
  501. return ret;
  502. }
  503. av_reduce(&aspect_ratio.num, &aspect_ratio.den,
  504. (int64_t)height * r_width,
  505. (int64_t)width * r_height,
  506. INT_MAX);
  507. if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
  508. ret = ff_set_sar(avctx, aspect_ratio);
  509. if (ret < 0)
  510. return ret;
  511. }
  512. return 0;
  513. }
  514. static av_cold int av1_decode_init(AVCodecContext *avctx)
  515. {
  516. AV1DecContext *s = avctx->priv_data;
  517. AV1RawSequenceHeader *seq;
  518. int ret;
  519. s->avctx = avctx;
  520. s->pix_fmt = AV_PIX_FMT_NONE;
  521. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  522. s->ref[i].tf.f = av_frame_alloc();
  523. if (!s->ref[i].tf.f) {
  524. av_log(avctx, AV_LOG_ERROR,
  525. "Failed to allocate reference frame buffer %d.\n", i);
  526. return AVERROR(ENOMEM);
  527. }
  528. }
  529. s->cur_frame.tf.f = av_frame_alloc();
  530. if (!s->cur_frame.tf.f) {
  531. av_log(avctx, AV_LOG_ERROR,
  532. "Failed to allocate current frame buffer.\n");
  533. return AVERROR(ENOMEM);
  534. }
  535. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
  536. if (ret < 0)
  537. return ret;
  538. if (avctx->extradata && avctx->extradata_size) {
  539. ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
  540. avctx->extradata_size);
  541. if (ret < 0) {
  542. av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
  543. return ret;
  544. }
  545. seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
  546. if (!seq) {
  547. av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
  548. goto end;
  549. }
  550. ret = set_context_with_sequence(avctx, seq);
  551. if (ret < 0) {
  552. av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
  553. goto end;
  554. }
  555. end:
  556. ff_cbs_fragment_reset(&s->current_obu);
  557. }
  558. return ret;
  559. }
  560. static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
  561. {
  562. AV1DecContext *s = avctx->priv_data;
  563. AV1RawFrameHeader *header= s->raw_frame_header;
  564. AVFrame *frame;
  565. int ret;
  566. f->header_ref = av_buffer_ref(s->header_ref);
  567. if (!f->header_ref)
  568. return AVERROR(ENOMEM);
  569. f->raw_frame_header = s->raw_frame_header;
  570. ret = update_context_with_frame_header(avctx, header);
  571. if (ret < 0) {
  572. av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
  573. return ret;
  574. }
  575. if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
  576. return ret;
  577. frame = f->tf.f;
  578. frame->key_frame = header->frame_type == AV1_FRAME_KEY;
  579. switch (header->frame_type) {
  580. case AV1_FRAME_KEY:
  581. case AV1_FRAME_INTRA_ONLY:
  582. frame->pict_type = AV_PICTURE_TYPE_I;
  583. break;
  584. case AV1_FRAME_INTER:
  585. frame->pict_type = AV_PICTURE_TYPE_P;
  586. break;
  587. case AV1_FRAME_SWITCH:
  588. frame->pict_type = AV_PICTURE_TYPE_SP;
  589. break;
  590. }
  591. if (avctx->hwaccel) {
  592. const AVHWAccel *hwaccel = avctx->hwaccel;
  593. if (hwaccel->frame_priv_data_size) {
  594. f->hwaccel_priv_buf =
  595. av_buffer_allocz(hwaccel->frame_priv_data_size);
  596. if (!f->hwaccel_priv_buf)
  597. goto fail;
  598. f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
  599. }
  600. }
  601. return 0;
  602. fail:
  603. av1_frame_unref(avctx, f);
  604. return AVERROR(ENOMEM);
  605. }
  606. static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
  607. const AVPacket *pkt, int *got_frame)
  608. {
  609. AV1DecContext *s = avctx->priv_data;
  610. const AVFrame *srcframe = s->cur_frame.tf.f;
  611. int ret;
  612. ret = av_frame_ref(frame, srcframe);
  613. if (ret < 0)
  614. return ret;
  615. frame->pts = pkt->pts;
  616. frame->pkt_dts = pkt->dts;
  617. frame->pkt_size = pkt->size;
  618. *got_frame = 1;
  619. return 0;
  620. }
  621. static int update_reference_list(AVCodecContext *avctx)
  622. {
  623. AV1DecContext *s = avctx->priv_data;
  624. const AV1RawFrameHeader *header = s->raw_frame_header;
  625. int ret;
  626. for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
  627. if (header->refresh_frame_flags & (1 << i)) {
  628. if (s->ref[i].tf.f->buf[0])
  629. av1_frame_unref(avctx, &s->ref[i]);
  630. if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
  631. av_log(avctx, AV_LOG_ERROR,
  632. "Failed to update frame %d in reference list\n", i);
  633. return ret;
  634. }
  635. }
  636. }
  637. return 0;
  638. }
  639. static int get_current_frame(AVCodecContext *avctx)
  640. {
  641. AV1DecContext *s = avctx->priv_data;
  642. int ret;
  643. if (s->cur_frame.tf.f->buf[0])
  644. av1_frame_unref(avctx, &s->cur_frame);
  645. ret = av1_frame_alloc(avctx, &s->cur_frame);
  646. if (ret < 0) {
  647. av_log(avctx, AV_LOG_ERROR,
  648. "Failed to allocate space for current frame.\n");
  649. return ret;
  650. }
  651. ret = init_tile_data(s);
  652. if (ret < 0) {
  653. av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
  654. return ret;
  655. }
  656. global_motion_params(s);
  657. skip_mode_params(s);
  658. coded_lossless_param(s);
  659. return ret;
  660. }
  661. static int av1_decode_frame(AVCodecContext *avctx, void *frame,
  662. int *got_frame, AVPacket *pkt)
  663. {
  664. AV1DecContext *s = avctx->priv_data;
  665. AV1RawTileGroup *raw_tile_group = NULL;
  666. int ret;
  667. ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
  668. if (ret < 0) {
  669. av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
  670. goto end;
  671. }
  672. av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
  673. s->current_obu.nb_units);
  674. for (int i = 0; i < s->current_obu.nb_units; i++) {
  675. CodedBitstreamUnit *unit = &s->current_obu.units[i];
  676. AV1RawOBU *obu = unit->content;
  677. const AV1RawOBUHeader *header;
  678. if (!obu)
  679. continue;
  680. header = &obu->header;
  681. av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
  682. switch (unit->type) {
  683. case AV1_OBU_SEQUENCE_HEADER:
  684. av_buffer_unref(&s->seq_ref);
  685. s->seq_ref = av_buffer_ref(unit->content_ref);
  686. if (!s->seq_ref) {
  687. ret = AVERROR(ENOMEM);
  688. goto end;
  689. }
  690. s->raw_seq = &obu->obu.sequence_header;
  691. ret = set_context_with_sequence(avctx, s->raw_seq);
  692. if (ret < 0) {
  693. av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
  694. s->raw_seq = NULL;
  695. goto end;
  696. }
  697. if (s->pix_fmt == AV_PIX_FMT_NONE) {
  698. ret = get_pixel_format(avctx);
  699. if (ret < 0) {
  700. av_log(avctx, AV_LOG_ERROR,
  701. "Failed to get pixel format.\n");
  702. s->raw_seq = NULL;
  703. goto end;
  704. }
  705. }
  706. if (avctx->hwaccel && avctx->hwaccel->decode_params) {
  707. ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
  708. unit->data_size);
  709. if (ret < 0) {
  710. av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
  711. s->raw_seq = NULL;
  712. goto end;
  713. }
  714. }
  715. break;
  716. case AV1_OBU_REDUNDANT_FRAME_HEADER:
  717. if (s->raw_frame_header)
  718. break;
  719. // fall-through
  720. case AV1_OBU_FRAME:
  721. case AV1_OBU_FRAME_HEADER:
  722. if (!s->raw_seq) {
  723. av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
  724. ret = AVERROR_INVALIDDATA;
  725. goto end;
  726. }
  727. av_buffer_unref(&s->header_ref);
  728. s->header_ref = av_buffer_ref(unit->content_ref);
  729. if (!s->header_ref) {
  730. ret = AVERROR(ENOMEM);
  731. goto end;
  732. }
  733. if (unit->type == AV1_OBU_FRAME)
  734. s->raw_frame_header = &obu->obu.frame.header;
  735. else
  736. s->raw_frame_header = &obu->obu.frame_header;
  737. if (s->raw_frame_header->show_existing_frame) {
  738. if (s->cur_frame.tf.f->buf[0])
  739. av1_frame_unref(avctx, &s->cur_frame);
  740. ret = av1_frame_ref(avctx, &s->cur_frame,
  741. &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
  742. if (ret < 0) {
  743. av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
  744. goto end;
  745. }
  746. ret = update_reference_list(avctx);
  747. if (ret < 0) {
  748. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  749. goto end;
  750. }
  751. ret = set_output_frame(avctx, frame, pkt, got_frame);
  752. if (ret < 0)
  753. av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
  754. s->raw_frame_header = NULL;
  755. goto end;
  756. }
  757. ret = get_current_frame(avctx);
  758. if (ret < 0) {
  759. av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
  760. goto end;
  761. }
  762. s->cur_frame.spatial_id = header->spatial_id;
  763. s->cur_frame.temporal_id = header->temporal_id;
  764. if (avctx->hwaccel) {
  765. ret = avctx->hwaccel->start_frame(avctx, unit->data,
  766. unit->data_size);
  767. if (ret < 0) {
  768. av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
  769. goto end;
  770. }
  771. }
  772. if (unit->type != AV1_OBU_FRAME)
  773. break;
  774. // fall-through
  775. case AV1_OBU_TILE_GROUP:
  776. if (!s->raw_frame_header) {
  777. av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
  778. ret = AVERROR_INVALIDDATA;
  779. goto end;
  780. }
  781. if (unit->type == AV1_OBU_FRAME)
  782. raw_tile_group = &obu->obu.frame.tile_group;
  783. else
  784. raw_tile_group = &obu->obu.tile_group;
  785. ret = get_tiles_info(avctx, raw_tile_group);
  786. if (ret < 0)
  787. goto end;
  788. if (avctx->hwaccel) {
  789. ret = avctx->hwaccel->decode_slice(avctx,
  790. raw_tile_group->tile_data.data,
  791. raw_tile_group->tile_data.data_size);
  792. if (ret < 0) {
  793. av_log(avctx, AV_LOG_ERROR,
  794. "HW accel decode slice fail.\n");
  795. goto end;
  796. }
  797. }
  798. break;
  799. case AV1_OBU_TILE_LIST:
  800. case AV1_OBU_TEMPORAL_DELIMITER:
  801. case AV1_OBU_PADDING:
  802. case AV1_OBU_METADATA:
  803. break;
  804. default:
  805. av_log(avctx, AV_LOG_DEBUG,
  806. "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
  807. unit->type, unit->data_size);
  808. }
  809. if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
  810. if (avctx->hwaccel) {
  811. ret = avctx->hwaccel->end_frame(avctx);
  812. if (ret < 0) {
  813. av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
  814. goto end;
  815. }
  816. }
  817. ret = update_reference_list(avctx);
  818. if (ret < 0) {
  819. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  820. goto end;
  821. }
  822. if (s->raw_frame_header->show_frame) {
  823. ret = set_output_frame(avctx, frame, pkt, got_frame);
  824. if (ret < 0) {
  825. av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
  826. goto end;
  827. }
  828. }
  829. raw_tile_group = NULL;
  830. s->raw_frame_header = NULL;
  831. }
  832. }
  833. end:
  834. ff_cbs_fragment_reset(&s->current_obu);
  835. if (ret < 0)
  836. s->raw_frame_header = NULL;
  837. return ret;
  838. }
  839. static void av1_decode_flush(AVCodecContext *avctx)
  840. {
  841. AV1DecContext *s = avctx->priv_data;
  842. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
  843. av1_frame_unref(avctx, &s->ref[i]);
  844. av1_frame_unref(avctx, &s->cur_frame);
  845. s->raw_frame_header = NULL;
  846. s->raw_seq = NULL;
  847. ff_cbs_flush(s->cbc);
  848. }
  849. AVCodec ff_av1_decoder = {
  850. .name = "av1",
  851. .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
  852. .type = AVMEDIA_TYPE_VIDEO,
  853. .id = AV_CODEC_ID_AV1,
  854. .priv_data_size = sizeof(AV1DecContext),
  855. .init = av1_decode_init,
  856. .close = av1_decode_free,
  857. .decode = av1_decode_frame,
  858. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
  859. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  860. FF_CODEC_CAP_INIT_CLEANUP |
  861. FF_CODEC_CAP_SETS_PKT_DTS,
  862. .flush = av1_decode_flush,
  863. .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
  864. .hw_configs = (const AVCodecHWConfigInternal * []) {
  865. #if CONFIG_AV1_DXVA2_HWACCEL
  866. HWACCEL_DXVA2(av1),
  867. #endif
  868. #if CONFIG_AV1_D3D11VA_HWACCEL
  869. HWACCEL_D3D11VA(av1),
  870. #endif
  871. #if CONFIG_AV1_D3D11VA2_HWACCEL
  872. HWACCEL_D3D11VA2(av1),
  873. #endif
  874. #if CONFIG_AV1_NVDEC_HWACCEL
  875. HWACCEL_NVDEC(av1),
  876. #endif
  877. #if CONFIG_AV1_VAAPI_HWACCEL
  878. HWACCEL_VAAPI(av1),
  879. #endif
  880. NULL
  881. },
  882. };