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
, (opt_test_f
)int_pos
},
132 {NULL
, 0, NULL
, NULL
}
134 const char *info_message
= NULL
;
136 mp_msg(MSGT_VO
, MSGL_INFO
, "%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_INFO
, "%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 /* Silly MING32 bug workaround */
205 if ( mkdir(buf
, 0755) < 0 ) {
207 if ( mkdir(buf
) < 0 ) {
209 switch (errno
) { /* use switch in case other errors need to be caught
210 and handled in the future */
212 if ( stat(buf
, &stat_p
) < 0 ) {
213 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n", info
.short_name
,
214 _("This error has occurred"), strerror(errno
) );
215 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s %s\n", info
.short_name
,
216 _("Unable to access"), buf
);
217 exit_player_bad(_("Fatal error"));
219 if ( !S_ISDIR(stat_p
.st_mode
) ) {
220 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s %s\n", info
.short_name
,
221 buf
, _("already exists, but is not a directory."));
222 exit_player_bad(_("Fatal error"));
224 if ( !(stat_p
.st_mode
& S_IWUSR
) ) {
225 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s - %s\n", info
.short_name
,
226 buf
, _("Output directory already exists, but is not writable."));
227 exit_player_bad(_("Fatal error"));
230 if (strcmp(buf
, ".") != 0) {
231 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
232 buf
, _("Output directory already exists and is writable."));
237 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n", info
.short_name
,
238 _("This error has occurred"), strerror(errno
) );
239 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s - %s\n", info
.short_name
,
240 buf
, _("Unable to create output directory."));
241 exit_player_bad(_("Fatal error"));
243 } else if ( verbose
) {
244 mp_msg(MSGT_VO
, MSGL_INFO
, "%s: %s - %s\n", info
.short_name
,
245 buf
, _("Output directory successfully created."));
249 /* ------------------------------------------------------------------------- */
251 /** \brief Configure the video output driver.
253 * This functions configures the video output driver. It determines the
254 * width and height of the image(s) and creates the output directory.
256 * \return 0 All went well.
259 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
260 uint32_t d_height
, uint32_t flags
, char *title
,
265 if (vo_config_count
> 0 ) { /* Already configured */
271 snprintf(buf
, BUFLENGTH
, "%s", pnm_outdir
);
272 pnm_mkdir(buf
, 1); /* This function only returns if creation was
273 successful. If not, the player will exit. */
275 if (pnm_type
== PNM_TYPE_PPM
) {
276 pnm_file_extension
= strdup("ppm");
277 } else if (pnm_type
== PNM_TYPE_PGM
) {
278 pnm_file_extension
= strdup("pgm");
279 } else if (pnm_type
== PNM_TYPE_PGMYUV
) {
280 pnm_file_extension
= strdup("pgmyuv");
286 /* ------------------------------------------------------------------------- */
288 /** \brief Write PNM file to output file
290 * This function writes PPM, PGM or PGMYUV data to an output file, depending
291 * on which type was selected on the commandline. pnm_type and pnm_mode are
292 * global variables. Depending on which mode was selected, it will write
293 * a RAW or an ASCII file.
295 * \param outfile Filedescriptor of output file.
296 * \param mpi The image to write.
298 * \return none The player will exit if anything goes wrong.
301 static void pnm_write_pnm(FILE *outfile
, mp_image_t
*mpi
)
305 uint8_t *rgbimage
= mpi
->planes
[0];
306 uint8_t *planeY
= mpi
->planes
[0];
307 uint8_t *planeU
= mpi
->planes
[1];
308 uint8_t *planeV
= mpi
->planes
[2];
310 uint32_t strideY
= mpi
->stride
[0];
311 uint32_t strideU
= mpi
->stride
[1];
312 uint32_t strideV
= mpi
->stride
[2];
316 if (pnm_mode
== PNM_RAW_MODE
) {
318 if (pnm_type
== PNM_TYPE_PPM
) {
319 if ( fprintf(outfile
, "P6\n%d %d\n255\n", w
, h
) < 0 )
321 if ( fwrite(rgbimage
, w
* 3, h
, outfile
) < h
) pnm_write_error();
322 } else if (pnm_type
== PNM_TYPE_PGM
) {
323 if ( fprintf(outfile
, "P5\n%d %d\n255\n", w
, h
) < 0 )
325 for (i
=0; i
<h
; i
++) {
326 if ( fwrite(planeY
+ i
* strideY
, w
, 1, outfile
) < 1 )
329 } else if (pnm_type
== PNM_TYPE_PGMYUV
) {
330 if ( fprintf(outfile
, "P5\n%d %d\n255\n", w
, h
*3/2) < 0 )
332 for (i
=0; i
<h
; i
++) {
333 if ( fwrite(planeY
+ i
* strideY
, w
, 1, outfile
) < 1 )
338 for (i
=0; i
<h
; i
++) {
339 if ( fwrite(planeU
+ i
* strideU
, w
, 1, outfile
) < 1 )
341 if ( fwrite(planeV
+ i
* strideV
, w
, 1, outfile
) < 1 )
344 } /* end if pnm_type */
346 } else if (pnm_mode
== PNM_ASCII_MODE
) {
348 if (pnm_type
== PNM_TYPE_PPM
) {
349 if ( fprintf(outfile
, "P3\n%d %d\n255\n", w
, h
) < 0 )
351 for (i
=0; i
<= w
* h
* 3 - 16 ; i
+= 15) {
352 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
353 PNM_LINE15(rgbimage
,i
) ) < 0 ) pnm_write_error();
355 while (i
< (w
* h
* 3) ) {
356 if ( fprintf(outfile
, "%03d ", rgbimage
[i
]) < 0 )
360 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
361 } else if ( (pnm_type
== PNM_TYPE_PGM
) ||
362 (pnm_type
== PNM_TYPE_PGMYUV
) ) {
364 /* different header for pgm and pgmyuv. pgmyuv is 'higher' */
365 if (pnm_type
== PNM_TYPE_PGM
) {
366 if ( fprintf(outfile
, "P2\n%d %d\n255\n", w
, h
) < 0 )
368 } else { /* PNM_TYPE_PGMYUV */
369 if ( fprintf(outfile
, "P2\n%d %d\n255\n", w
, h
*3/2) < 0 )
373 /* output Y plane for both PGM and PGMYUV */
374 for (j
=0; j
<h
; j
++) {
375 curline
= planeY
+ strideY
* j
;
376 for (i
=0; i
<= w
- 16; i
+=15) {
377 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
378 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
381 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
385 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
388 /* also output U and V planes fpr PGMYUV */
389 if (pnm_type
== PNM_TYPE_PGMYUV
) {
392 for (j
=0; j
<h
; j
++) {
393 curline
= planeU
+ strideU
* j
;
394 for (i
=0; i
<= w
-16; i
+=15) {
395 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
396 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
399 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
403 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
405 curline
= planeV
+ strideV
* j
;
406 for (i
=0; i
<= w
-16; i
+=15) {
407 if ( fprintf(outfile
, PNM_LINE_OF_ASCII
,
408 PNM_LINE15(curline
,i
) ) < 0 ) pnm_write_error();
411 if ( fprintf(outfile
, "%03d ", curline
[i
]) < 0 )
415 if ( fputc('\n', outfile
) < 0 ) pnm_write_error();
419 } /* end if pnm_type */
420 } /* end if pnm_mode */
423 /* ------------------------------------------------------------------------- */
425 /** \brief Write a PNM image.
427 * This function gets called first if a PNM image has to be written to disk.
428 * It contains the subdirectory framework and it calls pnm_write_pnm() to
429 * actually write the image to disk.
431 * \param mpi The image to write.
433 * \return none The player will exit if anything goes wrong.
436 static void pnm_write_image(mp_image_t
*mpi
)
438 static int framenum
= 0, framecounter
= 0, subdircounter
= 0;
440 static char subdirname
[BUFLENGTH
] = "";
444 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: No image data suplied to video output driver\n", info
.short_name
);
445 exit_player_bad(_("Fatal error"));
448 /* Start writing to new subdirectory after a certain amount of frames */
449 if ( framecounter
== pnm_maxfiles
) {
453 /* If framecounter is zero (or reset to zero), increment subdirectory
454 * number and create the subdirectory.
455 * If pnm_subdirs is not set, do nothing. */
456 if ( !framecounter
&& pnm_subdirs
) {
458 snprintf(subdirname
, BUFLENGTH
, "%s%08d", pnm_subdirs
, subdircounter
);
459 snprintf(buf
, BUFLENGTH
, "%s/%s", pnm_outdir
, subdirname
);
460 pnm_mkdir(buf
, 0); /* This function only returns if creation was
461 successful. If not, the player will exit. */
467 /* snprintf the full pathname of the outputfile */
468 snprintf(buf
, BUFLENGTH
, "%s/%s/%08d.%s", pnm_outdir
, subdirname
,
469 framenum
, pnm_file_extension
);
471 if ( (outfile
= fopen(buf
, "wb") ) == NULL
) {
472 mp_msg(MSGT_VO
, MSGL_ERR
, "\n%s: %s\n", info
.short_name
,
473 "Unable to create output file.");
474 mp_msg(MSGT_VO
, MSGL_ERR
, "%s: %s: %s\n",
475 info
.short_name
, "This error has occurred",
477 exit_player_bad(_("Fatal error"));
480 pnm_write_pnm(outfile
, mpi
);
485 /* ------------------------------------------------------------------------- */
487 static uint32_t draw_image(mp_image_t
*mpi
)
489 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) { /* Planar */
490 if (mpi
->flags
& MP_IMGFLAG_YUV
) { /* Planar YUV */
491 pnm_write_image(mpi
);
493 } else { /* Planar RGB */
496 } else { /* Packed */
497 if (mpi
->flags
& MP_IMGFLAG_YUV
) { /* Packed YUV */
499 } else { /* Packed RGB */
500 pnm_write_image(mpi
);
508 /* ------------------------------------------------------------------------- */
510 static int draw_frame(uint8_t *src
[])
512 mp_msg(MSGT_VO
, MSGL_V
, "%s: draw_frame() is called!\n", info
.short_name
);
516 /* ------------------------------------------------------------------------- */
518 static int draw_slice(uint8_t *src
[], int stride
[], int w
, int h
,
524 /* ------------------------------------------------------------------------- */
526 static int query_format(uint32_t format
)
528 /* Ensure that for PPM we get Packed RGB and for PGM(YUV) we get
530 if (pnm_type
== PNM_TYPE_PPM
) {
531 if (format
== IMGFMT_RGB24
) {
532 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
534 } else if ( (pnm_type
== PNM_TYPE_PGM
) || (pnm_type
== PNM_TYPE_PGMYUV
) ) {
535 if (format
== IMGFMT_YV12
) {
536 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
;
543 /* ------------------------------------------------------------------------- */
545 static int control(uint32_t request
, void *data
)
548 case VOCTRL_QUERY_FORMAT
:
549 return query_format(*((uint32_t*)data
));
550 case VOCTRL_DRAW_IMAGE
:
551 return draw_image(data
);
556 /* ------------------------------------------------------------------------- */
558 static void uninit(void)
570 /* ------------------------------------------------------------------------- */
572 static void check_events(void)
576 /* ------------------------------------------------------------------------- */
578 static void draw_osd(void)
582 /* ------------------------------------------------------------------------- */
584 static void flip_page (void)
588 /* ------------------------------------------------------------------------- */
592 #undef PNM_ASCII_MODE
595 #undef PNM_TYPE_PGMYUV
597 /* ------------------------------------------------------------------------- */