Mercurial > libavcodec.hg
annotate asv1.c @ 1333:a1cc1810d58f libavcodec
build error on Alpha patch by (Sam Hocevar <sam at zoy dot org>)
author | michaelni |
---|---|
date | Sun, 29 Jun 2003 00:23:30 +0000 |
parents | 1cbc2380d172 |
children | 46d3fa8501cd |
rev | line source |
---|---|
1273 | 1 /* |
2 * ASUS V1 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 asv1.c | |
22 * ASUS V1 codec. | |
23 */ | |
24 | |
25 #include "avcodec.h" | |
26 #include "dsputil.h" | |
27 #include "mpegvideo.h" | |
28 | |
29 //#undef NDEBUG | |
30 //#include <assert.h> | |
31 | |
32 #define VLC_BITS 5 | |
33 | |
34 typedef struct ASV1Context{ | |
35 AVCodecContext *avctx; | |
36 DSPContext dsp; | |
37 AVFrame picture; | |
38 PutBitContext pb; | |
39 GetBitContext gb; | |
40 ScanTable scantable; | |
41 int inv_qscale; | |
42 int mb_width; | |
43 int mb_height; | |
44 int mb_width2; | |
45 int mb_height2; | |
46 DCTELEM __align8 block[6][64]; | |
47 uint16_t __align8 intra_matrix[64]; | |
48 int __align8 q_intra_matrix[64]; | |
49 uint8_t *bitstream_buffer; | |
50 int bitstream_buffer_size; | |
51 } ASV1Context; | |
52 | |
53 static const uint8_t scantab[64]={ | |
54 0x00,0x08,0x01,0x09,0x10,0x18,0x11,0x19, | |
55 0x02,0x0A,0x03,0x0B,0x12,0x1A,0x13,0x1B, | |
56 0x04,0x0C,0x05,0x0D,0x20,0x28,0x21,0x29, | |
57 0x06,0x0E,0x07,0x0F,0x14,0x1C,0x15,0x1D, | |
58 0x22,0x2A,0x23,0x2B,0x30,0x38,0x31,0x39, | |
59 }; | |
60 | |
61 static const uint8_t ccp_tab[17][2]={ | |
62 {0x2,2}, {0xE,5}, {0xD,5}, {0xC,5}, | |
63 {0xB,5}, {0xA,5}, {0x9,5}, {0x8,5}, | |
64 {0x7,5}, {0x6,5}, {0x5,5}, {0x4,5}, | |
65 {0x3,5}, {0x2,5}, {0x1,5}, {0x3,2}, | |
66 {0xF,5}, //EOB | |
67 }; | |
68 | |
69 static const uint8_t level_tab[7][2]={ | |
70 {3,4}, {3,3}, {3,2}, {0,3}, {2,2}, {2,3}, {2,4} | |
71 }; | |
72 | |
73 static VLC ccp_vlc; | |
74 static VLC level_vlc; | |
75 | |
76 static void init_vlcs(ASV1Context *a){ | |
77 static int done = 0; | |
78 | |
79 if (!done) { | |
80 done = 1; | |
81 | |
82 init_vlc(&ccp_vlc, VLC_BITS, 17, | |
83 &ccp_tab[0][1], 2, 1, | |
84 &ccp_tab[0][0], 2, 1); | |
85 init_vlc(&level_vlc, VLC_BITS, 7, | |
86 &level_tab[0][1], 2, 1, | |
87 &level_tab[0][0], 2, 1); | |
88 } | |
89 } | |
90 | |
91 static inline int get_level(GetBitContext *gb){ | |
92 int code= get_vlc2(gb, level_vlc.table, VLC_BITS, 1); | |
93 | |
94 if(code==3) return get_sbits(gb, 8); | |
95 else return code - 3; | |
96 } | |
97 | |
98 static inline void put_level(PutBitContext *pb, int level){ | |
99 unsigned int index= level + 3; | |
100 | |
101 if(index <= 6) put_bits(pb, level_tab[index][1], level_tab[index][0]); | |
102 else{ | |
103 put_bits(pb, level_tab[3][1], level_tab[3][0]); | |
104 put_bits(pb, 8, level&0xFF); | |
105 } | |
106 } | |
107 | |
108 static inline int decode_block(ASV1Context *a, DCTELEM block[64]){ | |
109 int i; | |
110 | |
111 block[0]= 8*get_bits(&a->gb, 8); | |
112 | |
113 for(i=0; i<11; i++){ | |
114 const int ccp= get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1); | |
115 | |
116 if(ccp){ | |
117 if(ccp == 16) break; | |
118 if(ccp < 0 || i>=10){ | |
119 printf("coded coeff pattern damaged\n"); | |
120 return -1; | |
121 } | |
122 | |
123 if(ccp&1) block[a->scantable.permutated[4*i+0]]= (get_level(&a->gb) * a->intra_matrix[4*i+0])>>4; | |
1322 | 124 if(ccp&2) block[a->scantable.permutated[4*i+1]]= (get_level(&a->gb) * a->intra_matrix[4*i+1])>>4; |
125 if(ccp&4) block[a->scantable.permutated[4*i+2]]= (get_level(&a->gb) * a->intra_matrix[4*i+2])>>4; | |
126 if(ccp&8) block[a->scantable.permutated[4*i+3]]= (get_level(&a->gb) * a->intra_matrix[4*i+3])>>4; | |
1273 | 127 } |
128 } | |
129 | |
130 return 0; | |
131 } | |
132 | |
133 static inline void encode_block(ASV1Context *a, DCTELEM block[64]){ | |
134 int i; | |
135 int nc_count=0; | |
136 | |
137 put_bits(&a->pb, 8, (block[0] + 32)>>6); | |
138 block[0]= 0; | |
139 | |
140 for(i=0; i<10; i++){ | |
141 const int index= scantab[4*i]; | |
142 int ccp=0; | |
143 | |
144 if( (block[index + 0] = (block[index + 0]*a->q_intra_matrix[index + 0] + (1<<15))>>16) ) ccp |= 1; | |
145 if( (block[index + 8] = (block[index + 8]*a->q_intra_matrix[index + 8] + (1<<15))>>16) ) ccp |= 2; | |
146 if( (block[index + 1] = (block[index + 1]*a->q_intra_matrix[index + 1] + (1<<15))>>16) ) ccp |= 4; | |
147 if( (block[index + 9] = (block[index + 9]*a->q_intra_matrix[index + 9] + (1<<15))>>16) ) ccp |= 8; | |
148 | |
149 if(ccp){ | |
150 for(;nc_count; nc_count--) | |
151 put_bits(&a->pb, ccp_tab[0][1], ccp_tab[0][0]); | |
152 | |
153 put_bits(&a->pb, ccp_tab[ccp][1], ccp_tab[ccp][0]); | |
154 | |
155 if(ccp&1) put_level(&a->pb, block[index + 0]); | |
156 if(ccp&2) put_level(&a->pb, block[index + 8]); | |
157 if(ccp&4) put_level(&a->pb, block[index + 1]); | |
158 if(ccp&8) put_level(&a->pb, block[index + 9]); | |
159 }else{ | |
160 nc_count++; | |
161 } | |
162 } | |
163 put_bits(&a->pb, ccp_tab[16][1], ccp_tab[16][0]); | |
164 } | |
165 | |
166 static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){ | |
167 int i; | |
168 | |
169 a->dsp.clear_blocks(block[0]); | |
170 | |
171 for(i=0; i<6; i++){ | |
172 if( decode_block(a, block[i]) < 0) | |
173 return -1; | |
174 } | |
175 return 0; | |
176 } | |
177 | |
178 static inline void encode_mb(ASV1Context *a, DCTELEM block[6][64]){ | |
179 int i; | |
180 | |
181 for(i=0; i<6; i++){ | |
182 encode_block(a, block[i]); | |
183 } | |
184 } | |
1274
95061e8c5ea9
CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1273
diff
changeset
|
185 |
1273 | 186 static inline void idct_put(ASV1Context *a, int mb_x, int mb_y){ |
187 DCTELEM (*block)[64]= a->block; | |
188 int linesize= a->picture.linesize[0]; | |
189 | |
190 uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16; | |
191 uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8; | |
192 uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8; | |
193 | |
194 a->dsp.idct_put(dest_y , linesize, block[0]); | |
195 a->dsp.idct_put(dest_y + 8, linesize, block[1]); | |
196 a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]); | |
197 a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]); | |
198 | |
199 if(!(a->avctx->flags&CODEC_FLAG_GRAY)){ | |
200 a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]); | |
201 a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]); | |
202 } | |
203 } | |
204 | |
205 static inline void dct_get(ASV1Context *a, int mb_x, int mb_y){ | |
206 DCTELEM (*block)[64]= a->block; | |
207 int linesize= a->picture.linesize[0]; | |
208 int i; | |
209 | |
210 uint8_t *ptr_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16; | |
211 uint8_t *ptr_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8; | |
212 uint8_t *ptr_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8; | |
213 | |
214 a->dsp.get_pixels(block[0], ptr_y , linesize); | |
215 a->dsp.get_pixels(block[1], ptr_y + 8, linesize); | |
216 a->dsp.get_pixels(block[2], ptr_y + 8*linesize , linesize); | |
217 a->dsp.get_pixels(block[3], ptr_y + 8*linesize + 8, linesize); | |
218 for(i=0; i<4; i++) | |
219 a->dsp.fdct(block[i]); | |
220 | |
221 if(!(a->avctx->flags&CODEC_FLAG_GRAY)){ | |
222 a->dsp.get_pixels(block[4], ptr_cb, a->picture.linesize[1]); | |
223 a->dsp.get_pixels(block[5], ptr_cr, a->picture.linesize[2]); | |
224 for(i=4; i<6; i++) | |
225 a->dsp.fdct(block[i]); | |
226 } | |
227 } | |
228 | |
229 static int decode_frame(AVCodecContext *avctx, | |
230 void *data, int *data_size, | |
231 uint8_t *buf, int buf_size) | |
232 { | |
233 ASV1Context * const a = avctx->priv_data; | |
234 AVFrame *picture = data; | |
235 AVFrame * const p= (AVFrame*)&a->picture; | |
236 int mb_x, mb_y; | |
237 | |
238 *data_size = 0; | |
239 | |
240 /* special case for last picture */ | |
241 if (buf_size == 0) { | |
242 return 0; | |
243 } | |
244 | |
245 if(p->data[0]) | |
246 avctx->release_buffer(avctx, p); | |
247 | |
248 p->reference= 0; | |
249 if(avctx->get_buffer(avctx, p) < 0){ | |
250 fprintf(stderr, "get_buffer() failed\n"); | |
251 return -1; | |
252 } | |
253 p->pict_type= I_TYPE; | |
254 p->key_frame= 1; | |
255 | |
256 a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
257 a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (uint32_t*)buf, buf_size/4); | |
258 init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8); | |
259 | |
260 for(mb_y=0; mb_y<a->mb_height2; mb_y++){ | |
261 for(mb_x=0; mb_x<a->mb_width2; mb_x++){ | |
262 if( decode_mb(a, a->block) <0) | |
263 return -1; | |
264 | |
265 idct_put(a, mb_x, mb_y); | |
266 } | |
267 } | |
268 | |
269 if(a->mb_width2 != a->mb_width){ | |
270 mb_x= a->mb_width2; | |
271 for(mb_y=0; mb_y<a->mb_height2; mb_y++){ | |
272 if( decode_mb(a, a->block) <0) | |
273 return -1; | |
274 | |
275 idct_put(a, mb_x, mb_y); | |
276 } | |
277 } | |
278 | |
279 if(a->mb_height2 != a->mb_height){ | |
280 mb_y= a->mb_height2; | |
281 for(mb_x=0; mb_x<a->mb_width; mb_x++){ | |
282 if( decode_mb(a, a->block) <0) | |
283 return -1; | |
284 | |
285 idct_put(a, mb_x, mb_y); | |
286 } | |
287 } | |
288 #if 0 | |
289 int i; | |
290 printf("%d %d\n", 8*buf_size, get_bits_count(&a->gb)); | |
291 for(i=get_bits_count(&a->gb); i<8*buf_size; i++){ | |
292 printf("%d", get_bits1(&a->gb)); | |
293 } | |
294 | |
295 for(i=0; i<s->avctx->extradata_size; i++){ | |
296 printf("%c\n", ((uint8_t*)s->avctx->extradata)[i]); | |
297 } | |
298 #endif | |
299 | |
300 p->quality= (32 + a->inv_qscale/2)/a->inv_qscale; | |
301 memset(p->qscale_table, p->quality, p->qstride*a->mb_height); | |
302 | |
303 *picture= *(AVFrame*)&a->picture; | |
304 *data_size = sizeof(AVPicture); | |
305 | |
306 emms_c(); | |
307 | |
308 return (get_bits_count(&a->gb)+31)/32*4; | |
309 } | |
310 | |
311 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | |
312 ASV1Context * const a = avctx->priv_data; | |
313 AVFrame *pict = data; | |
314 AVFrame * const p= (AVFrame*)&a->picture; | |
315 int size; | |
316 int mb_x, mb_y; | |
317 | |
318 init_put_bits(&a->pb, buf, buf_size, NULL, NULL); | |
319 | |
320 *p = *pict; | |
321 p->pict_type= I_TYPE; | |
322 p->key_frame= 1; | |
323 | |
324 for(mb_y=0; mb_y<a->mb_height2; mb_y++){ | |
325 for(mb_x=0; mb_x<a->mb_width2; mb_x++){ | |
326 dct_get(a, mb_x, mb_y); | |
327 encode_mb(a, a->block); | |
328 } | |
329 } | |
330 | |
331 if(a->mb_width2 != a->mb_width){ | |
332 mb_x= a->mb_width2; | |
333 for(mb_y=0; mb_y<a->mb_height2; mb_y++){ | |
334 dct_get(a, mb_x, mb_y); | |
335 encode_mb(a, a->block); | |
336 } | |
337 } | |
338 | |
339 if(a->mb_height2 != a->mb_height){ | |
340 mb_y= a->mb_height2; | |
341 for(mb_x=0; mb_x<a->mb_width; mb_x++){ | |
342 dct_get(a, mb_x, mb_y); | |
343 encode_mb(a, a->block); | |
344 } | |
345 } | |
346 emms_c(); | |
347 | |
348 align_put_bits(&a->pb); | |
349 while(get_bit_count(&a->pb)&31) | |
350 put_bits(&a->pb, 8, 0); | |
351 | |
352 size= get_bit_count(&a->pb)/32; | |
353 | |
354 a->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size); | |
355 | |
356 return size*4; | |
357 } | |
358 | |
359 static void common_init(AVCodecContext *avctx){ | |
360 ASV1Context * const a = avctx->priv_data; | |
361 | |
362 dsputil_init(&a->dsp, avctx); | |
363 | |
364 a->mb_width = (avctx->width + 15) / 16; | |
365 a->mb_height = (avctx->height + 15) / 16; | |
366 a->mb_width2 = (avctx->width + 0) / 16; | |
367 a->mb_height2 = (avctx->height + 0) / 16; | |
368 | |
369 avctx->coded_frame= (AVFrame*)&a->picture; | |
370 a->avctx= avctx; | |
371 } | |
372 | |
373 static int decode_init(AVCodecContext *avctx){ | |
374 ASV1Context * const a = avctx->priv_data; | |
375 AVFrame *p= (AVFrame*)&a->picture; | |
376 int i; | |
377 | |
378 common_init(avctx); | |
379 init_vlcs(a); | |
380 ff_init_scantable(a->dsp.idct_permutation, &a->scantable, scantab); | |
381 | |
382 a->inv_qscale= le2me_32(((uint32_t*)avctx->extradata)[0]); | |
383 if(a->inv_qscale == 0){ | |
384 printf("illegal qscale 0\n"); | |
385 a->inv_qscale= 6; | |
386 } | |
387 | |
388 for(i=0; i<64; i++){ | |
389 int index= scantab[i]; | |
390 a->intra_matrix[i]= 64*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale; | |
391 } | |
392 | |
393 p->qstride= a->mb_width; | |
394 p->qscale_table= av_mallocz( p->qstride * a->mb_height); | |
395 | |
396 return 0; | |
397 } | |
398 | |
399 static int encode_init(AVCodecContext *avctx){ | |
400 ASV1Context * const a = avctx->priv_data; | |
401 int i; | |
402 | |
403 common_init(avctx); | |
404 | |
405 if(avctx->global_quality == 0) avctx->global_quality= 4*FF_QUALITY_SCALE; | |
406 | |
407 a->inv_qscale= (32*FF_QUALITY_SCALE + avctx->global_quality/2) / avctx->global_quality; | |
408 | |
409 avctx->extradata= av_mallocz(8); | |
410 avctx->extradata_size=8; | |
411 ((uint32_t*)avctx->extradata)[0]= le2me_32(a->inv_qscale); | |
412 ((uint32_t*)avctx->extradata)[1]= le2me_32(ff_get_fourcc("ASUS")); | |
413 | |
414 for(i=0; i<64; i++){ | |
415 int q= 32*ff_mpeg1_default_intra_matrix[i]; | |
416 a->q_intra_matrix[i]= ((a->inv_qscale<<16) + q/2) / q; | |
417 } | |
418 | |
419 return 0; | |
420 } | |
421 | |
422 static int decode_end(AVCodecContext *avctx){ | |
423 ASV1Context * const a = avctx->priv_data; | |
424 | |
425 av_freep(&a->bitstream_buffer); | |
426 av_freep(&a->picture.qscale_table); | |
427 a->bitstream_buffer_size=0; | |
428 | |
429 avcodec_default_free_buffers(avctx); | |
430 | |
431 return 0; | |
432 } | |
433 | |
434 AVCodec asv1_decoder = { | |
435 "asv1", | |
436 CODEC_TYPE_VIDEO, | |
437 CODEC_ID_ASV1, | |
438 sizeof(ASV1Context), | |
439 decode_init, | |
440 NULL, | |
441 decode_end, | |
442 decode_frame, | |
443 CODEC_CAP_DR1, | |
444 }; | |
445 | |
1274
95061e8c5ea9
CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1273
diff
changeset
|
446 #ifdef CONFIG_ENCODERS |
95061e8c5ea9
CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1273
diff
changeset
|
447 |
1273 | 448 AVCodec asv1_encoder = { |
449 "asv1", | |
450 CODEC_TYPE_VIDEO, | |
451 CODEC_ID_ASV1, | |
452 sizeof(ASV1Context), | |
453 encode_init, | |
454 encode_frame, | |
455 //encode_end, | |
456 }; | |
1274
95061e8c5ea9
CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1273
diff
changeset
|
457 |
95061e8c5ea9
CONFIG_ENCODERS patch by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1273
diff
changeset
|
458 #endif //CONFIG_ENCODERS |