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