svn merge ^/trunk/blender -r46380:HEAD
[blender.git] / source / blender / avi / intern / mjpeg.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 /** \file blender/avi/intern/mjpeg.c
30  *  \ingroup avi
31  *
32  * This is external code. Converts between avi and mpeg/jpeg.
33  */
34
35
36 #include "AVI_avi.h"
37 #include <stdlib.h>
38 #include <string.h>
39 #include "jpeglib.h"
40 #include "jerror.h"
41 #include "MEM_guardedalloc.h"
42
43 #include "mjpeg.h"
44
45 #define PADUP(num, amt) ((num + (amt - 1)) & ~(amt - 1))
46
47 static void jpegmemdestmgr_build(j_compress_ptr cinfo, unsigned char *buffer, int bufsize);
48 static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, int bufsize);
49
50 static int numbytes;
51
52 static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
53 {
54         if (*htblptr == NULL)
55                 *htblptr = jpeg_alloc_huff_table((j_common_ptr) dinfo);
56
57         memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits));
58         memcpy((*htblptr)->huffval, val, sizeof((*htblptr)->huffval));
59
60         /* Initialize sent_table FALSE so table will be written to JPEG file. */
61         (*htblptr)->sent_table = FALSE;
62 }
63
64 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
65 /* IMPORTANT: these are only valid for 8-bit data precision! */
66
67 static void std_huff_tables(j_decompress_ptr dinfo)
68 {
69         static const UINT8 bits_dc_luminance[17] =
70         { /* 0-base */
71                 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
72         };
73         static const UINT8 val_dc_luminance[] =
74         { 
75                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
76         };
77
78         static const UINT8 bits_dc_chrominance[17] =
79         { /* 0-base */
80                 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
81         };
82         static const UINT8 val_dc_chrominance[] =
83         { 
84                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
85         };
86
87         static const UINT8 bits_ac_luminance[17] =
88         { /* 0-base */
89                 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
90         };
91         static const UINT8 val_ac_luminance[] =
92         { 
93                 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
94                 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
95                 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
96                 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
97                 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
98                 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
99                 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
100                 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
101                 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
102                 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
103                 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
104                 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
105                 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
106                 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
107                 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
108                 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
109                 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
110                 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
111                 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
112                 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
113                 0xf9, 0xfa
114         };
115         static const UINT8 bits_ac_chrominance[17] =
116         { /* 0-base */
117                 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
118         };
119         static const UINT8 val_ac_chrominance[] =
120         { 
121                 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
122                 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
123                 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
124                 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
125                 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
126                 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
127                 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
128                 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
129                 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
130                 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
131                 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
132                 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
133                 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
134                 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
135                 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
136                 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
137                 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
138                 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
139                 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
140                 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
141                 0xf9, 0xfa
142         };
143
144         add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[0],
145                        bits_dc_luminance, val_dc_luminance);
146         add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[0],
147                        bits_ac_luminance, val_ac_luminance);
148         add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[1],
149                        bits_dc_chrominance, val_dc_chrominance);
150         add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[1],
151                        bits_ac_chrominance, val_ac_chrominance);
152 }
153
154 static int Decode_JPEG(unsigned char *inBuffer, unsigned char *outBuffer, unsigned int width, unsigned int height, int bufsize)
155 {
156         int rowstride;
157         unsigned int y;
158         struct jpeg_decompress_struct dinfo;
159         struct jpeg_error_mgr jerr;
160         
161         (void)width; /* unused */
162
163         numbytes = 0;
164
165         dinfo.err = jpeg_std_error(&jerr);
166         jpeg_create_decompress(&dinfo);
167         jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize);
168         jpeg_read_header(&dinfo, TRUE);
169         if (dinfo.dc_huff_tbl_ptrs[0] == NULL) {
170                 std_huff_tables(&dinfo);
171         }
172         dinfo.out_color_space = JCS_RGB;
173         dinfo.dct_method = JDCT_IFAST;
174
175         jpeg_start_decompress(&dinfo);
176
177         rowstride = dinfo.output_width * dinfo.output_components;
178         for (y = 0; y < dinfo.output_height; y++) {
179                 jpeg_read_scanlines(&dinfo, (JSAMPARRAY) &outBuffer, 1);
180                 outBuffer += rowstride;
181         }
182         jpeg_finish_decompress(&dinfo);
183
184         if (dinfo.output_height >= height) return 0;
185         
186         inBuffer += numbytes;
187         jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize - numbytes);
188
189         numbytes = 0;
190         jpeg_read_header(&dinfo, TRUE);
191         if (dinfo.dc_huff_tbl_ptrs[0] == NULL) {
192                 std_huff_tables(&dinfo);
193         }
194
195         jpeg_start_decompress(&dinfo);
196         rowstride = dinfo.output_width * dinfo.output_components;
197         for (y = 0; y < dinfo.output_height; y++) {
198                 jpeg_read_scanlines(&dinfo, (JSAMPARRAY) &outBuffer, 1);
199                 outBuffer += rowstride;
200         }
201         jpeg_finish_decompress(&dinfo);
202         jpeg_destroy_decompress(&dinfo);
203         
204         return 1;
205 }
206
207 static void Compress_JPEG(int quality, unsigned char *outbuffer, unsigned char *inBuffer, int width, int height, int bufsize)
208 {
209         int i, rowstride;
210         unsigned int y;
211         struct jpeg_compress_struct cinfo;
212         struct jpeg_error_mgr jerr;
213         unsigned char marker[60];
214
215         cinfo.err = jpeg_std_error(&jerr);
216         jpeg_create_compress(&cinfo);
217         jpegmemdestmgr_build(&cinfo, outbuffer, bufsize);
218
219         cinfo.image_width = width;
220         cinfo.image_height = height;
221         cinfo.input_components = 3;
222         cinfo.in_color_space = JCS_RGB;
223
224         jpeg_set_defaults(&cinfo);
225         jpeg_set_colorspace(&cinfo, JCS_YCbCr);
226                 
227         jpeg_set_quality(&cinfo, quality, TRUE);
228
229         cinfo.dc_huff_tbl_ptrs[0]->sent_table = TRUE;
230         cinfo.dc_huff_tbl_ptrs[1]->sent_table = TRUE;
231         cinfo.ac_huff_tbl_ptrs[0]->sent_table = TRUE;
232         cinfo.ac_huff_tbl_ptrs[1]->sent_table = TRUE;
233
234         cinfo.comp_info[0].component_id = 0;
235         cinfo.comp_info[0].v_samp_factor = 1;
236         cinfo.comp_info[1].component_id = 1;
237         cinfo.comp_info[2].component_id = 2;
238
239         cinfo.write_JFIF_header = FALSE;
240
241         jpeg_start_compress(&cinfo, FALSE);
242
243         i = 0;
244         marker[i++] = 'A';
245         marker[i++] = 'V';
246         marker[i++] = 'I';
247         marker[i++] = '1';
248         marker[i++] = 0;
249         while (i < 60)
250                 marker[i++] = 32;
251
252         jpeg_write_marker(&cinfo, JPEG_APP0, marker, 60);
253
254         i = 0;
255         while (i < 60)
256                 marker[i++] = 0;
257
258         jpeg_write_marker(&cinfo, JPEG_COM, marker, 60);
259
260         rowstride = cinfo.image_width * cinfo.input_components;
261         for (y = 0; y < cinfo.image_height; y++) {
262                 jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &inBuffer, 1);
263                 inBuffer += rowstride;
264         }
265         jpeg_finish_compress(&cinfo);
266         jpeg_destroy_compress(&cinfo);
267 }
268
269 static void interlace(unsigned char *to, unsigned char *from, int width, int height)
270 {
271         int i, rowstride = width * 3;
272         
273         for (i = 0; i < height; i++) {
274                 if (i & 1)
275                         memcpy(&to[i * rowstride], &from[(i / 2 + height / 2) * rowstride], rowstride);
276                 else 
277                         memcpy(&to[i * rowstride], &from[(i / 2) * rowstride], rowstride);
278         }
279 }
280
281 static void deinterlace(int odd, unsigned char *to, unsigned char *from, int width, int height)
282 {
283         int i, rowstride = width * 3;
284         
285         for (i = 0; i < height; i++) {
286                 if ((i & 1) == odd)
287                         memcpy(&to[(i / 2 + height / 2) * rowstride], &from[i * rowstride], rowstride);
288                 else 
289                         memcpy(&to[(i / 2) * rowstride], &from[i * rowstride], rowstride);
290         }
291 }
292
293 static int check_and_decode_jpeg(unsigned char *inbuf, unsigned char *outbuf, int width, int height, int bufsize)
294 {
295         /* JPEG's are always multiples of 16, extra is cropped out AVI's */
296         if ((width & 0xF) || (height & 0xF)) {
297                 int i, rrowstride, jrowstride;
298                 int jwidth = PADUP(width, 16);
299                 int jheight = PADUP(height, 16);
300                 unsigned char *tmpbuf = MEM_mallocN(jwidth * jheight * 3, "avi.check_and_decode_jpeg");
301                 int ret = Decode_JPEG(inbuf, tmpbuf, jwidth, jheight, bufsize);
302
303                 /* crop the tmpbuf into the real buffer */
304                 rrowstride = width * 3;
305                 jrowstride = jwidth * 3;
306                 for (i = 0; i < height; i++)
307                         memcpy(&outbuf[i * rrowstride], &tmpbuf[i * jrowstride], rrowstride);
308                 MEM_freeN(tmpbuf);
309                 
310                 return ret;
311         }
312         else {
313                 return Decode_JPEG(inbuf, outbuf, width, height, bufsize);
314         }
315 }
316
317 static void check_and_compress_jpeg(int quality, unsigned char *outbuf, unsigned char *inbuf, int width, int height, int bufsize)
318 {
319         /* JPEG's are always multiples of 16, extra is ignored in AVI's */
320         if ((width & 0xF) || (height & 0xF)) {
321                 int i, rrowstride, jrowstride;
322                 int jwidth = PADUP(width, 16);
323                 int jheight = PADUP(height, 16);
324                 unsigned char *tmpbuf = MEM_mallocN(jwidth * jheight * 3, "avi.check_and_compress_jpeg");
325
326                 /* resize the realbuf into the tmpbuf */
327                 rrowstride = width * 3;
328                 jrowstride = jwidth * 3;
329                 for (i = 0; i < jheight; i++) {
330                         if (i < height)
331                                 memcpy(&tmpbuf[i * jrowstride], &inbuf[i * rrowstride], rrowstride);
332                         else
333                                 memset(&tmpbuf[i * jrowstride], 0, rrowstride);
334                         memset(&tmpbuf[i * jrowstride + rrowstride], 0, jrowstride - rrowstride);
335                 }
336
337                 Compress_JPEG(quality, outbuf, tmpbuf, jwidth, jheight, bufsize);
338
339                 MEM_freeN(tmpbuf);
340         }
341         else {
342                 Compress_JPEG(quality, outbuf, inbuf, width, height, bufsize);
343         }
344 }
345
346 void *avi_converter_from_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, int *size)
347 {
348         int deint;
349         unsigned char *buf;
350
351         (void)stream; /* unused */
352
353         buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_from_mjpeg 1");
354
355         deint = check_and_decode_jpeg(buffer, buf, movie->header->Width, movie->header->Height, *size);
356         
357         MEM_freeN(buffer);
358         
359         if (deint) {
360                 buffer = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_from_mjpeg 2");
361                 interlace(buffer, buf, movie->header->Width, movie->header->Height);
362                 MEM_freeN(buf);
363         
364                 buf = buffer;
365         }
366                 
367         return buf;
368 }
369
370 void *avi_converter_to_mjpeg(AviMovie *movie, int stream, unsigned char *buffer, int *size)
371 {
372         unsigned char *buf;
373         int bufsize = *size;
374         
375         numbytes = 0;
376         *size = 0;
377
378         buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_to_mjpeg 1");
379         if (!movie->interlace) {
380                 check_and_compress_jpeg(movie->streams[stream].sh.Quality / 100, buf, buffer,  movie->header->Width, movie->header->Height, bufsize);
381         }
382         else {
383                 deinterlace(movie->odd_fields, buf, buffer, movie->header->Width, movie->header->Height);
384                 MEM_freeN(buffer);
385         
386                 buffer = buf;
387                 buf = MEM_mallocN(movie->header->Height * movie->header->Width * 3, "avi.avi_converter_to_mjpeg 2");
388         
389                 check_and_compress_jpeg(movie->streams[stream].sh.Quality / 100, buf, buffer,  movie->header->Width, movie->header->Height / 2, bufsize / 2);
390                 *size += numbytes;
391                 numbytes = 0;
392                 check_and_compress_jpeg(movie->streams[stream].sh.Quality / 100, buf + *size, buffer + (movie->header->Height / 2) * movie->header->Width * 3,  movie->header->Width, movie->header->Height / 2, bufsize / 2);
393         }
394         *size += numbytes;      
395
396         MEM_freeN(buffer);
397         return buf;
398 }
399
400
401 /* Compression from memory */
402
403 static void jpegmemdestmgr_init_destination(j_compress_ptr cinfo)
404 {
405         (void)cinfo; /* unused */
406 }
407
408 static boolean jpegmemdestmgr_empty_output_buffer(j_compress_ptr cinfo)
409 {
410         (void)cinfo; /* unused */
411         return TRUE;
412 }
413
414 static void jpegmemdestmgr_term_destination(j_compress_ptr cinfo)
415 {
416         numbytes -= cinfo->dest->free_in_buffer;
417
418         MEM_freeN(cinfo->dest);
419 }
420
421 static void jpegmemdestmgr_build(j_compress_ptr cinfo, unsigned char *buffer, int bufsize)
422 {
423         cinfo->dest = MEM_mallocN(sizeof(*(cinfo->dest)), "avi.jpegmemdestmgr_build");
424         
425         cinfo->dest->init_destination = jpegmemdestmgr_init_destination;
426         cinfo->dest->empty_output_buffer = jpegmemdestmgr_empty_output_buffer;
427         cinfo->dest->term_destination = jpegmemdestmgr_term_destination;
428
429         cinfo->dest->next_output_byte = buffer;
430         cinfo->dest->free_in_buffer = bufsize;
431         
432         numbytes = bufsize;
433 }
434
435 /* Decompression from memory */
436
437 static void jpegmemsrcmgr_init_source(j_decompress_ptr dinfo)
438 {
439         (void)dinfo;
440 }
441
442 static boolean jpegmemsrcmgr_fill_input_buffer(j_decompress_ptr dinfo)
443 {
444         unsigned char *buf = (unsigned char *) dinfo->src->next_input_byte - 2;
445         
446         /* if we get called, must have run out of data */
447         WARNMS(dinfo, JWRN_JPEG_EOF);
448         
449         buf[0] = (JOCTET) 0xFF;
450         buf[1] = (JOCTET) JPEG_EOI;
451         
452         dinfo->src->next_input_byte = buf;
453         dinfo->src->bytes_in_buffer = 2;
454         
455         return TRUE;
456 }
457
458 static void jpegmemsrcmgr_skip_input_data(j_decompress_ptr dinfo, long skipcnt)
459 {
460         if (dinfo->src->bytes_in_buffer < skipcnt)
461                 skipcnt = dinfo->src->bytes_in_buffer;
462
463         dinfo->src->next_input_byte += skipcnt;
464         dinfo->src->bytes_in_buffer -= skipcnt;
465 }
466
467 static void jpegmemsrcmgr_term_source(j_decompress_ptr dinfo)
468 {
469         numbytes -= dinfo->src->bytes_in_buffer;
470         
471         MEM_freeN(dinfo->src);
472 }
473
474 static void jpegmemsrcmgr_build(j_decompress_ptr dinfo, unsigned char *buffer, int bufsize)
475 {
476         dinfo->src = MEM_mallocN(sizeof(*(dinfo->src)), "avi.jpegmemsrcmgr_build");
477         
478         dinfo->src->init_source = jpegmemsrcmgr_init_source;
479         dinfo->src->fill_input_buffer = jpegmemsrcmgr_fill_input_buffer;
480         dinfo->src->skip_input_data = jpegmemsrcmgr_skip_input_data;
481         dinfo->src->resync_to_restart = jpeg_resync_to_restart;
482         dinfo->src->term_source = jpegmemsrcmgr_term_source;
483         
484         dinfo->src->bytes_in_buffer = bufsize;
485         dinfo->src->next_input_byte = buffer;
486
487         numbytes = bufsize;
488 }