Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions include/avif/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ typedef enum avifResult
AVIF_RESULT_WAITING_ON_IO, // similar to EAGAIN/EWOULDBLOCK, this means the avifIO doesn't have necessary data available yet
AVIF_RESULT_INVALID_ARGUMENT, // an argument passed into this function is invalid
AVIF_RESULT_NOT_IMPLEMENTED, // a requested code path is not (yet) implemented
AVIF_RESULT_OUT_OF_MEMORY
AVIF_RESULT_OUT_OF_MEMORY,
AVIF_RESULT_CANNOT_CHANGE_SETTING, // a setting that can't change is changed during encoding
AVIF_RESULT_INCOMPATIBLE_IMAGE // given image is not compatible with already encoded image
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I do not mind adding values to this enum but maybe we should be careful not to have too many. There is avifEncoder::diag that was meant for that, so maybe we should just return INVALID_PARAMETER and use a descriptive diag string for rare-ish errors in future PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understand. If both are newly added, I would make AVIF_RESULT_INVALID_IMAGE_GRID and AVIF_RESULT_INCOMPATIBLE_IMAGE share one value, but AVIF_RESULT_INVALID_IMAGE_GRID is already there and is very specific about grid.

} avifResult;

AVIF_API const char * avifResultToString(avifResult result);
Expand Down Expand Up @@ -1037,12 +1039,17 @@ struct avifCodecSpecificOptions;
// image in less bytes. AVIF_SPEED_DEFAULT means "Leave the AV1 codec to its default speed settings"./
// If avifEncoder uses rav1e, the speed value is directly passed through (0-10). If libaom is used,
// a combination of settings are tweaked to simulate this speed range.
// * AV1 encoder settings and codec specific options set by avifEncoderSetCodecSpecificOption()
// will be applied / updated to AV1 encoder before each call to avifEncoderAddImage().
typedef struct avifEncoder
{
// Defaults to AVIF_CODEC_CHOICE_AUTO: Preference determined by order in availableCodecs table (avif.c)
avifCodecChoice codecChoice;

// settings (see Notes above)
int keyframeInterval; // How many frames between automatic forced keyframes; 0 to disable (default).
uint64_t timescale; // timescale of the media (Hz)
// AV1 encoder settings.
int maxThreads;
int minQuantizer;
int maxQuantizer;
Expand All @@ -1051,8 +1058,6 @@ typedef struct avifEncoder
int tileRowsLog2;
int tileColsLog2;
int speed;
int keyframeInterval; // How many frames between automatic forced keyframes; 0 to disable (default).
uint64_t timescale; // timescale of the media (Hz)

// stats from the most recent write
avifIOStats ioStats;
Expand Down Expand Up @@ -1104,9 +1109,7 @@ AVIF_API avifResult avifEncoderAddImageGrid(avifEncoder * encoder,
avifAddImageFlags addImageFlags);
AVIF_API avifResult avifEncoderFinish(avifEncoder * encoder, avifRWData * output);

// Codec-specific, optional "advanced" tuning settings, in the form of string key/value pairs. These
// should be set as early as possible, preferably just after creating avifEncoder but before
// performing any other actions.
// Codec-specific, optional "advanced" tuning settings, in the form of string key/value pairs.
// key must be non-NULL, but passing a NULL value will delete that key, if it exists.
// Setting an incorrect or unknown option for the current codec will cause errors of type
// AVIF_RESULT_INVALID_CODEC_SPECIFIC_OPTION from avifEncoderWrite() or avifEncoderAddImage().
Expand Down
1 change: 1 addition & 0 deletions include/avif/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ typedef avifResult (*avifCodecEncodeImageFunc)(struct avifCodec * codec,
avifEncoder * encoder,
const avifImage * image,
avifBool alpha,
avifBool updateConfig,
avifAddImageFlags addImageFlags,
avifCodecEncodeOutput * output);
typedef avifBool (*avifCodecEncodeFinishFunc)(struct avifCodec * codec, avifCodecEncodeOutput * output);
Expand Down
2 changes: 2 additions & 0 deletions src/avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ const char * avifResultToString(avifResult result)
case AVIF_RESULT_INVALID_ARGUMENT: return "Invalid argument";
case AVIF_RESULT_NOT_IMPLEMENTED: return "Not implemented";
case AVIF_RESULT_OUT_OF_MEMORY: return "Out of memory";
case AVIF_RESULT_CANNOT_CHANGE_SETTING: return "Can not change some settings during encoding";
case AVIF_RESULT_INCOMPATIBLE_IMAGE: return "This image is incompatible with already encoded image";
case AVIF_RESULT_UNKNOWN_ERROR:
default:
break;
Expand Down
105 changes: 61 additions & 44 deletions src/codec_aom.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct avifCodecInternal
#if defined(AVIF_CODEC_AOM_ENCODE)
avifBool encoderInitialized;
aom_codec_ctx_t encoder;
struct aom_codec_enc_cfg cfg;
avifPixelFormatInfo formatInfo;
aom_img_fmt_t aomFormat;
avifBool monochromeEnabled;
Expand Down Expand Up @@ -525,10 +526,11 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
avifEncoder * encoder,
const avifImage * image,
avifBool alpha,
avifBool updateConfig,
avifAddImageFlags addImageFlags,
avifCodecEncodeOutput * output)
{
if (!codec->internal->encoderInitialized) {
if (!codec->internal->encoderInitialized || updateConfig) {
// Map encoder speed to AOM usage + CpuUsed:
// Speed 0: GoodQuality CpuUsed 0
// Speed 1: GoodQuality CpuUsed 1
Expand Down Expand Up @@ -586,37 +588,41 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
}
}

codec->internal->aomFormat = avifImageCalcAOMFmt(image, alpha);
if (codec->internal->aomFormat == AOM_IMG_FMT_NONE) {
return AVIF_RESULT_UNKNOWN_ERROR;
}
struct aom_codec_enc_cfg * cfg = &codec->internal->cfg;

avifGetPixelFormatInfo(image->yuvFormat, &codec->internal->formatInfo);
aom_codec_iface_t * encoderInterface = NULL;
if (!codec->internal->encoderInitialized) {
codec->internal->aomFormat = avifImageCalcAOMFmt(image, alpha);
if (codec->internal->aomFormat == AOM_IMG_FMT_NONE) {
return AVIF_RESULT_UNKNOWN_ERROR;
}

aom_codec_iface_t * encoderInterface = aom_codec_av1_cx();
struct aom_codec_enc_cfg cfg;
aom_codec_err_t err = aom_codec_enc_config_default(encoderInterface, &cfg, aomUsage);
if (err != AOM_CODEC_OK) {
avifDiagnosticsPrintf(codec->diag, "aom_codec_enc_config_default() failed: %s", aom_codec_err_to_string(err));
return AVIF_RESULT_UNKNOWN_ERROR;
avifGetPixelFormatInfo(image->yuvFormat, &codec->internal->formatInfo);

encoderInterface = aom_codec_av1_cx();
aom_codec_err_t err = aom_codec_enc_config_default(encoderInterface, cfg, aomUsage);
if (err != AOM_CODEC_OK) {
avifDiagnosticsPrintf(codec->diag, "aom_codec_enc_config_default() failed: %s", aom_codec_err_to_string(err));
return AVIF_RESULT_UNKNOWN_ERROR;
}
}

// Set our own default cfg.rc_end_usage value, which may differ from libaom's default.
// Set our own default cfg->rc_end_usage value, which may differ from libaom's default.
switch (aomUsage) {
case AOM_USAGE_GOOD_QUALITY:
// libaom's default is AOM_VBR. Change the default to AOM_Q since we don't need to
// hit a certain target bit rate. It's easier to control the worst quality in Q
// mode.
cfg.rc_end_usage = AOM_Q;
cfg->rc_end_usage = AOM_Q;
break;
case AOM_USAGE_REALTIME:
// For real-time mode we need to use CBR rate control mode. AOM_Q doesn't fit the
// rate control requirements for real-time mode. CBR does.
cfg.rc_end_usage = AOM_CBR;
cfg->rc_end_usage = AOM_CBR;
break;
#if defined(AOM_USAGE_ALL_INTRA)
case AOM_USAGE_ALL_INTRA:
cfg.rc_end_usage = AOM_Q;
cfg->rc_end_usage = AOM_Q;
break;
#endif
}
Expand Down Expand Up @@ -655,31 +661,31 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
}
}

cfg.g_profile = seqProfile;
cfg.g_bit_depth = image->depth;
cfg.g_input_bit_depth = image->depth;
cfg.g_w = image->width;
cfg.g_h = image->height;
cfg->g_profile = seqProfile;
cfg->g_bit_depth = image->depth;
cfg->g_input_bit_depth = image->depth;
cfg->g_w = image->width;
cfg->g_h = image->height;
if (addImageFlags & AVIF_ADD_IMAGE_FLAG_SINGLE) {
// Set the maximum number of frames to encode to 1. This instructs
// libaom to set still_picture and reduced_still_picture_header to
// 1 in AV1 sequence headers.
cfg.g_limit = 1;
cfg->g_limit = 1;

// Use the default settings of the new AOM_USAGE_ALL_INTRA (added in
// https://crbug.com/aomedia/2959).
//
// Set g_lag_in_frames to 0 to reduce the number of frame buffers
// (from 20 to 2) in libaom's lookahead structure. This reduces
// memory consumption when encoding a single image.
cfg.g_lag_in_frames = 0;
cfg->g_lag_in_frames = 0;
// Disable automatic placement of key frames by the encoder.
cfg.kf_mode = AOM_KF_DISABLED;
cfg->kf_mode = AOM_KF_DISABLED;
// Tell libaom that all frames will be key frames.
cfg.kf_max_dist = 0;
cfg->kf_max_dist = 0;
}
if (encoder->maxThreads > 1) {
cfg.g_threads = encoder->maxThreads;
cfg->g_threads = encoder->maxThreads;
}

int minQuantizer = AVIF_CLAMP(encoder->minQuantizer, 0, 63);
Expand All @@ -689,41 +695,52 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
maxQuantizer = AVIF_CLAMP(encoder->maxQuantizerAlpha, 0, 63);
}
avifBool lossless = ((minQuantizer == AVIF_QUANTIZER_LOSSLESS) && (maxQuantizer == AVIF_QUANTIZER_LOSSLESS));
cfg.rc_min_quantizer = minQuantizer;
cfg.rc_max_quantizer = maxQuantizer;
cfg->rc_min_quantizer = minQuantizer;
cfg->rc_max_quantizer = maxQuantizer;

codec->internal->monochromeEnabled = AVIF_FALSE;
if (aomVersion > aomVersion_2_0_0) {
// There exists a bug in libaom's chroma_check() function where it will attempt to
// access nonexistent UV planes when encoding monochrome at faster libavif "speeds". It
// was fixed shortly after the 2.0.0 libaom release, and the fix exists in both the
// master and applejack branches. This ensures that the next version *after* 2.0.0 will
// have the fix, and we must avoid cfg.monochrome until then.
// have the fix, and we must avoid cfg->monochrome until then.
//
// Bugfix Change-Id: https://aomedia-review.googlesource.com/q/I26a39791f820b4d4e1d63ff7141f594c3c7181f5

if (alpha || (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV400)) {
codec->internal->monochromeEnabled = AVIF_TRUE;
cfg.monochrome = 1;
cfg->monochrome = 1;
}
}

if (!avifProcessAOMOptionsPreInit(codec, alpha, &cfg)) {
if (!avifProcessAOMOptionsPreInit(codec, alpha, cfg)) {
return AVIF_RESULT_INVALID_CODEC_SPECIFIC_OPTION;
}

aom_codec_flags_t encoderFlags = 0;
if (image->depth > 8) {
encoderFlags |= AOM_CODEC_USE_HIGHBITDEPTH;
}
if (aom_codec_enc_init(&codec->internal->encoder, encoderInterface, &cfg, encoderFlags) != AOM_CODEC_OK) {
avifDiagnosticsPrintf(codec->diag,
"aom_codec_enc_init() failed: %s: %s",
aom_codec_error(&codec->internal->encoder),
aom_codec_error_detail(&codec->internal->encoder));
return AVIF_RESULT_UNKNOWN_ERROR;
if (!codec->internal->encoderInitialized) {
aom_codec_flags_t encoderFlags = 0;
if (image->depth > 8) {
encoderFlags |= AOM_CODEC_USE_HIGHBITDEPTH;
}

if (aom_codec_enc_init(&codec->internal->encoder, encoderInterface, cfg, encoderFlags) != AOM_CODEC_OK) {
avifDiagnosticsPrintf(codec->diag,
"aom_codec_enc_init() failed: %s: %s",
aom_codec_error(&codec->internal->encoder),
aom_codec_error_detail(&codec->internal->encoder));
return AVIF_RESULT_UNKNOWN_ERROR;
}
codec->internal->encoderInitialized = AVIF_TRUE;
} else {
if (aom_codec_enc_config_set(&codec->internal->encoder, cfg) != AOM_CODEC_OK) {
avifDiagnosticsPrintf(codec->diag,
"aom_codec_enc_config_set() failed: %s: %s",
aom_codec_error(&codec->internal->encoder),
aom_codec_error_detail(&codec->internal->encoder));
return AVIF_RESULT_UNKNOWN_ERROR;
}
}
codec->internal->encoderInitialized = AVIF_TRUE;

if (lossless) {
aom_codec_control(&codec->internal->encoder, AV1E_SET_LOSSLESS, 1);
Expand Down Expand Up @@ -754,8 +771,8 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
// set the min and max quantizers in the avifEncoder struct. If this is the case, set
// cq-level to a reasonable value for the user, otherwise the default cq-level
// (currently 10) will be unknowingly used.
assert(cfg.rc_end_usage == AOM_Q);
unsigned int cqLevel = (cfg.rc_min_quantizer + cfg.rc_max_quantizer) / 2;
assert(cfg->rc_end_usage == AOM_Q);
unsigned int cqLevel = (cfg->rc_min_quantizer + cfg->rc_max_quantizer) / 2;
aom_codec_control(&codec->internal->encoder, AOME_SET_CQ_LEVEL, cqLevel);
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/codec_rav1e.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ static avifResult rav1eCodecEncodeImage(avifCodec * codec,
avifEncoder * encoder,
const avifImage * image,
avifBool alpha,
avifBool updateConfig,
uint32_t addImageFlags,
avifCodecEncodeOutput * output)
{
if (updateConfig) {
return AVIF_RESULT_NOT_IMPLEMENTED;
}

avifResult result = AVIF_RESULT_UNKNOWN_ERROR;

RaConfig * rav1eConfig = NULL;
Expand Down
5 changes: 5 additions & 0 deletions src/codec_svt.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ static avifResult svtCodecEncodeImage(avifCodec * codec,
avifEncoder * encoder,
const avifImage * image,
avifBool alpha,
avifBool updateConfig,
uint32_t addImageFlags,
avifCodecEncodeOutput * output)
{
if (updateConfig) {
return AVIF_RESULT_NOT_IMPLEMENTED;
}

avifResult result = AVIF_RESULT_UNKNOWN_ERROR;
EbColorFormat color_format = EB_YUV420;
EbBufferHeaderType * input_buffer = NULL;
Expand Down
Loading