author | nemo |
Fri, 28 Nov 2014 14:58:33 -0500 | |
changeset 10556 | c4ca0fc00c3b |
parent 10108 | c68cf030eded |
child 11046 | 47a8c19ecb60 |
permissions | -rw-r--r-- |
7542
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
1 |
/* |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
9998 | 3 |
* Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com> |
7542
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
4 |
* |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
8 |
* |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
12 |
* GNU General Public License for more details. |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
13 |
* |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
9998
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
7542
37ef1891efe2
Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
17 |
*/ |
7180 | 18 |
|
19 |
#include <stdlib.h> |
|
20 |
#include <stdio.h> |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
21 |
#include <stdint.h> |
7180 | 22 |
#include <string.h> |
23 |
#include <stdarg.h> |
|
24 |
#include "libavformat/avformat.h" |
|
8065 | 25 |
#include "libavutil/mathematics.h" |
7180 | 26 |
|
7356
1ae5cf294216
fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents:
7306
diff
changeset
|
27 |
#ifndef AVIO_FLAG_WRITE |
1ae5cf294216
fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents:
7306
diff
changeset
|
28 |
#define AVIO_FLAG_WRITE AVIO_WRONLY |
1ae5cf294216
fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents:
7306
diff
changeset
|
29 |
#endif |
1ae5cf294216
fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents:
7306
diff
changeset
|
30 |
|
9203
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
31 |
#if (defined _MSC_VER) |
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
32 |
#define AVWRAP_DECL __declspec(dllexport) |
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
33 |
#elif ((__GNUC__ >= 3) && (!__EMX__) && (!sun)) |
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
34 |
#define AVWRAP_DECL __attribute__((visibility("default"))) |
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
35 |
#else |
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
36 |
#define AVWRAP_DECL |
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
37 |
#endif |
bcc25cb9b122
and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents:
9080
diff
changeset
|
38 |
|
7180 | 39 |
static AVFormatContext* g_pContainer; |
40 |
static AVOutputFormat* g_pFormat; |
|
41 |
static AVStream* g_pAStream; |
|
42 |
static AVStream* g_pVStream; |
|
43 |
static AVFrame* g_pAFrame; |
|
44 |
static AVFrame* g_pVFrame; |
|
45 |
static AVCodec* g_pACodec; |
|
46 |
static AVCodec* g_pVCodec; |
|
47 |
static AVCodecContext* g_pAudio; |
|
48 |
static AVCodecContext* g_pVideo; |
|
49 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
50 |
static int g_Width, g_Height; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
51 |
static uint32_t g_Frequency, g_Channels; |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
52 |
static int g_VQuality; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
53 |
static AVRational g_Framerate; |
7180 | 54 |
|
55 |
static FILE* g_pSoundFile; |
|
56 |
static int16_t* g_pSamples; |
|
57 |
static int g_NumSamples; |
|
58 |
||
59 |
||
60 |
#if LIBAVCODEC_VERSION_MAJOR < 54 |
|
61 |
#define OUTBUFFER_SIZE 200000 |
|
62 |
static uint8_t g_OutBuffer[OUTBUFFER_SIZE]; |
|
63 |
#endif |
|
64 |
||
65 |
// pointer to function from hwengine (uUtils.pas) |
|
66 |
static void (*AddFileLogRaw)(const char* pString); |
|
67 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
68 |
static int FatalError(const char* pFmt, ...) |
7180 | 69 |
{ |
8065 | 70 |
char Buffer[1024]; |
7180 | 71 |
va_list VaArgs; |
72 |
||
73 |
va_start(VaArgs, pFmt); |
|
74 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
75 |
va_end(VaArgs); |
|
76 |
||
77 |
AddFileLogRaw("Error in av-wrapper: "); |
|
78 |
AddFileLogRaw(Buffer); |
|
79 |
AddFileLogRaw("\n"); |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
80 |
return(-1); |
7180 | 81 |
} |
82 |
||
83 |
// Function to be called from libav for logging. |
|
84 |
// Note: libav can call LogCallback from different threads |
|
85 |
// (there is mutex in AddFileLogRaw). |
|
86 |
static void LogCallback(void* p, int Level, const char* pFmt, va_list VaArgs) |
|
87 |
{ |
|
8065 | 88 |
char Buffer[1024]; |
7180 | 89 |
|
90 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
91 |
AddFileLogRaw(Buffer); |
|
92 |
} |
|
93 |
||
94 |
static void Log(const char* pFmt, ...) |
|
95 |
{ |
|
8065 | 96 |
char Buffer[1024]; |
7180 | 97 |
va_list VaArgs; |
98 |
||
99 |
va_start(VaArgs, pFmt); |
|
100 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
101 |
va_end(VaArgs); |
|
102 |
||
103 |
AddFileLogRaw(Buffer); |
|
104 |
} |
|
105 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
106 |
static void AddAudioStream() |
7180 | 107 |
{ |
8065 | 108 |
#if LIBAVFORMAT_VERSION_MAJOR >= 53 |
7180 | 109 |
g_pAStream = avformat_new_stream(g_pContainer, g_pACodec); |
110 |
#else |
|
111 |
g_pAStream = av_new_stream(g_pContainer, 1); |
|
112 |
#endif |
|
113 |
if(!g_pAStream) |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
114 |
{ |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7282
diff
changeset
|
115 |
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
|
116 |
return; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
117 |
} |
7180 | 118 |
g_pAStream->id = 1; |
119 |
||
120 |
g_pAudio = g_pAStream->codec; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
121 |
|
7180 | 122 |
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
|
123 |
g_pAudio->codec_id = g_pACodec->id; |
7180 | 124 |
|
125 |
// put parameters |
|
126 |
g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16; |
|
127 |
g_pAudio->sample_rate = g_Frequency; |
|
128 |
g_pAudio->channels = g_Channels; |
|
129 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
130 |
// set quality |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
131 |
g_pAudio->bit_rate = 160000; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
132 |
|
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
133 |
// for codecs that support variable bitrate use it, it should be better |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
134 |
g_pAudio->flags |= CODEC_FLAG_QSCALE; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
135 |
g_pAudio->global_quality = 1*FF_QP2LAMBDA; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
136 |
|
7180 | 137 |
// some formats want stream headers to be separate |
138 |
if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
|
139 |
g_pAudio->flags |= CODEC_FLAG_GLOBAL_HEADER; |
|
140 |
||
141 |
// open it |
|
7359
8bb4da06b065
Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents:
7356
diff
changeset
|
142 |
#if LIBAVCODEC_VERSION_MAJOR >= 53 |
7180 | 143 |
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
|
144 |
#else |
8bb4da06b065
Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents:
7356
diff
changeset
|
145 |
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
|
146 |
#endif |
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 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
|
149 |
return; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
150 |
} |
7180 | 151 |
|
152 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
153 |
if (g_pACodec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) |
|
154 |
#else |
|
155 |
if (g_pAudio->frame_size == 0) |
|
156 |
#endif |
|
157 |
g_NumSamples = 4096; |
|
158 |
else |
|
159 |
g_NumSamples = g_pAudio->frame_size; |
|
160 |
g_pSamples = (int16_t*)av_malloc(g_NumSamples*g_Channels*sizeof(int16_t)); |
|
161 |
g_pAFrame = avcodec_alloc_frame(); |
|
162 |
if (!g_pAFrame) |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
163 |
{ |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7282
diff
changeset
|
164 |
Log("Could not allocate frame\n"); |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
165 |
return; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
166 |
} |
7180 | 167 |
} |
168 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
169 |
// returns non-zero if there is more sound, -1 in case of error |
7180 | 170 |
static int WriteAudioFrame() |
171 |
{ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
172 |
if (!g_pAStream) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
173 |
return 0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
174 |
|
7180 | 175 |
AVPacket Packet = { 0 }; |
176 |
av_init_packet(&Packet); |
|
177 |
||
178 |
int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile); |
|
179 |
||
8065 | 180 |
#if LIBAVCODEC_VERSION_MAJOR >= 53 |
7180 | 181 |
AVFrame* pFrame = NULL; |
182 |
if (NumSamples > 0) |
|
183 |
{ |
|
184 |
g_pAFrame->nb_samples = NumSamples; |
|
185 |
avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16, |
|
186 |
(uint8_t*)g_pSamples, NumSamples*2*g_Channels, 1); |
|
187 |
pFrame = g_pAFrame; |
|
188 |
} |
|
189 |
// when NumSamples == 0 we still need to call encode_audio2 to flush |
|
190 |
int got_packet; |
|
191 |
if (avcodec_encode_audio2(g_pAudio, &Packet, pFrame, &got_packet) != 0) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
192 |
return FatalError("avcodec_encode_audio2 failed"); |
7180 | 193 |
if (!got_packet) |
194 |
return 0; |
|
195 |
#else |
|
196 |
if (NumSamples == 0) |
|
197 |
return 0; |
|
198 |
int BufferSize = OUTBUFFER_SIZE; |
|
199 |
if (g_pAudio->frame_size == 0) |
|
200 |
BufferSize = NumSamples*g_Channels*2; |
|
201 |
Packet.size = avcodec_encode_audio(g_pAudio, g_OutBuffer, BufferSize, g_pSamples); |
|
202 |
if (Packet.size == 0) |
|
203 |
return 1; |
|
204 |
if (g_pAudio->coded_frame && g_pAudio->coded_frame->pts != AV_NOPTS_VALUE) |
|
205 |
Packet.pts = av_rescale_q(g_pAudio->coded_frame->pts, g_pAudio->time_base, g_pAStream->time_base); |
|
206 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
207 |
Packet.data = g_OutBuffer; |
|
208 |
#endif |
|
209 |
||
210 |
// Write the compressed frame to the media file. |
|
211 |
Packet.stream_index = g_pAStream->index; |
|
8811 | 212 |
if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
213 |
return FatalError("Error while writing audio frame"); |
7180 | 214 |
return 1; |
215 |
} |
|
216 |
||
217 |
// add a video output stream |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
218 |
static int AddVideoStream() |
7180 | 219 |
{ |
8065 | 220 |
#if LIBAVFORMAT_VERSION_MAJOR >= 53 |
7180 | 221 |
g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec); |
222 |
#else |
|
223 |
g_pVStream = av_new_stream(g_pContainer, 0); |
|
224 |
#endif |
|
225 |
if (!g_pVStream) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
226 |
return FatalError("Could not allocate video stream"); |
7180 | 227 |
|
228 |
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
|
229 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
230 |
avcodec_get_context_defaults3(g_pVideo, g_pVCodec); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
231 |
g_pVideo->codec_id = g_pVCodec->id; |
7180 | 232 |
|
233 |
// put parameters |
|
234 |
// 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
|
235 |
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
|
236 |
g_pVideo->height = g_Height & ~1; // make even |
7180 | 237 |
/* time base: this is the fundamental unit of time (in seconds) in terms |
238 |
of which frame timestamps are represented. for fixed-fps content, |
|
239 |
timebase should be 1/framerate and timestamp increments should be |
|
240 |
identically 1. */ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
241 |
g_pVideo->time_base.den = g_Framerate.num; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
242 |
g_pVideo->time_base.num = g_Framerate.den; |
7180 | 243 |
//g_pVideo->gop_size = 12; /* emit one intra frame every twelve frames at most */ |
244 |
g_pVideo->pix_fmt = PIX_FMT_YUV420P; |
|
245 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
246 |
// set quality |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
247 |
if (g_VQuality > 100) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
248 |
g_pVideo->bit_rate = g_VQuality; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
249 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
250 |
{ |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
251 |
g_pVideo->flags |= CODEC_FLAG_QSCALE; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
252 |
g_pVideo->global_quality = g_VQuality*FF_QP2LAMBDA; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
253 |
} |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
254 |
|
7180 | 255 |
// some formats want stream headers to be separate |
256 |
if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
|
257 |
g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER; |
|
258 |
||
8065 | 259 |
#if LIBAVCODEC_VERSION_MAJOR < 53 |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
260 |
// for some versions of ffmpeg x264 options must be set explicitly |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
261 |
if (strcmp(g_pVCodec->name, "libx264") == 0) |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
262 |
{ |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
263 |
g_pVideo->coder_type = FF_CODER_TYPE_AC; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
264 |
g_pVideo->flags |= CODEC_FLAG_LOOP_FILTER; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
265 |
g_pVideo->crf = 23; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
266 |
g_pVideo->thread_count = 3; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
267 |
g_pVideo->me_cmp = FF_CMP_CHROMA; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
268 |
g_pVideo->partitions = X264_PART_I8X8 | X264_PART_I4X4 | X264_PART_P8X8 | X264_PART_B8X8; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
269 |
g_pVideo->me_method = ME_HEX; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
270 |
g_pVideo->me_subpel_quality = 7; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
271 |
g_pVideo->me_range = 16; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
272 |
g_pVideo->gop_size = 250; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
273 |
g_pVideo->keyint_min = 25; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
274 |
g_pVideo->scenechange_threshold = 40; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
275 |
g_pVideo->i_quant_factor = 0.71; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
276 |
g_pVideo->b_frame_strategy = 1; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
277 |
g_pVideo->qcompress = 0.6; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
278 |
g_pVideo->qmin = 10; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
279 |
g_pVideo->qmax = 51; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
280 |
g_pVideo->max_qdiff = 4; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
281 |
g_pVideo->max_b_frames = 3; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
282 |
g_pVideo->refs = 3; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
283 |
g_pVideo->directpred = 1; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
284 |
g_pVideo->trellis = 1; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
285 |
g_pVideo->flags2 = CODEC_FLAG2_BPYRAMID | CODEC_FLAG2_MIXED_REFS | CODEC_FLAG2_WPRED | CODEC_FLAG2_8X8DCT | CODEC_FLAG2_FASTPSKIP; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
286 |
g_pVideo->weighted_p_pred = 2; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
287 |
} |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
288 |
#endif |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
289 |
|
7180 | 290 |
// open the codec |
7359
8bb4da06b065
Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents:
7356
diff
changeset
|
291 |
#if LIBAVCODEC_VERSION_MAJOR >= 53 |
8bb4da06b065
Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents:
7356
diff
changeset
|
292 |
AVDictionary* pDict = NULL; |
8bb4da06b065
Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents:
7356
diff
changeset
|
293 |
if (strcmp(g_pVCodec->name, "libx264") == 0) |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
294 |
av_dict_set(&pDict, "preset", "medium", 0); |
7359
8bb4da06b065
Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents:
7356
diff
changeset
|
295 |
|
7180 | 296 |
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
|
297 |
#else |
8bb4da06b065
Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents:
7356
diff
changeset
|
298 |
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
|
299 |
#endif |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
300 |
return FatalError("Could not open video codec %s", g_pVCodec->long_name); |
7180 | 301 |
|
302 |
g_pVFrame = avcodec_alloc_frame(); |
|
303 |
if (!g_pVFrame) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
304 |
return FatalError("Could not allocate frame"); |
7180 | 305 |
|
306 |
g_pVFrame->linesize[0] = g_Width; |
|
307 |
g_pVFrame->linesize[1] = g_Width/2; |
|
308 |
g_pVFrame->linesize[2] = g_Width/2; |
|
309 |
g_pVFrame->linesize[3] = 0; |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
310 |
return 0; |
7180 | 311 |
} |
312 |
||
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
313 |
static int WriteFrame(AVFrame* pFrame) |
7180 | 314 |
{ |
315 |
double AudioTime, VideoTime; |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
316 |
int ret; |
7180 | 317 |
// write interleaved audio frame |
318 |
if (g_pAStream) |
|
319 |
{ |
|
320 |
VideoTime = (double)g_pVStream->pts.val*g_pVStream->time_base.num/g_pVStream->time_base.den; |
|
321 |
do |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
322 |
{ |
7180 | 323 |
AudioTime = (double)g_pAStream->pts.val*g_pAStream->time_base.num/g_pAStream->time_base.den; |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
324 |
ret = WriteAudioFrame(); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
325 |
} |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
326 |
while (AudioTime < VideoTime && ret); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
327 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
328 |
return ret; |
7180 | 329 |
} |
8811 | 330 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
331 |
if (!g_pVStream) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
332 |
return 0; |
7180 | 333 |
|
334 |
AVPacket Packet; |
|
335 |
av_init_packet(&Packet); |
|
336 |
Packet.data = NULL; |
|
337 |
Packet.size = 0; |
|
338 |
||
339 |
g_pVFrame->pts++; |
|
340 |
if (g_pFormat->flags & AVFMT_RAWPICTURE) |
|
341 |
{ |
|
342 |
/* raw video case. The API will change slightly in the near |
|
343 |
future for that. */ |
|
344 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
345 |
Packet.stream_index = g_pVStream->index; |
|
346 |
Packet.data = (uint8_t*)pFrame; |
|
347 |
Packet.size = sizeof(AVPicture); |
|
348 |
||
349 |
if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
350 |
return FatalError("Error while writing video frame"); |
7180 | 351 |
return 0; |
352 |
} |
|
353 |
else |
|
354 |
{ |
|
355 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
356 |
int got_packet; |
|
357 |
if (avcodec_encode_video2(g_pVideo, &Packet, pFrame, &got_packet) < 0) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
358 |
return FatalError("avcodec_encode_video2 failed"); |
7180 | 359 |
if (!got_packet) |
360 |
return 0; |
|
361 |
||
362 |
if (Packet.pts != AV_NOPTS_VALUE) |
|
363 |
Packet.pts = av_rescale_q(Packet.pts, g_pVideo->time_base, g_pVStream->time_base); |
|
364 |
if (Packet.dts != AV_NOPTS_VALUE) |
|
365 |
Packet.dts = av_rescale_q(Packet.dts, g_pVideo->time_base, g_pVStream->time_base); |
|
8811 | 366 |
#else |
7180 | 367 |
Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame); |
368 |
if (Packet.size < 0) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
369 |
return FatalError("avcodec_encode_video failed"); |
7180 | 370 |
if (Packet.size == 0) |
371 |
return 0; |
|
372 |
||
373 |
if( g_pVideo->coded_frame->pts != AV_NOPTS_VALUE) |
|
374 |
Packet.pts = av_rescale_q(g_pVideo->coded_frame->pts, g_pVideo->time_base, g_pVStream->time_base); |
|
375 |
if( g_pVideo->coded_frame->key_frame ) |
|
376 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
377 |
Packet.data = g_OutBuffer; |
|
378 |
#endif |
|
379 |
// write the compressed frame in the media file |
|
380 |
Packet.stream_index = g_pVStream->index; |
|
381 |
if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
382 |
return FatalError("Error while writing video frame"); |
8811 | 383 |
|
7180 | 384 |
return 1; |
385 |
} |
|
386 |
} |
|
387 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
388 |
AVWRAP_DECL int AVWrapper_WriteFrame(uint8_t* pY, uint8_t* pCb, uint8_t* pCr) |
7180 | 389 |
{ |
390 |
g_pVFrame->data[0] = pY; |
|
391 |
g_pVFrame->data[1] = pCb; |
|
392 |
g_pVFrame->data[2] = pCr; |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
393 |
return WriteFrame(g_pVFrame); |
7180 | 394 |
} |
395 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
396 |
AVWRAP_DECL int AVWrapper_Init( |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
397 |
void (*pAddFileLogRaw)(const char*), |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
398 |
const char* pFilename, |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
399 |
const char* pDesc, |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
400 |
const char* pSoundFile, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
401 |
const char* pFormatName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
402 |
const char* pVCodecName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
403 |
const char* pACodecName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
404 |
int Width, int Height, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
405 |
int FramerateNum, int FramerateDen, |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7359
diff
changeset
|
406 |
int VQuality) |
8811 | 407 |
{ |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
408 |
int ret; |
7180 | 409 |
AddFileLogRaw = pAddFileLogRaw; |
410 |
av_log_set_callback( &LogCallback ); |
|
411 |
||
7282
c65992e2d794
correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents:
7280
diff
changeset
|
412 |
g_Width = Width; |
c65992e2d794
correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents:
7280
diff
changeset
|
413 |
g_Height = Height; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
414 |
g_Framerate.num = FramerateNum; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
415 |
g_Framerate.den = FramerateDen; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
416 |
g_VQuality = VQuality; |
7180 | 417 |
|
418 |
// initialize libav and register all codecs and formats |
|
419 |
av_register_all(); |
|
420 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
421 |
// find format |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
422 |
g_pFormat = av_guess_format(pFormatName, NULL, NULL); |
7180 | 423 |
if (!g_pFormat) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
424 |
return FatalError("Format \"%s\" was not found", pFormatName); |
7180 | 425 |
|
426 |
// allocate the output media context |
|
427 |
g_pContainer = avformat_alloc_context(); |
|
428 |
if (!g_pContainer) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
429 |
return FatalError("Could not allocate output context"); |
7180 | 430 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
431 |
g_pContainer->oformat = g_pFormat; |
7180 | 432 |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
433 |
// store description of file |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
434 |
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
|
435 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
436 |
// 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
|
437 |
char ext[16]; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
438 |
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
|
439 |
ext[15] = 0; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
440 |
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
|
441 |
snprintf(g_pContainer->filename, sizeof(g_pContainer->filename), "%s.%s", pFilename, ext); |
7180 | 442 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
443 |
// find codecs |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
444 |
g_pVCodec = avcodec_find_encoder_by_name(pVCodecName); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
445 |
g_pACodec = avcodec_find_encoder_by_name(pACodecName); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
446 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
447 |
// add audio and video stream to container |
7180 | 448 |
g_pVStream = NULL; |
449 |
g_pAStream = NULL; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
450 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
451 |
if (g_pVCodec) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
452 |
{ |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
453 |
ret = AddVideoStream(); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
454 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
455 |
return ret; |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
456 |
} |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
457 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
458 |
Log("Video codec \"%s\" was not found; video will be ignored.\n", pVCodecName); |
7180 | 459 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
460 |
if (g_pACodec) |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
461 |
{ |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
462 |
g_pSoundFile = fopen(pSoundFile, "rb"); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
463 |
if (g_pSoundFile) |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
464 |
{ |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
465 |
fread(&g_Frequency, 4, 1, g_pSoundFile); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
466 |
fread(&g_Channels, 4, 1, g_pSoundFile); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
467 |
AddAudioStream(); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
468 |
} |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
469 |
else |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7282
diff
changeset
|
470 |
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
|
471 |
} |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
472 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
473 |
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
|
474 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
475 |
if (!g_pAStream && !g_pVStream) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
476 |
return FatalError("No video, no audio, aborting..."); |
7180 | 477 |
|
478 |
// write format info to log |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
479 |
av_dump_format(g_pContainer, 0, g_pContainer->filename, 1); |
7180 | 480 |
|
481 |
// open the output file, if needed |
|
482 |
if (!(g_pFormat->flags & AVFMT_NOFILE)) |
|
483 |
{ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
484 |
if (avio_open(&g_pContainer->pb, g_pContainer->filename, AVIO_FLAG_WRITE) < 0) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
485 |
return FatalError("Could not open output file (%s)", g_pContainer->filename); |
7180 | 486 |
} |
487 |
||
488 |
// write the stream header, if any |
|
489 |
avformat_write_header(g_pContainer, NULL); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
490 |
|
7180 | 491 |
g_pVFrame->pts = -1; |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
492 |
return 0; |
7180 | 493 |
} |
494 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
495 |
AVWRAP_DECL int AVWrapper_Close() |
7180 | 496 |
{ |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
497 |
int ret; |
7180 | 498 |
// output buffered frames |
499 |
if (g_pVCodec->capabilities & CODEC_CAP_DELAY) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
500 |
{ |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
501 |
do |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
502 |
ret = WriteFrame(NULL); |
9929
01609dac9d79
Fixed commit 887b9eca023c94c26daf9607c7ac9b18172c634d
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9925
diff
changeset
|
503 |
while (ret >= 0); |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
504 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
505 |
return ret; |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
506 |
} |
7180 | 507 |
// output any remaining audio |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
508 |
do |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
509 |
{ |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
510 |
ret = WriteAudioFrame(); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
511 |
} |
9929
01609dac9d79
Fixed commit 887b9eca023c94c26daf9607c7ac9b18172c634d
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9925
diff
changeset
|
512 |
while(ret >= 0); |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
513 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
514 |
return ret; |
7180 | 515 |
|
516 |
// write the trailer, if any. |
|
517 |
av_write_trailer(g_pContainer); |
|
518 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
519 |
// close the output file |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
520 |
if (!(g_pFormat->flags & AVFMT_NOFILE)) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
521 |
avio_close(g_pContainer->pb); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
522 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
523 |
// free everything |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
524 |
if (g_pVStream) |
7180 | 525 |
{ |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
526 |
avcodec_close(g_pVideo); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
527 |
av_free(g_pVideo); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
528 |
av_free(g_pVStream); |
7180 | 529 |
av_free(g_pVFrame); |
530 |
} |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
531 |
if (g_pAStream) |
7180 | 532 |
{ |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
533 |
avcodec_close(g_pAudio); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
534 |
av_free(g_pAudio); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
535 |
av_free(g_pAStream); |
7180 | 536 |
av_free(g_pAFrame); |
537 |
av_free(g_pSamples); |
|
538 |
fclose(g_pSoundFile); |
|
539 |
} |
|
540 |
||
541 |
av_free(g_pContainer); |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
542 |
return 0; |
7180 | 543 |
} |