]> git.sesse.net Git - ffmpeg/blob - libavcodec/v210x.c
Merge commit 'ff953fecffd3b9a616a046723fb9d4690be032a6'
[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 "internal.h"
23 #include "libavutil/bswap.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/mem.h"
26
27 static av_cold int decode_init(AVCodecContext *avctx)
28 {
29     if(avctx->width & 1){
30         av_log(avctx, AV_LOG_ERROR, "v210x needs even width\n");
31         return -1;
32     }
33     avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
34     avctx->bits_per_raw_sample= 10;
35
36     avctx->coded_frame= avcodec_alloc_frame();
37     if (!avctx->coded_frame)
38         return AVERROR(ENOMEM);
39
40     return 0;
41 }
42
43 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
44 {
45     int y=0;
46     int width= avctx->width;
47     AVFrame *pic= avctx->coded_frame;
48     const uint32_t *src= (const uint32_t *)avpkt->data;
49     uint16_t *ydst, *udst, *vdst, *yend;
50
51     if(pic->data[0])
52         avctx->release_buffer(avctx, pic);
53
54     if(avpkt->size < avctx->width * avctx->height * 8 / 3){
55         av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
56         return -1;
57     }
58
59     if(avpkt->size > avctx->width * avctx->height * 8 / 3){
60         av_log_ask_for_sample(avctx, "Probably padded data\n");
61     }
62
63     pic->reference= 0;
64     if(ff_get_buffer(avctx, pic) < 0)
65         return -1;
66
67     ydst= (uint16_t *)pic->data[0];
68     udst= (uint16_t *)pic->data[1];
69     vdst= (uint16_t *)pic->data[2];
70     yend= ydst + width;
71     pic->pict_type= AV_PICTURE_TYPE_I;
72     pic->key_frame= 1;
73
74     for(;;){
75         uint32_t v= av_be2ne32(*src++);
76         *udst++= (v>>16) & 0xFFC0;
77         *ydst++= (v>>6 ) & 0xFFC0;
78         *vdst++= (v<<4 ) & 0xFFC0;
79
80         v= av_be2ne32(*src++);
81         *ydst++= (v>>16) & 0xFFC0;
82
83         if(ydst >= yend){
84             ydst+= pic->linesize[0]/2 - width;
85             udst+= pic->linesize[1]/2 - width/2;
86             vdst+= pic->linesize[2]/2 - width/2;
87             yend= ydst + width;
88             if(++y >= avctx->height)
89                 break;
90         }
91
92         *udst++= (v>>6 ) & 0xFFC0;
93         *ydst++= (v<<4 ) & 0xFFC0;
94
95         v= av_be2ne32(*src++);
96         *vdst++= (v>>16) & 0xFFC0;
97         *ydst++= (v>>6 ) & 0xFFC0;
98
99         if(ydst >= yend){
100             ydst+= pic->linesize[0]/2 - width;
101             udst+= pic->linesize[1]/2 - width/2;
102             vdst+= pic->linesize[2]/2 - width/2;
103             yend= ydst + width;
104             if(++y >= avctx->height)
105                 break;
106         }
107
108         *udst++= (v<<4 ) & 0xFFC0;
109
110         v= av_be2ne32(*src++);
111         *ydst++= (v>>16) & 0xFFC0;
112         *vdst++= (v>>6 ) & 0xFFC0;
113         *ydst++= (v<<4 ) & 0xFFC0;
114         if(ydst >= yend){
115             ydst+= pic->linesize[0]/2 - width;
116             udst+= pic->linesize[1]/2 - width/2;
117             vdst+= pic->linesize[2]/2 - width/2;
118             yend= ydst + width;
119             if(++y >= avctx->height)
120                 break;
121         }
122     }
123
124     *data_size=sizeof(AVFrame);
125     *(AVFrame*)data= *avctx->coded_frame;
126
127     return avpkt->size;
128 }
129
130 static av_cold int decode_close(AVCodecContext *avctx)
131 {
132     AVFrame *pic = avctx->coded_frame;
133     if (pic->data[0])
134         avctx->release_buffer(avctx, pic);
135     av_freep(&avctx->coded_frame);
136
137     return 0;
138 }
139
140 AVCodec ff_v210x_decoder = {
141     .name           = "v210x",
142     .type           = AVMEDIA_TYPE_VIDEO,
143     .id             = AV_CODEC_ID_V210X,
144     .init           = decode_init,
145     .close          = decode_close,
146     .decode         = decode_frame,
147     .capabilities   = CODEC_CAP_DR1,
148     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
149 };