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.

861 lines
27KB

  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 "get_bits.h"
  24. #include "hwconfig.h"
  25. #include "internal.h"
  26. #include "profiles.h"
  27. static void setup_past_independence(AV1Frame *f)
  28. {
  29. f->loop_filter_delta_enabled = 1;
  30. f->loop_filter_ref_deltas[AV1_REF_FRAME_INTRA] = 1;
  31. f->loop_filter_ref_deltas[AV1_REF_FRAME_LAST] = 0;
  32. f->loop_filter_ref_deltas[AV1_REF_FRAME_LAST2] = 0;
  33. f->loop_filter_ref_deltas[AV1_REF_FRAME_LAST3] = 0;
  34. f->loop_filter_ref_deltas[AV1_REF_FRAME_GOLDEN] = -1;
  35. f->loop_filter_ref_deltas[AV1_REF_FRAME_BWDREF] = 0;
  36. f->loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF2] = -1;
  37. f->loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF] = -1;
  38. f->loop_filter_mode_deltas[0] = 0;
  39. f->loop_filter_mode_deltas[1] = 0;
  40. }
  41. static void load_previous_and_update(AV1DecContext *s)
  42. {
  43. uint8_t primary_frame, prev_frame;
  44. primary_frame = s->raw_frame_header->primary_ref_frame;
  45. prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
  46. memcpy(s->cur_frame.loop_filter_ref_deltas,
  47. s->ref[prev_frame].loop_filter_ref_deltas,
  48. AV1_NUM_REF_FRAMES * sizeof(int8_t));
  49. memcpy(s->cur_frame.loop_filter_mode_deltas,
  50. s->ref[prev_frame].loop_filter_mode_deltas,
  51. 2 * sizeof(int8_t));
  52. if (s->raw_frame_header->loop_filter_delta_update) {
  53. for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
  54. if (s->raw_frame_header->update_ref_delta[i])
  55. s->cur_frame.loop_filter_ref_deltas[i] =
  56. s->raw_frame_header->loop_filter_ref_deltas[i];
  57. }
  58. for (int i = 0; i < 2; i++) {
  59. if (s->raw_frame_header->update_mode_delta[i])
  60. s->cur_frame.loop_filter_mode_deltas[i] =
  61. s->raw_frame_header->loop_filter_mode_deltas[i];
  62. }
  63. }
  64. s->cur_frame.loop_filter_delta_enabled =
  65. s->raw_frame_header->loop_filter_delta_enabled;
  66. }
  67. static uint32_t inverse_recenter(int r, uint32_t v)
  68. {
  69. if (v > 2 * r)
  70. return v;
  71. else if (v & 1)
  72. return r - ((v + 1) >> 1);
  73. else
  74. return r + (v >> 1);
  75. }
  76. static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
  77. int mx, int r)
  78. {
  79. if ((r << 1) <= mx) {
  80. return inverse_recenter(r, sub_exp);
  81. } else {
  82. return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
  83. }
  84. }
  85. static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
  86. int high, int r)
  87. {
  88. int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
  89. return x + low;
  90. }
  91. static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
  92. {
  93. uint8_t primary_frame, prev_frame;
  94. uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
  95. int32_t r;
  96. primary_frame = s->raw_frame_header->primary_ref_frame;
  97. prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
  98. abs_bits = AV1_GM_ABS_ALPHA_BITS;
  99. prec_bits = AV1_GM_ALPHA_PREC_BITS;
  100. if (idx < 2) {
  101. if (type == AV1_WARP_MODEL_TRANSLATION) {
  102. abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
  103. !s->raw_frame_header->allow_high_precision_mv;
  104. prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
  105. !s->raw_frame_header->allow_high_precision_mv;
  106. } else {
  107. abs_bits = AV1_GM_ABS_TRANS_BITS;
  108. prec_bits = AV1_GM_TRANS_PREC_BITS;
  109. }
  110. }
  111. round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
  112. prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
  113. sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
  114. mx = 1 << abs_bits;
  115. r = (s->ref[prev_frame].gm_params[ref][idx] >> prec_diff) - sub;
  116. s->cur_frame.gm_params[ref][idx] =
  117. (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
  118. -mx, mx + 1, r) << prec_diff) + round;
  119. }
  120. /**
  121. * update gm type/params, since cbs already implemented part of this funcation,
  122. * so we don't need to full implement spec.
  123. */
  124. static void global_motion_params(AV1DecContext *s)
  125. {
  126. const AV1RawFrameHeader *header = s->raw_frame_header;
  127. int type, ref;
  128. for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
  129. s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
  130. for (int i = 0; i < 6; i++)
  131. s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
  132. 1 << AV1_WARPEDMODEL_PREC_BITS : 0;
  133. }
  134. if (header->frame_type == AV1_FRAME_KEY ||
  135. header->frame_type == AV1_FRAME_INTRA_ONLY)
  136. return;
  137. for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
  138. if (header->is_global[ref]) {
  139. if (header->is_rot_zoom[ref]) {
  140. type = AV1_WARP_MODEL_ROTZOOM;
  141. } else {
  142. type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
  143. : AV1_WARP_MODEL_AFFINE;
  144. }
  145. } else {
  146. type = AV1_WARP_MODEL_IDENTITY;
  147. }
  148. s->cur_frame.gm_type[ref] = type;
  149. if (type >= AV1_WARP_MODEL_ROTZOOM) {
  150. read_global_param(s, type, ref, 2);
  151. read_global_param(s, type, ref, 3);
  152. if (type == AV1_WARP_MODEL_AFFINE) {
  153. read_global_param(s, type, ref, 4);
  154. read_global_param(s, type, ref, 5);
  155. } else {
  156. s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
  157. s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
  158. }
  159. }
  160. if (type >= AV1_WARP_MODEL_TRANSLATION) {
  161. read_global_param(s, type, ref, 0);
  162. read_global_param(s, type, ref, 1);
  163. }
  164. }
  165. }
  166. static int init_tile_data(AV1DecContext *s)
  167. {
  168. int cur_tile_num =
  169. s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
  170. if (s->tile_num < cur_tile_num) {
  171. int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
  172. sizeof(TileGroupInfo));
  173. if (ret < 0) {
  174. s->tile_num = 0;
  175. return ret;
  176. }
  177. }
  178. s->tile_num = cur_tile_num;
  179. return 0;
  180. }
  181. static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
  182. {
  183. AV1DecContext *s = avctx->priv_data;
  184. GetBitContext gb;
  185. uint16_t tile_num, tile_row, tile_col;
  186. uint32_t size = 0, size_bytes = 0, offset = 0;
  187. int ret;
  188. if ((ret = init_get_bits8(&gb,
  189. tile_group->tile_data.data,
  190. tile_group->tile_data.data_size)) < 0) {
  191. av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader.\n");
  192. return ret;
  193. }
  194. s->tg_start = tile_group->tg_start;
  195. s->tg_end = tile_group->tg_end;
  196. for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
  197. tile_row = tile_num / s->raw_frame_header->tile_cols;
  198. tile_col = tile_num % s->raw_frame_header->tile_cols;
  199. if (tile_num == tile_group->tg_end) {
  200. s->tile_group_info[tile_num].tile_size = get_bits_left(&gb) / 8;
  201. s->tile_group_info[tile_num].tile_offset = offset;
  202. s->tile_group_info[tile_num].tile_row = tile_row;
  203. s->tile_group_info[tile_num].tile_column = tile_col;
  204. return 0;
  205. }
  206. size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
  207. size = get_bits_le(&gb, size_bytes * 8) + 1;
  208. skip_bits(&gb, size * 8);
  209. offset += size_bytes;
  210. s->tile_group_info[tile_num].tile_size = size;
  211. s->tile_group_info[tile_num].tile_offset = offset;
  212. s->tile_group_info[tile_num].tile_row = tile_row;
  213. s->tile_group_info[tile_num].tile_column = tile_col;
  214. offset += size;
  215. }
  216. return 0;
  217. }
  218. static int get_pixel_format(AVCodecContext *avctx)
  219. {
  220. AV1DecContext *s = avctx->priv_data;
  221. const AV1RawSequenceHeader *seq = s->raw_seq;
  222. uint8_t bit_depth;
  223. int ret;
  224. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  225. #define HWACCEL_MAX (0)
  226. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 1], *fmtp = pix_fmts;
  227. /**
  228. * check if the HW accel is inited correctly. If not, return un-implemented.
  229. * Since now the av1 decoder doesn't support native decode, if it will be
  230. * implemented in the future, need remove this check.
  231. */
  232. if (!avctx->hwaccel) {
  233. av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
  234. " hardware accelerated AV1 decoding.\n");
  235. return AVERROR(ENOSYS);
  236. }
  237. if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
  238. bit_depth = seq->color_config.twelve_bit ? 12 : 10;
  239. else if (seq->seq_profile <= 2)
  240. bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
  241. else {
  242. av_log(avctx, AV_LOG_ERROR,
  243. "Unknown AV1 profile %d.\n", seq->seq_profile);
  244. return -1;
  245. }
  246. if (!seq->color_config.mono_chrome) {
  247. // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
  248. if (seq->color_config.subsampling_x == 0 &&
  249. seq->color_config.subsampling_y == 0) {
  250. if (bit_depth == 8)
  251. pix_fmt = AV_PIX_FMT_YUV444P;
  252. else if (bit_depth == 10)
  253. pix_fmt = AV_PIX_FMT_YUV444P10;
  254. else if (bit_depth == 12)
  255. pix_fmt = AV_PIX_FMT_YUV444P12;
  256. else
  257. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  258. } else if (seq->color_config.subsampling_x == 1 &&
  259. seq->color_config.subsampling_y == 0) {
  260. if (bit_depth == 8)
  261. pix_fmt = AV_PIX_FMT_YUV422P;
  262. else if (bit_depth == 10)
  263. pix_fmt = AV_PIX_FMT_YUV422P10;
  264. else if (bit_depth == 12)
  265. pix_fmt = AV_PIX_FMT_YUV422P12;
  266. else
  267. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  268. } else if (seq->color_config.subsampling_x == 1 &&
  269. seq->color_config.subsampling_y == 1) {
  270. if (bit_depth == 8)
  271. pix_fmt = AV_PIX_FMT_YUV420P;
  272. else if (bit_depth == 10)
  273. pix_fmt = AV_PIX_FMT_YUV420P10;
  274. else if (bit_depth == 12)
  275. pix_fmt = AV_PIX_FMT_YUV420P12;
  276. else
  277. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  278. }
  279. } else {
  280. if (seq->color_config.subsampling_x == 1 &&
  281. seq->color_config.subsampling_y == 1)
  282. pix_fmt = AV_PIX_FMT_YUV440P;
  283. else
  284. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  285. }
  286. av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
  287. av_get_pix_fmt_name(pix_fmt));
  288. if (pix_fmt == AV_PIX_FMT_NONE)
  289. return -1;
  290. s->pix_fmt = pix_fmt;
  291. *fmtp = AV_PIX_FMT_NONE;
  292. avctx->sw_pix_fmt = s->pix_fmt;
  293. ret = ff_thread_get_format(avctx, pix_fmts);
  294. if (ret < 0)
  295. return ret;
  296. avctx->pix_fmt = ret;
  297. return 0;
  298. }
  299. static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
  300. {
  301. ff_thread_release_buffer(avctx, &f->tf);
  302. av_buffer_unref(&f->hwaccel_priv_buf);
  303. f->hwaccel_picture_private = NULL;
  304. }
  305. static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
  306. {
  307. int ret;
  308. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  309. if (ret < 0)
  310. return ret;
  311. if (src->hwaccel_picture_private) {
  312. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  313. if (!dst->hwaccel_priv_buf)
  314. goto fail;
  315. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  316. }
  317. dst->loop_filter_delta_enabled = src->loop_filter_delta_enabled;
  318. memcpy(dst->loop_filter_ref_deltas,
  319. src->loop_filter_ref_deltas,
  320. AV1_NUM_REF_FRAMES * sizeof(int8_t));
  321. memcpy(dst->loop_filter_mode_deltas,
  322. src->loop_filter_mode_deltas,
  323. 2 * sizeof(int8_t));
  324. memcpy(dst->gm_type,
  325. src->gm_type,
  326. AV1_NUM_REF_FRAMES * sizeof(uint8_t));
  327. memcpy(dst->gm_params,
  328. src->gm_params,
  329. AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
  330. return 0;
  331. fail:
  332. av1_frame_unref(avctx, dst);
  333. return AVERROR(ENOMEM);
  334. }
  335. static av_cold int av1_decode_free(AVCodecContext *avctx)
  336. {
  337. AV1DecContext *s = avctx->priv_data;
  338. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  339. if (s->ref[i].tf.f->buf[0])
  340. av1_frame_unref(avctx, &s->ref[i]);
  341. av_frame_free(&s->ref[i].tf.f);
  342. }
  343. if (s->cur_frame.tf.f->buf[0])
  344. av1_frame_unref(avctx, &s->cur_frame);
  345. av_frame_free(&s->cur_frame.tf.f);
  346. av_buffer_unref(&s->seq_ref);
  347. av_buffer_unref(&s->header_ref);
  348. av_freep(&s->tile_group_info);
  349. ff_cbs_fragment_free(&s->current_obu);
  350. ff_cbs_close(&s->cbc);
  351. return 0;
  352. }
  353. static int set_context_with_sequence(AVCodecContext *avctx,
  354. const AV1RawSequenceHeader *seq)
  355. {
  356. avctx->profile = seq->seq_profile;
  357. avctx->level = seq->seq_level_idx[0];
  358. avctx->color_range =
  359. seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  360. avctx->color_primaries = seq->color_config.color_primaries;
  361. avctx->colorspace = seq->color_config.color_primaries;
  362. avctx->color_trc = seq->color_config.transfer_characteristics;
  363. switch (seq->color_config.chroma_sample_position) {
  364. case AV1_CSP_VERTICAL:
  365. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  366. break;
  367. case AV1_CSP_COLOCATED:
  368. avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  369. break;
  370. }
  371. if (seq->timing_info.num_units_in_display_tick &&
  372. seq->timing_info.time_scale) {
  373. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  374. seq->timing_info.num_units_in_display_tick,
  375. seq->timing_info.time_scale,
  376. INT_MAX);
  377. if (seq->timing_info.equal_picture_interval)
  378. avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
  379. }
  380. return 0;
  381. }
  382. static int update_context_with_frame_header(AVCodecContext *avctx,
  383. const AV1RawFrameHeader *header)
  384. {
  385. AVRational aspect_ratio;
  386. int width = header->frame_width_minus_1 + 1;
  387. int height = header->frame_height_minus_1 + 1;
  388. int r_width = header->render_width_minus_1 + 1;
  389. int r_height = header->render_height_minus_1 + 1;
  390. int ret;
  391. if (avctx->width != width || avctx->height != height) {
  392. ret = ff_set_dimensions(avctx, width, height);
  393. if (ret < 0)
  394. return ret;
  395. }
  396. av_reduce(&aspect_ratio.num, &aspect_ratio.den,
  397. (int64_t)height * r_width,
  398. (int64_t)width * r_height,
  399. INT_MAX);
  400. if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
  401. ret = ff_set_sar(avctx, aspect_ratio);
  402. if (ret < 0)
  403. return ret;
  404. }
  405. return 0;
  406. }
  407. static av_cold int av1_decode_init(AVCodecContext *avctx)
  408. {
  409. AV1DecContext *s = avctx->priv_data;
  410. AV1RawSequenceHeader *seq;
  411. int ret;
  412. s->avctx = avctx;
  413. s->pix_fmt = AV_PIX_FMT_NONE;
  414. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  415. s->ref[i].tf.f = av_frame_alloc();
  416. if (!s->ref[i].tf.f) {
  417. av1_decode_free(avctx);
  418. av_log(avctx, AV_LOG_ERROR,
  419. "Failed to allocate reference frame buffer %d.\n", i);
  420. return AVERROR(ENOMEM);
  421. }
  422. }
  423. s->cur_frame.tf.f = av_frame_alloc();
  424. if (!s->cur_frame.tf.f) {
  425. av1_decode_free(avctx);
  426. av_log(avctx, AV_LOG_ERROR,
  427. "Failed to allocate current frame buffer.\n");
  428. return AVERROR(ENOMEM);
  429. }
  430. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
  431. if (ret < 0)
  432. return ret;
  433. if (avctx->extradata && avctx->extradata_size) {
  434. ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
  435. avctx->extradata_size);
  436. if (ret < 0) {
  437. av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
  438. goto end;
  439. }
  440. seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
  441. if (!seq) {
  442. av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
  443. goto end;
  444. }
  445. ret = set_context_with_sequence(avctx, seq);
  446. if (ret < 0) {
  447. av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
  448. goto end;
  449. }
  450. end:
  451. ff_cbs_fragment_reset(&s->current_obu);
  452. }
  453. return ret;
  454. }
  455. static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
  456. {
  457. AV1DecContext *s = avctx->priv_data;
  458. AV1RawFrameHeader *header= s->raw_frame_header;
  459. AVFrame *frame;
  460. int ret;
  461. ret = update_context_with_frame_header(avctx, header);
  462. if (ret < 0) {
  463. av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
  464. return ret;
  465. }
  466. if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
  467. return ret;
  468. frame = f->tf.f;
  469. frame->key_frame = header->frame_type == AV1_FRAME_KEY;
  470. switch (header->frame_type) {
  471. case AV1_FRAME_KEY:
  472. case AV1_FRAME_INTRA_ONLY:
  473. frame->pict_type = AV_PICTURE_TYPE_I;
  474. break;
  475. case AV1_FRAME_INTER:
  476. frame->pict_type = AV_PICTURE_TYPE_P;
  477. break;
  478. case AV1_FRAME_SWITCH:
  479. frame->pict_type = AV_PICTURE_TYPE_SP;
  480. break;
  481. }
  482. if (avctx->hwaccel) {
  483. const AVHWAccel *hwaccel = avctx->hwaccel;
  484. if (hwaccel->frame_priv_data_size) {
  485. f->hwaccel_priv_buf =
  486. av_buffer_allocz(hwaccel->frame_priv_data_size);
  487. if (!f->hwaccel_priv_buf)
  488. goto fail;
  489. f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
  490. }
  491. }
  492. return 0;
  493. fail:
  494. av1_frame_unref(avctx, f);
  495. return AVERROR(ENOMEM);
  496. }
  497. static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
  498. const AVPacket *pkt, int *got_frame)
  499. {
  500. AV1DecContext *s = avctx->priv_data;
  501. const AV1RawFrameHeader *header = s->raw_frame_header;
  502. const AVFrame *srcframe;
  503. int ret;
  504. if (header->show_existing_frame)
  505. srcframe = s->ref[header->frame_to_show_map_idx].tf.f;
  506. else
  507. srcframe = s->cur_frame.tf.f;
  508. ret = av_frame_ref(frame, srcframe);
  509. if (ret < 0)
  510. return ret;
  511. frame->pts = pkt->pts;
  512. frame->pkt_dts = pkt->dts;
  513. frame->pkt_size = pkt->size;
  514. *got_frame = 1;
  515. return 0;
  516. }
  517. static int update_reference_list(AVCodecContext *avctx)
  518. {
  519. AV1DecContext *s = avctx->priv_data;
  520. const AV1RawFrameHeader *header = s->raw_frame_header;
  521. int ret;
  522. for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
  523. if (header->refresh_frame_flags & (1 << i)) {
  524. if (s->ref[i].tf.f->buf[0])
  525. av1_frame_unref(avctx, &s->ref[i]);
  526. if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
  527. av_log(avctx, AV_LOG_ERROR,
  528. "Failed to update frame %d in reference list\n", i);
  529. return ret;
  530. }
  531. }
  532. }
  533. return 0;
  534. }
  535. static int get_current_frame(AVCodecContext *avctx)
  536. {
  537. AV1DecContext *s = avctx->priv_data;
  538. int ret;
  539. if (s->cur_frame.tf.f->buf[0])
  540. av1_frame_unref(avctx, &s->cur_frame);
  541. ret = av1_frame_alloc(avctx, &s->cur_frame);
  542. if (ret < 0) {
  543. av_log(avctx, AV_LOG_ERROR,
  544. "Failed to allocate space for current frame.\n");
  545. return ret;
  546. }
  547. ret = init_tile_data(s);
  548. if (ret < 0) {
  549. av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
  550. return ret;
  551. }
  552. if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
  553. setup_past_independence(&s->cur_frame);
  554. else
  555. load_previous_and_update(s);
  556. global_motion_params(s);
  557. return ret;
  558. }
  559. static int av1_decode_frame(AVCodecContext *avctx, void *frame,
  560. int *got_frame, AVPacket *pkt)
  561. {
  562. AV1DecContext *s = avctx->priv_data;
  563. AV1RawTileGroup *raw_tile_group = NULL;
  564. int ret;
  565. ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
  566. if (ret < 0) {
  567. av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
  568. goto end;
  569. }
  570. av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
  571. s->current_obu.nb_units);
  572. for (int i = 0; i < s->current_obu.nb_units; i++) {
  573. CodedBitstreamUnit *unit = &s->current_obu.units[i];
  574. AV1RawOBU *obu = unit->content;
  575. av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
  576. switch (unit->type) {
  577. case AV1_OBU_SEQUENCE_HEADER:
  578. av_buffer_unref(&s->seq_ref);
  579. s->seq_ref = av_buffer_ref(unit->content_ref);
  580. if (!s->seq_ref) {
  581. ret = AVERROR(ENOMEM);
  582. goto end;
  583. }
  584. s->raw_seq = &obu->obu.sequence_header;
  585. ret = set_context_with_sequence(avctx, s->raw_seq);
  586. if (ret < 0) {
  587. av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
  588. goto end;
  589. }
  590. if (s->pix_fmt == AV_PIX_FMT_NONE) {
  591. ret = get_pixel_format(avctx);
  592. if (ret < 0) {
  593. av_log(avctx, AV_LOG_ERROR,
  594. "Failed to get pixel format.\n");
  595. goto end;
  596. }
  597. }
  598. if (avctx->hwaccel && avctx->hwaccel->decode_params) {
  599. ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
  600. unit->data_size);
  601. if (ret < 0) {
  602. av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
  603. goto end;
  604. }
  605. }
  606. break;
  607. case AV1_OBU_REDUNDANT_FRAME_HEADER:
  608. if (s->raw_frame_header)
  609. break;
  610. // fall-through
  611. case AV1_OBU_FRAME:
  612. case AV1_OBU_FRAME_HEADER:
  613. if (!s->seq_ref) {
  614. av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
  615. ret = AVERROR_INVALIDDATA;
  616. goto end;
  617. }
  618. av_buffer_unref(&s->header_ref);
  619. s->header_ref = av_buffer_ref(unit->content_ref);
  620. if (!s->header_ref) {
  621. ret = AVERROR(ENOMEM);
  622. goto end;
  623. }
  624. if (unit->type == AV1_OBU_FRAME)
  625. s->raw_frame_header = &obu->obu.frame.header;
  626. else
  627. s->raw_frame_header = &obu->obu.frame_header;
  628. if (s->raw_frame_header->show_existing_frame) {
  629. ret = set_output_frame(avctx, frame, pkt, got_frame);
  630. if (ret < 0)
  631. av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
  632. s->raw_frame_header = NULL;
  633. goto end;
  634. }
  635. ret = get_current_frame(avctx);
  636. if (ret < 0) {
  637. av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
  638. goto end;
  639. }
  640. if (avctx->hwaccel) {
  641. ret = avctx->hwaccel->start_frame(avctx, unit->data,
  642. unit->data_size);
  643. if (ret < 0) {
  644. av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
  645. goto end;
  646. }
  647. }
  648. if (unit->type != AV1_OBU_FRAME)
  649. break;
  650. // fall-through
  651. case AV1_OBU_TILE_GROUP:
  652. if (!s->raw_frame_header) {
  653. av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
  654. ret = AVERROR_INVALIDDATA;
  655. goto end;
  656. }
  657. if (unit->type == AV1_OBU_FRAME)
  658. raw_tile_group = &obu->obu.frame.tile_group;
  659. else
  660. raw_tile_group = &obu->obu.tile_group;
  661. get_tiles_info(avctx, raw_tile_group);
  662. if (avctx->hwaccel) {
  663. ret = avctx->hwaccel->decode_slice(avctx,
  664. raw_tile_group->tile_data.data,
  665. raw_tile_group->tile_data.data_size);
  666. if (ret < 0) {
  667. av_log(avctx, AV_LOG_ERROR,
  668. "HW accel decode slice fail.\n");
  669. goto end;
  670. }
  671. }
  672. break;
  673. case AV1_OBU_TILE_LIST:
  674. case AV1_OBU_TEMPORAL_DELIMITER:
  675. case AV1_OBU_PADDING:
  676. case AV1_OBU_METADATA:
  677. break;
  678. default:
  679. av_log(avctx, AV_LOG_DEBUG,
  680. "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
  681. unit->type, unit->data_size);
  682. }
  683. if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
  684. if (avctx->hwaccel) {
  685. ret = avctx->hwaccel->end_frame(avctx);
  686. if (ret < 0) {
  687. av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
  688. goto end;
  689. }
  690. }
  691. ret = update_reference_list(avctx);
  692. if (ret < 0) {
  693. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  694. goto end;
  695. }
  696. if (s->raw_frame_header->show_frame) {
  697. ret = set_output_frame(avctx, frame, pkt, got_frame);
  698. if (ret < 0) {
  699. av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
  700. goto end;
  701. }
  702. }
  703. raw_tile_group = NULL;
  704. s->raw_frame_header = NULL;
  705. }
  706. }
  707. end:
  708. ff_cbs_fragment_reset(&s->current_obu);
  709. return ret;
  710. }
  711. static void av1_decode_flush(AVCodecContext *avctx)
  712. {
  713. AV1DecContext *s = avctx->priv_data;
  714. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
  715. av1_frame_unref(avctx, &s->ref[i]);
  716. av1_frame_unref(avctx, &s->cur_frame);
  717. s->raw_frame_header = NULL;
  718. s->raw_seq = NULL;
  719. }
  720. AVCodec ff_av1_decoder = {
  721. .name = "av1",
  722. .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
  723. .type = AVMEDIA_TYPE_VIDEO,
  724. .id = AV_CODEC_ID_AV1,
  725. .priv_data_size = sizeof(AV1DecContext),
  726. .init = av1_decode_init,
  727. .close = av1_decode_free,
  728. .decode = av1_decode_frame,
  729. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
  730. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  731. FF_CODEC_CAP_INIT_CLEANUP |
  732. FF_CODEC_CAP_SETS_PKT_DTS,
  733. .flush = av1_decode_flush,
  734. .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
  735. .hw_configs = (const AVCodecHWConfigInternal * []) {
  736. NULL
  737. },
  738. };