plugins/multimedia/gstreamer/mediacapture/qgstreamervideoencode.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qgstreamervideoencode.h"
       
    43 #include "qgstreamercapturesession.h"
       
    44 #include "qgstreamermediacontainercontrol.h"
       
    45 
       
    46 #include <QtCore/qdebug.h>
       
    47 
       
    48 #include <math.h>
       
    49 
       
    50 QGstreamerVideoEncode::QGstreamerVideoEncode(QGstreamerCaptureSession *session)
       
    51     :QVideoEncoderControl(session), m_session(session)
       
    52 {
       
    53     QList<QByteArray> codecCandidates;
       
    54     codecCandidates << "video/h264" << "video/xvid" << "video/mpeg4" << "video/mpeg1" << "video/mpeg2" << "video/theora";
       
    55 
       
    56     m_elementNames["video/h264"] = "x264enc";
       
    57     m_elementNames["video/xvid"] = "xvidenc";
       
    58     m_elementNames["video/mpeg4"] = "ffenc_mpeg4";
       
    59     m_elementNames["video/mpeg1"] = "ffenc_mpeg1video";
       
    60     m_elementNames["video/mpeg2"] = "ffenc_mpeg2video";
       
    61     m_elementNames["video/theora"] = "theoraenc";
       
    62 
       
    63     m_codecOptions["video/h264"] = QStringList() << "quantizer";
       
    64     m_codecOptions["video/xvid"] = QStringList() << "quantizer" << "profile";
       
    65     m_codecOptions["video/mpeg4"] = QStringList() << "quantizer";
       
    66     m_codecOptions["video/mpeg1"] = QStringList() << "quantizer";
       
    67     m_codecOptions["video/mpeg2"] = QStringList() << "quantizer";
       
    68     m_codecOptions["video/theora"] = QStringList();
       
    69 
       
    70     foreach( const QByteArray& codecName, codecCandidates ) {
       
    71         QByteArray elementName = m_elementNames[codecName];
       
    72         GstElementFactory *factory = gst_element_factory_find(elementName.constData());
       
    73         if (factory) {
       
    74             m_codecs.append(codecName);
       
    75             const gchar *descr = gst_element_factory_get_description(factory);
       
    76             m_codecDescriptions.insert(codecName, QString::fromUtf8(descr));
       
    77 
       
    78             m_streamTypes.insert(codecName,
       
    79                                  QGstreamerMediaContainerControl::supportedStreamTypes(factory, GST_PAD_SRC));
       
    80 
       
    81             gst_object_unref(GST_OBJECT(factory));
       
    82         }
       
    83     }
       
    84 
       
    85     //if (!m_codecs.isEmpty())
       
    86     //    m_videoSettings.setCodec(m_codecs[0]);
       
    87 }
       
    88 
       
    89 QGstreamerVideoEncode::~QGstreamerVideoEncode()
       
    90 {
       
    91 }
       
    92 
       
    93 QList<QSize> QGstreamerVideoEncode::supportedResolutions(const QVideoEncoderSettings &, bool *continuous) const
       
    94 {
       
    95     if (continuous)
       
    96         *continuous = m_session->videoInput() != 0;
       
    97 
       
    98     return m_session->videoInput() ? m_session->videoInput()->supportedResolutions() : QList<QSize>();
       
    99 }
       
   100 
       
   101 QList< qreal > QGstreamerVideoEncode::supportedFrameRates(const QVideoEncoderSettings &, bool *continuous) const
       
   102 {
       
   103     if (continuous)
       
   104         *continuous = false;
       
   105 
       
   106     return m_session->videoInput() ? m_session->videoInput()->supportedFrameRates() : QList<qreal>();
       
   107 }
       
   108 
       
   109 QStringList QGstreamerVideoEncode::supportedVideoCodecs() const
       
   110 {
       
   111     return m_codecs;
       
   112 }
       
   113 
       
   114 QString QGstreamerVideoEncode::videoCodecDescription(const QString &codecName) const
       
   115 {
       
   116     return m_codecDescriptions.value(codecName);
       
   117 }
       
   118 
       
   119 QStringList QGstreamerVideoEncode::supportedEncodingOptions(const QString &codec) const
       
   120 {
       
   121     return m_codecOptions.value(codec);
       
   122 }
       
   123 
       
   124 QVariant QGstreamerVideoEncode::encodingOption(const QString &codec, const QString &name) const
       
   125 {
       
   126     return m_options[codec].value(name);
       
   127 }
       
   128 
       
   129 void QGstreamerVideoEncode::setEncodingOption(
       
   130         const QString &codec, const QString &name, const QVariant &value)
       
   131 {
       
   132     m_options[codec][name] = value;
       
   133 }
       
   134 
       
   135 QVideoEncoderSettings QGstreamerVideoEncode::videoSettings() const
       
   136 {
       
   137     return m_videoSettings;
       
   138 }
       
   139 
       
   140 void QGstreamerVideoEncode::setVideoSettings(const QVideoEncoderSettings &settings)
       
   141 {
       
   142     m_videoSettings = settings;
       
   143 }
       
   144 
       
   145 GstElement *QGstreamerVideoEncode::createEncoder()
       
   146 {
       
   147     GstBin *encoderBin = GST_BIN(gst_bin_new("video-encoder-bin"));
       
   148     Q_ASSERT(encoderBin);
       
   149 
       
   150     GstElement *capsFilter = gst_element_factory_make("capsfilter", "capsfilter-video");
       
   151     gst_bin_add(encoderBin, capsFilter);
       
   152 
       
   153     GstElement *colorspace = gst_element_factory_make("ffmpegcolorspace", NULL);
       
   154     gst_bin_add(encoderBin, colorspace);
       
   155 
       
   156     QString codec = m_videoSettings.codec();
       
   157     //qDebug() << "create encoder for video codec" << codec;
       
   158 
       
   159     GstElement *encoderElement = gst_element_factory_make( m_elementNames.value(codec).constData(), "video-encoder");
       
   160     gst_bin_add(encoderBin, encoderElement);
       
   161 
       
   162     gst_element_link_many(capsFilter, colorspace, encoderElement, NULL);
       
   163 
       
   164     // add ghostpads
       
   165     GstPad *pad = gst_element_get_static_pad(capsFilter, "sink");
       
   166     gst_element_add_pad(GST_ELEMENT(encoderBin), gst_ghost_pad_new("sink", pad));
       
   167     gst_object_unref(GST_OBJECT(pad));
       
   168 
       
   169     pad = gst_element_get_static_pad(encoderElement, "src");
       
   170     gst_element_add_pad(GST_ELEMENT(encoderBin), gst_ghost_pad_new("src", pad));
       
   171     gst_object_unref(GST_OBJECT(pad));
       
   172 
       
   173     if (encoderElement) {
       
   174         if (m_videoSettings.encodingMode() == QtMultimediaKit::ConstantQualityEncoding) {
       
   175             QtMultimediaKit::EncodingQuality qualityValue = m_videoSettings.quality();
       
   176 
       
   177             if (codec == QLatin1String("video/h264")) {
       
   178                 //constant quantizer mode
       
   179                 g_object_set(G_OBJECT(encoderElement), "pass", 4, NULL);
       
   180                 int qualityTable[] = {
       
   181                     50, //VeryLow
       
   182                     35, //Low
       
   183                     21, //Normal
       
   184                     15, //High
       
   185                     8 //VeryHigh
       
   186                 };
       
   187                 g_object_set(G_OBJECT(encoderElement), "quantizer", qualityTable[qualityValue], NULL);
       
   188             } else if (codec == QLatin1String("video/xvid")) {
       
   189                 //constant quantizer mode
       
   190                 g_object_set(G_OBJECT(encoderElement), "pass", 3, NULL);
       
   191                 int qualityTable[] = {
       
   192                     32, //VeryLow
       
   193                     12, //Low
       
   194                     5, //Normal
       
   195                     3, //High
       
   196                     2 //VeryHigh
       
   197                 };
       
   198                 int quant = qualityTable[qualityValue];
       
   199                 g_object_set(G_OBJECT(encoderElement), "quantizer", quant, NULL);
       
   200             } else if (codec == QLatin1String("video/mpeg4") ||
       
   201                        codec == QLatin1String("video/mpeg1") ||
       
   202                        codec == QLatin1String("video/mpeg2") ) {
       
   203                 //constant quantizer mode
       
   204                 g_object_set(G_OBJECT(encoderElement), "pass", 2, NULL);
       
   205                 //quant from 1 to 30, default ~3
       
   206                 double qualityTable[] = {
       
   207                     20, //VeryLow
       
   208                     8.0, //Low
       
   209                     3.0, //Normal
       
   210                     2.5, //High
       
   211                     2.0 //VeryHigh
       
   212                 };
       
   213                 double quant = qualityTable[qualityValue];
       
   214                 g_object_set(G_OBJECT(encoderElement), "quantizer", quant, NULL);
       
   215             } else if (codec == QLatin1String("video/theora")) {
       
   216                 int qualityTable[] = {
       
   217                     8, //VeryLow
       
   218                     16, //Low
       
   219                     32, //Normal
       
   220                     45, //High
       
   221                     60 //VeryHigh
       
   222                 };
       
   223                 //quality from 0 to 63
       
   224                 int quality = qualityTable[qualityValue];
       
   225                 g_object_set(G_OBJECT(encoderElement), "quality", quality, NULL);
       
   226             }
       
   227         } else {
       
   228             int bitrate = m_videoSettings.bitRate();
       
   229             if (bitrate > 0) {
       
   230                 g_object_set(G_OBJECT(encoderElement), "bitrate", bitrate, NULL);
       
   231             }
       
   232         }
       
   233 
       
   234         QMap<QString,QVariant> options = m_options.value(codec);
       
   235         QMapIterator<QString,QVariant> it(options);
       
   236         while (it.hasNext()) {
       
   237             it.next();
       
   238             QString option = it.key();
       
   239             QVariant value = it.value();
       
   240 
       
   241             switch (value.type()) {
       
   242             case QVariant::Int:
       
   243                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toInt(), NULL);
       
   244                 break;
       
   245             case QVariant::Bool:
       
   246                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toBool(), NULL);
       
   247                 break;
       
   248             case QVariant::Double:
       
   249                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toDouble(), NULL);
       
   250                 break;
       
   251             case QVariant::String:
       
   252                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toString().toUtf8().constData(), NULL);
       
   253                 break;
       
   254             default:
       
   255                 qWarning() << "unsupported option type:" << option << value;
       
   256                 break;
       
   257             }
       
   258 
       
   259         }
       
   260     }
       
   261 
       
   262     if (!m_videoSettings.resolution().isEmpty() || m_videoSettings.frameRate() > 0.001) {
       
   263         GstCaps *caps = gst_caps_new_empty();
       
   264         QStringList structureTypes;
       
   265         structureTypes << "video/x-raw-yuv" << "video/x-raw-rgb";
       
   266 
       
   267         foreach(const QString &structureType, structureTypes) {
       
   268             GstStructure *structure = gst_structure_new(structureType.toAscii().constData(), NULL);
       
   269 
       
   270             if (!m_videoSettings.resolution().isEmpty()) {
       
   271                 gst_structure_set(structure, "width", G_TYPE_INT, m_videoSettings.resolution().width(), NULL);
       
   272                 gst_structure_set(structure, "height", G_TYPE_INT, m_videoSettings.resolution().height(), NULL);
       
   273             }
       
   274 
       
   275             if (m_videoSettings.frameRate() > 0.001) {
       
   276                 QPair<int,int> rate = rateAsRational();
       
   277 
       
   278                 //qDebug() << "frame rate:" << num << denum;
       
   279 
       
   280                 gst_structure_set(structure, "framerate", GST_TYPE_FRACTION, rate.first, rate.second, NULL);
       
   281             }
       
   282 
       
   283             gst_caps_append_structure(caps,structure);
       
   284         }
       
   285 
       
   286         //qDebug() << "set video caps filter:" << gst_caps_to_string(caps);
       
   287 
       
   288         g_object_set(G_OBJECT(capsFilter), "caps", caps, NULL);
       
   289     }
       
   290 
       
   291     return GST_ELEMENT(encoderBin);
       
   292 }
       
   293 
       
   294 QPair<int,int> QGstreamerVideoEncode::rateAsRational() const
       
   295 {
       
   296     qreal frameRate = m_videoSettings.frameRate();
       
   297 
       
   298     if (frameRate > 0.001) {
       
   299         //convert to rational number
       
   300         QList<int> denumCandidates;
       
   301         denumCandidates << 1 << 2 << 3 << 5 << 10 << 1001 << 1000;
       
   302 
       
   303         qreal error = 1.0;
       
   304         int num = 1;
       
   305         int denum = 1;
       
   306 
       
   307         foreach (int curDenum, denumCandidates) {
       
   308             int curNum = qRound(frameRate*curDenum);
       
   309             qreal curError = qAbs(qreal(curNum)/curDenum - frameRate);
       
   310 
       
   311             if (curError < error) {
       
   312                 error = curError;
       
   313                 num = curNum;
       
   314                 denum = curDenum;
       
   315             }
       
   316 
       
   317             if (curError < 1e-8)
       
   318                 break;
       
   319         }
       
   320 
       
   321         return QPair<int,int>(num,denum);
       
   322     }
       
   323 
       
   324     return QPair<int,int>();
       
   325 }
       
   326 
       
   327 
       
   328 QSet<QString> QGstreamerVideoEncode::supportedStreamTypes(const QString &codecName) const
       
   329 {
       
   330     return m_streamTypes.value(codecName);
       
   331 }