Libav
|
00001 /* 00002 * V210 encoder 00003 * 00004 * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at> 00005 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> 00006 * 00007 * This file is part of FFmpeg. 00008 * 00009 * FFmpeg is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * FFmpeg is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with FFmpeg; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00024 #include "avcodec.h" 00025 #include "libavcodec/bytestream.h" 00026 00027 static av_cold int encode_init(AVCodecContext *avctx) 00028 { 00029 if (avctx->width & 1) { 00030 av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n"); 00031 return -1; 00032 } 00033 00034 if (avctx->pix_fmt != PIX_FMT_YUV422P16) { 00035 av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P16\n"); 00036 return -1; 00037 } 00038 00039 if (avctx->bits_per_raw_sample != 10) 00040 av_log(avctx, AV_LOG_WARNING, "bits per raw sample: %d != 10-bit\n", 00041 avctx->bits_per_raw_sample); 00042 00043 avctx->coded_frame = avcodec_alloc_frame(); 00044 00045 avctx->coded_frame->key_frame = 1; 00046 avctx->coded_frame->pict_type = FF_I_TYPE; 00047 00048 return 0; 00049 } 00050 00051 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, 00052 int buf_size, void *data) 00053 { 00054 const AVFrame *pic = data; 00055 int aligned_width = ((avctx->width + 47) / 48) * 48; 00056 int stride = aligned_width * 8 / 3; 00057 int h, w; 00058 const uint16_t *y = (const uint16_t*)pic->data[0]; 00059 const uint16_t *u = (const uint16_t*)pic->data[1]; 00060 const uint16_t *v = (const uint16_t*)pic->data[2]; 00061 uint8_t *p = buf; 00062 uint8_t *pdst = buf; 00063 00064 if (buf_size < aligned_width * avctx->height * 8 / 3) { 00065 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n"); 00066 return -1; 00067 } 00068 00069 #define WRITE_PIXELS(a, b, c) \ 00070 do { \ 00071 val = (*a++ >> 6) | \ 00072 ((*b++ & 0xFFC0) << 4); \ 00073 val|= (*c++ & 0xFFC0) << 14; \ 00074 bytestream_put_le32(&p, val); \ 00075 } while (0) 00076 00077 for (h = 0; h < avctx->height; h++) { 00078 uint32_t val; 00079 for (w = 0; w < avctx->width - 5; w += 6) { 00080 WRITE_PIXELS(u, y, v); 00081 WRITE_PIXELS(y, u, y); 00082 WRITE_PIXELS(v, y, u); 00083 WRITE_PIXELS(y, v, y); 00084 } 00085 if (w < avctx->width - 1) { 00086 WRITE_PIXELS(u, y, v); 00087 00088 val = *y++ >> 6; 00089 if (w == avctx->width - 2) 00090 bytestream_put_le32(&p, val); 00091 } 00092 if (w < avctx->width - 3) { 00093 val |=((*u++ & 0xFFC0) << 4) | 00094 ((*y++ & 0xFFC0) << 14); 00095 bytestream_put_le32(&p, val); 00096 00097 val = (*v++ >> 6) | 00098 (*y++ & 0xFFC0) << 4; 00099 bytestream_put_le32(&p, val); 00100 } 00101 00102 pdst += stride; 00103 memset(p, 0, pdst - p); 00104 p = pdst; 00105 y += pic->linesize[0] / 2 - avctx->width; 00106 u += pic->linesize[1] / 2 - avctx->width / 2; 00107 v += pic->linesize[2] / 2 - avctx->width / 2; 00108 } 00109 00110 return p - buf; 00111 } 00112 00113 static av_cold int encode_close(AVCodecContext *avctx) 00114 { 00115 av_freep(&avctx->coded_frame); 00116 00117 return 0; 00118 } 00119 00120 AVCodec v210_encoder = { 00121 "v210", 00122 AVMEDIA_TYPE_VIDEO, 00123 CODEC_ID_V210, 00124 0, 00125 encode_init, 00126 encode_frame, 00127 encode_close, 00128 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P16, PIX_FMT_NONE}, 00129 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"), 00130 };