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