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.

897 lines
29KB

  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 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. GetByteContext gb;
  185. uint16_t tile_num, tile_row, tile_col;
  186. uint32_t size = 0, size_bytes = 0;
  187. bytestream2_init(&gb, tile_group->tile_data.data,
  188. tile_group->tile_data.data_size);
  189. s->tg_start = tile_group->tg_start;
  190. s->tg_end = tile_group->tg_end;
  191. for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
  192. tile_row = tile_num / s->raw_frame_header->tile_cols;
  193. tile_col = tile_num % s->raw_frame_header->tile_cols;
  194. if (tile_num == tile_group->tg_end) {
  195. s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
  196. s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
  197. s->tile_group_info[tile_num].tile_row = tile_row;
  198. s->tile_group_info[tile_num].tile_column = tile_col;
  199. return 0;
  200. }
  201. size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
  202. if (bytestream2_get_bytes_left(&gb) < size_bytes)
  203. return AVERROR_INVALIDDATA;
  204. size = 0;
  205. for (int i = 0; i < size_bytes; i++)
  206. size |= bytestream2_get_byteu(&gb) << 8 * i;
  207. if (bytestream2_get_bytes_left(&gb) <= size)
  208. return AVERROR_INVALIDDATA;
  209. size++;
  210. s->tile_group_info[tile_num].tile_size = size;
  211. s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
  212. s->tile_group_info[tile_num].tile_row = tile_row;
  213. s->tile_group_info[tile_num].tile_column = tile_col;
  214. bytestream2_skipu(&gb, 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 + 2], *fmtp = pix_fmts;
  227. if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
  228. bit_depth = seq->color_config.twelve_bit ? 12 : 10;
  229. else if (seq->seq_profile <= 2)
  230. bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
  231. else {
  232. av_log(avctx, AV_LOG_ERROR,
  233. "Unknown AV1 profile %d.\n", seq->seq_profile);
  234. return -1;
  235. }
  236. if (!seq->color_config.mono_chrome) {
  237. // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
  238. if (seq->color_config.subsampling_x == 0 &&
  239. seq->color_config.subsampling_y == 0) {
  240. if (bit_depth == 8)
  241. pix_fmt = AV_PIX_FMT_YUV444P;
  242. else if (bit_depth == 10)
  243. pix_fmt = AV_PIX_FMT_YUV444P10;
  244. else if (bit_depth == 12)
  245. pix_fmt = AV_PIX_FMT_YUV444P12;
  246. else
  247. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  248. } else if (seq->color_config.subsampling_x == 1 &&
  249. seq->color_config.subsampling_y == 0) {
  250. if (bit_depth == 8)
  251. pix_fmt = AV_PIX_FMT_YUV422P;
  252. else if (bit_depth == 10)
  253. pix_fmt = AV_PIX_FMT_YUV422P10;
  254. else if (bit_depth == 12)
  255. pix_fmt = AV_PIX_FMT_YUV422P12;
  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 == 1) {
  260. if (bit_depth == 8)
  261. pix_fmt = AV_PIX_FMT_YUV420P;
  262. else if (bit_depth == 10)
  263. pix_fmt = AV_PIX_FMT_YUV420P10;
  264. else if (bit_depth == 12)
  265. pix_fmt = AV_PIX_FMT_YUV420P12;
  266. else
  267. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  268. }
  269. } else {
  270. if (seq->color_config.subsampling_x == 1 &&
  271. seq->color_config.subsampling_y == 1)
  272. pix_fmt = AV_PIX_FMT_YUV440P;
  273. else
  274. av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
  275. }
  276. av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
  277. av_get_pix_fmt_name(pix_fmt));
  278. if (pix_fmt == AV_PIX_FMT_NONE)
  279. return -1;
  280. s->pix_fmt = pix_fmt;
  281. *fmtp++ = s->pix_fmt;
  282. *fmtp = AV_PIX_FMT_NONE;
  283. ret = ff_thread_get_format(avctx, pix_fmts);
  284. if (ret < 0)
  285. return ret;
  286. /**
  287. * check if the HW accel is inited correctly. If not, return un-implemented.
  288. * Since now the av1 decoder doesn't support native decode, if it will be
  289. * implemented in the future, need remove this check.
  290. */
  291. if (!avctx->hwaccel) {
  292. av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
  293. " hardware accelerated AV1 decoding.\n");
  294. return AVERROR(ENOSYS);
  295. }
  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. f->spatial_id = f->temporal_id = 0;
  305. }
  306. static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
  307. {
  308. int ret;
  309. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  310. if (ret < 0)
  311. return ret;
  312. if (src->hwaccel_picture_private) {
  313. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  314. if (!dst->hwaccel_priv_buf)
  315. goto fail;
  316. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  317. }
  318. dst->spatial_id = src->spatial_id;
  319. dst->temporal_id = src->temporal_id;
  320. dst->loop_filter_delta_enabled = src->loop_filter_delta_enabled;
  321. memcpy(dst->loop_filter_ref_deltas,
  322. src->loop_filter_ref_deltas,
  323. AV1_NUM_REF_FRAMES * sizeof(int8_t));
  324. memcpy(dst->loop_filter_mode_deltas,
  325. src->loop_filter_mode_deltas,
  326. 2 * sizeof(int8_t));
  327. memcpy(dst->gm_type,
  328. src->gm_type,
  329. AV1_NUM_REF_FRAMES * sizeof(uint8_t));
  330. memcpy(dst->gm_params,
  331. src->gm_params,
  332. AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
  333. return 0;
  334. fail:
  335. av1_frame_unref(avctx, dst);
  336. return AVERROR(ENOMEM);
  337. }
  338. static av_cold int av1_decode_free(AVCodecContext *avctx)
  339. {
  340. AV1DecContext *s = avctx->priv_data;
  341. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  342. av1_frame_unref(avctx, &s->ref[i]);
  343. av_frame_free(&s->ref[i].tf.f);
  344. }
  345. av1_frame_unref(avctx, &s->cur_frame);
  346. av_frame_free(&s->cur_frame.tf.f);
  347. av_buffer_unref(&s->seq_ref);
  348. av_buffer_unref(&s->header_ref);
  349. av_freep(&s->tile_group_info);
  350. ff_cbs_fragment_free(&s->current_obu);
  351. ff_cbs_close(&s->cbc);
  352. return 0;
  353. }
  354. static int set_context_with_sequence(AVCodecContext *avctx,
  355. const AV1RawSequenceHeader *seq)
  356. {
  357. int width = seq->max_frame_width_minus_1 + 1;
  358. int height = seq->max_frame_height_minus_1 + 1;
  359. avctx->profile = seq->seq_profile;
  360. avctx->level = seq->seq_level_idx[0];
  361. avctx->color_range =
  362. seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  363. avctx->color_primaries = seq->color_config.color_primaries;
  364. avctx->colorspace = seq->color_config.color_primaries;
  365. avctx->color_trc = seq->color_config.transfer_characteristics;
  366. switch (seq->color_config.chroma_sample_position) {
  367. case AV1_CSP_VERTICAL:
  368. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  369. break;
  370. case AV1_CSP_COLOCATED:
  371. avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  372. break;
  373. }
  374. if (avctx->width != width || avctx->height != height) {
  375. int ret = ff_set_dimensions(avctx, width, height);
  376. if (ret < 0)
  377. return ret;
  378. }
  379. avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
  380. if (seq->timing_info.num_units_in_display_tick &&
  381. seq->timing_info.time_scale) {
  382. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  383. seq->timing_info.num_units_in_display_tick,
  384. seq->timing_info.time_scale,
  385. INT_MAX);
  386. if (seq->timing_info.equal_picture_interval)
  387. avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
  388. }
  389. return 0;
  390. }
  391. static int update_context_with_frame_header(AVCodecContext *avctx,
  392. const AV1RawFrameHeader *header)
  393. {
  394. AVRational aspect_ratio;
  395. int width = header->frame_width_minus_1 + 1;
  396. int height = header->frame_height_minus_1 + 1;
  397. int r_width = header->render_width_minus_1 + 1;
  398. int r_height = header->render_height_minus_1 + 1;
  399. int ret;
  400. if (avctx->width != width || avctx->height != height) {
  401. ret = ff_set_dimensions(avctx, width, height);
  402. if (ret < 0)
  403. return ret;
  404. }
  405. av_reduce(&aspect_ratio.num, &aspect_ratio.den,
  406. (int64_t)height * r_width,
  407. (int64_t)width * r_height,
  408. INT_MAX);
  409. if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
  410. ret = ff_set_sar(avctx, aspect_ratio);
  411. if (ret < 0)
  412. return ret;
  413. }
  414. return 0;
  415. }
  416. static av_cold int av1_decode_init(AVCodecContext *avctx)
  417. {
  418. AV1DecContext *s = avctx->priv_data;
  419. AV1RawSequenceHeader *seq;
  420. int ret;
  421. s->avctx = avctx;
  422. s->pix_fmt = AV_PIX_FMT_NONE;
  423. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  424. s->ref[i].tf.f = av_frame_alloc();
  425. if (!s->ref[i].tf.f) {
  426. av_log(avctx, AV_LOG_ERROR,
  427. "Failed to allocate reference frame buffer %d.\n", i);
  428. return AVERROR(ENOMEM);
  429. }
  430. }
  431. s->cur_frame.tf.f = av_frame_alloc();
  432. if (!s->cur_frame.tf.f) {
  433. av_log(avctx, AV_LOG_ERROR,
  434. "Failed to allocate current frame buffer.\n");
  435. return AVERROR(ENOMEM);
  436. }
  437. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
  438. if (ret < 0)
  439. return ret;
  440. if (avctx->extradata && avctx->extradata_size) {
  441. ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
  442. avctx->extradata_size);
  443. if (ret < 0) {
  444. av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
  445. return ret;
  446. }
  447. seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
  448. if (!seq) {
  449. av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
  450. goto end;
  451. }
  452. ret = set_context_with_sequence(avctx, seq);
  453. if (ret < 0) {
  454. av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
  455. goto end;
  456. }
  457. end:
  458. ff_cbs_fragment_reset(&s->current_obu);
  459. }
  460. return ret;
  461. }
  462. static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
  463. {
  464. AV1DecContext *s = avctx->priv_data;
  465. AV1RawFrameHeader *header= s->raw_frame_header;
  466. AVFrame *frame;
  467. int ret;
  468. ret = update_context_with_frame_header(avctx, header);
  469. if (ret < 0) {
  470. av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
  471. return ret;
  472. }
  473. if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
  474. return ret;
  475. frame = f->tf.f;
  476. frame->key_frame = header->frame_type == AV1_FRAME_KEY;
  477. switch (header->frame_type) {
  478. case AV1_FRAME_KEY:
  479. case AV1_FRAME_INTRA_ONLY:
  480. frame->pict_type = AV_PICTURE_TYPE_I;
  481. break;
  482. case AV1_FRAME_INTER:
  483. frame->pict_type = AV_PICTURE_TYPE_P;
  484. break;
  485. case AV1_FRAME_SWITCH:
  486. frame->pict_type = AV_PICTURE_TYPE_SP;
  487. break;
  488. }
  489. if (avctx->hwaccel) {
  490. const AVHWAccel *hwaccel = avctx->hwaccel;
  491. if (hwaccel->frame_priv_data_size) {
  492. f->hwaccel_priv_buf =
  493. av_buffer_allocz(hwaccel->frame_priv_data_size);
  494. if (!f->hwaccel_priv_buf)
  495. goto fail;
  496. f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
  497. }
  498. }
  499. return 0;
  500. fail:
  501. av1_frame_unref(avctx, f);
  502. return AVERROR(ENOMEM);
  503. }
  504. static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
  505. const AVPacket *pkt, int *got_frame)
  506. {
  507. AV1DecContext *s = avctx->priv_data;
  508. const AVFrame *srcframe = s->cur_frame.tf.f;
  509. int ret;
  510. ret = av_frame_ref(frame, srcframe);
  511. if (ret < 0)
  512. return ret;
  513. frame->pts = pkt->pts;
  514. frame->pkt_dts = pkt->dts;
  515. frame->pkt_size = pkt->size;
  516. *got_frame = 1;
  517. return 0;
  518. }
  519. static int update_reference_list(AVCodecContext *avctx)
  520. {
  521. AV1DecContext *s = avctx->priv_data;
  522. const AV1RawFrameHeader *header = s->raw_frame_header;
  523. int ret;
  524. for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
  525. if (header->refresh_frame_flags & (1 << i)) {
  526. if (s->ref[i].tf.f->buf[0])
  527. av1_frame_unref(avctx, &s->ref[i]);
  528. if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
  529. av_log(avctx, AV_LOG_ERROR,
  530. "Failed to update frame %d in reference list\n", i);
  531. return ret;
  532. }
  533. }
  534. }
  535. return 0;
  536. }
  537. static int get_current_frame(AVCodecContext *avctx)
  538. {
  539. AV1DecContext *s = avctx->priv_data;
  540. int ret;
  541. if (s->cur_frame.tf.f->buf[0])
  542. av1_frame_unref(avctx, &s->cur_frame);
  543. ret = av1_frame_alloc(avctx, &s->cur_frame);
  544. if (ret < 0) {
  545. av_log(avctx, AV_LOG_ERROR,
  546. "Failed to allocate space for current frame.\n");
  547. return ret;
  548. }
  549. ret = init_tile_data(s);
  550. if (ret < 0) {
  551. av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
  552. return ret;
  553. }
  554. if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
  555. setup_past_independence(&s->cur_frame);
  556. else
  557. load_previous_and_update(s);
  558. global_motion_params(s);
  559. return ret;
  560. }
  561. static int av1_decode_frame(AVCodecContext *avctx, void *frame,
  562. int *got_frame, AVPacket *pkt)
  563. {
  564. AV1DecContext *s = avctx->priv_data;
  565. AV1RawTileGroup *raw_tile_group = NULL;
  566. int ret;
  567. ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
  568. if (ret < 0) {
  569. av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
  570. goto end;
  571. }
  572. av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
  573. s->current_obu.nb_units);
  574. for (int i = 0; i < s->current_obu.nb_units; i++) {
  575. CodedBitstreamUnit *unit = &s->current_obu.units[i];
  576. AV1RawOBU *obu = unit->content;
  577. const AV1RawOBUHeader *header;
  578. if (!obu)
  579. continue;
  580. header = &obu->header;
  581. av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
  582. switch (unit->type) {
  583. case AV1_OBU_SEQUENCE_HEADER:
  584. av_buffer_unref(&s->seq_ref);
  585. s->seq_ref = av_buffer_ref(unit->content_ref);
  586. if (!s->seq_ref) {
  587. ret = AVERROR(ENOMEM);
  588. goto end;
  589. }
  590. s->raw_seq = &obu->obu.sequence_header;
  591. ret = set_context_with_sequence(avctx, s->raw_seq);
  592. if (ret < 0) {
  593. av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
  594. s->raw_seq = NULL;
  595. goto end;
  596. }
  597. if (s->pix_fmt == AV_PIX_FMT_NONE) {
  598. ret = get_pixel_format(avctx);
  599. if (ret < 0) {
  600. av_log(avctx, AV_LOG_ERROR,
  601. "Failed to get pixel format.\n");
  602. s->raw_seq = NULL;
  603. goto end;
  604. }
  605. }
  606. if (avctx->hwaccel && avctx->hwaccel->decode_params) {
  607. ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
  608. unit->data_size);
  609. if (ret < 0) {
  610. av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
  611. s->raw_seq = NULL;
  612. goto end;
  613. }
  614. }
  615. break;
  616. case AV1_OBU_REDUNDANT_FRAME_HEADER:
  617. if (s->raw_frame_header)
  618. break;
  619. // fall-through
  620. case AV1_OBU_FRAME:
  621. case AV1_OBU_FRAME_HEADER:
  622. if (!s->raw_seq) {
  623. av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
  624. ret = AVERROR_INVALIDDATA;
  625. goto end;
  626. }
  627. av_buffer_unref(&s->header_ref);
  628. s->header_ref = av_buffer_ref(unit->content_ref);
  629. if (!s->header_ref) {
  630. ret = AVERROR(ENOMEM);
  631. goto end;
  632. }
  633. if (unit->type == AV1_OBU_FRAME)
  634. s->raw_frame_header = &obu->obu.frame.header;
  635. else
  636. s->raw_frame_header = &obu->obu.frame_header;
  637. if (s->raw_frame_header->show_existing_frame) {
  638. if (s->cur_frame.tf.f->buf[0])
  639. av1_frame_unref(avctx, &s->cur_frame);
  640. ret = av1_frame_ref(avctx, &s->cur_frame,
  641. &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
  642. if (ret < 0) {
  643. av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
  644. goto end;
  645. }
  646. ret = update_reference_list(avctx);
  647. if (ret < 0) {
  648. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  649. goto end;
  650. }
  651. ret = set_output_frame(avctx, frame, pkt, got_frame);
  652. if (ret < 0)
  653. av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
  654. s->raw_frame_header = NULL;
  655. goto end;
  656. }
  657. ret = get_current_frame(avctx);
  658. if (ret < 0) {
  659. av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
  660. goto end;
  661. }
  662. s->cur_frame.spatial_id = header->spatial_id;
  663. s->cur_frame.temporal_id = header->temporal_id;
  664. if (avctx->hwaccel) {
  665. ret = avctx->hwaccel->start_frame(avctx, unit->data,
  666. unit->data_size);
  667. if (ret < 0) {
  668. av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
  669. goto end;
  670. }
  671. }
  672. if (unit->type != AV1_OBU_FRAME)
  673. break;
  674. // fall-through
  675. case AV1_OBU_TILE_GROUP:
  676. if (!s->raw_frame_header) {
  677. av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
  678. ret = AVERROR_INVALIDDATA;
  679. goto end;
  680. }
  681. if (unit->type == AV1_OBU_FRAME)
  682. raw_tile_group = &obu->obu.frame.tile_group;
  683. else
  684. raw_tile_group = &obu->obu.tile_group;
  685. ret = get_tiles_info(avctx, raw_tile_group);
  686. if (ret < 0)
  687. goto end;
  688. if (avctx->hwaccel) {
  689. ret = avctx->hwaccel->decode_slice(avctx,
  690. raw_tile_group->tile_data.data,
  691. raw_tile_group->tile_data.data_size);
  692. if (ret < 0) {
  693. av_log(avctx, AV_LOG_ERROR,
  694. "HW accel decode slice fail.\n");
  695. goto end;
  696. }
  697. }
  698. break;
  699. case AV1_OBU_TILE_LIST:
  700. case AV1_OBU_TEMPORAL_DELIMITER:
  701. case AV1_OBU_PADDING:
  702. case AV1_OBU_METADATA:
  703. break;
  704. default:
  705. av_log(avctx, AV_LOG_DEBUG,
  706. "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
  707. unit->type, unit->data_size);
  708. }
  709. if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
  710. if (avctx->hwaccel) {
  711. ret = avctx->hwaccel->end_frame(avctx);
  712. if (ret < 0) {
  713. av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
  714. goto end;
  715. }
  716. }
  717. ret = update_reference_list(avctx);
  718. if (ret < 0) {
  719. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  720. goto end;
  721. }
  722. if (s->raw_frame_header->show_frame) {
  723. ret = set_output_frame(avctx, frame, pkt, got_frame);
  724. if (ret < 0) {
  725. av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
  726. goto end;
  727. }
  728. }
  729. raw_tile_group = NULL;
  730. s->raw_frame_header = NULL;
  731. }
  732. }
  733. end:
  734. ff_cbs_fragment_reset(&s->current_obu);
  735. if (ret < 0)
  736. s->raw_frame_header = NULL;
  737. return ret;
  738. }
  739. static void av1_decode_flush(AVCodecContext *avctx)
  740. {
  741. AV1DecContext *s = avctx->priv_data;
  742. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
  743. av1_frame_unref(avctx, &s->ref[i]);
  744. av1_frame_unref(avctx, &s->cur_frame);
  745. s->raw_frame_header = NULL;
  746. s->raw_seq = NULL;
  747. ff_cbs_flush(s->cbc);
  748. }
  749. AVCodec ff_av1_decoder = {
  750. .name = "av1",
  751. .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
  752. .type = AVMEDIA_TYPE_VIDEO,
  753. .id = AV_CODEC_ID_AV1,
  754. .priv_data_size = sizeof(AV1DecContext),
  755. .init = av1_decode_init,
  756. .close = av1_decode_free,
  757. .decode = av1_decode_frame,
  758. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
  759. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  760. FF_CODEC_CAP_INIT_CLEANUP |
  761. FF_CODEC_CAP_SETS_PKT_DTS,
  762. .flush = av1_decode_flush,
  763. .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
  764. .hw_configs = (const AVCodecHWConfigInternal * []) {
  765. NULL
  766. },
  767. };