1 /* ------------------------------------------------------------------------- */
4 * vo_jpeg.c, JPEG Renderer for MPlayer
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 /* ------------------------------------------------------------------------- */
28 #include <sys/types.h>
31 /* ------------------------------------------------------------------------- */
36 #include "subopt-helper.h"
38 #include "video_out.h"
39 #include "video_out_internal.h"
40 #include "mplayer.h" /* for exit_player() */
43 /* ------------------------------------------------------------------------- */
47 /* Used for temporary buffers to store file- and pathnames */
50 /* ------------------------------------------------------------------------- */
54 static vo_info_t info
=
58 "Zoltan Ponekker (pontscho@makacs.poliod.hu)",
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;
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 void jpeg_mkdir(char *buf
, int verbose
) {
105 if ( mkdir(buf
, 0755) < 0 ) {
107 if ( mkdir(buf
) < 0 ) {
109 switch (errno
) { /* use switch in case other errors need to be caught
110 and handled in the future */
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
);
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
);
141 } else if ( verbose
) {
142 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
143 buf
, MSGTR_VO_DirectoryCreateSuccess
);
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
,
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
;
164 /* Save for JFIF-Header PAR */
165 image_d_width
= d_width
;
166 image_d_height
= d_height
;
171 /* ------------------------------------------------------------------------- */
173 static uint32_t jpeg_write(uint8_t * name
, uint8_t * buffer
)
176 struct jpeg_compress_struct cinfo
;
177 struct jpeg_error_mgr jerr
;
178 JSAMPROW row_pointer
[1];
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
,
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
);
230 jpeg_destroy_compress(&cinfo
);
235 /* ------------------------------------------------------------------------- */
237 static int draw_frame(uint8_t *src
[])
239 static int framecounter
= 0, subdircounter
= 0;
241 static char subdirname
[BUFLENGTH
] = "";
243 /* Start writing to new subdirectory after a certain amount of frames */
244 if ( framecounter
== jpeg_maxfiles
) {
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
) {
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. */
261 /* snprintf the full pathname of the outputfile */
262 snprintf(buf
, BUFLENGTH
, "%s/%s/%08d.jpg", jpeg_outdir
, subdirname
,
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
,
290 /* ------------------------------------------------------------------------- */
292 static int query_format(uint32_t format
)
294 if (format
== IMGFMT_RGB24
) {
295 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
301 /* ------------------------------------------------------------------------- */
303 static void uninit(void)
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) )
333 static int preinit(const char *arg
)
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;
360 jpeg_maxfiles
= 1000;
361 jpeg_outdir
= strdup(".");
364 if (subopt_parse(arg
, subopts
) != 0) {
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
,
378 mp_msg(MSGT_VO
, MSGL_V
, "%s: smooth --> %d\n", info
.short_name
,
380 mp_msg(MSGT_VO
, MSGL_V
, "%s: quality --> %d\n", info
.short_name
,
382 mp_msg(MSGT_VO
, MSGL_V
, "%s: dpi --> %d\n", info
.short_name
,
384 mp_msg(MSGT_VO
, MSGL_V
, "%s: outdir --> %s\n", info
.short_name
,
387 mp_msg(MSGT_VO
, MSGL_V
, "%s: subdirs --> %s\n", info
.short_name
,
389 mp_msg(MSGT_VO
, MSGL_V
, "%s: maxfiles --> %d\n", info
.short_name
,
393 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s\n", info
.short_name
,
394 MSGTR_VO_SuboptionsParsedOK
);
398 /* ------------------------------------------------------------------------- */
400 static int control(uint32_t request
, void *data
, ...)
403 case VOCTRL_QUERY_FORMAT
:
404 return query_format(*((uint32_t*)data
));
409 /* ------------------------------------------------------------------------- */
413 /* ------------------------------------------------------------------------- */