2 * PPM/PGM/PGMYUV video output driver
4 * copyright (C) 2004, 2005 Ivo van Poorten
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* ------------------------------------------------------------------------- */
33 /* ------------------------------------------------------------------------- */
38 #include "subopt-helper.h"
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "mplayer.h" /* for exit_player_bad() */
44 /* ------------------------------------------------------------------------- */
48 /* Used for temporary buffers to store file- and pathnames */
51 #define PNM_ASCII_MODE 0
52 #define PNM_RAW_MODE 1
53 #define PNM_TYPE_PPM 0
54 #define PNM_TYPE_PGM 1
55 #define PNM_TYPE_PGMYUV 2
57 #define PNM_LINE_OF_ASCII "%03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d\n"
58 #define PNM_LINE15(a,b) a[b], a[b+1], a[b+2], a[b+3], a[b+4], a[b+5], a[b+6], \
59 a[b+7], a[b+8], a[b+9], a[b+10], a[b+11], a[b+12], \
62 /* ------------------------------------------------------------------------- */
66 static const vo_info_t info
=
68 "PPM/PGM/PGMYUV file",
70 "Ivo van Poorten (ivop@euronet.nl)",
74 const LIBVO_EXTERN (pnm
)
76 /* ------------------------------------------------------------------------- */
78 /* Global Variables */
80 int pnm_type
= PNM_TYPE_PPM
;
81 int pnm_mode
= PNM_RAW_MODE
;
83 char *pnm_outdir
= NULL
;
84 char *pnm_subdirs
= NULL
;
85 int pnm_maxfiles
= 1000;
86 char *pnm_file_extension
= NULL
;
88 /* ------------------------------------------------------------------------- */
90 /** \brief An error occured while writing to a file.
92 * The program failed to write data to a file.
93 * It displays a message and exits the player.
95 * \return nothing It does not return.
98 static void pnm_write_error(void) {
99 mp_tmsg(MSGT_VO
, MSGL_ERR
, "%s: Error writing file.\n", info
.short_name
);
100 exit_player_bad(_("Fatal error"));
103 /* ------------------------------------------------------------------------- */
105 /** \brief Pre-initialisation.
107 * This function is called before initialising the video output driver. It
108 * parses all suboptions and sets variables accordingly. If an error occurs
109 * (like an option being out of range, not having any value or an unknown
110 * option is stumbled upon) the player will exit.
112 * \param arg A string containing all the suboptions passed to the video
115 * \return 0 All went well.
118 static int preinit(const char *arg
)
120 int ppm_type
= 0, pgm_type
= 0, pgmyuv_type
= 0,
121 raw_mode
= 0, ascii_mode
= 0;
122 const opt_t subopts
[] = {
123 {"ppm", OPT_ARG_BOOL
, &ppm_type
, NULL
},
124 {"pgm", OPT_ARG_BOOL
, &pgm_type
, NULL
},
125 {"pgmyuv", OPT_ARG_BOOL
, &pgmyuv_type
, NULL
},
126 {"raw", OPT_ARG_BOOL
, &raw_mode
, NULL
},
127 {"ascii", OPT_ARG_BOOL
, &ascii_mode
, NULL
},
128 {"outdir", OPT_ARG_MSTRZ
, &pnm_outdir
, NULL
},
129 {"subdirs", OPT_ARG_MSTRZ
, &pnm_subdirs
, NULL
},
130 {"maxfiles", OPT_ARG_INT
, &pnm_maxfiles
, int_pos
},
131 {NULL
, 0, NULL
, NULL
}
133 const char *info_message
= NULL
;
135 mp_msg(MSGT_VO
, MSGL_V
, "%s: %s\n", info
.short_name
,
136 "Parsing suboptions.");
139 pnm_outdir
= strdup(".");
142 if (subopt_parse(arg
, subopts
) != 0) {
146 pnm_type
= PNM_TYPE_PPM
;
147 pnm_mode
= PNM_RAW_MODE
;
149 if (pgmyuv_type
) pnm_type
= PNM_TYPE_PGMYUV
;
150 if (pgm_type
) pnm_type
= PNM_TYPE_PGM
;
151 if (ppm_type
) pnm_type
= PNM_TYPE_PPM
;
152 if (ascii_mode
) pnm_mode
= PNM_ASCII_MODE
;
153 if (raw_mode
) pnm_mode
= PNM_RAW_MODE
;
157 info_message
= _("ASCII mode enabled.");
160 info_message
= _("Raw mode enabled.");
163 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s\n", info
.short_name
, info_message
);
167 info_message
= _("Will write PPM files.");
170 info_message
= _("Will write PGM files.");
172 case PNM_TYPE_PGMYUV
:
173 info_message
= _("Will write PGMYUV files.");
176 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s\n", info
.short_name
, info_message
);
178 mp_msg(MSGT_VO
, MSGL_V
, "%s: %s\n", info
.short_name
,
179 "Suboptions parsed OK.");
183 /* ------------------------------------------------------------------------- */
185 /** \brief Create a directory.
187 * This function creates a directory. If it already exists, it tests if
188 * it's a directory and not something else, and if it is, it tests whether
189 * the directory is writable or not.
191 * \param buf Pointer to directory name.
192 * \param verbose Verbose on success. If verbose is non-zero, it will print
193 * a message if it was successful in creating the directory.
195 * \return nothing In case anything fails, the player will exit. If it
196 * returns, everything went well.
199 static void pnm_mkdir(char *buf
, int verbose
) {
202 /* Silly MING32 bug workaround */
204 if ( mkdir(buf
, 0755) < 0 ) {
206 if ( mkdir(buf
) < 0 ) {
208 switch (errno
) { /* use switch in case other errors need to be caught
209 and handled in the future */
211 if ( stat(buf
, &stat_p
) < 0 ) {
212 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n", info
.short_name
,
213 _("This error has occurred"), strerror(errno
) );
214 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s %s\n", info
.short_name
,
215 _("Unable to access"), buf
);
216 exit_player_bad(_("Fatal error"));
218 if ( !S_ISDIR(stat_p
.st_mode
) ) {
219 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s %s\n", info
.short_name
,
220 buf
, _("already exists, but is not a directory."));
221 exit_player_bad(_("Fatal error"));
223 if ( !(stat_p
.st_mode
& S_IWUSR
) ) {
224 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s - %s\n", info
.short_name
,
225 buf
, _("Output directory already exists, but is not writable."));
226 exit_player_bad(_("Fatal error"));
229 if (strcmp(buf
, ".") != 0) {
230 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
231 buf
, _("Output directory already exists and is writable."));
236 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n", info
.short_name
,
237 _("This error has occurred"), strerror(errno
) );
238 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s - %s\n", info
.short_name
,
239 buf
, _("Unable to create output directory."));
240 exit_player_bad(_("Fatal error"));
242 } else if ( verbose
) {
243 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
244 buf
, _("Output directory successfully created."));
248 /* ------------------------------------------------------------------------- */
250 /** \brief Configure the video output driver.
252 * This functions configures the video output driver. It determines the
253 * width and height of the image(s) and creates the output directory.
255 * \return 0 All went well.
258 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
259 uint32_t d_height
, uint32_t flags
, char *title
,
264 if (vo_config_count
> 0 ) { /* Already configured */
270 snprintf(buf
, BUFLENGTH
, "%s", pnm_outdir
);
271 pnm_mkdir(buf
, 1); /* This function only returns if creation was
272 successful. If not, the player will exit. */
274 if (pnm_type
== PNM_TYPE_PPM
) {
275 pnm_file_extension
= strdup("ppm");
276 } else if (pnm_type
== PNM_TYPE_PGM
) {
277 pnm_file_extension
= strdup("pgm");
278 } else if (pnm_type
== PNM_TYPE_PGMYUV
) {
279 pnm_file_extension
= strdup("pgmyuv");
285 /* ------------------------------------------------------------------------- */
287 /** \brief Write PNM file to output file
289 * This function writes PPM, PGM or PGMYUV data to an output file, depending
290 * on which type was selected on the commandline. pnm_type and pnm_mode are
291 * global variables. Depending on which mode was selected, it will write
292 * a RAW or an ASCII file.
294 * \param outfile Filedescriptor of output file.
295 * \param mpi The image to write.
297 * \return none The player will exit if anything goes wrong.
300 static void pnm_write_pnm(FILE *outfile
, mp_image_t
*mpi
)
304 uint8_t *rgbimage
= mpi
->planes
[0];
305 uint8_t *planeY
= mpi
->planes
[0];
306 uint8_t *planeU
= mpi
->planes
[1];
307 uint8_t *planeV
= mpi
->planes
[2];
309 uint32_t strideY
= mpi
->stride
[0];
310 uint32_t strideU
= mpi
->stride
[1];
311 uint32_t strideV
= mpi
->stride
[2];
315 if (pnm_mode
== PNM_RAW_MODE
) {
317 if (pnm_type
== PNM_TYPE_PPM
) {
318 if ( fprintf(outfile
, "P6\n%d %d\n255\n", w
, h
) < 0 )
320 if ( fwrite(rgbimage
, w
* 3, h
, outfile
) < h
) pnm_write_error();
321 } else if (pnm_type
== PNM_TYPE_PGM
) {
322 if ( fprintf(outfile
, "P5\n%d %d\n255\n", w
, h
) < 0 )
324 for (i
=0; i
<h
; i
++) {
325 if ( fwrite(planeY
+ i
* strideY
, w
, 1, outfile
) < 1 )
328 } else if (pnm_type
== PNM_TYPE_PGMYUV
) {
329 if ( fprintf(outfile
, "P5\n%d %d\n255\n", w
, h
*3/2) < 0 )
331 for (i
=0; i
<h
; i
++) {
332 if ( fwrite(planeY
+ i
* strideY
, w
, 1, outfile
) < 1 )
337 for (i
=0; i
<h
; i
++) {
338 if ( fwrite(planeU
+ i
* strideU
, w
, 1, outfile
) < 1 )
340 if ( fwrite(planeV
+ i
* strideV
, w
, 1, outfile
) < 1 )
343 } /* end if pnm_type */
345 } else if (pnm_mode
== PNM_ASCII_MODE
) {
347 if (pnm_type
== PNM_TYPE_PPM
) {
348 if ( fprintf(outfile
, "P3\n%d %d\n255\n", w
, h
) < 0 )
350 for (i
=0; i
<= w
* h
* 3 - 16 ; i
+= 15) {
351 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
352 PNM_LINE15(rgbimage
,i
) ) < 0 ) pnm_write_error();
354 while (i
< (w
* h
* 3) ) {
355 if ( fprintf(outfile
, "%03d ", rgbimage
[i
]) < 0 )
359 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
360 } else if ( (pnm_type
== PNM_TYPE_PGM
) ||
361 (pnm_type
== PNM_TYPE_PGMYUV
) ) {
363 /* different header for pgm and pgmyuv. pgmyuv is 'higher' */
364 if (pnm_type
== PNM_TYPE_PGM
) {
365 if ( fprintf(outfile
, "P2\n%d %d\n255\n", w
, h
) < 0 )
367 } else { /* PNM_TYPE_PGMYUV */
368 if ( fprintf(outfile
, "P2\n%d %d\n255\n", w
, h
*3/2) < 0 )
372 /* output Y plane for both PGM and PGMYUV */
373 for (j
=0; j
<h
; j
++) {
374 curline
= planeY
+ strideY
* j
;
375 for (i
=0; i
<= w
- 16; i
+=15) {
376 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
377 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
380 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
384 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
387 /* also output U and V planes fpr PGMYUV */
388 if (pnm_type
== PNM_TYPE_PGMYUV
) {
391 for (j
=0; j
<h
; j
++) {
392 curline
= planeU
+ strideU
* j
;
393 for (i
=0; i
<= w
-16; i
+=15) {
394 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
395 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
398 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
402 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
404 curline
= planeV
+ strideV
* j
;
405 for (i
=0; i
<= w
-16; i
+=15) {
406 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
407 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
410 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
414 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
418 } /* end if pnm_type */
419 } /* end if pnm_mode */
422 /* ------------------------------------------------------------------------- */
424 /** \brief Write a PNM image.
426 * This function gets called first if a PNM image has to be written to disk.
427 * It contains the subdirectory framework and it calls pnm_write_pnm() to
428 * actually write the image to disk.
430 * \param mpi The image to write.
432 * \return none The player will exit if anything goes wrong.
435 static void pnm_write_image(mp_image_t
*mpi
)
437 static int framenum
= 0, framecounter
= 0, subdircounter
= 0;
439 static char subdirname
[BUFLENGTH
] = "";
443 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: No image data suplied to video output driver\n", info
.short_name
);
444 exit_player_bad(_("Fatal error"));
447 /* Start writing to new subdirectory after a certain amount of frames */
448 if ( framecounter
== pnm_maxfiles
) {
452 /* If framecounter is zero (or reset to zero), increment subdirectory
453 * number and create the subdirectory.
454 * If pnm_subdirs is not set, do nothing. */
455 if ( !framecounter
&& pnm_subdirs
) {
457 snprintf(subdirname
, BUFLENGTH
, "%s%08d", pnm_subdirs
, subdircounter
);
458 snprintf(buf
, BUFLENGTH
, "%s/%s", pnm_outdir
, subdirname
);
459 pnm_mkdir(buf
, 0); /* This function only returns if creation was
460 successful. If not, the player will exit. */
466 /* snprintf the full pathname of the outputfile */
467 snprintf(buf
, BUFLENGTH
, "%s/%s/%08d.%s", pnm_outdir
, subdirname
,
468 framenum
, pnm_file_extension
);
470 if ( (outfile
= fopen(buf
, "wb") ) == NULL
) {
471 mp_msg(MSGT_VO
, MSGL_ERR
, "\n%s: %s\n", info
.short_name
,
472 "Unable to create output file.");
473 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n",
474 info
.short_name
, "This error has occurred",
476 exit_player_bad(_("Fatal error"));
479 pnm_write_pnm(outfile
, mpi
);
484 /* ------------------------------------------------------------------------- */
486 static uint32_t draw_image(mp_image_t
*mpi
)
488 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) { /* Planar */
489 if (mpi
->flags
& MP_IMGFLAG_YUV
) { /* Planar YUV */
490 pnm_write_image(mpi
);
492 } else { /* Planar RGB */
495 } else { /* Packed */
496 if (mpi
->flags
& MP_IMGFLAG_YUV
) { /* Packed YUV */
498 } else { /* Packed RGB */
499 pnm_write_image(mpi
);
507 /* ------------------------------------------------------------------------- */
509 static int draw_frame(uint8_t *src
[])
511 mp_msg(MSGT_VO
, MSGL_V
, "%s: draw_frame() is called!\n", info
.short_name
);
515 /* ------------------------------------------------------------------------- */
517 static int draw_slice(uint8_t *src
[], int stride
[], int w
, int h
,
523 /* ------------------------------------------------------------------------- */
525 static int query_format(uint32_t format
)
527 /* Ensure that for PPM we get Packed RGB and for PGM(YUV) we get
529 if (pnm_type
== PNM_TYPE_PPM
) {
530 if (format
== IMGFMT_RGB24
) {
531 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
533 } else if ( (pnm_type
== PNM_TYPE_PGM
) || (pnm_type
== PNM_TYPE_PGMYUV
) ) {
534 if (format
== IMGFMT_YV12
) {
535 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
542 /* ------------------------------------------------------------------------- */
544 static int control(uint32_t request
, void *data
)
547 case VOCTRL_QUERY_FORMAT
:
548 return query_format(*((uint32_t*)data
));
549 case VOCTRL_DRAW_IMAGE
:
550 return draw_image(data
);
555 /* ------------------------------------------------------------------------- */
557 static void uninit(void)
569 /* ------------------------------------------------------------------------- */
571 static void check_events(void)
575 /* ------------------------------------------------------------------------- */
577 static void draw_osd(void)
581 /* ------------------------------------------------------------------------- */
583 static void flip_page (void)
587 /* ------------------------------------------------------------------------- */
591 #undef PNM_ASCII_MODE
594 #undef PNM_TYPE_PGMYUV
596 /* ------------------------------------------------------------------------- */