]> git.sesse.net Git - ffmpeg/blob - libavcodec/v210x.c
1435249e04cb6461e94c12f9dba20a04d14c45f8
[ffmpeg] / libavcodec / v210x.c
1 /*
2  * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "avcodec.h"
22 #include "libavutil/bswap.h"
23
24 static av_cold int decode_init(AVCodecContext *avctx)
25 {
26     if(avctx->width & 1){
27         av_log(avctx, AV_LOG_ERROR, "v210x needs even width\n");
28         return -1;
29     }
30     avctx->pix_fmt = PIX_FMT_YUV422P16;
31     avctx->bits_per_raw_sample= 10;
32
33     avctx->coded_frame= avcodec_alloc_frame();
34     if (!avctx->coded_frame)
35         return AVERROR(ENOMEM);
36
37     return 0;
38 }
39
40 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
41 {
42     int y=0;
43     int width= avctx->width;
44     AVFrame *pic= avctx->coded_frame;
45     const uint32_t *src= (const uint32_t *)avpkt->data;
46     uint16_t *ydst, *udst, *vdst, *yend;
47
48     if(pic->data[0])
49         avctx->release_buffer(avctx, pic);
50
51     if(avpkt->size < avctx->width * avctx->height * 8 / 3){
52         av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
53         return -1;
54     }
55
56     if(avpkt->size > avctx->width * avctx->height * 8 / 3){
57         av_log_ask_for_sample(avctx, "Probably padded data\n");
58     }
59
60     pic->reference= 0;
61     if(avctx->get_buffer(avctx, pic) < 0)
62         return -1;
63
64     ydst= (uint16_t *)pic->data[0];
65     udst= (uint16_t *)pic->data[1];
66     vdst= (uint16_t *)pic->data[2];
67     yend= ydst + width;
68     pic->pict_type= AV_PICTURE_TYPE_I;
69     pic->key_frame= 1;
70
71     for(;;){
72         uint32_t v= av_be2ne32(*src++);
73         *udst++= (v>>16) & 0xFFC0;
74         *ydst++= (v>>6 ) & 0xFFC0;
75         *vdst++= (v<<4 ) & 0xFFC0;
76
77         v= av_be2ne32(*src++);
78         *ydst++= (v>>16) & 0xFFC0;
79
80         if(ydst >= yend){
81             ydst+= pic->linesize[0]/2 - width;
82             udst+= pic->linesize[1]/2 - width/2;
83             vdst+= pic->linesize[2]/2 - width/2;
84             yend= ydst + width;
85             if(++y >= avctx->height)
86                 break;
87         }
88
89         *udst++= (v>>6 ) & 0xFFC0;
90         *ydst++= (v<<4 ) & 0xFFC0;
91
92         v= av_be2ne32(*src++);
93         *vdst++= (v>>16) & 0xFFC0;
94         *ydst++= (v>>6 ) & 0xFFC0;
95
96         if(ydst >= yend){
97             ydst+= pic->linesize[0]/2 - width;
98             udst+= pic->linesize[1]/2 - width/2;
99             vdst+= pic->linesize[2]/2 - width/2;
100             yend= ydst + width;
101             if(++y >= avctx->height)
102                 break;
103         }
104
105         *udst++= (v<<4 ) & 0xFFC0;
106
107         v= av_be2ne32(*src++);
108         *ydst++= (v>>16) & 0xFFC0;
109         *vdst++= (v>>6 ) & 0xFFC0;
110         *ydst++= (v<<4 ) & 0xFFC0;
111         if(ydst >= yend){
112             ydst+= pic->linesize[0]/2 - width;
113             udst+= pic->linesize[1]/2 - width/2;
114             vdst+= pic->linesize[2]/2 - width/2;
115             yend= ydst + width;
116             if(++y >= avctx->height)
117                 break;
118         }
119     }
120
121     *data_size=sizeof(AVFrame);
122     *(AVFrame*)data= *avctx->coded_frame;
123
124     return avpkt->size;
125 }
126
127 static av_cold int decode_close(AVCodecContext *avctx)
128 {
129     AVFrame *pic = avctx->coded_frame;
130     if (pic->data[0])
131         avctx->release_buffer(avctx, pic);
132     av_freep(&avctx->coded_frame);
133
134     return 0;
135 }
136
137 AVCodec ff_v210x_decoder = {
138     .name           = "v210x",
139     .type           = AVMEDIA_TYPE_VIDEO,
140     .id             = CODEC_ID_V210X,
141     .init           = decode_init,
142     .close          = decode_close,
143     .decode         = decode_frame,
144     .capabilities   = CODEC_CAP_DR1,
145     .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
146 };