vo_corevideo: Avoid some code duplication.
[mplayer/glamo.git] / libvo / vo_jpeg.c
blobf27f2fd3fbb14531f64ee4aeeb1135cb376402ad
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 "mplayer.h" /* for exit_player_bad() */
49 /* ------------------------------------------------------------------------- */
51 /* Defines */
53 /* Used for temporary buffers to store file- and pathnames */
54 #define BUFLENGTH 512
56 /* ------------------------------------------------------------------------- */
58 /* Info */
60 static const vo_info_t info=
62 "JPEG file",
63 "jpeg",
64 "Zoltan Ponekker (pontscho@makacs.poliod.hu)",
68 const LIBVO_EXTERN (jpeg)
70 /* ------------------------------------------------------------------------- */
72 /* Global Variables */
74 static int image_width;
75 static int image_height;
76 static int image_d_width;
77 static int image_d_height;
79 int jpeg_baseline = 1;
80 int jpeg_progressive_mode = 0;
81 int jpeg_optimize = 100;
82 int jpeg_smooth = 0;
83 int jpeg_quality = 75;
84 int jpeg_dpi = 72; /** Screen resolution = 72 dpi */
85 char *jpeg_outdir = NULL;
86 char *jpeg_subdirs = NULL;
87 int jpeg_maxfiles = 1000;
89 static int framenum = 0;
91 /* ------------------------------------------------------------------------- */
93 /** \brief Create a directory.
95 * This function creates a directory. If it already exists, it tests if
96 * it's a directory and not something else, and if it is, it tests whether
97 * the directory is writable or not.
99 * \param buf Pointer to directory name.
100 * \param verbose Verbose on success. If verbose is non-zero, it will print
101 * a message if it was successful in creating the directory.
103 * \return nothing In case anything fails, the player will exit. If it
104 * returns, everything went well.
107 static void jpeg_mkdir(char *buf, int verbose) {
108 struct stat stat_p;
110 #ifndef __MINGW32__
111 if ( mkdir(buf, 0755) < 0 ) {
112 #else
113 if ( mkdir(buf) < 0 ) {
114 #endif
115 switch (errno) { /* use switch in case other errors need to be caught
116 and handled in the future */
117 case EEXIST:
118 if ( stat(buf, &stat_p ) < 0 ) {
119 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
120 _("This error has occurred"), strerror(errno) );
121 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
122 _("Unable to access"), buf);
123 exit_player_bad(_("Fatal error"));
125 if ( !S_ISDIR(stat_p.st_mode) ) {
126 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
127 buf, _("already exists, but is not a directory."));
128 exit_player_bad(_("Fatal error"));
130 if ( !(stat_p.st_mode & S_IWUSR) ) {
131 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
132 buf, _("Output directory already exists, but is not writable."));
133 exit_player_bad(_("Fatal error"));
136 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
137 buf, _("Output directory already exists and is writable."));
138 break;
140 default:
141 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
142 _("This error has occurred"), strerror(errno) );
143 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
144 buf, _("Unable to create output directory."));
145 exit_player_bad(_("Fatal error"));
146 } /* end switch */
147 } else if ( verbose ) {
148 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
149 buf, _("Output directory successfully created."));
150 } /* end if */
153 /* ------------------------------------------------------------------------- */
155 static int config(uint32_t width, uint32_t height, uint32_t d_width,
156 uint32_t d_height, uint32_t flags, char *title,
157 uint32_t format)
159 char buf[BUFLENGTH];
161 /* Create outdir. */
163 snprintf(buf, BUFLENGTH, "%s", jpeg_outdir);
165 jpeg_mkdir(buf, 1); /* This function only returns if creation was
166 successful. If not, the player will exit. */
168 image_height = height;
169 image_width = width;
170 /* Save for JFIF-Header PAR */
171 image_d_width = d_width;
172 image_d_height = d_height;
174 return 0;
177 /* ------------------------------------------------------------------------- */
179 static uint32_t jpeg_write(uint8_t * name, uint8_t * buffer)
181 FILE *outfile;
182 struct jpeg_compress_struct cinfo;
183 struct jpeg_error_mgr jerr;
184 JSAMPROW row_pointer[1];
185 int row_stride;
187 if ( !buffer ) return 1;
188 if ( (outfile = fopen(name, "wb") ) == NULL ) {
189 mp_msg(MSGT_VO, MSGL_ERR, "\n%s: %s\n", info.short_name,
190 _("Unable to create output file."));
191 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n",
192 info.short_name, _("This error has occurred"),
193 strerror(errno) );
194 exit_player_bad(_("Fatal error"));
197 cinfo.err = jpeg_std_error(&jerr);
198 jpeg_create_compress(&cinfo);
199 jpeg_stdio_dest(&cinfo, outfile);
201 cinfo.image_width = image_width;
202 cinfo.image_height = image_height;
203 cinfo.input_components = 3;
204 cinfo.in_color_space = JCS_RGB;
206 jpeg_set_defaults(&cinfo);
207 /* Important: Header info must be set AFTER jpeg_set_defaults() */
208 cinfo.write_JFIF_header = TRUE;
209 cinfo.JFIF_major_version = 1;
210 cinfo.JFIF_minor_version = 2;
211 cinfo.density_unit = 1; /* 0=unknown, 1=dpi, 2=dpcm */
212 /* Image DPI is determined by Y_density, so we leave that at
213 jpeg_dpi if possible and crunch X_density instead (PAR > 1) */
214 cinfo.X_density = jpeg_dpi*image_width/image_d_width;
215 cinfo.Y_density = jpeg_dpi*image_height/image_d_height;
216 cinfo.write_Adobe_marker = TRUE;
218 jpeg_set_quality(&cinfo,jpeg_quality, jpeg_baseline);
219 cinfo.optimize_coding = jpeg_optimize;
220 cinfo.smoothing_factor = jpeg_smooth;
222 if ( jpeg_progressive_mode ) {
223 jpeg_simple_progression(&cinfo);
226 jpeg_start_compress(&cinfo, TRUE);
228 row_stride = image_width * 3;
229 while (cinfo.next_scanline < cinfo.image_height) {
230 row_pointer[0] = &buffer[cinfo.next_scanline * row_stride];
231 (void)jpeg_write_scanlines(&cinfo, row_pointer,1);
234 jpeg_finish_compress(&cinfo);
235 fclose(outfile);
236 jpeg_destroy_compress(&cinfo);
238 return 0;
241 /* ------------------------------------------------------------------------- */
243 static int draw_frame(uint8_t *src[])
245 static int framecounter = 0, subdircounter = 0;
246 char buf[BUFLENGTH];
247 static char subdirname[BUFLENGTH] = "";
249 /* Start writing to new subdirectory after a certain amount of frames */
250 if ( framecounter == jpeg_maxfiles ) {
251 framecounter = 0;
254 /* If framecounter is zero (or reset to zero), increment subdirectory
255 * number and create the subdirectory.
256 * If jpeg_subdirs is not set, do nothing and resort to old behaviour. */
257 if ( !framecounter && jpeg_subdirs ) {
258 subdircounter++;
259 snprintf(subdirname, BUFLENGTH, "%s%08d", jpeg_subdirs, subdircounter);
260 snprintf(buf, BUFLENGTH, "%s/%s", jpeg_outdir, subdirname);
261 jpeg_mkdir(buf, 0); /* This function only returns if creation was
262 successful. If not, the player will exit. */
265 framenum++;
267 /* snprintf the full pathname of the outputfile */
268 snprintf(buf, BUFLENGTH, "%s/%s/%08d.jpg", jpeg_outdir, subdirname,
269 framenum);
271 framecounter++;
273 return jpeg_write(buf, src[0]);
276 /* ------------------------------------------------------------------------- */
278 static void draw_osd(void)
282 /* ------------------------------------------------------------------------- */
284 static void flip_page (void)
288 /* ------------------------------------------------------------------------- */
290 static int draw_slice(uint8_t *src[], int stride[], int w, int h,
291 int x, int y)
293 return 0;
296 /* ------------------------------------------------------------------------- */
298 static int query_format(uint32_t format)
300 if (format == IMGFMT_RGB24) {
301 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
304 return 0;
307 /* ------------------------------------------------------------------------- */
309 static void uninit(void)
311 if (jpeg_subdirs) {
312 free(jpeg_subdirs);
313 jpeg_subdirs = NULL;
315 if (jpeg_outdir) {
316 free(jpeg_outdir);
317 jpeg_outdir = NULL;
321 /* ------------------------------------------------------------------------- */
323 static void check_events(void)
327 /* ------------------------------------------------------------------------- */
329 /** \brief Validation function for values [0-100]
332 static int int_zero_hundred(void *valp)
334 int *val = valp;
335 return *val >= 0 && *val <= 100;
338 static int preinit(const char *arg)
340 const opt_t subopts[] = {
341 {"progressive", OPT_ARG_BOOL, &jpeg_progressive_mode, NULL},
342 {"baseline", OPT_ARG_BOOL, &jpeg_baseline, NULL},
343 {"optimize", OPT_ARG_INT, &jpeg_optimize,
344 int_zero_hundred},
345 {"smooth", OPT_ARG_INT, &jpeg_smooth,
346 int_zero_hundred},
347 {"quality", OPT_ARG_INT, &jpeg_quality,
348 int_zero_hundred},
349 {"dpi", OPT_ARG_INT, &jpeg_dpi, NULL},
350 {"outdir", OPT_ARG_MSTRZ, &jpeg_outdir, NULL},
351 {"subdirs", OPT_ARG_MSTRZ, &jpeg_subdirs, NULL},
352 {"maxfiles", OPT_ARG_INT, &jpeg_maxfiles, int_pos},
353 {NULL, 0, NULL, NULL}
355 const char *info_message = NULL;
357 mp_msg(MSGT_VO, MSGL_V, "%s: %s\n", info.short_name,
358 "Parsing suboptions.");
360 jpeg_progressive_mode = 0;
361 jpeg_baseline = 1;
362 jpeg_optimize = 100;
363 jpeg_smooth = 0;
364 jpeg_quality = 75;
365 jpeg_maxfiles = 1000;
366 jpeg_outdir = strdup(".");
367 jpeg_subdirs = NULL;
369 if (subopt_parse(arg, subopts) != 0) {
370 return -1;
373 if (jpeg_progressive_mode) info_message = _("Progressive JPEG enabled.");
374 else info_message = _("Progressive JPEG disabled.");
375 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
377 if (jpeg_baseline) info_message = _("Baseline JPEG enabled.");
378 else info_message = _("Baseline JPEG disabled.");
379 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
381 mp_msg(MSGT_VO, MSGL_V, "%s: optimize --> %d\n", info.short_name,
382 jpeg_optimize);
383 mp_msg(MSGT_VO, MSGL_V, "%s: smooth --> %d\n", info.short_name,
384 jpeg_smooth);
385 mp_msg(MSGT_VO, MSGL_V, "%s: quality --> %d\n", info.short_name,
386 jpeg_quality);
387 mp_msg(MSGT_VO, MSGL_V, "%s: dpi --> %d\n", info.short_name,
388 jpeg_dpi);
389 mp_msg(MSGT_VO, MSGL_V, "%s: outdir --> %s\n", info.short_name,
390 jpeg_outdir);
391 if (jpeg_subdirs) {
392 mp_msg(MSGT_VO, MSGL_V, "%s: subdirs --> %s\n", info.short_name,
393 jpeg_subdirs);
394 mp_msg(MSGT_VO, MSGL_V, "%s: maxfiles --> %d\n", info.short_name,
395 jpeg_maxfiles);
398 mp_msg(MSGT_VO, MSGL_V, "%s: %s\n", info.short_name,
399 "Suboptions parsed OK.");
400 return 0;
403 /* ------------------------------------------------------------------------- */
405 static int control(uint32_t request, void *data)
407 switch (request) {
408 case VOCTRL_QUERY_FORMAT:
409 return query_format(*((uint32_t*)data));
411 return VO_NOTIMPL;
414 /* ------------------------------------------------------------------------- */
416 #undef BUFLENGTH
418 /* ------------------------------------------------------------------------- */