QTfrontend/util/LibavInteraction.cpp
changeset 11298 9fcdf0969d53
parent 11046 47a8c19ecb60
child 11296 2f812d4513d1
equal deleted inserted replaced
11286:9bd2acb1b577 11298:9fcdf0969d53
    19 #include "LibavInteraction.h"
    19 #include "LibavInteraction.h"
    20 
    20 
    21 #if VIDEOREC
    21 #if VIDEOREC
    22 extern "C"
    22 extern "C"
    23 {
    23 {
       
    24 #include "libavcodec/avcodec.h"
    24 #include "libavformat/avformat.h"
    25 #include "libavformat/avformat.h"
       
    26 #include "libavutil/avutil.h"
    25 }
    27 }
    26 
    28 
    27 #include <QVector>
    29 #include <QVector>
    28 #include <QList>
    30 #include <QList>
    29 #include <QComboBox>
    31 #include <QComboBox>
    30 
    32 
    31 #include "HWApplication.h"
    33 #include "HWApplication.h"
    32 
    34 
    33 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 25, 0)
    35 // compatibility section
    34 #define CodecID AVCodecID
    36 #if LIBAVCODEC_VERSION_MAJOR < 55
       
    37 #define av_codec_is_encoder(x)          x->encode
       
    38 #define AVCodecID                       CodecID
       
    39 #endif
       
    40 
       
    41 #if LIBAVFORMAT_VERSION_MAJOR < 54
       
    42 #define avformat_find_stream_info(x, y) av_find_stream_info(x)
       
    43 #define avformat_close_input(x)         av_close_input_file(*(x))
       
    44 #endif
       
    45 
       
    46 #if LIBAVUTIL_VERSION_MAJOR < 54
       
    47 #define AVPixelFormat                   PixelFormat
       
    48 #define AV_PIX_FMT_YUV420P              PIX_FMT_YUV420P
    35 #endif
    49 #endif
    36 
    50 
    37 struct Codec
    51 struct Codec
    38 {
    52 {
    39     CodecID id;
    53     AVCodecID id;
    40     bool isAudio;
    54     bool isAudio;
    41     QString shortName; // used for identification
    55     QString shortName; // used for identification
    42     QString longName; // used for displaying to user
    56     QString longName; // used for displaying to user
    43     bool isRecomended;
    57     bool isRecomended;
    44 };
    58 };
    54 
    68 
    55 QList<Codec> codecs;
    69 QList<Codec> codecs;
    56 QMap<QString,Format> formats;
    70 QMap<QString,Format> formats;
    57 
    71 
    58 // test if given format supports given codec
    72 // test if given format supports given codec
    59 bool FormatQueryCodec(AVOutputFormat *ofmt, enum CodecID codec_id)
    73 bool FormatQueryCodec(AVOutputFormat *ofmt, enum AVCodecID codec_id)
    60 {
    74 {
    61 #if LIBAVFORMAT_VERSION_MAJOR >= 54
    75 #if LIBAVFORMAT_VERSION_MAJOR >= 54
    62     return avformat_query_codec(ofmt, codec_id, FF_COMPLIANCE_NORMAL) == 1;
    76     return avformat_query_codec(ofmt, codec_id, FF_COMPLIANCE_NORMAL) == 1;
    63 #else
    77 #else
    64     if (ofmt->codec_tag)
    78     if (ofmt->codec_tag)
    74 
    88 
    75     // get list of all codecs
    89     // get list of all codecs
    76     AVCodec* pCodec = NULL;
    90     AVCodec* pCodec = NULL;
    77     while ((pCodec = av_codec_next(pCodec)))
    91     while ((pCodec = av_codec_next(pCodec)))
    78     {
    92     {
    79 #if LIBAVCODEC_VERSION_MAJOR >= 54
       
    80         if (!av_codec_is_encoder(pCodec))
    93         if (!av_codec_is_encoder(pCodec))
    81 #else
       
    82         if (!pCodec->encode)
       
    83 #endif
       
    84             continue;
    94             continue;
    85 
    95 
    86         if (pCodec->type != AVMEDIA_TYPE_VIDEO && pCodec->type != AVMEDIA_TYPE_AUDIO)
    96         if (pCodec->type != AVMEDIA_TYPE_VIDEO && pCodec->type != AVMEDIA_TYPE_AUDIO)
    87             continue;
    97             continue;
    88 
    98 
   104 
   114 
   105             // check if codec supports yuv 4:2:0 format
   115             // check if codec supports yuv 4:2:0 format
   106             if (!pCodec->pix_fmts)
   116             if (!pCodec->pix_fmts)
   107                 continue;
   117                 continue;
   108             bool yuv420Supported = false;
   118             bool yuv420Supported = false;
   109             for (const PixelFormat* pfmt = pCodec->pix_fmts; *pfmt != -1; pfmt++)
   119             for (const AVPixelFormat* pfmt = pCodec->pix_fmts; *pfmt != -1; pfmt++)
   110                 if (*pfmt == PIX_FMT_YUV420P)
   120                 if (*pfmt == AV_PIX_FMT_YUV420P)
   111                 {
   121                 {
   112                     yuv420Supported = true;
   122                     yuv420Supported = true;
   113                     break;
   123                     break;
   114                 }
   124                 }
   115             if (!yuv420Supported)
   125             if (!yuv420Supported)
   272 {
   282 {
   273     AVFormatContext* pContext = NULL;
   283     AVFormatContext* pContext = NULL;
   274     QByteArray utf8path = filepath.toUtf8();
   284     QByteArray utf8path = filepath.toUtf8();
   275     if (avformat_open_input(&pContext, utf8path.data(), NULL, NULL) < 0)
   285     if (avformat_open_input(&pContext, utf8path.data(), NULL, NULL) < 0)
   276         return "";
   286         return "";
   277 #if LIBAVFORMAT_VERSION_MAJOR < 53
       
   278     if (av_find_stream_info(pContext) < 0)
       
   279 #else
       
   280     if (avformat_find_stream_info(pContext, NULL) < 0)
   287     if (avformat_find_stream_info(pContext, NULL) < 0)
   281 #endif
       
   282         return "";
   288         return "";
   283 
   289 
   284     int s = float(pContext->duration)/AV_TIME_BASE;
   290     int s = float(pContext->duration)/AV_TIME_BASE;
   285     QString desc = tr("Duration: %1m %2s").arg(s/60).arg(s%60) + "\n";
   291     QString desc = tr("Duration: %1m %2s").arg(s/60).arg(s%60) + "\n";
   286     for (int i = 0; i < (int)pContext->nb_streams; i++)
   292     for (int i = 0; i < (int)pContext->nb_streams; i++)
   310         desc += "\n";
   316         desc += "\n";
   311     }
   317     }
   312     AVDictionaryEntry* pComment = av_dict_get(pContext->metadata, "comment", NULL, 0);
   318     AVDictionaryEntry* pComment = av_dict_get(pContext->metadata, "comment", NULL, 0);
   313     if (pComment)
   319     if (pComment)
   314         desc += QString("\n") + pComment->value;
   320         desc += QString("\n") + pComment->value;
   315 #if LIBAVFORMAT_VERSION_MAJOR < 53
       
   316     av_close_input_file(pContext);
       
   317 #else
       
   318     avformat_close_input(&pContext);
   321     avformat_close_input(&pContext);
   319 #endif
       
   320     return desc;
   322     return desc;
   321 }
   323 }
   322 
   324 
   323 #else
   325 #else
   324 LibavInteraction::LibavInteraction() : QObject()
   326 LibavInteraction::LibavInteraction() : QObject()