2 * This library is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published
4 * by the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 #include "colormodels.h"
79 #define QUICKTIME_MARKER_SIZE 0x2c
80 #define AVI_MARKER_SIZE 0x12
81 #define QUICKTIME_JPEG_TAG 0x6d6a7067
82 #define QUICKTIME_AVI_TAG 0x41564931
85 METHODDEF(void) mjpeg_error_exit (j_common_ptr cinfo)
87 /* cinfo->err really points to a mjpeg_error_mgr struct, so coerce pointer */
88 mjpeg_error_ptr mjpegerr = (mjpeg_error_ptr) cinfo->err;
90 /* Always display the message. */
91 /* We could postpone this until after returning, if we chose. */
92 (*cinfo->err->output_message) (cinfo);
94 /* Return control to the setjmp point */
95 longjmp(mjpegerr->setjmp_buffer, 1);
100 struct jpeg_destination_mgr pub; /* public fields */
102 JOCTET *buffer; /* Pointer to buffer */
103 mjpeg_compressor *engine;
104 } mjpeg_destination_mgr;
106 typedef mjpeg_destination_mgr *mjpeg_dest_ptr;
110 * Initialize destination --- called by jpeg_start_compress
111 * before any data is actually written.
114 METHODDEF(void) init_destination(j_compress_ptr cinfo)
116 mjpeg_dest_ptr dest = (mjpeg_dest_ptr)cinfo->dest;
118 /* Set the pointer to the preallocated buffer */
119 if(!dest->engine->output_buffer)
121 dest->engine->output_buffer = calloc(1, 65536);
122 dest->engine->output_allocated = 65536;
124 dest->buffer = dest->engine->output_buffer;
125 dest->pub.next_output_byte = dest->engine->output_buffer;
126 dest->pub.free_in_buffer = dest->engine->output_allocated;
130 * Empty the output buffer --- called whenever buffer fills up.
132 * In typical applications, this should write the entire output buffer
133 * (ignoring the current state of next_output_byte & free_in_buffer),
134 * reset the pointer & count to the start of the buffer, and return TRUE
135 * indicating that the buffer has been dumped.
137 * In applications that need to be able to suspend compression due to output
138 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
139 * In this situation, the compressor will return to its caller (possibly with
140 * an indication that it has not accepted all the supplied scanlines). The
141 * application should resume compression after it has made more room in the
142 * output buffer. Note that there are substantial restrictions on the use of
143 * suspension --- see the documentation.
145 * When suspending, the compressor will back up to a convenient restart point
146 * (typically the start of the current MCU). next_output_byte & free_in_buffer
147 * indicate where the restart point will be if the current call returns FALSE.
148 * Data beyond this point will be regenerated after resumption, so do not
149 * write it out when emptying the buffer externally.
152 METHODDEF(boolean) empty_output_buffer(j_compress_ptr cinfo)
154 /* Allocate a bigger buffer. */
155 mjpeg_dest_ptr dest = (mjpeg_dest_ptr)cinfo->dest;
157 dest->engine->output_size = dest->engine->output_allocated;
158 dest->engine->output_allocated *= 2;
159 dest->engine->output_buffer = realloc(dest->engine->output_buffer,
160 dest->engine->output_allocated);
161 dest->buffer = dest->engine->output_buffer;
162 dest->pub.next_output_byte = dest->buffer + dest->engine->output_size;
163 dest->pub.free_in_buffer = dest->engine->output_allocated - dest->engine->output_size;
169 * Terminate destination --- called by jpeg_finish_compress
170 * after all data has been written. Usually needs to flush buffer.
172 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
173 * application must deal with any cleanup that should happen even
176 METHODDEF(void) term_destination(j_compress_ptr cinfo)
178 /* Just get the length */
179 mjpeg_dest_ptr dest = (mjpeg_dest_ptr)cinfo->dest;
180 dest->engine->output_size = dest->engine->output_allocated - dest->pub.free_in_buffer;
183 GLOBAL(void) jpeg_buffer_dest(j_compress_ptr cinfo, mjpeg_compressor *engine)
187 /* The destination object is made permanent so that multiple JPEG images
188 * can be written to the same file without re-executing jpeg_stdio_dest.
189 * This makes it dangerous to use this manager and a different destination
190 * manager serially with the same JPEG object, because their private object
191 * sizes may be different. Caveat programmer.
193 if(cinfo->dest == NULL)
195 /* first time for this JPEG object? */
196 cinfo->dest = (struct jpeg_destination_mgr *)
197 (*cinfo->mem->alloc_small)((j_common_ptr)cinfo,
199 sizeof(mjpeg_destination_mgr));
202 dest = (mjpeg_dest_ptr)cinfo->dest;
203 dest->pub.init_destination = init_destination;
204 dest->pub.empty_output_buffer = empty_output_buffer;
205 dest->pub.term_destination = term_destination;
206 dest->engine = engine;
223 struct jpeg_source_mgr pub; /* public fields */
225 JOCTET * buffer; /* start of buffer */
226 int bytes; /* total size of buffer */
229 typedef mjpeg_source_mgr* mjpeg_src_ptr;
231 METHODDEF(void) init_source(j_decompress_ptr cinfo)
233 //mjpeg_src_ptr src = (mjpeg_src_ptr) cinfo->src;
236 METHODDEF(boolean) fill_input_buffer(j_decompress_ptr cinfo)
238 mjpeg_src_ptr src = (mjpeg_src_ptr) cinfo->src;
240 src->buffer[0] = (JOCTET)0xFF;
241 src->buffer[1] = (JOCTET)M_EOI;
242 src->pub.next_input_byte = src->buffer;
243 src->pub.bytes_in_buffer = 2;
249 METHODDEF(void) skip_input_data(j_decompress_ptr cinfo, long num_bytes)
251 mjpeg_src_ptr src = (mjpeg_src_ptr)cinfo->src;
253 src->pub.next_input_byte += (size_t)num_bytes;
254 src->pub.bytes_in_buffer -= (size_t)num_bytes;
258 METHODDEF(void) term_source(j_decompress_ptr cinfo)
262 GLOBAL(void) jpeg_buffer_src(j_decompress_ptr cinfo, unsigned char *buffer, long bytes)
266 /* first time for this JPEG object? */
267 if(cinfo->src == NULL)
269 cinfo->src = (struct jpeg_source_mgr*)
270 (*cinfo->mem->alloc_small)((j_common_ptr)cinfo,
272 sizeof(mjpeg_source_mgr));
273 src = (mjpeg_src_ptr)cinfo->src;
276 src = (mjpeg_src_ptr)cinfo->src;
277 src->pub.init_source = init_source;
278 src->pub.fill_input_buffer = fill_input_buffer;
279 src->pub.skip_input_data = skip_input_data;
280 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
281 src->pub.term_source = term_source;
282 src->pub.bytes_in_buffer = bytes;
283 src->pub.next_input_byte = buffer;
284 src->buffer = buffer;
288 /* JPEG DHT Segment for YCrCb omitted from MJPEG data */
290 unsigned char jpeg_odml_dht[0x1a4] = {
291 0xff, 0xc4, 0x01, 0xa2,
293 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
296 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
297 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
299 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
300 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
301 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
302 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
303 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
304 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
305 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
306 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
307 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
308 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
309 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
312 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
313 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
314 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
315 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
316 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
317 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
318 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
319 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
320 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
321 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
322 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
328 * Parse the DHT table.
329 * This code comes from jpeg6b (jdmarker.c).
332 int jpeg_load_dht (struct jpeg_decompress_struct *info, unsigned char *dht,
333 JHUFF_TBL *ac_tables[], JHUFF_TBL *dc_tables[])
335 unsigned int length = (dht[2] << 8) + dht[3] - 2;
336 unsigned int pos = 4;
337 unsigned int count, i;
341 unsigned char bits[17];
342 unsigned char huffval[256];
349 for (i = 1; i <= 16; ++i)
351 bits[i] = dht[pos++];
356 if (count > 256 || count > length)
359 for (i = 0; i < count; ++i)
360 huffval[i] = dht[pos++];
366 hufftbl = &ac_tables[index];
369 hufftbl = &dc_tables[index];
371 if (index < 0 || index >= NUM_HUFF_TBLS)
374 if (*hufftbl == NULL)
375 *hufftbl = jpeg_alloc_huff_table ((j_common_ptr)info);
376 if (*hufftbl == NULL)
379 memcpy ((*hufftbl)->bits, bits, sizeof (*hufftbl)->bits);
380 memcpy ((*hufftbl)->huffval, huffval, sizeof (*hufftbl)->huffval);
403 static void reset_buffer(unsigned char **buffer, long *size, long *allocated)
408 static void delete_buffer(unsigned char **buffer, long *size, long *allocated)
418 static void append_buffer(unsigned char **buffer,
426 *buffer = calloc(1, 65536);
431 if(*size + data_size + 0x100 > *allocated)
433 *allocated = *size + data_size + 0x100;
434 *buffer = realloc(*buffer, *allocated);
437 memcpy(*buffer + *size, data, data_size);
441 static void allocate_temps(mjpeg_t *mjpeg)
445 if(!mjpeg->temp_data)
447 switch(mjpeg->jpeg_color_model)
450 //printf("allocate_temps 1\n");
451 mjpeg->temp_data = calloc(1, mjpeg->coded_w * mjpeg->coded_h * 2);
452 mjpeg->temp_rows[0] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
453 mjpeg->temp_rows[1] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
454 mjpeg->temp_rows[2] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
455 for(i = 0; i < mjpeg->coded_h; i++)
457 mjpeg->temp_rows[0][i] = mjpeg->temp_data + i * mjpeg->coded_w;
458 mjpeg->temp_rows[1][i] = mjpeg->temp_data + mjpeg->coded_w * mjpeg->coded_h + i * mjpeg->coded_w / 2;
459 mjpeg->temp_rows[2][i] = mjpeg->temp_data + mjpeg->coded_w * mjpeg->coded_h + mjpeg->coded_w / 2 * mjpeg->coded_h + i * mjpeg->coded_w / 2;
464 mjpeg->temp_data = calloc(1, mjpeg->coded_w * mjpeg->coded_h * 3);
465 mjpeg->temp_rows[0] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
466 mjpeg->temp_rows[1] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
467 mjpeg->temp_rows[2] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
470 memset(mjpeg->temp_data + mjpeg->coded_w * mjpeg->coded_h,
472 mjpeg->coded_w * mjpeg->coded_h * 2);
474 for(i = 0; i < mjpeg->coded_h; i++)
476 mjpeg->temp_rows[0][i] = mjpeg->temp_data + i * mjpeg->coded_w;
477 mjpeg->temp_rows[1][i] = mjpeg->temp_data + mjpeg->coded_w * mjpeg->coded_h + i * mjpeg->coded_w;
478 mjpeg->temp_rows[2][i] = mjpeg->temp_data + mjpeg->coded_w * mjpeg->coded_h + mjpeg->coded_w * mjpeg->coded_h + i * mjpeg->coded_w;
483 mjpeg->temp_data = calloc(1, mjpeg->coded_w * mjpeg->coded_h + mjpeg->coded_w * mjpeg->coded_h / 2);
484 mjpeg->temp_rows[0] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
485 mjpeg->temp_rows[1] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h / 2);
486 mjpeg->temp_rows[2] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h / 2);
487 for(i = 0; i < mjpeg->coded_h; i++)
489 mjpeg->temp_rows[0][i] = mjpeg->temp_data + i * mjpeg->coded_w;
490 if(i < mjpeg->coded_h / 2)
492 mjpeg->temp_rows[1][i] = mjpeg->temp_data + mjpeg->coded_w * mjpeg->coded_h + i * (mjpeg->coded_w / 2);
493 mjpeg->temp_rows[2][i] = mjpeg->temp_data + mjpeg->coded_w * mjpeg->coded_h + (mjpeg->coded_h / 2) * (mjpeg->coded_w / 2) + i * (mjpeg->coded_w / 2);
501 static int get_input_row(mjpeg_t *mjpeg, mjpeg_compressor *compressor, int i)
504 if(mjpeg->fields > 1)
505 input_row = i * 2 + compressor->instance;
508 if(input_row >= mjpeg->coded_h) input_row = mjpeg->coded_h - 1;
512 // Get pointers to rows for the JPEG compressor
513 static void get_rows(mjpeg_t *mjpeg, mjpeg_compressor *compressor)
516 switch(mjpeg->jpeg_color_model)
520 if(!compressor->rows[0])
522 compressor->rows[0] = calloc(1, sizeof(unsigned char*) * compressor->coded_field_h);
523 compressor->rows[1] = calloc(1, sizeof(unsigned char*) * compressor->coded_field_h);
524 compressor->rows[2] = calloc(1, sizeof(unsigned char*) * compressor->coded_field_h);
527 // User colormodel matches jpeg colormodel
528 if(mjpeg->color_model == BC_YUV444P &&
529 mjpeg->output_w == mjpeg->coded_w &&
530 mjpeg->output_h == mjpeg->coded_h)
532 for(i = 0; i < compressor->coded_field_h; i++)
534 int input_row = get_input_row(mjpeg, compressor, i);
535 compressor->rows[0][i] = mjpeg->y_argument +
536 mjpeg->coded_w * input_row;
537 compressor->rows[1][i] = mjpeg->u_argument +
538 mjpeg->coded_w * input_row;
539 compressor->rows[2][i] = mjpeg->v_argument +
540 mjpeg->coded_w * input_row;
545 for(i = 0; i < compressor->coded_field_h; i++)
547 int input_row = get_input_row(mjpeg, compressor, i);
548 compressor->rows[0][i] = mjpeg->temp_rows[0][input_row];
549 compressor->rows[1][i] = mjpeg->temp_rows[1][input_row];
550 compressor->rows[2][i] = mjpeg->temp_rows[2][input_row];
558 if(!compressor->rows[0])
560 compressor->rows[0] = calloc(1, sizeof(unsigned char*) * compressor->coded_field_h);
561 compressor->rows[1] = calloc(1, sizeof(unsigned char*) * compressor->coded_field_h);
562 compressor->rows[2] = calloc(1, sizeof(unsigned char*) * compressor->coded_field_h);
565 // User colormodel matches jpeg colormodel
566 if(mjpeg->color_model == BC_YUV422P &&
567 mjpeg->output_w == mjpeg->coded_w &&
568 mjpeg->output_h == mjpeg->coded_h)
570 for(i = 0; i < compressor->coded_field_h; i++)
572 int input_row = get_input_row(mjpeg, compressor, i);
573 compressor->rows[0][i] = mjpeg->y_argument +
574 mjpeg->coded_w * input_row;
575 compressor->rows[1][i] = mjpeg->u_argument +
576 (mjpeg->coded_w / 2) * input_row;
577 compressor->rows[2][i] = mjpeg->v_argument +
578 (mjpeg->coded_w / 2) * input_row;
583 for(i = 0; i < compressor->coded_field_h; i++)
585 int input_row = get_input_row(mjpeg, compressor, i);
586 compressor->rows[0][i] = mjpeg->temp_rows[0][input_row];
587 compressor->rows[1][i] = mjpeg->temp_rows[1][input_row];
588 compressor->rows[2][i] = mjpeg->temp_rows[2][input_row];
596 if(!compressor->rows[0])
598 compressor->rows[0] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h);
599 compressor->rows[1] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h / 2);
600 compressor->rows[2] = calloc(1, sizeof(unsigned char*) * mjpeg->coded_h / 2);
603 // User colormodel matches jpeg colormodel
604 if(mjpeg->color_model == BC_YUV420P &&
605 mjpeg->output_w == mjpeg->coded_w &&
606 mjpeg->output_h == mjpeg->coded_h)
608 for(i = 0; i < compressor->coded_field_h; i++)
610 int input_row = get_input_row(mjpeg, compressor, i);
611 compressor->rows[0][i] = mjpeg->y_argument +
612 mjpeg->coded_w * input_row;
613 if(i < compressor->coded_field_h / 2)
615 compressor->rows[1][i] = mjpeg->u_argument +
616 (mjpeg->coded_w / 2) * input_row;
617 compressor->rows[2][i] = mjpeg->v_argument +
618 (mjpeg->coded_w / 2) * input_row;
624 for(i = 0; i < compressor->coded_field_h; i++)
626 int input_row = get_input_row(mjpeg, compressor, i);
627 compressor->rows[0][i] = mjpeg->temp_rows[0][input_row];
628 if(i < compressor->coded_field_h / 2)
630 compressor->rows[1][i] = mjpeg->temp_rows[1][input_row];
631 compressor->rows[2][i] = mjpeg->temp_rows[2][input_row];
640 static void delete_rows(mjpeg_compressor *compressor)
642 if(compressor->rows[0])
644 free(compressor->rows[0]);
645 free(compressor->rows[1]);
646 free(compressor->rows[2]);
650 /* replacement routine to suppress decompress warnings */
651 static void mjpeg_emit_message(j_common_ptr cinfo, int msg_level)
653 struct jpeg_error_mgr * err = cinfo->err;
656 if(err->trace_level >= 3)
657 (*err->output_message) (cinfo);
659 if (err->trace_level >= msg_level)
660 (*err->output_message) (cinfo);
664 static void new_jpeg_objects(mjpeg_compressor *engine)
666 engine->jpeg_decompress.err = jpeg_std_error(&(engine->jpeg_error.pub));
667 engine->jpeg_error.pub.error_exit = mjpeg_error_exit;
668 engine->jpeg_error.pub.emit_message = mjpeg_emit_message;
669 /* Ideally the error handler would be set here but it must be called in a thread */
670 jpeg_create_decompress(&(engine->jpeg_decompress));
671 engine->jpeg_decompress.raw_data_out = TRUE;
672 #if JPEG_LIB_VERSION >= 70
673 engine->jpeg_decompress.do_fancy_upsampling = FALSE;
675 engine->jpeg_decompress.dct_method = JDCT_IFAST;
678 static void delete_jpeg_objects(mjpeg_compressor *engine)
680 jpeg_destroy_decompress(&(engine->jpeg_decompress));
685 static void unlock_compress_loop(mjpeg_compressor *engine)
687 pthread_mutex_unlock(&(engine->input_lock));
690 static void lock_compress_loop(mjpeg_compressor *engine)
692 pthread_mutex_lock(&(engine->output_lock));
695 // Make temp rows for compressor
696 static void get_mcu_rows(mjpeg_t *mjpeg,
697 mjpeg_compressor *engine,
701 for(i = 0; i < 3; i++)
703 for(j = 0; j < 16; j++)
705 if(i > 0 && j >= 8 && mjpeg->jpeg_color_model == BC_YUV420P) break;
707 scanline = start_row;
708 if(i > 0 && mjpeg->jpeg_color_model == BC_YUV420P) scanline /= 2;
710 if(scanline >= engine->coded_field_h) scanline = engine->coded_field_h - 1;
711 engine->mcu_rows[i][j] = engine->rows[i][scanline];
717 static void decompress_field(mjpeg_compressor *engine)
719 mjpeg_t *mjpeg = engine->mjpeg;
720 long buffer_offset = engine->instance * mjpeg->input_field2;
721 unsigned char *buffer = mjpeg->input_data + buffer_offset;
724 //printf("decompress_field %02x%02x %d\n", buffer[0], buffer[1], engine->instance * mjpeg->input_field2);
725 if(engine->instance == 0 && mjpeg->fields > 1)
726 buffer_size = mjpeg->input_field2 - buffer_offset;
728 buffer_size = mjpeg->input_size - buffer_offset;
732 if(setjmp(engine->jpeg_error.setjmp_buffer))
734 /* If we get here, the JPEG code has signaled an error. */
735 printf("decompress_field %d\n", __LINE__);
736 delete_jpeg_objects(engine);
737 printf("decompress_field %d\n", __LINE__);
738 new_jpeg_objects(engine);
739 printf("decompress_field %d\n", __LINE__);
741 printf("decompress_field %d\n", __LINE__);
745 //printf("decompress_field 2\n");
746 jpeg_buffer_src(&engine->jpeg_decompress,
749 jpeg_read_header(&engine->jpeg_decompress, TRUE);
751 if ( engine->jpeg_decompress.ac_huff_tbl_ptrs[0] == NULL &&
752 engine->jpeg_decompress.ac_huff_tbl_ptrs[1] == NULL &&
753 engine->jpeg_decompress.dc_huff_tbl_ptrs[0] == NULL &&
754 engine->jpeg_decompress.dc_huff_tbl_ptrs[1] == NULL )
755 jpeg_load_dht( &engine->jpeg_decompress,
757 engine->jpeg_decompress.ac_huff_tbl_ptrs,
758 engine->jpeg_decompress.dc_huff_tbl_ptrs );
759 // Reset by jpeg_read_header
760 engine->jpeg_decompress.raw_data_out = TRUE;
761 #if JPEG_LIB_VERSION >= 70
762 engine->jpeg_decompress.do_fancy_upsampling = FALSE;
764 jpeg_start_decompress(&engine->jpeg_decompress);
766 // Generate colormodel from jpeg sampling
767 if(engine->jpeg_decompress.comp_info[0].v_samp_factor == 2 &&
768 engine->jpeg_decompress.comp_info[0].h_samp_factor == 2)
769 mjpeg->jpeg_color_model = BC_YUV420P;
771 if(engine->jpeg_decompress.comp_info[0].v_samp_factor == 1 &&
772 engine->jpeg_decompress.comp_info[0].h_samp_factor == 2)
773 mjpeg->jpeg_color_model = BC_YUV422P;
775 mjpeg->jpeg_color_model = BC_YUV444P;
777 if(engine->jpeg_decompress.jpeg_color_space == JCS_GRAYSCALE)
778 mjpeg->greyscale = 1;
780 //printf("%d %d\n", engine->jpeg_decompress.comp_info[0].h_samp_factor, engine->jpeg_decompress.comp_info[0].v_samp_factor);
781 // Must be here because the color model isn't known until now
782 pthread_mutex_lock(&(mjpeg->decompress_init));
783 allocate_temps(mjpeg);
784 pthread_mutex_unlock(&(mjpeg->decompress_init));
785 get_rows(mjpeg, engine);
787 //printf("decompress_field 30\n");
789 while(engine->jpeg_decompress.output_scanline < engine->jpeg_decompress.output_height)
791 get_mcu_rows(mjpeg, engine, engine->jpeg_decompress.output_scanline);
792 jpeg_read_raw_data(&engine->jpeg_decompress,
794 engine->coded_field_h);
796 jpeg_finish_decompress(&engine->jpeg_decompress);
798 //printf("decompress_field 40\n");
804 void mjpeg_decompress_loop(mjpeg_compressor *engine)
808 pthread_mutex_lock(&engine->input_lock);
811 decompress_field(engine);
813 pthread_mutex_unlock(&(engine->output_lock));
818 static void compress_field(mjpeg_compressor *engine)
820 mjpeg_t *mjpeg = engine->mjpeg;
822 //printf("compress_field 1\n");
823 get_rows(engine->mjpeg, engine);
824 reset_buffer(&engine->output_buffer, &engine->output_size, &engine->output_allocated);
825 jpeg_buffer_dest(&engine->jpeg_compress, engine);
828 engine->jpeg_compress.raw_data_in = TRUE;
829 #if JPEG_LIB_VERSION >= 70
830 engine->jpeg_compress.do_fancy_downsampling = FALSE;
832 jpeg_start_compress(&engine->jpeg_compress, TRUE);
834 while(engine->jpeg_compress.next_scanline < engine->jpeg_compress.image_height)
836 get_mcu_rows(mjpeg, engine, engine->jpeg_compress.next_scanline);
838 jpeg_write_raw_data(&engine->jpeg_compress,
840 engine->coded_field_h);
842 jpeg_finish_compress(&engine->jpeg_compress);
843 //printf("compress_field 2\n");
847 void mjpeg_compress_loop(mjpeg_compressor *engine)
851 pthread_mutex_lock(&engine->input_lock);
854 compress_field(engine);
856 pthread_mutex_unlock(&engine->output_lock);
860 static void delete_temps(mjpeg_t *mjpeg)
864 free(mjpeg->temp_data);
865 free(mjpeg->temp_rows[0]);
866 free(mjpeg->temp_rows[1]);
867 free(mjpeg->temp_rows[2]);
868 mjpeg->temp_data = 0;
872 mjpeg_compressor* mjpeg_new_decompressor(mjpeg_t *mjpeg, int instance)
874 mjpeg_compressor *result = calloc(1, sizeof(mjpeg_compressor));
876 pthread_mutexattr_t mutex_attr;
878 result->mjpeg = mjpeg;
879 result->instance = instance;
880 new_jpeg_objects(result);
881 result->field_h = mjpeg->output_h / mjpeg->fields;
882 result->coded_field_h = (result->field_h % 16) ?
883 result->field_h + (16 - (result->field_h % 16)) : result->field_h;
885 result->mcu_rows[0] = malloc(16 * sizeof(unsigned char*));
886 result->mcu_rows[1] = malloc(16 * sizeof(unsigned char*));
887 result->mcu_rows[2] = malloc(16 * sizeof(unsigned char*));
889 pthread_mutexattr_init(&mutex_attr);
890 // pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP);
891 pthread_mutex_init(&(result->input_lock), &mutex_attr);
892 pthread_mutex_lock(&(result->input_lock));
893 pthread_mutex_init(&(result->output_lock), &mutex_attr);
894 pthread_mutex_lock(&(result->output_lock));
896 pthread_attr_init(&attr);
897 pthread_create(&(result->tid), &attr, (void*)mjpeg_decompress_loop, result);
902 void mjpeg_delete_decompressor(mjpeg_compressor *engine)
905 pthread_mutex_unlock(&(engine->input_lock));
906 pthread_join(engine->tid, 0);
907 pthread_mutex_destroy(&(engine->input_lock));
908 pthread_mutex_destroy(&(engine->output_lock));
909 jpeg_destroy_decompress(&(engine->jpeg_decompress));
911 free(engine->mcu_rows[0]);
912 free(engine->mcu_rows[1]);
913 free(engine->mcu_rows[2]);
917 mjpeg_compressor* mjpeg_new_compressor(mjpeg_t *mjpeg, int instance)
920 pthread_mutexattr_t mutex_attr;
921 mjpeg_compressor *result = calloc(1, sizeof(mjpeg_compressor));
923 result->field_h = mjpeg->output_h / mjpeg->fields;
924 result->coded_field_h = (result->field_h % 16) ?
925 result->field_h + (16 - (result->field_h % 16)) : result->field_h;
926 result->mjpeg = mjpeg;
927 result->instance = instance;
928 result->jpeg_compress.err = jpeg_std_error(&(result->jpeg_error.pub));
929 jpeg_create_compress(&(result->jpeg_compress));
930 result->jpeg_compress.image_width = mjpeg->output_w;
931 result->jpeg_compress.image_height = result->field_h;
932 result->jpeg_compress.input_components = 3;
933 result->jpeg_compress.in_color_space = JCS_RGB;
934 jpeg_set_defaults(&(result->jpeg_compress));
935 result->jpeg_compress.input_components = 3;
936 result->jpeg_compress.in_color_space = JCS_RGB;
937 jpeg_set_quality(&(result->jpeg_compress), mjpeg->quality, 0);
940 result->jpeg_compress.dct_method = JDCT_FLOAT;
942 result->jpeg_compress.dct_method = JDCT_IFAST;
943 // result->jpeg_compress.dct_method = JDCT_ISLOW;
946 switch(mjpeg->fields)
949 mjpeg->jpeg_color_model = BC_YUV420P;
950 result->jpeg_compress.comp_info[0].h_samp_factor = 2;
951 result->jpeg_compress.comp_info[0].v_samp_factor = 2;
952 result->jpeg_compress.comp_info[1].h_samp_factor = 1;
953 result->jpeg_compress.comp_info[1].v_samp_factor = 1;
954 result->jpeg_compress.comp_info[2].h_samp_factor = 1;
955 result->jpeg_compress.comp_info[2].v_samp_factor = 1;
958 mjpeg->jpeg_color_model = BC_YUV422P;
959 result->jpeg_compress.comp_info[0].h_samp_factor = 2;
960 result->jpeg_compress.comp_info[0].v_samp_factor = 1;
961 result->jpeg_compress.comp_info[1].h_samp_factor = 1;
962 result->jpeg_compress.comp_info[1].v_samp_factor = 1;
963 result->jpeg_compress.comp_info[2].h_samp_factor = 1;
964 result->jpeg_compress.comp_info[2].v_samp_factor = 1;
967 allocate_temps(mjpeg);
969 result->mcu_rows[0] = malloc(16 * sizeof(unsigned char*));
970 result->mcu_rows[1] = malloc(16 * sizeof(unsigned char*));
971 result->mcu_rows[2] = malloc(16 * sizeof(unsigned char*));
973 pthread_mutexattr_init(&mutex_attr);
974 // pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP);
975 pthread_mutex_init(&(result->input_lock), &mutex_attr);
976 pthread_mutex_lock(&(result->input_lock));
977 pthread_mutex_init(&(result->output_lock), &mutex_attr);
978 pthread_mutex_lock(&(result->output_lock));
980 pthread_attr_init(&attr);
981 pthread_create(&(result->tid), &attr, (void*)mjpeg_compress_loop, result);
986 void mjpeg_delete_compressor(mjpeg_compressor *engine)
989 pthread_mutex_unlock(&(engine->input_lock));
990 pthread_join(engine->tid, 0);
991 pthread_mutex_destroy(&(engine->input_lock));
992 pthread_mutex_destroy(&(engine->output_lock));
993 jpeg_destroy((j_common_ptr)&(engine->jpeg_compress));
994 if(engine->output_buffer) free(engine->output_buffer);
996 free(engine->mcu_rows[0]);
997 free(engine->mcu_rows[1]);
998 free(engine->mcu_rows[2]);
1002 unsigned char* mjpeg_output_buffer(mjpeg_t *mjpeg)
1004 return mjpeg->output_data;
1007 long mjpeg_output_field2(mjpeg_t *mjpeg)
1009 return mjpeg->output_field2;
1012 long mjpeg_output_size(mjpeg_t *mjpeg)
1014 return mjpeg->output_size;
1017 long mjpeg_output_allocated(mjpeg_t *mjpeg)
1019 return mjpeg->output_allocated;
1022 void mjpeg_set_output_size(mjpeg_t *mjpeg, long output_size)
1024 mjpeg->output_size = output_size;
1028 int mjpeg_compress(mjpeg_t *mjpeg,
1029 unsigned char **row_pointers,
1030 unsigned char *y_plane,
1031 unsigned char *u_plane,
1032 unsigned char *v_plane,
1037 int corrected_fields = mjpeg->fields;
1038 mjpeg->color_model = color_model;
1041 //printf("mjpeg_compress 1 %d\n", color_model);
1042 /* Reset output buffer */
1043 reset_buffer(&mjpeg->output_data,
1044 &mjpeg->output_size,
1045 &mjpeg->output_allocated);
1047 /* Create compression engines as needed */
1048 for(i = 0; i < mjpeg->fields; i++)
1050 if(!mjpeg->compressors[i])
1052 mjpeg->compressors[i] = mjpeg_new_compressor(mjpeg, i);
1056 /* Arm YUV buffers */
1057 mjpeg->row_argument = row_pointers;
1058 mjpeg->y_argument = y_plane;
1059 mjpeg->u_argument = u_plane;
1060 mjpeg->v_argument = v_plane;
1061 // User colormodel doesn't match encoder colormodel
1062 // Copy to interlacing buffer first
1063 if(mjpeg->color_model != mjpeg->jpeg_color_model ||
1064 mjpeg->output_w != mjpeg->coded_w ||
1065 mjpeg->output_h != mjpeg->coded_h)
1068 * printf("mjpeg_compress %d %d %d %d\n",
1069 * mjpeg->output_w, mjpeg->output_h, mjpeg->coded_w, mjpeg->coded_h);
1073 mjpeg->temp_rows[0][0],
1074 mjpeg->temp_rows[1][0],
1075 mjpeg->temp_rows[2][0],
1088 mjpeg->jpeg_color_model,
1094 /* Start the compressors on the image fields */
1095 if(mjpeg->deinterlace) corrected_fields = 1;
1096 for(i = 0; i < corrected_fields && !result; i++)
1098 unlock_compress_loop(mjpeg->compressors[i]);
1100 if(mjpeg->cpus < 2 && i < corrected_fields - 1)
1102 lock_compress_loop(mjpeg->compressors[i]);
1106 /* Wait for the compressors and store in master output */
1107 for(i = 0; i < corrected_fields && !result; i++)
1109 if(mjpeg->cpus > 1 || i == corrected_fields - 1)
1111 lock_compress_loop(mjpeg->compressors[i]);
1114 append_buffer(&mjpeg->output_data,
1115 &mjpeg->output_size,
1116 &mjpeg->output_allocated,
1117 mjpeg->compressors[i]->output_buffer,
1118 mjpeg->compressors[i]->output_size);
1119 if(i == 0) mjpeg->output_field2 = mjpeg->output_size;
1122 if(corrected_fields < mjpeg->fields)
1124 append_buffer(&mjpeg->output_data,
1125 &mjpeg->output_size,
1126 &mjpeg->output_allocated,
1127 mjpeg->compressors[0]->output_buffer,
1128 mjpeg->compressors[0]->output_size);
1130 //printf("mjpeg_compress 2\n");
1136 int mjpeg_decompress(mjpeg_t *mjpeg,
1137 unsigned char *buffer,
1140 unsigned char **row_pointers,
1141 unsigned char *y_plane,
1142 unsigned char *u_plane,
1143 unsigned char *v_plane,
1148 int got_first_thread = 0;
1150 //printf("mjpeg_decompress 1 %d\n", color_model);
1151 if(buffer_len == 0) return 1;
1152 if(input_field2 == 0 && mjpeg->fields > 1) return 1;
1154 //printf("mjpeg_decompress 2\n");
1155 /* Create decompression engines as needed */
1156 for(i = 0; i < mjpeg->fields; i++)
1158 if(!mjpeg->decompressors[i])
1160 mjpeg->decompressors[i] = mjpeg_new_decompressor(mjpeg, i);
1164 //printf("mjpeg_decompress 3\n");
1165 /* Arm YUV buffers */
1166 mjpeg->row_argument = row_pointers;
1167 mjpeg->y_argument = y_plane;
1168 mjpeg->u_argument = u_plane;
1169 mjpeg->v_argument = v_plane;
1170 mjpeg->input_data = buffer;
1171 mjpeg->input_size = buffer_len;
1172 mjpeg->input_field2 = input_field2;
1173 mjpeg->color_model = color_model;
1176 //printf("mjpeg_decompress 4 %02x %02x %d %02x %02x\n", buffer[0], buffer[1], input_field2, buffer[input_field2], buffer[input_field2 + 1]);
1177 /* Start decompressors */
1178 for(i = 0; i < mjpeg->fields && !result; i++)
1180 //printf("mjpeg_decompress 5\n");
1181 unlock_compress_loop(mjpeg->decompressors[i]);
1182 //printf("mjpeg_decompress 6\n");
1184 // For dual CPUs, don't want second thread to start until temp data is allocated by the first.
1185 // For single CPUs, don't want two threads running simultaneously
1186 if(mjpeg->cpus < 2 || !mjpeg->temp_data)
1188 //printf("mjpeg_decompress 7\n");
1189 lock_compress_loop(mjpeg->decompressors[i]);
1190 //printf("mjpeg_decompress 8\n");
1191 if(i == 0) got_first_thread = 1;
1195 //printf("mjpeg_decompress 10\n");
1196 /* Wait for decompressors */
1197 for(i = 0; i < mjpeg->fields && !result; i++)
1201 if(i > 0 || !got_first_thread)
1202 lock_compress_loop(mjpeg->decompressors[i]);
1206 /* Convert colormodel */
1207 // User colormodel didn't match decompressor
1209 * if(!mjpeg->error &&
1210 * (mjpeg->jpeg_color_model != mjpeg->color_model ||
1211 * mjpeg->coded_w != mjpeg->output_w ||
1212 * mjpeg->coded_h != mjpeg->output_h))
1215 //printf("mjpeg_decompress 6 %d %d %d %d\n", mjpeg->coded_w, mjpeg->coded_h, mjpeg->output_w, mjpeg->output_h);
1216 if((mjpeg->jpeg_color_model != mjpeg->color_model ||
1217 mjpeg->coded_w != mjpeg->output_w ||
1218 mjpeg->coded_h != mjpeg->output_h)
1220 (mjpeg->temp_data ||
1223 unsigned char *y_in = mjpeg->temp_rows[0][0];
1224 unsigned char *u_in = mjpeg->temp_rows[1][0];
1225 unsigned char *v_in = mjpeg->temp_rows[2][0];
1229 * printf("mjpeg_decompress 7 coded_w=%d coded_h=%d output_w=%d output_h=%d out_rowspan=%d in_colormodel=%d out_colormodel=%d\n",
1234 * mjpeg->rowspan ? mjpeg->rowspan : mjpeg->output_w,
1235 * mjpeg->jpeg_color_model,
1236 * mjpeg->color_model);
1239 cmodel_transfer(row_pointers,
1255 mjpeg->jpeg_color_model,
1259 mjpeg->rowspan ? mjpeg->rowspan : mjpeg->output_w);
1260 //printf("mjpeg_decompress 8\n");
1266 void mjpeg_set_deinterlace(mjpeg_t *mjpeg, int value)
1268 mjpeg->deinterlace = value;
1271 void mjpeg_set_quality(mjpeg_t *mjpeg, int quality)
1273 mjpeg->quality = quality;
1276 void mjpeg_set_float(mjpeg_t *mjpeg, int use_float)
1278 mjpeg->use_float = use_float;
1281 void mjpeg_set_cpus(mjpeg_t *mjpeg, int cpus)
1286 void mjpeg_set_rowspan(mjpeg_t *mjpeg, int rowspan)
1288 mjpeg->rowspan = rowspan;
1291 int mjpeg_get_fields(mjpeg_t *mjpeg)
1293 return mjpeg->fields;
1297 mjpeg_t* mjpeg_new(int w,
1301 mjpeg_t *result = calloc(1, sizeof(mjpeg_t));
1302 pthread_mutexattr_t mutex_attr;
1304 result->output_w = w;
1305 result->output_h = h;
1306 result->fields = fields;
1307 result->color_model = BC_RGB888;
1309 result->quality = 80;
1310 result->use_float = 0;
1312 pthread_mutexattr_init(&mutex_attr);
1313 // pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP);
1314 pthread_mutex_init(&(result->decompress_init), &mutex_attr);
1317 // Calculate coded dimensions
1318 // An interlaced frame with 4:2:0 sampling must be a multiple of 32
1320 result->coded_w = (w % 16) ? w + (16 - (w % 16)) : w;
1323 result->coded_h = (h % 16) ? h + (16 - (h % 16)) : h;
1325 result->coded_h = (h % 32) ? h + (32 - (h % 32)) : h;
1329 //printf("mjpeg_new %d %d %d %d\n", result->output_w, result->output_h, result->coded_w, result->coded_h);
1336 void mjpeg_delete(mjpeg_t *mjpeg)
1339 //printf("mjpeg_delete 1\n");
1340 for(i = 0; i < mjpeg->fields; i++)
1342 //printf("mjpeg_delete 2\n");
1343 if(mjpeg->compressors[i]) mjpeg_delete_compressor(mjpeg->compressors[i]);
1344 //printf("mjpeg_delete 3\n");
1345 if(mjpeg->decompressors[i]) mjpeg_delete_decompressor(mjpeg->decompressors[i]);
1346 //printf("mjpeg_delete 4\n");
1348 //printf("mjpeg_delete 5\n");
1349 delete_temps(mjpeg);
1350 //printf("mjpeg_delete 6\n");
1351 delete_buffer(&mjpeg->output_data, &mjpeg->output_size, &mjpeg->output_allocated);
1352 //printf("mjpeg_delete 7\n");
1354 //printf("mjpeg_delete 2\n");
1358 /* Open up a space to insert a marker */
1359 static void insert_space(unsigned char **buffer,
1361 long *buffer_allocated,
1366 // Make sure enough space is available
1367 if(*buffer_allocated - *buffer_size < space_len)
1369 *buffer_allocated += space_len;
1370 *buffer = realloc(*buffer, *buffer_allocated);
1374 for(in = *buffer_size - 1, out = *buffer_size - 1 + space_len;
1378 (*buffer)[out] = (*buffer)[in];
1380 *buffer_size += space_len;
1384 static inline int nextbyte(unsigned char *data, long *offset, long length)
1386 if(length - *offset < 1) return 0;
1388 return (unsigned char)data[*offset - 1];
1391 static inline int read_int32(unsigned char *data, long *offset, long length)
1393 if(length - *offset < 4)
1399 return ((((unsigned int)data[*offset - 4]) << 24) |
1400 (((unsigned int)data[*offset - 3]) << 16) |
1401 (((unsigned int)data[*offset - 2]) << 8) |
1402 (((unsigned int)data[*offset - 1])));
1405 static inline int read_int16(unsigned char *data, long *offset, long length)
1407 if(length - *offset < 2)
1414 return ((((unsigned int)data[*offset - 2]) << 8) |
1415 (((unsigned int)data[*offset - 1])));
1418 static inline unsigned char read_char(unsigned char *data, long *offset, long length)
1420 if(length - *offset < 1)
1427 return (unsigned char)data[*offset - 1];
1430 static inline int next_int16(unsigned char *data, long *offset, long length)
1432 if(length - *offset < 2)
1437 return ((((unsigned int)data[*offset]) << 8) |
1438 (((unsigned int)data[*offset + 1])));
1441 static inline void write_int32(unsigned char *data, long *offset, long length, unsigned int value)
1443 if(length - *offset < 4)
1450 data[(*offset)++] = (unsigned int)(value & 0xff000000) >> 24;
1451 data[(*offset)++] = (unsigned int)(value & 0xff0000) >> 16;
1452 data[(*offset)++] = (unsigned int)(value & 0xff00) >> 8;
1453 data[(*offset)++] = (unsigned char)(value & 0xff);
1457 static inline void write_char(unsigned char *data, long *offset, long length, unsigned char value)
1459 if(length - *offset < 1)
1465 data[(*offset)++] = value;
1469 static int next_marker(unsigned char *buffer, long *offset, long buffer_size)
1471 while(*offset < buffer_size - 1) {
1472 if(buffer[*offset] == 0xff && buffer[*offset + 1] != 0xff) {
1474 return buffer[*offset - 1];
1481 /* Find the next marker after offset and return 0 on success */
1482 static int find_marker(unsigned char *buffer,
1485 unsigned long marker_type)
1489 while(!result && *offset < buffer_size - 1)
1491 int marker = next_marker(buffer, offset, buffer_size);
1492 if(marker == (marker_type & 0xff)) result = 1;
1502 int padded_field_size;
1515 int unpadded_field_size;
1518 #define LML_MARKER_SIZE 0x2c
1519 #define LML_MARKER_TAG 0xffe3
1520 void insert_lml33_markers(unsigned char **buffer,
1521 long *field2_offset,
1523 long *buffer_allocated)
1525 long marker_offset = -1;
1527 /* Search for existing marker to replace */
1528 // marker_offset = find_marker(*buffer, *buffer_size, LML_MARKER_TAG);
1530 /* Insert new marker */
1531 if(marker_offset < 0)
1534 insert_space(buffer,
1542 static int qt_table_offsets(unsigned char *buffer,
1553 bzero(header, sizeof(qt_hdr_t) * 2);
1555 // Read every marker to get the offsets for the headers
1556 for(field = 0; field < 2; field++)
1562 marker = next_marker(buffer,
1571 // The first field may be padded
1574 header[0].next_offset =
1575 header[0].padded_field_size =
1582 // Quicktime marker already exists. Abort.
1583 if(buffer[offset + 6] == 'm' &&
1584 buffer[offset + 7] == 'j' &&
1585 buffer[offset + 8] == 'p' &&
1586 buffer[offset + 9] == 'a')
1594 if(!header[field].quant_offset)
1596 header[field].quant_offset = offset - 2;
1598 header[field].quant_offset -= header[0].next_offset;
1600 len = read_int16(buffer, &offset, buffer_size);
1605 if(!header[field].huffman_offset)
1607 header[field].huffman_offset = offset - 2;
1609 header[field].huffman_offset -= header[0].next_offset;
1611 len = read_int16(buffer, &offset, buffer_size);
1616 if(!header[field].image_offset)
1618 header[field].image_offset = offset - 2;
1620 header[field].image_offset -= header[0].next_offset;
1622 len = read_int16(buffer, &offset, buffer_size);
1627 header[field].scan_offset = offset - 2;
1629 header[field].scan_offset -= header[0].next_offset;
1630 len = read_int16(buffer, &offset, buffer_size);
1632 header[field].data_offset = offset + len;
1634 header[field].data_offset -= header[0].next_offset;
1641 header[field].field_size =
1642 header[field].padded_field_size =
1643 offset - header[0].next_offset;
1644 header[field].next_offset = 0;
1648 // Often misses second SOI but gets first EOI
1649 // header[0].next_offset =
1650 // header[0].padded_field_size =
1653 //printf("table_offsets M_EOI %d %x\n", field, offset);
1659 // Junk appears between fields
1661 // len = read_int16(buffer, &offset, buffer_size);
1666 if(!done) offset += len;
1667 if(offset >= buffer_size) done = 1;
1669 //printf("qt_table_offsets 10 %d\n", field);
1675 static void insert_quicktime_marker(unsigned char *buffer,
1680 write_int32(buffer, &offset, buffer_size, 0xff000000 |
1681 ((unsigned long)M_APP1 << 16) |
1682 (QUICKTIME_MARKER_SIZE - 2));
1683 write_int32(buffer, &offset, buffer_size, 0);
1684 write_int32(buffer, &offset, buffer_size, QUICKTIME_JPEG_TAG);
1685 write_int32(buffer, &offset, buffer_size, header->field_size);
1686 write_int32(buffer, &offset, buffer_size, header->padded_field_size);
1687 write_int32(buffer, &offset, buffer_size, header->next_offset);
1688 write_int32(buffer, &offset, buffer_size, header->quant_offset);
1689 write_int32(buffer, &offset, buffer_size, header->huffman_offset);
1690 write_int32(buffer, &offset, buffer_size, header->image_offset);
1691 write_int32(buffer, &offset, buffer_size, header->scan_offset);
1692 write_int32(buffer, &offset, buffer_size, header->data_offset);
1696 void mjpeg_insert_quicktime_markers(unsigned char **buffer,
1698 long *buffer_allocated,
1700 long *field2_offset)
1704 *field2_offset = -1;
1706 if(fields < 2) return;
1709 // Get offsets for tables in both fields
1710 exists = qt_table_offsets(*buffer, *buffer_size, header);
1712 // APP1 for quicktime already exists
1715 //printf("mjpeg_insert_quicktime_markers %x %02x %02x\n",
1716 // header[0].next_offset, (*buffer)[*field2_offset], (*buffer)[*field2_offset + 1]);
1717 //if(*field2_offset == 0)
1718 // fwrite(*buffer, *buffer_size, 1, stdout);
1722 header[0].field_size += QUICKTIME_MARKER_SIZE;
1723 header[0].padded_field_size += QUICKTIME_MARKER_SIZE;
1724 header[0].next_offset += QUICKTIME_MARKER_SIZE;
1725 header[0].quant_offset += QUICKTIME_MARKER_SIZE;
1726 header[0].huffman_offset += QUICKTIME_MARKER_SIZE;
1727 header[0].image_offset += QUICKTIME_MARKER_SIZE;
1728 header[0].scan_offset += QUICKTIME_MARKER_SIZE;
1729 header[0].data_offset += QUICKTIME_MARKER_SIZE;
1730 header[1].field_size += QUICKTIME_MARKER_SIZE;
1731 header[1].padded_field_size += QUICKTIME_MARKER_SIZE;
1732 header[1].quant_offset += QUICKTIME_MARKER_SIZE;
1733 header[1].huffman_offset += QUICKTIME_MARKER_SIZE;
1734 header[1].image_offset += QUICKTIME_MARKER_SIZE;
1735 header[1].scan_offset += QUICKTIME_MARKER_SIZE;
1736 header[1].data_offset += QUICKTIME_MARKER_SIZE;
1737 *field2_offset = header[0].next_offset;
1741 // Insert APP1 marker
1742 insert_space(buffer,
1746 QUICKTIME_MARKER_SIZE);
1748 insert_quicktime_marker(*buffer,
1753 insert_space(buffer,
1756 header[0].next_offset + 2,
1757 QUICKTIME_MARKER_SIZE);
1759 header[1].next_offset = 0;
1760 insert_quicktime_marker(*buffer,
1762 header[0].next_offset + 2,
1767 static int avi_table_offsets(unsigned char *buffer,
1771 int field2 = mjpeg_get_field2(buffer, buffer_size);
1773 header[0].field_number = 1;
1774 header[0].field_size = field2;
1775 header[0].unpadded_field_size = field2;
1777 header[1].field_number = 2;
1778 header[1].field_size = buffer_size - field2;
1779 header[1].unpadded_field_size = buffer_size - field2;
1783 static void insert_avi_marker(unsigned char *buffer,
1788 write_int32(buffer, &offset, buffer_size, 0xff000000 |
1789 ((unsigned long)M_APP0 << 16) |
1790 (AVI_MARKER_SIZE - 2));
1791 write_int32(buffer, &offset, buffer_size, QUICKTIME_AVI_TAG);
1793 // One version of McRoweSoft only allows field polarity while
1794 // another version allows field size.
1795 write_char(buffer, &offset, buffer_size, header->field_number);
1796 write_char(buffer, &offset, buffer_size, 0);
1797 write_int32(buffer, &offset, buffer_size, header->field_size);
1798 write_int32(buffer, &offset, buffer_size, header->unpadded_field_size);
1801 void mjpeg_insert_avi_markers(unsigned char **buffer,
1803 long *buffer_allocated,
1805 long *field2_offset)
1807 avi_hdr_t header[2];
1809 *field2_offset = -1;
1812 // Test for existing marker
1813 if(!find_marker(*buffer, &offset, *buffer_size, M_APP0))
1815 if((*buffer)[offset + 2] == 'A' &&
1816 (*buffer)[offset + 3] == 'V' &&
1817 (*buffer)[offset + 4] == 'I' &&
1818 (*buffer)[offset + 5] == '1')
1823 avi_table_offsets(*buffer, *buffer_size, header);
1825 header[0].field_size += AVI_MARKER_SIZE;
1826 header[0].unpadded_field_size += AVI_MARKER_SIZE;
1827 header[1].field_size += AVI_MARKER_SIZE;
1828 header[1].unpadded_field_size += AVI_MARKER_SIZE;
1829 *field2_offset = header[0].field_size;
1831 // Insert APP0 marker into field 1
1832 insert_space(buffer,
1837 insert_avi_marker(*buffer,
1842 insert_space(buffer,
1847 insert_avi_marker(*buffer,
1857 static void read_avi_markers(unsigned char *buffer,
1862 int marker_count = 0;
1864 int marker_size = 0;
1865 while(marker_count < 2 && offset < buffer_size && !result)
1867 result = find_marker(buffer,
1871 marker_size = ((unsigned char)buffer[offset] << 8) | (unsigned char)buffer[offset];
1874 if(!result && marker_size >= 16)
1876 // Marker size, AVI1
1879 header[marker_count].field_number = read_char(buffer, &offset, buffer_size);
1880 read_char(buffer, &offset, buffer_size);
1881 header[marker_count].field_size = read_int32(buffer, &offset, buffer_size);
1882 header[marker_count].unpadded_field_size = read_int32(buffer, &offset, buffer_size);
1889 static void read_quicktime_markers(unsigned char *buffer,
1894 int marker_count = 0;
1897 while(marker_count < 2 && offset < buffer_size && !result)
1899 result = find_marker(buffer,
1907 read_int16(buffer, &offset, buffer_size);
1909 read_int32(buffer, &offset, buffer_size);
1911 read_int32(buffer, &offset, buffer_size);
1913 header[marker_count].field_size = read_int32(buffer, &offset, buffer_size);
1914 header[marker_count].padded_field_size = read_int32(buffer, &offset, buffer_size);
1915 header[marker_count].next_offset = read_int32(buffer, &offset, buffer_size);
1916 header[marker_count].quant_offset = read_int32(buffer, &offset, buffer_size);
1917 header[marker_count].huffman_offset = read_int32(buffer, &offset, buffer_size);
1918 header[marker_count].image_offset = read_int32(buffer, &offset, buffer_size);
1919 header[marker_count].scan_offset = read_int32(buffer, &offset, buffer_size);
1920 header[marker_count].data_offset = read_int32(buffer, &offset, buffer_size);
1924 //printf("read_quicktime_markers 1 %d\n", marker_count);
1927 long mjpeg_get_quicktime_field2(unsigned char *buffer, long buffer_size)
1930 bzero(&header, sizeof(qt_hdr_t) * 2);
1932 read_quicktime_markers(buffer, buffer_size, header);
1933 return header[0].next_offset;
1936 long mjpeg_get_avi_field2(unsigned char *buffer,
1938 int *field_dominance)
1940 avi_hdr_t header[2];
1941 bzero(&header, sizeof(avi_hdr_t) * 2);
1942 read_avi_markers(buffer, buffer_size, header);
1944 *field_dominance = (header[0].field_number == 1) ? 1 : 2;
1946 // One version of McRoweSoft only allows field polarity while
1947 // another version allows field size.
1948 if(header[0].field_size)
1950 return header[0].field_size;
1954 return mjpeg_get_field2(buffer, buffer_size);
1959 long mjpeg_get_field2(unsigned char *buffer, long buffer_size)
1961 int total_fields = 0;
1962 long field2_offset = 0;
1965 for(i = 0; i < buffer_size; i++)
1967 if(buffer[i] == 0xff && buffer[i + 1] == M_SOI)
1971 if(total_fields == 2) break;
1974 return field2_offset;
1977 void mjpeg_video_size(unsigned char *data, long data_size, int *w, int *h)
1984 *h = (data[offset + 3] << 8) | (data[offset + 4]);
1985 *w = (data[offset + 5] << 8) | (data[offset + 6]);