| 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 "bytestream.h" |
| 26 | #include "internal.h" |
| 27 | #include "v210enc.h" |
| 28 | |
| 29 | #define CLIP(v) av_clip(v, 4, 1019) |
| 30 | #define CLIP8(v) av_clip(v, 1, 254) |
| 31 | |
| 32 | #define WRITE_PIXELS(a, b, c) \ |
| 33 | do { \ |
| 34 | val = CLIP(*a++); \ |
| 35 | val |= (CLIP(*b++) << 10) | \ |
| 36 | (CLIP(*c++) << 20); \ |
| 37 | AV_WL32(dst, val); \ |
| 38 | dst += 4; \ |
| 39 | } while (0) |
| 40 | |
| 41 | #define WRITE_PIXELS8(a, b, c) \ |
| 42 | do { \ |
| 43 | val = (CLIP8(*a++) << 2); \ |
| 44 | val |= (CLIP8(*b++) << 12) | \ |
| 45 | (CLIP8(*c++) << 22); \ |
| 46 | AV_WL32(dst, val); \ |
| 47 | dst += 4; \ |
| 48 | } while (0) |
| 49 | |
| 50 | static void v210_planar_pack_8_c(const uint8_t *y, const uint8_t *u, |
| 51 | const uint8_t *v, uint8_t *dst, ptrdiff_t width) |
| 52 | { |
| 53 | uint32_t val; |
| 54 | int i; |
| 55 | |
| 56 | /* unroll this to match the assembly */ |
| 57 | for( i = 0; i < width-11; i += 12 ){ |
| 58 | WRITE_PIXELS8(u, y, v); |
| 59 | WRITE_PIXELS8(y, u, y); |
| 60 | WRITE_PIXELS8(v, y, u); |
| 61 | WRITE_PIXELS8(y, v, y); |
| 62 | WRITE_PIXELS8(u, y, v); |
| 63 | WRITE_PIXELS8(y, u, y); |
| 64 | WRITE_PIXELS8(v, y, u); |
| 65 | WRITE_PIXELS8(y, v, y); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void v210_planar_pack_10_c(const uint16_t *y, const uint16_t *u, |
| 70 | const uint16_t *v, uint8_t *dst, ptrdiff_t width) |
| 71 | { |
| 72 | uint32_t val; |
| 73 | int i; |
| 74 | |
| 75 | for( i = 0; i < width-5; i += 6 ){ |
| 76 | WRITE_PIXELS(u, y, v); |
| 77 | WRITE_PIXELS(y, u, y); |
| 78 | WRITE_PIXELS(v, y, u); |
| 79 | WRITE_PIXELS(y, v, y); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static av_cold int encode_init(AVCodecContext *avctx) |
| 84 | { |
| 85 | V210EncContext *s = avctx->priv_data; |
| 86 | |
| 87 | if (avctx->width & 1) { |
| 88 | av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n"); |
| 89 | return AVERROR(EINVAL); |
| 90 | } |
| 91 | |
| 92 | avctx->coded_frame = av_frame_alloc(); |
| 93 | if (!avctx->coded_frame) |
| 94 | return AVERROR(ENOMEM); |
| 95 | |
| 96 | avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; |
| 97 | |
| 98 | s->pack_line_8 = v210_planar_pack_8_c; |
| 99 | s->pack_line_10 = v210_planar_pack_10_c; |
| 100 | |
| 101 | if (ARCH_X86) |
| 102 | ff_v210enc_init_x86(s); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
| 108 | const AVFrame *pic, int *got_packet) |
| 109 | { |
| 110 | V210EncContext *s = avctx->priv_data; |
| 111 | |
| 112 | int aligned_width = ((avctx->width + 47) / 48) * 48; |
| 113 | int stride = aligned_width * 8 / 3; |
| 114 | int line_padding = stride - ((avctx->width * 8 + 11) / 12) * 4; |
| 115 | int h, w, ret; |
| 116 | uint8_t *dst; |
| 117 | |
| 118 | if ((ret = ff_alloc_packet(pkt, avctx->height * stride)) < 0) { |
| 119 | av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n"); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | dst = pkt->data; |
| 124 | |
| 125 | if (pic->format == AV_PIX_FMT_YUV422P10) { |
| 126 | const uint16_t *y = (const uint16_t*)pic->data[0]; |
| 127 | const uint16_t *u = (const uint16_t*)pic->data[1]; |
| 128 | const uint16_t *v = (const uint16_t*)pic->data[2]; |
| 129 | for (h = 0; h < avctx->height; h++) { |
| 130 | uint32_t val; |
| 131 | w = (avctx->width / 6) * 6; |
| 132 | s->pack_line_10(y, u, v, dst, w); |
| 133 | |
| 134 | y += w; |
| 135 | u += w >> 1; |
| 136 | v += w >> 1; |
| 137 | dst += (w / 6) * 16; |
| 138 | if (w < avctx->width - 1) { |
| 139 | WRITE_PIXELS(u, y, v); |
| 140 | |
| 141 | val = CLIP(*y++); |
| 142 | if (w == avctx->width - 2) { |
| 143 | AV_WL32(dst, val); |
| 144 | dst += 4; |
| 145 | } |
| 146 | } |
| 147 | if (w < avctx->width - 3) { |
| 148 | val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20); |
| 149 | AV_WL32(dst, val); |
| 150 | dst += 4; |
| 151 | |
| 152 | val = CLIP(*v++) | (CLIP(*y++) << 10); |
| 153 | AV_WL32(dst, val); |
| 154 | dst += 4; |
| 155 | } |
| 156 | |
| 157 | memset(dst, 0, line_padding); |
| 158 | dst += line_padding; |
| 159 | |
| 160 | y += pic->linesize[0] / 2 - avctx->width; |
| 161 | u += pic->linesize[1] / 2 - avctx->width / 2; |
| 162 | v += pic->linesize[2] / 2 - avctx->width / 2; |
| 163 | } |
| 164 | } |
| 165 | else if(pic->format == AV_PIX_FMT_YUV422P) { |
| 166 | const uint8_t *y = pic->data[0]; |
| 167 | const uint8_t *u = pic->data[1]; |
| 168 | const uint8_t *v = pic->data[2]; |
| 169 | for (h = 0; h < avctx->height; h++) { |
| 170 | uint32_t val; |
| 171 | w = (avctx->width / 12) * 12; |
| 172 | s->pack_line_8(y, u, v, dst, w); |
| 173 | |
| 174 | y += w; |
| 175 | u += w >> 1; |
| 176 | v += w >> 1; |
| 177 | dst += (w / 12) * 32; |
| 178 | |
| 179 | for( ; w < avctx->width-5; w += 6 ){ |
| 180 | WRITE_PIXELS8(u, y, v); |
| 181 | WRITE_PIXELS8(y, u, y); |
| 182 | WRITE_PIXELS8(v, y, u); |
| 183 | WRITE_PIXELS8(y, v, y); |
| 184 | } |
| 185 | if (w < avctx->width - 1) { |
| 186 | WRITE_PIXELS8(u, y, v); |
| 187 | |
| 188 | val = CLIP8(*y++) << 2; |
| 189 | if (w == avctx->width - 2) { |
| 190 | AV_WL32(dst, val); |
| 191 | dst += 4; |
| 192 | } |
| 193 | } |
| 194 | if (w < avctx->width - 3) { |
| 195 | val |= (CLIP8(*u++) << 12) | (CLIP8(*y++) << 22); |
| 196 | AV_WL32(dst, val); |
| 197 | dst += 4; |
| 198 | |
| 199 | val = (CLIP8(*v++) << 2) | (CLIP8(*y++) << 12); |
| 200 | AV_WL32(dst, val); |
| 201 | dst += 4; |
| 202 | } |
| 203 | |
| 204 | memset(dst, 0, line_padding); |
| 205 | dst += line_padding; |
| 206 | |
| 207 | y += pic->linesize[0] - avctx->width; |
| 208 | u += pic->linesize[1] - avctx->width / 2; |
| 209 | v += pic->linesize[2] - avctx->width / 2; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | pkt->flags |= AV_PKT_FLAG_KEY; |
| 214 | *got_packet = 1; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static av_cold int encode_close(AVCodecContext *avctx) |
| 219 | { |
| 220 | av_freep(&avctx->coded_frame); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | AVCodec ff_v210_encoder = { |
| 226 | .name = "v210", |
| 227 | .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"), |
| 228 | .type = AVMEDIA_TYPE_VIDEO, |
| 229 | .id = AV_CODEC_ID_V210, |
| 230 | .priv_data_size = sizeof(V210EncContext), |
| 231 | .init = encode_init, |
| 232 | .encode2 = encode_frame, |
| 233 | .close = encode_close, |
| 234 | .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE }, |
| 235 | }; |