forked from Raveler/ffmpeg-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodec.cpp
More file actions
89 lines (71 loc) · 1.76 KB
/
Codec.cpp
File metadata and controls
89 lines (71 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "Codecs/Codec.h"
#include "FFmpegException.h"
#include "CodecDeducer.h"
using namespace std;
namespace ffmpegcpp
{
Codec::Codec(const char* codecName)
{
AVCodec* codec = CodecDeducer::DeduceEncoder(codecName);
codecContext = LoadContext(codec);
}
Codec::Codec(AVCodecID codecId)
{
AVCodec* codec = CodecDeducer::DeduceEncoder(codecId);
codecContext = LoadContext(codec);
}
void Codec::SetOption(const char* name, const char* value)
{
av_opt_set(codecContext->priv_data, name, value, 0);
}
void Codec::SetOption(const char* name, int value)
{
av_opt_set_int(codecContext->priv_data, name, value, 0);
}
void Codec::SetOption(const char* name, double value)
{
av_opt_set_double(codecContext->priv_data, name, value, 0);
}
AVCodecContext* Codec::LoadContext(AVCodec* codec)
{
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
if (!codecContext)
{
CleanUp();
throw FFmpegException("Could not allocate video codec context for codec " + string(codec->name));
}
// copy the type
codecContext->codec_type = codec->type;
return codecContext;
}
void Codec::CleanUp()
{
if (codecContext != nullptr && !opened)
{
avcodec_free_context(&codecContext);
}
}
OpenCodec* Codec::Open()
{
if (opened)
{
throw FFmpegException("You can only open a codec once");
}
int ret = avcodec_open2(codecContext, codecContext->codec, NULL);
if (ret < 0)
{
throw FFmpegException("Could not open codecContext for codec", ret);
}
opened = true;
return new OpenCodec(codecContext);
}
Codec::~Codec()
{
CleanUp();
}
void Codec::SetGlobalContainerHeader()
{
if (opened) throw FFmpegException("This flag should be set before opening the codec");
codecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}