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