4 * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
7 * This file is part of Libav.
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.
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.
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
25 #include "bytestream.h"
29 #define CLIP(v) av_clip(v, 4, 1019)
30 #define CLIP8(v) av_clip(v, 1, 254)
32 #define WRITE_PIXELS(a, b, c) \
35 val |= (CLIP(*b++) << 10) | \
41 #define WRITE_PIXELS8(a, b, c) \
43 val = (CLIP8(*a++) << 2); \
44 val |= (CLIP8(*b++) << 12) | \
45 (CLIP8(*c++) << 22); \
50 static void v210_planar_pack_8_c(const uint8_t *y, const uint8_t *u,
51 const uint8_t *v, uint8_t *dst,
57 /* unroll this to match the assembly */
58 for (i = 0; i < width - 11; i += 12) {
59 WRITE_PIXELS8(u, y, v);
60 WRITE_PIXELS8(y, u, y);
61 WRITE_PIXELS8(v, y, u);
62 WRITE_PIXELS8(y, v, y);
63 WRITE_PIXELS8(u, y, v);
64 WRITE_PIXELS8(y, u, y);
65 WRITE_PIXELS8(v, y, u);
66 WRITE_PIXELS8(y, v, y);
70 static void v210_planar_pack_10_c(const uint16_t *y, const uint16_t *u,
71 const uint16_t *v, uint8_t *dst,
77 for (i = 0; i < width - 5; i += 6) {
78 WRITE_PIXELS(u, y, v);
79 WRITE_PIXELS(y, u, y);
80 WRITE_PIXELS(v, y, u);
81 WRITE_PIXELS(y, v, y);
85 static av_cold int encode_init(AVCodecContext *avctx)
87 V210EncContext *s = avctx->priv_data;
89 if (avctx->width & 1) {
90 av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
91 return AVERROR(EINVAL);
94 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
96 s->pack_line_8 = v210_planar_pack_8_c;
97 s->pack_line_10 = v210_planar_pack_10_c;
100 ff_v210enc_init_x86(s);
105 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
106 const AVFrame *pic, int *got_packet)
108 V210EncContext *s = avctx->priv_data;
109 int aligned_width = ((avctx->width + 47) / 48) * 48;
110 int stride = aligned_width * 8 / 3;
111 int line_padding = stride - ((avctx->width * 8 + 11) / 12) * 4;
115 ret = ff_alloc_packet(pkt, avctx->height * stride);
117 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
122 if (pic->format == AV_PIX_FMT_YUV422P10) {
123 const uint16_t *y = (const uint16_t *)pic->data[0];
124 const uint16_t *u = (const uint16_t *)pic->data[1];
125 const uint16_t *v = (const uint16_t *)pic->data[2];
126 for (h = 0; h < avctx->height; h++) {
128 w = (avctx->width / 6) * 6;
129 s->pack_line_10(y, u, v, dst, w);
135 if (w < avctx->width - 1) {
136 WRITE_PIXELS(u, y, v);
139 if (w == avctx->width - 2) {
144 if (w < avctx->width - 3) {
145 val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
149 val = CLIP(*v++) | (CLIP(*y++) << 10);
154 memset(dst, 0, line_padding);
156 y += pic->linesize[0] / 2 - avctx->width;
157 u += pic->linesize[1] / 2 - avctx->width / 2;
158 v += pic->linesize[2] / 2 - avctx->width / 2;
160 } else if(pic->format == AV_PIX_FMT_YUV422P) {
161 const uint8_t *y = pic->data[0];
162 const uint8_t *u = pic->data[1];
163 const uint8_t *v = pic->data[2];
164 for (h = 0; h < avctx->height; h++) {
166 w = (avctx->width / 12) * 12;
167 s->pack_line_8(y, u, v, dst, w);
172 dst += (w / 12) * 32;
174 for (; w < avctx->width - 5; w += 6) {
175 WRITE_PIXELS8(u, y, v);
176 WRITE_PIXELS8(y, u, y);
177 WRITE_PIXELS8(v, y, u);
178 WRITE_PIXELS8(y, v, y);
180 if (w < avctx->width - 1) {
181 WRITE_PIXELS8(u, y, v);
183 val = CLIP8(*y++) << 2;
184 if (w == avctx->width - 2) {
189 if (w < avctx->width - 3) {
190 val |= (CLIP8(*u++) << 12) | (CLIP8(*y++) << 22);
194 val = (CLIP8(*v++) << 2) | (CLIP8(*y++) << 12);
198 memset(dst, 0, line_padding);
201 y += pic->linesize[0] - avctx->width;
202 u += pic->linesize[1] - avctx->width / 2;
203 v += pic->linesize[2] - avctx->width / 2;
207 pkt->flags |= AV_PKT_FLAG_KEY;
212 AVCodec ff_v210_encoder = {
214 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
215 .type = AVMEDIA_TYPE_VIDEO,
216 .id = AV_CODEC_ID_V210,
217 .priv_data_size = sizeof(V210EncContext),
219 .encode2 = encode_frame,
220 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE },