comparison mjpeg.c @ 23:1e131bc21101 libavcodec

added MJPEG decoder, renamed mjpegenc.c to mjpeg.c
author glantau
date Mon, 06 Aug 2001 00:44:48 +0000
parents mjpegenc.c@986e461dc072
children ce751dfbcace
comparison
equal deleted inserted replaced
22:c31fb57d17a6 23:1e131bc21101
1 /*
2 * MJPEG encoder and decoder
3 * Copyright (c) 2000, 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "avcodec.h"
22 #include "dsputil.h"
23 #include "mpegvideo.h"
24
25 typedef struct MJpegContext {
26 UINT8 huff_size_dc_luminance[12];
27 UINT16 huff_code_dc_luminance[12];
28 UINT8 huff_size_dc_chrominance[12];
29 UINT16 huff_code_dc_chrominance[12];
30
31 UINT8 huff_size_ac_luminance[256];
32 UINT16 huff_code_ac_luminance[256];
33 UINT8 huff_size_ac_chrominance[256];
34 UINT16 huff_code_ac_chrominance[256];
35 } MJpegContext;
36
37 #define SOF0 0xc0
38 #define SOI 0xd8
39 #define EOI 0xd9
40 #define DQT 0xdb
41 #define DHT 0xc4
42 #define SOS 0xda
43
44 #if 0
45 /* These are the sample quantization tables given in JPEG spec section K.1.
46 * The spec says that the values given produce "good" quality, and
47 * when divided by 2, "very good" quality.
48 */
49 static const unsigned char std_luminance_quant_tbl[64] = {
50 16, 11, 10, 16, 24, 40, 51, 61,
51 12, 12, 14, 19, 26, 58, 60, 55,
52 14, 13, 16, 24, 40, 57, 69, 56,
53 14, 17, 22, 29, 51, 87, 80, 62,
54 18, 22, 37, 56, 68, 109, 103, 77,
55 24, 35, 55, 64, 81, 104, 113, 92,
56 49, 64, 78, 87, 103, 121, 120, 101,
57 72, 92, 95, 98, 112, 100, 103, 99
58 };
59 static const unsigned char std_chrominance_quant_tbl[64] = {
60 17, 18, 24, 47, 99, 99, 99, 99,
61 18, 21, 26, 66, 99, 99, 99, 99,
62 24, 26, 56, 99, 99, 99, 99, 99,
63 47, 66, 99, 99, 99, 99, 99, 99,
64 99, 99, 99, 99, 99, 99, 99, 99,
65 99, 99, 99, 99, 99, 99, 99, 99,
66 99, 99, 99, 99, 99, 99, 99, 99,
67 99, 99, 99, 99, 99, 99, 99, 99
68 };
69 #endif
70
71 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
72 /* IMPORTANT: these are only valid for 8-bit data precision! */
73 static const UINT8 bits_dc_luminance[17] =
74 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
75 static const UINT8 val_dc_luminance[] =
76 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
77
78 static const UINT8 bits_dc_chrominance[17] =
79 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
80 static const UINT8 val_dc_chrominance[] =
81 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
82
83 static const UINT8 bits_ac_luminance[17] =
84 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
85 static const UINT8 val_ac_luminance[] =
86 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
87 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
88 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
89 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
90 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
91 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
92 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
93 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
94 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
95 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
96 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
97 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
98 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
99 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
100 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
101 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
102 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
103 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
104 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
105 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
106 0xf9, 0xfa
107 };
108
109 static const UINT8 bits_ac_chrominance[17] =
110 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
111
112 static const UINT8 val_ac_chrominance[] =
113 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
114 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
115 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
116 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
117 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
118 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
119 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
120 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
121 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
122 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
123 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
124 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
125 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
126 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
127 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
128 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
129 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
130 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
131 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
132 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
133 0xf9, 0xfa
134 };
135
136
137 /* isn't this function nicer than the one in the libjpeg ? */
138 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
139 const UINT8 *bits_table, const UINT8 *val_table)
140 {
141 int i, j, k,nb, code, sym;
142
143 code = 0;
144 k = 0;
145 for(i=1;i<=16;i++) {
146 nb = bits_table[i];
147 for(j=0;j<nb;j++) {
148 sym = val_table[k++];
149 huff_size[sym] = i;
150 huff_code[sym] = code;
151 code++;
152 }
153 code <<= 1;
154 }
155 }
156
157 int mjpeg_init(MpegEncContext *s)
158 {
159 MJpegContext *m;
160
161 m = malloc(sizeof(MJpegContext));
162 if (!m)
163 return -1;
164
165 /* build all the huffman tables */
166 build_huffman_codes(m->huff_size_dc_luminance,
167 m->huff_code_dc_luminance,
168 bits_dc_luminance,
169 val_dc_luminance);
170 build_huffman_codes(m->huff_size_dc_chrominance,
171 m->huff_code_dc_chrominance,
172 bits_dc_chrominance,
173 val_dc_chrominance);
174 build_huffman_codes(m->huff_size_ac_luminance,
175 m->huff_code_ac_luminance,
176 bits_ac_luminance,
177 val_ac_luminance);
178 build_huffman_codes(m->huff_size_ac_chrominance,
179 m->huff_code_ac_chrominance,
180 bits_ac_chrominance,
181 val_ac_chrominance);
182
183 s->mjpeg_ctx = m;
184 return 0;
185 }
186
187 void mjpeg_close(MpegEncContext *s)
188 {
189 free(s->mjpeg_ctx);
190 }
191
192 static inline void put_marker(PutBitContext *p, int code)
193 {
194 put_bits(p, 8, 0xff);
195 put_bits(p, 8, code);
196 }
197
198 /* table_class: 0 = DC coef, 1 = AC coefs */
199 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
200 const UINT8 *bits_table, const UINT8 *value_table)
201 {
202 PutBitContext *p = &s->pb;
203 int n, i;
204
205 put_bits(p, 4, table_class);
206 put_bits(p, 4, table_id);
207
208 n = 0;
209 for(i=1;i<=16;i++) {
210 n += bits_table[i];
211 put_bits(p, 8, bits_table[i]);
212 }
213
214 for(i=0;i<n;i++)
215 put_bits(p, 8, value_table[i]);
216
217 return n + 17;
218 }
219
220 static void jpeg_table_header(MpegEncContext *s)
221 {
222 PutBitContext *p = &s->pb;
223 int i, size;
224 UINT8 *ptr;
225
226 /* quant matrixes */
227 put_marker(p, DQT);
228 put_bits(p, 16, 2 + 1 * (1 + 64));
229 put_bits(p, 4, 0); /* 8 bit precision */
230 put_bits(p, 4, 0); /* table 0 */
231 for(i=0;i<64;i++) {
232 put_bits(p, 8, s->intra_matrix[i]);
233 }
234 #if 0
235 put_bits(p, 4, 0); /* 8 bit precision */
236 put_bits(p, 4, 1); /* table 1 */
237 for(i=0;i<64;i++) {
238 put_bits(p, 8, s->chroma_intra_matrix[i]);
239 }
240 #endif
241
242 /* huffman table */
243 put_marker(p, DHT);
244 flush_put_bits(p);
245 ptr = p->buf_ptr;
246 put_bits(p, 16, 0); /* patched later */
247 size = 2;
248 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
249 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
250
251 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
252 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
253 ptr[0] = size >> 8;
254 ptr[1] = size;
255 }
256
257 void mjpeg_picture_header(MpegEncContext *s)
258 {
259 put_marker(&s->pb, SOI);
260
261 jpeg_table_header(s);
262
263 put_marker(&s->pb, SOF0);
264
265 put_bits(&s->pb, 16, 17);
266 put_bits(&s->pb, 8, 8); /* 8 bits/component */
267 put_bits(&s->pb, 16, s->height);
268 put_bits(&s->pb, 16, s->width);
269 put_bits(&s->pb, 8, 3); /* 3 components */
270
271 /* Y component */
272 put_bits(&s->pb, 8, 1); /* component number */
273 put_bits(&s->pb, 4, 2); /* H factor */
274 put_bits(&s->pb, 4, 2); /* V factor */
275 put_bits(&s->pb, 8, 0); /* select matrix */
276
277 /* Cb component */
278 put_bits(&s->pb, 8, 2); /* component number */
279 put_bits(&s->pb, 4, 1); /* H factor */
280 put_bits(&s->pb, 4, 1); /* V factor */
281 put_bits(&s->pb, 8, 0); /* select matrix */
282
283 /* Cr component */
284 put_bits(&s->pb, 8, 3); /* component number */
285 put_bits(&s->pb, 4, 1); /* H factor */
286 put_bits(&s->pb, 4, 1); /* V factor */
287 put_bits(&s->pb, 8, 0); /* select matrix */
288
289 /* scan header */
290 put_marker(&s->pb, SOS);
291 put_bits(&s->pb, 16, 12); /* length */
292 put_bits(&s->pb, 8, 3); /* 3 components */
293
294 /* Y component */
295 put_bits(&s->pb, 8, 1); /* index */
296 put_bits(&s->pb, 4, 0); /* DC huffman table index */
297 put_bits(&s->pb, 4, 0); /* AC huffman table index */
298
299 /* Cb component */
300 put_bits(&s->pb, 8, 2); /* index */
301 put_bits(&s->pb, 4, 1); /* DC huffman table index */
302 put_bits(&s->pb, 4, 1); /* AC huffman table index */
303
304 /* Cr component */
305 put_bits(&s->pb, 8, 3); /* index */
306 put_bits(&s->pb, 4, 1); /* DC huffman table index */
307 put_bits(&s->pb, 4, 1); /* AC huffman table index */
308
309 put_bits(&s->pb, 8, 0); /* Ss (not used) */
310 put_bits(&s->pb, 8, 63); /* Se (not used) */
311 put_bits(&s->pb, 8, 0); /* (not used) */
312 }
313
314 void mjpeg_picture_trailer(MpegEncContext *s)
315 {
316 jflush_put_bits(&s->pb);
317 put_marker(&s->pb, EOI);
318 }
319
320 static inline void encode_dc(MpegEncContext *s, int val,
321 UINT8 *huff_size, UINT16 *huff_code)
322 {
323 int mant, nbits;
324
325 if (val == 0) {
326 jput_bits(&s->pb, huff_size[0], huff_code[0]);
327 } else {
328 mant = val;
329 if (val < 0) {
330 val = -val;
331 mant--;
332 }
333
334 /* compute the log (XXX: optimize) */
335 nbits = 0;
336 while (val != 0) {
337 val = val >> 1;
338 nbits++;
339 }
340
341 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
342
343 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
344 }
345 }
346
347 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
348 {
349 int mant, nbits, code, i, j;
350 int component, dc, run, last_index, val;
351 MJpegContext *m = s->mjpeg_ctx;
352 UINT8 *huff_size_ac;
353 UINT16 *huff_code_ac;
354
355 /* DC coef */
356 component = (n <= 3 ? 0 : n - 4 + 1);
357 dc = block[0]; /* overflow is impossible */
358 val = dc - s->last_dc[component];
359 if (n < 4) {
360 encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
361 huff_size_ac = m->huff_size_ac_luminance;
362 huff_code_ac = m->huff_code_ac_luminance;
363 } else {
364 encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
365 huff_size_ac = m->huff_size_ac_chrominance;
366 huff_code_ac = m->huff_code_ac_chrominance;
367 }
368 s->last_dc[component] = dc;
369
370 /* AC coefs */
371
372 run = 0;
373 last_index = s->block_last_index[n];
374 for(i=1;i<=last_index;i++) {
375 j = zigzag_direct[i];
376 val = block[j];
377 if (val == 0) {
378 run++;
379 } else {
380 while (run >= 16) {
381 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
382 run -= 16;
383 }
384 mant = val;
385 if (val < 0) {
386 val = -val;
387 mant--;
388 }
389
390 /* compute the log (XXX: optimize) */
391 nbits = 0;
392 while (val != 0) {
393 val = val >> 1;
394 nbits++;
395 }
396 code = (run << 4) | nbits;
397
398 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
399
400 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
401 run = 0;
402 }
403 }
404
405 /* output EOB only if not already 64 values */
406 if (last_index < 63 || run != 0)
407 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
408 }
409
410 void mjpeg_encode_mb(MpegEncContext *s,
411 DCTELEM block[6][64])
412 {
413 int i;
414 for(i=0;i<6;i++) {
415 encode_block(s, block[i], i);
416 }
417 }
418
419 /******************************************/
420 /* decoding */
421
422 //#define DEBUG
423
424 #ifdef DEBUG
425 #define dprintf(fmt,args...) printf(fmt, ## args)
426 #else
427 #define dprintf(fmt,args...)
428 #endif
429
430 /* compressed picture size */
431 #define PICTURE_BUFFER_SIZE 100000
432
433 #define MAX_COMPONENTS 4
434
435 typedef struct MJpegDecodeContext {
436 GetBitContext gb;
437 UINT32 header_state;
438 int start_code; /* current start code */
439 UINT8 *buf_ptr;
440 int buffer_size;
441 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
442 INT16 quant_matrixes[4][64];
443 VLC vlcs[2][4];
444 int width, height;
445 int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
446 int v_count[MAX_COMPONENTS];
447 int h_max, v_max; /* maximum h and v counts */
448 int quant_index[4]; /* quant table index for each component */
449 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
450 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
451 int linesize[MAX_COMPONENTS];
452 DCTELEM block[64] __align8;
453 UINT8 buffer[PICTURE_BUFFER_SIZE];
454 } MJpegDecodeContext;
455
456 static int mjpeg_decode_init(AVCodecContext *avctx)
457 {
458 MJpegDecodeContext *s = avctx->priv_data;
459
460 s->header_state = 0;
461 s->mpeg_enc_ctx_allocated = 0;
462 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
463 account FF 00 case */
464 s->start_code = -1;
465 s->buf_ptr = s->buffer;
466 return 0;
467 }
468
469 /* quantize tables */
470 static int mjpeg_decode_dqt(MJpegDecodeContext *s,
471 UINT8 *buf, int buf_size)
472 {
473 int len, index, i;
474 init_get_bits(&s->gb, buf, buf_size);
475
476 len = get_bits(&s->gb, 16);
477 len -= 2;
478
479 while (len >= 65) {
480 /* only 8 bit precision handled */
481 if (get_bits(&s->gb, 4) != 0)
482 return -1;
483 index = get_bits(&s->gb, 4);
484 if (index >= 4)
485 return -1;
486 dprintf("index=%d\n", index);
487 /* read quant table */
488 for(i=0;i<64;i++)
489 s->quant_matrixes[index][i] = get_bits(&s->gb, 8);
490 len -= 65;
491 }
492 return 0;
493 }
494
495 /* decode huffman tables and build VLC decoders */
496 static int mjpeg_decode_dht(MJpegDecodeContext *s,
497 UINT8 *buf, int buf_size)
498 {
499 int len, index, i, class, n, v, code_max;
500 UINT8 bits_table[17];
501 UINT8 val_table[256];
502 UINT8 huff_size[256];
503 UINT16 huff_code[256];
504
505 init_get_bits(&s->gb, buf, buf_size);
506
507 len = get_bits(&s->gb, 16);
508 len -= 2;
509
510 while (len > 0) {
511 if (len < 17)
512 return -1;
513 class = get_bits(&s->gb, 4);
514 if (class >= 2)
515 return -1;
516 index = get_bits(&s->gb, 4);
517 if (index >= 4)
518 return -1;
519 n = 0;
520 for(i=1;i<=16;i++) {
521 bits_table[i] = get_bits(&s->gb, 8);
522 n += bits_table[i];
523 }
524 len -= 17;
525 if (len < n || n > 256)
526 return -1;
527
528 code_max = 0;
529 for(i=0;i<n;i++) {
530 v = get_bits(&s->gb, 8);
531 if (v > code_max)
532 code_max = v;
533 val_table[i] = v;
534 }
535 len -= n;
536
537 /* now build size/code table */
538 memset(huff_size, 0, sizeof(huff_size));
539 build_huffman_codes(huff_size, huff_code, bits_table, val_table);
540
541 /* build VLC and flush previous vlc if present */
542 free_vlc(&s->vlcs[class][index]);
543 dprintf("class=%d index=%d nb_codes=%d\n",
544 class, index, code_max + 1);
545 init_vlc(&s->vlcs[class][index], 9, code_max + 1,
546 huff_size, 1, 1,
547 huff_code, 2, 2);
548 }
549 return 0;
550 }
551
552 static int mjpeg_decode_sof0(MJpegDecodeContext *s,
553 UINT8 *buf, int buf_size)
554 {
555 int len, nb_components, i, index, width, height;
556
557 init_get_bits(&s->gb, buf, buf_size);
558
559 /* XXX: verify len field validity */
560 len = get_bits(&s->gb, 16);
561 /* only 8 bits/component accepted */
562 if (get_bits(&s->gb, 8) != 8)
563 return -1;
564 height = get_bits(&s->gb, 16);
565 width = get_bits(&s->gb, 16);
566
567 nb_components = get_bits(&s->gb, 8);
568 if (nb_components <= 0 ||
569 nb_components > MAX_COMPONENTS)
570 return -1;
571 s->h_max = 1;
572 s->v_max = 1;
573 for(i=0;i<nb_components;i++) {
574 /* component id */
575 index = get_bits(&s->gb, 8) - 1;
576 /* XXX: avoid this limitation */
577 if (index < 0 || index >= MAX_COMPONENTS)
578 return -1;
579 s->h_count[i] = get_bits(&s->gb, 4);
580 s->v_count[i] = get_bits(&s->gb, 4);
581 /* compute hmax and vmax (only used in interleaved case) */
582 if (s->h_count[i] > s->h_max)
583 s->h_max = s->h_count[i];
584 if (s->v_count[i] > s->v_max)
585 s->v_max = s->v_count[i];
586
587 /* XXX: only 420 is accepted */
588 if ((i == 0 && (s->h_count[i] != 2 || s->v_count[i] != 2)) ||
589 (i != 0 && (s->h_count[i] != 1 || s->v_count[i] != 1)))
590 return -1;
591 s->quant_index[i] = get_bits(&s->gb, 8);
592 if (s->quant_index[i] >= 4)
593 return -1;
594 dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
595 }
596
597 /* if different size, realloc/alloc picture */
598 /* XXX: also check h_count and v_count */
599 if (width != s->width || height != s->height) {
600 for(i=0;i<MAX_COMPONENTS;i++) {
601 free(s->current_picture[i]);
602 s->current_picture[i] = NULL;
603 }
604 s->width = width;
605 s->height = height;
606 for(i=0;i<nb_components;i++) {
607 int w, h, hh, vv;
608 hh = s->h_max / s->h_count[i];
609 vv = s->v_max / s->v_count[i];
610 w = (s->width + 8 * hh - 1) / (8 * hh);
611 h = (s->height + 8 * vv - 1) / (8 * vv);
612 w = w * 8;
613 h = h * 8;
614 s->linesize[i] = w;
615 /* memory test is done in mjpeg_decode_sos() */
616 s->current_picture[i] = av_mallocz(w * h);
617 }
618 }
619
620 return 0;
621 }
622
623 static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
624 {
625 VLC *dc_vlc;
626 int code, diff;
627
628 dc_vlc = &s->vlcs[0][dc_index];
629 code = get_vlc(&s->gb, dc_vlc);
630 if (code < 0)
631 return 0xffff;
632 if (code == 0) {
633 diff = 0;
634 } else {
635 diff = get_bits(&s->gb, code);
636 if ((diff & (1 << (code - 1))) == 0)
637 diff = (-1 << code) | (diff + 1);
638 }
639 return diff;
640 }
641
642 /* decode block and dequantize */
643 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
644 int component, int dc_index, int ac_index, int quant_index)
645 {
646 int nbits, code, i, j, level;
647 int run, val;
648 VLC *ac_vlc;
649 INT16 *quant_matrix;
650
651 quant_matrix = s->quant_matrixes[quant_index];
652 /* DC coef */
653 val = decode_dc(s, dc_index);
654 if (val == 0xffff) {
655 dprintf("error dc\n");
656 return -1;
657 }
658 val = val * quant_matrix[0] + s->last_dc[component];
659 s->last_dc[component] = val;
660 block[0] = val;
661
662 /* AC coefs */
663 ac_vlc = &s->vlcs[1][ac_index];
664 i = 1;
665 for(;;) {
666 code = get_vlc(&s->gb, ac_vlc);
667 if (code < 0) {
668 dprintf("error ac\n");
669 return -1;
670 }
671 /* EOB */
672 if (code == 0)
673 break;
674 if (code == 0xf0) {
675 i += 16;
676 } else {
677 run = code >> 4;
678 nbits = code & 0xf;
679 level = get_bits(&s->gb, nbits);
680 if ((level & (1 << (nbits - 1))) == 0)
681 level = (-1 << nbits) | (level + 1);
682 i += run;
683 if (i >= 64) {
684 dprintf("error count: %d\n", i);
685 return -1;
686 }
687 j = zigzag_direct[i];
688 block[j] = level * quant_matrix[j];
689 i++;
690 }
691 }
692 return 0;
693 }
694
695 static int mjpeg_decode_sos(MJpegDecodeContext *s,
696 UINT8 *buf, int buf_size)
697 {
698 int len, nb_components, i, j, n, h, v;
699 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index;
700 int comp_index[4];
701 int dc_index[4];
702 int ac_index[4];
703 int nb_blocks[4];
704 int h_count[4];
705 int v_count[4];
706
707 init_get_bits(&s->gb, buf, buf_size);
708 /* XXX: verify len field validity */
709 len = get_bits(&s->gb, 16);
710 nb_components = get_bits(&s->gb, 8);
711 /* XXX: only interleaved scan accepted */
712 if (nb_components != 3)
713 return -1;
714 vmax = 0;
715 hmax = 0;
716 for(i=0;i<nb_components;i++) {
717 index = get_bits(&s->gb, 8) - 1;
718 /* XXX: this limitation is not OK */
719 if (index < 0 || index >= 4)
720 return -1;
721 comp_index[i] = index;
722
723 nb_blocks[i] = s->h_count[index] * s->v_count[index];
724 h_count[i] = s->h_count[index];
725 v_count[i] = s->v_count[index];
726
727 dc_index[i] = get_bits(&s->gb, 4);
728 if (dc_index[i] >= 4)
729 return -1;
730 ac_index[i] = get_bits(&s->gb, 4);
731 if (ac_index[i] >= 4)
732 return -1;
733 }
734 get_bits(&s->gb, 8); /* Ss */
735 get_bits(&s->gb, 8); /* Se */
736 get_bits(&s->gb, 8); /* not used */
737
738 for(i=0;i<nb_components;i++)
739 s->last_dc[i] = 1024;
740
741 if (nb_components > 1) {
742 /* interleaved stream */
743 mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
744 mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
745 } else {
746 h = s->h_max / s->h_count[comp_index[0]];
747 v = s->v_max / s->v_count[comp_index[0]];
748 mb_width = (s->width + h * 8 - 1) / (h * 8);
749 mb_height = (s->height + v * 8 - 1) / (v * 8);
750 nb_blocks[0] = 1;
751 h_count[0] = 1;
752 v_count[0] = 1;
753 }
754
755 for(mb_y = 0; mb_y < mb_height; mb_y++) {
756 for(mb_x = 0; mb_x < mb_width; mb_x++) {
757 for(i=0;i<nb_components;i++) {
758 UINT8 *ptr;
759 int x, y, c;
760 n = nb_blocks[i];
761 c = comp_index[i];
762 h = h_count[i];
763 v = v_count[i];
764 x = 0;
765 y = 0;
766 for(j=0;j<n;j++) {
767 memset(s->block, 0, sizeof(s->block));
768 if (decode_block(s, s->block, i,
769 dc_index[i], ac_index[i],
770 s->quant_index[c]) < 0) {
771 dprintf("error %d %d\n", mb_y, mb_x);
772 return -1;
773 }
774 ff_idct (s->block);
775 ptr = s->current_picture[c] +
776 (s->linesize[c] * (v * mb_y + y) * 8) +
777 (h * mb_x + x) * 8;
778 put_pixels_clamped(s->block, ptr, s->linesize[c]);
779 if (++x == h) {
780 x = 0;
781 y++;
782 }
783 }
784 }
785 }
786 }
787 return 0;
788 }
789
790 /* return the 8 bit start code value and update the search
791 state. Return -1 if no start code found */
792 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
793 UINT32 *header_state)
794 {
795 UINT8 *buf_ptr;
796 unsigned int state, v;
797 int val;
798
799 state = *header_state;
800 buf_ptr = *pbuf_ptr;
801 if (state) {
802 /* get marker */
803 found:
804 if (buf_ptr < buf_end) {
805 val = *buf_ptr++;
806 state = 0;
807 } else {
808 val = -1;
809 }
810 } else {
811 while (buf_ptr < buf_end) {
812 v = *buf_ptr++;
813 if (v == 0xff) {
814 state = 1;
815 goto found;
816 }
817 }
818 val = -1;
819 }
820 *pbuf_ptr = buf_ptr;
821 *header_state = state;
822 return val;
823 }
824
825 static int mjpeg_decode_frame(AVCodecContext *avctx,
826 void *data, int *data_size,
827 UINT8 *buf, int buf_size)
828 {
829 MJpegDecodeContext *s = avctx->priv_data;
830 UINT8 *buf_end, *buf_ptr, *buf_start;
831 int len, code, start_code, input_size, i;
832 AVPicture *picture = data;
833
834 /* no supplementary picture */
835 if (buf_size == 0) {
836 *data_size = 0;
837 return 0;
838 }
839
840 buf_ptr = buf;
841 buf_end = buf + buf_size;
842 while (buf_ptr < buf_end) {
843 buf_start = buf_ptr;
844 /* find start next marker */
845 code = find_marker(&buf_ptr, buf_end, &s->header_state);
846 /* copy to buffer */
847 len = buf_ptr - buf_start;
848 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
849 /* data too big : flush */
850 s->buf_ptr = s->buffer;
851 if (code > 0)
852 s->start_code = code;
853 } else {
854 memcpy(s->buf_ptr, buf_start, len);
855 s->buf_ptr += len;
856 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
857 if (code == 0) {
858 s->buf_ptr--;
859 } else if (code > 0) {
860 /* prepare data for next start code */
861 input_size = s->buf_ptr - s->buffer;
862 start_code = s->start_code;
863 s->buf_ptr = s->buffer;
864 s->start_code = code;
865 switch(start_code) {
866 case SOI:
867 /* nothing to do on SOI */
868 break;
869 case DQT:
870 mjpeg_decode_dqt(s, s->buffer, input_size);
871 break;
872 case DHT:
873 mjpeg_decode_dht(s, s->buffer, input_size);
874 break;
875 case SOF0:
876 mjpeg_decode_sof0(s, s->buffer, input_size);
877 break;
878 case SOS:
879 mjpeg_decode_sos(s, s->buffer, input_size);
880 if (s->start_code == EOI) {
881 /* XXX: YUV420 hardcoded */
882 for(i=0;i<3;i++) {
883 picture->data[i] = s->current_picture[i];
884 picture->linesize[i] = s->linesize[i];
885 }
886 *data_size = sizeof(AVPicture);
887 avctx->height = s->height;
888 avctx->width = s->width;
889 avctx->pix_fmt = PIX_FMT_YUV420P;
890 goto the_end;
891 }
892 break;
893 }
894 }
895 }
896 }
897 the_end:
898 return buf_ptr - buf;
899 }
900
901 static int mjpeg_decode_end(AVCodecContext *avctx)
902 {
903 MJpegDecodeContext *s = avctx->priv_data;
904 int i, j;
905
906 for(i=0;i<MAX_COMPONENTS;i++)
907 free(s->current_picture[i]);
908 for(i=0;i<2;i++) {
909 for(j=0;j<4;j++)
910 free_vlc(&s->vlcs[i][j]);
911 }
912 return 0;
913 }
914
915 AVCodec mjpeg_decoder = {
916 "mjpeg",
917 CODEC_TYPE_VIDEO,
918 CODEC_ID_MJPEG,
919 sizeof(MJpegDecodeContext),
920 mjpeg_decode_init,
921 NULL,
922 mjpeg_decode_end,
923 mjpeg_decode_frame,
924 };