Skip to content

Commit

Permalink
WIP filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggarra13 committed Feb 18, 2024
1 parent 61b19a1 commit ed9c067
Showing 1 changed file with 82 additions and 49 deletions.
131 changes: 82 additions & 49 deletions lib/tlIO/FFmpegWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,11 +935,25 @@ namespace tl
.arg(p.fileName));
}

// Initialize filter parameters
char args[512];
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
p.avCodecContext->width, p.avCodecContext->height,
p.avCodecContext->pix_fmt,
p.avVideoStream->time_base.num,
p.avVideoStream->time_base.den,
p.avCodecContext->sample_aspect_ratio.num,
p.avCodecContext->sample_aspect_ratio.den);

// Create input filter
const AVFilter* inputFilter = avfilter_get_by_name("buffer");
p.inputFilterCtx = avfilter_graph_alloc_filter(p.filterGraph,
inputFilter,
"in");
avfilter_graph_create_filter(
&p.inputFilterCtx, inputFilter, "in", args, NULL,
p.filterGraph);
// p.inputFilterCtx = avfilter_graph_alloc_filter(p.filterGraph,
// inputFilter,
// "in");
if (!p.inputFilterCtx)
{
throw std::runtime_error(
Expand All @@ -950,56 +964,75 @@ namespace tl
// Create output filter
const AVFilter* outputFilter =
avfilter_get_by_name("buffersink");
p.outputFilterCtx = avfilter_graph_alloc_filter(
p.filterGraph, outputFilter, "out");
avfilter_graph_create_filter(
&p.outputFilterCtx, outputFilter, "out", NULL, NULL,
p.filterGraph);
// p.outputFilterCtx = avfilter_graph_alloc_filter(
// p.filterGraph, outputFilter, "out");
if (!p.outputFilterCtx)
{
throw std::runtime_error(
string::Format("{0}: Could not create output filter")
.arg(p.fileName));
}

// Initialize filter parameters
char args[512];
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
p.avCodecContext->width, p.avCodecContext->height,
p.avCodecContext->pix_fmt,
p.avVideoStream->time_base.num,
p.avVideoStream->time_base.den,
p.avCodecContext->sample_aspect_ratio.num,
p.avCodecContext->sample_aspect_ratio.den);

r = avfilter_init_str(p.inputFilterCtx, args);
if (r)
{
throw std::runtime_error(
string::Format(
"{0}: Could not initialize input filter paramters")
.arg(p.fileName)
.arg(getErrorLabel(r)));
}

// Specify scale filter options
AVDictionary *options_dict = NULL;
av_dict_set(&options_dict, "scale", "in_range=full:in_color_matrix=bt709:out_range=full:out_color_matrix=bt709", 0);

// Initialize the filter link
AVFilterInOut *outputs = avfilter_inout_alloc();
AVFilterInOut *inputs = avfilter_inout_alloc();
outputs->name = av_strdup("in");
outputs->filter_ctx = p.inputFilterCtx;
outputs->pad_idx = 0;
outputs->next = NULL;
inputs->name = av_strdup("out");
inputs->filter_ctx = p.outputFilterCtx;
inputs->pad_idx = 0;
inputs->next = NULL;

// Link filters
r = avfilter_link(p.inputFilterCtx, 0, p.outputFilterCtx, 0);
if (r)
{
throw std::runtime_error(
string::Format("{0}: avfilter_link - {1}")
.arg(p.fileName)
.arg(getErrorLabel(r)));
}
// r = avfilter_link(p.inputFilterCtx, 0, p.outputFilterCtx, 0);
// if (r)
// {
// throw std::runtime_error(
// string::Format("{0}: avfilter_link - {1}")
// .arg(p.fileName)
// .arg(getErrorLabel(r)));
// }

// Configuring the filter context with the provided parameters
char *filterParams = "scale=in_range=full:in_color_matrix=bt709:out_range=full:out_color_matrix=bt709";
const char* filterParams = "scale=in_range=full:in_color_matrix=bt709:out_range=full:out_color_matrix=bt709";

// r = avfilter_init_str(p.outputFilterCtx, filterParams);
// if (r < 0)
// {
// throw std::runtime_error(
// string::Format("{0}: avfilter_init_str - {1}")
// .arg(p.fileName)
// .arg(getErrorLabel(r)));
// }// Parse the filtergraph specification and create a link

int ret = avfilter_graph_parse_ptr(p.filterGraph, filterParams,
&inputs, &outputs, NULL);
if (ret < 0) {
// Handle error
// Handle error
throw std::runtime_error(
string::Format("{0}: avfilter_graph_parse_ptr - {1}")
.arg(p.fileName)
.arg(getErrorLabel(r)));
}

r = avfilter_init_str(p.outputFilterCtx, filterParams);
if (r)
{
// Configure the filtergraph
ret = avfilter_graph_config(p.filterGraph, NULL);
if (ret < 0) {
// Handle error
throw std::runtime_error(
string::Format("{0}: avfilter_init_str - {1}")
.arg(p.fileName)
.arg(getErrorLabel(r)));
string::Format("{0}: avfilter_graph_config - {1}")
.arg(p.fileName)
.arg(getErrorLabel(r)));
}
}

Expand Down Expand Up @@ -1195,14 +1228,14 @@ namespace tl
.arg(getErrorLabel(r)));
}

r = av_frame_make_writable(p.avFilteredFrame);
if (r < 0)
{
throw std::runtime_error(
string::Format("Could not make filtered frame writable "
"at time {0}.")
.arg(time));
}
// r = av_frame_make_writable(p.avFilteredFrame);
// if (r < 0)
// {
// throw std::runtime_error(
// string::Format("Could not make filtered frame writable "
// "at time {0}.")
// .arg(time));
// }

r = av_buffersink_get_frame(
p.outputFilterCtx, p.avFilteredFrame);
Expand Down

0 comments on commit ed9c067

Please sign in to comment.