00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <unistd.h>
00024 #include <fcntl.h>
00025 #include <sys/types.h>
00026 #include <sys/wait.h>
00027 #include <ctype.h>
00028 #include "framehook.h"
00029 #include "avformat.h"
00030 #include "swscale.h"
00031 #include "avstring.h"
00032
00033 static int sws_flags = SWS_BICUBIC;
00034
00038 typedef struct rwpipe
00039 {
00040 int pid;
00041 FILE *reader;
00042 FILE *writer;
00043 }
00044 rwpipe;
00045
00049 static rwpipe *rwpipe_open( int argc, char *argv[] )
00050 {
00051 rwpipe *this = av_mallocz( sizeof( rwpipe ) );
00052
00053 if ( this != NULL )
00054 {
00055 int input[ 2 ];
00056 int output[ 2 ];
00057
00058 pipe( input );
00059 pipe( output );
00060
00061 this->pid = fork();
00062
00063 if ( this->pid == 0 )
00064 {
00065 #define COMMAND_SIZE 10240
00066 char *command = av_mallocz( COMMAND_SIZE );
00067 int i;
00068
00069 strcpy( command, "" );
00070 for ( i = 0; i < argc; i ++ )
00071 {
00072 av_strlcat( command, argv[ i ], COMMAND_SIZE );
00073 av_strlcat( command, " ", COMMAND_SIZE );
00074 }
00075
00076 dup2( output[ 0 ], STDIN_FILENO );
00077 dup2( input[ 1 ], STDOUT_FILENO );
00078
00079 close( input[ 0 ] );
00080 close( input[ 1 ] );
00081 close( output[ 0 ] );
00082 close( output[ 1 ] );
00083
00084 execl("/bin/sh", "sh", "-c", command, (char*)NULL );
00085 _exit( 255 );
00086 }
00087 else
00088 {
00089 close( input[ 1 ] );
00090 close( output[ 0 ] );
00091
00092 this->reader = fdopen( input[ 0 ], "r" );
00093 this->writer = fdopen( output[ 1 ], "w" );
00094 }
00095 }
00096
00097 return this;
00098 }
00099
00103 static FILE *rwpipe_reader( rwpipe *this )
00104 {
00105 if ( this != NULL )
00106 return this->reader;
00107 else
00108 return NULL;
00109 }
00110
00114 static FILE *rwpipe_writer( rwpipe *this )
00115 {
00116 if ( this != NULL )
00117 return this->writer;
00118 else
00119 return NULL;
00120 }
00121
00122
00123
00124
00125 static int rwpipe_read_number( rwpipe *rw )
00126 {
00127 int value = 0;
00128 int c = 0;
00129 FILE *in = rwpipe_reader( rw );
00130
00131 do
00132 {
00133 c = fgetc( in );
00134
00135 while( c != EOF && !isdigit( c ) && c != '#' )
00136 c = fgetc( in );
00137
00138 if ( c == '#' )
00139 while( c != EOF && c != '\n' )
00140 c = fgetc( in );
00141 }
00142 while ( c != EOF && !isdigit( c ) );
00143
00144 while( c != EOF && isdigit( c ) )
00145 {
00146 value = value * 10 + ( c - '0' );
00147 c = fgetc( in );
00148 }
00149
00150 return value;
00151 }
00152
00156 static int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
00157 {
00158 char line[ 3 ];
00159 FILE *in = rwpipe_reader( rw );
00160 int max;
00161
00162 fgets( line, 3, in );
00163 if ( !strncmp( line, "P6", 2 ) )
00164 {
00165 *width = rwpipe_read_number( rw );
00166 *height = rwpipe_read_number( rw );
00167 max = rwpipe_read_number( rw );
00168 return max != 255 || *width <= 0 || *height <= 0;
00169 }
00170 return 1;
00171 }
00172
00176 static void rwpipe_close( rwpipe *this )
00177 {
00178 if ( this != NULL )
00179 {
00180 fclose( this->reader );
00181 fclose( this->writer );
00182 waitpid( this->pid, NULL, 0 );
00183 av_free( this );
00184 }
00185 }
00186
00190 typedef struct
00191 {
00192 rwpipe *rw;
00193 int size1;
00194 char *buf1;
00195 int size2;
00196 char *buf2;
00197
00198
00199 struct SwsContext *toRGB_convert_ctx;
00200
00201
00202 struct SwsContext *fromRGB_convert_ctx;
00203 }
00204 ContextInfo;
00205
00209 int Configure(void **ctxp, int argc, char *argv[])
00210 {
00211 if ( argc > 1 )
00212 {
00213 *ctxp = av_mallocz(sizeof(ContextInfo));
00214 if ( ctxp != NULL && argc > 1 )
00215 {
00216 ContextInfo *info = (ContextInfo *)*ctxp;
00217 info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
00218 return 0;
00219 }
00220 }
00221 return 1;
00222 }
00223
00227 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
00228 {
00229 int err = 0;
00230 ContextInfo *ci = (ContextInfo *) ctx;
00231 AVPicture picture1;
00232 AVPicture picture2;
00233 AVPicture *pict = picture;
00234 int out_width;
00235 int out_height;
00236 int i;
00237 uint8_t *ptr = NULL;
00238 FILE *in = rwpipe_reader( ci->rw );
00239 FILE *out = rwpipe_writer( ci->rw );
00240
00241
00242 if ( in == NULL || out == NULL )
00243 err = 1;
00244
00245
00246 if ( !err && pix_fmt != PIX_FMT_RGB24 )
00247 {
00248 int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
00249
00250 if ( size != ci->size1 )
00251 {
00252 av_free( ci->buf1 );
00253 ci->buf1 = av_malloc(size);
00254 ci->size1 = size;
00255 err = ci->buf1 == NULL;
00256 }
00257
00258 if ( !err )
00259 {
00260 avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
00261
00262
00263 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
00264 width, height, pix_fmt,
00265 width, height, PIX_FMT_RGB24,
00266 sws_flags, NULL, NULL, NULL);
00267 if (ci->toRGB_convert_ctx == NULL) {
00268 av_log(NULL, AV_LOG_ERROR,
00269 "Cannot initialize the toRGB conversion context\n");
00270 return;
00271 }
00272
00273
00274
00275 sws_scale(ci->toRGB_convert_ctx,
00276 picture->data, picture->linesize, 0, height,
00277 picture1.data, picture1.linesize);
00278
00279 pict = &picture1;
00280 }
00281 }
00282
00283
00284 if ( !err )
00285 {
00286 ptr = pict->data[ 0 ];
00287 fprintf( out, "P6\n%d %d\n255\n", width, height );
00288 for ( i = 0; !err && i < height; i ++ )
00289 {
00290 err = !fwrite( ptr, width * 3, 1, out );
00291 ptr += pict->linesize[ 0 ];
00292 }
00293 if ( !err )
00294 err = fflush( out );
00295 }
00296
00297
00298 if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
00299 {
00300 int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
00301
00302 if ( size != ci->size2 )
00303 {
00304 av_free( ci->buf2 );
00305 ci->buf2 = av_malloc(size);
00306 ci->size2 = size;
00307 err = ci->buf2 == NULL;
00308 }
00309
00310 if ( !err )
00311 {
00312 avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
00313 ptr = picture2.data[ 0 ];
00314 for ( i = 0; !err && i < out_height; i ++ )
00315 {
00316 err = !fread( ptr, out_width * 3, 1, in );
00317 ptr += picture2.linesize[ 0 ];
00318 }
00319 }
00320 }
00321
00322
00323 if ( !err )
00324 {
00325
00326
00327
00328
00329 av_log(NULL, AV_LOG_DEBUG,
00330 "PPM vhook: Input dimensions: %d x %d Output dimensions: %d x %d\n",
00331 width, height, out_width, out_height);
00332 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
00333 out_width, out_height, PIX_FMT_RGB24,
00334 width, height, pix_fmt,
00335 sws_flags, NULL, NULL, NULL);
00336 if (ci->fromRGB_convert_ctx == NULL) {
00337 av_log(NULL, AV_LOG_ERROR,
00338 "Cannot initialize the fromRGB conversion context\n");
00339 return;
00340 }
00341
00342
00343
00344 sws_scale(ci->fromRGB_convert_ctx,
00345 picture2.data, picture2.linesize, 0, out_height,
00346 picture->data, picture->linesize);
00347 }
00348 }
00349
00353 void Release(void *ctx)
00354 {
00355 ContextInfo *ci;
00356 ci = (ContextInfo *) ctx;
00357
00358 if (ctx)
00359 {
00360 rwpipe_close( ci->rw );
00361 av_free( ci->buf1 );
00362 av_free( ci->buf2 );
00363 sws_freeContext(ci->toRGB_convert_ctx);
00364 sws_freeContext(ci->fromRGB_convert_ctx);
00365 av_free(ctx);
00366 }
00367 }
00368