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