libutvideoenc.cpp 7.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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:
24 25
 *     'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA),
 *     'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 */

extern "C" {
#include "libavutil/avassert.h"
#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;
42
    int ret;
43 44

    switch (avctx->pix_fmt) {
45
    case AV_PIX_FMT_YUV420P:
46 47
        in_format = UTVF_YV12;
        avctx->bits_per_coded_sample = 12;
48 49 50 51
        if (avctx->colorspace == AVCOL_SPC_BT709)
            avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
        else
            avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
52
        break;
53
    case AV_PIX_FMT_YUYV422:
54 55
        in_format = UTVF_YUYV;
        avctx->bits_per_coded_sample = 16;
56 57 58 59
        if (avctx->colorspace == AVCOL_SPC_BT709)
            avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
        else
            avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
60
        break;
61
    case AV_PIX_FMT_BGR24:
62
        in_format = UTVF_NFCC_BGR_BU;
63 64 65
        avctx->bits_per_coded_sample = 24;
        avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
        break;
66
    case AV_PIX_FMT_RGB32:
67
        in_format = UTVF_NFCC_BGRA_BU;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        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 prediction method.\n");
        return AVERROR(EINVAL);
    }

    flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);

    avctx->priv_data = utv;
84
    avctx->coded_frame = av_frame_alloc();
85 86 87 88

    /* Alloc extradata buffer */
    info = (UtVideoExtra *)av_malloc(sizeof(*info));

89
    if (!info) {
90 91 92 93 94 95 96 97
        av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
        return AVERROR(ENOMEM);
    }

    /*
     * We use this buffer to hold the data that Ut Video returns,
     * since we cannot decode planes separately with it.
     */
98
    ret = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
99 100
    if (ret < 0) {
        av_free(info);
101
        return ret;
102
    }
103 104
    utv->buf_size = ret;

105 106 107 108
    utv->buffer = (uint8_t *)av_malloc(utv->buf_size);

    if (utv->buffer == NULL) {
        av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
109
        av_free(info);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        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 */
147
    if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
148 149 150 151 152 153
        return ret;

    dst = pkt->data;

    /* Move input if needed data into Ut Video friendly buffer */
    switch (avctx->pix_fmt) {
154
    case AV_PIX_FMT_YUV420P:
155 156 157 158 159 160 161 162 163 164 165 166 167 168
        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;
169
    case AV_PIX_FMT_YUYV422:
170 171 172 173
        for (i = 0; i < h; i++)
            memcpy(utv->buffer + i * (w << 1),
                   pic->data[0] + i * pic->linesize[0], w << 1);
        break;
174 175
    case AV_PIX_FMT_BGR24:
    case AV_PIX_FMT_RGB32:
176
        /* Ut Video takes bottom-up BGR */
177
        rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        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;
    }

    /*
     * Ut Video is intra-only and every frame is a keyframe,
     * and the API always returns true. In case something
     * durastic changes in the future, such as inter support,
     * assert that this is true.
     */
    av_assert2(keyframe == true);
    avctx->coded_frame->key_frame = 1;
    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;

214
    av_frame_free(&avctx->coded_frame);
215 216 217 218 219 220 221 222 223 224 225 226 227
    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,
228
    AV_CODEC_ID_UTVIDEO,
229 230
    CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
    NULL, /* supported_framerates */
231 232 233
    (const enum AVPixelFormat[]) {
        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
        AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    },
    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 */
};