Initial revision
[libav.git] / libav / mjpegenc.c
1 /*
2 * MJPEG encoder
3 * Copyright (c) 2000 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "avcodec.h"
22 #include "mpegvideo.h"
23
24 typedef struct MJpegContext {
25 UINT8 huff_size_dc_luminance[12];
26 UINT16 huff_code_dc_luminance[12];
27 UINT8 huff_size_dc_chrominance[12];
28 UINT16 huff_code_dc_chrominance[12];
29
30 UINT8 huff_size_ac_luminance[256];
31 UINT16 huff_code_ac_luminance[256];
32 UINT8 huff_size_ac_chrominance[256];
33 UINT16 huff_code_ac_chrominance[256];
34 } MJpegContext;
35
36 #define SOF0 0xc0
37 #define SOI 0xd8
38 #define EOI 0xd9
39 #define DQT 0xdb
40 #define DHT 0xc4
41 #define SOS 0xda
42
43 #if 0
44 /* These are the sample quantization tables given in JPEG spec section K.1.
45 * The spec says that the values given produce "good" quality, and
46 * when divided by 2, "very good" quality.
47 */
48 static const unsigned char std_luminance_quant_tbl[64] = {
49 16, 11, 10, 16, 24, 40, 51, 61,
50 12, 12, 14, 19, 26, 58, 60, 55,
51 14, 13, 16, 24, 40, 57, 69, 56,
52 14, 17, 22, 29, 51, 87, 80, 62,
53 18, 22, 37, 56, 68, 109, 103, 77,
54 24, 35, 55, 64, 81, 104, 113, 92,
55 49, 64, 78, 87, 103, 121, 120, 101,
56 72, 92, 95, 98, 112, 100, 103, 99
57 };
58 static const unsigned char std_chrominance_quant_tbl[64] = {
59 17, 18, 24, 47, 99, 99, 99, 99,
60 18, 21, 26, 66, 99, 99, 99, 99,
61 24, 26, 56, 99, 99, 99, 99, 99,
62 47, 66, 99, 99, 99, 99, 99, 99,
63 99, 99, 99, 99, 99, 99, 99, 99,
64 99, 99, 99, 99, 99, 99, 99, 99,
65 99, 99, 99, 99, 99, 99, 99, 99,
66 99, 99, 99, 99, 99, 99, 99, 99
67 };
68 #endif
69
70 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
71 /* IMPORTANT: these are only valid for 8-bit data precision! */
72 static const UINT8 bits_dc_luminance[17] =
73 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
74 static const UINT8 val_dc_luminance[] =
75 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
76
77 static const UINT8 bits_dc_chrominance[17] =
78 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
79 static const UINT8 val_dc_chrominance[] =
80 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
81
82 static const UINT8 bits_ac_luminance[17] =
83 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
84 static const UINT8 val_ac_luminance[] =
85 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
86 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
87 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
88 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
89 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
90 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
91 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
92 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
93 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
94 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
95 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
96 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
97 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
98 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
99 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
100 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
101 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
102 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
103 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
104 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
105 0xf9, 0xfa
106 };
107
108 static const UINT8 bits_ac_chrominance[17] =
109 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
110
111 static const UINT8 val_ac_chrominance[] =
112 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
113 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
114 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
115 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
116 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
117 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
118 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
119 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
120 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
121 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
122 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
123 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
124 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
125 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
126 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
127 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
128 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
129 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
130 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
131 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
132 0xf9, 0xfa
133 };
134
135
136 /* isn't this function nicer than the one in the libjpeg ? */
137 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
138 const UINT8 *bits_table, const UINT8 *val_table)
139 {
140 int i, j, k,nb, code, sym;
141
142 code = 0;
143 k = 0;
144 for(i=1;i<=16;i++) {
145 nb = bits_table[i];
146 for(j=0;j<nb;j++) {
147 sym = val_table[k++];
148 huff_size[sym] = i;
149 huff_code[sym] = code;
150 code++;
151 }
152 code <<= 1;
153 }
154 }
155
156 int mjpeg_init(MpegEncContext *s)
157 {
158 MJpegContext *m;
159
160 m = malloc(sizeof(MJpegContext));
161 if (!m)
162 return -1;
163
164 /* build all the huffman tables */
165 build_huffman_codes(m->huff_size_dc_luminance,
166 m->huff_code_dc_luminance,
167 bits_dc_luminance,
168 val_dc_luminance);
169 build_huffman_codes(m->huff_size_dc_chrominance,
170 m->huff_code_dc_chrominance,
171 bits_dc_chrominance,
172 val_dc_chrominance);
173 build_huffman_codes(m->huff_size_ac_luminance,
174 m->huff_code_ac_luminance,
175 bits_ac_luminance,
176 val_ac_luminance);
177 build_huffman_codes(m->huff_size_ac_chrominance,
178 m->huff_code_ac_chrominance,
179 bits_ac_chrominance,
180 val_ac_chrominance);
181
182 s->mjpeg_ctx = m;
183 return 0;
184 }
185
186 void mjpeg_close(MpegEncContext *s)
187 {
188 free(s->mjpeg_ctx);
189 }
190
191 static inline void put_marker(PutBitContext *p, int code)
192 {
193 put_bits(p, 8, 0xff);
194 put_bits(p, 8, code);
195 }
196
197 /* table_class: 0 = DC coef, 1 = AC coefs */
198 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
199 const UINT8 *bits_table, const UINT8 *value_table)
200 {
201 PutBitContext *p = &s->pb;
202 int n, i;
203
204 put_bits(p, 4, table_class);
205 put_bits(p, 4, table_id);
206
207 n = 0;
208 for(i=1;i<=16;i++) {
209 n += bits_table[i];
210 put_bits(p, 8, bits_table[i]);
211 }
212
213 for(i=0;i<n;i++)
214 put_bits(p, 8, value_table[i]);
215
216 return n + 17;
217 }
218
219 static void jpeg_table_header(MpegEncContext *s)
220 {
221 PutBitContext *p = &s->pb;
222 int i, size;
223 UINT8 *ptr;
224
225 /* quant matrixes */
226 put_marker(p, DQT);
227 put_bits(p, 16, 2 + 1 * (1 + 64));
228 put_bits(p, 4, 0); /* 8 bit precision */
229 put_bits(p, 4, 0); /* table 0 */
230 for(i=0;i<64;i++) {
231 put_bits(p, 8, s->init_intra_matrix[i]);
232 }
233 #if 0
234 put_bits(p, 4, 0); /* 8 bit precision */
235 put_bits(p, 4, 1); /* table 1 */
236 for(i=0;i<64;i++) {
237 put_bits(p, 8, m->chrominance_matrix[i]);
238 }
239 #endif
240
241 /* huffman table */
242 put_marker(p, DHT);
243 flush_put_bits(p);
244 ptr = p->buf_ptr;
245 put_bits(p, 16, 0); /* patched later */
246 size = 2;
247 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
248 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
249
250 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
251 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
252 ptr[0] = size >> 8;
253 ptr[1] = size;
254 }
255
256 void mjpeg_picture_header(MpegEncContext *s)
257 {
258 put_marker(&s->pb, SOI);
259
260 jpeg_table_header(s);
261
262 put_marker(&s->pb, SOF0);
263
264 put_bits(&s->pb, 16, 17);
265 put_bits(&s->pb, 8, 8); /* 8 bits/component */
266 put_bits(&s->pb, 16, s->height);
267 put_bits(&s->pb, 16, s->width);
268 put_bits(&s->pb, 8, 3); /* 3 components */
269
270 /* Y component */
271 put_bits(&s->pb, 8, 1); /* component number */
272 put_bits(&s->pb, 4, 2); /* H factor */
273 put_bits(&s->pb, 4, 2); /* V factor */
274 put_bits(&s->pb, 8, 0); /* select matrix */
275
276 /* Cb component */
277 put_bits(&s->pb, 8, 2); /* component number */
278 put_bits(&s->pb, 4, 1); /* H factor */
279 put_bits(&s->pb, 4, 1); /* V factor */
280 put_bits(&s->pb, 8, 0); /* select matrix */
281
282 /* Cr component */
283 put_bits(&s->pb, 8, 3); /* component number */
284 put_bits(&s->pb, 4, 1); /* H factor */
285 put_bits(&s->pb, 4, 1); /* V factor */
286 put_bits(&s->pb, 8, 0); /* select matrix */
287
288 /* scan header */
289 put_marker(&s->pb, SOS);
290 put_bits(&s->pb, 16, 12); /* length */
291 put_bits(&s->pb, 8, 3); /* 3 components */
292
293 /* Y component */
294 put_bits(&s->pb, 8, 1); /* index */
295 put_bits(&s->pb, 4, 0); /* DC huffman table index */
296 put_bits(&s->pb, 4, 0); /* AC huffman table index */
297
298 /* Cb component */
299 put_bits(&s->pb, 8, 2); /* index */
300 put_bits(&s->pb, 4, 1); /* DC huffman table index */
301 put_bits(&s->pb, 4, 1); /* AC huffman table index */
302
303 /* Cr component */
304 put_bits(&s->pb, 8, 3); /* index */
305 put_bits(&s->pb, 4, 1); /* DC huffman table index */
306 put_bits(&s->pb, 4, 1); /* AC huffman table index */
307
308 put_bits(&s->pb, 8, 0); /* Ss (not used) */
309 put_bits(&s->pb, 8, 63); /* Se (not used) */
310 put_bits(&s->pb, 8, 0); /* (not used) */
311 }
312
313 void mjpeg_picture_trailer(MpegEncContext *s)
314 {
315 jflush_put_bits(&s->pb);
316 put_marker(&s->pb, EOI);
317 }
318
319 static inline void encode_dc(MpegEncContext *s, int val,
320 UINT8 *huff_size, UINT16 *huff_code)
321 {
322 int mant, nbits;
323
324 if (val == 0) {
325 jput_bits(&s->pb, huff_size[0], huff_code[0]);
326 } else {
327 mant = val;
328 if (val < 0) {
329 val = -val;
330 mant--;
331 }
332
333 /* compute the log (XXX: optimize) */
334 nbits = 0;
335 while (val != 0) {
336 val = val >> 1;
337 nbits++;
338 }
339
340 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
341
342 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
343 }
344 }
345
346 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
347 {
348 int mant, nbits, code, i, j;
349 int component, dc, run, last_index, val;
350 MJpegContext *m = s->mjpeg_ctx;
351 UINT8 *huff_size_ac;
352 UINT16 *huff_code_ac;
353
354 /* DC coef */
355 component = (n <= 3 ? 0 : n - 4 + 1);
356 dc = block[0]; /* overflow is impossible */
357 val = dc - s->last_dc[component];
358 if (n < 4) {
359 encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
360 huff_size_ac = m->huff_size_ac_luminance;
361 huff_code_ac = m->huff_code_ac_luminance;
362 } else {
363 encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
364 huff_size_ac = m->huff_size_ac_chrominance;
365 huff_code_ac = m->huff_code_ac_chrominance;
366 }
367 s->last_dc[component] = dc;
368
369 /* AC coefs */
370
371 run = 0;
372 last_index = s->block_last_index[n];
373 for(i=1;i<=last_index;i++) {
374 j = zigzag_direct[i];
375 val = block[j];
376 if (val == 0) {
377 run++;
378 } else {
379 while (run >= 16) {
380 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
381 run -= 16;
382 }
383 mant = val;
384 if (val < 0) {
385 val = -val;
386 mant--;
387 }
388
389 /* compute the log (XXX: optimize) */
390 nbits = 0;
391 while (val != 0) {
392 val = val >> 1;
393 nbits++;
394 }
395 code = (run << 4) | nbits;
396
397 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
398
399 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
400 run = 0;
401 }
402 }
403
404 /* output EOB only if not already 64 values */
405 if (last_index < 63 || run != 0)
406 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
407 }
408
409 void mjpeg_encode_mb(MpegEncContext *s,
410 DCTELEM block[6][64])
411 {
412 int i;
413 for(i=0;i<6;i++) {
414 encode_block(s, block[i], i);
415 }
416 }