]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/v210enc.c
8bbd720918cd9c8364bbba9dd7b27fbcec682def
[frescor/ffmpeg.git] / libavcodec / v210enc.c
1 /*
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     if (avctx->pix_fmt != PIX_FMT_YUV422P16) {
35         av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P16\n");
36         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     avctx->coded_frame->pict_type = FF_I_TYPE;
47
48     return 0;
49 }
50
51 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
52 {
53     const AVFrame *pic = data;
54     int aligned_width = ((avctx->width + 47) / 48) * 48;
55     int stride = aligned_width * 8 / 3;
56     int h, w;
57     const uint16_t *y = pic->data[0];
58     const uint16_t *u = pic->data[1];
59     const uint16_t *v = pic->data[2];
60     uint8_t *p = buf;
61     uint8_t *pdst = buf;
62
63     if (buf_size < aligned_width * avctx->height * 8 / 3) {
64         av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
65         return -1;
66     }
67
68 #define WRITE_PIXELS(a, b, c)           \
69     do {                                \
70         val =  (*a++           >>  6) | \
71               ((*b++ & 0xFFC0) <<  4);  \
72         val|=  (*c++ & 0xFFC0) << 14;   \
73         bytestream_put_le32(&p, val);   \
74     } while (0);
75
76     for (h = 0; h < avctx->height; h++) {
77         uint32_t val;
78         for (w = 0; w < avctx->width - 5; w += 6) {
79             WRITE_PIXELS(u, y, v);
80             WRITE_PIXELS(y, u, y);
81             WRITE_PIXELS(v, y, u);
82             WRITE_PIXELS(y, v, y);
83         }
84         if (w < avctx->width - 1) {
85             WRITE_PIXELS(u, y, v);
86
87             val =   *y++           >>  6;
88             if (w == avctx->width - 2)
89                 bytestream_put_le32(&p, val);
90         }
91         if (w < avctx->width - 3) {
92             val |=((*u++ & 0xFFC0) <<  4) |
93                   ((*y++ & 0xFFC0) << 14);
94             bytestream_put_le32(&p, val);
95
96             val =  (*v++           >>  6) |
97                    (*y++ & 0xFFC0) <<  4;
98             bytestream_put_le32(&p, val);
99         }
100
101         pdst += stride;
102         memset(p, 0, pdst - p);
103         p = pdst;
104         y += pic->linesize[0]/2 - avctx->width;
105         u += pic->linesize[1]/2 - avctx->width/2;
106         v += pic->linesize[2]/2 - avctx->width/2;
107     }
108
109     return p - buf;
110 }
111
112 static av_cold int encode_close(AVCodecContext *avctx)
113 {
114     av_freep(&avctx->coded_frame);
115
116     return 0;
117 }
118
119 AVCodec v210_encoder = {
120     "v210",
121     CODEC_TYPE_VIDEO,
122     CODEC_ID_V210,
123     0,
124     encode_init,
125     encode_frame,
126     encode_close,
127     .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV422P16, -1},
128     .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
129 };