History

avcodec/libjxlenc: prevent color encoding from being set twice

We currently populate the color encoding bundle and then check to see if there's an ICC profile to attach, and set the color encoding bundle in either case. The ICC profile overrides the color encoding bundle, so we should not calculate enum-based color encoding if we have an ICC profile present. Fixes several unnecessary warnings from being emitted.

Leo Izen committed 1 year ago

Commit b0950101 parent afe6c121 file · +73 −60Browse files

Unified · Split

libavcodec/libjxlenc.c+73−60
@@ -247,6 +247,73 @@ static int libjxl_populate_primaries(void *avctx, JxlColorEncoding *jxl_color, e · 246 unmodified lines
247247 return 0;
248248 }
249249
250+static int libjxl_populate_colorspace(AVCodecContext *avctx, const AVFrame *frame,
251+ const AVPixFmtDescriptor *pix_desc, const JxlBasicInfo *info)
252+{
253+ JxlColorEncoding jxl_color;
254+ LibJxlEncodeContext *ctx = avctx->priv_data;
255+ int ret;
256+
257+ switch (frame->color_trc && frame->color_trc != AVCOL_TRC_UNSPECIFIED
258+ ? frame->color_trc : avctx->color_trc) {
259+ case AVCOL_TRC_BT709:
260+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_709;
261+ break;
262+ case AVCOL_TRC_LINEAR:
263+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
264+ break;
265+ case AVCOL_TRC_IEC61966_2_1:
266+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
267+ break;
268+ case AVCOL_TRC_SMPTE428:
269+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_DCI;
270+ break;
271+ case AVCOL_TRC_SMPTE2084:
272+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_PQ;
273+ break;
274+ case AVCOL_TRC_ARIB_STD_B67:
275+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_HLG;
276+ break;
277+ case AVCOL_TRC_GAMMA22:
278+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
279+ jxl_color.gamma = 1/2.2f;
280+ break;
281+ case AVCOL_TRC_GAMMA28:
282+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
283+ jxl_color.gamma = 1/2.8f;
284+ break;
285+ default:
286+ if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
287+ av_log(avctx, AV_LOG_WARNING,
288+ "Unknown transfer function, assuming Linear Light. Colors may be wrong.\n");
289+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
290+ } else {
291+ av_log(avctx, AV_LOG_WARNING,
292+ "Unknown transfer function, assuming IEC61966-2-1/sRGB. Colors may be wrong.\n");
293+ jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
294+ }
295+ }
296+
297+ jxl_color.rendering_intent = JXL_RENDERING_INTENT_RELATIVE;
298+ if (info->num_color_channels == 1)
299+ jxl_color.color_space = JXL_COLOR_SPACE_GRAY;
300+ else
301+ jxl_color.color_space = JXL_COLOR_SPACE_RGB;
302+
303+ ret = libjxl_populate_primaries(avctx, &jxl_color,
304+ frame->color_primaries && frame->color_primaries != AVCOL_PRI_UNSPECIFIED
305+ ? frame->color_primaries : avctx->color_primaries);
306+ if (ret < 0)
307+ return ret;
308+
309+ if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS) {
310+ av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");
311+ return AVERROR_EXTERNAL;
312+ }
313+
314+ return 0;
315+}
316+
250317 /**
251318 * Sends metadata to libjxl based on the first frame of the stream, such as pixel format,
252319 * orientation, bit depth, and that sort of thing.
@@ -254,11 +321,9 @@ static int libjxl_populate_primaries(void *avctx, JxlColorEncoding *jxl_color, e · 1 unmodified line
254321 static int libjxl_preprocess_stream(AVCodecContext *avctx, const AVFrame *frame, int animated)
255322 {
256323 LibJxlEncodeContext *ctx = avctx->priv_data;
257− int ret;
258324 AVFrameSideData *sd;
259325 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
260326 JxlBasicInfo info;
261− JxlColorEncoding jxl_color;
262327 JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
263328 int bits_per_sample;
264329 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
@@ -320,66 +385,14 @@ static int libjxl_preprocess_stream(AVCodecContext *avctx, const AVFrame *frame, · 55 unmodified lines
320385 return AVERROR_EXTERNAL;
321386 }
322387
323− /* rendering intent doesn't matter here
324− * but libjxl will whine if we don't set it */
325− jxl_color.rendering_intent = JXL_RENDERING_INTENT_RELATIVE;
326−
327− switch (frame->color_trc && frame->color_trc != AVCOL_TRC_UNSPECIFIED
328− ? frame->color_trc : avctx->color_trc) {
329− case AVCOL_TRC_BT709:
330− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_709;
331− break;
332− case AVCOL_TRC_LINEAR:
333− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
334− break;
335− case AVCOL_TRC_IEC61966_2_1:
336− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
337− break;
338− case AVCOL_TRC_SMPTE428:
339− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_DCI;
340− break;
341− case AVCOL_TRC_SMPTE2084:
342− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_PQ;
343− break;
344− case AVCOL_TRC_ARIB_STD_B67:
345− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_HLG;
346− break;
347− case AVCOL_TRC_GAMMA22:
348− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
349− jxl_color.gamma = 1/2.2f;
350− break;
351− case AVCOL_TRC_GAMMA28:
352− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
353− jxl_color.gamma = 1/2.8f;
354− break;
355− default:
356− if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
357− av_log(avctx, AV_LOG_WARNING, "Unknown transfer function, assuming Linear Light. Colors may be wrong.\n");
358− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
359− } else {
360− av_log(avctx, AV_LOG_WARNING, "Unknown transfer function, assuming IEC61966-2-1/sRGB. Colors may be wrong.\n");
361− jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
362− }
363− }
364−
365− /* This should be implied to be honest
366− * but a libjxl bug makes it fail otherwise */
367− if (info.num_color_channels == 1)
368− jxl_color.color_space = JXL_COLOR_SPACE_GRAY;
369− else
370− jxl_color.color_space = JXL_COLOR_SPACE_RGB;
371−
372− ret = libjxl_populate_primaries(avctx, &jxl_color,
373− frame->color_primaries && frame->color_primaries != AVCOL_PRI_UNSPECIFIED
374− ? frame->color_primaries : avctx->color_primaries);
375− if (ret < 0)
376− return ret;
377−
378388 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
379− if (sd && sd->size && JxlEncoderSetICCProfile(ctx->encoder, sd->data, sd->size) != JXL_ENC_SUCCESS)
389+ if (sd && sd->size && JxlEncoderSetICCProfile(ctx->encoder, sd->data, sd->size) != JXL_ENC_SUCCESS) {
380390 av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");
381− if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS)
382− av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");
391+ sd = NULL;
392+ }
393+
394+ if (!sd || !sd->size)
395+ libjxl_populate_colorspace(avctx, frame, pix_desc, &info);
383396
384397 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
385398 if (JxlEncoderSetFrameBitDepth(ctx->options, &jxl_bit_depth) != JXL_ENC_SUCCESS)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t