|
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 #define __STDC_CONSTANT_MACROS |
|
20 extern "C" |
|
21 { |
|
22 #include "libavformat/avformat.h" |
|
23 } |
|
24 #include <QVector> |
|
25 #include <QList> |
|
26 #include <QMessageBox> |
|
27 #include <QComboBox> |
|
28 |
|
29 #include "libav_iteraction.h" |
|
30 #include "HWApplication.h" |
|
31 |
|
32 struct Codec |
|
33 { |
|
34 CodecID id; |
|
35 bool isAudio; |
|
36 QString shortName; // used for identification |
|
37 QString longName; // used for displaying to user |
|
38 bool isRecomended; |
|
39 }; |
|
40 |
|
41 struct Format |
|
42 { |
|
43 QString shortName; |
|
44 QString longName; |
|
45 bool isRecomended; |
|
46 QString extension; |
|
47 QVector<Codec*> codecs; |
|
48 }; |
|
49 |
|
50 QList<Codec> codecs; |
|
51 QMap<QString,Format> formats; |
|
52 |
|
53 LibavIteraction & LibavIteraction::instance() |
|
54 { |
|
55 static LibavIteraction instance; |
|
56 return instance; |
|
57 } |
|
58 |
|
59 // test if given format supports given codec |
|
60 bool FormatQueryCodec(AVOutputFormat *ofmt, enum CodecID codec_id) |
|
61 { |
|
62 #if LIBAVFORMAT_VERSION_MAJOR >= 54 |
|
63 return avformat_query_codec(ofmt, codec_id, FF_COMPLIANCE_NORMAL) == 1; |
|
64 #else |
|
65 if (ofmt->codec_tag) |
|
66 return !!av_codec_get_tag(ofmt->codec_tag, codec_id); |
|
67 return codec_id == ofmt->video_codec || codec_id == ofmt->audio_codec; |
|
68 #endif |
|
69 } |
|
70 |
|
71 LibavIteraction::LibavIteraction() |
|
72 { |
|
73 // initialize libav and register all codecs and formats |
|
74 av_register_all(); |
|
75 |
|
76 // get list of all codecs |
|
77 AVCodec* pCodec = NULL; |
|
78 while (pCodec = av_codec_next(pCodec)) |
|
79 { |
|
80 #if LIBAVCODEC_VERSION_MAJOR >= 54 |
|
81 if (!av_codec_is_encoder(pCodec)) |
|
82 #else |
|
83 if (!pCodec->encode) |
|
84 #endif |
|
85 continue; |
|
86 |
|
87 if (pCodec->type != AVMEDIA_TYPE_VIDEO && pCodec->type != AVMEDIA_TYPE_AUDIO) |
|
88 continue; |
|
89 |
|
90 // this encoders seems to be buggy |
|
91 if (strcmp(pCodec->name, "rv10") == 0 || strcmp(pCodec->name, "rv20") == 0) |
|
92 continue; |
|
93 |
|
94 // doesn't support stereo sound |
|
95 if (strcmp(pCodec->name, "real_144") == 0) |
|
96 continue; |
|
97 |
|
98 if (!pCodec->long_name || strlen(pCodec->long_name) == 0) |
|
99 continue; |
|
100 |
|
101 if (pCodec->type == AVMEDIA_TYPE_VIDEO) |
|
102 { |
|
103 if (pCodec->supported_framerates != NULL) |
|
104 continue; |
|
105 |
|
106 // check if codec supports yuv 4:2:0 format |
|
107 if (!pCodec->pix_fmts) |
|
108 continue; |
|
109 bool yuv420Supported = false; |
|
110 for (const PixelFormat* pfmt = pCodec->pix_fmts; *pfmt != -1; pfmt++) |
|
111 if (*pfmt == PIX_FMT_YUV420P) |
|
112 { |
|
113 yuv420Supported = true; |
|
114 break; |
|
115 } |
|
116 if (!yuv420Supported) |
|
117 continue; |
|
118 } |
|
119 if (pCodec->type == AVMEDIA_TYPE_AUDIO) |
|
120 { |
|
121 // check if codec supports signed 16-bit format |
|
122 if (!pCodec->sample_fmts) |
|
123 continue; |
|
124 bool s16Supported = false; |
|
125 for (const AVSampleFormat* pfmt = pCodec->sample_fmts; *pfmt != -1; pfmt++) |
|
126 if (*pfmt == AV_SAMPLE_FMT_S16) |
|
127 { |
|
128 s16Supported = true; |
|
129 break; |
|
130 } |
|
131 if (!s16Supported) |
|
132 continue; |
|
133 } |
|
134 // add codec to list of codecs |
|
135 codecs.push_back(Codec()); |
|
136 Codec & codec = codecs.back(); |
|
137 codec.id = pCodec->id; |
|
138 codec.isAudio = pCodec->type == AVMEDIA_TYPE_AUDIO; |
|
139 codec.shortName = pCodec->name; |
|
140 codec.longName = pCodec->long_name; |
|
141 |
|
142 codec.isRecomended = false; |
|
143 if (strcmp(pCodec->name, "libx264") == 0) |
|
144 { |
|
145 codec.longName = "H.264/MPEG-4 Part 10 AVC (x264)"; |
|
146 codec.isRecomended = true; |
|
147 } |
|
148 else if (strcmp(pCodec->name, "libxvid") == 0) |
|
149 { |
|
150 codec.longName = "MPEG-4 Part 2 (Xvid)"; |
|
151 codec.isRecomended = true; |
|
152 } |
|
153 else if (strcmp(pCodec->name, "libmp3lame") == 0) |
|
154 { |
|
155 codec.longName = "MP3 (MPEG audio layer 3) (LAME)"; |
|
156 codec.isRecomended = true; |
|
157 } |
|
158 else |
|
159 codec.longName = pCodec->long_name; |
|
160 |
|
161 if (strcmp(pCodec->name, "mpeg4") == 0 || strcmp(pCodec->name, "ac3_fixed") == 0) |
|
162 codec.isRecomended = true; |
|
163 |
|
164 // FIXME: remove next line |
|
165 //codec.longName += QString(" (%1)").arg(codec.shortName); |
|
166 } |
|
167 |
|
168 // get list of all formats |
|
169 AVOutputFormat* pFormat = NULL; |
|
170 while (pFormat = av_oformat_next(pFormat)) |
|
171 { |
|
172 if (!pFormat->extensions) |
|
173 continue; |
|
174 |
|
175 // skip some strange formats to not confuse users |
|
176 if (strstr(pFormat->long_name, "raw")) |
|
177 continue; |
|
178 |
|
179 Format format; |
|
180 bool hasVideoCodec = false; |
|
181 for (QList<Codec>::iterator codec = codecs.begin(); codec != codecs.end(); ++codec) |
|
182 { |
|
183 if (!FormatQueryCodec(pFormat, codec->id)) |
|
184 continue; |
|
185 format.codecs.push_back(&*codec); |
|
186 if (!codec->isAudio) |
|
187 hasVideoCodec = true; |
|
188 } |
|
189 if (!hasVideoCodec) |
|
190 continue; |
|
191 |
|
192 QString ext(pFormat->extensions); |
|
193 ext.truncate(strcspn(pFormat->extensions, ",")); |
|
194 format.extension = ext; |
|
195 format.shortName = pFormat->name; |
|
196 format.longName = QString("%1 (*.%2)").arg(pFormat->long_name).arg(ext); |
|
197 |
|
198 // FIXME: remove next line |
|
199 //format.longName += QString(" (%1)").arg(format.shortName); |
|
200 |
|
201 format.isRecomended = strcmp(pFormat->name, "mp4") == 0 || strcmp(pFormat->name, "avi") == 0; |
|
202 |
|
203 formats[pFormat->name] = format; |
|
204 } |
|
205 } |
|
206 |
|
207 void LibavIteraction::fillFormats(QComboBox * pFormats) |
|
208 { |
|
209 // first insert recomended formats |
|
210 foreach(const Format & format, formats) |
|
211 if (format.isRecomended) |
|
212 pFormats->addItem(format.longName, format.shortName); |
|
213 |
|
214 // remember where to place separator between recomended and other formats |
|
215 int sep = pFormats->count(); |
|
216 |
|
217 // insert remaining formats |
|
218 foreach(const Format & format, formats) |
|
219 if (!format.isRecomended) |
|
220 pFormats->addItem(format.longName, format.shortName); |
|
221 |
|
222 // insert separator if necessary |
|
223 if (sep != 0 && sep != pFormats->count()) |
|
224 pFormats->insertSeparator(sep); |
|
225 } |
|
226 |
|
227 void LibavIteraction::fillCodecs(const QString & fmt, QComboBox * pVCodecs, QComboBox * pACodecs) |
|
228 { |
|
229 Format & format = formats[fmt]; |
|
230 |
|
231 // first insert recomended codecs |
|
232 foreach(Codec * codec, format.codecs) |
|
233 { |
|
234 if (codec->isRecomended) |
|
235 { |
|
236 if (codec->isAudio) |
|
237 pACodecs->addItem(codec->longName, codec->shortName); |
|
238 else |
|
239 pVCodecs->addItem(codec->longName, codec->shortName); |
|
240 } |
|
241 } |
|
242 |
|
243 // remember where to place separators between recomended and other codecs |
|
244 int vsep = pVCodecs->count(); |
|
245 int asep = pACodecs->count(); |
|
246 |
|
247 // insert remaining codecs |
|
248 foreach(Codec * codec, format.codecs) |
|
249 { |
|
250 if (!codec->isRecomended) |
|
251 { |
|
252 if (codec->isAudio) |
|
253 pACodecs->addItem(codec->longName, codec->shortName); |
|
254 else |
|
255 pVCodecs->addItem(codec->longName, codec->shortName); |
|
256 } |
|
257 } |
|
258 |
|
259 // insert separators if necessary |
|
260 if (vsep != 0 && vsep != pVCodecs->count()) |
|
261 pVCodecs->insertSeparator(vsep); |
|
262 if (asep != 0 && asep != pACodecs->count()) |
|
263 pACodecs->insertSeparator(asep); |
|
264 } |
|
265 |
|
266 QString LibavIteraction::getExtension(const QString & format) |
|
267 { |
|
268 return formats[format].extension; |
|
269 } |
|
270 |
|
271 QString tr(QString a) |
|
272 { |
|
273 return a; |
|
274 } |
|
275 |
|
276 // get information abaout file (duration, resolution etc) in multiline string |
|
277 QString LibavIteraction::getFileInfo(const QString & filepath) |
|
278 { |
|
279 AVFormatContext* pContext = NULL; |
|
280 QByteArray utf8path = filepath.toUtf8(); |
|
281 if (avformat_open_input(&pContext, utf8path.data(), NULL, NULL) < 0) |
|
282 return ""; |
|
283 #if LIBAFORMAT_VERSION_MAJOR < 54 |
|
284 if (av_find_stream_info(pContext) < 0) |
|
285 #else |
|
286 if (avformat_find_stream_info(pContext, NULL) < 0) |
|
287 #endif |
|
288 return ""; |
|
289 |
|
290 int s = float(pContext->duration)/AV_TIME_BASE; |
|
291 QString desc = QString(tr("Duration: %1m %2s\n")).arg(s/60).arg(s%60); |
|
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 { |
|
303 desc += QString(tr("Video: %1x%2, ")).arg(pCodec->width).arg(pCodec->height); |
|
304 if (pStream->avg_frame_rate.den) |
|
305 { |
|
306 float fps = float(pStream->avg_frame_rate.num)/pStream->avg_frame_rate.den; |
|
307 desc += QString(tr("%1 fps, ")).arg(fps, 0, 'f', 2); |
|
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); |
|
315 desc += pDecoder? pDecoder->name : "unknown"; |
|
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 #if LIBAFORMAT_VERSION_MAJOR < 54 |
|
322 av_close_input_file(pContext); |
|
323 #else |
|
324 avformat_close_input(&pContext); |
|
325 #endif |
|
326 return desc; |
|
327 } |