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