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