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() */
45 /* ------------------------------------------------------------------------- */
49 /* Used for temporary buffers to store file- and pathnames */
52 #define PNM_ASCII_MODE 0
53 #define PNM_RAW_MODE 1
54 #define PNM_TYPE_PPM 0
55 #define PNM_TYPE_PGM 1
56 #define PNM_TYPE_PGMYUV 2
58 #define PNM_LINE_OF_ASCII "%03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d\n"
59 #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], \
60 a[b+7], a[b+8], a[b+9], a[b+10], a[b+11], a[b+12], \
63 /* ------------------------------------------------------------------------- */
67 static const vo_info_t info
=
69 "PPM/PGM/PGMYUV file",
71 "Ivo van Poorten (ivop@euronet.nl)",
75 const LIBVO_EXTERN (pnm
)
77 /* ------------------------------------------------------------------------- */
79 /* Global Variables */
81 int pnm_type
= PNM_TYPE_PPM
;
82 int pnm_mode
= PNM_RAW_MODE
;
84 char *pnm_outdir
= NULL
;
85 char *pnm_subdirs
= NULL
;
86 int pnm_maxfiles
= 1000;
87 char *pnm_file_extension
= NULL
;
89 /* ------------------------------------------------------------------------- */
91 /** \brief An error occured while writing to a file.
93 * The program failed to write data to a file.
94 * It displays a message and exits the player.
96 * \return nothing It does not return.
99 static void pnm_write_error(void) {
100 mp_tmsg(MSGT_VO
, MSGL_ERR
, "%s: Error writing file.\n", info
.short_name
);
101 exit_player_bad(_("Fatal error"));
104 /* ------------------------------------------------------------------------- */
106 /** \brief Pre-initialisation.
108 * This function is called before initialising the video output driver. It
109 * parses all suboptions and sets variables accordingly. If an error occurs
110 * (like an option being out of range, not having any value or an unknown
111 * option is stumbled upon) the player will exit.
113 * \param arg A string containing all the suboptions passed to the video
116 * \return 0 All went well.
119 static int preinit(const char *arg
)
121 int ppm_type
= 0, pgm_type
= 0, pgmyuv_type
= 0,
122 raw_mode
= 0, ascii_mode
= 0;
123 const opt_t subopts
[] = {
124 {"ppm", OPT_ARG_BOOL
, &ppm_type
, NULL
},
125 {"pgm", OPT_ARG_BOOL
, &pgm_type
, NULL
},
126 {"pgmyuv", OPT_ARG_BOOL
, &pgmyuv_type
, NULL
},
127 {"raw", OPT_ARG_BOOL
, &raw_mode
, NULL
},
128 {"ascii", OPT_ARG_BOOL
, &ascii_mode
, NULL
},
129 {"outdir", OPT_ARG_MSTRZ
, &pnm_outdir
, NULL
},
130 {"subdirs", OPT_ARG_MSTRZ
, &pnm_subdirs
, NULL
},
131 {"maxfiles", OPT_ARG_INT
, &pnm_maxfiles
, int_pos
},
132 {NULL
, 0, NULL
, NULL
}
134 const char *info_message
= NULL
;
136 mp_msg(MSGT_VO
, MSGL_V
, "%s: %s\n", info
.short_name
,
137 "Parsing suboptions.");
140 pnm_outdir
= strdup(".");
143 if (subopt_parse(arg
, subopts
) != 0) {
147 pnm_type
= PNM_TYPE_PPM
;
148 pnm_mode
= PNM_RAW_MODE
;
150 if (pgmyuv_type
) pnm_type
= PNM_TYPE_PGMYUV
;
151 if (pgm_type
) pnm_type
= PNM_TYPE_PGM
;
152 if (ppm_type
) pnm_type
= PNM_TYPE_PPM
;
153 if (ascii_mode
) pnm_mode
= PNM_ASCII_MODE
;
154 if (raw_mode
) pnm_mode
= PNM_RAW_MODE
;
158 info_message
= _("ASCII mode enabled.");
161 info_message
= _("Raw mode enabled.");
164 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s\n", info
.short_name
, info_message
);
168 info_message
= _("Will write PPM files.");
171 info_message
= _("Will write PGM files.");
173 case PNM_TYPE_PGMYUV
:
174 info_message
= _("Will write PGMYUV files.");
177 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s\n", info
.short_name
, info_message
);
179 mp_msg(MSGT_VO
, MSGL_V
, "%s: %s\n", info
.short_name
,
180 "Suboptions parsed OK.");
184 /* ------------------------------------------------------------------------- */
186 /** \brief Create a directory.
188 * This function creates a directory. If it already exists, it tests if
189 * it's a directory and not something else, and if it is, it tests whether
190 * the directory is writable or not.
192 * \param buf Pointer to directory name.
193 * \param verbose Verbose on success. If verbose is non-zero, it will print
194 * a message if it was successful in creating the directory.
196 * \return nothing In case anything fails, the player will exit. If it
197 * returns, everything went well.
200 static void pnm_mkdir(char *buf
, int verbose
) {
203 if ( mkdir(buf
, 0755) < 0 ) {
204 switch (errno
) { /* use switch in case other errors need to be caught
205 and handled in the future */
207 if ( mp_stat(buf
, &stat_p
) < 0 ) {
208 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n", info
.short_name
,
209 _("This error has occurred"), strerror(errno
) );
210 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s %s\n", info
.short_name
,
211 _("Unable to access"), buf
);
212 exit_player_bad(_("Fatal error"));
214 if ( !S_ISDIR(stat_p
.st_mode
) ) {
215 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s %s\n", info
.short_name
,
216 buf
, _("already exists, but is not a directory."));
217 exit_player_bad(_("Fatal error"));
219 if ( !(stat_p
.st_mode
& S_IWUSR
) ) {
220 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s - %s\n", info
.short_name
,
221 buf
, _("Output directory already exists, but is not writable."));
222 exit_player_bad(_("Fatal error"));
225 if (strcmp(buf
, ".") != 0) {
226 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
227 buf
, _("Output directory already exists and is writable."));
232 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n", info
.short_name
,
233 _("This error has occurred"), strerror(errno
) );
234 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s - %s\n", info
.short_name
,
235 buf
, _("Unable to create output directory."));
236 exit_player_bad(_("Fatal error"));
238 } else if ( verbose
) {
239 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
240 buf
, _("Output directory successfully created."));
244 /* ------------------------------------------------------------------------- */
246 /** \brief Configure the video output driver.
248 * This functions configures the video output driver. It determines the
249 * width and height of the image(s) and creates the output directory.
251 * \return 0 All went well.
254 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
255 uint32_t d_height
, uint32_t flags
, char *title
,
260 if (vo_config_count
> 0 ) { /* Already configured */
266 snprintf(buf
, BUFLENGTH
, "%s", pnm_outdir
);
267 pnm_mkdir(buf
, 1); /* This function only returns if creation was
268 successful. If not, the player will exit. */
270 if (pnm_type
== PNM_TYPE_PPM
) {
271 pnm_file_extension
= strdup("ppm");
272 } else if (pnm_type
== PNM_TYPE_PGM
) {
273 pnm_file_extension
= strdup("pgm");
274 } else if (pnm_type
== PNM_TYPE_PGMYUV
) {
275 pnm_file_extension
= strdup("pgmyuv");
281 /* ------------------------------------------------------------------------- */
283 /** \brief Write PNM file to output file
285 * This function writes PPM, PGM or PGMYUV data to an output file, depending
286 * on which type was selected on the commandline. pnm_type and pnm_mode are
287 * global variables. Depending on which mode was selected, it will write
288 * a RAW or an ASCII file.
290 * \param outfile Filedescriptor of output file.
291 * \param mpi The image to write.
293 * \return none The player will exit if anything goes wrong.
296 static void pnm_write_pnm(FILE *outfile
, mp_image_t
*mpi
)
300 uint8_t *rgbimage
= mpi
->planes
[0];
301 uint8_t *planeY
= mpi
->planes
[0];
302 uint8_t *planeU
= mpi
->planes
[1];
303 uint8_t *planeV
= mpi
->planes
[2];
305 uint32_t strideY
= mpi
->stride
[0];
306 uint32_t strideU
= mpi
->stride
[1];
307 uint32_t strideV
= mpi
->stride
[2];
311 if (pnm_mode
== PNM_RAW_MODE
) {
313 if (pnm_type
== PNM_TYPE_PPM
) {
314 if ( fprintf(outfile
, "P6\n%d %d\n255\n", w
, h
) < 0 )
316 if ( fwrite(rgbimage
, w
* 3, h
, outfile
) < h
) pnm_write_error();
317 } else if (pnm_type
== PNM_TYPE_PGM
) {
318 if ( fprintf(outfile
, "P5\n%d %d\n255\n", w
, h
) < 0 )
320 for (i
=0; i
<h
; i
++) {
321 if ( fwrite(planeY
+ i
* strideY
, w
, 1, outfile
) < 1 )
324 } else if (pnm_type
== PNM_TYPE_PGMYUV
) {
325 if ( fprintf(outfile
, "P5\n%d %d\n255\n", w
, h
*3/2) < 0 )
327 for (i
=0; i
<h
; i
++) {
328 if ( fwrite(planeY
+ i
* strideY
, w
, 1, outfile
) < 1 )
333 for (i
=0; i
<h
; i
++) {
334 if ( fwrite(planeU
+ i
* strideU
, w
, 1, outfile
) < 1 )
336 if ( fwrite(planeV
+ i
* strideV
, w
, 1, outfile
) < 1 )
339 } /* end if pnm_type */
341 } else if (pnm_mode
== PNM_ASCII_MODE
) {
343 if (pnm_type
== PNM_TYPE_PPM
) {
344 if ( fprintf(outfile
, "P3\n%d %d\n255\n", w
, h
) < 0 )
346 for (i
=0; i
<= w
* h
* 3 - 16 ; i
+= 15) {
347 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
348 PNM_LINE15(rgbimage
,i
) ) < 0 ) pnm_write_error();
350 while (i
< (w
* h
* 3) ) {
351 if ( fprintf(outfile
, "%03d ", rgbimage
[i
]) < 0 )
355 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
356 } else if ( (pnm_type
== PNM_TYPE_PGM
) ||
357 (pnm_type
== PNM_TYPE_PGMYUV
) ) {
359 /* different header for pgm and pgmyuv. pgmyuv is 'higher' */
360 if (pnm_type
== PNM_TYPE_PGM
) {
361 if ( fprintf(outfile
, "P2\n%d %d\n255\n", w
, h
) < 0 )
363 } else { /* PNM_TYPE_PGMYUV */
364 if ( fprintf(outfile
, "P2\n%d %d\n255\n", w
, h
*3/2) < 0 )
368 /* output Y plane for both PGM and PGMYUV */
369 for (j
=0; j
<h
; j
++) {
370 curline
= planeY
+ strideY
* j
;
371 for (i
=0; i
<= w
- 16; i
+=15) {
372 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
373 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
376 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
380 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
383 /* also output U and V planes fpr PGMYUV */
384 if (pnm_type
== PNM_TYPE_PGMYUV
) {
387 for (j
=0; j
<h
; j
++) {
388 curline
= planeU
+ strideU
* j
;
389 for (i
=0; i
<= w
-16; i
+=15) {
390 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
391 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
394 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
398 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
400 curline
= planeV
+ strideV
* j
;
401 for (i
=0; i
<= w
-16; i
+=15) {
402 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
403 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
406 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
410 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
414 } /* end if pnm_type */
415 } /* end if pnm_mode */
418 /* ------------------------------------------------------------------------- */
420 /** \brief Write a PNM image.
422 * This function gets called first if a PNM image has to be written to disk.
423 * It contains the subdirectory framework and it calls pnm_write_pnm() to
424 * actually write the image to disk.
426 * \param mpi The image to write.
428 * \return none The player will exit if anything goes wrong.
431 static void pnm_write_image(mp_image_t
*mpi
)
433 static int framenum
= 0, framecounter
= 0, subdircounter
= 0;
435 static char subdirname
[BUFLENGTH
] = "";
439 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: No image data supplied to video output driver\n", info
.short_name
);
440 exit_player_bad(_("Fatal error"));
443 /* Start writing to new subdirectory after a certain amount of frames */
444 if ( framecounter
== pnm_maxfiles
) {
448 /* If framecounter is zero (or reset to zero), increment subdirectory
449 * number and create the subdirectory.
450 * If pnm_subdirs is not set, do nothing. */
451 if ( !framecounter
&& pnm_subdirs
) {
453 snprintf(subdirname
, BUFLENGTH
, "%s%08d", pnm_subdirs
, subdircounter
);
454 snprintf(buf
, BUFLENGTH
, "%s/%s", pnm_outdir
, subdirname
);
455 pnm_mkdir(buf
, 0); /* This function only returns if creation was
456 successful. If not, the player will exit. */
462 /* snprintf the full pathname of the outputfile */
463 snprintf(buf
, BUFLENGTH
, "%s/%s/%08d.%s", pnm_outdir
, subdirname
,
464 framenum
, pnm_file_extension
);
466 if ( (outfile
= fopen(buf
, "wb") ) == NULL
) {
467 mp_msg(MSGT_VO
, MSGL_ERR
, "\n%s: %s\n", info
.short_name
,
468 "Unable to create output file.");
469 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n",
470 info
.short_name
, "This error has occurred",
472 exit_player_bad(_("Fatal error"));
475 pnm_write_pnm(outfile
, mpi
);
480 /* ------------------------------------------------------------------------- */
482 static uint32_t draw_image(mp_image_t
*mpi
)
484 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) { /* Planar */
485 if (mpi
->flags
& MP_IMGFLAG_YUV
) { /* Planar YUV */
486 pnm_write_image(mpi
);
488 } else { /* Planar RGB */
491 } else { /* Packed */
492 if (mpi
->flags
& MP_IMGFLAG_YUV
) { /* Packed YUV */
494 } else { /* Packed RGB */
495 pnm_write_image(mpi
);
503 /* ------------------------------------------------------------------------- */
505 static int draw_frame(uint8_t *src
[])
507 mp_msg(MSGT_VO
, MSGL_V
, "%s: draw_frame() is called!\n", info
.short_name
);
511 /* ------------------------------------------------------------------------- */
513 static int draw_slice(uint8_t *src
[], int stride
[], int w
, int h
,
519 /* ------------------------------------------------------------------------- */
521 static int query_format(uint32_t format
)
523 /* Ensure that for PPM we get Packed RGB and for PGM(YUV) we get
525 if (pnm_type
== PNM_TYPE_PPM
) {
526 if (format
== IMGFMT_RGB24
) {
527 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
529 } else if ( (pnm_type
== PNM_TYPE_PGM
) || (pnm_type
== PNM_TYPE_PGMYUV
) ) {
530 if (format
== IMGFMT_YV12
) {
531 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
538 /* ------------------------------------------------------------------------- */
540 static int control(uint32_t request
, void *data
)
543 case VOCTRL_QUERY_FORMAT
:
544 return query_format(*((uint32_t*)data
));
545 case VOCTRL_DRAW_IMAGE
:
546 return draw_image(data
);
551 /* ------------------------------------------------------------------------- */
553 static void uninit(void)
561 /* ------------------------------------------------------------------------- */
563 static void check_events(void)
567 /* ------------------------------------------------------------------------- */
569 static void draw_osd(void)
573 /* ------------------------------------------------------------------------- */
575 static void flip_page (void)
579 /* ------------------------------------------------------------------------- */
583 #undef PNM_ASCII_MODE
586 #undef PNM_TYPE_PGMYUV
588 /* ------------------------------------------------------------------------- */