configure: Improve pkg-config tests for FFmpeg libraries
[mplayer.git] / libvo / vo_jpeg.c
blobb6a5a8c3524169ad971a8eb428ca195ea97fc145
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() */
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 _("This error has occurred"), strerror(errno) );
122 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
123 _("Unable to access"), buf);
124 exit_player_bad(_("Fatal 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, _("already exists, but is not a directory."));
129 exit_player_bad(_("Fatal 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, _("Output directory already exists, but is not writable."));
134 exit_player_bad(_("Fatal error"));
137 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
138 buf, _("Output directory already exists and is writable."));
139 break;
141 default:
142 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
143 _("This error has occurred"), strerror(errno) );
144 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
145 buf, _("Unable to create output directory."));
146 exit_player_bad(_("Fatal error"));
147 } /* end switch */
148 } else if ( verbose ) {
149 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
150 buf, _("Output directory successfully created."));
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 _("Unable to create output file."));
192 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n",
193 info.short_name, _("This error has occurred"),
194 strerror(errno) );
195 exit_player_bad(_("Fatal 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(int *val)
335 if ( (*val >=0) && (*val<=100) )
336 return 1;
337 return 0;
340 static int preinit(const char *arg)
342 const opt_t subopts[] = {
343 {"progressive", OPT_ARG_BOOL, &jpeg_progressive_mode, NULL},
344 {"baseline", OPT_ARG_BOOL, &jpeg_baseline, NULL},
345 {"optimize", OPT_ARG_INT, &jpeg_optimize,
346 (opt_test_f)int_zero_hundred},
347 {"smooth", OPT_ARG_INT, &jpeg_smooth,
348 (opt_test_f)int_zero_hundred},
349 {"quality", OPT_ARG_INT, &jpeg_quality,
350 (opt_test_f)int_zero_hundred},
351 {"dpi", OPT_ARG_INT, &jpeg_dpi, NULL},
352 {"outdir", OPT_ARG_MSTRZ, &jpeg_outdir, NULL},
353 {"subdirs", OPT_ARG_MSTRZ, &jpeg_subdirs, NULL},
354 {"maxfiles", OPT_ARG_INT, &jpeg_maxfiles, (opt_test_f)int_pos},
355 {NULL, 0, NULL, NULL}
357 const char *info_message = NULL;
359 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
360 _("Parsing suboptions."));
362 jpeg_progressive_mode = 0;
363 jpeg_baseline = 1;
364 jpeg_optimize = 100;
365 jpeg_smooth = 0;
366 jpeg_quality = 75;
367 jpeg_maxfiles = 1000;
368 jpeg_outdir = strdup(".");
369 jpeg_subdirs = NULL;
371 if (subopt_parse(arg, subopts) != 0) {
372 return -1;
375 if (jpeg_progressive_mode) info_message = _("Progressive JPEG enabled.");
376 else info_message = _("Progressive JPEG disabled.");
377 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
379 if (jpeg_baseline) info_message = _("Baseline JPEG enabled.");
380 else info_message = _("Baseline JPEG disabled.");
381 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
383 mp_msg(MSGT_VO, MSGL_V, "%s: optimize --> %d\n", info.short_name,
384 jpeg_optimize);
385 mp_msg(MSGT_VO, MSGL_V, "%s: smooth --> %d\n", info.short_name,
386 jpeg_smooth);
387 mp_msg(MSGT_VO, MSGL_V, "%s: quality --> %d\n", info.short_name,
388 jpeg_quality);
389 mp_msg(MSGT_VO, MSGL_V, "%s: dpi --> %d\n", info.short_name,
390 jpeg_dpi);
391 mp_msg(MSGT_VO, MSGL_V, "%s: outdir --> %s\n", info.short_name,
392 jpeg_outdir);
393 if (jpeg_subdirs) {
394 mp_msg(MSGT_VO, MSGL_V, "%s: subdirs --> %s\n", info.short_name,
395 jpeg_subdirs);
396 mp_msg(MSGT_VO, MSGL_V, "%s: maxfiles --> %d\n", info.short_name,
397 jpeg_maxfiles);
400 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
401 _("Suboptions parsed OK."));
402 return 0;
405 /* ------------------------------------------------------------------------- */
407 static int control(uint32_t request, void *data)
409 switch (request) {
410 case VOCTRL_QUERY_FORMAT:
411 return query_format(*((uint32_t*)data));
413 return VO_NOTIMPL;
416 /* ------------------------------------------------------------------------- */
418 #undef BUFLENGTH
420 /* ------------------------------------------------------------------------- */