libavcodec/v210x.c
97f92143
 /*
  * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "avcodec.h"
594d4d5d
 #include "internal.h"
97f92143
 #include "libavutil/bswap.h"
1d9c2dc8
 #include "libavutil/internal.h"
 #include "libavutil/mem.h"
97f92143
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     if(avctx->width & 1){
         av_log(avctx, AV_LOG_ERROR, "v210x needs even width\n");
         return -1;
     }
716d413c
     avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
97f92143
     avctx->bits_per_raw_sample= 10;
 
     avctx->coded_frame= avcodec_alloc_frame();
f122cbf0
     if (!avctx->coded_frame)
         return AVERROR(ENOMEM);
97f92143
 
     return 0;
 }
 
df9b9567
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                         AVPacket *avpkt)
97f92143
 {
     int y=0;
     int width= avctx->width;
     AVFrame *pic= avctx->coded_frame;
b157e3e5
     const uint32_t *src= (const uint32_t *)avpkt->data;
97f92143
     uint16_t *ydst, *udst, *vdst, *yend;
 
     if(pic->data[0])
         avctx->release_buffer(avctx, pic);
 
     if(avpkt->size < avctx->width * avctx->height * 8 / 3){
         av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
         return -1;
     }
 
     if(avpkt->size > avctx->width * avctx->height * 8 / 3){
70fb031c
         av_log_ask_for_sample(avctx, "Probably padded data\n");
97f92143
     }
 
     pic->reference= 0;
594d4d5d
     if(ff_get_buffer(avctx, pic) < 0)
97f92143
         return -1;
 
b157e3e5
     ydst= (uint16_t *)pic->data[0];
     udst= (uint16_t *)pic->data[1];
     vdst= (uint16_t *)pic->data[2];
97f92143
     yend= ydst + width;
ce5e49b0
     pic->pict_type= AV_PICTURE_TYPE_I;
97f92143
     pic->key_frame= 1;
 
     for(;;){
8fc0162a
         uint32_t v= av_be2ne32(*src++);
97f92143
         *udst++= (v>>16) & 0xFFC0;
         *ydst++= (v>>6 ) & 0xFFC0;
         *vdst++= (v<<4 ) & 0xFFC0;
 
8fc0162a
         v= av_be2ne32(*src++);
97f92143
         *ydst++= (v>>16) & 0xFFC0;
 
         if(ydst >= yend){
             ydst+= pic->linesize[0]/2 - width;
             udst+= pic->linesize[1]/2 - width/2;
             vdst+= pic->linesize[2]/2 - width/2;
             yend= ydst + width;
             if(++y >= avctx->height)
                 break;
         }
 
         *udst++= (v>>6 ) & 0xFFC0;
         *ydst++= (v<<4 ) & 0xFFC0;
 
8fc0162a
         v= av_be2ne32(*src++);
97f92143
         *vdst++= (v>>16) & 0xFFC0;
         *ydst++= (v>>6 ) & 0xFFC0;
 
         if(ydst >= yend){
             ydst+= pic->linesize[0]/2 - width;
             udst+= pic->linesize[1]/2 - width/2;
             vdst+= pic->linesize[2]/2 - width/2;
             yend= ydst + width;
             if(++y >= avctx->height)
                 break;
         }
 
         *udst++= (v<<4 ) & 0xFFC0;
 
8fc0162a
         v= av_be2ne32(*src++);
97f92143
         *ydst++= (v>>16) & 0xFFC0;
         *vdst++= (v>>6 ) & 0xFFC0;
         *ydst++= (v<<4 ) & 0xFFC0;
         if(ydst >= yend){
             ydst+= pic->linesize[0]/2 - width;
             udst+= pic->linesize[1]/2 - width/2;
             vdst+= pic->linesize[2]/2 - width/2;
             yend= ydst + width;
             if(++y >= avctx->height)
                 break;
         }
     }
 
df9b9567
     *got_frame = 1;
97f92143
     *(AVFrame*)data= *avctx->coded_frame;
 
     return avpkt->size;
 }
 
0fbb0185
 static av_cold int decode_close(AVCodecContext *avctx)
 {
     AVFrame *pic = avctx->coded_frame;
     if (pic->data[0])
         avctx->release_buffer(avctx, pic);
     av_freep(&avctx->coded_frame);
 
     return 0;
 }
 
e7e2df27
 AVCodec ff_v210x_decoder = {
ec6402b7
     .name           = "v210x",
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_V210X,
ec6402b7
     .init           = decode_init,
     .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
97f92143
 };