OSDN Git Service

add ffmpeg
[android-x86/external-stagefright-plugins.git] / ffmpeg / libavcodec / vcr1.c
1 /*
2  * ATI VCR1 codec
3  * Copyright (c) 2003 Michael Niedermayer
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * ati vcr1 codec.
25  */
26
27 #include "avcodec.h"
28 #include "dsputil.h"
29
30 //#undef NDEBUG
31 //#include <assert.h>
32
33 /* Disable the encoder. */
34 #undef CONFIG_VCR1_ENCODER
35 #define CONFIG_VCR1_ENCODER 0
36
37 typedef struct VCR1Context{
38     AVCodecContext *avctx;
39     AVFrame picture;
40     int delta[16];
41     int offset[4];
42 } VCR1Context;
43
44 static int decode_frame(AVCodecContext *avctx,
45                         void *data, int *data_size,
46                         AVPacket *avpkt)
47 {
48     const uint8_t *buf = avpkt->data;
49     int buf_size = avpkt->size;
50     VCR1Context * const a = avctx->priv_data;
51     AVFrame *picture = data;
52     AVFrame * const p= (AVFrame*)&a->picture;
53     const uint8_t *bytestream= buf;
54     int i, x, y;
55
56     if(p->data[0])
57         avctx->release_buffer(avctx, p);
58
59     if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
60         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
61         return AVERROR(EINVAL);
62     }
63
64     p->reference= 0;
65     if(avctx->get_buffer(avctx, p) < 0){
66         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
67         return -1;
68     }
69     p->pict_type= AV_PICTURE_TYPE_I;
70     p->key_frame= 1;
71
72     for(i=0; i<16; i++){
73         a->delta[i]= *(bytestream++);
74         bytestream++;
75     }
76
77     for(y=0; y<avctx->height; y++){
78         int offset;
79         uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ];
80
81         if((y&3) == 0){
82             uint8_t *cb= &a->picture.data[1][ (y>>2)*a->picture.linesize[1] ];
83             uint8_t *cr= &a->picture.data[2][ (y>>2)*a->picture.linesize[2] ];
84
85             for(i=0; i<4; i++)
86                 a->offset[i]= *(bytestream++);
87
88             offset= a->offset[0] - a->delta[ bytestream[2]&0xF ];
89             for(x=0; x<avctx->width; x+=4){
90                 luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
91                 luma[1]=( offset += a->delta[ bytestream[2]>>4  ]);
92                 luma[2]=( offset += a->delta[ bytestream[0]&0xF ]);
93                 luma[3]=( offset += a->delta[ bytestream[0]>>4  ]);
94                 luma += 4;
95
96                 *(cb++) = bytestream[3];
97                 *(cr++) = bytestream[1];
98
99                 bytestream+= 4;
100             }
101         }else{
102             offset= a->offset[y&3] - a->delta[ bytestream[2]&0xF ];
103
104             for(x=0; x<avctx->width; x+=8){
105                 luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
106                 luma[1]=( offset += a->delta[ bytestream[2]>>4  ]);
107                 luma[2]=( offset += a->delta[ bytestream[3]&0xF ]);
108                 luma[3]=( offset += a->delta[ bytestream[3]>>4  ]);
109                 luma[4]=( offset += a->delta[ bytestream[0]&0xF ]);
110                 luma[5]=( offset += a->delta[ bytestream[0]>>4  ]);
111                 luma[6]=( offset += a->delta[ bytestream[1]&0xF ]);
112                 luma[7]=( offset += a->delta[ bytestream[1]>>4  ]);
113                 luma += 8;
114                 bytestream+= 4;
115             }
116         }
117     }
118
119     *picture= *(AVFrame*)&a->picture;
120     *data_size = sizeof(AVPicture);
121
122     return buf_size;
123 }
124
125 #if CONFIG_VCR1_ENCODER
126 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
127     VCR1Context * const a = avctx->priv_data;
128     AVFrame *pict = data;
129     AVFrame * const p= (AVFrame*)&a->picture;
130     int size;
131
132     *p = *pict;
133     p->pict_type= AV_PICTURE_TYPE_I;
134     p->key_frame= 1;
135
136     avpriv_align_put_bits(&a->pb);
137     while(get_bit_count(&a->pb)&31)
138         put_bits(&a->pb, 8, 0);
139
140     size= get_bit_count(&a->pb)/32;
141
142     return size*4;
143 }
144 #endif
145
146 static av_cold void common_init(AVCodecContext *avctx){
147     VCR1Context * const a = avctx->priv_data;
148
149     avctx->coded_frame= (AVFrame*)&a->picture;
150     avcodec_get_frame_defaults(&a->picture);
151     a->avctx= avctx;
152 }
153
154 static av_cold int decode_init(AVCodecContext *avctx){
155
156     common_init(avctx);
157
158     avctx->pix_fmt= PIX_FMT_YUV410P;
159
160     return 0;
161 }
162
163 static av_cold int decode_end(AVCodecContext *avctx){
164     VCR1Context *s = avctx->priv_data;
165
166     if (s->picture.data[0])
167         avctx->release_buffer(avctx, &s->picture);
168
169     return 0;
170 }
171
172 #if CONFIG_VCR1_ENCODER
173 static av_cold int encode_init(AVCodecContext *avctx){
174
175     common_init(avctx);
176
177     return 0;
178 }
179 #endif
180
181 AVCodec ff_vcr1_decoder = {
182     .name           = "vcr1",
183     .type           = AVMEDIA_TYPE_VIDEO,
184     .id             = CODEC_ID_VCR1,
185     .priv_data_size = sizeof(VCR1Context),
186     .init           = decode_init,
187     .close          = decode_end,
188     .decode         = decode_frame,
189     .capabilities   = CODEC_CAP_DR1,
190     .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
191 };
192
193 #if CONFIG_VCR1_ENCODER
194 AVCodec ff_vcr1_encoder = {
195     .name           = "vcr1",
196     .type           = AVMEDIA_TYPE_VIDEO,
197     .id             = CODEC_ID_VCR1,
198     .priv_data_size = sizeof(VCR1Context),
199     .init           = encode_init,
200     .encode         = encode_frame,
201     .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
202 };
203 #endif