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