summaryrefslogtreecommitdiffstats
path: root/libvo/vo_pnm.c
blob: 9593860412060e431d943d7cd1ca9abc83acbe9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 * PPM/PGM/PGMYUV video output driver
 *
 * copyright (C) 2004, 2005 Ivo van Poorten
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* ------------------------------------------------------------------------- */

/* Global Includes */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>

/* ------------------------------------------------------------------------- */

/* Local Includes */

#include "config.h"
#include "subopt-helper.h"
#include "mp_msg.h"
#include "video_out.h"
#include "video_out_internal.h"
#include "mplayer.h"			/* for exit_player_bad() */

/* ------------------------------------------------------------------------- */

/* Defines */

/* Used for temporary buffers to store file- and pathnames */
#define BUFLENGTH 512

#define PNM_ASCII_MODE 0
#define PNM_RAW_MODE 1
#define PNM_TYPE_PPM 0
#define PNM_TYPE_PGM 1
#define PNM_TYPE_PGMYUV 2

#define PNM_LINE_OF_ASCII "%03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d %03d\n"
#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], \
                        a[b+7], a[b+8], a[b+9], a[b+10], a[b+11], a[b+12], \
                        a[b+13], a[b+14]

/* ------------------------------------------------------------------------- */

/* Info */

static const vo_info_t info=
{
	"PPM/PGM/PGMYUV file",
	"pnm",
	"Ivo van Poorten (ivop@euronet.nl)",
	""
};

const LIBVO_EXTERN (pnm)

/* ------------------------------------------------------------------------- */

/* Global Variables */

int pnm_type = PNM_TYPE_PPM;
int pnm_mode = PNM_RAW_MODE;

char *pnm_outdir = NULL;
char *pnm_subdirs = NULL;
int pnm_maxfiles = 1000;
char *pnm_file_extension = NULL;

/* ------------------------------------------------------------------------- */

/** \brief An error occured while writing to a file.
 *
 * The program failed to write data to a file.
 * It displays a message and exits the player.
 *
 * \return nothing It does not return.
 */

static void pnm_write_error(void) {
    mp_tmsg(MSGT_VO, MSGL_ERR, "%s: Error writing file.\n", info.short_name);
    exit_player_bad(_("Fatal error"));
}

/* ------------------------------------------------------------------------- */

/** \brief Pre-initialisation.
 *
 * This function is called before initialising the video output driver. It
 * parses all suboptions and sets variables accordingly. If an error occurs
 * (like an option being out of range, not having any value or an unknown
 * option is stumbled upon) the player will exit.
 *
 * \param arg   A string containing all the suboptions passed to the video
 *              output driver.
 *
 * \return 0    All went well.
 */

static int preinit(const char *arg)
{
    int ppm_type = 0, pgm_type = 0, pgmyuv_type = 0,
        raw_mode = 0, ascii_mode = 0;
    const opt_t subopts[] = {
        {"ppm",         OPT_ARG_BOOL,   &ppm_type,      NULL},
        {"pgm",         OPT_ARG_BOOL,   &pgm_type,      NULL},
        {"pgmyuv",      OPT_ARG_BOOL,   &pgmyuv_type,   NULL},
        {"raw",         OPT_ARG_BOOL,   &raw_mode,      NULL},
        {"ascii",       OPT_ARG_BOOL,   &ascii_mode,    NULL},
        {"outdir",      OPT_ARG_MSTRZ,  &pnm_outdir,    NULL},
        {"subdirs",     OPT_ARG_MSTRZ,  &pnm_subdirs,   NULL},
        {"maxfiles",    OPT_ARG_INT,    &pnm_maxfiles,  int_pos},
        {NULL, 0, NULL, NULL}
    };
    const char *info_message = NULL;

    mp_msg(MSGT_VO, MSGL_V, "%s: %s\n", info.short_name,
           "Parsing suboptions.");

    pnm_maxfiles = 1000;
    pnm_outdir = strdup(".");
    pnm_subdirs = NULL;

    if (subopt_parse(arg, subopts) != 0) {
        return -1;
    }

    pnm_type     = PNM_TYPE_PPM;
    pnm_mode     = PNM_RAW_MODE;

    if (pgmyuv_type) pnm_type     = PNM_TYPE_PGMYUV;
    if (pgm_type)    pnm_type     = PNM_TYPE_PGM;
    if (ppm_type)    pnm_type     = PNM_TYPE_PPM;
    if (ascii_mode)  pnm_mode     = PNM_ASCII_MODE;
    if (raw_mode)    pnm_mode     = PNM_RAW_MODE;

    switch (pnm_mode) {
        case PNM_ASCII_MODE:
            info_message = _("ASCII mode enabled.");
            break;
        case PNM_RAW_MODE:
            info_message = _("Raw mode enabled.");
            break;
    }
    mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);

    switch (pnm_type) {
        case PNM_TYPE_PPM:
            info_message = _("Will write PPM files.");
            break;
        case PNM_TYPE_PGM:
            info_message = _("Will write PGM files.");
            break;
        case PNM_TYPE_PGMYUV:
            info_message = _("Will write PGMYUV files.");
            break;
    }
    mp_msg(MSGT_VO, MSGL_INFO, "%s: %s\n", info.short_name, info_message);

    mp_msg(MSGT_VO, MSGL_V, "%s: %s\n", info.short_name,
           "Suboptions parsed OK.");
    return 0;
}

/* ------------------------------------------------------------------------- */

/** \brief Create a directory.
 *
 *  This function creates a directory. If it already exists, it tests if
 *  it's a directory and not something else, and if it is, it tests whether
 *  the directory is writable or not.
 *
 * \param buf       Pointer to directory name.
 * \param verbose   Verbose on success. If verbose is non-zero, it will print
 *                  a message if it was successful in creating the directory.
 *
 * \return nothing  In case anything fails, the player will exit. If it
 *                  returns, everything went well.
 */

static void pnm_mkdir(char *buf, int verbose) {
    struct stat stat_p;

/* Silly MING32 bug workaround */
#ifndef __MINGW32__
    if ( mkdir(buf, 0755) < 0 ) {
#else
    if ( mkdir(buf) < 0 ) {
#endif
        switch (errno) { /* use switch in case other errors need to be caught
                            and handled in the future */
            case EEXIST:
                if ( stat(buf, &stat_p ) < 0 ) {
                    mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
                           _("This error has occurred"), strerror(errno) );
                    mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
                           _("Unable to access"), buf);
                    exit_player_bad(_("Fatal error"));
                }
                if ( !S_ISDIR(stat_p.st_mode) ) {
                    mp_msg(MSGT_VO, MSGL_ERR, "%s: %s %s\n", info.short_name,
                           buf, _("already exists, but is not a directory."));
                    exit_player_bad(_("Fatal error"));
                }
                if ( !(stat_p.st_mode & S_IWUSR) ) {
                    mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
                           buf, _("Output directory already exists, but is not writable."));
                    exit_player_bad(_("Fatal error"));
                }

                if (strcmp(buf, ".") != 0) {
                mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
                       buf, _("Output directory already exists and is writable."));
                }
                break;

            default:
                mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n", info.short_name,
                       _("This error has occurred"), strerror(errno) );
                mp_msg(MSGT_VO, MSGL_ERR, "%s: %s - %s\n", info.short_name,
                       buf, _("Unable to create output directory."));
                exit_player_bad(_("Fatal error"));
        } /* end switch */
    } else if ( verbose ) {
        mp_msg(MSGT_VO, MSGL_INFO, "%s: %s - %s\n", info.short_name,
               buf, _("Output directory successfully created."));
    } /* end if */
}

/* ------------------------------------------------------------------------- */

/** \brief Configure the video output driver.
 *
 * This functions configures the video output driver. It determines the
 * width and height of the image(s) and creates the output directory.
 *
 *  \return 0             All went well.
 */

static int config(uint32_t width, uint32_t height, uint32_t d_width,
                       uint32_t d_height, uint32_t flags, char *title,
                       uint32_t format)
{
    char buf[BUFLENGTH];

    if (vo_config_count > 0 ) { /* Already configured */
        return 0;
    }

    /* Create outdir. */

    snprintf(buf, BUFLENGTH, "%s", pnm_outdir);
    pnm_mkdir(buf, 1); /* This function only returns if creation was
                           successful. If not, the player will exit. */

    if (pnm_type == PNM_TYPE_PPM) {
        pnm_file_extension = strdup("ppm");
    } else if (pnm_type == PNM_TYPE_PGM) {
        pnm_file_extension = strdup("pgm");
    } else if (pnm_type == PNM_TYPE_PGMYUV) {
        pnm_file_extension = strdup("pgmyuv");
    }

    return 0;
}

/* ------------------------------------------------------------------------- */

/** \brief Write PNM file to output file
 *
 * This function writes PPM, PGM or PGMYUV data to an output file, depending
 * on which type was selected on the commandline. pnm_type and pnm_mode are
 * global variables. Depending on which mode was selected, it will write
 * a RAW or an ASCII file.
 *
 * \param outfile       Filedescriptor of output file.
 * \param mpi           The image to write.
 *
 * \return none         The player will exit if anything goes wrong.
 */

static void pnm_write_pnm(FILE *outfile, mp_image_t *mpi)
{
    uint32_t w = mpi->w;
    uint32_t h = mpi->h;
    uint8_t *rgbimage = mpi->planes[0];
    uint8_t *planeY = mpi->planes[0];
    uint8_t *planeU = mpi->planes[1];
    uint8_t *planeV = mpi->planes[2];
    uint8_t *curline;
    uint32_t strideY = mpi->stride[0];
    uint32_t strideU = mpi->stride[1];
    uint32_t strideV = mpi->stride[2];

    unsigned int i, j;

    if (pnm_mode == PNM_RAW_MODE) {

        if (pnm_type == PNM_TYPE_PPM) {
            if ( fprintf(outfile, "P6\n%d %d\n255\n", w, h) < 0 )
                pnm_write_error();
            if ( fwrite(rgbimage, w * 3, h, outfile) < h ) pnm_write_error();
        } else if (pnm_type == PNM_TYPE_PGM) {
            if ( fprintf(outfile, "P5\n%d %d\n255\n", w, h) < 0 )
                pnm_write_error();
            for (i=0; i<h; i++) {
                if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
                    pnm_write_error();
            }
        } else if (pnm_type == PNM_TYPE_PGMYUV) {
            if ( fprintf(outfile, "P5\n%d %d\n255\n", w, h*3/2) < 0 )
                pnm_write_error();
            for (i=0; i<h; i++) {
                if ( fwrite(planeY + i * strideY, w, 1, outfile) < 1 )
                    pnm_write_error();
            }
            w = w / 2;
            h = h / 2;
            for (i=0; i<h; i++) {
                if ( fwrite(planeU + i * strideU, w, 1, outfile) < 1 )
                    pnm_write_error();
                if ( fwrite(planeV + i * strideV, w, 1, outfile) < 1 )
                    pnm_write_error();
            }
        } /* end if pnm_type */

    } else if (pnm_mode == PNM_ASCII_MODE) {

        if (pnm_type == PNM_TYPE_PPM) {
            if ( fprintf(outfile, "P3\n%d %d\n255\n", w, h) < 0 )
                pnm_write_error();
            for (i=0; i <= w * h * 3 - 16 ; i += 15) {
                if ( fprintf(outfile, PNM_LINE_OF_ASCII,
                    PNM_LINE15(rgbimage,i) ) < 0 )  pnm_write_error();
            }
            while (i < (w * h * 3) ) {
                if ( fprintf(outfile, "%03d ", rgbimage[i]) < 0 )
                    pnm_write_error();
                i++;
            }
            if ( fputc('\n', outfile) < 0 ) pnm_write_error();
        } else if ( (pnm_type == PNM_TYPE_PGM) ||
                                            (pnm_type == PNM_TYPE_PGMYUV) ) {

            /* different header for pgm and pgmyuv. pgmyuv is 'higher' */
            if (pnm_type == PNM_TYPE_PGM) {
                if ( fprintf(outfile, "P2\n%d %d\n255\n", w, h) < 0 )
                    pnm_write_error();
            } else { /* PNM_TYPE_PGMYUV */
                if ( fprintf(outfile, "P2\n%d %d\n255\n", w, h*3/2) < 0 )
                    pnm_write_error();
            }

            /* output Y plane for both PGM and PGMYUV */
            for (j=0; j<h; j++) {
                curline = planeY + strideY * j;
                for (i=0; i <= w - 16; i+=15) {
                    if ( fprintf(outfile, PNM_LINE_OF_ASCII,
                        PNM_LINE15(curline,i) ) < 0 ) pnm_write_error();
                }
                while (i < w ) {
                    if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
                        pnm_write_error();
                    i++;
                }
                if ( fputc('\n', outfile) < 0 ) pnm_write_error();
            }

            /* also output U and V planes fpr PGMYUV */
            if (pnm_type == PNM_TYPE_PGMYUV) {
                w = w / 2;
                h = h / 2;
                for (j=0; j<h; j++) {
                    curline = planeU + strideU * j;
                    for (i=0; i<= w-16; i+=15) {
                        if ( fprintf(outfile, PNM_LINE_OF_ASCII,
                            PNM_LINE15(curline,i) ) < 0 ) pnm_write_error();
                    }
                    while (i < w ) {
                        if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
                            pnm_write_error();
                        i++;
                    }
                    if ( fputc('\n', outfile) < 0 ) pnm_write_error();

                    curline = planeV + strideV * j;
                    for (i=0; i<= w-16; i+=15) {
                        if ( fprintf(outfile, PNM_LINE_OF_ASCII,
                            PNM_LINE15(curline,i) ) < 0 ) pnm_write_error();
                    }
                    while (i < w ) {
                        if ( fprintf(outfile, "%03d ", curline[i]) < 0 )
                            pnm_write_error();
                        i++;
                    }
                    if ( fputc('\n', outfile) < 0 ) pnm_write_error();
                }
            }

        } /* end if pnm_type */
    } /* end if pnm_mode */
}

/* ------------------------------------------------------------------------- */

/** \brief Write a PNM image.
 *
 * This function gets called first if a PNM image has to be written to disk.
 * It contains the subdirectory framework and it calls pnm_write_pnm() to
 * actually write the image to disk.
 *
 * \param mpi       The image to write.
 *
 * \return none     The player will exit if anything goes wrong.
 */

static void pnm_write_image(mp_image_t *mpi)
{
    static int framenum = 0, framecounter = 0, subdircounter = 0;
    char buf[BUFLENGTH];
    static char subdirname[BUFLENGTH] = "";
    FILE *outfile;

    if (!mpi) {
        mp_msg(MSGT_VO, MSGL_ERR, "%s: No image data supplied to video output driver\n", info.short_name );
        exit_player_bad(_("Fatal error"));
    }

    /* Start writing to new subdirectory after a certain amount of frames */
    if ( framecounter == pnm_maxfiles ) {
        framecounter = 0;
    }

    /* If framecounter is zero (or reset to zero), increment subdirectory
     * number and create the subdirectory.
     * If pnm_subdirs is not set, do nothing. */
    if ( !framecounter && pnm_subdirs ) {
        subdircounter++;
        snprintf(subdirname, BUFLENGTH, "%s%08d", pnm_subdirs, subdircounter);
        snprintf(buf, BUFLENGTH, "%s/%s", pnm_outdir, subdirname);
        pnm_mkdir(buf, 0); /* This function only returns if creation was
                               successful. If not, the player will exit. */
    }

    framenum++;
    framecounter++;

    /* snprintf the full pathname of the outputfile */
    snprintf(buf, BUFLENGTH, "%s/%s/%08d.%s", pnm_outdir, subdirname,
                                            framenum, pnm_file_extension);

    if ( (outfile = fopen(buf, "wb") ) == NULL ) {
        mp_msg(MSGT_VO, MSGL_ERR, "\n%s: %s\n", info.short_name,
                "Unable to create output file.");
        mp_msg(MSGT_VO, MSGL_ERR, "%s: %s: %s\n",
                info.short_name, "This error has occurred",
                strerror(errno) );
        exit_player_bad(_("Fatal error"));
    }

    pnm_write_pnm(outfile, mpi);

    fclose(outfile);
}

/* ------------------------------------------------------------------------- */

static uint32_t draw_image(mp_image_t *mpi)
{
    if (mpi->flags & MP_IMGFLAG_PLANAR) { /* Planar */
        if (mpi->flags & MP_IMGFLAG_YUV) { /* Planar YUV */
            pnm_write_image(mpi);
            return VO_TRUE;
        } else { /* Planar RGB */
            return VO_FALSE;
        }
    } else { /* Packed */
        if (mpi->flags & MP_IMGFLAG_YUV) { /* Packed YUV */
            return VO_FALSE;
        } else { /* Packed RGB */
            pnm_write_image(mpi);
            return VO_TRUE;
        }
    }

    return VO_FALSE;
}

/* ------------------------------------------------------------------------- */

static int draw_frame(uint8_t *src[])
{
    mp_msg(MSGT_VO, MSGL_V, "%s: draw_frame() is called!\n", info.short_name);
    return -1;
}

/* ------------------------------------------------------------------------- */

static int draw_slice(uint8_t *src[], int stride[], int w, int h,
                           int x, int y)
{
    return 0;
}

/* ------------------------------------------------------------------------- */

static int query_format(uint32_t format)
{
    /* Ensure that for PPM we get Packed RGB and for PGM(YUV) we get
     * Planar YUV */
    if (pnm_type == PNM_TYPE_PPM) {
        if (format == IMGFMT_RGB24) {
            return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
        }
    } else if ( (pnm_type == PNM_TYPE_PGM) || (pnm_type == PNM_TYPE_PGMYUV) ) {
        if (format == IMGFMT_YV12) {
            return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
        }
    }

    return 0;
}

/* ------------------------------------------------------------------------- */

static int control(uint32_t request, void *data)
{
    switch (request) {
        case VOCTRL_QUERY_FORMAT:
            return query_format(*((uint32_t*)data));
        case VOCTRL_DRAW_IMAGE:
            return draw_image(data);
    }
    return VO_NOTIMPL;
}

/* ------------------------------------------------------------------------- */

static void uninit(void)
{
    free(pnm_subdirs);
    pnm_subdirs = NULL;
    free(pnm_outdir);
    pnm_outdir = NULL;
}

/* ------------------------------------------------------------------------- */

static void check_events(void)
{
}

/* ------------------------------------------------------------------------- */

static void draw_osd(void)
{
}

/* ------------------------------------------------------------------------- */

static void flip_page (void)
{
}

/* ------------------------------------------------------------------------- */

#undef BUFLENGTH
#undef PNM_RAW_MODE
#undef PNM_ASCII_MODE
#undef PNM_TYPE_PPM
#undef PNM_TYPE_PGM
#undef PNM_TYPE_PGMYUV

/* ------------------------------------------------------------------------- */