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