mplayer.c: #include mplayer.h and access_mpcontext.h
[mplayer.git] / libvo / vo_pnm.c
blobfe5909329a4cfe801bf831c3cc728473d4357819
1 /*
2 * PPM/PGM/PGMYUV video output driver
4 * Written by Ivo van Poorten. (C) Copyright 2004, 2005.
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 /* ------------------------------------------------------------------------- */
25 /* Global Includes */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <sys/stat.h>
33 /* ------------------------------------------------------------------------- */
35 /* Local Includes */
37 #include "config.h"
38 #include "subopt-helper.h"
39 #include "mp_msg.h"
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "mplayer.h" /* for exit_player_bad() */
43 #include "help_mp.h"
45 /* ------------------------------------------------------------------------- */
47 /* Defines */
49 /* Used for temporary buffers to store file- and pathnames */
50 #define BUFLENGTH 512
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], \
61 a[b+13], a[b+14]
63 /* ------------------------------------------------------------------------- */
65 /* Info */
67 static const vo_info_t info=
69 "PPM/PGM/PGMYUV file",
70 "pnm",
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_msg(MSGT_VO, MSGL_ERR, MSGTR_ErrorWritingFile, info.short_name);
101 exit_player_bad(MSGTR_Exit_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
114 * output driver.
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 opt_t subopts[] = {
124 {"ppm", OPT_ARG_BOOL, &ppm_type, NULL, 0},
125 {"pgm", OPT_ARG_BOOL, &pgm_type, NULL, 0},
126 {"pgmyuv", OPT_ARG_BOOL, &pgmyuv_type, NULL, 0},
127 {"raw", OPT_ARG_BOOL, &raw_mode, NULL, 0},
128 {"ascii", OPT_ARG_BOOL, &ascii_mode, NULL, 0},
129 {"outdir", OPT_ARG_MSTRZ, &pnm_outdir, NULL, 0},
130 {"subdirs", OPT_ARG_MSTRZ, &pnm_subdirs, NULL, 0},
131 {"maxfiles", OPT_ARG_INT, &pnm_maxfiles, (opt_test_f)int_pos, 0},
132 {NULL, 0, NULL, NULL, 0}
134 const char *info_message = NULL;
136 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name,
137 MSGTR_VO_ParsingSuboptions);
139 pnm_maxfiles = 1000;
140 pnm_outdir = strdup(".");
141 pnm_subdirs = NULL;
143 if (subopt_parse(arg, subopts) != 0) {
144 return -1;
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;
156 switch (pnm_mode) {
157 case PNM_ASCII_MODE:
158 info_message = MSGTR_VO_PNM_ASCIIMode;
159 break;
160 case PNM_RAW_MODE:
161 info_message = MSGTR_VO_PNM_RawMode;
162 break;
164 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
166 switch (pnm_type) {
167 case PNM_TYPE_PPM:
168 info_message = MSGTR_VO_PNM_PPMType;
169 break;
170 case PNM_TYPE_PGM:
171 info_message = MSGTR_VO_PNM_PGMType;
172 break;
173 case PNM_TYPE_PGMYUV:
174 info_message = MSGTR_VO_PNM_PGMYUVType;
175 break;
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 MSGTR_VO_SuboptionsParsedOK);
181 return 0;
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) {
201 struct stat stat_p;
203 /* Silly MING32 bug workaround */
204 #ifndef __MINGW32__
205 if ( mkdir(buf, 0755) < 0 ) {
206 #else
207 if ( mkdir(buf) < 0 ) {
208 #endif
209 switch (errno) { /* use switch in case other errors need to be caught
210 and handled in the future */
211 case EEXIST:
212 if ( stat(buf, &stat_p ) < 0 ) {
213 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
214 MSGTR_VO_GenericError, strerror(errno) );
215 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
216 MSGTR_VO_UnableToAccess,buf);
217 exit_player_bad(MSGTR_Exit_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, MSGTR_VO_ExistsButNoDirectory);
222 exit_player_bad(MSGTR_Exit_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, MSGTR_VO_DirExistsButNotWritable);
227 exit_player_bad(MSGTR_Exit_error);
230 if (strcmp(buf, ".") != 0) {
231 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
232 buf, MSGTR_VO_DirExistsAndIsWritable);
234 break;
236 default:
237 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
238 MSGTR_VO_GenericError, strerror(errno) );
239 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
240 buf, MSGTR_VO_CantCreateDirectory);
241 exit_player_bad(MSGTR_Exit_error);
242 } /* end switch */
243 } else if ( verbose ) {
244 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
245 buf, MSGTR_VO_DirectoryCreateSuccess);
246 } /* end if */
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,
261 uint32_t format)
263 char buf[BUFLENGTH];
265 if (vo_config_count > 0 ) { /* Already configured */
266 return 0;
269 /* Create outdir. */
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");
283 return 0;
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)
303 uint32_t w = mpi->w;
304 uint32_t h = mpi->h;
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];
309 uint8_t *curline;
310 uint32_t strideY = mpi->stride[0];
311 uint32_t strideU = mpi->stride[1];
312 uint32_t strideV = mpi->stride[2];
314 unsigned int i, j;
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 )
320 pnm_write_error();
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 )
324 pnm_write_error();
325 for (i=0; i<h; i++) {
326 if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
327 pnm_write_error();
329 } else if (pnm_type == PNM_TYPE_PGMYUV) {
330 if ( fprintf(outfile, "P5\n%d %d\n255\n", w, h*3/2) < 0 )
331 pnm_write_error();
332 for (i=0; i<h; i++) {
333 if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
334 pnm_write_error();
336 w = w / 2;
337 h = h / 2;
338 for (i=0; i<h; i++) {
339 if ( fwrite(planeU + i * strideU, w, 1, outfile) < 1 )
340 pnm_write_error();
341 if ( fwrite(planeV + i * strideV, w, 1, outfile) < 1 )
342 pnm_write_error();
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 )
350 pnm_write_error();
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 )
357 pnm_write_error();
358 i++;
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 )
367 pnm_write_error();
368 } else { /* PNM_TYPE_PGMYUV */
369 if ( fprintf(outfile, "P2\n%d %d\n255\n", w, h*3/2) < 0 )
370 pnm_write_error();
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();
380 while (i < w ) {
381 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
382 pnm_write_error();
383 i++;
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) {
390 w = w / 2;
391 h = h / 2;
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();
398 while (i < w ) {
399 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
400 pnm_write_error();
401 i++;
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();
410 while (i < w ) {
411 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
412 pnm_write_error();
413 i++;
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;
439 char buf[BUFLENGTH];
440 static char subdirname[BUFLENGTH] = "";
441 FILE *outfile;
443 if (!mpi) {
444 mp_msg(MSGT_VO, MSGL_ERR, "%s: No image data suplied to video output driver\n", info.short_name );
445 exit_player_bad(MSGTR_Exit_error);
448 /* Start writing to new subdirectory after a certain amount of frames */
449 if ( framecounter == pnm_maxfiles ) {
450 framecounter = 0;
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 ) {
457 subdircounter++;
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. */
464 framenum++;
465 framecounter++;
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 MSGTR_VO_CantCreateFile);
474 mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n",
475 info.short_name, MSGTR_VO_GenericError,
476 strerror(errno) );
477 exit_player_bad(MSGTR_Exit_error);
480 pnm_write_pnm(outfile, mpi);
482 fclose(outfile);
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);
492 return VO_TRUE;
493 } else { /* Planar RGB */
494 return VO_FALSE;
496 } else { /* Packed */
497 if (mpi->flags & MP_IMGFLAG_YUV) { /* Packed YUV */
498 return VO_FALSE;
499 } else { /* Packed RGB */
500 pnm_write_image(mpi);
501 return VO_TRUE;
505 return VO_FALSE;
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);
513 return -1;
516 /* ------------------------------------------------------------------------- */
518 static int draw_slice(uint8_t *src[], int stride[], int w, int h,
519 int x, int y)
521 return 0;
524 /* ------------------------------------------------------------------------- */
526 static int query_format(uint32_t format)
528 /* Ensure that for PPM we get Packed RGB and for PGM(YUV) we get
529 * Planar YUV */
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;
540 return 0;
543 /* ------------------------------------------------------------------------- */
545 static int control(uint32_t request, void *data)
547 switch (request) {
548 case VOCTRL_QUERY_FORMAT:
549 return query_format(*((uint32_t*)data));
550 case VOCTRL_DRAW_IMAGE:
551 return draw_image(data);
553 return VO_NOTIMPL;
556 /* ------------------------------------------------------------------------- */
558 static void uninit(void)
560 if (pnm_subdirs) {
561 free(pnm_subdirs);
562 pnm_subdirs = NULL;
564 if (pnm_outdir) {
565 free(pnm_outdir);
566 pnm_outdir = NULL;
570 /* ------------------------------------------------------------------------- */
572 static void check_events(void)
576 /* ------------------------------------------------------------------------- */
578 static void draw_osd(void)
582 /* ------------------------------------------------------------------------- */
584 static void flip_page (void)
588 /* ------------------------------------------------------------------------- */
590 #undef BUFLENGTH
591 #undef PNM_RAW_MODE
592 #undef PNM_ASCII_MODE
593 #undef PNM_TYPE_PPM
594 #undef PNM_TYPE_PGM
595 #undef PNM_TYPE_PGMYUV
597 /* ------------------------------------------------------------------------- */