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 /* ------------------------------------------------------------------------- */
35 #include <sys/types.h>
38 /* ------------------------------------------------------------------------- */
43 #include "subopt-helper.h"
45 #include "video_out.h"
46 #include "video_out_internal.h"
47 #include "mplayer.h" /* for exit_player_bad() */
49 /* ------------------------------------------------------------------------- */
53 /* Used for temporary buffers to store file- and pathnames */
56 /* ------------------------------------------------------------------------- */
60 static const vo_info_t info
=
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;
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
) {
111 if ( mkdir(buf
, 0755) < 0 ) {
113 if ( mkdir(buf
) < 0 ) {
115 switch (errno
) { /* use switch in case other errors need to be caught
116 and handled in the future */
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."));
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"));
147 } else if ( verbose
) {
148 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
149 buf
, _("Output directory successfully created."));
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
,
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
;
170 /* Save for JFIF-Header PAR */
171 image_d_width
= d_width
;
172 image_d_height
= d_height
;
177 /* ------------------------------------------------------------------------- */
179 static uint32_t jpeg_write(uint8_t * name
, uint8_t * buffer
)
182 struct jpeg_compress_struct cinfo
;
183 struct jpeg_error_mgr jerr
;
184 JSAMPROW row_pointer
[1];
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"),
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
);
236 jpeg_destroy_compress(&cinfo
);
241 /* ------------------------------------------------------------------------- */
243 static int draw_frame(uint8_t *src
[])
245 static int framecounter
= 0, subdircounter
= 0;
247 static char subdirname
[BUFLENGTH
] = "";
249 /* Start writing to new subdirectory after a certain amount of frames */
250 if ( framecounter
== jpeg_maxfiles
) {
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
) {
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. */
267 /* snprintf the full pathname of the outputfile */
268 snprintf(buf
, BUFLENGTH
, "%s/%s/%08d.jpg", jpeg_outdir
, subdirname
,
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
,
296 /* ------------------------------------------------------------------------- */
298 static int query_format(uint32_t format
)
300 if (format
== IMGFMT_RGB24
) {
301 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
307 /* ------------------------------------------------------------------------- */
309 static void uninit(void)
321 /* ------------------------------------------------------------------------- */
323 static void check_events(void)
327 /* ------------------------------------------------------------------------- */
329 /** \brief Validation function for values [0-100]
332 static int int_zero_hundred(void *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
,
345 {"smooth", OPT_ARG_INT
, &jpeg_smooth
,
347 {"quality", OPT_ARG_INT
, &jpeg_quality
,
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;
365 jpeg_maxfiles
= 1000;
366 jpeg_outdir
= strdup(".");
369 if (subopt_parse(arg
, subopts
) != 0) {
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
,
383 mp_msg(MSGT_VO
, MSGL_V
, "%s: smooth --> %d\n", info
.short_name
,
385 mp_msg(MSGT_VO
, MSGL_V
, "%s: quality --> %d\n", info
.short_name
,
387 mp_msg(MSGT_VO
, MSGL_V
, "%s: dpi --> %d\n", info
.short_name
,
389 mp_msg(MSGT_VO
, MSGL_V
, "%s: outdir --> %s\n", info
.short_name
,
392 mp_msg(MSGT_VO
, MSGL_V
, "%s: subdirs --> %s\n", info
.short_name
,
394 mp_msg(MSGT_VO
, MSGL_V
, "%s: maxfiles --> %d\n", info
.short_name
,
398 mp_msg(MSGT_VO
, MSGL_V
, "%s: %s\n", info
.short_name
,
399 "Suboptions parsed OK.");
403 /* ------------------------------------------------------------------------- */
405 static int control(uint32_t request
, void *data
)
408 case VOCTRL_QUERY_FORMAT
:
409 return query_format(*((uint32_t*)data
));
414 /* ------------------------------------------------------------------------- */
418 /* ------------------------------------------------------------------------- */