author | Stepan777 <stepik-777@mail.ru> |
Fri, 08 Jun 2012 02:52:35 +0400 | |
changeset 7198 | 5debd5fe526e |
parent 7180 | 53ffc8853008 |
child 7235 | baa69bd025d9 |
permissions | -rw-r--r-- |
7180 | 1 |
|
2 |
#include <stdlib.h> |
|
3 |
#include <stdio.h> |
|
4 |
#include <string.h> |
|
5 |
#include <stdarg.h> |
|
6 |
#include "libavformat/avformat.h" |
|
7 |
||
8 |
static AVFormatContext* g_pContainer; |
|
9 |
static AVOutputFormat* g_pFormat; |
|
10 |
static AVStream* g_pAStream; |
|
11 |
static AVStream* g_pVStream; |
|
12 |
static AVFrame* g_pAFrame; |
|
13 |
static AVFrame* g_pVFrame; |
|
14 |
static AVCodec* g_pACodec; |
|
15 |
static AVCodec* g_pVCodec; |
|
16 |
static AVCodecContext* g_pAudio; |
|
17 |
static AVCodecContext* g_pVideo; |
|
18 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
19 |
static int g_Width, g_Height; |
7180 | 20 |
static int g_Frequency, g_Channels; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
21 |
static int g_VQuality, g_AQuality; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
22 |
static AVRational g_Framerate; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
23 |
static const char* g_pPreset; |
7180 | 24 |
|
25 |
static FILE* g_pSoundFile; |
|
26 |
static int16_t* g_pSamples; |
|
27 |
static int g_NumSamples; |
|
28 |
||
29 |
/* |
|
30 |
Initially I wrote code for latest ffmpeg, but on Linux (Ubuntu) |
|
31 |
only older version is available from repository. That's why you see here |
|
32 |
all of this #if LIBAVCODEC_VERSION_MAJOR < 54. |
|
33 |
Actually, it may be possible to remove code for newer version |
|
34 |
and use only code for older version. |
|
35 |
*/ |
|
36 |
||
37 |
#if LIBAVCODEC_VERSION_MAJOR < 54 |
|
38 |
#define OUTBUFFER_SIZE 200000 |
|
39 |
static uint8_t g_OutBuffer[OUTBUFFER_SIZE]; |
|
40 |
#endif |
|
41 |
||
42 |
// pointer to function from hwengine (uUtils.pas) |
|
43 |
static void (*AddFileLogRaw)(const char* pString); |
|
44 |
||
45 |
static void FatalError(const char* pFmt, ...) |
|
46 |
{ |
|
47 |
const char Buffer[1024]; |
|
48 |
va_list VaArgs; |
|
49 |
||
50 |
va_start(VaArgs, pFmt); |
|
51 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
52 |
va_end(VaArgs); |
|
53 |
||
54 |
AddFileLogRaw("Error in av-wrapper: "); |
|
55 |
AddFileLogRaw(Buffer); |
|
56 |
AddFileLogRaw("\n"); |
|
57 |
exit(1); |
|
58 |
} |
|
59 |
||
60 |
// Function to be called from libav for logging. |
|
61 |
// Note: libav can call LogCallback from different threads |
|
62 |
// (there is mutex in AddFileLogRaw). |
|
63 |
static void LogCallback(void* p, int Level, const char* pFmt, va_list VaArgs) |
|
64 |
{ |
|
65 |
const char Buffer[1024]; |
|
66 |
||
67 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
68 |
AddFileLogRaw(Buffer); |
|
69 |
} |
|
70 |
||
71 |
static void Log(const char* pFmt, ...) |
|
72 |
{ |
|
73 |
const char Buffer[1024]; |
|
74 |
va_list VaArgs; |
|
75 |
||
76 |
va_start(VaArgs, pFmt); |
|
77 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
78 |
va_end(VaArgs); |
|
79 |
||
80 |
AddFileLogRaw(Buffer); |
|
81 |
} |
|
82 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
83 |
static void AddAudioStream() |
7180 | 84 |
{ |
85 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
86 |
g_pAStream = avformat_new_stream(g_pContainer, g_pACodec); |
|
87 |
#else |
|
88 |
g_pAStream = av_new_stream(g_pContainer, 1); |
|
89 |
#endif |
|
90 |
if(!g_pAStream) |
|
91 |
FatalError("Could not allocate audio stream"); |
|
92 |
g_pAStream->id = 1; |
|
93 |
||
94 |
g_pAudio = g_pAStream->codec; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
95 |
|
7180 | 96 |
avcodec_get_context_defaults3(g_pAudio, g_pACodec); |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
97 |
g_pAudio->codec_id = g_pACodec->id; |
7180 | 98 |
|
99 |
// put parameters |
|
100 |
g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16; |
|
101 |
g_pAudio->sample_rate = g_Frequency; |
|
102 |
g_pAudio->channels = g_Channels; |
|
103 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
104 |
// set quality |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
105 |
if (g_AQuality > 100) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
106 |
g_pAudio->bit_rate = g_AQuality; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
107 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
108 |
{ |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
109 |
g_pAudio->flags |= CODEC_FLAG_QSCALE; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
110 |
g_pAudio->global_quality = g_AQuality*FF_QP2LAMBDA; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
111 |
} |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
112 |
|
7180 | 113 |
// some formats want stream headers to be separate |
114 |
if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
|
115 |
g_pAudio->flags |= CODEC_FLAG_GLOBAL_HEADER; |
|
116 |
||
117 |
// open it |
|
118 |
if (avcodec_open2(g_pAudio, g_pACodec, NULL) < 0) |
|
119 |
FatalError("Could not open audio codec %s", g_pACodec->long_name); |
|
120 |
||
121 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
122 |
if (g_pACodec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) |
|
123 |
#else |
|
124 |
if (g_pAudio->frame_size == 0) |
|
125 |
#endif |
|
126 |
g_NumSamples = 4096; |
|
127 |
else |
|
128 |
g_NumSamples = g_pAudio->frame_size; |
|
129 |
g_pSamples = (int16_t*)av_malloc(g_NumSamples*g_Channels*sizeof(int16_t)); |
|
130 |
g_pAFrame = avcodec_alloc_frame(); |
|
131 |
if (!g_pAFrame) |
|
132 |
FatalError("Could not allocate frame"); |
|
133 |
} |
|
134 |
||
135 |
// returns non-zero if there is more sound |
|
136 |
static int WriteAudioFrame() |
|
137 |
{ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
138 |
if (!g_pAStream) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
139 |
return 0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
140 |
|
7180 | 141 |
AVPacket Packet = { 0 }; |
142 |
av_init_packet(&Packet); |
|
143 |
||
144 |
int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile); |
|
145 |
||
146 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
147 |
AVFrame* pFrame = NULL; |
|
148 |
if (NumSamples > 0) |
|
149 |
{ |
|
150 |
g_pAFrame->nb_samples = NumSamples; |
|
151 |
avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16, |
|
152 |
(uint8_t*)g_pSamples, NumSamples*2*g_Channels, 1); |
|
153 |
pFrame = g_pAFrame; |
|
154 |
} |
|
155 |
// when NumSamples == 0 we still need to call encode_audio2 to flush |
|
156 |
int got_packet; |
|
157 |
if (avcodec_encode_audio2(g_pAudio, &Packet, pFrame, &got_packet) != 0) |
|
158 |
FatalError("avcodec_encode_audio2 failed"); |
|
159 |
if (!got_packet) |
|
160 |
return 0; |
|
161 |
#else |
|
162 |
if (NumSamples == 0) |
|
163 |
return 0; |
|
164 |
int BufferSize = OUTBUFFER_SIZE; |
|
165 |
if (g_pAudio->frame_size == 0) |
|
166 |
BufferSize = NumSamples*g_Channels*2; |
|
167 |
Packet.size = avcodec_encode_audio(g_pAudio, g_OutBuffer, BufferSize, g_pSamples); |
|
168 |
if (Packet.size == 0) |
|
169 |
return 1; |
|
170 |
if (g_pAudio->coded_frame && g_pAudio->coded_frame->pts != AV_NOPTS_VALUE) |
|
171 |
Packet.pts = av_rescale_q(g_pAudio->coded_frame->pts, g_pAudio->time_base, g_pAStream->time_base); |
|
172 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
173 |
Packet.data = g_OutBuffer; |
|
174 |
#endif |
|
175 |
||
176 |
// Write the compressed frame to the media file. |
|
177 |
Packet.stream_index = g_pAStream->index; |
|
178 |
if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) |
|
179 |
FatalError("Error while writing audio frame"); |
|
180 |
return 1; |
|
181 |
} |
|
182 |
||
183 |
// add a video output stream |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
184 |
static void AddVideoStream() |
7180 | 185 |
{ |
186 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
187 |
g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec); |
|
188 |
#else |
|
189 |
g_pVStream = av_new_stream(g_pContainer, 0); |
|
190 |
#endif |
|
191 |
if (!g_pVStream) |
|
192 |
FatalError("Could not allocate video stream"); |
|
193 |
||
194 |
g_pVideo = g_pVStream->codec; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
195 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
196 |
avcodec_get_context_defaults3(g_pVideo, g_pVCodec); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
197 |
g_pVideo->codec_id = g_pVCodec->id; |
7180 | 198 |
|
199 |
// put parameters |
|
200 |
// resolution must be a multiple of two |
|
201 |
g_pVideo->width = g_Width; |
|
202 |
g_pVideo->height = g_Height; |
|
203 |
/* time base: this is the fundamental unit of time (in seconds) in terms |
|
204 |
of which frame timestamps are represented. for fixed-fps content, |
|
205 |
timebase should be 1/framerate and timestamp increments should be |
|
206 |
identically 1. */ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
207 |
g_pVideo->time_base.den = g_Framerate.num; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
208 |
g_pVideo->time_base.num = g_Framerate.den; |
7180 | 209 |
//g_pVideo->gop_size = 12; /* emit one intra frame every twelve frames at most */ |
210 |
g_pVideo->pix_fmt = PIX_FMT_YUV420P; |
|
211 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
212 |
// set quality |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
213 |
if (g_VQuality > 100) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
214 |
g_pVideo->bit_rate = g_VQuality; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
215 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
216 |
{ |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
217 |
g_pVideo->flags |= CODEC_FLAG_QSCALE; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
218 |
g_pVideo->global_quality = g_VQuality*FF_QP2LAMBDA; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
219 |
} |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
220 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
221 |
AVDictionary* pDict = NULL; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
222 |
if (strcmp(g_pVCodec->name, "libx264") == 0) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
223 |
av_dict_set(&pDict, "preset", g_pPreset, 0); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
224 |
|
7180 | 225 |
// some formats want stream headers to be separate |
226 |
if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
|
227 |
g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER; |
|
228 |
||
229 |
// open the codec |
|
230 |
if (avcodec_open2(g_pVideo, g_pVCodec, &pDict) < 0) |
|
231 |
FatalError("Could not open video codec %s", g_pVCodec->long_name); |
|
232 |
||
233 |
g_pVFrame = avcodec_alloc_frame(); |
|
234 |
if (!g_pVFrame) |
|
235 |
FatalError("Could not allocate frame"); |
|
236 |
||
237 |
g_pVFrame->linesize[0] = g_Width; |
|
238 |
g_pVFrame->linesize[1] = g_Width/2; |
|
239 |
g_pVFrame->linesize[2] = g_Width/2; |
|
240 |
g_pVFrame->linesize[3] = 0; |
|
241 |
} |
|
242 |
||
243 |
static int WriteFrame( AVFrame* pFrame ) |
|
244 |
{ |
|
245 |
double AudioTime, VideoTime; |
|
246 |
||
247 |
// write interleaved audio frame |
|
248 |
if (g_pAStream) |
|
249 |
{ |
|
250 |
VideoTime = (double)g_pVStream->pts.val*g_pVStream->time_base.num/g_pVStream->time_base.den; |
|
251 |
do |
|
252 |
AudioTime = (double)g_pAStream->pts.val*g_pAStream->time_base.num/g_pAStream->time_base.den; |
|
253 |
while (AudioTime < VideoTime && WriteAudioFrame()); |
|
254 |
} |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
255 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
256 |
if (!g_pVStream) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
257 |
return 0; |
7180 | 258 |
|
259 |
AVPacket Packet; |
|
260 |
av_init_packet(&Packet); |
|
261 |
Packet.data = NULL; |
|
262 |
Packet.size = 0; |
|
263 |
||
264 |
g_pVFrame->pts++; |
|
265 |
if (g_pFormat->flags & AVFMT_RAWPICTURE) |
|
266 |
{ |
|
267 |
/* raw video case. The API will change slightly in the near |
|
268 |
future for that. */ |
|
269 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
270 |
Packet.stream_index = g_pVStream->index; |
|
271 |
Packet.data = (uint8_t*)pFrame; |
|
272 |
Packet.size = sizeof(AVPicture); |
|
273 |
||
274 |
if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) |
|
275 |
FatalError("Error while writing video frame"); |
|
276 |
return 0; |
|
277 |
} |
|
278 |
else |
|
279 |
{ |
|
280 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
281 |
int got_packet; |
|
282 |
if (avcodec_encode_video2(g_pVideo, &Packet, pFrame, &got_packet) < 0) |
|
283 |
FatalError("avcodec_encode_video2 failed"); |
|
284 |
if (!got_packet) |
|
285 |
return 0; |
|
286 |
||
287 |
if (Packet.pts != AV_NOPTS_VALUE) |
|
288 |
Packet.pts = av_rescale_q(Packet.pts, g_pVideo->time_base, g_pVStream->time_base); |
|
289 |
if (Packet.dts != AV_NOPTS_VALUE) |
|
290 |
Packet.dts = av_rescale_q(Packet.dts, g_pVideo->time_base, g_pVStream->time_base); |
|
291 |
#else |
|
292 |
Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame); |
|
293 |
if (Packet.size < 0) |
|
294 |
FatalError("avcodec_encode_video failed"); |
|
295 |
if (Packet.size == 0) |
|
296 |
return 0; |
|
297 |
||
298 |
if( g_pVideo->coded_frame->pts != AV_NOPTS_VALUE) |
|
299 |
Packet.pts = av_rescale_q(g_pVideo->coded_frame->pts, g_pVideo->time_base, g_pVStream->time_base); |
|
300 |
if( g_pVideo->coded_frame->key_frame ) |
|
301 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
302 |
Packet.data = g_OutBuffer; |
|
303 |
#endif |
|
304 |
// write the compressed frame in the media file |
|
305 |
Packet.stream_index = g_pVStream->index; |
|
306 |
if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) |
|
307 |
FatalError("Error while writing video frame"); |
|
308 |
||
309 |
return 1; |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
void AVWrapper_WriteFrame(uint8_t* pY, uint8_t* pCb, uint8_t* pCr) |
|
314 |
{ |
|
315 |
g_pVFrame->data[0] = pY; |
|
316 |
g_pVFrame->data[1] = pCb; |
|
317 |
g_pVFrame->data[2] = pCr; |
|
318 |
WriteFrame(g_pVFrame); |
|
319 |
} |
|
320 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
321 |
void AVWrapper_Init( |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
322 |
void (*pAddFileLogRaw)(const char*), |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
323 |
const char* pFilename, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
324 |
const char* pSoundFile, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
325 |
const char* pFormatName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
326 |
const char* pVCodecName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
327 |
const char* pACodecName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
328 |
const char* pVPreset, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
329 |
int Width, int Height, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
330 |
int FramerateNum, int FramerateDen, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
331 |
int Frequency, int Channels, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
332 |
int VQuality, int AQuality) |
7180 | 333 |
{ |
334 |
AddFileLogRaw = pAddFileLogRaw; |
|
335 |
av_log_set_callback( &LogCallback ); |
|
336 |
||
337 |
g_Width = Width; |
|
338 |
g_Height = Height; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
339 |
g_Framerate.num = FramerateNum; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
340 |
g_Framerate.den = FramerateDen; |
7180 | 341 |
g_Frequency = Frequency; |
342 |
g_Channels = Channels; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
343 |
g_VQuality = VQuality; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
344 |
g_AQuality = AQuality; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
345 |
g_pPreset = pVPreset; |
7180 | 346 |
|
347 |
// initialize libav and register all codecs and formats |
|
348 |
av_register_all(); |
|
349 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
350 |
// find format |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
351 |
g_pFormat = av_guess_format(pFormatName, NULL, NULL); |
7180 | 352 |
if (!g_pFormat) |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
353 |
FatalError("Format \"%s\" was not found", pFormatName); |
7180 | 354 |
|
355 |
// allocate the output media context |
|
356 |
g_pContainer = avformat_alloc_context(); |
|
357 |
if (!g_pContainer) |
|
358 |
FatalError("Could not allocate output context"); |
|
359 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
360 |
g_pContainer->oformat = g_pFormat; |
7180 | 361 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
362 |
// append extesnion to filename |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
363 |
snprintf(g_pContainer->filename, sizeof(g_pContainer->filename), |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
364 |
"%s.%*s", |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
365 |
pFilename, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
366 |
strcspn(g_pFormat->extensions, ","), g_pFormat->extensions); |
7180 | 367 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
368 |
// find codecs |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
369 |
g_pVCodec = avcodec_find_encoder_by_name(pVCodecName); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
370 |
g_pACodec = avcodec_find_encoder_by_name(pACodecName); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
371 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
372 |
// add audio and video stream to container |
7180 | 373 |
g_pVStream = NULL; |
374 |
g_pAStream = NULL; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
375 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
376 |
if (g_pVCodec) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
377 |
AddVideoStream(); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
378 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
379 |
Log("Video codec \"%s\" was not found; video will be ignored.\n", pVCodecName); |
7180 | 380 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
381 |
if (g_pACodec) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
382 |
AddAudioStream(); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
383 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
384 |
Log("Audio codec \"%s\" was not found; audio will be ignored.\n", pACodecName); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
385 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
386 |
if (!g_pAStream && !g_pVStream) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
387 |
FatalError("No video, no audio, aborting..."); |
7180 | 388 |
|
389 |
if (g_pAStream) |
|
390 |
{ |
|
391 |
g_pSoundFile = fopen(pSoundFile, "rb"); |
|
392 |
if (!g_pSoundFile) |
|
393 |
FatalError("Could not open %s", pSoundFile); |
|
394 |
} |
|
395 |
||
396 |
// write format info to log |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
397 |
av_dump_format(g_pContainer, 0, g_pContainer->filename, 1); |
7180 | 398 |
|
399 |
// open the output file, if needed |
|
400 |
if (!(g_pFormat->flags & AVFMT_NOFILE)) |
|
401 |
{ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
402 |
if (avio_open(&g_pContainer->pb, g_pContainer->filename, AVIO_FLAG_WRITE) < 0) |
7180 | 403 |
FatalError("Could not open output file (%s)", pFilename); |
404 |
} |
|
405 |
||
406 |
// write the stream header, if any |
|
407 |
avformat_write_header(g_pContainer, NULL); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
408 |
|
7180 | 409 |
g_pVFrame->pts = -1; |
410 |
} |
|
411 |
||
412 |
void AVWrapper_Close() |
|
413 |
{ |
|
414 |
// output buffered frames |
|
415 |
if (g_pVCodec->capabilities & CODEC_CAP_DELAY) |
|
416 |
while( WriteFrame(NULL) ); |
|
417 |
// output any remaining audio |
|
418 |
while( WriteAudioFrame() ); |
|
419 |
||
420 |
// write the trailer, if any. |
|
421 |
av_write_trailer(g_pContainer); |
|
422 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
423 |
// close the output file |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
424 |
if (!(g_pFormat->flags & AVFMT_NOFILE)) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
425 |
avio_close(g_pContainer->pb); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
426 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
427 |
// free everything |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
428 |
if (g_pVStream) |
7180 | 429 |
{ |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
430 |
avcodec_close(g_pVideo); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
431 |
av_free(g_pVideo); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
432 |
av_free(g_pVStream); |
7180 | 433 |
av_free(g_pVFrame); |
434 |
} |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
435 |
if (g_pAStream) |
7180 | 436 |
{ |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
437 |
avcodec_close(g_pAudio); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
438 |
av_free(g_pAudio); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
439 |
av_free(g_pAStream); |
7180 | 440 |
av_free(g_pAFrame); |
441 |
av_free(g_pSamples); |
|
442 |
fclose(g_pSoundFile); |
|
443 |
} |
|
444 |
||
445 |
av_free(g_pContainer); |
|
446 |
} |