demux_ts: Support S302M audio
[mplayer.git] / libvo / vo_pnm.c
blob9593860412060e431d943d7cd1ca9abc83acbe9f
1 /*
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 /* ------------------------------------------------------------------------- */
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() */
44 /* ------------------------------------------------------------------------- */
46 /* Defines */
48 /* Used for temporary buffers to store file- and pathnames */
49 #define BUFLENGTH 512
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], \
60 a[b+13], a[b+14]
62 /* ------------------------------------------------------------------------- */
64 /* Info */
66 static const vo_info_t info=
68 "PPM/PGM/PGMYUV file",
69 "pnm",
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
113 * output driver.
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.");
138 pnm_maxfiles = 1000;
139 pnm_outdir = strdup(".");
140 pnm_subdirs = NULL;
142 if (subopt_parse(arg, subopts) != 0) {
143 return -1;
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;
155 switch (pnm_mode) {
156 case PNM_ASCII_MODE:
157 info_message = _("ASCII mode enabled.");
158 break;
159 case PNM_RAW_MODE:
160 info_message = _("Raw mode enabled.");
161 break;
163 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);
165 switch (pnm_type) {
166 case PNM_TYPE_PPM:
167 info_message = _("Will write PPM files.");
168 break;
169 case PNM_TYPE_PGM:
170 info_message = _("Will write PGM files.");
171 break;
172 case PNM_TYPE_PGMYUV:
173 info_message = _("Will write PGMYUV files.");
174 break;
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.");
180 return 0;
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) {
200 struct stat stat_p;
202 /* Silly MING32 bug workaround */
203 #ifndef __MINGW32__
204 if ( mkdir(buf, 0755) < 0 ) {
205 #else
206 if ( mkdir(buf) < 0 ) {
207 #endif
208 switch (errno) { /* use switch in case other errors need to be caught
209 and handled in the future */
210 case EEXIST:
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."));
233 break;
235 default:
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"));
241 } /* end switch */
242 } else if ( verbose ) {
243 mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
244 buf, _("Output directory successfully created."));
245 } /* end if */
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,
260 uint32_t format)
262 char buf[BUFLENGTH];
264 if (vo_config_count > 0 ) { /* Already configured */
265 return 0;
268 /* Create outdir. */
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");
282 return 0;
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)
302 uint32_t w = mpi->w;
303 uint32_t h = mpi->h;
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];
308 uint8_t *curline;
309 uint32_t strideY = mpi->stride[0];
310 uint32_t strideU = mpi->stride[1];
311 uint32_t strideV = mpi->stride[2];
313 unsigned int i, j;
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 )
319 pnm_write_error();
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 )
323 pnm_write_error();
324 for (i=0; i<h; i++) {
325 if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
326 pnm_write_error();
328 } else if (pnm_type == PNM_TYPE_PGMYUV) {
329 if ( fprintf(outfile, "P5\n%d %d\n255\n", w, h*3/2) < 0 )
330 pnm_write_error();
331 for (i=0; i<h; i++) {
332 if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
333 pnm_write_error();
335 w = w / 2;
336 h = h / 2;
337 for (i=0; i<h; i++) {
338 if ( fwrite(planeU + i * strideU, w, 1, outfile) < 1 )
339 pnm_write_error();
340 if ( fwrite(planeV + i * strideV, w, 1, outfile) < 1 )
341 pnm_write_error();
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 )
349 pnm_write_error();
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 )
356 pnm_write_error();
357 i++;
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 )
366 pnm_write_error();
367 } else { /* PNM_TYPE_PGMYUV */
368 if ( fprintf(outfile, "P2\n%d %d\n255\n", w, h*3/2) < 0 )
369 pnm_write_error();
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();
379 while (i < w ) {
380 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
381 pnm_write_error();
382 i++;
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) {
389 w = w / 2;
390 h = h / 2;
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();
397 while (i < w ) {
398 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
399 pnm_write_error();
400 i++;
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();
409 while (i < w ) {
410 if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
411 pnm_write_error();
412 i++;
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;
438 char buf[BUFLENGTH];
439 static char subdirname[BUFLENGTH] = "";
440 FILE *outfile;
442 if (!mpi) {
443 mp_msg(MSGT_VO, MSGL_ERR, "%s: No image data supplied 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 ) {
449 framecounter = 0;
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 ) {
456 subdircounter++;
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. */
463 framenum++;
464 framecounter++;
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",
475 strerror(errno) );
476 exit_player_bad(_("Fatal error"));
479 pnm_write_pnm(outfile, mpi);
481 fclose(outfile);
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);
491 return VO_TRUE;
492 } else { /* Planar RGB */
493 return VO_FALSE;
495 } else { /* Packed */
496 if (mpi->flags & MP_IMGFLAG_YUV) { /* Packed YUV */
497 return VO_FALSE;
498 } else { /* Packed RGB */
499 pnm_write_image(mpi);
500 return VO_TRUE;
504 return VO_FALSE;
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);
512 return -1;
515 /* ------------------------------------------------------------------------- */
517 static int draw_slice(uint8_t *src[], int stride[], int w, int h,
518 int x, int y)
520 return 0;
523 /* ------------------------------------------------------------------------- */
525 static int query_format(uint32_t format)
527 /* Ensure that for PPM we get Packed RGB and for PGM(YUV) we get
528 * Planar YUV */
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;
539 return 0;
542 /* ------------------------------------------------------------------------- */
544 static int control(uint32_t request, void *data)
546 switch (request) {
547 case VOCTRL_QUERY_FORMAT:
548 return query_format(*((uint32_t*)data));
549 case VOCTRL_DRAW_IMAGE:
550 return draw_image(data);
552 return VO_NOTIMPL;
555 /* ------------------------------------------------------------------------- */
557 static void uninit(void)
559 free(pnm_subdirs);
560 pnm_subdirs = NULL;
561 free(pnm_outdir);
562 pnm_outdir = NULL;
565 /* ------------------------------------------------------------------------- */
567 static void check_events(void)
571 /* ------------------------------------------------------------------------- */
573 static void draw_osd(void)
577 /* ------------------------------------------------------------------------- */
579 static void flip_page (void)
583 /* ------------------------------------------------------------------------- */
585 #undef BUFLENGTH
586 #undef PNM_RAW_MODE
587 #undef PNM_ASCII_MODE
588 #undef PNM_TYPE_PPM
589 #undef PNM_TYPE_PGM
590 #undef PNM_TYPE_PGMYUV
592 /* ------------------------------------------------------------------------- */