v210enc: use FFALIGN()
[libav.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 Libav.
8 *
9 * Libav 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 * Libav 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 Libav; 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 "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 AVERROR(EINVAL);
32 }
33
34 if (avctx->pix_fmt != PIX_FMT_YUV422P10) {
35 av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P10\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 if (!avctx->coded_frame)
45 return AVERROR(ENOMEM);
46
47 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
48
49 return 0;
50 }
51
52 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
53 int buf_size, void *data)
54 {
55 const AVFrame *pic = data;
56 int aligned_width = FFALIGN(avctx->width, 48);
57 int stride = aligned_width * 8 / 3;
58 int h, w;
59 const uint16_t *y = (const uint16_t*)pic->data[0];
60 const uint16_t *u = (const uint16_t*)pic->data[1];
61 const uint16_t *v = (const uint16_t*)pic->data[2];
62 uint8_t *p = buf;
63 uint8_t *pdst = buf;
64
65 if (buf_size < aligned_width * avctx->height * 8 / 3) {
66 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
67 return AVERROR(ENOMEM);
68 }
69
70 #define CLIP(v) av_clip(v, 4, 1019)
71
72 #define WRITE_PIXELS(a, b, c) \
73 do { \
74 val = CLIP(*a++); \
75 val |= (CLIP(*b++) << 10) | \
76 (CLIP(*c++) << 20); \
77 bytestream_put_le32(&p, val); \
78 } while (0)
79
80 for (h = 0; h < avctx->height; h++) {
81 uint32_t val;
82 for (w = 0; w < avctx->width - 5; w += 6) {
83 WRITE_PIXELS(u, y, v);
84 WRITE_PIXELS(y, u, y);
85 WRITE_PIXELS(v, y, u);
86 WRITE_PIXELS(y, v, y);
87 }
88 if (w < avctx->width - 1) {
89 WRITE_PIXELS(u, y, v);
90
91 val = CLIP(*y++);
92 if (w == avctx->width - 2)
93 bytestream_put_le32(&p, val);
94 }
95 if (w < avctx->width - 3) {
96 val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
97 bytestream_put_le32(&p, val);
98
99 val = CLIP(*v++) | (CLIP(*y++) << 10);
100 bytestream_put_le32(&p, val);
101 }
102
103 pdst += stride;
104 memset(p, 0, pdst - p);
105 p = pdst;
106 y += pic->linesize[0] / 2 - avctx->width;
107 u += pic->linesize[1] / 2 - avctx->width / 2;
108 v += pic->linesize[2] / 2 - avctx->width / 2;
109 }
110
111 return p - buf;
112 }
113
114 static av_cold int encode_close(AVCodecContext *avctx)
115 {
116 av_freep(&avctx->coded_frame);
117
118 return 0;
119 }
120
121 AVCodec ff_v210_encoder = {
122 .name = "v210",
123 .type = AVMEDIA_TYPE_VIDEO,
124 .id = CODEC_ID_V210,
125 .init = encode_init,
126 .encode = encode_frame,
127 .close = encode_close,
128 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P10, PIX_FMT_NONE},
129 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
130 };