Skip to content

Commit cf69925

Browse files
committed
Ensure colorspace and trc are set consistently
1 parent 2935333 commit cf69925

12 files changed

Lines changed: 192 additions & 114 deletions

src/framework/mlt.vers

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,12 @@ MLT_7.34.0 {
677677
mlt_image_colorspace_id;
678678
mlt_image_color_pri_name;
679679
mlt_image_color_pri_id;
680+
} MLT_7.32.0;
681+
682+
MLT_7.36.0 {
683+
global:
684+
mlt_image_default_colorspace;
680685
mlt_image_default_trc;
681686
mlt_image_default_primaries;
682687
mlt_color_convert_trc;
683-
} MLT_7.32.0;
688+
} MLT_7.34.0;

src/framework/mlt_image.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,48 @@ mlt_color_primaries mlt_image_color_pri_id(const char *name)
472472
return mlt_color_pri_none;
473473
}
474474

475+
/** Get the default colorspace for a given image format.
476+
*
477+
* \public \memberof mlt_image_s
478+
* \param format the format
479+
* \param height the image height. Pass 0 if you do not know.
480+
* \return a colorspace
481+
*/
482+
483+
mlt_colorspace mlt_image_default_colorspace(mlt_image_format format, int height)
484+
{
485+
mlt_colorspace colorspace = mlt_colorspace_bt709;
486+
switch (format) {
487+
case mlt_image_rgb:
488+
case mlt_image_rgba:
489+
case mlt_image_rgba64:
490+
case mlt_image_movit:
491+
case mlt_image_opengl_texture:
492+
colorspace = mlt_colorspace_rgb;
493+
break;
494+
case mlt_image_yuv422:
495+
case mlt_image_yuv420p:
496+
case mlt_image_yuv422p16:
497+
case mlt_image_yuv420p10:
498+
case mlt_image_yuv444p10:
499+
if (height < 576) {
500+
// Assume NTSC
501+
colorspace = mlt_colorspace_smpte170m;
502+
} else if (height < 720) {
503+
// Assume PAL
504+
colorspace = mlt_colorspace_bt470bg;
505+
} else {
506+
// Assume HDTV
507+
colorspace = mlt_color_trc_bt709;
508+
}
509+
break;
510+
case mlt_image_none:
511+
case mlt_image_invalid:
512+
break;
513+
}
514+
return colorspace;
515+
}
516+
475517
/** Get the default color transfer characteristics for a given colorspace.
476518
*
477519
* \public \memberof mlt_image_s

src/framework/mlt_image.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ MLT_EXPORT const char *mlt_image_colorspace_name(mlt_colorspace colorspace);
6969
MLT_EXPORT mlt_colorspace mlt_image_colorspace_id(const char *name);
7070
MLT_EXPORT const char *mlt_image_color_pri_name(mlt_color_primaries primaries);
7171
MLT_EXPORT mlt_color_primaries mlt_image_color_pri_id(const char *name);
72+
MLT_EXPORT mlt_colorspace mlt_image_default_colorspace(mlt_image_format format, int height);
7273
MLT_EXPORT mlt_color_trc mlt_image_default_trc(mlt_colorspace colorspace);
74+
MLT_EXPORT mlt_color_primaries mlt_image_default_primaries(mlt_colorspace colorspace, int height);
7375
MLT_EXPORT int mlt_image_rgba_opaque(uint8_t *image, int width, int height);
7476
MLT_EXPORT int mlt_image_full_range(const char *color_range);
7577

src/modules/avformat/common.c

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -582,61 +582,11 @@ mlt_color_primaries av_to_mlt_color_primaries(int primaries)
582582
return mlt_color_pri_none;
583583
}
584584

585-
mlt_color_primaries mlt_color_primaries_from_colorspace(mlt_colorspace colorspace, int height)
586-
{
587-
switch (colorspace) {
588-
case mlt_colorspace_rgb: // sRGB
589-
case mlt_colorspace_bt709:
590-
return mlt_color_pri_bt709;
591-
case mlt_colorspace_bt470bg:
592-
return mlt_color_pri_bt470bg;
593-
case mlt_colorspace_smpte240m:
594-
return mlt_color_pri_smpte170m;
595-
case mlt_colorspace_bt601:
596-
return height == 576 ? mlt_color_pri_bt470bg : mlt_color_pri_smpte170m;
597-
case mlt_colorspace_smpte170m:
598-
return mlt_color_pri_smpte170m;
599-
case mlt_colorspace_bt2020_ncl:
600-
return mlt_color_pri_bt2020;
601-
case mlt_colorspace_unspecified:
602-
case mlt_colorspace_reserved:
603-
case mlt_colorspace_fcc:
604-
case mlt_colorspace_ycgco:
605-
case mlt_colorspace_bt2020_cl:
606-
case mlt_colorspace_smpte2085:
607-
case mlt_colorspace_invalid:
608-
break;
609-
}
610-
return mlt_color_pri_none;
611-
}
612-
613585
int mlt_to_av_color_range(int full_range)
614586
{
615587
return full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
616588
}
617589

618-
mlt_color_trc mlt_color_trc_from_colorspace(mlt_colorspace colorspace)
619-
{
620-
switch (colorspace) {
621-
case mlt_colorspace_bt709:
622-
return mlt_color_trc_bt709;
623-
case mlt_colorspace_bt470bg:
624-
return mlt_color_trc_gamma28;
625-
case mlt_colorspace_smpte240m:
626-
return mlt_color_trc_smpte240m;
627-
case mlt_colorspace_rgb: // sRGB
628-
return mlt_color_trc_iec61966_2_1;
629-
case mlt_colorspace_bt601:
630-
case mlt_colorspace_smpte170m:
631-
return mlt_color_trc_smpte170m;
632-
case mlt_colorspace_bt2020_ncl:
633-
return mlt_color_trc_bt2020_10;
634-
default:
635-
break;
636-
}
637-
return mlt_color_trc_none;
638-
}
639-
640590
int av_to_mlt_full_range(int color_range)
641591
{
642592
return color_range == AVCOL_RANGE_JPEG;

src/modules/avformat/common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ int mlt_to_av_colorspace(mlt_colorspace colorspace, int height);
5050
mlt_colorspace av_to_mlt_colorspace(int colorspace, int width, int height);
5151
int mlt_to_av_color_primaries(mlt_color_primaries primaries);
5252
mlt_color_primaries av_to_mlt_color_primaries(int primaries);
53-
mlt_color_primaries mlt_color_primaries_from_colorspace(mlt_colorspace colorspace, int height);
54-
mlt_color_trc mlt_color_trc_from_colorspace(mlt_colorspace colorspace);
5553
int mlt_to_av_color_range(int full_range);
5654
int av_to_mlt_full_range(int color_range);
5755
void mlt_image_to_avframe(mlt_image image, mlt_frame mltframe, AVFrame *avframe);

src/modules/avformat/consumer_avformat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static void color_trc_from_colorspace(mlt_properties properties)
371371
// Default color transfer characteristic from MLT colorspace.
372372
const char *colorspace_str = mlt_properties_get(properties, "colorspace");
373373
const mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
374-
const mlt_color_trc trc = mlt_color_trc_from_colorspace(colorspace);
374+
const mlt_color_trc trc = mlt_image_default_trc(colorspace);
375375
mlt_properties_set_int(properties, "color_trc", trc);
376376
}
377377

@@ -381,7 +381,7 @@ static void color_primaries_from_colorspace(mlt_properties properties)
381381
const char *colorspace_str = mlt_properties_get(properties, "colorspace");
382382
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
383383
int height = mlt_properties_get_int(properties, "height");
384-
mlt_color_primaries primaries = mlt_color_primaries_from_colorspace(colorspace, height);
384+
mlt_color_primaries primaries = mlt_image_default_primaries(colorspace, height);
385385
if (primaries != mlt_color_pri_none)
386386
mlt_properties_set_int(properties, "color_primaries", primaries);
387387
}

src/modules/avformat/filter_avcolour_space.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,29 @@ static int convert_image(mlt_frame frame,
155155
if (*format != output_format || out_width) {
156156
mlt_profile profile = mlt_service_profile(
157157
MLT_PRODUCER_SERVICE(mlt_frame_get_original_producer(frame)));
158-
mlt_colorspace profile_colorspace = profile ? profile->colorspace : mlt_colorspace_bt601;
159-
const char *colorspace_str = mlt_properties_get(properties, "colorspace");
160-
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
161158
int width = mlt_properties_get_int(properties, "width");
162159
int height = mlt_properties_get_int(properties, "height");
160+
mlt_colorspace dst_colorspace = profile ? profile->colorspace : mlt_colorspace_bt601;
161+
const char *src_colorspace_str = mlt_properties_get(properties, "colorspace");
162+
mlt_colorspace src_colorspace = mlt_image_colorspace_id(src_colorspace_str);
163+
if (src_colorspace == mlt_colorspace_unspecified
164+
|| src_colorspace == mlt_colorspace_reserved
165+
|| src_colorspace == mlt_colorspace_invalid) {
166+
src_colorspace = mlt_image_default_colorspace(*format, height);
167+
}
163168
int src_full_range = mlt_properties_get_int(properties, "full_range");
164169
const char *dst_color_range = mlt_properties_get(properties, "consumer.color_range");
165170
int dst_full_range = mlt_image_full_range(dst_color_range);
171+
// Fix some producers that may not be setting properties correctly
172+
if (output_format == mlt_image_rgb || output_format == mlt_image_rgba
173+
|| output_format == mlt_image_rgba64) {
174+
dst_full_range = 1;
175+
dst_colorspace = mlt_colorspace_rgb;
176+
}
177+
if (*format == mlt_image_rgb || *format == mlt_image_rgba || *format == mlt_image_rgba64) {
178+
src_full_range = 1;
179+
src_colorspace = mlt_colorspace_rgb;
180+
}
166181

167182
if (out_width <= 0)
168183
out_width = width;
@@ -178,8 +193,8 @@ static int convert_image(mlt_frame frame,
178193
mlt_image_format_name(output_format),
179194
out_width,
180195
out_height,
181-
colorspace,
182-
profile_colorspace,
196+
src_colorspace,
197+
dst_colorspace,
183198
src_full_range,
184199
dst_full_range,
185200
mlt_frame_get_position(frame));
@@ -260,15 +275,11 @@ static int convert_image(mlt_frame frame,
260275
out_height,
261276
width,
262277
height,
263-
colorspace,
264-
profile_colorspace,
278+
src_colorspace,
279+
dst_colorspace,
265280
src_full_range,
266281
dst_full_range)) {
267-
// The new colorspace is only valid if destination is YUV.
268-
if (output_format == mlt_image_yuv422 || output_format == mlt_image_yuv420p
269-
|| output_format == mlt_image_yuv422p16 || output_format == mlt_image_yuv420p10
270-
|| output_format == mlt_image_yuv444p10)
271-
mlt_properties_set_int(properties, "colorspace", profile_colorspace);
282+
mlt_properties_set_int(properties, "colorspace", dst_colorspace);
272283
mlt_properties_set_int(properties, "full_range", dst_full_range);
273284
}
274285
mlt_frame_set_image(frame, output, size, mlt_pool_release);
@@ -344,15 +355,6 @@ static int convert_image(mlt_frame frame,
344355

345356
static mlt_frame filter_process(mlt_filter filter, mlt_frame frame)
346357
{
347-
// Set a default colorspace on the frame if not yet set by the producer.
348-
// The producer may still change it during get_image.
349-
// This way we do not have to modify each producer to set a valid colorspace.
350-
mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
351-
if (mlt_properties_get_int(properties, "colorspace") <= 0)
352-
mlt_properties_set_int(properties,
353-
"colorspace",
354-
mlt_service_profile(MLT_FILTER_SERVICE(filter))->colorspace);
355-
356358
if (!frame->convert_image)
357359
frame->convert_image = convert_image;
358360

src/modules/avformat/filter_avfilter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,12 +863,12 @@ static int filter_get_image(mlt_frame frame,
863863
const char *primaries_str = mlt_properties_get(frame_properties, "color_primaries");
864864
mlt_color_primaries primaries = mlt_image_color_pri_id(primaries_str);
865865
if (primaries == mlt_color_pri_none)
866-
primaries = mlt_color_primaries_from_colorspace(colorspace, *height);
866+
primaries = mlt_image_default_primaries(colorspace, *height);
867867
pdata->avinframe->color_primaries = mlt_to_av_color_primaries(primaries);
868868
const char *color_trc_str = mlt_properties_get(frame_properties, "color_trc");
869869
mlt_color_trc trc = mlt_image_color_trc_id(color_trc_str);
870870
if (trc == mlt_color_trc_none)
871-
trc = mlt_color_trc_from_colorspace(colorspace);
871+
trc = mlt_image_default_trc(colorspace);
872872
pdata->avinframe->color_trc = mlt_to_av_color_trc(trc);
873873
pdata->avinframe->color_range = mlt_to_av_color_range(full_range);
874874

src/modules/avformat/link_avfilter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,13 +967,13 @@ static int link_get_image(mlt_frame frame,
967967
const char *primaries_str = mlt_properties_get(frame_properties, "color_primaries");
968968
mlt_color_primaries primaries = mlt_image_color_pri_id(primaries_str);
969969
if (primaries == mlt_color_pri_none)
970-
primaries = mlt_color_primaries_from_colorspace(colorspace, *height);
970+
primaries = mlt_image_default_primaries(colorspace, *height);
971971
pdata->avinframe->color_primaries = mlt_to_av_color_primaries(primaries);
972972
const char *color_trc_str = mlt_properties_get(frame_properties, "color_trc");
973973
mlt_color_trc trc = mlt_image_color_trc_id(color_trc_str);
974974
pdata->avinframe->color_trc = mlt_to_av_color_trc(trc);
975975
if (trc == mlt_color_trc_none)
976-
trc = mlt_color_trc_from_colorspace(colorspace);
976+
trc = mlt_image_default_trc(colorspace);
977977
pdata->avinframe->color_range = mlt_to_av_color_range(full_range);
978978
const char *colorspace_str = mlt_properties_get(frame_properties, "colorspace");
979979
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);

src/modules/avformat/producer_avformat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ static void convert_image(producer_avformat self,
20972097
}
20982098
mlt_log_timings_end(NULL, __FUNCTION__);
20992099

2100-
mlt_color_primaries primaries = mlt_color_primaries_from_colorspace(colorspace, height);
2100+
mlt_color_primaries primaries = mlt_image_default_primaries(colorspace, height);
21012101
if (primaries == mlt_color_pri_none)
21022102
primaries = mlt_color_pri_bt709;
21032103
mlt_properties_set_int(frame_properties, "color_primaries", primaries);

0 commit comments

Comments
 (0)