Remove unused static functions asx_list_add() and asx_list_remove().
[mplayer/glamo.git] / libvo / vo_jpeg.c
blob97c7994f6d00a24898d0c5928578adc26d4418d7
1 /*
2 * JPEG Renderer for MPlayer
4 * Copyright (C) 2002 by Pontscho <pontscho@makacs.poliod.hu>
5 * Copyright (C) 2003 by Alex
6 * Copyright (C) 2004, 2005 by Ivo van Poorten <ivop@euronet.nl>
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer 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.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 /* ------------------------------------------------------------------------- */
27 /* Global Includes */
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <jpeglib.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <unistd.h>
38 /* ------------------------------------------------------------------------- */
40 /* Local Includes */
42 #include "config.h"
43 #include "subopt-helper.h"
44 #include "mp_msg.h"
45 #include "video_out.h"
46 #include "video_out_internal.h"
47 #include "mp_core.h"
48 #include "help_mp.h"
50 /* ------------------------------------------------------------------------- */
52 /* Defines */
54 /* Used for temporary buffers to store file- and pathnames */
55 #define BUFLENGTH 512
57 /* ------------------------------------------------------------------------- */
59 /* Info */
61 static const vo_info_t info=
63 "JPEG file",
64 "jpeg",
65 "Zoltan Ponekker (pontscho@makacs.poliod.hu)",
69 const LIBVO_EXTERN (jpeg)
71 /* ------------------------------------------------------------------------- */
73 /* Global Variables */
75 static int image_width;
76 static int image_height;
77 static int image_d_width;
78 static int image_d_height;
80 int jpeg_baseline = 1;
81 int jpeg_progressive_mode = 0;
82 int jpeg_optimize = 100;
83 int jpeg_smooth = 0;
84 int jpeg_quality = 75;
85 int jpeg_dpi = 72; /** Screen resolution = 72 dpi */
86 char *jpeg_outdir = NULL;
87 char *jpeg_subdirs = NULL;
88 int jpeg_maxfiles = 1000;
90 static int framenum = 0;
92 /* ------------------------------------------------------------------------- */
94 /** \brief Create a directory.
96 * This function creates a directory. If it already exists, it tests if
97 * it's a directory and not something else, and if it is, it tests whether
98 * the directory is writable or not.
100 * \param buf Pointer to directory name.
101 * \param verbose Verbose on success. If verbose is non-zero, it will print
102 * a message if it was successful in creating the directory.
104 * \return nothing In case anything fails, the player will exit. If it
105 * returns, everything went well.
108 static void jpeg_mkdir(char *buf, int verbose) {
109 struct stat stat_p;
111 #ifndef __MINGW32__
112 if ( mkdir(buf, 0755) < 0 ) {
113 #else
114 if ( mkdir(buf) < 0 ) {
115 #endif
116 switch (errno) { /* use switch in case other errors need to be caught
117 and handled in the future */
118 case EEXIST:
119 if ( stat(buf, &stat_p ) < 0 ) {
120 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
121 MSGTR_VO_GenericError, strerror(errno) );
122 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
123 MSGTR_VO_UnableToAccess,buf);
124 exit_player(EXIT_ERROR);
126 if ( !S_ISDIR(stat_p.st_mode) ) {
127 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
128 buf, MSGTR_VO_ExistsButNoDirectory);
129 exit_player(EXIT_ERROR);
131 if ( !(stat_p.st_mode & S_IWUSR) ) {
132 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
133 buf, MSGTR_VO_DirExistsButNotWritable);
134 exit_player(EXIT_ERROR);
137 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
138 buf, MSGTR_VO_DirExistsAndIsWritable);
139 break;
141 default:
142 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
143 MSGTR_VO_GenericError, strerror(errno) );
144 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
145 buf, MSGTR_VO_CantCreateDirectory);
146 exit_player(EXIT_ERROR);
147 } /* end switch */
148 } else if ( verbose ) {
149 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
150 buf, MSGTR_VO_DirectoryCreateSuccess);
151 } /* end if */
154 /* ------------------------------------------------------------------------- */
156 static int config(uint32_t width, uint32_t height, uint32_t d_width,
157 uint32_t d_height, uint32_t flags, char *title,
158 uint32_t format)
160 char buf[BUFLENGTH];
162 /* Create outdir. */
164 snprintf(buf, BUFLENGTH, "%s", jpeg_outdir);
166 jpeg_mkdir(buf, 1); /* This function only returns if creation was
167 successful. If not, the player will exit. */
169 image_height = height;
170 image_width = width;
171 /* Save for JFIF-Header PAR */
172 image_d_width = d_width;
173 image_d_height = d_height;
175 return 0;
178 /* ------------------------------------------------------------------------- */
180 static uint32_t jpeg_write(uint8_t * name, uint8_t * buffer)
182 FILE *outfile;
183 struct jpeg_compress_struct cinfo;
184 struct jpeg_error_mgr jerr;
185 JSAMPROW row_pointer[1];
186 int row_stride;
188 if ( !buffer ) return 1;
189 if ( (outfile = fopen(name, "wb") ) == NULL ) {
190 mp_msg(MSGT_VO, MSGL_ERR, "\n%s: %s\n", info.short_name,
191 MSGTR_VO_CantCreateFile);
192 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n",
193 info.short_name, MSGTR_VO_GenericError,
194 strerror(errno) );
195 exit_player(EXIT_ERROR);
198 cinfo.err = jpeg_std_error(&jerr);
199 jpeg_create_compress(&cinfo);
200 jpeg_stdio_dest(&cinfo, outfile);
202 cinfo.image_width = image_width;
203 cinfo.image_height = image_height;
204 cinfo.input_components = 3;
205 cinfo.in_color_space = JCS_RGB;
207 jpeg_set_defaults(&cinfo);
208 /* Important: Header info must be set AFTER jpeg_set_defaults() */
209 cinfo.write_JFIF_header = TRUE;
210 cinfo.JFIF_major_version = 1;
211 cinfo.JFIF_minor_version = 2;
212 cinfo.density_unit = 1; /* 0=unknown, 1=dpi, 2=dpcm */
213 /* Image DPI is determined by Y_density, so we leave that at
214 jpeg_dpi if possible and crunch X_density instead (PAR > 1) */
215 cinfo.X_density = jpeg_dpi*image_width/image_d_width;
216 cinfo.Y_density = jpeg_dpi*image_height/image_d_height;
217 cinfo.write_Adobe_marker = TRUE;
219 jpeg_set_quality(&cinfo,jpeg_quality, jpeg_baseline);
220 cinfo.optimize_coding = jpeg_optimize;
221 cinfo.smoothing_factor = jpeg_smooth;
223 if ( jpeg_progressive_mode ) {
224 jpeg_simple_progression(&cinfo);
227 jpeg_start_compress(&cinfo, TRUE);
229 row_stride = image_width * 3;
230 while (cinfo.next_scanline < cinfo.image_height) {
231 row_pointer[0] = &buffer[cinfo.next_scanline * row_stride];
232 (void)jpeg_write_scanlines(&cinfo, row_pointer,1);
235 jpeg_finish_compress(&cinfo);
236 fclose(outfile);
237 jpeg_destroy_compress(&cinfo);
239 return 0;
242 /* ------------------------------------------------------------------------- */
244 static int draw_frame(uint8_t *src[])
246 static int framecounter = 0, subdircounter = 0;
247 char buf[BUFLENGTH];
248 static char subdirname[BUFLENGTH] = "";
250 /* Start writing to new subdirectory after a certain amount of frames */
251 if ( framecounter == jpeg_maxfiles ) {
252 framecounter = 0;
255 /* If framecounter is zero (or reset to zero), increment subdirectory
256 * number and create the subdirectory.
257 * If jpeg_subdirs is not set, do nothing and resort to old behaviour. */
258 if ( !framecounter && jpeg_subdirs ) {
259 subdircounter++;
260 snprintf(subdirname, BUFLENGTH, "%s%08d", jpeg_subdirs, subdircounter);
261 snprintf(buf, BUFLENGTH, "%s/%s", jpeg_outdir, subdirname);
262 jpeg_mkdir(buf, 0); /* This function only returns if creation was
263 successful. If not, the player will exit. */
266 framenum++;
268 /* snprintf the full pathname of the outputfile */
269 snprintf(buf, BUFLENGTH, "%s/%s/%08d.jpg", jpeg_outdir, subdirname,
270 framenum);
272 framecounter++;
274 return jpeg_write(buf, src[0]);
277 /* ------------------------------------------------------------------------- */
279 static void draw_osd(void)
283 /* ------------------------------------------------------------------------- */
285 static void flip_page (void)
289 /* ------------------------------------------------------------------------- */
291 static int draw_slice(uint8_t *src[], int stride[], int w, int h,
292 int x, int y)
294 return 0;
297 /* ------------------------------------------------------------------------- */
299 static int query_format(uint32_t format)
301 if (format == IMGFMT_RGB24) {
302 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
305 return 0;
308 /* ------------------------------------------------------------------------- */
310 static void uninit(void)
312 if (jpeg_subdirs) {
313 free(jpeg_subdirs);
314 jpeg_subdirs = NULL;
316 if (jpeg_outdir) {
317 free(jpeg_outdir);
318 jpeg_outdir = NULL;
322 /* ------------------------------------------------------------------------- */
324 static void check_events(void)
328 /* ------------------------------------------------------------------------- */
330 /** \brief Validation function for values [0-100]
333 static int int_zero_hundred(void *valp)
335 int *val = valp;
336 return *val >= 0 && *val <= 100;
339 static int preinit(const char *arg)
341 const opt_t subopts[] = {
342 {"progressive", OPT_ARG_BOOL, &jpeg_progressive_mode, NULL},
343 {"baseline", OPT_ARG_BOOL, &jpeg_baseline, NULL},
344 {"optimize", OPT_ARG_INT, &jpeg_optimize,
345 int_zero_hundred},
346 {"smooth", OPT_ARG_INT, &jpeg_smooth,
347 int_zero_hundred},
348 {"quality", OPT_ARG_INT, &jpeg_quality,
349 int_zero_hundred},
350 {"dpi", OPT_ARG_INT, &jpeg_dpi, NULL},
351 {"outdir", OPT_ARG_MSTRZ, &jpeg_outdir, NULL},
352 {"subdirs", OPT_ARG_MSTRZ, &jpeg_subdirs, NULL},
353 {"maxfiles", OPT_ARG_INT, &jpeg_maxfiles, int_pos},
354 {NULL, 0, NULL, NULL}
356 const char *info_message = NULL;
358 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
359 MSGTR_VO_ParsingSuboptions);
361 jpeg_progressive_mode = 0;
362 jpeg_baseline = 1;
363 jpeg_optimize = 100;
364 jpeg_smooth = 0;
365 jpeg_quality = 75;
366 jpeg_maxfiles = 1000;
367 jpeg_outdir = strdup(".");
368 jpeg_subdirs = NULL;
370 if (subopt_parse(arg, subopts) != 0) {
371 return -1;
374 if (jpeg_progressive_mode) info_message = MSGTR_VO_JPEG_ProgressiveJPEG;
375 else info_message = MSGTR_VO_JPEG_NoProgressiveJPEG;
376 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
378 if (jpeg_baseline) info_message = MSGTR_VO_JPEG_BaselineJPEG;
379 else info_message = MSGTR_VO_JPEG_NoBaselineJPEG;
380 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
382 mp_msg(MSGT_VO, MSGL_V, "%s: optimize --> %d\n", info.short_name,
383 jpeg_optimize);
384 mp_msg(MSGT_VO, MSGL_V, "%s: smooth --> %d\n", info.short_name,
385 jpeg_smooth);
386 mp_msg(MSGT_VO, MSGL_V, "%s: quality --> %d\n", info.short_name,
387 jpeg_quality);
388 mp_msg(MSGT_VO, MSGL_V, "%s: dpi --> %d\n", info.short_name,
389 jpeg_dpi);
390 mp_msg(MSGT_VO, MSGL_V, "%s: outdir --> %s\n", info.short_name,
391 jpeg_outdir);
392 if (jpeg_subdirs) {
393 mp_msg(MSGT_VO, MSGL_V, "%s: subdirs --> %s\n", info.short_name,
394 jpeg_subdirs);
395 mp_msg(MSGT_VO, MSGL_V, "%s: maxfiles --> %d\n", info.short_name,
396 jpeg_maxfiles);
399 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
400 MSGTR_VO_SuboptionsParsedOK);
401 return 0;
404 /* ------------------------------------------------------------------------- */
406 static int control(uint32_t request, void *data, ...)
408 switch (request) {
409 case VOCTRL_QUERY_FORMAT:
410 return query_format(*((uint32_t*)data));
412 return VO_NOTIMPL;
415 /* ------------------------------------------------------------------------- */
417 #undef BUFLENGTH
419 /* ------------------------------------------------------------------------- */