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
247 return 0;247 return 0;
248}248}
249 249
250static 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
250/**317/**
251 * Sends metadata to libjxl based on the first frame of the stream, such as pixel format,318 * Sends metadata to libjxl based on the first frame of the stream, such as pixel format,
252 * orientation, bit depth, and that sort of thing.319 * 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
254static int libjxl_preprocess_stream(AVCodecContext *avctx, const AVFrame *frame, int animated)321static int libjxl_preprocess_stream(AVCodecContext *avctx, const AVFrame *frame, int animated)
255{322{
256 LibJxlEncodeContext *ctx = avctx->priv_data;323 LibJxlEncodeContext *ctx = avctx->priv_data;
257 int ret;
258 AVFrameSideData *sd;324 AVFrameSideData *sd;
259 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);325 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
260 JxlBasicInfo info;326 JxlBasicInfo info;
261 JxlColorEncoding jxl_color;
262 JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;327 JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
263 int bits_per_sample;328 int bits_per_sample;
264#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)329#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
320 return AVERROR_EXTERNAL;385 return AVERROR_EXTERNAL;
321 }386 }
322 387
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
378 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);388 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) {
380 av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");390 av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");
381 if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS)391 sd = NULL;
382 av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");392 }
393
394 if (!sd || !sd->size)
395 libjxl_populate_colorspace(avctx, frame, pix_desc, &info);
383 396
384#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)397#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
385 if (JxlEncoderSetFrameBitDepth(ctx->options, &jxl_bit_depth) != JXL_ENC_SUCCESS)398 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