Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mjpeg.c @ 05f361f0

History | View | Annotate | Download (29.7 KB)

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
//#define DEBUG
20
#include "avcodec.h"
21
#include "dsputil.h"
22
#include "mpegvideo.h"
23

    
24
typedef struct MJpegContext {
25
    UINT8 huff_size_dc_luminance[12];
26
    UINT16 huff_code_dc_luminance[12];
27
    UINT8 huff_size_dc_chrominance[12];
28
    UINT16 huff_code_dc_chrominance[12];
29

    
30
    UINT8 huff_size_ac_luminance[256];
31
    UINT16 huff_code_ac_luminance[256];
32
    UINT8 huff_size_ac_chrominance[256];
33
    UINT16 huff_code_ac_chrominance[256];
34
} MJpegContext;
35

    
36
#define SOF0 0xc0
37
#define SOI 0xd8
38
#define EOI 0xd9
39
#define DQT 0xdb
40
#define DHT 0xc4
41
#define SOS 0xda
42

    
43
#if 0
44
/* These are the sample quantization tables given in JPEG spec section K.1.
45
 * The spec says that the values given produce "good" quality, and
46
 * when divided by 2, "very good" quality.
47
 */
48
static const unsigned char std_luminance_quant_tbl[64] = {
49
    16,  11,  10,  16,  24,  40,  51,  61,
50
    12,  12,  14,  19,  26,  58,  60,  55,
51
    14,  13,  16,  24,  40,  57,  69,  56,
52
    14,  17,  22,  29,  51,  87,  80,  62,
53
    18,  22,  37,  56,  68, 109, 103,  77,
54
    24,  35,  55,  64,  81, 104, 113,  92,
55
    49,  64,  78,  87, 103, 121, 120, 101,
56
    72,  92,  95,  98, 112, 100, 103,  99
57
};
58
static const unsigned char std_chrominance_quant_tbl[64] = {
59
    17,  18,  24,  47,  99,  99,  99,  99,
60
    18,  21,  26,  66,  99,  99,  99,  99,
61
    24,  26,  56,  99,  99,  99,  99,  99,
62
    47,  66,  99,  99,  99,  99,  99,  99,
63
    99,  99,  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
};
68
#endif
69

    
70
/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
71
/* IMPORTANT: these are only valid for 8-bit data precision! */
72
static const UINT8 bits_dc_luminance[17] =
73
{ /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
74
static const UINT8 val_dc_luminance[] =
75
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
76

    
77
static const UINT8 bits_dc_chrominance[17] =
78
{ /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
79
static const UINT8 val_dc_chrominance[] =
80
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
81

    
82
static const UINT8 bits_ac_luminance[17] =
83
{ /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
84
static const UINT8 val_ac_luminance[] =
85
{ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
86
  0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
87
  0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
88
  0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
89
  0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
90
  0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
91
  0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
92
  0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
93
  0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
94
  0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
95
  0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
96
  0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
97
  0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
98
  0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
99
  0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
100
  0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
101
  0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
102
  0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
103
  0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
104
  0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
105
  0xf9, 0xfa 
106
};
107

    
108
static const UINT8 bits_ac_chrominance[17] =
109
{ /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
110

    
111
static const UINT8 val_ac_chrominance[] =
112
{ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
113
  0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
114
  0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
115
  0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
116
  0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
117
  0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
118
  0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
119
  0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
120
  0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
121
  0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
122
  0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
123
  0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
124
  0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
125
  0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
126
  0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
127
  0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
128
  0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
129
  0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
130
  0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
131
  0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
132
  0xf9, 0xfa 
133
};
134

    
135

    
136
/* isn't this function nicer than the one in the libjpeg ? */
137
static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
138
                                const UINT8 *bits_table, const UINT8 *val_table)
139
{
140
    int i, j, k,nb, code, sym;
141

    
142
    code = 0;
143
    k = 0;
144
    for(i=1;i<=16;i++) {
145
        nb = bits_table[i];
146
        for(j=0;j<nb;j++) {
147
            sym = val_table[k++];
148
            huff_size[sym] = i;
149
            huff_code[sym] = code;
150
            code++;
151
        }
152
        code <<= 1;
153
    }
154
}
155

    
156
int mjpeg_init(MpegEncContext *s)
157
{
158
    MJpegContext *m;
159
    
160
    m = malloc(sizeof(MJpegContext));
161
    if (!m)
162
        return -1;
163

    
164
    /* build all the huffman tables */
165
    build_huffman_codes(m->huff_size_dc_luminance,
166
                        m->huff_code_dc_luminance,
167
                        bits_dc_luminance,
168
                        val_dc_luminance);
169
    build_huffman_codes(m->huff_size_dc_chrominance,
170
                        m->huff_code_dc_chrominance,
171
                        bits_dc_chrominance,
172
                        val_dc_chrominance);
173
    build_huffman_codes(m->huff_size_ac_luminance,
174
                        m->huff_code_ac_luminance,
175
                        bits_ac_luminance,
176
                        val_ac_luminance);
177
    build_huffman_codes(m->huff_size_ac_chrominance,
178
                        m->huff_code_ac_chrominance,
179
                        bits_ac_chrominance,
180
                        val_ac_chrominance);
181
    
182
    s->mjpeg_ctx = m;
183
    return 0;
184
}
185

    
186
void mjpeg_close(MpegEncContext *s)
187
{
188
    free(s->mjpeg_ctx);
189
}
190

    
191
static inline void put_marker(PutBitContext *p, int code)
192
{
193
    put_bits(p, 8, 0xff);
194
    put_bits(p, 8, code);
195
}
196

    
197
/* table_class: 0 = DC coef, 1 = AC coefs */
198
static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
199
                             const UINT8 *bits_table, const UINT8 *value_table)
200
{
201
    PutBitContext *p = &s->pb;
202
    int n, i;
203

    
204
    put_bits(p, 4, table_class);
205
    put_bits(p, 4, table_id);
206

    
207
    n = 0;
208
    for(i=1;i<=16;i++) {
209
        n += bits_table[i];
210
        put_bits(p, 8, bits_table[i]);
211
    }
212

    
213
    for(i=0;i<n;i++)
214
        put_bits(p, 8, value_table[i]);
215

    
216
    return n + 17;
217
}
218

    
219
static void jpeg_table_header(MpegEncContext *s)
220
{
221
    PutBitContext *p = &s->pb;
222
    int i, j, size;
223
    UINT8 *ptr;
224

    
225
    /* quant matrixes */
226
    put_marker(p, DQT);
227
    put_bits(p, 16, 2 + 1 * (1 + 64));
228
    put_bits(p, 4, 0); /* 8 bit precision */
229
    put_bits(p, 4, 0); /* table 0 */
230
    for(i=0;i<64;i++) {
231
        j = zigzag_direct[i];
232
        put_bits(p, 8, s->intra_matrix[j]);
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
        j = zigzag_direct[i];
239
        put_bits(p, 8, s->chroma_intra_matrix[j]);
240
    }
241
#endif
242

    
243
    /* huffman table */
244
    put_marker(p, DHT);
245
    flush_put_bits(p);
246
    ptr = p->buf_ptr;
247
    put_bits(p, 16, 0); /* patched later */
248
    size = 2;
249
    size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
250
    size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
251
    
252
    size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
253
    size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
254
    ptr[0] = size >> 8;
255
    ptr[1] = size;
256
}
257

    
258
void mjpeg_picture_header(MpegEncContext *s)
259
{
260
    put_marker(&s->pb, SOI);
261

    
262
    jpeg_table_header(s);
263

    
264
    put_marker(&s->pb, SOF0);
265

    
266
    put_bits(&s->pb, 16, 17);
267
    put_bits(&s->pb, 8, 8); /* 8 bits/component */
268
    put_bits(&s->pb, 16, s->height);
269
    put_bits(&s->pb, 16, s->width);
270
    put_bits(&s->pb, 8, 3); /* 3 components */
271
    
272
    /* Y component */
273
    put_bits(&s->pb, 8, 1); /* component number */
274
    put_bits(&s->pb, 4, 2); /* H factor */
275
    put_bits(&s->pb, 4, 2); /* V factor */
276
    put_bits(&s->pb, 8, 0); /* select matrix */
277
    
278
    /* Cb component */
279
    put_bits(&s->pb, 8, 2); /* component number */
280
    put_bits(&s->pb, 4, 1); /* H factor */
281
    put_bits(&s->pb, 4, 1); /* V factor */
282
    put_bits(&s->pb, 8, 0); /* select matrix */
283

    
284
    /* Cr component */
285
    put_bits(&s->pb, 8, 3); /* component number */
286
    put_bits(&s->pb, 4, 1); /* H factor */
287
    put_bits(&s->pb, 4, 1); /* V factor */
288
    put_bits(&s->pb, 8, 0); /* select matrix */
289

    
290
    /* scan header */
291
    put_marker(&s->pb, SOS);
292
    put_bits(&s->pb, 16, 12); /* length */
293
    put_bits(&s->pb, 8, 3); /* 3 components */
294
    
295
    /* Y component */
296
    put_bits(&s->pb, 8, 1); /* index */
297
    put_bits(&s->pb, 4, 0); /* DC huffman table index */
298
    put_bits(&s->pb, 4, 0); /* AC huffman table index */
299
    
300
    /* Cb component */
301
    put_bits(&s->pb, 8, 2); /* index */
302
    put_bits(&s->pb, 4, 1); /* DC huffman table index */
303
    put_bits(&s->pb, 4, 1); /* AC huffman table index */
304
    
305
    /* Cr component */
306
    put_bits(&s->pb, 8, 3); /* index */
307
    put_bits(&s->pb, 4, 1); /* DC huffman table index */
308
    put_bits(&s->pb, 4, 1); /* AC huffman table index */
309

    
310
    put_bits(&s->pb, 8, 0); /* Ss (not used) */
311
    put_bits(&s->pb, 8, 63); /* Se (not used) */
312
    put_bits(&s->pb, 8, 0); /* (not used) */
313
}
314

    
315
void mjpeg_picture_trailer(MpegEncContext *s)
316
{
317
    jflush_put_bits(&s->pb);
318
    put_marker(&s->pb, EOI);
319
}
320

    
321
static inline void encode_dc(MpegEncContext *s, int val, 
322
                             UINT8 *huff_size, UINT16 *huff_code)
323
{
324
    int mant, nbits;
325

    
326
    if (val == 0) {
327
        jput_bits(&s->pb, huff_size[0], huff_code[0]);
328
    } else {
329
        mant = val;
330
        if (val < 0) {
331
            val = -val;
332
            mant--;
333
        }
334
        
335
        /* compute the log (XXX: optimize) */
336
        nbits = 0;
337
        while (val != 0) {
338
            val = val >> 1;
339
            nbits++;
340
        }
341
            
342
        jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
343
        
344
        jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
345
    }
346
}
347

    
348
static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
349
{
350
    int mant, nbits, code, i, j;
351
    int component, dc, run, last_index, val;
352
    MJpegContext *m = s->mjpeg_ctx;
353
    UINT8 *huff_size_ac;
354
    UINT16 *huff_code_ac;
355
    
356
    /* DC coef */
357
    component = (n <= 3 ? 0 : n - 4 + 1);
358
    dc = block[0]; /* overflow is impossible */
359
    val = dc - s->last_dc[component];
360
    if (n < 4) {
361
        encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
362
        huff_size_ac = m->huff_size_ac_luminance;
363
        huff_code_ac = m->huff_code_ac_luminance;
364
    } else {
365
        encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
366
        huff_size_ac = m->huff_size_ac_chrominance;
367
        huff_code_ac = m->huff_code_ac_chrominance;
368
    }
369
    s->last_dc[component] = dc;
370
    
371
    /* AC coefs */
372
    
373
    run = 0;
374
    last_index = s->block_last_index[n];
375
    for(i=1;i<=last_index;i++) {
376
        j = zigzag_direct[i];
377
        val = block[j];
378
        if (val == 0) {
379
            run++;
380
        } else {
381
            while (run >= 16) {
382
                jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
383
                run -= 16;
384
            }
385
            mant = val;
386
            if (val < 0) {
387
                val = -val;
388
                mant--;
389
            }
390
            
391
            /* compute the log (XXX: optimize) */
392
            nbits = 0;
393
            while (val != 0) {
394
                val = val >> 1;
395
                nbits++;
396
            }
397
            code = (run << 4) | nbits;
398

    
399
            jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
400
        
401
            jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
402
            run = 0;
403
        }
404
    }
405

    
406
    /* output EOB only if not already 64 values */
407
    if (last_index < 63 || run != 0)
408
        jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
409
}
410

    
411
void mjpeg_encode_mb(MpegEncContext *s, 
412
                     DCTELEM block[6][64])
413
{
414
    int i;
415
    for(i=0;i<6;i++) {
416
        encode_block(s, block[i], i);
417
    }
418
}
419

    
420
/******************************************/
421
/* decoding */
422

    
423
/* compressed picture size */
424
#define PICTURE_BUFFER_SIZE 100000
425

    
426
#define MAX_COMPONENTS 4
427

    
428
typedef struct MJpegDecodeContext {
429
    GetBitContext gb;
430
    UINT32 header_state;
431
    int start_code; /* current start code */
432
    UINT8 *buf_ptr;
433
    int buffer_size;
434
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
435
    INT16 quant_matrixes[4][64];
436
    VLC vlcs[2][4];
437

    
438
    int org_width, org_height;  /* size given at codec init */
439
    int first_picture;    /* true if decoding first picture */
440
    int interlaced;     /* true if interlaced */
441
    int bottom_field;   /* true if bottom field */
442

    
443
    int width, height;
444
    int nb_components;
445
    int component_id[MAX_COMPONENTS];
446
    int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
447
    int v_count[MAX_COMPONENTS];
448
    int h_max, v_max; /* maximum h and v counts */
449
    int quant_index[4];   /* quant table index for each component */
450
    int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
451
    UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
452
    int linesize[MAX_COMPONENTS];
453
    DCTELEM block[64] __align8;
454
    UINT8 buffer[PICTURE_BUFFER_SIZE]; 
455
} MJpegDecodeContext;
456

    
457
static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table, 
458
                      int nb_codes)
459
{
460
    UINT8 huff_size[256];
461
    UINT16 huff_code[256];
462

    
463
    memset(huff_size, 0, sizeof(huff_size));
464
    build_huffman_codes(huff_size, huff_code, bits_table, val_table);
465
    
466
    init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
467
}
468

    
469
static int mjpeg_decode_init(AVCodecContext *avctx)
470
{
471
    MJpegDecodeContext *s = avctx->priv_data;
472

    
473
    s->header_state = 0;
474
    s->mpeg_enc_ctx_allocated = 0;
475
    s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
476
                                                 account FF 00 case */
477
    s->start_code = -1;
478
    s->buf_ptr = s->buffer;
479
    s->first_picture = 1;
480
    s->org_width = avctx->width;
481
    s->org_height = avctx->height;
482

    
483
    build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
484
    build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
485
    build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
486
    build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
487
    return 0;
488
}
489

    
490
/* quantize tables */
491
static int mjpeg_decode_dqt(MJpegDecodeContext *s,
492
                            UINT8 *buf, int buf_size)
493
{
494
    int len, index, i, j;
495
    init_get_bits(&s->gb, buf, buf_size);
496

    
497
    len = get_bits(&s->gb, 16);
498
    len -= 2;
499

    
500
    while (len >= 65) {
501
        /* only 8 bit precision handled */
502
        if (get_bits(&s->gb, 4) != 0)
503
            return -1;
504
        index = get_bits(&s->gb, 4);
505
        if (index >= 4)
506
            return -1;
507
        dprintf("index=%d\n", index);
508
        /* read quant table */
509
        for(i=0;i<64;i++) {
510
            j = zigzag_direct[i];
511
            s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
512
        }
513
        len -= 65;
514
    }
515
    return 0;
516
}
517

    
518
/* decode huffman tables and build VLC decoders */
519
static int mjpeg_decode_dht(MJpegDecodeContext *s,
520
                            UINT8 *buf, int buf_size)
521
{
522
    int len, index, i, class, n, v, code_max;
523
    UINT8 bits_table[17];
524
    UINT8 val_table[256];
525
    
526
    init_get_bits(&s->gb, buf, buf_size);
527

    
528
    len = get_bits(&s->gb, 16);
529
    len -= 2;
530

    
531
    while (len > 0) {
532
        if (len < 17)
533
            return -1;
534
        class = get_bits(&s->gb, 4);
535
        if (class >= 2)
536
            return -1;
537
        index = get_bits(&s->gb, 4);
538
        if (index >= 4)
539
            return -1;
540
        n = 0;
541
        for(i=1;i<=16;i++) {
542
            bits_table[i] = get_bits(&s->gb, 8);
543
            n += bits_table[i];
544
        }
545
        len -= 17;
546
        if (len < n || n > 256)
547
            return -1;
548

    
549
        code_max = 0;
550
        for(i=0;i<n;i++) {
551
            v = get_bits(&s->gb, 8);
552
            if (v > code_max)
553
                code_max = v;
554
            val_table[i] = v;
555
        }
556
        len -= n;
557

    
558
        /* build VLC and flush previous vlc if present */
559
        free_vlc(&s->vlcs[class][index]);
560
        dprintf("class=%d index=%d nb_codes=%d\n",
561
               class, index, code_max + 1);
562
        build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
563
    }
564
    return 0;
565
}
566

    
567
static int mjpeg_decode_sof0(MJpegDecodeContext *s,
568
                             UINT8 *buf, int buf_size)
569
{
570
    int len, nb_components, i, width, height;
571

    
572
    init_get_bits(&s->gb, buf, buf_size);
573

    
574
    /* XXX: verify len field validity */
575
    len = get_bits(&s->gb, 16);
576
    /* only 8 bits/component accepted */
577
    if (get_bits(&s->gb, 8) != 8)
578
        return -1;
579
    height = get_bits(&s->gb, 16);
580
    width = get_bits(&s->gb, 16);
581

    
582
    nb_components = get_bits(&s->gb, 8);
583
    if (nb_components <= 0 ||
584
        nb_components > MAX_COMPONENTS)
585
        return -1;
586
    s->nb_components = nb_components;
587
    s->h_max = 1;
588
    s->v_max = 1;
589
    for(i=0;i<nb_components;i++) {
590
        /* component id */
591
        s->component_id[i] = get_bits(&s->gb, 8) - 1;
592
        s->h_count[i] = get_bits(&s->gb, 4);
593
        s->v_count[i] = get_bits(&s->gb, 4);
594
        /* compute hmax and vmax (only used in interleaved case) */
595
        if (s->h_count[i] > s->h_max)
596
            s->h_max = s->h_count[i];
597
        if (s->v_count[i] > s->v_max)
598
            s->v_max = s->v_count[i];
599
        s->quant_index[i] = get_bits(&s->gb, 8);
600
        if (s->quant_index[i] >= 4)
601
            return -1;
602
        dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
603
    }
604

    
605
    /* if different size, realloc/alloc picture */
606
    /* XXX: also check h_count and v_count */
607
    if (width != s->width || height != s->height) {
608
        for(i=0;i<MAX_COMPONENTS;i++) {
609
            free(s->current_picture[i]);
610
            s->current_picture[i] = NULL;
611
        }
612
        s->width = width;
613
        s->height = height;
614
        /* test interlaced mode */
615
        if (s->first_picture &&
616
            s->org_height != 0 &&
617
            s->height < ((s->org_height * 3) / 4)) {
618
            s->interlaced = 1;
619
            s->bottom_field = 0;
620
        }
621

    
622
        for(i=0;i<nb_components;i++) {
623
            int w, h, hh, vv;
624
            hh = s->h_max / s->h_count[i];
625
            vv = s->v_max / s->v_count[i];
626
            w = (s->width + 8 * hh - 1) / (8 * hh);
627
            h = (s->height + 8 * vv - 1) / (8 * vv);
628
            w = w * 8;
629
            h = h * 8;
630
            if (s->interlaced)
631
                w *= 2;
632
            s->linesize[i] = w;
633
            /* memory test is done in mjpeg_decode_sos() */
634
            s->current_picture[i] = av_mallocz(w * h);
635
        }
636
        s->first_picture = 0;
637
    }
638
    
639
    return 0;
640
}
641

    
642
static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
643
{
644
    VLC *dc_vlc;
645
    int code, diff;
646

    
647
    dc_vlc = &s->vlcs[0][dc_index];
648
    code = get_vlc(&s->gb, dc_vlc);
649
    if (code < 0)
650
        return 0xffff;
651
    if (code == 0) {
652
        diff = 0;
653
    } else {
654
        diff = get_bits(&s->gb, code);
655
        if ((diff & (1 << (code - 1))) == 0) 
656
            diff = (-1 << code) | (diff + 1);
657
    }
658
    return diff;
659
}
660

    
661
/* decode block and dequantize */
662
static int decode_block(MJpegDecodeContext *s, DCTELEM *block, 
663
                        int component, int dc_index, int ac_index, int quant_index)
664
{
665
    int nbits, code, i, j, level;
666
    int run, val;
667
    VLC *ac_vlc;
668
    INT16 *quant_matrix;
669

    
670
    quant_matrix = s->quant_matrixes[quant_index];
671
    /* DC coef */
672
    val = decode_dc(s, dc_index);
673
    if (val == 0xffff) {
674
        dprintf("error dc\n");
675
        return -1;
676
    }
677
    val = val * quant_matrix[0] + s->last_dc[component];
678
    s->last_dc[component] = val;
679
    block[0] = val;
680
    /* AC coefs */
681
    ac_vlc = &s->vlcs[1][ac_index];
682
    i = 1;
683
    for(;;) {
684
        code = get_vlc(&s->gb, ac_vlc);
685
        if (code < 0) {
686
            dprintf("error ac\n");
687
            return -1;
688
        }
689
        /* EOB */
690
        if (code == 0)
691
            break;
692
        if (code == 0xf0) {
693
            i += 16;
694
        } else {
695
            run = code >> 4;
696
            nbits = code & 0xf;
697
            level = get_bits(&s->gb, nbits);
698
            if ((level & (1 << (nbits - 1))) == 0) 
699
                level = (-1 << nbits) | (level + 1);
700
            i += run;
701
            if (i >= 64) {
702
                dprintf("error count: %d\n", i);
703
                return -1;
704
            }
705
            j = zigzag_direct[i];
706
            block[j] = level * quant_matrix[j];
707
            i++;
708
            if (i >= 64)
709
                break;
710
        }
711
    }
712
    return 0;
713
}
714

    
715
static int mjpeg_decode_sos(MJpegDecodeContext *s,
716
                            UINT8 *buf, int buf_size)
717
{
718
    int len, nb_components, i, j, n, h, v, ret;
719
    int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
720
    int comp_index[4];
721
    int dc_index[4];
722
    int ac_index[4];
723
    int nb_blocks[4];
724
    int h_count[4];
725
    int v_count[4];
726
    
727
    init_get_bits(&s->gb, buf, buf_size);
728
    /* XXX: verify len field validity */
729
    len = get_bits(&s->gb, 16);
730
    nb_components = get_bits(&s->gb, 8);
731
    /* XXX: only interleaved scan accepted */
732
    if (nb_components != 3)
733
        return -1;
734
    vmax = 0;
735
    hmax = 0;
736
    for(i=0;i<nb_components;i++) {
737
        id = get_bits(&s->gb, 8) - 1;
738
        /* find component index */
739
        for(index=0;index<s->nb_components;index++)
740
            if (id == s->component_id[index])
741
                break;
742
        if (index == s->nb_components)
743
            return -1;
744

    
745
        comp_index[i] = index;
746
        nb_blocks[i] = s->h_count[index] * s->v_count[index];
747
        h_count[i] = s->h_count[index];
748
        v_count[i] = s->v_count[index];
749

    
750
        dc_index[i] = get_bits(&s->gb, 4);
751
        if (dc_index[i] >= 4)
752
            return -1;
753
        ac_index[i] = get_bits(&s->gb, 4);
754
        if (ac_index[i] >= 4)
755
            return -1;
756
    }
757
    get_bits(&s->gb, 8); /* Ss */
758
    get_bits(&s->gb, 8); /* Se */
759
    get_bits(&s->gb, 8); /* not used */
760

    
761
    for(i=0;i<nb_components;i++) 
762
        s->last_dc[i] = 1024;
763

    
764
    if (nb_components > 1) {
765
        /* interleaved stream */
766
        mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
767
        mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
768
    } else {
769
        h = s->h_max / s->h_count[comp_index[0]];
770
        v = s->v_max / s->v_count[comp_index[0]];
771
        mb_width = (s->width + h * 8 - 1) / (h * 8);
772
        mb_height = (s->height + v * 8 - 1) / (v * 8);
773
        nb_blocks[0] = 1;
774
        h_count[0] = 1;
775
        v_count[0] = 1;
776
    }
777

    
778
    for(mb_y = 0; mb_y < mb_height; mb_y++) {
779
        for(mb_x = 0; mb_x < mb_width; mb_x++) {
780
            for(i=0;i<nb_components;i++) {
781
                UINT8 *ptr;
782
                int x, y, c;
783
                n = nb_blocks[i];
784
                c = comp_index[i];
785
                h = h_count[i];
786
                v = v_count[i];
787
                x = 0;
788
                y = 0;
789
                for(j=0;j<n;j++) {
790
                    memset(s->block, 0, sizeof(s->block));
791
                    if (decode_block(s, s->block, i, 
792
                                     dc_index[i], ac_index[i], 
793
                                     s->quant_index[c]) < 0) {
794
                        dprintf("error %d %d\n", mb_y, mb_x);
795
                        ret = -1;
796
                        goto the_end;
797
                    }
798
                    ff_idct (s->block);
799
                    ptr = s->current_picture[c] + 
800
                        (s->linesize[c] * (v * mb_y + y) * 8) + 
801
                        (h * mb_x + x) * 8;
802
                    if (s->interlaced && s->bottom_field)
803
                        ptr += s->linesize[c] >> 1;
804
                    put_pixels_clamped(s->block, ptr, s->linesize[c]);
805
                    if (++x == h) {
806
                        x = 0;
807
                        y++;
808
                    }
809
                }
810
            }
811
        }
812
    }
813
    ret = 0;
814
 the_end:
815
    emms_c();
816
    return ret;
817
}
818

    
819
/* return the 8 bit start code value and update the search
820
   state. Return -1 if no start code found */
821
static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end, 
822
                       UINT32 *header_state)
823
{
824
    UINT8 *buf_ptr;
825
    unsigned int state, v;
826
    int val;
827

    
828
    state = *header_state;
829
    buf_ptr = *pbuf_ptr;
830
    if (state) {
831
        /* get marker */
832
    found:
833
        if (buf_ptr < buf_end) {
834
            val = *buf_ptr++;
835
            state = 0;
836
        } else {
837
            val = -1;
838
        }
839
    } else {
840
        while (buf_ptr < buf_end) {
841
            v = *buf_ptr++;
842
            if (v == 0xff) {
843
                state = 1;
844
                goto found;
845
            }
846
        }
847
        val = -1;
848
    }
849
    *pbuf_ptr = buf_ptr;
850
    *header_state = state;
851
    return val;
852
}
853

    
854
static int mjpeg_decode_frame(AVCodecContext *avctx, 
855
                              void *data, int *data_size,
856
                              UINT8 *buf, int buf_size)
857
{
858
    MJpegDecodeContext *s = avctx->priv_data;
859
    UINT8 *buf_end, *buf_ptr, *buf_start;
860
    int len, code, start_code, input_size, i;
861
    AVPicture *picture = data;
862

    
863
    *data_size = 0;
864

    
865
    /* no supplementary picture */
866
    if (buf_size == 0)
867
        return 0;
868

    
869
    buf_ptr = buf;
870
    buf_end = buf + buf_size;
871
    while (buf_ptr < buf_end) {
872
        buf_start = buf_ptr;
873
        /* find start next marker */
874
        code = find_marker(&buf_ptr, buf_end, &s->header_state);
875
        /* copy to buffer */
876
        len = buf_ptr - buf_start;
877
        if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
878
            /* data too big : flush */
879
            s->buf_ptr = s->buffer;
880
            if (code > 0)
881
                s->start_code = code;
882
        } else {
883
            memcpy(s->buf_ptr, buf_start, len);
884
            s->buf_ptr += len;
885
            /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
886
            if (code == 0) {
887
                s->buf_ptr--;
888
            } else if (code > 0) {
889
                /* prepare data for next start code */
890
                input_size = s->buf_ptr - s->buffer;
891
                start_code = s->start_code;
892
                s->buf_ptr = s->buffer;
893
                s->start_code = code;
894
                dprintf("marker=%x\n", start_code);
895
                switch(start_code) {
896
                case SOI:
897
                    /* nothing to do on SOI */
898
                    break;
899
                case DQT:
900
                    mjpeg_decode_dqt(s, s->buffer, input_size);
901
                    break;
902
                case DHT:
903
                    mjpeg_decode_dht(s, s->buffer, input_size);
904
                    break;
905
                case SOF0:
906
                    mjpeg_decode_sof0(s, s->buffer, input_size);
907
                    break;
908
                case SOS:
909
                    mjpeg_decode_sos(s, s->buffer, input_size);
910
                    if (s->start_code == EOI) {
911
                        int l;
912
                        if (s->interlaced) {
913
                            s->bottom_field ^= 1;
914
                            /* if not bottom field, do not output image yet */
915
                            if (s->bottom_field)
916
                                goto the_end;
917
                        }
918
                        for(i=0;i<3;i++) {
919
                            picture->data[i] = s->current_picture[i];
920
                            l = s->linesize[i];
921
                            if (s->interlaced)
922
                                l >>= 1;
923
                            picture->linesize[i] = l;
924
                        }
925
                        *data_size = sizeof(AVPicture);
926
                        avctx->height = s->height;
927
                        if (s->interlaced)
928
                            avctx->height *= 2;
929
                        avctx->width = s->width;
930
                        /* XXX: not complete test ! */
931
                        switch((s->h_count[0] << 4) | s->v_count[0]) {
932
                        case 0x11:
933
                            avctx->pix_fmt = PIX_FMT_YUV444P;
934
                            break;
935
                        case 0x21:
936
                            avctx->pix_fmt = PIX_FMT_YUV422P;
937
                            break;
938
                        default:
939
                        case 0x22:
940
                            avctx->pix_fmt = PIX_FMT_YUV420P;
941
                            break;
942
                        }
943
                        /* dummy quality */
944
                        /* XXX: infer it with matrix */
945
                        avctx->quality = 3; 
946
                        goto the_end;
947
                    }
948
                    break;
949
                }
950
            }
951
        }
952
    }
953
 the_end:
954
    return buf_ptr - buf;
955
}
956

    
957
static int mjpeg_decode_end(AVCodecContext *avctx)
958
{
959
    MJpegDecodeContext *s = avctx->priv_data;
960
    int i, j;
961

    
962
    for(i=0;i<MAX_COMPONENTS;i++)
963
        free(s->current_picture[i]);
964
    for(i=0;i<2;i++) {
965
        for(j=0;j<4;j++)
966
            free_vlc(&s->vlcs[i][j]);
967
    }
968
    return 0;
969
}
970

    
971
AVCodec mjpeg_decoder = {
972
    "mjpeg",
973
    CODEC_TYPE_VIDEO,
974
    CODEC_ID_MJPEG,
975
    sizeof(MJpegDecodeContext),
976
    mjpeg_decode_init,
977
    NULL,
978
    mjpeg_decode_end,
979
    mjpeg_decode_frame,
980
};