annotate mjpeg.c @ 65:bc27d8c5dc2e libavcodec

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