QTfrontend/util/libav_iteraction.cpp
changeset 7235 baa69bd025d9
child 7280 fd707afbc3a2
equal deleted inserted replaced
7231:f484455dd055 7235:baa69bd025d9
       
     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 
       
    28 #include "libav_iteraction.h"
       
    29 #include "HWApplication.h"
       
    30 
       
    31 struct Codec
       
    32 {
       
    33     CodecID id;
       
    34     bool isAudio;
       
    35     QString shortName; // used for identification
       
    36     QString longName; // used for displaying to user
       
    37     bool isRecomended;
       
    38 };
       
    39 
       
    40 struct Format
       
    41 {
       
    42     QString shortName;
       
    43     QString longName;
       
    44     bool isRecomended;
       
    45   //  QString extension;
       
    46     QVector<Codec*> codecs;
       
    47 };
       
    48 
       
    49 QList<Codec> codecs;
       
    50 QMap<QString,Format> formats;
       
    51 
       
    52 LibavIteraction & LibavIteraction::instance()
       
    53 {
       
    54     static LibavIteraction instance;
       
    55     return instance;
       
    56 }
       
    57 
       
    58 bool FormatQueryCodec(AVOutputFormat *ofmt, enum CodecID codec_id)
       
    59 {  
       
    60 #if LIBAVFORMAT_VERSION_MAJOR >= 54
       
    61     return avformat_query_codec(ofmt, codec_id, FF_COMPLIANCE_NORMAL) == 1;
       
    62 #else
       
    63     if (ofmt->codec_tag)
       
    64         return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
       
    65     return codec_id == ofmt->video_codec || codec_id == ofmt->audio_codec;
       
    66 #endif
       
    67 }
       
    68 
       
    69 LibavIteraction::LibavIteraction()
       
    70 {
       
    71     // initialize libav and register all codecs and formats
       
    72     av_register_all();
       
    73 
       
    74     // get list of all codecs
       
    75     AVCodec* pCodec = NULL;
       
    76     while (pCodec = av_codec_next(pCodec))
       
    77     {
       
    78 #if LIBAVCODEC_VERSION_MAJOR >= 54
       
    79         if (!av_codec_is_encoder(pCodec))
       
    80 #else
       
    81         if (!pCodec->encode)
       
    82 #endif
       
    83             continue;
       
    84 
       
    85         if (pCodec->capabilities & CODEC_CAP_EXPERIMENTAL)
       
    86             continue;
       
    87 
       
    88         if (pCodec->type != AVMEDIA_TYPE_VIDEO && pCodec->type != AVMEDIA_TYPE_AUDIO)
       
    89             continue;
       
    90 
       
    91         // this encoders seems to be buggy
       
    92         if (strcmp(pCodec->name, "rv10") == 0 ||
       
    93             strcmp(pCodec->name, "rv20") == 0)
       
    94             continue;
       
    95 
       
    96         // doesn't support stereo sound
       
    97         if (strcmp(pCodec->name, "real_144") == 0)
       
    98             continue;
       
    99 #if LIBAVCODEC_VERSION_MAJOR < 54
       
   100         // FIXME: theese doesn't work for some reason
       
   101         if (strcmp(pCodec->name, "libx264") == 0)
       
   102             continue;
       
   103         if (strcmp(pCodec->name, "libxvid") == 0)
       
   104             continue;
       
   105 #endif
       
   106 
       
   107         if (pCodec->type == AVMEDIA_TYPE_VIDEO)
       
   108         {
       
   109             if (pCodec->supported_framerates != NULL)
       
   110                 continue;
       
   111 
       
   112             // check if codec supports yuv 4:2:0 format
       
   113             if (!pCodec->pix_fmts)
       
   114                 continue;
       
   115             bool yuv420Supported = false;
       
   116             for (const PixelFormat* pfmt = pCodec->pix_fmts; *pfmt != -1; pfmt++)
       
   117                 if (*pfmt == PIX_FMT_YUV420P)
       
   118                 {
       
   119                     yuv420Supported = true;
       
   120                     break;
       
   121                 }
       
   122             if (!yuv420Supported)
       
   123                 continue;
       
   124         }
       
   125         if (pCodec->type == AVMEDIA_TYPE_AUDIO)
       
   126         {
       
   127             // check if codec supports signed 16-bit format
       
   128             if (!pCodec->sample_fmts)
       
   129                 continue;
       
   130             bool s16Supported = false;
       
   131             for (const AVSampleFormat* pfmt = pCodec->sample_fmts; *pfmt != -1; pfmt++)
       
   132                 if (*pfmt == AV_SAMPLE_FMT_S16/* || *pfmt == AV_SAMPLE_FMT_FLT*/)
       
   133                 {
       
   134                     s16Supported = true;
       
   135                     break;
       
   136                 }
       
   137             if (!s16Supported)
       
   138                 continue;
       
   139         }
       
   140         // add codec to list of codecs
       
   141         codecs.push_back(Codec());
       
   142         Codec & codec = codecs.back();
       
   143         codec.id = pCodec->id;
       
   144         codec.isAudio = pCodec->type == AVMEDIA_TYPE_AUDIO;
       
   145         codec.shortName = pCodec->name;
       
   146         codec.longName = pCodec->long_name;
       
   147 
       
   148         codec.isRecomended = false;
       
   149         if (strcmp(pCodec->name, "libx264") == 0)
       
   150         {
       
   151             codec.longName = "H.264/MPEG-4 Part 10 AVC (x264)";
       
   152             codec.isRecomended = true;
       
   153         }
       
   154         else if (strcmp(pCodec->name, "libxvid") == 0)
       
   155         {
       
   156             codec.longName = "MPEG-4 Part 2 (Xvid)";
       
   157             codec.isRecomended = true;
       
   158         }
       
   159         else if (strcmp(pCodec->name, "libmp3lame") == 0)
       
   160         {
       
   161             codec.longName = "MP3 (MPEG audio layer 3) (LAME)";
       
   162             codec.isRecomended = true;
       
   163         }
       
   164         else
       
   165             codec.longName = pCodec->long_name;
       
   166 
       
   167         if (strcmp(pCodec->name, "mpeg4") == 0 || strcmp(pCodec->name, "ac3_fixed") == 0)
       
   168             codec.isRecomended = true;
       
   169 
       
   170         // FIXME: remove next line
       
   171         codec.longName += QString(" (%1)").arg(codec.shortName);
       
   172     }
       
   173 
       
   174     // get list of all formats
       
   175     AVOutputFormat* pFormat = NULL;
       
   176     while (pFormat = av_oformat_next(pFormat))
       
   177     {
       
   178         if (!pFormat->extensions)
       
   179             continue;
       
   180 
       
   181         // skip some strange formats to not confuse users
       
   182         if (strstr(pFormat->long_name, "raw"))
       
   183             continue;
       
   184 
       
   185         Format format;
       
   186         bool hasVideoCodec = false;
       
   187         for (QList<Codec>::iterator codec = codecs.begin(); codec != codecs.end(); ++codec)
       
   188         {
       
   189             if (!FormatQueryCodec(pFormat, codec->id))
       
   190                 continue;
       
   191             format.codecs.push_back(&*codec);
       
   192             if (!codec->isAudio)
       
   193                 hasVideoCodec = true;
       
   194         }
       
   195         if (!hasVideoCodec)
       
   196             continue;
       
   197         format.shortName = pFormat->name;
       
   198 
       
   199         QString ext(pFormat->extensions);
       
   200         ext.truncate(strcspn(pFormat->extensions, ","));
       
   201         format.longName = QString("%1 (*.%2)").arg(pFormat->long_name).arg(ext);
       
   202 
       
   203         // FIXME: remove next line
       
   204         format.longName += QString(" (%1)").arg(format.shortName);
       
   205 
       
   206         format.isRecomended = strcmp(pFormat->name, "mp4") == 0 || strcmp(pFormat->name, "avi") == 0;
       
   207 
       
   208         formats[pFormat->name] = format;
       
   209     }
       
   210 }
       
   211 
       
   212 void LibavIteraction::FillFormats(QComboBox * pFormats)
       
   213 {
       
   214     // first insert recomended formats
       
   215     foreach(const Format & format, formats)
       
   216         if (format.isRecomended)
       
   217             pFormats->addItem(format.longName, format.shortName);
       
   218 
       
   219     // remember where to place separator between recomended and other formats
       
   220     int sep = pFormats->count();
       
   221 
       
   222     // insert remaining formats
       
   223     foreach(const Format & format, formats)
       
   224         if (!format.isRecomended)
       
   225             pFormats->addItem(format.longName, format.shortName);
       
   226 
       
   227     // insert separator if necessary
       
   228     if (sep != 0 && sep != pFormats->count())
       
   229         pFormats->insertSeparator(sep);
       
   230 }
       
   231 
       
   232 void LibavIteraction::FillCodecs(const QVariant & fmt, QComboBox * pVCodecs, QComboBox * pACodecs)
       
   233 {
       
   234     Format & format = formats[fmt.toString()];
       
   235 
       
   236     // first insert recomended codecs
       
   237     foreach(Codec * codec, format.codecs)
       
   238     {
       
   239         if (codec->isRecomended)
       
   240         {
       
   241             if (codec->isAudio)
       
   242                 pACodecs->addItem(codec->longName, codec->shortName);
       
   243             else
       
   244                 pVCodecs->addItem(codec->longName, codec->shortName);
       
   245         }
       
   246     }
       
   247 
       
   248     // remember where to place separators between recomended and other codecs
       
   249     int vsep = pVCodecs->count();
       
   250     int asep = pACodecs->count();
       
   251 
       
   252     // insert remaining codecs
       
   253     foreach(Codec * codec, format.codecs)
       
   254     {
       
   255         if (!codec->isRecomended)
       
   256         {
       
   257             if (codec->isAudio)
       
   258                 pACodecs->addItem(codec->longName, codec->shortName);
       
   259             else
       
   260                 pVCodecs->addItem(codec->longName, codec->shortName);
       
   261         }
       
   262     }
       
   263 
       
   264     // insert separators if necessary
       
   265     if (vsep != 0 && vsep != pVCodecs->count())
       
   266         pVCodecs->insertSeparator(vsep);
       
   267     if (asep != 0 && asep != pACodecs->count())
       
   268         pACodecs->insertSeparator(asep);
       
   269 }