Skip to content

Commit e1dea45

Browse files
committed
made it so that VideoEncoder will use the frame rate closest to the one provided by the source data that is supported by the codec
1 parent 0c96cf3 commit e1dea45

File tree

10 files changed

+52
-88
lines changed

10 files changed

+52
-88
lines changed
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
5-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6-
</PropertyGroup>
7-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
9-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10-
</PropertyGroup>
11-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
12-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
13-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
14-
</PropertyGroup>
15-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
16-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
17-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
18-
</PropertyGroup>
3+
<PropertyGroup />
194
</Project>
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
5-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6-
</PropertyGroup>
7-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
9-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10-
</PropertyGroup>
11-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
12-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
13-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
14-
</PropertyGroup>
15-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
16-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
17-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
18-
</PropertyGroup>
3+
<PropertyGroup />
194
</Project>

source/ffmpeg-cpp/demo/demo.vcxproj.user

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
4-
<LocalDebuggerCommandArguments>"D:\WonderMedia\ffmpeg\samples\carphone_qcif.y4m" "D:\WonderMedia\ffmpeg\samples\DesiJourney.wav" "D:\WonderMedia\ffmpeg\samples\out.mp4"</LocalDebuggerCommandArguments>
5-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
7-
</PropertyGroup>
8-
<PropertyGroup>
9-
<ShowAllFiles>false</ShowAllFiles>
10-
</PropertyGroup>
113
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
124
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
135
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
@@ -16,6 +8,10 @@
168
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
179
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
1810
</PropertyGroup>
11+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
12+
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
13+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
14+
</PropertyGroup>
1915
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
2016
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
2117
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
5-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6-
</PropertyGroup>
7-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
9-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10-
</PropertyGroup>
11-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
12-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
13-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
14-
</PropertyGroup>
15-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
16-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
17-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
18-
</PropertyGroup>
3+
<PropertyGroup />
194
</Project>

source/ffmpeg-cpp/encode_video/encode_video.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ int main()
2828
// but if we have something weird, we can specify the properties of the format
2929
// in the constructor as commented out below.
3030
RawVideoFileSource* videoFile = new RawVideoFileSource("samples/carphone_qcif.y4m", encoder);
31+
/*Demuxer* videoFile = new Demuxer("samples/big_buck_bunny.mp4");
32+
videoFile->DecodeBestVideoStream(encoder);*/
3133

3234
// Prepare the output pipeline. This will push a small amount of frames to the file sink until it IsPrimed returns true.
3335
videoFile->PreparePipeline();

source/ffmpeg-cpp/ffmpeg-cpp/Codecs/VideoCodec.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,37 @@ namespace ffmpegcpp
7777
if (*p == AV_PIX_FMT_NONE) throw FFmpegException("Codec " + string(codecContext->codec->name) + " does not have a default pixel format, you have to specify one");
7878
return *p;
7979
}
80+
81+
AVRational VideoCodec::GetClosestSupportedFrameRate(AVRational originalFrameRate)
82+
{
83+
if (!codecContext->codec->supported_framerates)
84+
{
85+
// make up a frame rate - there is no supported frame rate
86+
return originalFrameRate;
87+
};
88+
89+
const AVRational *p = codecContext->codec->supported_framerates;
90+
AVRational bestFrameRate;
91+
bestFrameRate.num = 0;
92+
bestFrameRate.den = 1;
93+
double bestDiff = std::numeric_limits<double>::max();
94+
double fVal = av_q2d(originalFrameRate);
95+
while (p->num)
96+
{
97+
double pVal = av_q2d(*p);
98+
double diff = abs(pVal - fVal);
99+
if (diff < bestDiff)
100+
{
101+
bestDiff = diff;
102+
bestFrameRate.num = p->num;
103+
bestFrameRate.den = p->den;
104+
}
105+
p++;
106+
}
107+
108+
// There were no valid frame rates in the list... this should never happen unless ffmpeg screws up.
109+
if (bestFrameRate.num == 0) return originalFrameRate;
110+
111+
return bestFrameRate;
112+
}
80113
}

source/ffmpeg-cpp/ffmpeg-cpp/Codecs/VideoCodec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace ffmpegcpp
2020
bool IsFrameRateSupported(AVRational* frameRate);
2121

2222
AVPixelFormat GetDefaultPixelFormat();
23+
AVRational GetClosestSupportedFrameRate(AVRational frameRate);
2324

2425
};
2526

source/ffmpeg-cpp/ffmpeg-cpp/Frame Sinks/VideoEncoder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,18 @@ namespace ffmpegcpp
7171
// configure the parameters for the codec based on the frame and our preferences
7272
int width = frame->width;
7373
int height = frame->height;
74+
75+
// the format is either the provided format, or the default format if it is not supported
7476
AVPixelFormat format = finalPixelFormat;
77+
if (!closedCodec->IsPixelFormatSupported(format)) format = closedCodec->GetDefaultPixelFormat();
7578
if (format == AV_PIX_FMT_NONE) format = closedCodec->GetDefaultPixelFormat();
79+
80+
// the frame rate is either the input frame rate, OR the default frame rate if the input frame rate
81+
// is not supported, OR the explicitly chosen framerate.
7682
AVRational frameRate;
7783
frameRate.num = timeBase->den;
7884
frameRate.den = timeBase->num;
85+
if (!closedCodec->IsFrameRateSupported(&frameRate)) frameRate = closedCodec->GetClosestSupportedFrameRate(frameRate);
7986
if (finalFrameRateSet) frameRate = finalFrameRate;
8087

8188
// open the codec
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
5-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6-
</PropertyGroup>
7-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
9-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10-
</PropertyGroup>
11-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
12-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
13-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
14-
</PropertyGroup>
15-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
16-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
17-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
18-
</PropertyGroup>
3+
<PropertyGroup />
194
</Project>
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
5-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6-
</PropertyGroup>
7-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
9-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10-
</PropertyGroup>
11-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
12-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
13-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
14-
</PropertyGroup>
15-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
16-
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
17-
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
18-
</PropertyGroup>
3+
<PropertyGroup />
194
</Project>

0 commit comments

Comments
 (0)