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
22 /* Motion JPEG library */
34 #include <png.h> /* Need setjmp.h included by png.h */
38 #define QUICKTIME_MJPA_MARKSIZE 40
39 #define QUICKTIME_JPEG_TAG 0x6d6a7067
42 struct mjpeg_error_mgr
{
43 struct jpeg_error_mgr pub
; /* "public" fields */
44 jmp_buf setjmp_buffer
; /* for return to caller */
47 typedef struct mjpeg_error_mgr
* mjpeg_error_ptr
;
60 // The compressor structure is shared between decompressors and compressors
65 unsigned char *output_buffer
; /* Buffer for MJPEG output */
66 long output_size
; /* Size of image stored in buffer */
67 long output_allocated
; /* Allocated size of output buffer */
68 struct jpeg_decompress_struct jpeg_decompress
;
69 struct jpeg_compress_struct jpeg_compress
;
70 struct mjpeg_error_mgr jpeg_error
;
71 pthread_t tid
; /* ID of thread */
72 pthread_mutex_t input_lock
, output_lock
;
73 int done
; /* Flag to end */
75 // Pointer to uncompressed YUV rows
76 // [3 planes][downsampled rows][downsampled pixels]
77 unsigned char **rows
[3];
78 /* Temp rows for each MCU */
79 unsigned char **mcu_rows
[3];
80 /* Height of the field */
87 // Dimensions of user frame buffer
90 // Dimensions for encoder frame buffer
97 // Color model of user interface.
99 // Color model of compressed data. Since MJPA streams use 4:2:0
100 int jpeg_color_model
;
101 // Error in compression process
104 mjpeg_compressor
*compressors
[MAXFIELDS
];
105 mjpeg_compressor
*decompressors
[MAXFIELDS
];
107 // Temp frame for interlacing
108 // [3 planes][downsampled rows][downsampled pixels]
109 unsigned char *temp_data
;
110 unsigned char **temp_rows
[3];
111 unsigned char **row_argument
, *y_argument
, *u_argument
, *v_argument
;
113 // Buffer passed to user
114 // This can contain one progressive field or two fields with headers
115 unsigned char *output_data
;
117 long output_allocated
;
118 // Offset to field2 in output_data
120 // Buffer passed from user
121 unsigned char *input_data
;
123 // Offset to field2 in input_data
128 // Workarounds for thread unsafe libraries
129 pthread_mutex_t decompress_init
;
130 int decompress_initialized
;
138 mjpeg_t
* mjpeg_new(int w
,
141 void mjpeg_delete(mjpeg_t
*mjpeg
);
143 void mjpeg_set_quality(mjpeg_t
*mjpeg
, int quality
);
144 void mjpeg_set_float(mjpeg_t
*mjpeg
, int use_float
);
145 // This is useful when producing realtime NTSC output for a JPEG board.
146 void mjpeg_set_deinterlace(mjpeg_t
*mjpeg
, int value
);
147 void mjpeg_set_cpus(mjpeg_t
*mjpeg
, int cpus
);
148 void mjpeg_set_rowspan(mjpeg_t
*mjpeg
, int rowspan
);
151 int mjpeg_get_fields(mjpeg_t
*mjpeg
);
153 int mjpeg_decompress(mjpeg_t
*mjpeg
,
154 unsigned char *buffer
,
157 unsigned char **row_pointers
,
158 unsigned char *y_plane
,
159 unsigned char *u_plane
,
160 unsigned char *v_plane
,
164 int mjpeg_compress(mjpeg_t
*mjpeg
,
165 unsigned char **row_pointers
,
166 unsigned char *y_plane
,
167 unsigned char *u_plane
,
168 unsigned char *v_plane
,
172 // Get buffer information after compressing
173 unsigned char* mjpeg_output_buffer(mjpeg_t
*mjpeg
);
174 long mjpeg_output_field2(mjpeg_t
*mjpeg
);
175 long mjpeg_output_size(mjpeg_t
*mjpeg
);
177 // Retrieve width and height from a buffer of JPEG data
178 void mjpeg_video_size(unsigned char *data
, long data_size
, int *w
, int *h
);
182 // Calculate marker contents and insert them into a buffer.
183 // Reallocates the buffer if it isn't big enough so make sure it's big enough
184 // when passing VFrames.
185 // field2_offset is set to -1 if the markers already exist or the field offset
186 // if markers don't already exist.
187 void mjpeg_insert_quicktime_markers(unsigned char **buffer
,
189 long *buffer_allocated
,
191 long *field2_offset
);
192 void mjpeg_insert_avi_markers(unsigned char **buffer
,
194 long *buffer_allocated
,
196 long *field2_offset
);
198 // Get the second field offset from the markers
199 long mjpeg_get_buz_field2(unsigned char *buffer
, long buffer_size
);
200 long mjpeg_get_lml33_field2(unsigned char *buffer
, long buffer_size
);
201 long mjpeg_get_quicktime_field2(unsigned char *buffer
, long buffer_size
);
202 // Field dominance is retrieved for the jpeg decoder. AVI stores field
203 // dominance in each field.
204 long mjpeg_get_avi_field2(unsigned char *buffer
,
206 int *field_dominance
);
207 long mjpeg_get_field2(unsigned char *buffer
, long buffer_size
);