Mercurial > libavcodec.hg
comparison mjpeg.c @ 1064:b32afefe7d33 libavcodec
* UINTX -> uintx_t INTX -> intx_t
author | kabi |
---|---|
date | Tue, 11 Feb 2003 16:35:48 +0000 |
parents | 081b1f28c1ae |
children | f59c3f66363b |
comparison
equal
deleted
inserted
replaced
1063:fdeac9642346 | 1064:b32afefe7d33 |
---|---|
28 /* use two quantizer tables (one for luminance and one for chrominance) */ | 28 /* use two quantizer tables (one for luminance and one for chrominance) */ |
29 /* not yet working */ | 29 /* not yet working */ |
30 #undef TWOMATRIXES | 30 #undef TWOMATRIXES |
31 | 31 |
32 typedef struct MJpegContext { | 32 typedef struct MJpegContext { |
33 UINT8 huff_size_dc_luminance[12]; | 33 uint8_t huff_size_dc_luminance[12]; |
34 UINT16 huff_code_dc_luminance[12]; | 34 uint16_t huff_code_dc_luminance[12]; |
35 UINT8 huff_size_dc_chrominance[12]; | 35 uint8_t huff_size_dc_chrominance[12]; |
36 UINT16 huff_code_dc_chrominance[12]; | 36 uint16_t huff_code_dc_chrominance[12]; |
37 | 37 |
38 UINT8 huff_size_ac_luminance[256]; | 38 uint8_t huff_size_ac_luminance[256]; |
39 UINT16 huff_code_ac_luminance[256]; | 39 uint16_t huff_code_ac_luminance[256]; |
40 UINT8 huff_size_ac_chrominance[256]; | 40 uint8_t huff_size_ac_chrominance[256]; |
41 UINT16 huff_code_ac_chrominance[256]; | 41 uint16_t huff_code_ac_chrominance[256]; |
42 } MJpegContext; | 42 } MJpegContext; |
43 | 43 |
44 /* JPEG marker codes */ | 44 /* JPEG marker codes */ |
45 typedef enum { | 45 typedef enum { |
46 /* start of frame */ | 46 /* start of frame */ |
150 }; | 150 }; |
151 #endif | 151 #endif |
152 | 152 |
153 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ | 153 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ |
154 /* IMPORTANT: these are only valid for 8-bit data precision! */ | 154 /* IMPORTANT: these are only valid for 8-bit data precision! */ |
155 static const UINT8 bits_dc_luminance[17] = | 155 static const uint8_t bits_dc_luminance[17] = |
156 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; | 156 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; |
157 static const UINT8 val_dc_luminance[] = | 157 static const uint8_t val_dc_luminance[] = |
158 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; | 158 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; |
159 | 159 |
160 static const UINT8 bits_dc_chrominance[17] = | 160 static const uint8_t bits_dc_chrominance[17] = |
161 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; | 161 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; |
162 static const UINT8 val_dc_chrominance[] = | 162 static const uint8_t val_dc_chrominance[] = |
163 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; | 163 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; |
164 | 164 |
165 static const UINT8 bits_ac_luminance[17] = | 165 static const uint8_t bits_ac_luminance[17] = |
166 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; | 166 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; |
167 static const UINT8 val_ac_luminance[] = | 167 static const uint8_t val_ac_luminance[] = |
168 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, | 168 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, |
169 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, | 169 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, |
170 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, | 170 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, |
171 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, | 171 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, |
172 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, | 172 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, |
186 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, | 186 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, |
187 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | 187 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, |
188 0xf9, 0xfa | 188 0xf9, 0xfa |
189 }; | 189 }; |
190 | 190 |
191 static const UINT8 bits_ac_chrominance[17] = | 191 static const uint8_t bits_ac_chrominance[17] = |
192 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; | 192 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; |
193 | 193 |
194 static const UINT8 val_ac_chrominance[] = | 194 static const uint8_t val_ac_chrominance[] = |
195 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, | 195 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, |
196 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, | 196 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, |
197 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, | 197 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, |
198 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, | 198 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, |
199 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, | 199 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, |
214 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | 214 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, |
215 0xf9, 0xfa | 215 0xf9, 0xfa |
216 }; | 216 }; |
217 | 217 |
218 /* isn't this function nicer than the one in the libjpeg ? */ | 218 /* isn't this function nicer than the one in the libjpeg ? */ |
219 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code, | 219 static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, |
220 const UINT8 *bits_table, const UINT8 *val_table) | 220 const uint8_t *bits_table, const uint8_t *val_table) |
221 { | 221 { |
222 int i, j, k,nb, code, sym; | 222 int i, j, k,nb, code, sym; |
223 | 223 |
224 code = 0; | 224 code = 0; |
225 k = 0; | 225 k = 0; |
280 put_bits(p, 8, code); | 280 put_bits(p, 8, code); |
281 } | 281 } |
282 | 282 |
283 /* table_class: 0 = DC coef, 1 = AC coefs */ | 283 /* table_class: 0 = DC coef, 1 = AC coefs */ |
284 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id, | 284 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id, |
285 const UINT8 *bits_table, const UINT8 *value_table) | 285 const uint8_t *bits_table, const uint8_t *value_table) |
286 { | 286 { |
287 PutBitContext *p = &s->pb; | 287 PutBitContext *p = &s->pb; |
288 int n, i; | 288 int n, i; |
289 | 289 |
290 put_bits(p, 4, table_class); | 290 put_bits(p, 4, table_class); |
304 | 304 |
305 static void jpeg_table_header(MpegEncContext *s) | 305 static void jpeg_table_header(MpegEncContext *s) |
306 { | 306 { |
307 PutBitContext *p = &s->pb; | 307 PutBitContext *p = &s->pb; |
308 int i, j, size; | 308 int i, j, size; |
309 UINT8 *ptr; | 309 uint8_t *ptr; |
310 | 310 |
311 /* quant matrixes */ | 311 /* quant matrixes */ |
312 put_marker(p, DQT); | 312 put_marker(p, DQT); |
313 #ifdef TWOMATRIXES | 313 #ifdef TWOMATRIXES |
314 put_bits(p, 16, 2 + 2 * (1 + 64)); | 314 put_bits(p, 16, 2 + 2 * (1 + 64)); |
347 | 347 |
348 static void jpeg_put_comments(MpegEncContext *s) | 348 static void jpeg_put_comments(MpegEncContext *s) |
349 { | 349 { |
350 PutBitContext *p = &s->pb; | 350 PutBitContext *p = &s->pb; |
351 int size; | 351 int size; |
352 UINT8 *ptr; | 352 uint8_t *ptr; |
353 | 353 |
354 if (s->aspect_ratio_info) | 354 if (s->aspect_ratio_info) |
355 { | 355 { |
356 /* JFIF header */ | 356 /* JFIF header */ |
357 put_marker(p, APP0); | 357 put_marker(p, APP0); |
539 | 539 |
540 put_marker(&s->pb, EOI); | 540 put_marker(&s->pb, EOI); |
541 } | 541 } |
542 | 542 |
543 static inline void mjpeg_encode_dc(MpegEncContext *s, int val, | 543 static inline void mjpeg_encode_dc(MpegEncContext *s, int val, |
544 UINT8 *huff_size, UINT16 *huff_code) | 544 uint8_t *huff_size, uint16_t *huff_code) |
545 { | 545 { |
546 int mant, nbits; | 546 int mant, nbits; |
547 | 547 |
548 if (val == 0) { | 548 if (val == 0) { |
549 put_bits(&s->pb, huff_size[0], huff_code[0]); | 549 put_bits(&s->pb, huff_size[0], huff_code[0]); |
570 static void encode_block(MpegEncContext *s, DCTELEM *block, int n) | 570 static void encode_block(MpegEncContext *s, DCTELEM *block, int n) |
571 { | 571 { |
572 int mant, nbits, code, i, j; | 572 int mant, nbits, code, i, j; |
573 int component, dc, run, last_index, val; | 573 int component, dc, run, last_index, val; |
574 MJpegContext *m = s->mjpeg_ctx; | 574 MJpegContext *m = s->mjpeg_ctx; |
575 UINT8 *huff_size_ac; | 575 uint8_t *huff_size_ac; |
576 UINT16 *huff_code_ac; | 576 uint16_t *huff_code_ac; |
577 | 577 |
578 /* DC coef */ | 578 /* DC coef */ |
579 component = (n <= 3 ? 0 : n - 4 + 1); | 579 component = (n <= 3 ? 0 : n - 4 + 1); |
580 dc = block[0]; /* overflow is impossible */ | 580 dc = block[0]; /* overflow is impossible */ |
581 val = dc - s->last_dc[component]; | 581 val = dc - s->last_dc[component]; |
649 GetBitContext gb; | 649 GetBitContext gb; |
650 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ | 650 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ |
651 | 651 |
652 int start_code; /* current start code */ | 652 int start_code; /* current start code */ |
653 int buffer_size; | 653 int buffer_size; |
654 UINT8 *buffer; | 654 uint8_t *buffer; |
655 | 655 |
656 INT16 quant_matrixes[4][64]; | 656 int16_t quant_matrixes[4][64]; |
657 VLC vlcs[2][4]; | 657 VLC vlcs[2][4]; |
658 | 658 |
659 int org_width, org_height; /* size given at codec init */ | 659 int org_width, org_height; /* size given at codec init */ |
660 int first_picture; /* true if decoding first picture */ | 660 int first_picture; /* true if decoding first picture */ |
661 int interlaced; /* true if interlaced */ | 661 int interlaced; /* true if interlaced */ |
667 int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */ | 667 int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */ |
668 int v_count[MAX_COMPONENTS]; | 668 int v_count[MAX_COMPONENTS]; |
669 int h_max, v_max; /* maximum h and v counts */ | 669 int h_max, v_max; /* maximum h and v counts */ |
670 int quant_index[4]; /* quant table index for each component */ | 670 int quant_index[4]; /* quant table index for each component */ |
671 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ | 671 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ |
672 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */ | 672 uint8_t *current_picture[MAX_COMPONENTS]; /* picture structure */ |
673 int linesize[MAX_COMPONENTS]; | 673 int linesize[MAX_COMPONENTS]; |
674 DCTELEM block[64] __align8; | 674 DCTELEM block[64] __align8; |
675 ScanTable scantable; | 675 ScanTable scantable; |
676 void (*idct_put)(UINT8 *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/); | 676 void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/); |
677 | 677 |
678 int restart_interval; | 678 int restart_interval; |
679 int restart_count; | 679 int restart_count; |
680 | 680 |
681 int buggy_avid; | 681 int buggy_avid; |
682 int interlace_polarity; | 682 int interlace_polarity; |
683 } MJpegDecodeContext; | 683 } MJpegDecodeContext; |
684 | 684 |
685 static int mjpeg_decode_dht(MJpegDecodeContext *s); | 685 static int mjpeg_decode_dht(MJpegDecodeContext *s); |
686 | 686 |
687 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table, | 687 static void build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, |
688 int nb_codes) | 688 int nb_codes) |
689 { | 689 { |
690 UINT8 huff_size[256]; | 690 uint8_t huff_size[256]; |
691 UINT16 huff_code[256]; | 691 uint16_t huff_code[256]; |
692 | 692 |
693 memset(huff_size, 0, sizeof(huff_size)); | 693 memset(huff_size, 0, sizeof(huff_size)); |
694 build_huffman_codes(huff_size, huff_code, bits_table, val_table); | 694 build_huffman_codes(huff_size, huff_code, bits_table, val_table); |
695 | 695 |
696 init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2); | 696 init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2); |
774 | 774 |
775 /* decode huffman tables and build VLC decoders */ | 775 /* decode huffman tables and build VLC decoders */ |
776 static int mjpeg_decode_dht(MJpegDecodeContext *s) | 776 static int mjpeg_decode_dht(MJpegDecodeContext *s) |
777 { | 777 { |
778 int len, index, i, class, n, v, code_max; | 778 int len, index, i, class, n, v, code_max; |
779 UINT8 bits_table[17]; | 779 uint8_t bits_table[17]; |
780 UINT8 val_table[256]; | 780 uint8_t val_table[256]; |
781 | 781 |
782 len = get_bits(&s->gb, 16) - 2; | 782 len = get_bits(&s->gb, 16) - 2; |
783 | 783 |
784 while (len > 0) { | 784 while (len > 0) { |
785 if (len < 17) | 785 if (len < 17) |
926 int component, int dc_index, int ac_index, int quant_index) | 926 int component, int dc_index, int ac_index, int quant_index) |
927 { | 927 { |
928 int nbits, code, i, j, level; | 928 int nbits, code, i, j, level; |
929 int run, val; | 929 int run, val; |
930 VLC *ac_vlc; | 930 VLC *ac_vlc; |
931 INT16 *quant_matrix; | 931 int16_t *quant_matrix; |
932 | 932 |
933 /* DC coef */ | 933 /* DC coef */ |
934 val = mjpeg_decode_dc(s, dc_index); | 934 val = mjpeg_decode_dc(s, dc_index); |
935 if (val == 0xffff) { | 935 if (val == 0xffff) { |
936 dprintf("error dc\n"); | 936 dprintf("error dc\n"); |
1069 } | 1069 } |
1070 | 1070 |
1071 for(mb_y = 0; mb_y < mb_height; mb_y++) { | 1071 for(mb_y = 0; mb_y < mb_height; mb_y++) { |
1072 for(mb_x = 0; mb_x < mb_width; mb_x++) { | 1072 for(mb_x = 0; mb_x < mb_width; mb_x++) { |
1073 for(i=0;i<nb_components;i++) { | 1073 for(i=0;i<nb_components;i++) { |
1074 UINT8 *ptr; | 1074 uint8_t *ptr; |
1075 int x, y, c; | 1075 int x, y, c; |
1076 n = nb_blocks[i]; | 1076 n = nb_blocks[i]; |
1077 c = comp_index[i]; | 1077 c = comp_index[i]; |
1078 h = h_count[i]; | 1078 h = h_count[i]; |
1079 v = v_count[i]; | 1079 v = v_count[i]; |
1264 { | 1264 { |
1265 /* XXX: verify len field validity */ | 1265 /* XXX: verify len field validity */ |
1266 unsigned int len = get_bits(&s->gb, 16); | 1266 unsigned int len = get_bits(&s->gb, 16); |
1267 if (len >= 2 && len < 32768) { | 1267 if (len >= 2 && len < 32768) { |
1268 /* XXX: any better upper bound */ | 1268 /* XXX: any better upper bound */ |
1269 UINT8 *cbuf = av_malloc(len - 1); | 1269 uint8_t *cbuf = av_malloc(len - 1); |
1270 if (cbuf) { | 1270 if (cbuf) { |
1271 int i; | 1271 int i; |
1272 for (i = 0; i < len - 2; i++) | 1272 for (i = 0; i < len - 2; i++) |
1273 cbuf[i] = get_bits(&s->gb, 8); | 1273 cbuf[i] = get_bits(&s->gb, 8); |
1274 if (i > 0 && cbuf[i-1] == '\n') | 1274 if (i > 0 && cbuf[i-1] == '\n') |
1316 } | 1316 } |
1317 #endif | 1317 #endif |
1318 | 1318 |
1319 /* return the 8 bit start code value and update the search | 1319 /* return the 8 bit start code value and update the search |
1320 state. Return -1 if no start code found */ | 1320 state. Return -1 if no start code found */ |
1321 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end) | 1321 static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end) |
1322 { | 1322 { |
1323 UINT8 *buf_ptr; | 1323 uint8_t *buf_ptr; |
1324 unsigned int v, v2; | 1324 unsigned int v, v2; |
1325 int val; | 1325 int val; |
1326 #ifdef DEBUG | 1326 #ifdef DEBUG |
1327 int skipped=0; | 1327 int skipped=0; |
1328 #endif | 1328 #endif |
1348 return val; | 1348 return val; |
1349 } | 1349 } |
1350 | 1350 |
1351 static int mjpeg_decode_frame(AVCodecContext *avctx, | 1351 static int mjpeg_decode_frame(AVCodecContext *avctx, |
1352 void *data, int *data_size, | 1352 void *data, int *data_size, |
1353 UINT8 *buf, int buf_size) | 1353 uint8_t *buf, int buf_size) |
1354 { | 1354 { |
1355 MJpegDecodeContext *s = avctx->priv_data; | 1355 MJpegDecodeContext *s = avctx->priv_data; |
1356 UINT8 *buf_end, *buf_ptr; | 1356 uint8_t *buf_end, *buf_ptr; |
1357 int i, start_code; | 1357 int i, start_code; |
1358 AVPicture *picture = data; | 1358 AVPicture *picture = data; |
1359 | 1359 |
1360 *data_size = 0; | 1360 *data_size = 0; |
1361 | 1361 |
1385 } | 1385 } |
1386 | 1386 |
1387 /* unescape buffer of SOS */ | 1387 /* unescape buffer of SOS */ |
1388 if (start_code == SOS) | 1388 if (start_code == SOS) |
1389 { | 1389 { |
1390 UINT8 *src = buf_ptr; | 1390 uint8_t *src = buf_ptr; |
1391 UINT8 *dst = s->buffer; | 1391 uint8_t *dst = s->buffer; |
1392 | 1392 |
1393 while (src<buf_end) | 1393 while (src<buf_end) |
1394 { | 1394 { |
1395 UINT8 x = *(src++); | 1395 uint8_t x = *(src++); |
1396 | 1396 |
1397 *(dst++) = x; | 1397 *(dst++) = x; |
1398 if (x == 0xff) | 1398 if (x == 0xff) |
1399 { | 1399 { |
1400 while(*src == 0xff) src++; | 1400 while(*src == 0xff) src++; |
1525 return buf_ptr - buf; | 1525 return buf_ptr - buf; |
1526 } | 1526 } |
1527 | 1527 |
1528 static int mjpegb_decode_frame(AVCodecContext *avctx, | 1528 static int mjpegb_decode_frame(AVCodecContext *avctx, |
1529 void *data, int *data_size, | 1529 void *data, int *data_size, |
1530 UINT8 *buf, int buf_size) | 1530 uint8_t *buf, int buf_size) |
1531 { | 1531 { |
1532 MJpegDecodeContext *s = avctx->priv_data; | 1532 MJpegDecodeContext *s = avctx->priv_data; |
1533 UINT8 *buf_end, *buf_ptr; | 1533 uint8_t *buf_end, *buf_ptr; |
1534 int i; | 1534 int i; |
1535 AVPicture *picture = data; | 1535 AVPicture *picture = data; |
1536 GetBitContext hgb; /* for the header */ | 1536 GetBitContext hgb; /* for the header */ |
1537 uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs; | 1537 uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs; |
1538 uint32_t field_size; | 1538 uint32_t field_size; |