Skip to content

Commit c08911c

Browse files
committed
Fix video frame properties warning in filtergraph based services
This message was being displayed for each frame: "Changing video frame properties on the fly is not supported by all filters." The colorspace was being set on the video frames, but not on the filter graph.
1 parent d8e70e4 commit c08911c

File tree

5 files changed

+78
-9
lines changed

5 files changed

+78
-9
lines changed

src/modules/avformat/common.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,11 @@ mlt_color_primaries mlt_color_primaries_from_colorspace(mlt_colorspace colorspac
610610
return mlt_color_pri_none;
611611
}
612612

613+
int mlt_to_av_color_range(int full_range)
614+
{
615+
return full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
616+
}
617+
613618
void mlt_image_to_avframe(mlt_image image, mlt_frame mltframe, AVFrame *avframe)
614619
{
615620
mlt_properties frame_properties = MLT_FRAME_PROPERTIES(mltframe);

src/modules/avformat/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ 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);
5353
mlt_color_primaries mlt_color_primaries_from_colorspace(mlt_colorspace colorspace, int height);
54+
int mlt_to_av_color_range(int full_range);
5455
void mlt_image_to_avframe(mlt_image image, mlt_frame mltframe, AVFrame *avframe);
5556
void avframe_to_mlt_image(AVFrame *avframe, mlt_image image);
5657

src/modules/avformat/filter_avfilter.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef struct
5454
int format;
5555
int width;
5656
int height;
57+
mlt_colorspace colorspace;
5758
int reset;
5859
} private_data;
5960

@@ -371,8 +372,12 @@ static void init_audio_filtergraph(mlt_filter filter,
371372
avfilter_graph_free(&pdata->avfilter_graph);
372373
}
373374

374-
static void init_image_filtergraph(
375-
mlt_filter filter, mlt_image_format format, int width, int height, double resolution_scale)
375+
static void init_image_filtergraph(mlt_filter filter,
376+
mlt_image_format format,
377+
int width,
378+
int height,
379+
mlt_colorspace colorspace,
380+
double resolution_scale)
376381
{
377382
private_data *pdata = (private_data *) filter->child;
378383
mlt_profile profile = mlt_service_profile(MLT_FILTER_SERVICE(filter));
@@ -385,11 +390,13 @@ static void init_image_filtergraph(
385390
AVRational sar = (AVRational){profile->sample_aspect_num, profile->sample_aspect_den};
386391
AVRational timebase = (AVRational){profile->frame_rate_den, profile->frame_rate_num};
387392
AVRational framerate = (AVRational){profile->frame_rate_num, profile->frame_rate_den};
393+
int avcolorspace = mlt_to_av_colorspace(colorspace, pdata->height);
388394
int ret;
389395

390396
pdata->format = format;
391397
pdata->width = width;
392398
pdata->height = height;
399+
pdata->colorspace = avcolorspace;
393400

394401
// Set up formats
395402
pixel_fmts[0] = mlt_to_av_image_format(format);
@@ -452,6 +459,11 @@ static void init_image_filtergraph(
452459
mlt_log_error(filter, "Cannot set src frame_rate %d/%d\n", framerate.num, framerate.den);
453460
goto fail;
454461
}
462+
ret = av_opt_set_int(pdata->avbuffsrc_ctx, "colorspace", avcolorspace, AV_OPT_SEARCH_CHILDREN);
463+
if (ret < 0) {
464+
mlt_log_error(filter, "Cannot set src colorspace %d\n", avcolorspace);
465+
goto fail;
466+
}
455467
ret = avfilter_init_str(pdata->avbuffsrc_ctx, NULL);
456468
if (ret < 0) {
457469
mlt_log_error(filter, "Cannot init buffer source\n");
@@ -796,10 +808,12 @@ static int filter_get_image(mlt_frame frame,
796808
mlt_service_lock(MLT_FILTER_SERVICE(filter));
797809

798810
double scale = mlt_profile_scale_width(profile, *width);
811+
const char *colorspace_str = mlt_properties_get(frame_properties, "colorspace");
812+
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
799813

800814
if (pdata->reset || pdata->format != *format || pdata->width != *width
801-
|| pdata->height != *height) {
802-
init_image_filtergraph(filter, *format, *width, *height, scale);
815+
|| pdata->height != *height || pdata->colorspace != colorspace) {
816+
init_image_filtergraph(filter, *format, *width, *height, colorspace, scale);
803817
pdata->reset = 0;
804818
}
805819

src/modules/avformat/link_avdeinterlace.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ typedef struct
4242
int outformat;
4343
int width;
4444
int height;
45+
mlt_colorspace incolorspace;
46+
int infullrange;
4547
} private_data;
4648

4749
typedef struct
@@ -98,6 +100,8 @@ static void init_image_filtergraph(mlt_link self, AVRational sar)
98100
enum AVPixelFormat out_pixel_fmts[] = {-1, -1};
99101
AVRational timebase = (AVRational){profile->frame_rate_den, profile->frame_rate_num};
100102
AVRational framerate = (AVRational){profile->frame_rate_num, profile->frame_rate_den};
103+
int colorspace = mlt_to_av_colorspace(pdata->incolorspace, pdata->height);
104+
int color_range = mlt_to_av_color_range(pdata->infullrange);
101105
AVFilterContext *prev_ctx = NULL;
102106
AVFilterContext *avfilter_ctx = NULL;
103107
int ret;
@@ -156,6 +160,16 @@ static void init_image_filtergraph(mlt_link self, AVRational sar)
156160
mlt_log_error(self, "Cannot set src frame_rate %d/%d\n", framerate.num, framerate.den);
157161
goto fail;
158162
}
163+
ret = av_opt_set_int(fdata->avbuffsrc_ctx, "colorspace", colorspace, AV_OPT_SEARCH_CHILDREN);
164+
if (ret < 0) {
165+
mlt_log_error(self, "Cannot set src colorspace %d\n", colorspace);
166+
goto fail;
167+
}
168+
ret = av_opt_set_int(fdata->avbuffsrc_ctx, "range", color_range, AV_OPT_SEARCH_CHILDREN);
169+
if (ret < 0) {
170+
mlt_log_error(self, "Cannot set src range %d\n", color_range);
171+
goto fail;
172+
}
159173
ret = avfilter_init_str(fdata->avbuffsrc_ctx, NULL);
160174
if (ret < 0) {
161175
mlt_log_error(self, "Cannot init buffer source\n");
@@ -391,12 +405,16 @@ static int link_get_image(mlt_frame frame,
391405
}
392406
srcimg.format = validate_format(srcimg.format);
393407
dstimg.format = validate_format(*format);
408+
const char *colorspace_str = mlt_properties_get(unique_properties, "colorspace");
409+
mlt_colorspace incolorspace = mlt_image_colorspace_id(colorspace_str);
410+
int infullrange = mlt_properties_get_int(unique_properties, "full_range");
394411

395412
mlt_service_lock(MLT_LINK_SERVICE(self));
396413

397414
if (pdata->method != method || pdata->expected_frame != mlt_frame_get_position(frame)
398415
|| pdata->informat != srcimg.format || pdata->width != srcimg.width
399-
|| pdata->height != srcimg.height || pdata->outformat != dstimg.format) {
416+
|| pdata->height != srcimg.height || pdata->outformat != dstimg.format
417+
|| pdata->incolorspace != incolorspace || pdata->infullrange != infullrange) {
400418
mlt_log_debug(MLT_LINK_SERVICE(self),
401419
"Init: %s->%s\t%d->%d\n",
402420
mlt_deinterlacer_name(pdata->method),
@@ -410,6 +428,8 @@ static int link_get_image(mlt_frame frame,
410428
pdata->width = srcimg.width;
411429
pdata->height = srcimg.height;
412430
pdata->outformat = dstimg.format;
431+
pdata->incolorspace = incolorspace;
432+
pdata->infullrange = infullrange;
413433
init_image_filtergraph(self, av_d2q(mlt_frame_get_aspect_ratio(frame), 1024));
414434
}
415435

@@ -555,6 +575,21 @@ static int link_get_frame(mlt_link self, mlt_frame_ptr frame, int index)
555575
"format",
556576
mlt_properties_get_int(original_producer_properties, "format"));
557577
}
578+
if (mlt_properties_exists(original_producer_properties, "meta.media.colorspace")) {
579+
mlt_properties_set(unique_properties,
580+
"colorspace",
581+
mlt_properties_get(original_producer_properties,
582+
"meta.media.colorspace"));
583+
} else if (mlt_properties_exists(MLT_FRAME_PROPERTIES(*frame), "colorspace")) {
584+
mlt_properties_set(unique_properties,
585+
"colorspace",
586+
mlt_properties_get(MLT_FRAME_PROPERTIES(*frame), "colorspace"));
587+
}
588+
if (mlt_properties_exists(MLT_FRAME_PROPERTIES(*frame), "full_range")) {
589+
mlt_properties_set(unique_properties,
590+
"full_range",
591+
mlt_properties_get(MLT_FRAME_PROPERTIES(*frame), "full_range"));
592+
}
558593

559594
// Pass future frames
560595
int i = 0;

src/modules/avformat/link_avfilter.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef struct
5454
int format;
5555
int width;
5656
int height;
57+
mlt_colorspace colorspace;
5758
int channels;
5859
int frequency;
5960
int reset;
@@ -388,8 +389,12 @@ static void init_audio_filtergraph(mlt_link self,
388389
avfilter_graph_free(&pdata->avfilter_graph);
389390
}
390391

391-
static void init_image_filtergraph(
392-
mlt_link self, mlt_image_format format, int width, int height, double resolution_scale)
392+
static void init_image_filtergraph(mlt_link self,
393+
mlt_image_format format,
394+
int width,
395+
int height,
396+
mlt_colorspace colorspace,
397+
double resolution_scale)
393398
{
394399
private_data *pdata = (private_data *) self->child;
395400
mlt_profile profile = mlt_service_profile(MLT_LINK_SERVICE(self));
@@ -402,11 +407,13 @@ static void init_image_filtergraph(
402407
AVRational sar = (AVRational){profile->sample_aspect_num, profile->sample_aspect_den};
403408
AVRational timebase = (AVRational){profile->frame_rate_den, profile->frame_rate_num};
404409
AVRational framerate = (AVRational){profile->frame_rate_num, profile->frame_rate_den};
410+
int avcolorspace = mlt_to_av_colorspace(colorspace, height);
405411
int ret;
406412

407413
pdata->format = format;
408414
pdata->width = width;
409415
pdata->height = height;
416+
pdata->colorspace = colorspace;
410417

411418
// Set up formats
412419
pixel_fmts[0] = mlt_to_av_image_format(format);
@@ -469,6 +476,11 @@ static void init_image_filtergraph(
469476
mlt_log_error(self, "Cannot set src frame_rate %d/%d\n", framerate.num, framerate.den);
470477
goto fail;
471478
}
479+
ret = av_opt_set_int(pdata->avbuffsrc_ctx, "colorspace", avcolorspace, AV_OPT_SEARCH_CHILDREN);
480+
if (ret < 0) {
481+
mlt_log_error(self, "Cannot set src colorspace %d\n", avcolorspace);
482+
goto fail;
483+
}
472484
ret = avfilter_init_str(pdata->avbuffsrc_ctx, NULL);
473485
if (ret < 0) {
474486
mlt_log_error(self, "Cannot init buffer source\n");
@@ -900,10 +912,12 @@ static int link_get_image(mlt_frame frame,
900912
mlt_service_lock(MLT_LINK_SERVICE(self));
901913

902914
double scale = mlt_profile_scale_width(profile, *width);
915+
const char *colorspace_str = mlt_properties_get(frame_properties, "colorspace");
916+
mlt_colorspace colorspace = mlt_image_colorspace_id(colorspace_str);
903917

904918
if (pdata->reset || pdata->format != *format || pdata->width != *width
905-
|| pdata->height != *height) {
906-
init_image_filtergraph(self, *format, *width, *height, scale);
919+
|| pdata->height != *height || pdata->colorspace != colorspace) {
920+
init_image_filtergraph(self, *format, *width, *height, colorspace, scale);
907921
pdata->reset = 0;
908922
}
909923

0 commit comments

Comments
 (0)