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 FFmpeg.
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.
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.
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
25 #include "bytestream.h"
31 #define BYTES_PER_PIXEL 1
32 #define RENAME(a) a ## _ ## 8
33 #include "v210_template.c"
36 #undef BYTES_PER_PIXEL
41 #define BYTES_PER_PIXEL 2
42 #define RENAME(a) a ## _ ## 10
43 #include "v210_template.c"
46 #undef BYTES_PER_PIXEL
49 static void v210_planar_pack_8_c(const uint8_t *y, const uint8_t *u,
50 const uint8_t *v, uint8_t *dst,
56 /* unroll this to match the assembly */
57 for (i = 0; i < width - 11; i += 12) {
58 WRITE_PIXELS(u, y, v, 8);
59 WRITE_PIXELS(y, u, y, 8);
60 WRITE_PIXELS(v, y, u, 8);
61 WRITE_PIXELS(y, v, y, 8);
62 WRITE_PIXELS(u, y, v, 8);
63 WRITE_PIXELS(y, u, y, 8);
64 WRITE_PIXELS(v, y, u, 8);
65 WRITE_PIXELS(y, v, y, 8);
69 static void v210_planar_pack_10_c(const uint16_t *y, const uint16_t *u,
70 const uint16_t *v, uint8_t *dst,
76 for (i = 0; i < width - 5; i += 6) {
77 WRITE_PIXELS(u, y, v, 10);
78 WRITE_PIXELS(y, u, y, 10);
79 WRITE_PIXELS(v, y, u, 10);
80 WRITE_PIXELS(y, v, y, 10);
84 av_cold void ff_v210enc_init(V210EncContext *s)
86 s->pack_line_8 = v210_planar_pack_8_c;
87 s->pack_line_10 = v210_planar_pack_10_c;
88 s->sample_factor_8 = 2;
89 s->sample_factor_10 = 1;
92 ff_v210enc_init_x86(s);
95 static av_cold int encode_init(AVCodecContext *avctx)
97 V210EncContext *s = avctx->priv_data;
99 if (avctx->width & 1) {
100 av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
101 return AVERROR(EINVAL);
104 #if FF_API_CODED_FRAME
105 FF_DISABLE_DEPRECATION_WARNINGS
106 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
107 FF_ENABLE_DEPRECATION_WARNINGS
112 avctx->bits_per_coded_sample = 20;
113 avctx->bit_rate = ff_guess_coded_bitrate(avctx) * 16 / 15;
118 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
119 const AVFrame *pic, int *got_packet)
121 int aligned_width = ((avctx->width + 47) / 48) * 48;
122 int stride = aligned_width * 8 / 3;
123 AVFrameSideData *side_data;
127 ret = ff_alloc_packet2(avctx, pkt, avctx->height * stride, avctx->height * stride);
129 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
134 if (pic->format == AV_PIX_FMT_YUV422P10)
135 v210_enc_10(avctx, dst, pic);
136 else if(pic->format == AV_PIX_FMT_YUV422P)
137 v210_enc_8(avctx, dst, pic);
139 side_data = av_frame_get_side_data(pic, AV_FRAME_DATA_A53_CC);
140 if (side_data && side_data->size) {
141 uint8_t *buf = av_packet_new_side_data(pkt, AV_PKT_DATA_A53_CC, side_data->size);
143 return AVERROR(ENOMEM);
144 memcpy(buf, side_data->data, side_data->size);
147 side_data = av_frame_get_side_data(pic, AV_FRAME_DATA_AFD);
148 if (side_data && side_data->size) {
149 uint8_t *buf = av_packet_new_side_data(pkt, AV_PKT_DATA_AFD, side_data->size);
151 return AVERROR(ENOMEM);
152 memcpy(buf, side_data->data, side_data->size);
155 pkt->flags |= AV_PKT_FLAG_KEY;
160 AVCodec ff_v210_encoder = {
162 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
163 .type = AVMEDIA_TYPE_VIDEO,
164 .id = AV_CODEC_ID_V210,
165 .priv_data_size = sizeof(V210EncContext),
167 .encode2 = encode_frame,
168 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE },