Skip to content

Commit d4e9715

Browse files
committed
- fixed issue with side_data not being copied from the codec to the output stream
- added SetQualityScale factor that maps to the command line -qscale parameter
1 parent e1dea45 commit d4e9715

File tree

9 files changed

+112
-15
lines changed

9 files changed

+112
-15
lines changed
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup />
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>
419
</Project>
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup />
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>
419
</Project>
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup />
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>
419
</Project>

source/ffmpeg-cpp/encode_video/encode_video.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ int main()
1616

1717
// Create a MP3 codec that will encode the raw data.
1818
VideoCodec* codec = new VideoCodec("mpeg2video");
19-
//codec->SetOption("preset", "default");
20-
//VideoCodec* codec = new H264NVEncCodec();
19+
20+
// Set the global quality of the video encoding. This maps to the command line
21+
// parameter -qscale and must be within range [0,31].
22+
codec->SetQualityScale(0);
2123

2224
// Create an encoder that will encode the raw audio data as MP3.
2325
// Tie it to the muxer so it will be written to the file.
@@ -28,8 +30,6 @@ int main()
2830
// but if we have something weird, we can specify the properties of the format
2931
// in the constructor as commented out below.
3032
RawVideoFileSource* videoFile = new RawVideoFileSource("samples/carphone_qcif.y4m", encoder);
31-
/*Demuxer* videoFile = new Demuxer("samples/big_buck_bunny.mp4");
32-
videoFile->DecodeBestVideoStream(encoder);*/
3333

3434
// Prepare the output pipeline. This will push a small amount of frames to the file sink until it IsPrimed returns true.
3535
videoFile->PreparePipeline();
@@ -43,16 +43,12 @@ int main()
4343
// Save everything to disk by closing the muxer.
4444
muxer->Close();
4545
}
46-
catch (const char* bla)
47-
{
48-
49-
}
50-
/*catch (FFmpegException e)
46+
catch (FFmpegException e)
5147
{
5248
cerr << "Exception caught!" << endl;
5349
cerr << e.what() << endl;
5450
throw e;
55-
}*/
51+
}
5652

5753
cout << "Encoding complete!" << endl;
5854
cout << "Press any key to continue..." << endl;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ namespace ffmpegcpp
1919
{
2020
}
2121

22+
void VideoCodec::SetQualityScale(int qscale)
23+
{
24+
25+
codecContext->flags |= AV_CODEC_FLAG_QSCALE;
26+
codecContext->global_quality = FF_QP2LAMBDA * 0;
27+
}
28+
2229
bool VideoCodec::IsPixelFormatSupported(AVPixelFormat format)
2330
{
2431
if (format == AV_PIX_FMT_NONE) return true; // let the codec deal with this

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ namespace ffmpegcpp
1616

1717
OpenCodec* Open(int width, int height, AVRational* frameRate, AVPixelFormat format);
1818

19+
// This maps to the qscale parameter so should be in the range [0,31].
20+
void SetQualityScale(int qscale);
21+
1922
bool IsPixelFormatSupported(AVPixelFormat format);
2023
bool IsFrameRateSupported(AVRational* frameRate);
2124

source/ffmpeg-cpp/ffmpeg-cpp/Muxing/AudioOutputStream.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ namespace ffmpegcpp
3737
}
3838

3939
codecTimeBase = openCodec->GetContext()->time_base;
40+
41+
// Copy side_data from the codec context to the stream... this is
42+
// necessary for certain codecs such as mpeg2video!
43+
if (openCodec->GetContext()->nb_coded_side_data)
44+
{
45+
int i;
46+
47+
for (i = 0; i < openCodec->GetContext()->nb_coded_side_data; i++)
48+
{
49+
const AVPacketSideData *sd_src = &openCodec->GetContext()->coded_side_data[i];
50+
uint8_t *dst_data;
51+
52+
dst_data = av_stream_new_side_data(stream, sd_src->type, sd_src->size);
53+
if (!dst_data)
54+
{
55+
throw FFmpegException("Failed to allocate memory for new side_data");
56+
}
57+
memcpy(dst_data, sd_src->data, sd_src->size);
58+
}
59+
}
4060
}
4161

4262
void AudioOutputStream::WritePacket(AVPacket *pkt, OpenCodec* openCodec)

source/ffmpeg-cpp/ffmpeg-cpp/Muxing/Muxer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace ffmpegcpp
1717
avformat_alloc_output_context2(&containerContext, NULL, NULL, fileName);
1818
if (!containerContext)
1919
{
20-
printf("WARNING: Could not deduce output format from file extension: using MPEG. as default\n");
21-
avformat_alloc_output_context2(&containerContext, NULL, "mpeg", fileName);
20+
printf("WARNING: Could not deduce output format from file extension: using MP4. as default\n");
21+
avformat_alloc_output_context2(&containerContext, NULL, "mp4", fileName);
2222
}
2323
if (!containerContext)
2424
{
@@ -38,6 +38,12 @@ namespace ffmpegcpp
3838
{
3939
if (containerContext != nullptr)
4040
{
41+
// if some of the output streams weren't primed, we cannot finish this process
42+
if (!IsPrimed())
43+
{
44+
throw FFmpegException("You cannot close a muxer when one of the streams wasn't primed. You need to make sure all streams are primed before closing the muxer.");
45+
}
46+
4147
/* Write the trailer, if any. The trailer must be written before you
4248
* close the CodecContexts open when you wrote the header; otherwise
4349
* av_write_trailer() may try to use memory that was freed on

source/ffmpeg-cpp/ffmpeg-cpp/Muxing/VideoOutputStream.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ namespace ffmpegcpp
3737
}
3838

3939
codecTimeBase = openCodec->GetContext()->time_base;
40+
41+
// Copy side_data from the codec context to the stream... this is
42+
// necessary for certain codecs such as mpeg2video!
43+
if (openCodec->GetContext()->nb_coded_side_data)
44+
{
45+
int i;
46+
47+
for (i = 0; i < openCodec->GetContext()->nb_coded_side_data; i++)
48+
{
49+
const AVPacketSideData *sd_src = &openCodec->GetContext()->coded_side_data[i];
50+
uint8_t *dst_data;
51+
52+
dst_data = av_stream_new_side_data(stream, sd_src->type, sd_src->size);
53+
if (!dst_data)
54+
{
55+
throw FFmpegException("Failed to allocate memory for new side_data");
56+
}
57+
memcpy(dst_data, sd_src->data, sd_src->size);
58+
}
59+
}
4060
}
4161

4262
void VideoOutputStream::WritePacket(AVPacket *pkt, OpenCodec* openCodec)

0 commit comments

Comments
 (0)