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