ppm.c 7.87 KB
Newer Older
1
/*
2
 * PPM Video Hook
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Copyright (c) 2003 Charles Yates
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include "framehook.h"
27
#include "avformat.h"
28 29 30 31 32 33

/** Bi-directional pipe structure.
*/

typedef struct rwpipe
{
34 35 36
    int pid;
    FILE *reader;
    FILE *writer;
37 38 39 40 41 42 43 44
}
rwpipe;

/** Create a bidirectional pipe for the given command.
*/

rwpipe *rwpipe_open( int argc, char *argv[] )
{
45
    rwpipe *this = av_mallocz( sizeof( rwpipe ) );
46

47 48
    if ( this != NULL )
    {
49 50 51 52 53 54 55 56 57 58
        int input[ 2 ];
        int output[ 2 ];

        pipe( input );
        pipe( output );

        this->pid = fork();

        if ( this->pid == 0 )
        {
Michael Niedermayer's avatar
Michael Niedermayer committed
59 60
#define COMMAND_SIZE 10240
            char *command = av_mallocz( COMMAND_SIZE );
61
            int i;
62

63 64 65
            strcpy( command, "" );
            for ( i = 0; i < argc; i ++ )
            {
Michael Niedermayer's avatar
Michael Niedermayer committed
66 67
                pstrcat( command, COMMAND_SIZE, argv[ i ] );
                pstrcat( command, COMMAND_SIZE, " " );
68
            }
69 70 71 72 73 74 75 76 77

            dup2( output[ 0 ], STDIN_FILENO );
            dup2( input[ 1 ], STDOUT_FILENO );

            close( input[ 0 ] );
            close( input[ 1 ] );
            close( output[ 0 ] );
            close( output[ 1 ] );

78
            execl("/bin/sh", "sh", "-c", command, (char*)NULL );
79
            exit( 255 );
80 81 82 83 84 85 86 87 88
        }
        else
        {
            close( input[ 1 ] );
            close( output[ 0 ] );

            this->reader = fdopen( input[ 0 ], "r" );
            this->writer = fdopen( output[ 1 ], "w" );
        }
89
    }
90

91
    return this;
92 93 94 95 96 97 98
}

/** Read data from the pipe.
*/

FILE *rwpipe_reader( rwpipe *this )
{
99 100 101 102
    if ( this != NULL )
        return this->reader;
    else
        return NULL;
103 104 105 106 107 108 109
}

/** Write data to the pipe.
*/

FILE *rwpipe_writer( rwpipe *this )
{
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    if ( this != NULL )
        return this->writer;
    else
        return NULL;
}

/* Read a number from the pipe - assumes PNM style headers.
*/

int rwpipe_read_number( rwpipe *rw )
{
    int value = 0;
    int c = 0;
    FILE *in = rwpipe_reader( rw );

125
    do
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
    {
        c = fgetc( in );

        while( c != EOF && !isdigit( c ) && c != '#' )
            c = fgetc( in );

        if ( c == '#' )
            while( c != EOF && c != '\n' )
                c = fgetc( in );
    }
    while ( c != EOF && !isdigit( c ) );

    while( c != EOF && isdigit( c ) )
    {
        value = value * 10 + ( c - '0' );
        c = fgetc( in );
    }

    return value;
}

/** Read a PPM P6 header.
*/

int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
{
    char line[ 3 ];
    FILE *in = rwpipe_reader( rw );
    int max;

    fgets( line, 3, in );
    if ( !strncmp( line, "P6", 2 ) )
    {
        *width = rwpipe_read_number( rw );
        *height = rwpipe_read_number( rw );
        max = rwpipe_read_number( rw );
        return max != 255 || *width <= 0 || *height <= 0;
    }
    return 1;
165 166 167 168 169 170 171
}

/** Close the pipe and process.
*/

void rwpipe_close( rwpipe *this )
{
172 173 174 175 176 177 178
    if ( this != NULL )
    {
        fclose( this->reader );
        fclose( this->writer );
        waitpid( this->pid, NULL, 0 );
        av_free( this );
    }
179 180
}

181 182 183
/** Context info for this vhook - stores the pipe and image buffers.
*/

184
typedef struct
185
{
186 187 188 189 190
    rwpipe *rw;
    int size1;
    char *buf1;
    int size2;
    char *buf2;
191
}
192 193
ContextInfo;

194 195
/** Initialise the context info for this vhook.
*/
196

197
int Configure(void **ctxp, int argc, char *argv[])
198
{
199 200 201 202 203 204 205 206 207 208 209
    if ( argc > 1 )
    {
        *ctxp = av_mallocz(sizeof(ContextInfo));
        if ( ctxp != NULL && argc > 1 )
        {
            ContextInfo *info = (ContextInfo *)*ctxp;
            info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
            return 0;
        }
    }
    return 1;
210 211
}

212 213
/** Process a frame.
*/
214 215 216

void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
{
217
    int err = 0;
218 219 220 221
    ContextInfo *ci = (ContextInfo *) ctx;
    AVPicture picture1;
    AVPicture picture2;
    AVPicture *pict = picture;
222 223 224 225 226 227 228 229 230 231 232 233
    int out_width;
    int out_height;
    int i;
    uint8_t *ptr = NULL;
    FILE *in = rwpipe_reader( ci->rw );
    FILE *out = rwpipe_writer( ci->rw );

    /* Check that we have a pipe to talk to. */
    if ( in == NULL || out == NULL )
        err = 1;

    /* Convert to RGB24 if necessary */
234
    if ( !err && pix_fmt != PIX_FMT_RGB24 )
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
    {
        int size = avpicture_get_size(PIX_FMT_RGB24, width, height);

        if ( size != ci->size1 )
        {
            av_free( ci->buf1 );
            ci->buf1 = av_malloc(size);
            ci->size1 = size;
            err = ci->buf1 == NULL;
        }

        if ( !err )
        {
            avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
            if (img_convert(&picture1, PIX_FMT_RGB24, picture, pix_fmt, width, height) < 0)
                err = 1;
            pict = &picture1;
        }
    }

    /* Write out the PPM */
    if ( !err )
    {
        ptr = pict->data[ 0 ];
        fprintf( out, "P6\n%d %d\n255\n", width, height );
        for ( i = 0; !err && i < height; i ++ )
        {
            err = !fwrite( ptr, width * 3, 1, out );
            ptr += pict->linesize[ 0 ];
264
        }
265 266
        if ( !err )
            err = fflush( out );
267 268
    }

269 270 271
    /* Read the PPM returned. */
    if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
    {
272
        int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

        if ( size != ci->size2 )
        {
            av_free( ci->buf2 );
            ci->buf2 = av_malloc(size);
            ci->size2 = size;
            err = ci->buf2 == NULL;
        }

        if ( !err )
        {
            avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
            ptr = picture2.data[ 0 ];
            for ( i = 0; !err && i < out_height; i ++ )
            {
                err = !fread( ptr, out_width * 3, 1, in );
                ptr += picture2.linesize[ 0 ];
            }
291
        }
292
    }
293

294 295 296 297
    /* Convert the returned PPM back to the input format */
    if ( !err )
    {
        /* Actually, this is wrong, since the out_width/out_height returned from the
298 299 300
         * filter won't necessarily be the same as width and height - img_resample
         * won't scale rgb24, so the only way out of this is to convert to something
         * that img_resample does like [which may or may not be pix_fmt], rescale
301 302 303 304 305 306 307 308 309
         * and finally convert to pix_fmt... slow, but would provide the most flexibility.
         *
         * Currently, we take the upper left width/height pixels from the filtered image,
         * smaller images are going to be corrupted or cause a crash.
         *
         * Finally, what should we do in case of this call failing? Up to now, failures
         * are gracefully ignored and the original image is returned - in this case, a
         * failure may corrupt the input.
         */
310
        if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0)
311 312 313
        {
        }
    }
314 315
}

316 317 318
/** Clean up the effect.
*/

319 320 321 322 323 324
void Release(void *ctx)
{
    ContextInfo *ci;
    ci = (ContextInfo *) ctx;

    if (ctx)
325 326 327 328
    {
        rwpipe_close( ci->rw );
        av_free( ci->buf1 );
        av_free( ci->buf2 );
329
        av_free(ctx);
330
    }
331 332
}