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.

986 lines
31KB

  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]].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]].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_NVDEC_HWACCEL + CONFIG_AV1_VAAPI_HWACCEL)
  283. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
  284. if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
  285. bit_depth = seq->color_config.twelve_bit ? 12 : 10;
  286. else if (seq->seq_profile <= 2)
  287. bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
  288. else {
  289. av_log(avctx, AV_LOG_ERROR,
  290. "Unknown AV1 profile %d.\n", seq->seq_profile);
  291. return -1;
  292. }
  293. if (!seq->color_config.mono_chrome) {
  294. // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
  295. if (seq->color_config.subsampling_x == 0 &&
  296. seq->color_config.subsampling_y == 0) {
  297. if (bit_depth == 8)
  298. pix_fmt = AV_PIX_FMT_YUV444P;
  299. else if (bit_depth == 10)
  300. pix_fmt = AV_PIX_FMT_YUV444P10;
  301. else if (bit_depth == 12)
  302. pix_fmt = AV_PIX_FMT_YUV444P12;
  303. else
  304. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  305. } else if (seq->color_config.subsampling_x == 1 &&
  306. seq->color_config.subsampling_y == 0) {
  307. if (bit_depth == 8)
  308. pix_fmt = AV_PIX_FMT_YUV422P;
  309. else if (bit_depth == 10)
  310. pix_fmt = AV_PIX_FMT_YUV422P10;
  311. else if (bit_depth == 12)
  312. pix_fmt = AV_PIX_FMT_YUV422P12;
  313. else
  314. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  315. } else if (seq->color_config.subsampling_x == 1 &&
  316. seq->color_config.subsampling_y == 1) {
  317. if (bit_depth == 8)
  318. pix_fmt = AV_PIX_FMT_YUV420P;
  319. else if (bit_depth == 10)
  320. pix_fmt = AV_PIX_FMT_YUV420P10;
  321. else if (bit_depth == 12)
  322. pix_fmt = AV_PIX_FMT_YUV420P12;
  323. else
  324. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  325. }
  326. } else {
  327. if (seq->color_config.subsampling_x == 1 &&
  328. seq->color_config.subsampling_y == 1)
  329. pix_fmt = AV_PIX_FMT_YUV440P;
  330. else
  331. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  332. }
  333. av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
  334. av_get_pix_fmt_name(pix_fmt));
  335. if (pix_fmt == AV_PIX_FMT_NONE)
  336. return -1;
  337. s->pix_fmt = pix_fmt;
  338. switch (s->pix_fmt) {
  339. case AV_PIX_FMT_YUV420P:
  340. #if CONFIG_AV1_NVDEC_HWACCEL
  341. *fmtp++ = AV_PIX_FMT_CUDA;
  342. #endif
  343. #if CONFIG_AV1_VAAPI_HWACCEL
  344. *fmtp++ = AV_PIX_FMT_VAAPI;
  345. #endif
  346. break;
  347. case AV_PIX_FMT_YUV420P10:
  348. #if CONFIG_AV1_NVDEC_HWACCEL
  349. *fmtp++ = AV_PIX_FMT_CUDA;
  350. #endif
  351. #if CONFIG_AV1_VAAPI_HWACCEL
  352. *fmtp++ = AV_PIX_FMT_VAAPI;
  353. #endif
  354. break;
  355. }
  356. *fmtp++ = s->pix_fmt;
  357. *fmtp = AV_PIX_FMT_NONE;
  358. ret = ff_thread_get_format(avctx, pix_fmts);
  359. if (ret < 0)
  360. return ret;
  361. /**
  362. * check if the HW accel is inited correctly. If not, return un-implemented.
  363. * Since now the av1 decoder doesn't support native decode, if it will be
  364. * implemented in the future, need remove this check.
  365. */
  366. if (!avctx->hwaccel) {
  367. av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
  368. " hardware accelerated AV1 decoding.\n");
  369. return AVERROR(ENOSYS);
  370. }
  371. avctx->pix_fmt = ret;
  372. return 0;
  373. }
  374. static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
  375. {
  376. ff_thread_release_buffer(avctx, &f->tf);
  377. av_buffer_unref(&f->hwaccel_priv_buf);
  378. f->hwaccel_picture_private = NULL;
  379. f->spatial_id = f->temporal_id = 0;
  380. f->order_hint = 0;
  381. memset(f->skip_mode_frame_idx, 0,
  382. 2 * sizeof(uint8_t));
  383. f->coded_lossless = 0;
  384. }
  385. static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
  386. {
  387. int ret;
  388. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  389. if (ret < 0)
  390. return ret;
  391. if (src->hwaccel_picture_private) {
  392. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  393. if (!dst->hwaccel_priv_buf)
  394. goto fail;
  395. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  396. }
  397. dst->spatial_id = src->spatial_id;
  398. dst->temporal_id = src->temporal_id;
  399. memcpy(dst->gm_type,
  400. src->gm_type,
  401. AV1_NUM_REF_FRAMES * sizeof(uint8_t));
  402. memcpy(dst->gm_params,
  403. src->gm_params,
  404. AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
  405. dst->order_hint = src->order_hint;
  406. memcpy(dst->skip_mode_frame_idx,
  407. src->skip_mode_frame_idx,
  408. 2 * sizeof(uint8_t));
  409. dst->coded_lossless = src->coded_lossless;
  410. return 0;
  411. fail:
  412. av1_frame_unref(avctx, dst);
  413. return AVERROR(ENOMEM);
  414. }
  415. static av_cold int av1_decode_free(AVCodecContext *avctx)
  416. {
  417. AV1DecContext *s = avctx->priv_data;
  418. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  419. av1_frame_unref(avctx, &s->ref[i]);
  420. av_frame_free(&s->ref[i].tf.f);
  421. }
  422. av1_frame_unref(avctx, &s->cur_frame);
  423. av_frame_free(&s->cur_frame.tf.f);
  424. av_buffer_unref(&s->seq_ref);
  425. av_buffer_unref(&s->header_ref);
  426. av_freep(&s->tile_group_info);
  427. ff_cbs_fragment_free(&s->current_obu);
  428. ff_cbs_close(&s->cbc);
  429. return 0;
  430. }
  431. static int set_context_with_sequence(AVCodecContext *avctx,
  432. const AV1RawSequenceHeader *seq)
  433. {
  434. int width = seq->max_frame_width_minus_1 + 1;
  435. int height = seq->max_frame_height_minus_1 + 1;
  436. avctx->profile = seq->seq_profile;
  437. avctx->level = seq->seq_level_idx[0];
  438. avctx->color_range =
  439. seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  440. avctx->color_primaries = seq->color_config.color_primaries;
  441. avctx->colorspace = seq->color_config.color_primaries;
  442. avctx->color_trc = seq->color_config.transfer_characteristics;
  443. switch (seq->color_config.chroma_sample_position) {
  444. case AV1_CSP_VERTICAL:
  445. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  446. break;
  447. case AV1_CSP_COLOCATED:
  448. avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  449. break;
  450. }
  451. if (avctx->width != width || avctx->height != height) {
  452. int ret = ff_set_dimensions(avctx, width, height);
  453. if (ret < 0)
  454. return ret;
  455. }
  456. avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
  457. if (seq->timing_info.num_units_in_display_tick &&
  458. seq->timing_info.time_scale) {
  459. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  460. seq->timing_info.num_units_in_display_tick,
  461. seq->timing_info.time_scale,
  462. INT_MAX);
  463. if (seq->timing_info.equal_picture_interval)
  464. avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
  465. }
  466. return 0;
  467. }
  468. static int update_context_with_frame_header(AVCodecContext *avctx,
  469. const AV1RawFrameHeader *header)
  470. {
  471. AVRational aspect_ratio;
  472. int width = header->frame_width_minus_1 + 1;
  473. int height = header->frame_height_minus_1 + 1;
  474. int r_width = header->render_width_minus_1 + 1;
  475. int r_height = header->render_height_minus_1 + 1;
  476. int ret;
  477. if (avctx->width != width || avctx->height != height) {
  478. ret = ff_set_dimensions(avctx, width, height);
  479. if (ret < 0)
  480. return ret;
  481. }
  482. av_reduce(&aspect_ratio.num, &aspect_ratio.den,
  483. (int64_t)height * r_width,
  484. (int64_t)width * r_height,
  485. INT_MAX);
  486. if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
  487. ret = ff_set_sar(avctx, aspect_ratio);
  488. if (ret < 0)
  489. return ret;
  490. }
  491. return 0;
  492. }
  493. static av_cold int av1_decode_init(AVCodecContext *avctx)
  494. {
  495. AV1DecContext *s = avctx->priv_data;
  496. AV1RawSequenceHeader *seq;
  497. int ret;
  498. s->avctx = avctx;
  499. s->pix_fmt = AV_PIX_FMT_NONE;
  500. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  501. s->ref[i].tf.f = av_frame_alloc();
  502. if (!s->ref[i].tf.f) {
  503. av_log(avctx, AV_LOG_ERROR,
  504. "Failed to allocate reference frame buffer %d.\n", i);
  505. return AVERROR(ENOMEM);
  506. }
  507. }
  508. s->cur_frame.tf.f = av_frame_alloc();
  509. if (!s->cur_frame.tf.f) {
  510. av_log(avctx, AV_LOG_ERROR,
  511. "Failed to allocate current frame buffer.\n");
  512. return AVERROR(ENOMEM);
  513. }
  514. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
  515. if (ret < 0)
  516. return ret;
  517. if (avctx->extradata && avctx->extradata_size) {
  518. ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
  519. avctx->extradata_size);
  520. if (ret < 0) {
  521. av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
  522. return ret;
  523. }
  524. seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
  525. if (!seq) {
  526. av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
  527. goto end;
  528. }
  529. ret = set_context_with_sequence(avctx, seq);
  530. if (ret < 0) {
  531. av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
  532. goto end;
  533. }
  534. end:
  535. ff_cbs_fragment_reset(&s->current_obu);
  536. }
  537. return ret;
  538. }
  539. static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
  540. {
  541. AV1DecContext *s = avctx->priv_data;
  542. AV1RawFrameHeader *header= s->raw_frame_header;
  543. AVFrame *frame;
  544. int ret;
  545. ret = update_context_with_frame_header(avctx, header);
  546. if (ret < 0) {
  547. av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
  548. return ret;
  549. }
  550. if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
  551. return ret;
  552. frame = f->tf.f;
  553. frame->key_frame = header->frame_type == AV1_FRAME_KEY;
  554. switch (header->frame_type) {
  555. case AV1_FRAME_KEY:
  556. case AV1_FRAME_INTRA_ONLY:
  557. frame->pict_type = AV_PICTURE_TYPE_I;
  558. break;
  559. case AV1_FRAME_INTER:
  560. frame->pict_type = AV_PICTURE_TYPE_P;
  561. break;
  562. case AV1_FRAME_SWITCH:
  563. frame->pict_type = AV_PICTURE_TYPE_SP;
  564. break;
  565. }
  566. if (avctx->hwaccel) {
  567. const AVHWAccel *hwaccel = avctx->hwaccel;
  568. if (hwaccel->frame_priv_data_size) {
  569. f->hwaccel_priv_buf =
  570. av_buffer_allocz(hwaccel->frame_priv_data_size);
  571. if (!f->hwaccel_priv_buf)
  572. goto fail;
  573. f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
  574. }
  575. }
  576. return 0;
  577. fail:
  578. av1_frame_unref(avctx, f);
  579. return AVERROR(ENOMEM);
  580. }
  581. static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
  582. const AVPacket *pkt, int *got_frame)
  583. {
  584. AV1DecContext *s = avctx->priv_data;
  585. const AVFrame *srcframe = s->cur_frame.tf.f;
  586. int ret;
  587. ret = av_frame_ref(frame, srcframe);
  588. if (ret < 0)
  589. return ret;
  590. frame->pts = pkt->pts;
  591. frame->pkt_dts = pkt->dts;
  592. frame->pkt_size = pkt->size;
  593. *got_frame = 1;
  594. return 0;
  595. }
  596. static int update_reference_list(AVCodecContext *avctx)
  597. {
  598. AV1DecContext *s = avctx->priv_data;
  599. const AV1RawFrameHeader *header = s->raw_frame_header;
  600. int ret;
  601. for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
  602. if (header->refresh_frame_flags & (1 << i)) {
  603. if (s->ref[i].tf.f->buf[0])
  604. av1_frame_unref(avctx, &s->ref[i]);
  605. if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
  606. av_log(avctx, AV_LOG_ERROR,
  607. "Failed to update frame %d in reference list\n", i);
  608. return ret;
  609. }
  610. }
  611. }
  612. return 0;
  613. }
  614. static int get_current_frame(AVCodecContext *avctx)
  615. {
  616. AV1DecContext *s = avctx->priv_data;
  617. int ret;
  618. if (s->cur_frame.tf.f->buf[0])
  619. av1_frame_unref(avctx, &s->cur_frame);
  620. ret = av1_frame_alloc(avctx, &s->cur_frame);
  621. if (ret < 0) {
  622. av_log(avctx, AV_LOG_ERROR,
  623. "Failed to allocate space for current frame.\n");
  624. return ret;
  625. }
  626. ret = init_tile_data(s);
  627. if (ret < 0) {
  628. av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
  629. return ret;
  630. }
  631. global_motion_params(s);
  632. skip_mode_params(s);
  633. coded_lossless_param(s);
  634. return ret;
  635. }
  636. static int av1_decode_frame(AVCodecContext *avctx, void *frame,
  637. int *got_frame, AVPacket *pkt)
  638. {
  639. AV1DecContext *s = avctx->priv_data;
  640. AV1RawTileGroup *raw_tile_group = NULL;
  641. int ret;
  642. ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
  643. if (ret < 0) {
  644. av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
  645. goto end;
  646. }
  647. av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
  648. s->current_obu.nb_units);
  649. for (int i = 0; i < s->current_obu.nb_units; i++) {
  650. CodedBitstreamUnit *unit = &s->current_obu.units[i];
  651. AV1RawOBU *obu = unit->content;
  652. const AV1RawOBUHeader *header;
  653. if (!obu)
  654. continue;
  655. header = &obu->header;
  656. av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
  657. switch (unit->type) {
  658. case AV1_OBU_SEQUENCE_HEADER:
  659. av_buffer_unref(&s->seq_ref);
  660. s->seq_ref = av_buffer_ref(unit->content_ref);
  661. if (!s->seq_ref) {
  662. ret = AVERROR(ENOMEM);
  663. goto end;
  664. }
  665. s->raw_seq = &obu->obu.sequence_header;
  666. ret = set_context_with_sequence(avctx, s->raw_seq);
  667. if (ret < 0) {
  668. av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
  669. s->raw_seq = NULL;
  670. goto end;
  671. }
  672. if (s->pix_fmt == AV_PIX_FMT_NONE) {
  673. ret = get_pixel_format(avctx);
  674. if (ret < 0) {
  675. av_log(avctx, AV_LOG_ERROR,
  676. "Failed to get pixel format.\n");
  677. s->raw_seq = NULL;
  678. goto end;
  679. }
  680. }
  681. if (avctx->hwaccel && avctx->hwaccel->decode_params) {
  682. ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
  683. unit->data_size);
  684. if (ret < 0) {
  685. av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
  686. s->raw_seq = NULL;
  687. goto end;
  688. }
  689. }
  690. break;
  691. case AV1_OBU_REDUNDANT_FRAME_HEADER:
  692. if (s->raw_frame_header)
  693. break;
  694. // fall-through
  695. case AV1_OBU_FRAME:
  696. case AV1_OBU_FRAME_HEADER:
  697. if (!s->raw_seq) {
  698. av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
  699. ret = AVERROR_INVALIDDATA;
  700. goto end;
  701. }
  702. av_buffer_unref(&s->header_ref);
  703. s->header_ref = av_buffer_ref(unit->content_ref);
  704. if (!s->header_ref) {
  705. ret = AVERROR(ENOMEM);
  706. goto end;
  707. }
  708. if (unit->type == AV1_OBU_FRAME)
  709. s->raw_frame_header = &obu->obu.frame.header;
  710. else
  711. s->raw_frame_header = &obu->obu.frame_header;
  712. if (s->raw_frame_header->show_existing_frame) {
  713. if (s->cur_frame.tf.f->buf[0])
  714. av1_frame_unref(avctx, &s->cur_frame);
  715. ret = av1_frame_ref(avctx, &s->cur_frame,
  716. &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
  717. if (ret < 0) {
  718. av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
  719. goto end;
  720. }
  721. ret = update_reference_list(avctx);
  722. if (ret < 0) {
  723. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  724. goto end;
  725. }
  726. ret = set_output_frame(avctx, frame, pkt, got_frame);
  727. if (ret < 0)
  728. av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
  729. s->raw_frame_header = NULL;
  730. goto end;
  731. }
  732. ret = get_current_frame(avctx);
  733. if (ret < 0) {
  734. av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
  735. goto end;
  736. }
  737. s->cur_frame.spatial_id = header->spatial_id;
  738. s->cur_frame.temporal_id = header->temporal_id;
  739. s->cur_frame.order_hint = s->raw_frame_header->order_hint;
  740. if (avctx->hwaccel) {
  741. ret = avctx->hwaccel->start_frame(avctx, unit->data,
  742. unit->data_size);
  743. if (ret < 0) {
  744. av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
  745. goto end;
  746. }
  747. }
  748. if (unit->type != AV1_OBU_FRAME)
  749. break;
  750. // fall-through
  751. case AV1_OBU_TILE_GROUP:
  752. if (!s->raw_frame_header) {
  753. av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
  754. ret = AVERROR_INVALIDDATA;
  755. goto end;
  756. }
  757. if (unit->type == AV1_OBU_FRAME)
  758. raw_tile_group = &obu->obu.frame.tile_group;
  759. else
  760. raw_tile_group = &obu->obu.tile_group;
  761. ret = get_tiles_info(avctx, raw_tile_group);
  762. if (ret < 0)
  763. goto end;
  764. if (avctx->hwaccel) {
  765. ret = avctx->hwaccel->decode_slice(avctx,
  766. raw_tile_group->tile_data.data,
  767. raw_tile_group->tile_data.data_size);
  768. if (ret < 0) {
  769. av_log(avctx, AV_LOG_ERROR,
  770. "HW accel decode slice fail.\n");
  771. goto end;
  772. }
  773. }
  774. break;
  775. case AV1_OBU_TILE_LIST:
  776. case AV1_OBU_TEMPORAL_DELIMITER:
  777. case AV1_OBU_PADDING:
  778. case AV1_OBU_METADATA:
  779. break;
  780. default:
  781. av_log(avctx, AV_LOG_DEBUG,
  782. "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
  783. unit->type, unit->data_size);
  784. }
  785. if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
  786. if (avctx->hwaccel) {
  787. ret = avctx->hwaccel->end_frame(avctx);
  788. if (ret < 0) {
  789. av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
  790. goto end;
  791. }
  792. }
  793. ret = update_reference_list(avctx);
  794. if (ret < 0) {
  795. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  796. goto end;
  797. }
  798. if (s->raw_frame_header->show_frame) {
  799. ret = set_output_frame(avctx, frame, pkt, got_frame);
  800. if (ret < 0) {
  801. av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
  802. goto end;
  803. }
  804. }
  805. raw_tile_group = NULL;
  806. s->raw_frame_header = NULL;
  807. }
  808. }
  809. end:
  810. ff_cbs_fragment_reset(&s->current_obu);
  811. if (ret < 0)
  812. s->raw_frame_header = NULL;
  813. return ret;
  814. }
  815. static void av1_decode_flush(AVCodecContext *avctx)
  816. {
  817. AV1DecContext *s = avctx->priv_data;
  818. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
  819. av1_frame_unref(avctx, &s->ref[i]);
  820. av1_frame_unref(avctx, &s->cur_frame);
  821. s->raw_frame_header = NULL;
  822. s->raw_seq = NULL;
  823. ff_cbs_flush(s->cbc);
  824. }
  825. AVCodec ff_av1_decoder = {
  826. .name = "av1",
  827. .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
  828. .type = AVMEDIA_TYPE_VIDEO,
  829. .id = AV_CODEC_ID_AV1,
  830. .priv_data_size = sizeof(AV1DecContext),
  831. .init = av1_decode_init,
  832. .close = av1_decode_free,
  833. .decode = av1_decode_frame,
  834. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
  835. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  836. FF_CODEC_CAP_INIT_CLEANUP |
  837. FF_CODEC_CAP_SETS_PKT_DTS,
  838. .flush = av1_decode_flush,
  839. .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
  840. .hw_configs = (const AVCodecHWConfigInternal * []) {
  841. #if CONFIG_AV1_NVDEC_HWACCEL
  842. HWACCEL_NVDEC(av1),
  843. #endif
  844. #if CONFIG_AV1_VAAPI_HWACCEL
  845. HWACCEL_VAAPI(av1),
  846. #endif
  847. NULL
  848. },
  849. };