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.

880 lines
28KB

  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. }
  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. av1_frame_unref(avctx, &s->ref[i]);
  340. av_frame_free(&s->ref[i].tf.f);
  341. }
  342. av1_frame_unref(avctx, &s->cur_frame);
  343. av_frame_free(&s->cur_frame.tf.f);
  344. av_buffer_unref(&s->seq_ref);
  345. av_buffer_unref(&s->header_ref);
  346. av_freep(&s->tile_group_info);
  347. ff_cbs_fragment_free(&s->current_obu);
  348. ff_cbs_close(&s->cbc);
  349. return 0;
  350. }
  351. static int set_context_with_sequence(AVCodecContext *avctx,
  352. const AV1RawSequenceHeader *seq)
  353. {
  354. int width = seq->max_frame_width_minus_1 + 1;
  355. int height = seq->max_frame_height_minus_1 + 1;
  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 (avctx->width != width || avctx->height != height) {
  372. int ret = ff_set_dimensions(avctx, width, height);
  373. if (ret < 0)
  374. return ret;
  375. }
  376. avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
  377. if (seq->timing_info.num_units_in_display_tick &&
  378. seq->timing_info.time_scale) {
  379. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  380. seq->timing_info.num_units_in_display_tick,
  381. seq->timing_info.time_scale,
  382. INT_MAX);
  383. if (seq->timing_info.equal_picture_interval)
  384. avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
  385. }
  386. return 0;
  387. }
  388. static int update_context_with_frame_header(AVCodecContext *avctx,
  389. const AV1RawFrameHeader *header)
  390. {
  391. AVRational aspect_ratio;
  392. int width = header->frame_width_minus_1 + 1;
  393. int height = header->frame_height_minus_1 + 1;
  394. int r_width = header->render_width_minus_1 + 1;
  395. int r_height = header->render_height_minus_1 + 1;
  396. int ret;
  397. if (avctx->width != width || avctx->height != height) {
  398. ret = ff_set_dimensions(avctx, width, height);
  399. if (ret < 0)
  400. return ret;
  401. }
  402. av_reduce(&aspect_ratio.num, &aspect_ratio.den,
  403. (int64_t)height * r_width,
  404. (int64_t)width * r_height,
  405. INT_MAX);
  406. if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
  407. ret = ff_set_sar(avctx, aspect_ratio);
  408. if (ret < 0)
  409. return ret;
  410. }
  411. return 0;
  412. }
  413. static av_cold int av1_decode_init(AVCodecContext *avctx)
  414. {
  415. AV1DecContext *s = avctx->priv_data;
  416. AV1RawSequenceHeader *seq;
  417. int ret;
  418. s->avctx = avctx;
  419. s->pix_fmt = AV_PIX_FMT_NONE;
  420. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
  421. s->ref[i].tf.f = av_frame_alloc();
  422. if (!s->ref[i].tf.f) {
  423. av_log(avctx, AV_LOG_ERROR,
  424. "Failed to allocate reference frame buffer %d.\n", i);
  425. return AVERROR(ENOMEM);
  426. }
  427. }
  428. s->cur_frame.tf.f = av_frame_alloc();
  429. if (!s->cur_frame.tf.f) {
  430. av_log(avctx, AV_LOG_ERROR,
  431. "Failed to allocate current frame buffer.\n");
  432. return AVERROR(ENOMEM);
  433. }
  434. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
  435. if (ret < 0)
  436. return ret;
  437. if (avctx->extradata && avctx->extradata_size) {
  438. ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
  439. avctx->extradata_size);
  440. if (ret < 0) {
  441. av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
  442. return ret;
  443. }
  444. seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
  445. if (!seq) {
  446. av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
  447. goto end;
  448. }
  449. ret = set_context_with_sequence(avctx, seq);
  450. if (ret < 0) {
  451. av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
  452. goto end;
  453. }
  454. end:
  455. ff_cbs_fragment_reset(&s->current_obu);
  456. }
  457. return ret;
  458. }
  459. static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
  460. {
  461. AV1DecContext *s = avctx->priv_data;
  462. AV1RawFrameHeader *header= s->raw_frame_header;
  463. AVFrame *frame;
  464. int ret;
  465. ret = update_context_with_frame_header(avctx, header);
  466. if (ret < 0) {
  467. av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
  468. return ret;
  469. }
  470. if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
  471. return ret;
  472. frame = f->tf.f;
  473. frame->key_frame = header->frame_type == AV1_FRAME_KEY;
  474. switch (header->frame_type) {
  475. case AV1_FRAME_KEY:
  476. case AV1_FRAME_INTRA_ONLY:
  477. frame->pict_type = AV_PICTURE_TYPE_I;
  478. break;
  479. case AV1_FRAME_INTER:
  480. frame->pict_type = AV_PICTURE_TYPE_P;
  481. break;
  482. case AV1_FRAME_SWITCH:
  483. frame->pict_type = AV_PICTURE_TYPE_SP;
  484. break;
  485. }
  486. if (avctx->hwaccel) {
  487. const AVHWAccel *hwaccel = avctx->hwaccel;
  488. if (hwaccel->frame_priv_data_size) {
  489. f->hwaccel_priv_buf =
  490. av_buffer_allocz(hwaccel->frame_priv_data_size);
  491. if (!f->hwaccel_priv_buf)
  492. goto fail;
  493. f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
  494. }
  495. }
  496. return 0;
  497. fail:
  498. av1_frame_unref(avctx, f);
  499. return AVERROR(ENOMEM);
  500. }
  501. static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
  502. const AVPacket *pkt, int *got_frame)
  503. {
  504. AV1DecContext *s = avctx->priv_data;
  505. const AVFrame *srcframe = s->cur_frame.tf.f;
  506. int ret;
  507. ret = av_frame_ref(frame, srcframe);
  508. if (ret < 0)
  509. return ret;
  510. frame->pts = pkt->pts;
  511. frame->pkt_dts = pkt->dts;
  512. frame->pkt_size = pkt->size;
  513. *got_frame = 1;
  514. return 0;
  515. }
  516. static int update_reference_list(AVCodecContext *avctx)
  517. {
  518. AV1DecContext *s = avctx->priv_data;
  519. const AV1RawFrameHeader *header = s->raw_frame_header;
  520. int ret;
  521. for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
  522. if (header->refresh_frame_flags & (1 << i)) {
  523. if (s->ref[i].tf.f->buf[0])
  524. av1_frame_unref(avctx, &s->ref[i]);
  525. if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
  526. av_log(avctx, AV_LOG_ERROR,
  527. "Failed to update frame %d in reference list\n", i);
  528. return ret;
  529. }
  530. }
  531. }
  532. return 0;
  533. }
  534. static int get_current_frame(AVCodecContext *avctx)
  535. {
  536. AV1DecContext *s = avctx->priv_data;
  537. int ret;
  538. if (s->cur_frame.tf.f->buf[0])
  539. av1_frame_unref(avctx, &s->cur_frame);
  540. ret = av1_frame_alloc(avctx, &s->cur_frame);
  541. if (ret < 0) {
  542. av_log(avctx, AV_LOG_ERROR,
  543. "Failed to allocate space for current frame.\n");
  544. return ret;
  545. }
  546. ret = init_tile_data(s);
  547. if (ret < 0) {
  548. av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
  549. return ret;
  550. }
  551. if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
  552. setup_past_independence(&s->cur_frame);
  553. else
  554. load_previous_and_update(s);
  555. global_motion_params(s);
  556. return ret;
  557. }
  558. static int av1_decode_frame(AVCodecContext *avctx, void *frame,
  559. int *got_frame, AVPacket *pkt)
  560. {
  561. AV1DecContext *s = avctx->priv_data;
  562. AV1RawTileGroup *raw_tile_group = NULL;
  563. int ret;
  564. ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
  565. if (ret < 0) {
  566. av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
  567. goto end;
  568. }
  569. av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
  570. s->current_obu.nb_units);
  571. for (int i = 0; i < s->current_obu.nb_units; i++) {
  572. CodedBitstreamUnit *unit = &s->current_obu.units[i];
  573. AV1RawOBU *obu = unit->content;
  574. av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
  575. switch (unit->type) {
  576. case AV1_OBU_SEQUENCE_HEADER:
  577. av_buffer_unref(&s->seq_ref);
  578. s->seq_ref = av_buffer_ref(unit->content_ref);
  579. if (!s->seq_ref) {
  580. ret = AVERROR(ENOMEM);
  581. goto end;
  582. }
  583. s->raw_seq = &obu->obu.sequence_header;
  584. ret = set_context_with_sequence(avctx, s->raw_seq);
  585. if (ret < 0) {
  586. av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
  587. goto end;
  588. }
  589. if (s->pix_fmt == AV_PIX_FMT_NONE) {
  590. ret = get_pixel_format(avctx);
  591. if (ret < 0) {
  592. av_log(avctx, AV_LOG_ERROR,
  593. "Failed to get pixel format.\n");
  594. goto end;
  595. }
  596. }
  597. if (avctx->hwaccel && avctx->hwaccel->decode_params) {
  598. ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
  599. unit->data_size);
  600. if (ret < 0) {
  601. av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
  602. goto end;
  603. }
  604. }
  605. break;
  606. case AV1_OBU_REDUNDANT_FRAME_HEADER:
  607. if (s->raw_frame_header)
  608. break;
  609. // fall-through
  610. case AV1_OBU_FRAME:
  611. case AV1_OBU_FRAME_HEADER:
  612. if (!s->raw_seq) {
  613. av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
  614. ret = AVERROR_INVALIDDATA;
  615. goto end;
  616. }
  617. av_buffer_unref(&s->header_ref);
  618. s->header_ref = av_buffer_ref(unit->content_ref);
  619. if (!s->header_ref) {
  620. ret = AVERROR(ENOMEM);
  621. goto end;
  622. }
  623. if (unit->type == AV1_OBU_FRAME)
  624. s->raw_frame_header = &obu->obu.frame.header;
  625. else
  626. s->raw_frame_header = &obu->obu.frame_header;
  627. if (s->raw_frame_header->show_existing_frame) {
  628. if (s->cur_frame.tf.f->buf[0])
  629. av1_frame_unref(avctx, &s->cur_frame);
  630. ret = av1_frame_ref(avctx, &s->cur_frame,
  631. &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
  632. if (ret < 0) {
  633. av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
  634. goto end;
  635. }
  636. ret = update_reference_list(avctx);
  637. if (ret < 0) {
  638. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  639. goto end;
  640. }
  641. ret = set_output_frame(avctx, frame, pkt, got_frame);
  642. if (ret < 0)
  643. av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
  644. s->raw_frame_header = NULL;
  645. goto end;
  646. }
  647. ret = get_current_frame(avctx);
  648. if (ret < 0) {
  649. av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
  650. goto end;
  651. }
  652. if (avctx->hwaccel) {
  653. ret = avctx->hwaccel->start_frame(avctx, unit->data,
  654. unit->data_size);
  655. if (ret < 0) {
  656. av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
  657. goto end;
  658. }
  659. }
  660. if (unit->type != AV1_OBU_FRAME)
  661. break;
  662. // fall-through
  663. case AV1_OBU_TILE_GROUP:
  664. if (!s->raw_frame_header) {
  665. av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
  666. ret = AVERROR_INVALIDDATA;
  667. goto end;
  668. }
  669. if (unit->type == AV1_OBU_FRAME)
  670. raw_tile_group = &obu->obu.frame.tile_group;
  671. else
  672. raw_tile_group = &obu->obu.tile_group;
  673. ret = get_tiles_info(avctx, raw_tile_group);
  674. if (ret < 0)
  675. goto end;
  676. if (avctx->hwaccel) {
  677. ret = avctx->hwaccel->decode_slice(avctx,
  678. raw_tile_group->tile_data.data,
  679. raw_tile_group->tile_data.data_size);
  680. if (ret < 0) {
  681. av_log(avctx, AV_LOG_ERROR,
  682. "HW accel decode slice fail.\n");
  683. goto end;
  684. }
  685. }
  686. break;
  687. case AV1_OBU_TILE_LIST:
  688. case AV1_OBU_TEMPORAL_DELIMITER:
  689. case AV1_OBU_PADDING:
  690. case AV1_OBU_METADATA:
  691. break;
  692. default:
  693. av_log(avctx, AV_LOG_DEBUG,
  694. "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
  695. unit->type, unit->data_size);
  696. }
  697. if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
  698. if (avctx->hwaccel) {
  699. ret = avctx->hwaccel->end_frame(avctx);
  700. if (ret < 0) {
  701. av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
  702. goto end;
  703. }
  704. }
  705. ret = update_reference_list(avctx);
  706. if (ret < 0) {
  707. av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
  708. goto end;
  709. }
  710. if (s->raw_frame_header->show_frame) {
  711. ret = set_output_frame(avctx, frame, pkt, got_frame);
  712. if (ret < 0) {
  713. av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
  714. goto end;
  715. }
  716. }
  717. raw_tile_group = NULL;
  718. s->raw_frame_header = NULL;
  719. }
  720. }
  721. end:
  722. ff_cbs_fragment_reset(&s->current_obu);
  723. return ret;
  724. }
  725. static void av1_decode_flush(AVCodecContext *avctx)
  726. {
  727. AV1DecContext *s = avctx->priv_data;
  728. for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
  729. av1_frame_unref(avctx, &s->ref[i]);
  730. av1_frame_unref(avctx, &s->cur_frame);
  731. s->raw_frame_header = NULL;
  732. s->raw_seq = NULL;
  733. ff_cbs_flush(s->cbc);
  734. }
  735. AVCodec ff_av1_decoder = {
  736. .name = "av1",
  737. .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
  738. .type = AVMEDIA_TYPE_VIDEO,
  739. .id = AV_CODEC_ID_AV1,
  740. .priv_data_size = sizeof(AV1DecContext),
  741. .init = av1_decode_init,
  742. .close = av1_decode_free,
  743. .decode = av1_decode_frame,
  744. .capabilities = AV_CODEC_CAP_DR1,
  745. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  746. FF_CODEC_CAP_INIT_CLEANUP |
  747. FF_CODEC_CAP_SETS_PKT_DTS,
  748. .flush = av1_decode_flush,
  749. .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
  750. .hw_configs = (const AVCodecHWConfigInternal * []) {
  751. NULL
  752. },
  753. };