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