7897
|
1 |
/*
|
|
2 |
* Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include "LibavInteraction.h"
|
|
20 |
|
|
21 |
#if VIDEOREC
|
|
22 |
extern "C"
|
|
23 |
{
|
|
24 |
#include "libavformat/avformat.h"
|
|
25 |
}
|
|
26 |
|
|
27 |
#include <QVector>
|
|
28 |
#include <QList>
|
|
29 |
#include <QComboBox>
|
|
30 |
|
|
31 |
#include "HWApplication.h"
|
|
32 |
|
|
33 |
struct Codec
|
|
34 |
{
|
|
35 |
CodecID id;
|
|
36 |
bool isAudio;
|
|
37 |
QString shortName; // used for identification
|
|
38 |
QString longName; // used for displaying to user
|
|
39 |
bool isRecomended;
|
|
40 |
};
|
|
41 |
|
|
42 |
struct Format
|
|
43 |
{
|
|
44 |
QString shortName;
|
|
45 |
QString longName;
|
|
46 |
bool isRecomended;
|
|
47 |
QString extension;
|
|
48 |
QVector<Codec*> codecs;
|
|
49 |
};
|
|
50 |
|
|
51 |
QList<Codec> codecs;
|
|
52 |
QMap<QString,Format> formats;
|
|
53 |
|
|
54 |
// test if given format supports given codec
|
|
55 |
bool FormatQueryCodec(AVOutputFormat *ofmt, enum CodecID codec_id)
|
|
56 |
{
|
|
57 |
#if LIBAVFORMAT_VERSION_MAJOR >= 54
|
|
58 |
return avformat_query_codec(ofmt, codec_id, FF_COMPLIANCE_NORMAL) == 1;
|
|
59 |
#else
|
|
60 |
if (ofmt->codec_tag)
|
|
61 |
return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
|
|
62 |
return codec_id == ofmt->video_codec || codec_id == ofmt->audio_codec;
|
|
63 |
#endif
|
|
64 |
}
|
|
65 |
|
|
66 |
LibavInteraction::LibavInteraction() : QObject()
|
|
67 |
{
|
|
68 |
// initialize libav and register all codecs and formats
|
|
69 |
av_register_all();
|
|
70 |
|
|
71 |
// get list of all codecs
|
|
72 |
AVCodec* pCodec = NULL;
|
|
73 |
while (pCodec = av_codec_next(pCodec))
|
|
74 |
{
|
|
75 |
#if LIBAVCODEC_VERSION_MAJOR >= 54
|
|
76 |
if (!av_codec_is_encoder(pCodec))
|
|
77 |
#else
|
|
78 |
if (!pCodec->encode)
|
|
79 |
#endif
|
|
80 |
continue;
|
|
81 |
|
|
82 |
if (pCodec->type != AVMEDIA_TYPE_VIDEO && pCodec->type != AVMEDIA_TYPE_AUDIO)
|
|
83 |
continue;
|
|
84 |
|
|
85 |
// this encoders seems to be buggy
|
|
86 |
if (strcmp(pCodec->name, "rv10") == 0 || strcmp(pCodec->name, "rv20") == 0)
|
|
87 |
continue;
|
|
88 |
|
|
89 |
// doesn't support stereo sound
|
|
90 |
if (strcmp(pCodec->name, "real_144") == 0)
|
|
91 |
continue;
|
|
92 |
|
|
93 |
if (!pCodec->long_name || strlen(pCodec->long_name) == 0)
|
|
94 |
continue;
|
|
95 |
|
|
96 |
if (pCodec->type == AVMEDIA_TYPE_VIDEO)
|
|
97 |
{
|
|
98 |
if (pCodec->supported_framerates != NULL)
|
|
99 |
continue;
|
|
100 |
|
|
101 |
// check if codec supports yuv 4:2:0 format
|
|
102 |
if (!pCodec->pix_fmts)
|
|
103 |
continue;
|
|
104 |
bool yuv420Supported = false;
|
|
105 |
for (const PixelFormat* pfmt = pCodec->pix_fmts; *pfmt != -1; pfmt++)
|
|
106 |
if (*pfmt == PIX_FMT_YUV420P)
|
|
107 |
{
|
|
108 |
yuv420Supported = true;
|
|
109 |
break;
|
|
110 |
}
|
|
111 |
if (!yuv420Supported)
|
|
112 |
continue;
|
|
113 |
}
|
|
114 |
if (pCodec->type == AVMEDIA_TYPE_AUDIO)
|
|
115 |
{
|
|
116 |
// check if codec supports signed 16-bit format
|
|
117 |
if (!pCodec->sample_fmts)
|
|
118 |
continue;
|
|
119 |
bool s16Supported = false;
|
|
120 |
for (const AVSampleFormat* pfmt = pCodec->sample_fmts; *pfmt != -1; pfmt++)
|
|
121 |
if (*pfmt == AV_SAMPLE_FMT_S16)
|
|
122 |
{
|
|
123 |
s16Supported = true;
|
|
124 |
break;
|
|
125 |
}
|
|
126 |
if (!s16Supported)
|
|
127 |
continue;
|
|
128 |
}
|
|
129 |
// add codec to list of codecs
|
|
130 |
codecs.push_back(Codec());
|
|
131 |
Codec & codec = codecs.back();
|
|
132 |
codec.id = pCodec->id;
|
|
133 |
codec.isAudio = pCodec->type == AVMEDIA_TYPE_AUDIO;
|
|
134 |
codec.shortName = pCodec->name;
|
|
135 |
codec.longName = pCodec->long_name;
|
|
136 |
|
|
137 |
codec.isRecomended = false;
|
|
138 |
if (strcmp(pCodec->name, "libx264") == 0)
|
|
139 |
{
|
|
140 |
codec.longName = "H.264/MPEG-4 Part 10 AVC (x264)";
|
|
141 |
codec.isRecomended = true;
|
|
142 |
}
|
|
143 |
else if (strcmp(pCodec->name, "libxvid") == 0)
|
|
144 |
{
|
|
145 |
codec.longName = "MPEG-4 Part 2 (Xvid)";
|
|
146 |
codec.isRecomended = true;
|
|
147 |
}
|
|
148 |
else if (strcmp(pCodec->name, "libmp3lame") == 0)
|
|
149 |
{
|
|
150 |
codec.longName = "MP3 (MPEG audio layer 3) (LAME)";
|
|
151 |
codec.isRecomended = true;
|
|
152 |
}
|
|
153 |
else
|
|
154 |
codec.longName = pCodec->long_name;
|
|
155 |
|
|
156 |
if (strcmp(pCodec->name, "mpeg4") == 0 || strcmp(pCodec->name, "ac3_fixed") == 0)
|
|
157 |
codec.isRecomended = true;
|
|
158 |
|
|
159 |
// FIXME: remove next line
|
|
160 |
//codec.longName += QString(" (%1)").arg(codec.shortName);
|
|
161 |
}
|
|
162 |
|
|
163 |
// get list of all formats
|
|
164 |
AVOutputFormat* pFormat = NULL;
|
|
165 |
while (pFormat = av_oformat_next(pFormat))
|
|
166 |
{
|
|
167 |
if (!pFormat->extensions)
|
|
168 |
continue;
|
|
169 |
|
|
170 |
// skip some strange formats to not confuse users
|
|
171 |
if (strstr(pFormat->long_name, "raw"))
|
|
172 |
continue;
|
|
173 |
|
|
174 |
Format format;
|
|
175 |
bool hasVideoCodec = false;
|
|
176 |
for (QList<Codec>::iterator codec = codecs.begin(); codec != codecs.end(); ++codec)
|
|
177 |
{
|
|
178 |
if (!FormatQueryCodec(pFormat, codec->id))
|
|
179 |
continue;
|
|
180 |
format.codecs.push_back(&*codec);
|
|
181 |
if (!codec->isAudio)
|
|
182 |
hasVideoCodec = true;
|
|
183 |
}
|
|
184 |
if (!hasVideoCodec)
|
|
185 |
continue;
|
|
186 |
|
|
187 |
QString ext(pFormat->extensions);
|
|
188 |
ext.truncate(strcspn(pFormat->extensions, ","));
|
|
189 |
format.extension = ext;
|
|
190 |
format.shortName = pFormat->name;
|
|
191 |
format.longName = QString("%1 (*.%2)").arg(pFormat->long_name).arg(ext);
|
|
192 |
|
|
193 |
// FIXME: remove next line
|
|
194 |
//format.longName += QString(" (%1)").arg(format.shortName);
|
|
195 |
|
|
196 |
format.isRecomended = strcmp(pFormat->name, "mp4") == 0 || strcmp(pFormat->name, "avi") == 0;
|
|
197 |
|
|
198 |
formats[pFormat->name] = format;
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
void LibavInteraction::fillFormats(QComboBox * pFormats)
|
|
203 |
{
|
|
204 |
// first insert recomended formats
|
|
205 |
foreach(const Format & format, formats)
|
|
206 |
if (format.isRecomended)
|
|
207 |
pFormats->addItem(format.longName, format.shortName);
|
|
208 |
|
|
209 |
// remember where to place separator between recomended and other formats
|
|
210 |
int sep = pFormats->count();
|
|
211 |
|
|
212 |
// insert remaining formats
|
|
213 |
foreach(const Format & format, formats)
|
|
214 |
if (!format.isRecomended)
|
|
215 |
pFormats->addItem(format.longName, format.shortName);
|
|
216 |
|
|
217 |
// insert separator if necessary
|
|
218 |
if (sep != 0 && sep != pFormats->count())
|
|
219 |
pFormats->insertSeparator(sep);
|
|
220 |
}
|
|
221 |
|
|
222 |
void LibavInteraction::fillCodecs(const QString & fmt, QComboBox * pVCodecs, QComboBox * pACodecs)
|
|
223 |
{
|
|
224 |
Format & format = formats[fmt];
|
|
225 |
|
|
226 |
// first insert recomended codecs
|
|
227 |
foreach(Codec * codec, format.codecs)
|
|
228 |
{
|
|
229 |
if (codec->isRecomended)
|
|
230 |
{
|
|
231 |
if (codec->isAudio)
|
|
232 |
pACodecs->addItem(codec->longName, codec->shortName);
|
|
233 |
else
|
|
234 |
pVCodecs->addItem(codec->longName, codec->shortName);
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
// remember where to place separators between recomended and other codecs
|
|
239 |
int vsep = pVCodecs->count();
|
|
240 |
int asep = pACodecs->count();
|
|
241 |
|
|
242 |
// insert remaining codecs
|
|
243 |
foreach(Codec * codec, format.codecs)
|
|
244 |
{
|
|
245 |
if (!codec->isRecomended)
|
|
246 |
{
|
|
247 |
if (codec->isAudio)
|
|
248 |
pACodecs->addItem(codec->longName, codec->shortName);
|
|
249 |
else
|
|
250 |
pVCodecs->addItem(codec->longName, codec->shortName);
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
// insert separators if necessary
|
|
255 |
if (vsep != 0 && vsep != pVCodecs->count())
|
|
256 |
pVCodecs->insertSeparator(vsep);
|
|
257 |
if (asep != 0 && asep != pACodecs->count())
|
|
258 |
pACodecs->insertSeparator(asep);
|
|
259 |
}
|
|
260 |
|
|
261 |
QString LibavInteraction::getExtension(const QString & format)
|
|
262 |
{
|
|
263 |
return formats[format].extension;
|
|
264 |
}
|
|
265 |
|
|
266 |
// get information abaout file (duration, resolution etc) in multiline string
|
|
267 |
QString LibavInteraction::getFileInfo(const QString & filepath)
|
|
268 |
{
|
|
269 |
AVFormatContext* pContext = NULL;
|
|
270 |
QByteArray utf8path = filepath.toUtf8();
|
|
271 |
if (avformat_open_input(&pContext, utf8path.data(), NULL, NULL) < 0)
|
|
272 |
return "";
|
|
273 |
#if LIBAVFORMAT_VERSION_MAJOR < 54
|
|
274 |
if (av_find_stream_info(pContext) < 0)
|
|
275 |
#else
|
|
276 |
if (avformat_find_stream_info(pContext, NULL) < 0)
|
|
277 |
#endif
|
|
278 |
return "";
|
|
279 |
|
|
280 |
int s = float(pContext->duration)/AV_TIME_BASE;
|
|
281 |
QString desc = QString(tr("Duration: %1m %2s\n")).arg(s/60).arg(s%60);
|
|
282 |
for (int i = 0; i < (int)pContext->nb_streams; i++)
|
|
283 |
{
|
|
284 |
AVStream* pStream = pContext->streams[i];
|
|
285 |
if (!pStream)
|
|
286 |
continue;
|
|
287 |
AVCodecContext* pCodec = pContext->streams[i]->codec;
|
|
288 |
if (!pCodec)
|
|
289 |
continue;
|
|
290 |
|
|
291 |
if (pCodec->codec_type == AVMEDIA_TYPE_VIDEO)
|
|
292 |
{
|
|
293 |
desc += QString(tr("Video: %1x%2, ")).arg(pCodec->width).arg(pCodec->height);
|
|
294 |
if (pStream->avg_frame_rate.den)
|
|
295 |
{
|
|
296 |
float fps = float(pStream->avg_frame_rate.num)/pStream->avg_frame_rate.den;
|
|
297 |
desc += QString(tr("%1 fps, ")).arg(fps, 0, 'f', 2);
|
|
298 |
}
|
|
299 |
}
|
|
300 |
else if (pCodec->codec_type == AVMEDIA_TYPE_AUDIO)
|
|
301 |
desc += tr("Audio: ");
|
|
302 |
else
|
|
303 |
continue;
|
|
304 |
AVCodec* pDecoder = avcodec_find_decoder(pCodec->codec_id);
|
|
305 |
desc += pDecoder? pDecoder->name : "unknown";
|
|
306 |
desc += "\n";
|
|
307 |
}
|
|
308 |
AVDictionaryEntry* pComment = av_dict_get(pContext->metadata, "comment", NULL, 0);
|
|
309 |
if (pComment)
|
|
310 |
desc += QString("\n") + pComment->value;
|
|
311 |
#if LIBAVFORMAT_VERSION_MAJOR < 54
|
|
312 |
av_close_input_file(pContext);
|
|
313 |
#else
|
|
314 |
avformat_close_input(&pContext);
|
|
315 |
#endif
|
|
316 |
return desc;
|
|
317 |
}
|
|
318 |
|
|
319 |
#else
|
|
320 |
LibavInteraction::LibavInteraction() : QObject()
|
|
321 |
{
|
|
322 |
|
|
323 |
}
|
|
324 |
|
|
325 |
void LibavInteraction::fillFormats(QComboBox * pFormats)
|
|
326 |
{
|
|
327 |
Q_UNUSED(pFormats);
|
|
328 |
}
|
|
329 |
|
|
330 |
void LibavInteraction::fillCodecs(const QString & format, QComboBox * pVCodecs, QComboBox * pACodecs)
|
|
331 |
{
|
|
332 |
Q_UNUSED(format);
|
|
333 |
Q_UNUSED(pVCodecs);
|
|
334 |
Q_UNUSED(pACodecs);
|
|
335 |
}
|
|
336 |
|
|
337 |
QString LibavInteraction::getExtension(const QString & format)
|
|
338 |
{
|
|
339 |
Q_UNUSED(format);
|
|
340 |
|
|
341 |
return QString();
|
|
342 |
}
|
|
343 |
|
|
344 |
QString LibavInteraction::getFileInfo(const QString & filepath)
|
|
345 |
{
|
|
346 |
Q_UNUSED(filepath);
|
|
347 |
|
|
348 |
return QString();
|
|
349 |
}
|
|
350 |
#endif
|
|
351 |
|
|
352 |
LibavInteraction & LibavInteraction::instance()
|
|
353 |
{
|
|
354 |
static LibavInteraction instance;
|
|
355 |
return instance;
|
|
356 |
}
|