author | koda |
Fri, 25 Mar 2016 10:54:23 -0400 | |
changeset 11618 | bc83e11e5ee7 |
parent 11614 | 3c959df6638e |
child 11619 | dc0fc56c03ca |
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 |
11046 | 3 |
* Copyright (c) 2004-2015 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> |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
24 |
|
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
25 |
#include "libavcodec/avcodec.h" |
7180 | 26 |
#include "libavformat/avformat.h" |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
27 |
#include "libavutil/avutil.h" |
8065 | 28 |
#include "libavutil/mathematics.h" |
7180 | 29 |
|
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
|
30 |
#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
|
31 |
#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
|
32 |
#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
|
33 |
#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
|
34 |
#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
|
35 |
#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
|
36 |
#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
|
37 |
|
7180 | 38 |
static AVFormatContext* g_pContainer; |
39 |
static AVOutputFormat* g_pFormat; |
|
40 |
static AVStream* g_pAStream; |
|
41 |
static AVStream* g_pVStream; |
|
42 |
static AVFrame* g_pAFrame; |
|
43 |
static AVFrame* g_pVFrame; |
|
44 |
static AVCodec* g_pACodec; |
|
45 |
static AVCodec* g_pVCodec; |
|
46 |
static AVCodecContext* g_pAudio; |
|
47 |
static AVCodecContext* g_pVideo; |
|
48 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
49 |
static int g_Width, g_Height; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
50 |
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
|
51 |
static int g_VQuality; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
52 |
static AVRational g_Framerate; |
7180 | 53 |
|
54 |
static FILE* g_pSoundFile; |
|
55 |
static int16_t* g_pSamples; |
|
56 |
static int g_NumSamples; |
|
57 |
||
58 |
||
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
59 |
// compatibility section |
7180 | 60 |
#if LIBAVCODEC_VERSION_MAJOR < 54 |
61 |
#define OUTBUFFER_SIZE 200000 |
|
62 |
static uint8_t g_OutBuffer[OUTBUFFER_SIZE]; |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
63 |
#define avcodec_open2(x, y, z) avcodec_open(x, y) |
7180 | 64 |
#endif |
65 |
||
11614 | 66 |
#if LIBAVCODEC_VERSION_MAJOR < 55 |
67 |
#define avcodec_default_get_buffer2(x, y ,z) avcodec_default_get_buffer(x, y) |
|
68 |
#endif |
|
69 |
||
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
70 |
#if LIBAVCODEC_VERSION_MAJOR < 56 |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
71 |
#define av_frame_alloc avcodec_alloc_frame |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
72 |
#define av_frame_free av_freep |
11618 | 73 |
#define av_frame_unref avcodec_get_frame_defaults |
11351
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
74 |
#define av_packet_rescale_ts rescale_ts |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
75 |
|
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
76 |
static void rescale_ts(AVPacket *pkt, AVRational ctb, AVRational stb) |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
77 |
{ |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
78 |
if (pkt->pts != AV_NOPTS_VALUE) |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
79 |
pkt->pts = av_rescale_q(pkt->pts, ctb, stb); |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
80 |
if (pkt->dts != AV_NOPTS_VALUE) |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
81 |
pkt->dts = av_rescale_q(pkt->dts, ctb, stb); |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
82 |
if (pkt->duration > 0) |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
83 |
pkt->duration = av_rescale_q(pkt->duration, ctb, stb); |
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
84 |
} |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
85 |
#endif |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
86 |
|
11401 | 87 |
#ifndef AV_CODEC_CAP_DELAY |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
88 |
#define AV_CODEC_CAP_DELAY CODEC_CAP_DELAY |
11401 | 89 |
#endif |
90 |
#ifndef AV_CODEC_CAP_VARIABLE_FRAME_SIZE |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
91 |
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE CODEC_CAP_VARIABLE_FRAME_SIZE |
11401 | 92 |
#endif |
93 |
#ifndef AV_CODEC_FLAG_GLOBAL_HEADER |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
94 |
#define AV_CODEC_FLAG_GLOBAL_HEADER CODEC_FLAG_GLOBAL_HEADER |
11401 | 95 |
#endif |
96 |
#ifndef AV_CODEC_FLAG_QSCALE |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
97 |
#define AV_CODEC_FLAG_QSCALE CODEC_FLAG_QSCALE |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
98 |
#endif |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
99 |
|
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
100 |
#if LIBAVFORMAT_VERSION_MAJOR < 53 |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
101 |
#define AVIO_FLAG_WRITE AVIO_WRONLY |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
102 |
#endif |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
103 |
|
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
104 |
#if LIBAVFORMAT_VERSION_MAJOR < 54 |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
105 |
#define avformat_new_stream(x, y) av_new_stream(x, y->type == AVMEDIA_TYPE_AUDIO) |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
106 |
#endif |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
107 |
|
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
108 |
#if LIBAVUTIL_VERSION_MAJOR < 54 |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
109 |
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
110 |
#endif |
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
111 |
|
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
112 |
|
7180 | 113 |
// pointer to function from hwengine (uUtils.pas) |
114 |
static void (*AddFileLogRaw)(const char* pString); |
|
115 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
116 |
static int FatalError(const char* pFmt, ...) |
7180 | 117 |
{ |
8065 | 118 |
char Buffer[1024]; |
7180 | 119 |
va_list VaArgs; |
120 |
||
121 |
va_start(VaArgs, pFmt); |
|
122 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
123 |
va_end(VaArgs); |
|
124 |
||
125 |
AddFileLogRaw("Error in av-wrapper: "); |
|
126 |
AddFileLogRaw(Buffer); |
|
127 |
AddFileLogRaw("\n"); |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
128 |
return(-1); |
7180 | 129 |
} |
130 |
||
131 |
// Function to be called from libav for logging. |
|
132 |
// Note: libav can call LogCallback from different threads |
|
133 |
// (there is mutex in AddFileLogRaw). |
|
134 |
static void LogCallback(void* p, int Level, const char* pFmt, va_list VaArgs) |
|
135 |
{ |
|
8065 | 136 |
char Buffer[1024]; |
7180 | 137 |
|
138 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
139 |
AddFileLogRaw(Buffer); |
|
140 |
} |
|
141 |
||
142 |
static void Log(const char* pFmt, ...) |
|
143 |
{ |
|
8065 | 144 |
char Buffer[1024]; |
7180 | 145 |
va_list VaArgs; |
146 |
||
147 |
va_start(VaArgs, pFmt); |
|
148 |
vsnprintf(Buffer, 1024, pFmt, VaArgs); |
|
149 |
va_end(VaArgs); |
|
150 |
||
151 |
AddFileLogRaw(Buffer); |
|
152 |
} |
|
153 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
154 |
static void AddAudioStream() |
7180 | 155 |
{ |
156 |
g_pAStream = avformat_new_stream(g_pContainer, g_pACodec); |
|
157 |
if(!g_pAStream) |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
158 |
{ |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7282
diff
changeset
|
159 |
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
|
160 |
return; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
161 |
} |
7180 | 162 |
g_pAStream->id = 1; |
163 |
||
164 |
g_pAudio = g_pAStream->codec; |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
165 |
|
7180 | 166 |
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
|
167 |
g_pAudio->codec_id = g_pACodec->id; |
7180 | 168 |
|
169 |
// put parameters |
|
170 |
g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16; |
|
171 |
g_pAudio->sample_rate = g_Frequency; |
|
172 |
g_pAudio->channels = g_Channels; |
|
173 |
||
11350
846aa36f7cdc
videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11349
diff
changeset
|
174 |
// set time base as invers of sample rate |
846aa36f7cdc
videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11349
diff
changeset
|
175 |
g_pAudio->time_base.den = g_pAStream->time_base.den = g_Frequency; |
846aa36f7cdc
videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11349
diff
changeset
|
176 |
g_pAudio->time_base.num = g_pAStream->time_base.num = 1; |
846aa36f7cdc
videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11349
diff
changeset
|
177 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
178 |
// 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
|
179 |
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
|
180 |
|
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
|
181 |
// for codecs that support variable bitrate use it, it should be better |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
182 |
g_pAudio->flags |= AV_CODEC_FLAG_QSCALE; |
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
|
183 |
g_pAudio->global_quality = 1*FF_QP2LAMBDA; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
184 |
|
7180 | 185 |
// some formats want stream headers to be separate |
186 |
if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
187 |
g_pAudio->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
7180 | 188 |
|
189 |
// open it |
|
190 |
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
|
191 |
{ |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7282
diff
changeset
|
192 |
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
|
193 |
return; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
194 |
} |
7180 | 195 |
|
196 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
197 |
if (g_pACodec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) |
7180 | 198 |
#else |
199 |
if (g_pAudio->frame_size == 0) |
|
200 |
#endif |
|
201 |
g_NumSamples = 4096; |
|
202 |
else |
|
203 |
g_NumSamples = g_pAudio->frame_size; |
|
204 |
g_pSamples = (int16_t*)av_malloc(g_NumSamples*g_Channels*sizeof(int16_t)); |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
205 |
g_pAFrame = av_frame_alloc(); |
7180 | 206 |
if (!g_pAFrame) |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
207 |
{ |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7282
diff
changeset
|
208 |
Log("Could not allocate frame\n"); |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
209 |
return; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
210 |
} |
7180 | 211 |
} |
212 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
213 |
// returns non-zero if there is more sound, -1 in case of error |
7180 | 214 |
static int WriteAudioFrame() |
215 |
{ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
216 |
if (!g_pAStream) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
217 |
return 0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
218 |
|
11300
f9fa0d6dc69f
Let initialization happen in the appropriate function
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11298
diff
changeset
|
219 |
AVPacket Packet; |
7180 | 220 |
av_init_packet(&Packet); |
11347
f830c080aa9d
videorec: Make sure the audio packet is considered empty
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11316
diff
changeset
|
221 |
Packet.data = NULL; |
f830c080aa9d
videorec: Make sure the audio packet is considered empty
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11316
diff
changeset
|
222 |
Packet.size = 0; |
7180 | 223 |
|
224 |
int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile); |
|
225 |
||
8065 | 226 |
#if LIBAVCODEC_VERSION_MAJOR >= 53 |
7180 | 227 |
AVFrame* pFrame = NULL; |
228 |
if (NumSamples > 0) |
|
229 |
{ |
|
230 |
g_pAFrame->nb_samples = NumSamples; |
|
231 |
avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16, |
|
232 |
(uint8_t*)g_pSamples, NumSamples*2*g_Channels, 1); |
|
233 |
pFrame = g_pAFrame; |
|
234 |
} |
|
235 |
// when NumSamples == 0 we still need to call encode_audio2 to flush |
|
236 |
int got_packet; |
|
237 |
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
|
238 |
return FatalError("avcodec_encode_audio2 failed"); |
7180 | 239 |
if (!got_packet) |
240 |
return 0; |
|
11351
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
241 |
|
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
242 |
av_packet_rescale_ts(&Packet, g_pAudio->time_base, g_pAStream->time_base); |
7180 | 243 |
#else |
244 |
if (NumSamples == 0) |
|
245 |
return 0; |
|
246 |
int BufferSize = OUTBUFFER_SIZE; |
|
247 |
if (g_pAudio->frame_size == 0) |
|
248 |
BufferSize = NumSamples*g_Channels*2; |
|
249 |
Packet.size = avcodec_encode_audio(g_pAudio, g_OutBuffer, BufferSize, g_pSamples); |
|
250 |
if (Packet.size == 0) |
|
251 |
return 1; |
|
252 |
if (g_pAudio->coded_frame && g_pAudio->coded_frame->pts != AV_NOPTS_VALUE) |
|
253 |
Packet.pts = av_rescale_q(g_pAudio->coded_frame->pts, g_pAudio->time_base, g_pAStream->time_base); |
|
254 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
255 |
Packet.data = g_OutBuffer; |
|
256 |
#endif |
|
257 |
||
258 |
// Write the compressed frame to the media file. |
|
259 |
Packet.stream_index = g_pAStream->index; |
|
8811 | 260 |
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
|
261 |
return FatalError("Error while writing audio frame"); |
7180 | 262 |
return 1; |
263 |
} |
|
264 |
||
265 |
// add a video output stream |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
266 |
static int AddVideoStream() |
7180 | 267 |
{ |
268 |
g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec); |
|
269 |
if (!g_pVStream) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
270 |
return FatalError("Could not allocate video stream"); |
7180 | 271 |
|
272 |
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
|
273 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
274 |
avcodec_get_context_defaults3(g_pVideo, g_pVCodec); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
275 |
g_pVideo->codec_id = g_pVCodec->id; |
7180 | 276 |
|
277 |
// put parameters |
|
278 |
// 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
|
279 |
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
|
280 |
g_pVideo->height = g_Height & ~1; // make even |
7180 | 281 |
/* time base: this is the fundamental unit of time (in seconds) in terms |
282 |
of which frame timestamps are represented. for fixed-fps content, |
|
283 |
timebase should be 1/framerate and timestamp increments should be |
|
284 |
identically 1. */ |
|
11350
846aa36f7cdc
videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11349
diff
changeset
|
285 |
g_pVideo->time_base.den = g_pVStream->time_base.den = g_Framerate.num; |
846aa36f7cdc
videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11349
diff
changeset
|
286 |
g_pVideo->time_base.num = g_pVStream->time_base.num = g_Framerate.den; |
11353
7ab863821864
videorec: Drop a commented out option
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11352
diff
changeset
|
287 |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
288 |
g_pVideo->pix_fmt = AV_PIX_FMT_YUV420P; |
7180 | 289 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
290 |
// set quality |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
291 |
if (g_VQuality > 100) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
292 |
g_pVideo->bit_rate = g_VQuality; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
293 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
294 |
{ |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
295 |
g_pVideo->flags |= AV_CODEC_FLAG_QSCALE; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
296 |
g_pVideo->global_quality = g_VQuality*FF_QP2LAMBDA; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
297 |
} |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
298 |
|
7180 | 299 |
// some formats want stream headers to be separate |
300 |
if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
301 |
g_pVideo->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
7180 | 302 |
|
8065 | 303 |
#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
|
304 |
// 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
|
305 |
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
|
306 |
{ |
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
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
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
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
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
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
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
|
325 |
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
|
326 |
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
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
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
|
331 |
} |
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
|
332 |
#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
|
333 |
|
7180 | 334 |
// open the codec |
11316 | 335 |
if (avcodec_open2(g_pVideo, g_pVCodec, NULL) < 0) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
336 |
return FatalError("Could not open video codec %s", g_pVCodec->long_name); |
7180 | 337 |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
338 |
g_pVFrame = av_frame_alloc(); |
7180 | 339 |
if (!g_pVFrame) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
340 |
return FatalError("Could not allocate frame"); |
11612
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
341 |
av_frame_unref(g_pVFrame); |
7180 | 342 |
|
11349
dc17a495ee7f
videorec: Fully set frame properties
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11348
diff
changeset
|
343 |
g_pVFrame->width = g_Width; |
dc17a495ee7f
videorec: Fully set frame properties
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11348
diff
changeset
|
344 |
g_pVFrame->height = g_Height; |
dc17a495ee7f
videorec: Fully set frame properties
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11348
diff
changeset
|
345 |
g_pVFrame->format = AV_PIX_FMT_YUV420P; |
11612
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
346 |
|
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
347 |
return avcodec_default_get_buffer2(g_pVideo, g_pVFrame, 0); |
7180 | 348 |
} |
349 |
||
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
350 |
static int WriteFrame(AVFrame* pFrame) |
7180 | 351 |
{ |
352 |
double AudioTime, VideoTime; |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
353 |
int ret; |
7180 | 354 |
// write interleaved audio frame |
355 |
if (g_pAStream) |
|
356 |
{ |
|
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
357 |
VideoTime = (double)g_pVFrame->pts * g_pVStream->time_base.num/g_pVStream->time_base.den; |
7180 | 358 |
do |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
359 |
{ |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
360 |
AudioTime = (double)g_pAFrame->pts * 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
|
361 |
ret = WriteAudioFrame(); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
362 |
} |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
363 |
while (AudioTime < VideoTime && ret); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
364 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
365 |
return ret; |
7180 | 366 |
} |
8811 | 367 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
368 |
if (!g_pVStream) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
369 |
return 0; |
7180 | 370 |
|
371 |
AVPacket Packet; |
|
372 |
av_init_packet(&Packet); |
|
373 |
Packet.data = NULL; |
|
374 |
Packet.size = 0; |
|
375 |
||
376 |
g_pVFrame->pts++; |
|
11352
ce154ffb9a8b
videorec: Use future API for raw video encoding
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11351
diff
changeset
|
377 |
#if LIBAVCODEC_VERSION_MAJOR < 58 |
7180 | 378 |
if (g_pFormat->flags & AVFMT_RAWPICTURE) |
379 |
{ |
|
380 |
/* raw video case. The API will change slightly in the near |
|
381 |
future for that. */ |
|
382 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
383 |
Packet.stream_index = g_pVStream->index; |
|
384 |
Packet.data = (uint8_t*)pFrame; |
|
385 |
Packet.size = sizeof(AVPicture); |
|
386 |
||
387 |
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
|
388 |
return FatalError("Error while writing video frame"); |
7180 | 389 |
return 0; |
390 |
} |
|
391 |
else |
|
11352
ce154ffb9a8b
videorec: Use future API for raw video encoding
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11351
diff
changeset
|
392 |
#endif |
7180 | 393 |
{ |
394 |
#if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
395 |
int got_packet; |
|
396 |
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
|
397 |
return FatalError("avcodec_encode_video2 failed"); |
7180 | 398 |
if (!got_packet) |
399 |
return 0; |
|
400 |
||
11351
82784d932577
videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11350
diff
changeset
|
401 |
av_packet_rescale_ts(&Packet, g_pVideo->time_base, g_pVStream->time_base); |
8811 | 402 |
#else |
7180 | 403 |
Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame); |
404 |
if (Packet.size < 0) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
405 |
return FatalError("avcodec_encode_video failed"); |
7180 | 406 |
if (Packet.size == 0) |
407 |
return 0; |
|
408 |
||
409 |
if( g_pVideo->coded_frame->pts != AV_NOPTS_VALUE) |
|
410 |
Packet.pts = av_rescale_q(g_pVideo->coded_frame->pts, g_pVideo->time_base, g_pVStream->time_base); |
|
411 |
if( g_pVideo->coded_frame->key_frame ) |
|
412 |
Packet.flags |= AV_PKT_FLAG_KEY; |
|
413 |
Packet.data = g_OutBuffer; |
|
414 |
#endif |
|
415 |
// write the compressed frame in the media file |
|
416 |
Packet.stream_index = g_pVStream->index; |
|
417 |
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
|
418 |
return FatalError("Error while writing video frame"); |
8811 | 419 |
|
7180 | 420 |
return 1; |
421 |
} |
|
422 |
} |
|
423 |
||
11612
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
424 |
AVWRAP_DECL int AVWrapper_WriteFrame(uint8_t *buf) |
7180 | 425 |
{ |
11612
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
426 |
int x, y, stride = g_Width * 4; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
427 |
uint8_t *data[3]; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
428 |
|
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
429 |
// copy pointers, prepare source |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
430 |
memcpy(data, g_pVFrame->data, sizeof(data)); |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
431 |
buf += (g_Height - 1) * stride; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
432 |
|
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
433 |
// convert to YUV 4:2:0 |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
434 |
for (y = 0; y < g_Height; y++) { |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
435 |
for (x = 0; x < g_Width; x++) { |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
436 |
int r = buf[x * 4 + 0]; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
437 |
int g = buf[x * 4 + 1]; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
438 |
int b = buf[x * 4 + 2]; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
439 |
|
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
440 |
int luma = (int)(0.299f * r + 0.587f * g + 0.114f * b); |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
441 |
data[0][x] = av_clip_uint8(luma); |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
442 |
|
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
443 |
if (!(x & 1) && !(y & 1)) { |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
444 |
int r = (buf[x * 4 + 0] + buf[(x + 1) * 4 + 0] + |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
445 |
buf[x * 4 + 0 + stride] + buf[(x + 1) * 4 + 0 + stride]) / 4; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
446 |
int g = (buf[x * 4 + 1] + buf[(x + 1) * 4 + 1] + |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
447 |
buf[x * 4 + 1 + stride] + buf[(x + 1) * 4 + 1 + stride]) / 4; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
448 |
int b = (buf[x * 4 + 2] + buf[(x + 1) * 4 + 2] + |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
449 |
buf[x * 4 + 2 + stride] + buf[(x + 1) * 4 + 2 + stride]) / 4; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
450 |
|
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
451 |
int cr = (int)(-0.14713f * r - 0.28886f * g + 0.436f * b); |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
452 |
int cb = (int)( 0.615f * r - 0.51499f * g - 0.10001f * b); |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
453 |
data[1][x / 2] = av_clip_uint8(128 + cr); |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
454 |
data[2][x / 2] = av_clip_uint8(128 + cb); |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
455 |
} |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
456 |
} |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
457 |
buf += -stride; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
458 |
data[0] += g_pVFrame->linesize[0]; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
459 |
if (y & 1) { |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
460 |
data[1] += g_pVFrame->linesize[1]; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
461 |
data[2] += g_pVFrame->linesize[2]; |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
462 |
} |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
463 |
} |
b7d5d75469ee
Move pixel format conversion from uVideoRec to AVWrapper
koda
parents:
11543
diff
changeset
|
464 |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
465 |
return WriteFrame(g_pVFrame); |
7180 | 466 |
} |
467 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
468 |
AVWRAP_DECL int AVWrapper_Init( |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
469 |
void (*pAddFileLogRaw)(const char*), |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
470 |
const char* pFilename, |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
471 |
const char* pDesc, |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
472 |
const char* pSoundFile, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
473 |
const char* pFormatName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
474 |
const char* pVCodecName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
475 |
const char* pACodecName, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
476 |
int Width, int Height, |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
477 |
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
|
478 |
int VQuality) |
8811 | 479 |
{ |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
480 |
int ret; |
7180 | 481 |
AddFileLogRaw = pAddFileLogRaw; |
482 |
av_log_set_callback( &LogCallback ); |
|
483 |
||
7282
c65992e2d794
correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents:
7280
diff
changeset
|
484 |
g_Width = Width; |
c65992e2d794
correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents:
7280
diff
changeset
|
485 |
g_Height = Height; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
486 |
g_Framerate.num = FramerateNum; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
487 |
g_Framerate.den = FramerateDen; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
488 |
g_VQuality = VQuality; |
7180 | 489 |
|
490 |
// initialize libav and register all codecs and formats |
|
491 |
av_register_all(); |
|
492 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
493 |
// find format |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
494 |
g_pFormat = av_guess_format(pFormatName, NULL, NULL); |
7180 | 495 |
if (!g_pFormat) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
496 |
return FatalError("Format \"%s\" was not found", pFormatName); |
7180 | 497 |
|
498 |
// allocate the output media context |
|
499 |
g_pContainer = avformat_alloc_context(); |
|
500 |
if (!g_pContainer) |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
501 |
return FatalError("Could not allocate output context"); |
7180 | 502 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
503 |
g_pContainer->oformat = g_pFormat; |
7180 | 504 |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
505 |
// store description of file |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
506 |
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
|
507 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
508 |
// 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
|
509 |
char ext[16]; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
510 |
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
|
511 |
ext[15] = 0; |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
512 |
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
|
513 |
snprintf(g_pContainer->filename, sizeof(g_pContainer->filename), "%s.%s", pFilename, ext); |
7180 | 514 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
515 |
// find codecs |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
516 |
g_pVCodec = avcodec_find_encoder_by_name(pVCodecName); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
517 |
g_pACodec = avcodec_find_encoder_by_name(pACodecName); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
518 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
519 |
// add audio and video stream to container |
7180 | 520 |
g_pVStream = NULL; |
521 |
g_pAStream = NULL; |
|
7198
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 |
if (g_pVCodec) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
524 |
{ |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
525 |
ret = AddVideoStream(); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
526 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
527 |
return ret; |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
528 |
} |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
529 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
530 |
Log("Video codec \"%s\" was not found; video will be ignored.\n", pVCodecName); |
7180 | 531 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
532 |
if (g_pACodec) |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
533 |
{ |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
534 |
g_pSoundFile = fopen(pSoundFile, "rb"); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
535 |
if (g_pSoundFile) |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
536 |
{ |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
537 |
fread(&g_Frequency, 4, 1, g_pSoundFile); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
538 |
fread(&g_Channels, 4, 1, g_pSoundFile); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
539 |
AddAudioStream(); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
540 |
} |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
541 |
else |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7282
diff
changeset
|
542 |
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
|
543 |
} |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
544 |
else |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
545 |
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
|
546 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
547 |
if (!g_pAStream && !g_pVStream) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
548 |
return FatalError("No video, no audio, aborting..."); |
7180 | 549 |
|
550 |
// write format info to log |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
551 |
av_dump_format(g_pContainer, 0, g_pContainer->filename, 1); |
7180 | 552 |
|
553 |
// open the output file, if needed |
|
554 |
if (!(g_pFormat->flags & AVFMT_NOFILE)) |
|
555 |
{ |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
556 |
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
|
557 |
return FatalError("Could not open output file (%s)", g_pContainer->filename); |
7180 | 558 |
} |
559 |
||
11543
3617c611406b
avwrapper: Do not ignore avformat_write_header return value
koda
parents:
11401
diff
changeset
|
560 |
g_pVFrame->pts = -1; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
561 |
|
11543
3617c611406b
avwrapper: Do not ignore avformat_write_header return value
koda
parents:
11401
diff
changeset
|
562 |
// write the stream header, if any |
3617c611406b
avwrapper: Do not ignore avformat_write_header return value
koda
parents:
11401
diff
changeset
|
563 |
return avformat_write_header(g_pContainer, NULL); |
7180 | 564 |
} |
565 |
||
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
566 |
AVWRAP_DECL int AVWrapper_Close() |
7180 | 567 |
{ |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
568 |
int ret; |
7180 | 569 |
// output buffered frames |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
570 |
if (g_pVCodec->capabilities & AV_CODEC_CAP_DELAY) |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
571 |
{ |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
572 |
do |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
573 |
ret = WriteFrame(NULL); |
11348
44248a9cdcac
videorec: Fix flushing encoders
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11347
diff
changeset
|
574 |
while (ret > 0); |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
575 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
576 |
return ret; |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
577 |
} |
7180 | 578 |
// output any remaining audio |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
579 |
do |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
580 |
{ |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
581 |
ret = WriteAudioFrame(); |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
582 |
} |
11348
44248a9cdcac
videorec: Fix flushing encoders
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11347
diff
changeset
|
583 |
while(ret > 0); |
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
584 |
if (ret < 0) |
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
585 |
return ret; |
7180 | 586 |
|
587 |
// write the trailer, if any. |
|
588 |
av_write_trailer(g_pContainer); |
|
589 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
590 |
// close the output file |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
591 |
if (!(g_pFormat->flags & AVFMT_NOFILE)) |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
592 |
avio_close(g_pContainer->pb); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
593 |
|
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
594 |
// free everything |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
595 |
if (g_pVStream) |
7180 | 596 |
{ |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
597 |
avcodec_close(g_pVideo); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
598 |
av_free(g_pVideo); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
599 |
av_free(g_pVStream); |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
600 |
av_frame_free(&g_pVFrame); |
7180 | 601 |
} |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
602 |
if (g_pAStream) |
7180 | 603 |
{ |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
604 |
avcodec_close(g_pAudio); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
605 |
av_free(g_pAudio); |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
606 |
av_free(g_pAStream); |
11298
9fcdf0969d53
Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents:
11046
diff
changeset
|
607 |
av_frame_free(&g_pAFrame); |
7180 | 608 |
av_free(g_pSamples); |
609 |
fclose(g_pSoundFile); |
|
610 |
} |
|
611 |
||
612 |
av_free(g_pContainer); |
|
9925
113e61a8cadc
Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9224
diff
changeset
|
613 |
return 0; |
7180 | 614 |
} |