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

Clément Bœsch ubitux at gmail.com
Sun Mar 4 02:26:22 CET 2012


On Sat, Mar 03, 2012 at 07:54:00PM -0500, Derek Buitenhuis wrote:
> 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 +++++++++++++++++++++++++++++++++++

Please use the -M option for this, and mention it in the description.

>  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"

herp derp

[...]
> 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");

precision*

> +        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));
> +

info = av_malloc(sizeof(*info));

No need to av_mallocz?

> +    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,

the*

I believe this ony is detected by patchcheck.

> +     * 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));

ditto, also sizeof(uint8_t) is and will always be 1

> +
> +    if (utv->buffer == NULL) {

some people prefer if (!utv->buffer)

> +        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;

Is it intra only? If so, I wonder if you shouldn't update is_intra_only()
in libavformat/utils.c.

[...]

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120304/11f23ea3/attachment.asc>


More information about the ffmpeg-devel mailing list