Skip to content

Commit 0e0b634

Browse files
committed
Fix warning about missing color range for filters that use it
In particular, the av_colorspace filter expects it to be set.
1 parent 9a6d3a6 commit 0e0b634

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/modules/avformat/common.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,8 @@ void mlt_image_to_avframe(mlt_image image, mlt_frame mltframe, AVFrame *avframe)
641641
avframe->color_primaries = mlt_to_av_color_primaries(primaries);
642642
const char *color_trc_str = mlt_properties_get(frame_properties, "color_trc");
643643
avframe->color_trc = mlt_to_av_color_trc(mlt_image_color_trc_id(color_trc_str));
644-
avframe->color_range = mlt_properties_get_int(frame_properties, "full_range")
645-
? AVCOL_RANGE_JPEG
646-
: AVCOL_RANGE_MPEG;
644+
avframe->color_range = mlt_to_av_color_range(
645+
mlt_properties_get_int(frame_properties, "full_range"));
647646
const char *colorspace_str = mlt_properties_get(frame_properties, "colorspace");
648647
avframe->colorspace = mlt_to_av_colorspace(mlt_image_colorspace_id(colorspace_str),
649648
avframe->height);

src/modules/avformat/filter_avfilter.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct
5555
int width;
5656
int height;
5757
mlt_colorspace colorspace;
58+
int full_range;
5859
int reset;
5960
} private_data;
6061

@@ -377,6 +378,7 @@ static void init_image_filtergraph(mlt_filter filter,
377378
int width,
378379
int height,
379380
mlt_colorspace colorspace,
381+
int full_range,
380382
double resolution_scale)
381383
{
382384
private_data *pdata = (private_data *) filter->child;
@@ -391,12 +393,14 @@ static void init_image_filtergraph(mlt_filter filter,
391393
AVRational timebase = (AVRational){profile->frame_rate_den, profile->frame_rate_num};
392394
AVRational framerate = (AVRational){profile->frame_rate_num, profile->frame_rate_den};
393395
int avcolorspace = mlt_to_av_colorspace(colorspace, pdata->height);
396+
int color_range = mlt_to_av_color_range(full_range);
394397
int ret;
395398

396399
pdata->format = format;
397400
pdata->width = width;
398401
pdata->height = height;
399-
pdata->colorspace = avcolorspace;
402+
pdata->colorspace = colorspace;
403+
pdata->full_range = full_range;
400404

401405
// Set up formats
402406
pixel_fmts[0] = mlt_to_av_image_format(format);
@@ -464,6 +468,11 @@ static void init_image_filtergraph(mlt_filter filter,
464468
mlt_log_error(filter, "Cannot set src colorspace %d\n", avcolorspace);
465469
goto fail;
466470
}
471+
ret = av_opt_set_int(pdata->avbuffsrc_ctx, "range", color_range, AV_OPT_SEARCH_CHILDREN);
472+
if (ret < 0) {
473+
mlt_log_error(filter, "Cannot set src range %d\n", color_range);
474+
goto fail;
475+
}
467476
ret = avfilter_init_str(pdata->avbuffsrc_ctx, NULL);
468477
if (ret < 0) {
469478
mlt_log_error(filter, "Cannot init buffer source\n");
@@ -810,10 +819,12 @@ static int filter_get_image(mlt_frame frame,
810819
double scale = mlt_profile_scale_width(profile, *width);
811820
const char *colorspace_str = mlt_properties_get(frame_properties, "colorspace");
812821
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
822+
int full_range = mlt_properties_get_int(frame_properties, "full_range");
813823

814824
if (pdata->reset || pdata->format != *format || pdata->width != *width
815-
|| pdata->height != *height || pdata->colorspace != colorspace) {
816-
init_image_filtergraph(filter, *format, *width, *height, colorspace, scale);
825+
|| pdata->height != *height || pdata->colorspace != colorspace
826+
|| pdata->full_range != full_range) {
827+
init_image_filtergraph(filter, *format, *width, *height, colorspace, full_range, scale);
817828
pdata->reset = 0;
818829
}
819830

@@ -844,11 +855,7 @@ static int filter_get_image(mlt_frame frame,
844855
pdata->avinframe->color_primaries = mlt_to_av_color_primaries(primaries);
845856
const char *color_trc_str = mlt_properties_get(frame_properties, "color_trc");
846857
pdata->avinframe->color_trc = mlt_to_av_color_trc(mlt_image_color_trc_id(color_trc_str));
847-
// Setting full_range here causes full range input to always be down-converted to limited
848-
// range when operating in YUV, regardless of the consumer.color_range -- for each avfilter!
849-
// pdata->avinframe->color_range = mlt_properties_get_int(frame_properties, "full_range")
850-
// ? AVCOL_RANGE_JPEG
851-
// : AVCOL_RANGE_MPEG;
858+
pdata->avinframe->color_range = mlt_to_av_color_range(full_range);
852859

853860
if (is_rgb(*format)) {
854861
pdata->avinframe->colorspace = AVCOL_SPC_RGB;

src/modules/avformat/link_avfilter.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct
5555
int width;
5656
int height;
5757
mlt_colorspace colorspace;
58+
int full_range;
5859
int channels;
5960
int frequency;
6061
int reset;
@@ -394,6 +395,7 @@ static void init_image_filtergraph(mlt_link self,
394395
int width,
395396
int height,
396397
mlt_colorspace colorspace,
398+
int full_range,
397399
double resolution_scale)
398400
{
399401
private_data *pdata = (private_data *) self->child;
@@ -408,12 +410,14 @@ static void init_image_filtergraph(mlt_link self,
408410
AVRational timebase = (AVRational){profile->frame_rate_den, profile->frame_rate_num};
409411
AVRational framerate = (AVRational){profile->frame_rate_num, profile->frame_rate_den};
410412
int avcolorspace = mlt_to_av_colorspace(colorspace, height);
413+
int color_range = mlt_to_av_color_range(full_range);
411414
int ret;
412415

413416
pdata->format = format;
414417
pdata->width = width;
415418
pdata->height = height;
416419
pdata->colorspace = colorspace;
420+
pdata->full_range = full_range;
417421

418422
// Set up formats
419423
pixel_fmts[0] = mlt_to_av_image_format(format);
@@ -481,6 +485,11 @@ static void init_image_filtergraph(mlt_link self,
481485
mlt_log_error(self, "Cannot set src colorspace %d\n", avcolorspace);
482486
goto fail;
483487
}
488+
ret = av_opt_set_int(pdata->avbuffsrc_ctx, "range", color_range, AV_OPT_SEARCH_CHILDREN);
489+
if (ret < 0) {
490+
mlt_log_error(self, "Cannot set src range %d\n", color_range);
491+
goto fail;
492+
}
484493
ret = avfilter_init_str(pdata->avbuffsrc_ctx, NULL);
485494
if (ret < 0) {
486495
mlt_log_error(self, "Cannot init buffer source\n");
@@ -914,10 +923,12 @@ static int link_get_image(mlt_frame frame,
914923
double scale = mlt_profile_scale_width(profile, *width);
915924
const char *colorspace_str = mlt_properties_get(frame_properties, "colorspace");
916925
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
926+
int full_range = mlt_properties_get_int(frame_properties, "full_range");
917927

918928
if (pdata->reset || pdata->format != *format || pdata->width != *width
919-
|| pdata->height != *height || pdata->colorspace != colorspace) {
920-
init_image_filtergraph(self, *format, *width, *height, colorspace, scale);
929+
|| pdata->height != *height || pdata->colorspace != colorspace
930+
|| pdata->full_range != full_range) {
931+
init_image_filtergraph(self, *format, *width, *height, colorspace, full_range, scale);
921932
pdata->reset = 0;
922933
}
923934

@@ -948,9 +959,7 @@ static int link_get_image(mlt_frame frame,
948959
pdata->avinframe->color_primaries = mlt_to_av_color_primaries(primaries);
949960
const char *color_trc_str = mlt_properties_get(frame_properties, "color_trc");
950961
pdata->avinframe->color_trc = mlt_to_av_color_trc(mlt_image_color_trc_id(color_trc_str));
951-
pdata->avinframe->color_range = mlt_properties_get_int(frame_properties, "full_range")
952-
? AVCOL_RANGE_JPEG
953-
: AVCOL_RANGE_MPEG;
962+
pdata->avinframe->color_range = mlt_to_av_color_range(full_range);
954963
const char *colorspace_str = mlt_properties_get(frame_properties, "colorspace");
955964
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
956965
pdata->avinframe->colorspace = mlt_to_av_colorspace(colorspace, pdata->avinframe->height);

0 commit comments

Comments
 (0)