Comment out the correct #endif directive.
[mplayer/greg.git] / libvo / vo_pnm.c
blobdf1a070b5aa23ca1a5b009e97832c1fa756c0adb
1 /* ------------------------------------------------------------------------- */
3 /*
4 * vo_pnm.c, PPM/PGM/PGMYUV Video Output Driver for MPlayer
6 * Written by Ivo van Poorten. (C) Copyright 2004, 2005.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program 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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 /* ------------------------------------------------------------------------- */
26 /* Global Includes */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/stat.h>
34 /* ------------------------------------------------------------------------- */
36 /* Local Includes */
38 #include "config.h"
39 #include "subopt-helper.h"
40 #include "mp_msg.h"
41 #include "video_out.h"
42 #include "video_out_internal.h"
43 #include "mplayer.h" /* for exit_player() */
44 #include "help_mp.h"
46 /* ------------------------------------------------------------------------- */
48 /* Defines */
50 /* Used for temporary buffers to store file- and pathnames */
51 #define BUFLENGTH 512
53 #define PNM_ASCII_MODE 0
54 #define PNM_RAW_MODE 1
55 #define PNM_TYPE_PPM 0
56 #define PNM_TYPE_PGM 1
57 #define PNM_TYPE_PGMYUV 2
59 #define PNM_LINE_OF_ASCII "%03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d\n"
60 #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], \
61 a[b+7], a[b+8], a[b+9], a[b+10], a[b+11], a[b+12], \
62 a[b+13], a[b+14]
64 /* ------------------------------------------------------------------------- */
66 /* Info */
68 static const vo_info_t info=
70 "PPM/PGM/PGMYUV file",
71 "pnm",
72 "Ivo van Poorten (ivop@euronet.nl)",
76 const LIBVO_EXTERN (pnm)
78 /* ------------------------------------------------------------------------- */
80 /* Global Variables */
82 int pnm_type = PNM_TYPE_PPM;
83 int pnm_mode = PNM_RAW_MODE;
85 char *pnm_outdir = NULL;
86 char *pnm_subdirs = NULL;
87 int pnm_maxfiles = 1000;
88 char *pnm_file_extension = NULL;
90 /* ------------------------------------------------------------------------- */
92 /** \brief An error occured while writing to a file.
94 * The program failed to write data to a file.
95 * It displays a message and exits the player.
97 * \return nothing It does not return.
100 static void pnm_write_error(void) {
101 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_ErrorWritingFile, info.short_name);
102 exit_player(MSGTR_Exit_error);
105 /* ------------------------------------------------------------------------- */
107 /** \brief Pre-initialisation.
109 * This function is called before initialising the video output driver. It
110 * parses all suboptions and sets variables accordingly. If an error occurs
111 * (like an option being out of range, not having any value or an unknown
112 * option is stumbled upon) the player will exit.
114 * \param arg A string containing all the suboptions passed to the video
115 * output driver.
117 * \return 0 All went well.
120 static int preinit(const char *arg)
122 int ppm_type = 0, pgm_type = 0, pgmyuv_type = 0,
123 raw_mode = 0, ascii_mode = 0;
124 opt_t subopts[] = {
125 {"ppm", OPT_ARG_BOOL, &ppm_type, NULL, 0},
126 {"pgm", OPT_ARG_BOOL, &pgm_type, NULL, 0},
127 {"pgmyuv", OPT_ARG_BOOL, &pgmyuv_type, NULL, 0},
128 {"raw", OPT_ARG_BOOL, &raw_mode, NULL, 0},
129 {"ascii", OPT_ARG_BOOL, &ascii_mode, NULL, 0},
130 {"outdir", OPT_ARG_MSTRZ, &pnm_outdir, NULL, 0},
131 {"subdirs", OPT_ARG_MSTRZ, &pnm_subdirs, NULL, 0},
132 {"maxfiles", OPT_ARG_INT, &pnm_maxfiles, (opt_test_f)int_pos, 0},
133 {NULL, 0, NULL, NULL, 0}
135 const char *info_message = NULL;
137 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
138 MSGTR_VO_ParsingSuboptions);
140 pnm_maxfiles = 1000;
141 pnm_outdir = strdup(".");
142 pnm_subdirs = NULL;
144 if (subopt_parse(arg, subopts) != 0) {
145 return -1;
148 pnm_type = PNM_TYPE_PPM;
149 pnm_mode = PNM_RAW_MODE;
151 if (pgmyuv_type) pnm_type = PNM_TYPE_PGMYUV;
152 if (pgm_type) pnm_type = PNM_TYPE_PGM;
153 if (ppm_type) pnm_type = PNM_TYPE_PPM;
154 if (ascii_mode) pnm_mode = PNM_ASCII_MODE;
155 if (raw_mode) pnm_mode = PNM_RAW_MODE;
157 switch (pnm_mode) {
158 case PNM_ASCII_MODE:
159 info_message = MSGTR_VO_PNM_ASCIIMode;
160 break;
161 case PNM_RAW_MODE:
162 info_message = MSGTR_VO_PNM_RawMode;
163 break;
165 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
167 switch (pnm_type) {
168 case PNM_TYPE_PPM:
169 info_message = MSGTR_VO_PNM_PPMType;
170 break;
171 case PNM_TYPE_PGM:
172 info_message = MSGTR_VO_PNM_PGMType;
173 break;
174 case PNM_TYPE_PGMYUV:
175 info_message = MSGTR_VO_PNM_PGMYUVType;
176 break;
178 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
180 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
181 MSGTR_VO_SuboptionsParsedOK);
182 return 0;
185 /* ------------------------------------------------------------------------- */
187 /** \brief Create a directory.
189 * This function creates a directory. If it already exists, it tests if
190 * it's a directory and not something else, and if it is, it tests whether
191 * the directory is writable or not.
193 * \param buf Pointer to directory name.
194 * \param verbose Verbose on success. If verbose is non-zero, it will print
195 * a message if it was successful in creating the directory.
197 * \return nothing In case anything fails, the player will exit. If it
198 * returns, everything went well.
201 static void pnm_mkdir(char *buf, int verbose) {
202 struct stat stat_p;
204 /* Silly MING32 bug workaround */
205 #ifndef __MINGW32__
206 if ( mkdir(buf, 0755) < 0 ) {
207 #else
208 if ( mkdir(buf) < 0 ) {
209 #endif
210 switch (errno) { /* use switch in case other errors need to be caught
211 and handled in the future */
212 case EEXIST:
213 if ( stat(buf, &stat_p ) < 0 ) {
214 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
215 MSGTR_VO_GenericError, strerror(errno) );
216 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
217 MSGTR_VO_UnableToAccess,buf);
218 exit_player(MSGTR_Exit_error);
220 if ( !S_ISDIR(stat_p.st_mode) ) {
221 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
222 buf, MSGTR_VO_ExistsButNoDirectory);
223 exit_player(MSGTR_Exit_error);
225 if ( !(stat_p.st_mode & S_IWUSR) ) {
226 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
227 buf, MSGTR_VO_DirExistsButNotWritable);
228 exit_player(MSGTR_Exit_error);
231 if (strcmp(buf, ".") != 0) {
232 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
233 buf, MSGTR_VO_DirExistsAndIsWritable);
235 break;
237 default:
238 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
239 MSGTR_VO_GenericError, strerror(errno) );
240 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
241 buf, MSGTR_VO_CantCreateDirectory);
242 exit_player(MSGTR_Exit_error);
243 } /* end switch */
244 } else if ( verbose ) {
245 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
246 buf, MSGTR_VO_DirectoryCreateSuccess);
247 } /* end if */
250 /* ------------------------------------------------------------------------- */
252 /** \brief Configure the video output driver.
254 * This functions configures the video output driver. It determines the
255 * width and height of the image(s) and creates the output directory.
257 * \return 0 All went well.
260 static int config(uint32_t width, uint32_t height, uint32_t d_width,
261 uint32_t d_height, uint32_t flags, char *title,
262 uint32_t format)
264 char buf[BUFLENGTH];
266 if (vo_config_count > 0 ) { /* Already configured */
267 return 0;
270 /* Create outdir. */
272 snprintf(buf, BUFLENGTH, "%s", pnm_outdir);
273 pnm_mkdir(buf, 1); /* This function only returns if creation was
274 successful. If not, the player will exit. */
276 if (pnm_type == PNM_TYPE_PPM) {
277 pnm_file_extension = strdup("ppm");
278 } else if (pnm_type == PNM_TYPE_PGM) {
279 pnm_file_extension = strdup("pgm");
280 } else if (pnm_type == PNM_TYPE_PGMYUV) {
281 pnm_file_extension = strdup("pgmyuv");
284 return 0;
287 /* ------------------------------------------------------------------------- */
289 /** \brief Write PNM file to output file
291 * This function writes PPM, PGM or PGMYUV data to an output file, depending
292 * on which type was selected on the commandline. pnm_type and pnm_mode are
293 * global variables. Depending on which mode was selected, it will write
294 * a RAW or an ASCII file.
296 * \param outfile Filedescriptor of output file.
297 * \param mpi The image to write.
299 * \return none The player will exit if anything goes wrong.
302 static void pnm_write_pnm(FILE *outfile, mp_image_t *mpi)
304 uint32_t w = mpi->w;
305 uint32_t h = mpi->h;
306 uint8_t *rgbimage = mpi->planes[0];
307 uint8_t *planeY = mpi->planes[0];
308 uint8_t *planeU = mpi->planes[1];
309 uint8_t *planeV = mpi->planes[2];
310 uint8_t *curline;
311 uint32_t strideY = mpi->stride[0];
312 uint32_t strideU = mpi->stride[1];
313 uint32_t strideV = mpi->stride[2];
315 unsigned int i, j;
317 if (pnm_mode == PNM_RAW_MODE) {
319 if (pnm_type == PNM_TYPE_PPM) {
320 if ( fprintf(outfile, "P6\n%d %d\n255\n", w, h) < 0 )
321 pnm_write_error();
322 if ( fwrite(rgbimage, w * 3, h, outfile) < h ) pnm_write_error();
323 } else if (pnm_type == PNM_TYPE_PGM) {
324 if ( fprintf(outfile, "P5\n%d %d\n255\n", w, h) < 0 )
325 pnm_write_error();
326 for (i=0; i<h; i++) {
327 if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
328 pnm_write_error();
330 } else if (pnm_type == PNM_TYPE_PGMYUV) {
331 if ( fprintf(outfile, "P5\n%d %d\n255\n", w, h*3/2) < 0 )
332 pnm_write_error();
333 for (i=0; i<h; i++) {
334 if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
335 pnm_write_error();
337 w = w / 2;
338 h = h / 2;
339 for (i=0; i<h; i++) {
340 if ( fwrite(planeU + i * strideU, w, 1, outfile) < 1 )
341 pnm_write_error();
342 if ( fwrite(planeV + i * strideV, w, 1, outfile) < 1 )
343 pnm_write_error();
345 } /* end if pnm_type */
347 } else if (pnm_mode == PNM_ASCII_MODE) {
349 if (pnm_type == PNM_TYPE_PPM) {
350 if ( fprintf(outfile, "P3\n%d %d\n255\n", w, h) < 0 )
351 pnm_write_error();
352 for (i=0; i <= w * h * 3 - 16 ; i += 15) {
353 if ( fprintf(outfile, PNM_LINE_OF_ASCII,
354 PNM_LINE15(rgbimage,i) ) < 0 ) pnm_write_error();
356 while (i < (w * h * 3) ) {
357 if ( fprintf(outfile, "%03d ", rgbimage[i]) < 0 )
358 pnm_write_error();
359 i++;
361 if ( fputc('\n', outfile) < 0 ) pnm_write_error();
362 } else if ( (pnm_type == PNM_TYPE_PGM) ||
363 (pnm_type == PNM_TYPE_PGMYUV) ) {
365 /* different header for pgm and pgmyuv. pgmyuv is 'higher' */
366 if (pnm_type == PNM_TYPE_PGM) {
367 if ( fprintf(outfile, "P2\n%d %d\n255\n", w, h) < 0 )
368 pnm_write_error();
369 } else { /* PNM_TYPE_PGMYUV */
370 if ( fprintf(outfile, "P2\n%d %d\n255\n", w, h*3/2) < 0 )
371 pnm_write_error();
374 /* output Y plane for both PGM and PGMYUV */
375 for (j=0; j<h; j++) {
376 curline = planeY + strideY * j;
377 for (i=0; i <= w - 16; i+=15) {
378 if ( fprintf(outfile, PNM_LINE_OF_ASCII,
379 PNM_LINE15(curline,i) ) < 0 ) pnm_write_error();
381 while (i < w ) {
382 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
383 pnm_write_error();
384 i++;
386 if ( fputc('\n', outfile) < 0 ) pnm_write_error();
389 /* also output U and V planes fpr PGMYUV */
390 if (pnm_type == PNM_TYPE_PGMYUV) {
391 w = w / 2;
392 h = h / 2;
393 for (j=0; j<h; j++) {
394 curline = planeU + strideU * j;
395 for (i=0; i<= w-16; i+=15) {
396 if ( fprintf(outfile, PNM_LINE_OF_ASCII,
397 PNM_LINE15(curline,i) ) < 0 ) pnm_write_error();
399 while (i < w ) {
400 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
401 pnm_write_error();
402 i++;
404 if ( fputc('\n', outfile) < 0 ) pnm_write_error();
406 curline = planeV + strideV * j;
407 for (i=0; i<= w-16; i+=15) {
408 if ( fprintf(outfile, PNM_LINE_OF_ASCII,
409 PNM_LINE15(curline,i) ) < 0 ) pnm_write_error();
411 while (i < w ) {
412 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
413 pnm_write_error();
414 i++;
416 if ( fputc('\n', outfile) < 0 ) pnm_write_error();
420 } /* end if pnm_type */
421 } /* end if pnm_mode */
424 /* ------------------------------------------------------------------------- */
426 /** \brief Write a PNM image.
428 * This function gets called first if a PNM image has to be written to disk.
429 * It contains the subdirectory framework and it calls pnm_write_pnm() to
430 * actually write the image to disk.
432 * \param mpi The image to write.
434 * \return none The player will exit if anything goes wrong.
437 static void pnm_write_image(mp_image_t *mpi)
439 static int framenum = 0, framecounter = 0, subdircounter = 0;
440 char buf[BUFLENGTH];
441 static char subdirname[BUFLENGTH] = "";
442 FILE *outfile;
444 if (!mpi) {
445 mp_msg(MSGT_VO, MSGL_ERR, "%s: No image data suplied to video output driver\n", info.short_name );
446 exit_player(MSGTR_Exit_error);
449 /* Start writing to new subdirectory after a certain amount of frames */
450 if ( framecounter == pnm_maxfiles ) {
451 framecounter = 0;
454 /* If framecounter is zero (or reset to zero), increment subdirectory
455 * number and create the subdirectory.
456 * If pnm_subdirs is not set, do nothing. */
457 if ( !framecounter && pnm_subdirs ) {
458 subdircounter++;
459 snprintf(subdirname, BUFLENGTH, "%s%08d", pnm_subdirs, subdircounter);
460 snprintf(buf, BUFLENGTH, "%s/%s", pnm_outdir, subdirname);
461 pnm_mkdir(buf, 0); /* This function only returns if creation was
462 successful. If not, the player will exit. */
465 framenum++;
466 framecounter++;
468 /* snprintf the full pathname of the outputfile */
469 snprintf(buf, BUFLENGTH, "%s/%s/%08d.%s", pnm_outdir, subdirname,
470 framenum, pnm_file_extension);
472 if ( (outfile = fopen(buf, "wb") ) == NULL ) {
473 mp_msg(MSGT_VO, MSGL_ERR, "\n%s: %s\n", info.short_name,
474 MSGTR_VO_CantCreateFile);
475 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n",
476 info.short_name, MSGTR_VO_GenericError,
477 strerror(errno) );
478 exit_player(MSGTR_Exit_error);
481 pnm_write_pnm(outfile, mpi);
483 fclose(outfile);
486 /* ------------------------------------------------------------------------- */
488 static uint32_t draw_image(mp_image_t *mpi)
490 if (mpi->flags & MP_IMGFLAG_PLANAR) { /* Planar */
491 if (mpi->flags & MP_IMGFLAG_YUV) { /* Planar YUV */
492 pnm_write_image(mpi);
493 return VO_TRUE;
494 } else { /* Planar RGB */
495 return VO_FALSE;
497 } else { /* Packed */
498 if (mpi->flags & MP_IMGFLAG_YUV) { /* Packed YUV */
499 return VO_FALSE;
500 } else { /* Packed RGB */
501 pnm_write_image(mpi);
502 return VO_TRUE;
506 return VO_FALSE;
509 /* ------------------------------------------------------------------------- */
511 static int draw_frame(uint8_t *src[])
513 mp_msg(MSGT_VO, MSGL_V, "%s: draw_frame() is called!\n", info.short_name);
514 return -1;
517 /* ------------------------------------------------------------------------- */
519 static int draw_slice(uint8_t *src[], int stride[], int w, int h,
520 int x, int y)
522 return 0;
525 /* ------------------------------------------------------------------------- */
527 static int query_format(uint32_t format)
529 /* Ensure that for PPM we get Packed RGB and for PGM(YUV) we get
530 * Planar YUV */
531 if (pnm_type == PNM_TYPE_PPM) {
532 if (format == IMGFMT_RGB24) {
533 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
535 } else if ( (pnm_type == PNM_TYPE_PGM) || (pnm_type == PNM_TYPE_PGMYUV) ) {
536 if (format == IMGFMT_YV12) {
537 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
541 return 0;
544 /* ------------------------------------------------------------------------- */
546 static int control(uint32_t request, void *data, ...)
548 switch (request) {
549 case VOCTRL_QUERY_FORMAT:
550 return query_format(*((uint32_t*)data));
551 case VOCTRL_DRAW_IMAGE:
552 return draw_image(data);
554 return VO_NOTIMPL;
557 /* ------------------------------------------------------------------------- */
559 static void uninit(void)
561 if (pnm_subdirs) {
562 free(pnm_subdirs);
563 pnm_subdirs = NULL;
565 if (pnm_outdir) {
566 free(pnm_outdir);
567 pnm_outdir = NULL;
571 /* ------------------------------------------------------------------------- */
573 static void check_events(void)
577 /* ------------------------------------------------------------------------- */
579 static void draw_osd(void)
583 /* ------------------------------------------------------------------------- */
585 static void flip_page (void)
589 /* ------------------------------------------------------------------------- */
591 #undef BUFLENGTH
592 #undef PNM_RAW_MODE
593 #undef PNM_ASCII_MODE
594 #undef PNM_TYPE_PPM
595 #undef PNM_TYPE_PGM
596 #undef PNM_TYPE_PGMYUV
598 /* ------------------------------------------------------------------------- */