* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "libavutil/film_grain_params.h"
#include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
#include "avcodec.h"
#include "av1dec.h"
#include "bytestream.h"
forward_idx = -1;
backward_idx = -1;
for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
- ref_hint = s->ref[header->ref_frame_idx[i]].order_hint;
+ ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
dist = get_relative_dist(seq, ref_hint, header->order_hint);
if (dist < 0) {
if (forward_idx < 0 ||
second_forward_idx = -1;
for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
- ref_hint = s->ref[header->ref_frame_idx[i]].order_hint;
+ ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {
if (second_forward_idx < 0 ||
get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {
}
}
+static void load_grain_params(AV1DecContext *s)
+{
+ const AV1RawFrameHeader *header = s->raw_frame_header;
+ const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;
+ AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain;
+
+ if (!film_grain->apply_grain)
+ return;
+
+ if (film_grain->update_grain) {
+ memcpy(dst, film_grain, sizeof(*dst));
+ return;
+ }
+
+ src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;
+
+ memcpy(dst, src, sizeof(*dst));
+ dst->grain_seed = film_grain->grain_seed;
+}
+
static int init_tile_data(AV1DecContext *s)
{
uint8_t bit_depth;
int ret;
enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
-#define HWACCEL_MAX (CONFIG_AV1_VAAPI_HWACCEL)
+#define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
+ CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
+ CONFIG_AV1_NVDEC_HWACCEL + \
+ CONFIG_AV1_VAAPI_HWACCEL)
enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
}
} else {
- if (seq->color_config.subsampling_x == 1 &&
- seq->color_config.subsampling_y == 1)
- pix_fmt = AV_PIX_FMT_YUV440P;
+ if (bit_depth == 8)
+ pix_fmt = AV_PIX_FMT_GRAY8;
+ else if (bit_depth == 10)
+ pix_fmt = AV_PIX_FMT_GRAY10;
+ else if (bit_depth == 12)
+ pix_fmt = AV_PIX_FMT_GRAY12;
else
av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
}
switch (s->pix_fmt) {
case AV_PIX_FMT_YUV420P:
+#if CONFIG_AV1_DXVA2_HWACCEL
+ *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
+#endif
+#if CONFIG_AV1_D3D11VA_HWACCEL
+ *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
+ *fmtp++ = AV_PIX_FMT_D3D11;
+#endif
+#if CONFIG_AV1_NVDEC_HWACCEL
+ *fmtp++ = AV_PIX_FMT_CUDA;
+#endif
#if CONFIG_AV1_VAAPI_HWACCEL
*fmtp++ = AV_PIX_FMT_VAAPI;
#endif
break;
case AV_PIX_FMT_YUV420P10:
+#if CONFIG_AV1_DXVA2_HWACCEL
+ *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
+#endif
+#if CONFIG_AV1_D3D11VA_HWACCEL
+ *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
+ *fmtp++ = AV_PIX_FMT_D3D11;
+#endif
+#if CONFIG_AV1_NVDEC_HWACCEL
+ *fmtp++ = AV_PIX_FMT_CUDA;
+#endif
#if CONFIG_AV1_VAAPI_HWACCEL
*fmtp++ = AV_PIX_FMT_VAAPI;
+#endif
+ break;
+ case AV_PIX_FMT_GRAY8:
+#if CONFIG_AV1_NVDEC_HWACCEL
+ *fmtp++ = AV_PIX_FMT_CUDA;
+#endif
+ break;
+ case AV_PIX_FMT_GRAY10:
+#if CONFIG_AV1_NVDEC_HWACCEL
+ *fmtp++ = AV_PIX_FMT_CUDA;
#endif
break;
}
ff_thread_release_buffer(avctx, &f->tf);
av_buffer_unref(&f->hwaccel_priv_buf);
f->hwaccel_picture_private = NULL;
+ av_buffer_unref(&f->header_ref);
+ f->raw_frame_header = NULL;
f->spatial_id = f->temporal_id = 0;
- f->order_hint = 0;
memset(f->skip_mode_frame_idx, 0,
2 * sizeof(uint8_t));
+ memset(&f->film_grain, 0, sizeof(f->film_grain));
f->coded_lossless = 0;
}
if (ret < 0)
return ret;
+ dst->header_ref = av_buffer_ref(src->header_ref);
+ if (!dst->header_ref)
+ goto fail;
+
+ dst->raw_frame_header = src->raw_frame_header;
+
if (src->hwaccel_picture_private) {
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
if (!dst->hwaccel_priv_buf)
memcpy(dst->gm_params,
src->gm_params,
AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
- dst->order_hint = src->order_hint;
memcpy(dst->skip_mode_frame_idx,
src->skip_mode_frame_idx,
2 * sizeof(uint8_t));
+ memcpy(&dst->film_grain,
+ &src->film_grain,
+ sizeof(dst->film_grain));
dst->coded_lossless = src->coded_lossless;
return 0;
if (ret < 0)
return ret;
+ av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
+
if (avctx->extradata && avctx->extradata_size) {
- ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
- avctx->extradata_size);
+ ret = ff_cbs_read_extradata_from_codec(s->cbc,
+ &s->current_obu,
+ avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
return ret;
return ret;
}
+ f->header_ref = av_buffer_ref(s->header_ref);
+ if (!f->header_ref)
+ return AVERROR(ENOMEM);
+
+ f->raw_frame_header = s->raw_frame_header;
+
if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
- return ret;
+ goto fail;
frame = f->tf.f;
frame->key_frame = header->frame_type == AV1_FRAME_KEY;
if (hwaccel->frame_priv_data_size) {
f->hwaccel_priv_buf =
av_buffer_allocz(hwaccel->frame_priv_data_size);
- if (!f->hwaccel_priv_buf)
+ if (!f->hwaccel_priv_buf) {
+ ret = AVERROR(ENOMEM);
goto fail;
+ }
f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
}
}
fail:
av1_frame_unref(avctx, f);
- return AVERROR(ENOMEM);
+ return ret;
+}
+
+static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
+{
+ AV1DecContext *s = avctx->priv_data;
+ const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
+ AVFilmGrainParams *fgp;
+ AVFilmGrainAOMParams *aom;
+
+ if (!film_grain->apply_grain)
+ return 0;
+
+ fgp = av_film_grain_params_create_side_data(frame);
+ if (!fgp)
+ return AVERROR(ENOMEM);
+
+ fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
+ fgp->seed = film_grain->grain_seed;
+
+ aom = &fgp->codec.aom;
+ aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
+ aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8;
+ aom->ar_coeff_lag = film_grain->ar_coeff_lag;
+ aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6;
+ aom->grain_scale_shift = film_grain->grain_scale_shift;
+ aom->overlap_flag = film_grain->overlap_flag;
+ aom->limit_output_range = film_grain->clip_to_restricted_range;
+
+ aom->num_y_points = film_grain->num_y_points;
+ for (int i = 0; i < film_grain->num_y_points; i++) {
+ aom->y_points[i][0] = film_grain->point_y_value[i];
+ aom->y_points[i][1] = film_grain->point_y_scaling[i];
+ }
+ aom->num_uv_points[0] = film_grain->num_cb_points;
+ for (int i = 0; i < film_grain->num_cb_points; i++) {
+ aom->uv_points[0][i][0] = film_grain->point_cb_value[i];
+ aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i];
+ }
+ aom->num_uv_points[1] = film_grain->num_cr_points;
+ for (int i = 0; i < film_grain->num_cr_points; i++) {
+ aom->uv_points[1][i][0] = film_grain->point_cr_value[i];
+ aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i];
+ }
+
+ for (int i = 0; i < 24; i++) {
+ aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
+ }
+ for (int i = 0; i < 25; i++) {
+ aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128;
+ aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128;
+ }
+
+ aom->uv_mult[0] = film_grain->cb_mult;
+ aom->uv_mult[1] = film_grain->cr_mult;
+ aom->uv_mult_luma[0] = film_grain->cb_luma_mult;
+ aom->uv_mult_luma[1] = film_grain->cr_luma_mult;
+ aom->uv_offset[0] = film_grain->cb_offset;
+ aom->uv_offset[1] = film_grain->cr_offset;
+
+ return 0;
}
static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
const AVFrame *srcframe = s->cur_frame.tf.f;
int ret;
+ // TODO: all layers
+ if (s->operating_point_idc &&
+ av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
+ return 0;
+
ret = av_frame_ref(frame, srcframe);
if (ret < 0)
return ret;
+ if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
+ ret = export_film_grain(avctx, frame);
+ if (ret < 0) {
+ av_frame_unref(frame);
+ return ret;
+ }
+ }
+
frame->pts = pkt->pts;
frame->pkt_dts = pkt->dts;
frame->pkt_size = pkt->size;
global_motion_params(s);
skip_mode_params(s);
coded_lossless_param(s);
+ load_grain_params(s);
return ret;
}
goto end;
}
+ s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
+
if (s->pix_fmt == AV_PIX_FMT_NONE) {
ret = get_pixel_format(avctx);
if (ret < 0) {
s->cur_frame.spatial_id = header->spatial_id;
s->cur_frame.temporal_id = header->temporal_id;
- s->cur_frame.order_hint = s->raw_frame_header->order_hint;
-
if (avctx->hwaccel) {
ret = avctx->hwaccel->start_frame(avctx, unit->data,
unit->data_size);
av1_frame_unref(avctx, &s->ref[i]);
av1_frame_unref(avctx, &s->cur_frame);
+ s->operating_point_idc = 0;
s->raw_frame_header = NULL;
s->raw_seq = NULL;
ff_cbs_flush(s->cbc);
}
-AVCodec ff_av1_decoder = {
+#define OFFSET(x) offsetof(AV1DecContext, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+static const AVOption av1_options[] = {
+ { "operating_point", "Select an operating point of the scalable bitstream",
+ OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
+ { NULL }
+};
+
+static const AVClass av1_class = {
+ .class_name = "AV1 decoder",
+ .item_name = av_default_item_name,
+ .option = av1_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+const AVCodec ff_av1_decoder = {
.name = "av1",
.long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
.type = AVMEDIA_TYPE_VIDEO,
FF_CODEC_CAP_SETS_PKT_DTS,
.flush = av1_decode_flush,
.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
- .hw_configs = (const AVCodecHWConfigInternal * []) {
+ .priv_class = &av1_class,
+ .hw_configs = (const AVCodecHWConfigInternal *const []) {
+#if CONFIG_AV1_DXVA2_HWACCEL
+ HWACCEL_DXVA2(av1),
+#endif
+#if CONFIG_AV1_D3D11VA_HWACCEL
+ HWACCEL_D3D11VA(av1),
+#endif
+#if CONFIG_AV1_D3D11VA2_HWACCEL
+ HWACCEL_D3D11VA2(av1),
+#endif
+#if CONFIG_AV1_NVDEC_HWACCEL
+ HWACCEL_NVDEC(av1),
+#endif
#if CONFIG_AV1_VAAPI_HWACCEL
HWACCEL_VAAPI(av1),
#endif