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