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