Skip to content

Commit f2c9c68

Browse files
committed
Improve libaom error handling with shared helper function
Add a static helper function aomDiagPrintf() that writes libaom error code and error detail into codec diagnostics, handling the case where aom_codec_error_detail() returns a null pointer. Decoder changes: - Call aom_codec_peek_stream_info() on every image to handle AV1 sequence header changes between frames - Add diagnostics for aom_codec_dec_init(), aom_codec_control(), and aom_codec_peek_stream_info() failures - Log image dimensions when they are too large - Remove unnecessary aom_codec_av1_dx() null check (it always returns a valid pointer) Encoder changes: - Make aom_codec_error_detail() usage null-safe in all encoder paths - Use the shared helper function for aom_codec_enc_init(), aom_codec_enc_config_set(), and aom_codec_encode() diagnostics No functional changes beyond improved diagnostics.
1 parent e40faf5 commit f2c9c68

1 file changed

Lines changed: 55 additions & 29 deletions

File tree

src/codec_aom.c

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ static void aomCodecDestroyInternal(avifCodec * codec)
8585
avifFree(codec->internal);
8686
}
8787

88+
// Writes a libaom error code and error detail into the diagnostics.
89+
static void aomDiagPrintf(avifDiagnostics * diag, const char * func, const char * error, const char * detail)
90+
{
91+
avifDiagnosticsPrintf(diag, "%s failed: %s: %s", func, error, detail ? detail : "no error detail");
92+
}
93+
8894
#if defined(AVIF_CODEC_AOM_DECODE)
8995

9096
static avifBool aomCodecGetNextImage(struct avifCodec * codec,
@@ -95,34 +101,53 @@ static avifBool aomCodecGetNextImage(struct avifCodec * codec,
95101
{
96102
assert(sample);
97103

98-
if (!codec->internal->decoderInitialized) {
99-
aom_codec_iface_t * const decoderInterface = aom_codec_av1_dx();
100-
struct aom_codec_stream_info streamInfo = { 0 };
101-
if (aom_codec_peek_stream_info(decoderInterface, sample->data.data, sample->data.size, &streamInfo) != AOM_CODEC_OK) {
102-
return AVIF_FALSE;
103-
}
104-
if (streamInfo.w == 0 || streamInfo.h == 0) {
105-
// The sequence header was not found: treat it as an error.
104+
aom_codec_iface_t * const decoderInterface = aom_codec_av1_dx();
105+
struct aom_codec_stream_info streamInfo = { 0 };
106+
aom_codec_err_t err = aom_codec_peek_stream_info(decoderInterface, sample->data.data, sample->data.size, &streamInfo);
107+
if (err != AOM_CODEC_OK) {
108+
avifDiagnosticsPrintf(codec->diag, "aom_codec_peek_stream_info() failed: %s", aom_codec_err_to_string(err));
109+
return AVIF_FALSE;
110+
}
111+
if (streamInfo.w == 0 || streamInfo.h == 0) {
112+
// The sequence header was not found.
113+
if (!codec->internal->decoderInitialized) {
114+
// Treat it as an error if the first frame isn't preceded by a sequence header.
106115
return AVIF_FALSE;
107116
}
117+
} else {
108118
if (avifDimensionsTooLarge(streamInfo.w, streamInfo.h, codec->imageSizeLimit, codec->imageDimensionLimit)) {
119+
avifDiagnosticsPrintf(codec->diag, "Image dimensions too large: %dx%d", streamInfo.w, streamInfo.h);
109120
return AVIF_FALSE;
110121
}
122+
}
111123

124+
if (!codec->internal->decoderInitialized) {
112125
aom_codec_dec_cfg_t cfg;
113126
memset(&cfg, 0, sizeof(aom_codec_dec_cfg_t));
114127
cfg.threads = codec->maxThreads;
115128
cfg.allow_lowbitdepth = 1;
116129

117130
if (aom_codec_dec_init(&codec->internal->decoder, decoderInterface, &cfg, 0)) {
131+
aomDiagPrintf(codec->diag,
132+
"aom_codec_dec_init()",
133+
aom_codec_error(&codec->internal->decoder),
134+
aom_codec_error_detail(&codec->internal->decoder));
118135
return AVIF_FALSE;
119136
}
120137
codec->internal->decoderInitialized = AVIF_TRUE;
121138

122139
if (aom_codec_control(&codec->internal->decoder, AV1D_SET_OUTPUT_ALL_LAYERS, codec->allLayers)) {
140+
aomDiagPrintf(codec->diag,
141+
"aom_codec_control(AV1D_SET_OUTPUT_ALL_LAYERS)",
142+
aom_codec_error(&codec->internal->decoder),
143+
aom_codec_error_detail(&codec->internal->decoder));
123144
return AVIF_FALSE;
124145
}
125146
if (aom_codec_control(&codec->internal->decoder, AV1D_SET_OPERATING_POINT, codec->operatingPoint)) {
147+
aomDiagPrintf(codec->diag,
148+
"aom_codec_control(AV1D_SET_OPERATING_POINT)",
149+
aom_codec_error(&codec->internal->decoder),
150+
aom_codec_error_detail(&codec->internal->decoder));
126151
return AVIF_FALSE;
127152
}
128153

@@ -148,10 +173,10 @@ static avifBool aomCodecGetNextImage(struct avifCodec * codec,
148173
} else if (sample) {
149174
codec->internal->iter = NULL;
150175
if (aom_codec_decode(&codec->internal->decoder, sample->data.data, sample->data.size, NULL)) {
151-
avifDiagnosticsPrintf(codec->diag,
152-
"aom_codec_decode() failed: %s: %s",
153-
aom_codec_error(&codec->internal->decoder),
154-
aom_codec_error_detail(&codec->internal->decoder));
176+
aomDiagPrintf(codec->diag,
177+
"aom_codec_decode()",
178+
aom_codec_error(&codec->internal->decoder),
179+
aom_codec_error_detail(&codec->internal->decoder));
155180
return AVIF_FALSE;
156181
}
157182
spatialID = sample->spatialID;
@@ -507,12 +532,13 @@ static avifBool avifProcessAOMOptionsPostInit(avifCodec * codec, avifBool alpha)
507532
key += shortPrefixLen;
508533
}
509534
if (aom_codec_set_option(&codec->internal->encoder, key, entry->value) != AOM_CODEC_OK) {
535+
const char * error_detail = aom_codec_error_detail(&codec->internal->encoder);
510536
avifDiagnosticsPrintf(codec->diag,
511537
"aom_codec_set_option(\"%s\", \"%s\") failed: %s: %s",
512538
key,
513539
entry->value,
514540
aom_codec_error(&codec->internal->encoder),
515-
aom_codec_error_detail(&codec->internal->encoder));
541+
error_detail ? error_detail : "no error detail");
516542
return AVIF_FALSE;
517543
}
518544
#else // !defined(HAVE_AOM_CODEC_SET_OPTION)
@@ -922,10 +948,10 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
922948
encoderFlags |= AOM_CODEC_USE_HIGHBITDEPTH;
923949
}
924950
if (aom_codec_enc_init(&codec->internal->encoder, encoderInterface, cfg, encoderFlags) != AOM_CODEC_OK) {
925-
avifDiagnosticsPrintf(codec->diag,
926-
"aom_codec_enc_init() failed: %s: %s",
927-
aom_codec_error(&codec->internal->encoder),
928-
aom_codec_error_detail(&codec->internal->encoder));
951+
aomDiagPrintf(codec->diag,
952+
"aom_codec_enc_init()",
953+
aom_codec_error(&codec->internal->encoder),
954+
aom_codec_error_detail(&codec->internal->encoder));
929955
return AVIF_RESULT_UNKNOWN_ERROR;
930956
}
931957
codec->internal->encoderInitialized = AVIF_TRUE;
@@ -1076,10 +1102,10 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
10761102
if (quantizerUpdated || dimensionsChanged) {
10771103
aom_codec_err_t err = aom_codec_enc_config_set(&codec->internal->encoder, cfg);
10781104
if (err != AOM_CODEC_OK) {
1079-
avifDiagnosticsPrintf(codec->diag,
1080-
"aom_codec_enc_config_set() failed: %s: %s",
1081-
aom_codec_error(&codec->internal->encoder),
1082-
aom_codec_error_detail(&codec->internal->encoder));
1105+
aomDiagPrintf(codec->diag,
1106+
"aom_codec_enc_config_set()",
1107+
aom_codec_error(&codec->internal->encoder),
1108+
aom_codec_error_detail(&codec->internal->encoder));
10831109
return AVIF_RESULT_UNKNOWN_ERROR;
10841110
}
10851111
}
@@ -1297,10 +1323,10 @@ static avifResult aomCodecEncodeImage(avifCodec * codec,
12971323
aom_img_free(&aomImage);
12981324
}
12991325
if (encodeErr != AOM_CODEC_OK) {
1300-
avifDiagnosticsPrintf(codec->diag,
1301-
"aom_codec_encode() failed: %s: %s",
1302-
aom_codec_error(&codec->internal->encoder),
1303-
aom_codec_error_detail(&codec->internal->encoder));
1326+
aomDiagPrintf(codec->diag,
1327+
"aom_codec_encode()",
1328+
aom_codec_error(&codec->internal->encoder),
1329+
aom_codec_error_detail(&codec->internal->encoder));
13041330
return AVIF_RESULT_UNKNOWN_ERROR;
13051331
}
13061332

@@ -1341,10 +1367,10 @@ static avifBool aomCodecEncodeFinish(avifCodec * codec, avifCodecEncodeOutput *
13411367
for (;;) {
13421368
// flush encoder
13431369
if (aom_codec_encode(&codec->internal->encoder, NULL, 0, 1, 0) != AOM_CODEC_OK) {
1344-
avifDiagnosticsPrintf(codec->diag,
1345-
"aom_codec_encode() with img=NULL failed: %s: %s",
1346-
aom_codec_error(&codec->internal->encoder),
1347-
aom_codec_error_detail(&codec->internal->encoder));
1370+
aomDiagPrintf(codec->diag,
1371+
"aom_codec_encode() with img=NULL",
1372+
aom_codec_error(&codec->internal->encoder),
1373+
aom_codec_error_detail(&codec->internal->encoder));
13481374
return AVIF_FALSE;
13491375
}
13501376

0 commit comments

Comments
 (0)