Fix segfault if an 'strf' chunk couldn't be found in avi
[mplayer/glamo.git] / libvo / vo_jpeg.c
blob82eab7163534088f610df995c8ae14a344601b25
1 /* ------------------------------------------------------------------------- */
3 /*
4 * vo_jpeg.c, JPEG Renderer for MPlayer
6 *
7 * Changelog
8 *
9 * Original version: Copyright 2002 by Pontscho (pontscho@makacs.poliod.hu)
10 * 2003-04-25 Spring cleanup -- Alex
11 * 2004-08-04 Added multiple subdirectory support -- Ivo (ivop@euronet.nl)
12 * 2004-09-01 Cosmetics update -- Ivo
13 * 2004-09-05 Added suboptions parser -- Ivo
14 * 2005-01-16 Replaced suboption parser by call to subopt-helper --Ivo
18 /* ------------------------------------------------------------------------- */
20 /* Global Includes */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <jpeglib.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
31 /* ------------------------------------------------------------------------- */
33 /* Local Includes */
35 #include "config.h"
36 #include "subopt-helper.h"
37 #include "mp_msg.h"
38 #include "video_out.h"
39 #include "video_out_internal.h"
40 #include "mplayer.h" /* for exit_player() */
41 #include "help_mp.h"
43 /* ------------------------------------------------------------------------- */
45 /* Defines */
47 /* Used for temporary buffers to store file- and pathnames */
48 #define BUFLENGTH 512
50 /* ------------------------------------------------------------------------- */
52 /* Info */
54 static vo_info_t info=
56 "JPEG file",
57 "jpeg",
58 "Zoltan Ponekker (pontscho@makacs.poliod.hu)",
62 LIBVO_EXTERN (jpeg)
64 /* ------------------------------------------------------------------------- */
66 /* Global Variables */
68 static int image_width;
69 static int image_height;
70 static int image_d_width;
71 static int image_d_height;
73 int jpeg_baseline = 1;
74 int jpeg_progressive_mode = 0;
75 int jpeg_optimize = 100;
76 int jpeg_smooth = 0;
77 int jpeg_quality = 75;
78 int jpeg_dpi = 72; /** Screen resolution = 72 dpi */
79 char *jpeg_outdir = NULL;
80 char *jpeg_subdirs = NULL;
81 int jpeg_maxfiles = 1000;
83 static int framenum = 0;
85 /* ------------------------------------------------------------------------- */
87 /** \brief Create a directory.
89 * This function creates a directory. If it already exists, it tests if
90 * it's a directory and not something else, and if it is, it tests whether
91 * the directory is writable or not.
93 * \param buf Pointer to directory name.
94 * \param verbose Verbose on success. If verbose is non-zero, it will print
95 * a message if it was successful in creating the directory.
97 * \return nothing In case anything fails, the player will exit. If it
98 * returns, everything went well.
101 static void jpeg_mkdir(char *buf, int verbose) {
102 struct stat stat_p;
104 #ifndef __MINGW32__
105 if ( mkdir(buf, 0755) < 0 ) {
106 #else
107 if ( mkdir(buf) < 0 ) {
108 #endif
109 switch (errno) { /* use switch in case other errors need to be caught
110 and handled in the future */
111 case EEXIST:
112 if ( stat(buf, &stat_p ) < 0 ) {
113 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
114 MSGTR_VO_GenericError, strerror(errno) );
115 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
116 MSGTR_VO_UnableToAccess,buf);
117 exit_player(MSGTR_Exit_error);
119 if ( !S_ISDIR(stat_p.st_mode) ) {
120 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
121 buf, MSGTR_VO_ExistsButNoDirectory);
122 exit_player(MSGTR_Exit_error);
124 if ( !(stat_p.st_mode & S_IWUSR) ) {
125 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
126 buf, MSGTR_VO_DirExistsButNotWritable);
127 exit_player(MSGTR_Exit_error);
130 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
131 buf, MSGTR_VO_DirExistsAndIsWritable);
132 break;
134 default:
135 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
136 MSGTR_VO_GenericError, strerror(errno) );
137 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
138 buf, MSGTR_VO_CantCreateDirectory);
139 exit_player(MSGTR_Exit_error);
140 } /* end switch */
141 } else if ( verbose ) {
142 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
143 buf, MSGTR_VO_DirectoryCreateSuccess);
144 } /* end if */
147 /* ------------------------------------------------------------------------- */
149 static int config(uint32_t width, uint32_t height, uint32_t d_width,
150 uint32_t d_height, uint32_t flags, char *title,
151 uint32_t format)
153 char buf[BUFLENGTH];
155 /* Create outdir. */
157 snprintf(buf, BUFLENGTH, "%s", jpeg_outdir);
159 jpeg_mkdir(buf, 1); /* This function only returns if creation was
160 successful. If not, the player will exit. */
162 image_height = height;
163 image_width = width;
164 /* Save for JFIF-Header PAR */
165 image_d_width = d_width;
166 image_d_height = d_height;
168 return 0;
171 /* ------------------------------------------------------------------------- */
173 static uint32_t jpeg_write(uint8_t * name, uint8_t * buffer)
175 FILE *outfile;
176 struct jpeg_compress_struct cinfo;
177 struct jpeg_error_mgr jerr;
178 JSAMPROW row_pointer[1];
179 int row_stride;
181 if ( !buffer ) return 1;
182 if ( (outfile = fopen(name, "wb") ) == NULL ) {
183 mp_msg(MSGT_VO, MSGL_ERR, "\n%s: %s\n", info.short_name,
184 MSGTR_VO_CantCreateFile);
185 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n",
186 info.short_name, MSGTR_VO_GenericError,
187 strerror(errno) );
188 exit_player(MSGTR_Exit_error);
191 cinfo.err = jpeg_std_error(&jerr);
192 jpeg_create_compress(&cinfo);
193 jpeg_stdio_dest(&cinfo, outfile);
195 cinfo.image_width = image_width;
196 cinfo.image_height = image_height;
197 cinfo.input_components = 3;
198 cinfo.in_color_space = JCS_RGB;
200 jpeg_set_defaults(&cinfo);
201 /* Important: Header info must be set AFTER jpeg_set_defaults() */
202 cinfo.write_JFIF_header = TRUE;
203 cinfo.JFIF_major_version = 1;
204 cinfo.JFIF_minor_version = 2;
205 cinfo.density_unit = 1; /* 0=unknown, 1=dpi, 2=dpcm */
206 /* Image DPI is determined by Y_density, so we leave that at
207 jpeg_dpi if possible and crunch X_density instead (PAR > 1) */
208 cinfo.X_density = jpeg_dpi*image_width/image_d_width;
209 cinfo.Y_density = jpeg_dpi*image_height/image_d_height;
210 cinfo.write_Adobe_marker = TRUE;
212 jpeg_set_quality(&cinfo,jpeg_quality, jpeg_baseline);
213 cinfo.optimize_coding = jpeg_optimize;
214 cinfo.smoothing_factor = jpeg_smooth;
216 if ( jpeg_progressive_mode ) {
217 jpeg_simple_progression(&cinfo);
220 jpeg_start_compress(&cinfo, TRUE);
222 row_stride = image_width * 3;
223 while (cinfo.next_scanline < cinfo.image_height) {
224 row_pointer[0] = &buffer[cinfo.next_scanline * row_stride];
225 (void)jpeg_write_scanlines(&cinfo, row_pointer,1);
228 jpeg_finish_compress(&cinfo);
229 fclose(outfile);
230 jpeg_destroy_compress(&cinfo);
232 return 0;
235 /* ------------------------------------------------------------------------- */
237 static int draw_frame(uint8_t *src[])
239 static int framecounter = 0, subdircounter = 0;
240 char buf[BUFLENGTH];
241 static char subdirname[BUFLENGTH] = "";
243 /* Start writing to new subdirectory after a certain amount of frames */
244 if ( framecounter == jpeg_maxfiles ) {
245 framecounter = 0;
248 /* If framecounter is zero (or reset to zero), increment subdirectory
249 * number and create the subdirectory.
250 * If jpeg_subdirs is not set, do nothing and resort to old behaviour. */
251 if ( !framecounter && jpeg_subdirs ) {
252 subdircounter++;
253 snprintf(subdirname, BUFLENGTH, "%s%08d", jpeg_subdirs, subdircounter);
254 snprintf(buf, BUFLENGTH, "%s/%s", jpeg_outdir, subdirname);
255 jpeg_mkdir(buf, 0); /* This function only returns if creation was
256 successful. If not, the player will exit. */
259 framenum++;
261 /* snprintf the full pathname of the outputfile */
262 snprintf(buf, BUFLENGTH, "%s/%s/%08d.jpg", jpeg_outdir, subdirname,
263 framenum);
265 framecounter++;
267 return jpeg_write(buf, src[0]);
270 /* ------------------------------------------------------------------------- */
272 static void draw_osd(void)
276 /* ------------------------------------------------------------------------- */
278 static void flip_page (void)
282 /* ------------------------------------------------------------------------- */
284 static int draw_slice(uint8_t *src[], int stride[], int w, int h,
285 int x, int y)
287 return 0;
290 /* ------------------------------------------------------------------------- */
292 static int query_format(uint32_t format)
294 if (format == IMGFMT_RGB24) {
295 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
298 return 0;
301 /* ------------------------------------------------------------------------- */
303 static void uninit(void)
305 if (jpeg_subdirs) {
306 free(jpeg_subdirs);
307 jpeg_subdirs = NULL;
309 if (jpeg_outdir) {
310 free(jpeg_outdir);
311 jpeg_outdir = NULL;
315 /* ------------------------------------------------------------------------- */
317 static void check_events(void)
321 /* ------------------------------------------------------------------------- */
323 /** \brief Validation function for values [0-100]
326 static int int_zero_hundred(int *val)
328 if ( (*val >=0) && (*val<=100) )
329 return 1;
330 return 0;
333 static int preinit(const char *arg)
335 opt_t subopts[] = {
336 {"progressive", OPT_ARG_BOOL, &jpeg_progressive_mode, NULL, 0},
337 {"baseline", OPT_ARG_BOOL, &jpeg_baseline, NULL, 0},
338 {"optimize", OPT_ARG_INT, &jpeg_optimize,
339 (opt_test_f)int_zero_hundred, 0},
340 {"smooth", OPT_ARG_INT, &jpeg_smooth,
341 (opt_test_f)int_zero_hundred, 0},
342 {"quality", OPT_ARG_INT, &jpeg_quality,
343 (opt_test_f)int_zero_hundred, 0},
344 {"dpi", OPT_ARG_INT, &jpeg_dpi, NULL, 0},
345 {"outdir", OPT_ARG_MSTRZ, &jpeg_outdir, NULL, 0},
346 {"subdirs", OPT_ARG_MSTRZ, &jpeg_subdirs, NULL, 0},
347 {"maxfiles", OPT_ARG_INT, &jpeg_maxfiles, (opt_test_f)int_pos, 0},
348 {NULL, 0, NULL, NULL, 0}
350 const char *info_message = NULL;
352 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
353 MSGTR_VO_ParsingSuboptions);
355 jpeg_progressive_mode = 0;
356 jpeg_baseline = 1;
357 jpeg_optimize = 100;
358 jpeg_smooth = 0;
359 jpeg_quality = 75;
360 jpeg_maxfiles = 1000;
361 jpeg_outdir = strdup(".");
362 jpeg_subdirs = NULL;
364 if (subopt_parse(arg, subopts) != 0) {
365 return -1;
368 if (jpeg_progressive_mode) info_message = MSGTR_VO_JPEG_ProgressiveJPEG;
369 else info_message = MSGTR_VO_JPEG_NoProgressiveJPEG;
370 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
372 if (jpeg_baseline) info_message = MSGTR_VO_JPEG_BaselineJPEG;
373 else info_message = MSGTR_VO_JPEG_NoBaselineJPEG;
374 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
376 mp_msg(MSGT_VO, MSGL_V, "%s: optimize --> %d\n", info.short_name,
377 jpeg_optimize);
378 mp_msg(MSGT_VO, MSGL_V, "%s: smooth --> %d\n", info.short_name,
379 jpeg_smooth);
380 mp_msg(MSGT_VO, MSGL_V, "%s: quality --> %d\n", info.short_name,
381 jpeg_quality);
382 mp_msg(MSGT_VO, MSGL_V, "%s: dpi --> %d\n", info.short_name,
383 jpeg_dpi);
384 mp_msg(MSGT_VO, MSGL_V, "%s: outdir --> %s\n", info.short_name,
385 jpeg_outdir);
386 if (jpeg_subdirs) {
387 mp_msg(MSGT_VO, MSGL_V, "%s: subdirs --> %s\n", info.short_name,
388 jpeg_subdirs);
389 mp_msg(MSGT_VO, MSGL_V, "%s: maxfiles --> %d\n", info.short_name,
390 jpeg_maxfiles);
393 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
394 MSGTR_VO_SuboptionsParsedOK);
395 return 0;
398 /* ------------------------------------------------------------------------- */
400 static int control(uint32_t request, void *data, ...)
402 switch (request) {
403 case VOCTRL_QUERY_FORMAT:
404 return query_format(*((uint32_t*)data));
406 return VO_NOTIMPL;
409 /* ------------------------------------------------------------------------- */
411 #undef BUFLENGTH
413 /* ------------------------------------------------------------------------- */