• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/v210dec.c

Go to the documentation of this file.
00001 /*
00002  * V210 decoder
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 "v210dec.h"
00026 #include "libavutil/bswap.h"
00027 
00028 #define READ_PIXELS(a, b, c)         \
00029     do {                             \
00030         val  = av_le2ne32(*src++);   \
00031         *a++ =  val & 0x3FF;         \
00032         *b++ = (val >> 10) & 0x3FF;  \
00033         *c++ = (val >> 20) & 0x3FF;  \
00034     } while (0)
00035 
00036 static void v210_planar_unpack_c(const uint32_t *src, uint16_t *y, uint16_t *u, uint16_t *v, int width)
00037 {
00038     uint32_t val;
00039     int i;
00040 
00041     for( i = 0; i < width-5; i += 6 ){
00042         READ_PIXELS(u, y, v);
00043         READ_PIXELS(y, u, y);
00044         READ_PIXELS(v, y, u);
00045         READ_PIXELS(y, v, y);
00046     }
00047 }
00048 
00049 static av_cold int decode_init(AVCodecContext *avctx)
00050 {
00051     V210DecContext *s = avctx->priv_data;
00052 
00053     if (avctx->width & 1) {
00054         av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
00055         return -1;
00056     }
00057     avctx->pix_fmt             = PIX_FMT_YUV422P10;
00058     avctx->bits_per_raw_sample = 10;
00059 
00060     avctx->coded_frame         = avcodec_alloc_frame();
00061 
00062     s->unpack_frame            = v210_planar_unpack_c;
00063 
00064     if (HAVE_MMX)
00065         v210_x86_init(s);
00066 
00067     return 0;
00068 }
00069 
00070 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00071                         AVPacket *avpkt)
00072 {
00073     V210DecContext *s = avctx->priv_data;
00074 
00075     int h, w, stride, aligned_input;
00076     AVFrame *pic = avctx->coded_frame;
00077     const uint8_t *psrc = avpkt->data;
00078     uint16_t *y, *u, *v;
00079 
00080     if (s->custom_stride )
00081         stride = s->custom_stride;
00082     else {
00083         int aligned_width = ((avctx->width + 47) / 48) * 48;
00084         stride = aligned_width * 8 / 3;
00085     }
00086 
00087     aligned_input = !((uintptr_t)psrc & 0xf) && !(stride & 0xf);
00088     if (aligned_input != s->aligned_input) {
00089         s->aligned_input = aligned_input;
00090         if (HAVE_MMX)
00091             v210_x86_init(s);
00092     }
00093 
00094     if (pic->data[0])
00095         avctx->release_buffer(avctx, pic);
00096 
00097     if (avpkt->size < stride * avctx->height) {
00098         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
00099         return -1;
00100     }
00101 
00102     pic->reference = 0;
00103     if (avctx->get_buffer(avctx, pic) < 0)
00104         return -1;
00105 
00106     y = (uint16_t*)pic->data[0];
00107     u = (uint16_t*)pic->data[1];
00108     v = (uint16_t*)pic->data[2];
00109     pic->pict_type = AV_PICTURE_TYPE_I;
00110     pic->key_frame = 1;
00111 
00112     for (h = 0; h < avctx->height; h++) {
00113         const uint32_t *src = (const uint32_t*)psrc;
00114         uint32_t val;
00115 
00116         w = (avctx->width / 6) * 6;
00117         s->unpack_frame(src, y, u, v, w);
00118 
00119         y += w;
00120         u += w >> 1;
00121         v += w >> 1;
00122         src += (w << 1) / 3;
00123 
00124         if (w < avctx->width - 1) {
00125             READ_PIXELS(u, y, v);
00126 
00127             val  = av_le2ne32(*src++);
00128             *y++ =  val & 0x3FF;
00129             if (w < avctx->width - 3) {
00130                 *u++ = (val >> 10) & 0x3FF;
00131                 *y++ = (val >> 20) & 0x3FF;
00132 
00133                 val  = av_le2ne32(*src++);
00134                 *v++ =  val & 0x3FF;
00135                 *y++ = (val >> 10) & 0x3FF;
00136             }
00137         }
00138 
00139         psrc += stride;
00140         y += pic->linesize[0] / 2 - avctx->width;
00141         u += pic->linesize[1] / 2 - avctx->width / 2;
00142         v += pic->linesize[2] / 2 - avctx->width / 2;
00143     }
00144 
00145     *data_size = sizeof(AVFrame);
00146     *(AVFrame*)data = *avctx->coded_frame;
00147 
00148     return avpkt->size;
00149 }
00150 
00151 static av_cold int decode_close(AVCodecContext *avctx)
00152 {
00153     AVFrame *pic = avctx->coded_frame;
00154     if (pic->data[0])
00155         avctx->release_buffer(avctx, pic);
00156     av_freep(&avctx->coded_frame);
00157 
00158     return 0;
00159 }
00160 
00161 #define V210DEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
00162 static const AVOption v210dec_options[] = {
00163     {"custom_stride", "Custom V210 stride", offsetof(V210DecContext, custom_stride), FF_OPT_TYPE_INT,
00164      {.dbl = 0}, INT_MIN, INT_MAX, V210DEC_FLAGS},
00165     {NULL}
00166 };
00167 
00168 static const AVClass v210dec_class = {
00169     "V210 Decoder",
00170     av_default_item_name,
00171     v210dec_options,
00172     LIBAVUTIL_VERSION_INT,
00173 };
00174 
00175 AVCodec ff_v210_decoder = {
00176     .name           = "v210",
00177     .type           = AVMEDIA_TYPE_VIDEO,
00178     .id             = CODEC_ID_V210,
00179     .priv_data_size = sizeof(V210DecContext),
00180     .init           = decode_init,
00181     .close          = decode_close,
00182     .decode         = decode_frame,
00183     .capabilities   = CODEC_CAP_DR1,
00184     .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
00185     .priv_class     = &v210dec_class,
00186 };
Generated on Fri Feb 1 2013 14:34:45 for FFmpeg by doxygen 1.7.1