3 * Copyright (c) 2003 Charles Yates
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
26 #include "framehook.h"
29 /** Bi-directional pipe structure.
40 /** Create a bidirectional pipe for the given command.
43 rwpipe *rwpipe_open( int argc, char *argv[] )
45 rwpipe *this = av_mallocz( sizeof( rwpipe ) );
59 #define COMMAND_SIZE 10240
60 char *command = av_mallocz( COMMAND_SIZE );
63 strcpy( command, "" );
64 for ( i = 0; i < argc; i ++ )
66 pstrcat( command, COMMAND_SIZE, argv[ i ] );
67 pstrcat( command, COMMAND_SIZE, " " );
70 dup2( output[ 0 ], STDIN_FILENO );
71 dup2( input[ 1 ], STDOUT_FILENO );
78 execl("/bin/sh", "sh", "-c", command, (char*)NULL );
86 this->reader = fdopen( input[ 0 ], "r" );
87 this->writer = fdopen( output[ 1 ], "w" );
94 /** Read data from the pipe.
97 FILE *rwpipe_reader( rwpipe *this )
105 /** Write data to the pipe.
108 FILE *rwpipe_writer( rwpipe *this )
116 /* Read a number from the pipe - assumes PNM style headers.
119 int rwpipe_read_number( rwpipe *rw )
123 FILE *in = rwpipe_reader( rw );
129 while( c != EOF && !isdigit( c ) && c != '#' )
133 while( c != EOF && c != '\n' )
136 while ( c != EOF && !isdigit( c ) );
138 while( c != EOF && isdigit( c ) )
140 value = value * 10 + ( c - '0' );
147 /** Read a PPM P6 header.
150 int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
153 FILE *in = rwpipe_reader( rw );
156 fgets( line, 3, in );
157 if ( !strncmp( line, "P6", 2 ) )
159 *width = rwpipe_read_number( rw );
160 *height = rwpipe_read_number( rw );
161 max = rwpipe_read_number( rw );
162 return max != 255 || *width <= 0 || *height <= 0;
167 /** Close the pipe and process.
170 void rwpipe_close( rwpipe *this )
174 fclose( this->reader );
175 fclose( this->writer );
176 waitpid( this->pid, NULL, 0 );
181 /** Context info for this vhook - stores the pipe and image buffers.
194 /** Initialise the context info for this vhook.
197 int Configure(void **ctxp, int argc, char *argv[])
201 *ctxp = av_mallocz(sizeof(ContextInfo));
202 if ( ctxp != NULL && argc > 1 )
204 ContextInfo *info = (ContextInfo *)*ctxp;
205 info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
215 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
218 ContextInfo *ci = (ContextInfo *) ctx;
221 AVPicture *pict = picture;
226 FILE *in = rwpipe_reader( ci->rw );
227 FILE *out = rwpipe_writer( ci->rw );
229 /* Check that we have a pipe to talk to. */
230 if ( in == NULL || out == NULL )
233 /* Convert to RGB24 if necessary */
234 if ( !err && pix_fmt != PIX_FMT_RGB24 )
236 int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
238 if ( size != ci->size1 )
241 ci->buf1 = av_malloc(size);
243 err = ci->buf1 == NULL;
248 avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
249 if (img_convert(&picture1, PIX_FMT_RGB24, picture, pix_fmt, width, height) < 0)
255 /* Write out the PPM */
258 ptr = pict->data[ 0 ];
259 fprintf( out, "P6\n%d %d\n255\n", width, height );
260 for ( i = 0; !err && i < height; i ++ )
262 err = !fwrite( ptr, width * 3, 1, out );
263 ptr += pict->linesize[ 0 ];
269 /* Read the PPM returned. */
270 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);
274 if ( size != ci->size2 )
277 ci->buf2 = av_malloc(size);
279 err = ci->buf2 == NULL;
284 avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
285 ptr = picture2.data[ 0 ];
286 for ( i = 0; !err && i < out_height; i ++ )
288 err = !fread( ptr, out_width * 3, 1, in );
289 ptr += picture2.linesize[ 0 ];
294 /* Convert the returned PPM back to the input format */
297 /* Actually, this is wrong, since the out_width/out_height returned from the
298 * filter won't necessarily be the same as width and height - img_resample
299 * won't scale rgb24, so the only way out of this is to convert to something
300 * that img_resample does like [which may or may not be pix_fmt], rescale
301 * and finally convert to pix_fmt... slow, but would provide the most flexibility.
303 * Currently, we take the upper left width/height pixels from the filtered image,
304 * smaller images are going to be corrupted or cause a crash.
306 * Finally, what should we do in case of this call failing? Up to now, failures
307 * are gracefully ignored and the original image is returned - in this case, a
308 * failure may corrupt the input.
310 if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0)
316 /** Clean up the effect.
319 void Release(void *ctx)
322 ci = (ContextInfo *) ctx;
326 rwpipe_close( ci->rw );