Skip to content

Commit 8b98c4e

Browse files
committed
Fix colorspace and trc for producers that do not set the
properties explicitly.
1 parent 3677333 commit 8b98c4e

12 files changed

Lines changed: 240 additions & 111 deletions

src/framework/mlt.vers

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,4 +678,5 @@ MLT_7.34.0 {
678678
mlt_image_color_pri_name;
679679
mlt_image_color_pri_id;
680680
mlt_image_default_trc;
681+
mlt_image_default_primaries;
681682
} MLT_7.32.0;

src/framework/mlt_image.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,11 @@ mlt_color_trc mlt_image_default_trc(mlt_colorspace colorspace)
483483
{
484484
switch (colorspace) {
485485
case mlt_colorspace_rgb:
486-
return mlt_color_trc_iec61966_2_1;
486+
return mlt_color_trc_smpte170m;
487487
case mlt_colorspace_bt709:
488488
return mlt_color_trc_bt709;
489489
case mlt_colorspace_bt470bg:
490-
return mlt_color_trc_gamma28;
490+
return mlt_color_trc_smpte170m;
491491
case mlt_colorspace_smpte170m:
492492
return mlt_color_trc_smpte170m;
493493
case mlt_colorspace_smpte240m:
@@ -509,6 +509,45 @@ mlt_color_trc mlt_image_default_trc(mlt_colorspace colorspace)
509509
return mlt_color_trc_bt709;
510510
}
511511

512+
/** Get the default color primaries for a given colorspace.
513+
*
514+
* \public \memberof mlt_image_s
515+
* \param colorspace the colorspace
516+
* \param height the image height. Pass 0 if you do not know.
517+
* \return a color primaries
518+
*/
519+
520+
mlt_color_primaries mlt_image_default_primaries(mlt_colorspace colorspace, int height)
521+
{
522+
switch (colorspace) {
523+
case mlt_colorspace_rgb:
524+
return mlt_color_pri_smpte170m;
525+
case mlt_colorspace_bt709:
526+
return mlt_color_pri_bt709;
527+
case mlt_colorspace_bt470bg:
528+
return mlt_color_pri_bt470bg;
529+
case mlt_colorspace_smpte170m:
530+
return mlt_color_pri_smpte170m;
531+
case mlt_colorspace_smpte240m:
532+
return mlt_color_pri_smpte170m;
533+
case mlt_colorspace_bt2020_ncl:
534+
return mlt_color_pri_bt2020;
535+
case mlt_colorspace_bt2020_cl:
536+
return mlt_color_pri_bt2020;
537+
case mlt_colorspace_bt601:
538+
return height >= 576 ? mlt_color_pri_bt470bg : mlt_color_pri_smpte170m;
539+
case mlt_colorspace_fcc:
540+
return mlt_color_pri_bt470m;
541+
case mlt_colorspace_smpte2085:
542+
case mlt_colorspace_ycgco:
543+
case mlt_colorspace_unspecified:
544+
case mlt_colorspace_reserved:
545+
case mlt_colorspace_invalid:
546+
break;
547+
}
548+
return mlt_color_pri_bt709;
549+
}
550+
512551
/** Fill an image with black.
513552
*
514553
* \bug This does not respect full range YUV if needed.

src/framework/mlt_image.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ 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);
7272
MLT_EXPORT mlt_color_trc mlt_image_default_trc(mlt_colorspace colorspace);
73+
MLT_EXPORT mlt_color_primaries mlt_image_default_primaries(mlt_colorspace colorspace, int height);
7374
MLT_EXPORT int mlt_image_rgba_opaque(uint8_t *image, int width, int height);
7475
MLT_EXPORT int mlt_image_full_range(const char *color_range);
7576

src/modules/avformat/common.c

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,9 @@ int mlt_to_av_colorspace(mlt_colorspace colorspace, int height)
460460
case mlt_colorspace_bt709:
461461
return AVCOL_SPC_BT709;
462462
case mlt_colorspace_unspecified:
463-
return AVCOL_SPC_UNSPECIFIED;
463+
break; // use calculation at the end
464464
case mlt_colorspace_reserved:
465-
return AVCOL_SPC_RESERVED;
465+
break; // use calculation at the end
466466
case mlt_colorspace_fcc:
467467
return AVCOL_SPC_FCC;
468468
case mlt_colorspace_bt470bg:
@@ -480,11 +480,16 @@ int mlt_to_av_colorspace(mlt_colorspace colorspace, int height)
480480
case mlt_colorspace_smpte2085:
481481
return AVCOL_SPC_SMPTE2085;
482482
case mlt_colorspace_bt601:
483-
return 576 % height ? AVCOL_SPC_SMPTE170M : AVCOL_SPC_BT470BG;
483+
break; // use calculation at the end
484484
case mlt_colorspace_invalid:
485-
return mlt_colorspace_unspecified;
485+
break; // use calculation at the end
486+
}
487+
if (height > 576) {
488+
return AVCOL_SPC_BT709;
489+
} else if (height == 576) {
490+
return AVCOL_SPC_BT470BG;
486491
}
487-
return mlt_colorspace_unspecified;
492+
return AVCOL_SPC_SMPTE170M;
488493
}
489494

490495
mlt_colorspace av_to_mlt_colorspace(int colorspace, int width, int height)
@@ -582,62 +587,11 @@ mlt_color_primaries av_to_mlt_color_primaries(int primaries)
582587
return mlt_color_pri_none;
583588
}
584589

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-
613-
614590
int mlt_to_av_color_range(int full_range)
615591
{
616592
return full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
617593
}
618594

619-
mlt_color_trc mlt_color_trc_from_colorspace(mlt_colorspace colorspace)
620-
{
621-
switch (colorspace) {
622-
case mlt_colorspace_bt709:
623-
return mlt_color_trc_bt709;
624-
case mlt_colorspace_bt470bg:
625-
return mlt_color_trc_gamma28;
626-
case mlt_colorspace_smpte240m:
627-
return mlt_color_trc_smpte240m;
628-
case mlt_colorspace_rgb: // sRGB
629-
return mlt_color_trc_iec61966_2_1;
630-
case mlt_colorspace_bt601:
631-
case mlt_colorspace_smpte170m:
632-
return mlt_color_trc_smpte170m;
633-
case mlt_colorspace_bt2020_ncl:
634-
return mlt_color_trc_bt2020_10;
635-
default:
636-
break;
637-
}
638-
return mlt_color_trc_none;
639-
}
640-
641595
int av_to_mlt_full_range(int color_range)
642596
{
643597
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: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,24 @@ 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);
158+
mlt_colorspace dst_colorspace = profile ? profile->colorspace : mlt_colorspace_bt601;
159+
const char *src_colorspace_str = mlt_properties_get(properties, "colorspace");
160+
mlt_colorspace src_colorspace = mlt_image_colorspace_id(src_colorspace_str);
161161
int width = mlt_properties_get_int(properties, "width");
162162
int height = mlt_properties_get_int(properties, "height");
163163
int src_full_range = mlt_properties_get_int(properties, "full_range");
164164
const char *dst_color_range = mlt_properties_get(properties, "consumer.color_range");
165165
int dst_full_range = mlt_image_full_range(dst_color_range);
166+
// Fix some producers that may not be setting properties correctly
167+
if (output_format == mlt_image_rgb || output_format == mlt_image_rgba
168+
|| output_format == mlt_image_rgba64) {
169+
dst_full_range = 1;
170+
dst_colorspace = mlt_colorspace_rgb;
171+
}
172+
if (*format == mlt_image_rgb || *format == mlt_image_rgba || *format == mlt_image_rgba64) {
173+
src_full_range = 1;
174+
src_colorspace = mlt_colorspace_rgb;
175+
}
166176

167177
if (out_width <= 0)
168178
out_width = width;
@@ -178,8 +188,8 @@ static int convert_image(mlt_frame frame,
178188
mlt_image_format_name(output_format),
179189
out_width,
180190
out_height,
181-
colorspace,
182-
profile_colorspace,
191+
src_colorspace,
192+
dst_colorspace,
183193
src_full_range,
184194
dst_full_range,
185195
mlt_frame_get_position(frame));
@@ -260,15 +270,11 @@ static int convert_image(mlt_frame frame,
260270
out_height,
261271
width,
262272
height,
263-
colorspace,
264-
profile_colorspace,
273+
src_colorspace,
274+
dst_colorspace,
265275
src_full_range,
266276
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);
277+
mlt_properties_set_int(properties, "colorspace", dst_colorspace);
272278
mlt_properties_set_int(properties, "full_range", dst_full_range);
273279
}
274280
mlt_frame_set_image(frame, output, size, mlt_pool_release);

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)