[FFmpeg-devel] [PATCH 2/2] libutvideo: Add Ut Video encoder wrapper

Derek Buitenhuis derek.buitenhuis at gmail.com
Sun Mar 4 01:54:00 CET 2012


All colorspaces are supported

Signed-off-by: Derek Buitenhuis <derek.buitenhuis at gmail.com>
---
 Changelog                    |    1 +
 MAINTAINERS                  |    1 +
 configure                    |    3 +-
 libavcodec/Makefile          |    3 +-
 libavcodec/allcodecs.c       |    2 +-
 libavcodec/libutvideo.cpp    |  199 -----------------------------------
 libavcodec/libutvideodec.cpp |  199 +++++++++++++++++++++++++++++++++++
 libavcodec/libutvideoenc.cpp |  233 ++++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.h         |    2 +-
 9 files changed, 440 insertions(+), 203 deletions(-)
 delete mode 100644 libavcodec/libutvideo.cpp
 create mode 100644 libavcodec/libutvideodec.cpp
 create mode 100644 libavcodec/libutvideoenc.cpp

diff --git a/Changelog b/Changelog
index 9679f83..9a4ee57 100644
--- a/Changelog
+++ b/Changelog
@@ -11,6 +11,7 @@ version next:
 - ID3v2 attached pictures reading and writing
 - WMA Lossless decoder
 - bluray protocol
+- libutvideo encoder wrapper (--enable-libutvideo)
 
 
 version 0.10:
diff --git a/MAINTAINERS b/MAINTAINERS
index 834686e..9441bd0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -171,6 +171,7 @@ Codecs:
   libschroedinger*                      David Conrad
   libspeexdec.c                         Justin Ruggles
   libtheoraenc.c                        David Conrad
+  libutvideo*                           Derek Buitenhuis
   libvorbis.c                           David Conrad
   libxavs.c                             Stefan Gehrer
   libx264.c                             Mans Rullgard, Jason Garrett-Glaser
diff --git a/configure b/configure
index b92609a..4325869 100755
--- a/configure
+++ b/configure
@@ -194,7 +194,7 @@ External library support:
   --enable-libspeex        enable Speex support via libspeex [no]
   --enable-libstagefright-h264  enable H.264 decoding via libstagefright [no]
   --enable-libtheora       enable Theora encoding via libtheora [no]
-  --enable-libutvideo      enable Ut Video decoding via libutvideo [no]
+  --enable-libutvideo      enable Ut Video encoding and decoding via libutvideo [no]
   --enable-libv4l2         enable libv4l2/v4l-utils [no]
   --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
   --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
@@ -1575,6 +1575,7 @@ libx264rgb_encoder_deps="libx264"
 libxavs_encoder_deps="libxavs"
 libxvid_encoder_deps="libxvid"
 libutvideo_decoder_deps="libutvideo gpl"
+libutvideo_encoder_derps="libutvideo gpl"
 
 # demuxers / muxers
 ac3_demuxer_select="ac3_parser"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 44dafb9..3e71c48 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -647,7 +647,8 @@ OBJS-$(CONFIG_LIBSPEEX_DECODER)           += libspeexdec.o
 OBJS-$(CONFIG_LIBSPEEX_ENCODER)           += libspeexenc.o
 OBJS-$(CONFIG_LIBSTAGEFRIGHT_H264_DECODER)+= libstagefright.o
 OBJS-$(CONFIG_LIBTHEORA_ENCODER)          += libtheoraenc.o
-OBJS-$(CONFIG_LIBUTVIDEO_DECODER)         += libutvideo.o
+OBJS-$(CONFIG_LIBUTVIDEO_DECODER)         += libutvideodec.o
+OBJS-$(CONFIG_LIBUTVIDEO_ENCODER)         += libutvideoenc.o
 OBJS-$(CONFIG_LIBVO_AACENC_ENCODER)       += libvo-aacenc.o mpeg4audio.o
 OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER)     += libvo-amrwbenc.o
 OBJS-$(CONFIG_LIBVORBIS_ENCODER)          += libvorbis.o vorbis_data.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 18121ba..200c1c6 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -412,7 +412,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC  (LIBSPEEX, libspeex);
     REGISTER_DECODER (LIBSTAGEFRIGHT_H264, libstagefright_h264);
     REGISTER_ENCODER (LIBTHEORA, libtheora);
-    REGISTER_DECODER (LIBUTVIDEO, libutvideo);
+    REGISTER_ENCDEC  (LIBUTVIDEO, libutvideo);
     REGISTER_ENCODER (LIBVO_AACENC, libvo_aacenc);
     REGISTER_ENCODER (LIBVO_AMRWBENC, libvo_amrwbenc);
     REGISTER_ENCODER (LIBVORBIS, libvorbis);
diff --git a/libavcodec/libutvideo.cpp b/libavcodec/libutvideo.cpp
deleted file mode 100644
index 443a77e..0000000
--- a/libavcodec/libutvideo.cpp
+++ /dev/null
@@ -1,199 +0,0 @@
-/*
- * Copyright (c) 2011 Derek Buitenhuis
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation;
- * version 2 of the License.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file
- * Known FOURCCs:
- *     'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
- */
-
-extern "C" {
-#include "avcodec.h"
-}
-
-#include "libutvideo.h"
-#include "get_bits.h"
-
-static av_cold int utvideo_decode_init(AVCodecContext *avctx)
-{
-    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
-    UtVideoExtra info;
-    int format;
-    int begin_ret;
-
-    if (avctx->extradata_size != 4*4) {
-        av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
-        return -1;
-    }
-
-    /* Read extradata */
-    info.version = AV_RL32(avctx->extradata);
-    info.original_format = AV_RL32(avctx->extradata + 4);
-    info.frameinfo_size = AV_RL32(avctx->extradata + 8);
-    info.flags = AV_RL32(avctx->extradata + 12);
-
-    /* Pick format based on FOURCC */
-    switch (avctx->codec_tag) {
-    case MKTAG('U', 'L', 'Y', '0'):
-        avctx->pix_fmt = PIX_FMT_YUV420P;
-        format = UTVF_YV12;
-        break;
-    case MKTAG('U', 'L', 'Y', '2'):
-        avctx->pix_fmt = PIX_FMT_YUYV422;
-        format = UTVF_YUY2;
-        break;
-    case MKTAG('U', 'L', 'R', 'G'):
-        avctx->pix_fmt = PIX_FMT_BGR24;
-        format = UTVF_RGB24_WIN;
-        break;
-    case MKTAG('U', 'L', 'R', 'A'):
-        avctx->pix_fmt = PIX_FMT_RGB32;
-        format = UTVF_RGB32_WIN;
-        break;
-    default:
-        av_log(avctx, AV_LOG_ERROR,
-              "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
-        return -1;
-    }
-
-    /* Only allocate the buffer once */
-    utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
-    utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
-
-    if (utv->buffer == NULL) {
-        av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
-        return -1;
-    }
-
-    /* Allocate the output frame */
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    /* Ut Video only supports 8-bit */
-    avctx->bits_per_raw_sample = 8;
-
-    /* Is it interlaced? */
-    avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
-
-    /* Apparently Ut Video doesn't store this info... */
-    avctx->coded_frame->top_field_first = 1;
-
-    /*
-     * Create a Ut Video instance. Since the function wants
-     * an "interface name" string, pass it the name of the lib.
-     */
-    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
-
-    /* Initialize Decoding */
-    begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
-                            CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
-
-    /* Check to see if the decoder initlized properly */
-    if (begin_ret != 0) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Could not initialize decoder: %d\n", begin_ret);
-        return -1;
-    }
-
-    return 0;
-}
-
-static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
-                                int *data_size, AVPacket *avpkt)
-{
-    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
-    AVFrame *pic = avctx->coded_frame;
-    int w = avctx->width, h = avctx->height;
-
-    /* Set flags */
-    pic->reference = 0;
-    pic->pict_type = AV_PICTURE_TYPE_I;
-    pic->key_frame = 1;
-
-    /* Decode the frame */
-    utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
-
-    /* Set the output data depending on the colorspace */
-    switch (avctx->pix_fmt) {
-    case PIX_FMT_YUV420P:
-        pic->linesize[0] = w;
-        pic->linesize[1] = pic->linesize[2] = w / 2;
-        pic->data[0] = utv->buffer;
-        pic->data[2] = utv->buffer + (w * h);
-        pic->data[1] = pic->data[2] + (w * h / 4);
-        break;
-    case PIX_FMT_YUYV422:
-        pic->linesize[0] = w * 2;
-        pic->data[0] = utv->buffer;
-        break;
-    case PIX_FMT_BGR24:
-    case PIX_FMT_RGB32:
-        /* Make the linesize negative, since Ut Video uses bottom-up BGR */
-        pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
-        pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
-        break;
-    }
-
-    *data_size = sizeof(AVFrame);
-    *(AVFrame *)data = *pic;
-
-    return avpkt->size;
-}
-
-static av_cold int utvideo_decode_close(AVCodecContext *avctx)
-{
-    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
-
-    /* Free output */
-    av_freep(&avctx->coded_frame);
-    av_freep(&utv->buffer);
-
-    /* Finish decoding and clean up the instance */
-    utv->codec->DecodeEnd();
-    CCodec::DeleteInstance(utv->codec);
-
-    return 0;
-}
-
-AVCodec ff_libutvideo_decoder = {
-    "libutvideo",
-    NULL_IF_CONFIG_SMALL("Ut Video"),
-    AVMEDIA_TYPE_VIDEO,
-    CODEC_ID_UTVIDEO,
-    0,    //capabilities
-    NULL, //supported_framerates
-    NULL, //pix_fmts
-    NULL, //supported_samplerates
-    NULL, //sample_fmts
-    NULL, //channel_layouts
-    0,    //max_lowres
-    NULL, //priv_class
-    NULL, //profiles
-    sizeof(UtVideoContext),
-    NULL, //next
-    NULL, //init_thread_copy
-    NULL, //update_thread_context
-    NULL, //defaults
-    NULL, //init_static_data
-    utvideo_decode_init,
-    NULL, //encode
-    NULL, //encode2
-    utvideo_decode_frame,
-    utvideo_decode_close,
-};
diff --git a/libavcodec/libutvideodec.cpp b/libavcodec/libutvideodec.cpp
new file mode 100644
index 0000000..443a77e
--- /dev/null
+++ b/libavcodec/libutvideodec.cpp
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2011 Derek Buitenhuis
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Known FOURCCs:
+ *     'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
+ */
+
+extern "C" {
+#include "avcodec.h"
+}
+
+#include "libutvideo.h"
+#include "get_bits.h"
+
+static av_cold int utvideo_decode_init(AVCodecContext *avctx)
+{
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
+    UtVideoExtra info;
+    int format;
+    int begin_ret;
+
+    if (avctx->extradata_size != 4*4) {
+        av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
+        return -1;
+    }
+
+    /* Read extradata */
+    info.version = AV_RL32(avctx->extradata);
+    info.original_format = AV_RL32(avctx->extradata + 4);
+    info.frameinfo_size = AV_RL32(avctx->extradata + 8);
+    info.flags = AV_RL32(avctx->extradata + 12);
+
+    /* Pick format based on FOURCC */
+    switch (avctx->codec_tag) {
+    case MKTAG('U', 'L', 'Y', '0'):
+        avctx->pix_fmt = PIX_FMT_YUV420P;
+        format = UTVF_YV12;
+        break;
+    case MKTAG('U', 'L', 'Y', '2'):
+        avctx->pix_fmt = PIX_FMT_YUYV422;
+        format = UTVF_YUY2;
+        break;
+    case MKTAG('U', 'L', 'R', 'G'):
+        avctx->pix_fmt = PIX_FMT_BGR24;
+        format = UTVF_RGB24_WIN;
+        break;
+    case MKTAG('U', 'L', 'R', 'A'):
+        avctx->pix_fmt = PIX_FMT_RGB32;
+        format = UTVF_RGB32_WIN;
+        break;
+    default:
+        av_log(avctx, AV_LOG_ERROR,
+              "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
+        return -1;
+    }
+
+    /* Only allocate the buffer once */
+    utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
+    utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
+
+    if (utv->buffer == NULL) {
+        av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
+        return -1;
+    }
+
+    /* Allocate the output frame */
+    avctx->coded_frame = avcodec_alloc_frame();
+
+    /* Ut Video only supports 8-bit */
+    avctx->bits_per_raw_sample = 8;
+
+    /* Is it interlaced? */
+    avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
+
+    /* Apparently Ut Video doesn't store this info... */
+    avctx->coded_frame->top_field_first = 1;
+
+    /*
+     * Create a Ut Video instance. Since the function wants
+     * an "interface name" string, pass it the name of the lib.
+     */
+    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
+
+    /* Initialize Decoding */
+    begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
+                            CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
+
+    /* Check to see if the decoder initlized properly */
+    if (begin_ret != 0) {
+        av_log(avctx, AV_LOG_ERROR,
+               "Could not initialize decoder: %d\n", begin_ret);
+        return -1;
+    }
+
+    return 0;
+}
+
+static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
+                                int *data_size, AVPacket *avpkt)
+{
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
+    AVFrame *pic = avctx->coded_frame;
+    int w = avctx->width, h = avctx->height;
+
+    /* Set flags */
+    pic->reference = 0;
+    pic->pict_type = AV_PICTURE_TYPE_I;
+    pic->key_frame = 1;
+
+    /* Decode the frame */
+    utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
+
+    /* Set the output data depending on the colorspace */
+    switch (avctx->pix_fmt) {
+    case PIX_FMT_YUV420P:
+        pic->linesize[0] = w;
+        pic->linesize[1] = pic->linesize[2] = w / 2;
+        pic->data[0] = utv->buffer;
+        pic->data[2] = utv->buffer + (w * h);
+        pic->data[1] = pic->data[2] + (w * h / 4);
+        break;
+    case PIX_FMT_YUYV422:
+        pic->linesize[0] = w * 2;
+        pic->data[0] = utv->buffer;
+        break;
+    case PIX_FMT_BGR24:
+    case PIX_FMT_RGB32:
+        /* Make the linesize negative, since Ut Video uses bottom-up BGR */
+        pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
+        pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
+        break;
+    }
+
+    *data_size = sizeof(AVFrame);
+    *(AVFrame *)data = *pic;
+
+    return avpkt->size;
+}
+
+static av_cold int utvideo_decode_close(AVCodecContext *avctx)
+{
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
+
+    /* Free output */
+    av_freep(&avctx->coded_frame);
+    av_freep(&utv->buffer);
+
+    /* Finish decoding and clean up the instance */
+    utv->codec->DecodeEnd();
+    CCodec::DeleteInstance(utv->codec);
+
+    return 0;
+}
+
+AVCodec ff_libutvideo_decoder = {
+    "libutvideo",
+    NULL_IF_CONFIG_SMALL("Ut Video"),
+    AVMEDIA_TYPE_VIDEO,
+    CODEC_ID_UTVIDEO,
+    0,    //capabilities
+    NULL, //supported_framerates
+    NULL, //pix_fmts
+    NULL, //supported_samplerates
+    NULL, //sample_fmts
+    NULL, //channel_layouts
+    0,    //max_lowres
+    NULL, //priv_class
+    NULL, //profiles
+    sizeof(UtVideoContext),
+    NULL, //next
+    NULL, //init_thread_copy
+    NULL, //update_thread_context
+    NULL, //defaults
+    NULL, //init_static_data
+    utvideo_decode_init,
+    NULL, //encode
+    NULL, //encode2
+    utvideo_decode_frame,
+    utvideo_decode_close,
+};
diff --git a/libavcodec/libutvideoenc.cpp b/libavcodec/libutvideoenc.cpp
new file mode 100644
index 0000000..40fddb6
--- /dev/null
+++ b/libavcodec/libutvideoenc.cpp
@@ -0,0 +1,233 @@
+/*
+ * Copyright (c) 2012 Derek Buitenhuis
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Known FOURCCs:
+ *     'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
+ */
+
+extern "C" {
+#include "avcodec.h"
+#include "internal.h"
+}
+
+#include "libutvideo.h"
+#include "put_bits.h"
+
+static av_cold int utvideo_encode_init(AVCodecContext *avctx)
+{
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
+    UtVideoExtra *info;
+    uint32_t flags, in_format;
+
+    switch (avctx->pix_fmt) {
+    case PIX_FMT_YUV420P:
+        in_format = UTVF_YV12;
+        avctx->bits_per_coded_sample = 12;
+        avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
+        break;
+    case PIX_FMT_YUYV422:
+        in_format = UTVF_YUYV;
+        avctx->bits_per_coded_sample = 16;
+        avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
+        break;
+    case PIX_FMT_BGR24:
+        in_format = UTVF_RGB24_WIN;
+        avctx->bits_per_coded_sample = 24;
+        avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
+        break;
+    case PIX_FMT_RGB32:
+        in_format = UTVF_RGB32_WIN;
+        avctx->bits_per_coded_sample = 32;
+        avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
+        break;
+    default:
+        return AVERROR(EINVAL);
+    }
+
+    /* Check before we alloc anything */
+    if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid precition method.\n");
+        return AVERROR(EINVAL);
+    }
+
+    flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
+
+    avctx->priv_data = utv;
+    avctx->coded_frame = avcodec_alloc_frame();
+
+    /* Alloc extradata buffer */
+    info = (UtVideoExtra *)av_malloc(sizeof(UtVideoExtra));
+
+    if (info == NULL) {
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    /*
+     * We use this buffer to hold teh data that Ut Video returns,
+     * since we cannot decode planes separately with it.
+     */
+    utv->buf_size = avpicture_get_size(avctx->pix_fmt,
+                                       avctx->width, avctx->height);
+    utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
+
+    if (utv->buffer == NULL) {
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    /*
+     * Create a UT Video instance. Since the function wants
+     * an "interface name" string, pass it the name of the lib.
+     */
+    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
+
+    /* Initialize encoder */
+    utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
+                            CBGROSSWIDTH_WINDOWS);
+
+    /* Get extradata from encoder */
+    avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
+    utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
+                                   avctx->width, avctx->height,
+                                   CBGROSSWIDTH_WINDOWS);
+    avctx->extradata = (uint8_t *)info;
+
+    /* Set flags */
+    utv->codec->SetState(&flags, sizeof(flags));
+
+    return 0;
+}
+
+static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
+                                const AVFrame *pic, int *got_packet)
+{
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
+    int w = avctx->width, h = avctx->height;
+    int ret, rgb_size, i;
+    bool keyframe;
+    uint8_t *y, *u, *v;
+    uint8_t *dst;
+
+    /* Alloc buffer */
+    if ((ret = ff_alloc_packet(pkt, utv->buf_size)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
+        return ret;
+    }
+
+    dst = pkt->data;
+
+    /* Move input if needed data into Ut Video friendly buffer */
+    switch (avctx->pix_fmt) {
+    case PIX_FMT_YUV420P:
+        y = utv->buffer;
+        u = y + w * h;
+        v = u + w * h / 4;
+        for (i = 0; i < h; i++) {
+            memcpy(y, pic->data[0] + i * pic->linesize[0], w);
+            y += w;
+        }
+        for (i = 0; i < h / 2; i++) {
+            memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
+            memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
+            u += w >> 1;
+            v += w >> 1;
+        }
+        break;
+    case PIX_FMT_YUYV422:
+        for (i = 0; i < h / 2; i++)
+            memcpy(utv->buffer + i * w,
+                   pic->data[0] + i * pic->linesize[0], w << 2);
+        break;
+    case PIX_FMT_BGR24:
+    case PIX_FMT_RGB32:
+        /* Ut Video takes bottom-up BGR */
+        rgb_size = avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4;
+        for (i = 0; i < h; i++)
+            memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
+                   pic->data[0] + i * pic->linesize[0],
+                   w * rgb_size);
+        break;
+    default:
+        return AVERROR(EINVAL);
+    }
+
+    /* Encode frame */
+    pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
+
+    if (!pkt->size) {
+        av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    avctx->coded_frame->key_frame = keyframe ? 1 : 0;
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
+
+    pkt->flags |= AV_PKT_FLAG_KEY;
+    *got_packet = 1;
+    return 0;
+}
+
+static av_cold int utvideo_encode_close(AVCodecContext *avctx)
+{
+    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
+
+    av_freep(&avctx->coded_frame);
+    av_freep(&avctx->extradata);
+    av_freep(&utv->buffer);
+
+    utv->codec->EncodeEnd();
+    CCodec::DeleteInstance(utv->codec);
+
+    return 0;
+}
+
+AVCodec ff_libutvideo_encoder = {
+    "libutvideo",
+    NULL_IF_CONFIG_SMALL("Ut Video"),
+    AVMEDIA_TYPE_VIDEO,
+    CODEC_ID_UTVIDEO,
+    CODEC_CAP_AUTO_THREADS,
+    NULL, //supported_framerates
+    (const enum PixelFormat[]) {
+        PIX_FMT_YUV420P, PIX_FMT_YUYV422, PIX_FMT_BGR24,
+        PIX_FMT_RGB32, PIX_FMT_NONE
+    },
+    NULL, //supported_samplerates
+    NULL, //sample_fmts
+    NULL, //channel_layouts
+    0,    //max_lowres
+    NULL, //priv_class
+    NULL, //profiles
+    sizeof(UtVideoContext),
+    NULL, //next
+    NULL, //init_thread_copy
+    NULL, //update_thread_context
+    NULL, //defaults
+    NULL, //init_static_data
+    utvideo_encode_init,
+    NULL, //encode
+    utvideo_encode_frame,
+    NULL, //decode
+    utvideo_encode_close,
+    NULL, //flush
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 1aa75ea..99691f2 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -21,7 +21,7 @@
 #define AVCODEC_VERSION_H
 
 #define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR  6
+#define LIBAVCODEC_VERSION_MINOR  7
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
-- 
1.7.9.1



More information about the ffmpeg-devel mailing list