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