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