Statistics
| Branch: | Revision:

ffmpeg / libavcodec / v210enc.c @ 74bf9d62

History | View | Annotate | Download (3.83 KB)

1 ca0bb1c4 Baptiste Coudurier
/*
2
 * V210 encoder
3
 *
4
 * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
5
 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
#include "avcodec.h"
25
#include "libavcodec/bytestream.h"
26
27
static av_cold int encode_init(AVCodecContext *avctx)
28
{
29
    if (avctx->width & 1) {
30
        av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
31
        return -1;
32
    }
33
34 3e002747 Baptiste Coudurier
    if (avctx->pix_fmt != PIX_FMT_YUV422P10) {
35
        av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P10\n");
36 ca0bb1c4 Baptiste Coudurier
        return -1;
37
    }
38
39
    if (avctx->bits_per_raw_sample != 10)
40
        av_log(avctx, AV_LOG_WARNING, "bits per raw sample: %d != 10-bit\n",
41
               avctx->bits_per_raw_sample);
42
43
    avctx->coded_frame = avcodec_alloc_frame();
44
45
    avctx->coded_frame->key_frame = 1;
46 ce5e49b0 Stefano Sabatini
    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
47 ca0bb1c4 Baptiste Coudurier
48
    return 0;
49
}
50
51 d87d5025 Diego Biurrun
static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
52
                        int buf_size, void *data)
53 ca0bb1c4 Baptiste Coudurier
{
54
    const AVFrame *pic = data;
55
    int aligned_width = ((avctx->width + 47) / 48) * 48;
56
    int stride = aligned_width * 8 / 3;
57
    int h, w;
58 520ca503 Baptiste Coudurier
    const uint16_t *y = (const uint16_t*)pic->data[0];
59
    const uint16_t *u = (const uint16_t*)pic->data[1];
60
    const uint16_t *v = (const uint16_t*)pic->data[2];
61 ca0bb1c4 Baptiste Coudurier
    uint8_t *p = buf;
62
    uint8_t *pdst = buf;
63
64
    if (buf_size < aligned_width * avctx->height * 8 / 3) {
65
        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
66
        return -1;
67
    }
68
69 74bf9d62 Baptiste Coudurier
#define CLIP(v) av_clip(v, 4, 1019)
70
71 ca0bb1c4 Baptiste Coudurier
#define WRITE_PIXELS(a, b, c)           \
72
    do {                                \
73 74bf9d62 Baptiste Coudurier
        val =   CLIP(*a++);             \
74
        val |= (CLIP(*b++) << 10) |     \
75
               (CLIP(*c++) << 20);      \
76 ca0bb1c4 Baptiste Coudurier
        bytestream_put_le32(&p, val);   \
77 e20b196d Ramiro Polla
    } while (0)
78 ca0bb1c4 Baptiste Coudurier
79
    for (h = 0; h < avctx->height; h++) {
80
        uint32_t val;
81
        for (w = 0; w < avctx->width - 5; w += 6) {
82
            WRITE_PIXELS(u, y, v);
83
            WRITE_PIXELS(y, u, y);
84
            WRITE_PIXELS(v, y, u);
85
            WRITE_PIXELS(y, v, y);
86
        }
87
        if (w < avctx->width - 1) {
88
            WRITE_PIXELS(u, y, v);
89
90 74bf9d62 Baptiste Coudurier
            val = CLIP(*y++);
91 ca0bb1c4 Baptiste Coudurier
            if (w == avctx->width - 2)
92
                bytestream_put_le32(&p, val);
93
        }
94
        if (w < avctx->width - 3) {
95 74bf9d62 Baptiste Coudurier
            val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
96 ca0bb1c4 Baptiste Coudurier
            bytestream_put_le32(&p, val);
97
98 74bf9d62 Baptiste Coudurier
            val = CLIP(*v++) | (CLIP(*y++) << 10);
99 ca0bb1c4 Baptiste Coudurier
            bytestream_put_le32(&p, val);
100
        }
101
102
        pdst += stride;
103
        memset(p, 0, pdst - p);
104
        p = pdst;
105 d87d5025 Diego Biurrun
        y += pic->linesize[0] / 2 - avctx->width;
106
        u += pic->linesize[1] / 2 - avctx->width / 2;
107
        v += pic->linesize[2] / 2 - avctx->width / 2;
108 ca0bb1c4 Baptiste Coudurier
    }
109
110
    return p - buf;
111
}
112
113
static av_cold int encode_close(AVCodecContext *avctx)
114
{
115
    av_freep(&avctx->coded_frame);
116
117
    return 0;
118
}
119
120 e7e2df27 Diego Elio Pettenò
AVCodec ff_v210_encoder = {
121 ca0bb1c4 Baptiste Coudurier
    "v210",
122 72415b2a Stefano Sabatini
    AVMEDIA_TYPE_VIDEO,
123 ca0bb1c4 Baptiste Coudurier
    CODEC_ID_V210,
124
    0,
125
    encode_init,
126
    encode_frame,
127
    encode_close,
128 3e002747 Baptiste Coudurier
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P10, PIX_FMT_NONE},
129 ca0bb1c4 Baptiste Coudurier
    .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
130
};