2003-09-03 17:20:33 +03:00
|
|
|
/*
|
2005-12-17 20:14:38 +02:00
|
|
|
* PPM Video Hook
|
2003-09-03 17:20:33 +03:00
|
|
|
* Copyright (c) 2003 Charles Yates
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-09-03 17:20:33 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-09-03 17:20:33 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-09-03 17:20:33 +03:00
|
|
|
* 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
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-09-03 17:20:33 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "framehook.h"
|
2005-02-25 00:07:10 +02:00
|
|
|
#include "avformat.h"
|
2006-09-03 22:25:06 +03:00
|
|
|
#include "swscale.h"
|
2007-06-24 15:23:34 +03:00
|
|
|
#include "avstring.h"
|
2006-09-03 22:25:06 +03:00
|
|
|
|
|
|
|
static int sws_flags = SWS_BICUBIC;
|
2003-09-03 17:20:33 +03:00
|
|
|
|
|
|
|
/** Bi-directional pipe structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct rwpipe
|
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
int pid;
|
|
|
|
FILE *reader;
|
|
|
|
FILE *writer;
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
rwpipe;
|
|
|
|
|
|
|
|
/** Create a bidirectional pipe for the given command.
|
|
|
|
*/
|
|
|
|
|
2006-01-31 01:33:19 +02:00
|
|
|
static rwpipe *rwpipe_open( int argc, char *argv[] )
|
2003-09-03 17:20:33 +03:00
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
rwpipe *this = av_mallocz( sizeof( rwpipe ) );
|
2003-09-03 17:20:33 +03:00
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
if ( this != NULL )
|
|
|
|
{
|
2003-09-03 17:20:33 +03:00
|
|
|
int input[ 2 ];
|
|
|
|
int output[ 2 ];
|
|
|
|
|
|
|
|
pipe( input );
|
|
|
|
pipe( output );
|
|
|
|
|
|
|
|
this->pid = fork();
|
|
|
|
|
|
|
|
if ( this->pid == 0 )
|
|
|
|
{
|
2005-01-12 03:44:02 +02:00
|
|
|
#define COMMAND_SIZE 10240
|
|
|
|
char *command = av_mallocz( COMMAND_SIZE );
|
2003-09-28 23:58:57 +03:00
|
|
|
int i;
|
2003-09-03 17:20:33 +03:00
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
strcpy( command, "" );
|
|
|
|
for ( i = 0; i < argc; i ++ )
|
|
|
|
{
|
2007-06-24 15:23:34 +03:00
|
|
|
av_strlcat( command, argv[ i ], COMMAND_SIZE );
|
|
|
|
av_strlcat( command, " ", COMMAND_SIZE );
|
2003-09-28 23:58:57 +03:00
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
|
|
|
|
dup2( output[ 0 ], STDIN_FILENO );
|
|
|
|
dup2( input[ 1 ], STDOUT_FILENO );
|
|
|
|
|
|
|
|
close( input[ 0 ] );
|
|
|
|
close( input[ 1 ] );
|
|
|
|
close( output[ 0 ] );
|
|
|
|
close( output[ 1 ] );
|
|
|
|
|
2005-05-10 11:53:07 +03:00
|
|
|
execl("/bin/sh", "sh", "-c", command, (char*)NULL );
|
2007-04-01 20:20:23 +03:00
|
|
|
_exit( 255 );
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close( input[ 1 ] );
|
|
|
|
close( output[ 0 ] );
|
|
|
|
|
|
|
|
this->reader = fdopen( input[ 0 ], "r" );
|
|
|
|
this->writer = fdopen( output[ 1 ], "w" );
|
|
|
|
}
|
2003-09-28 23:58:57 +03:00
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
return this;
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Read data from the pipe.
|
|
|
|
*/
|
|
|
|
|
2006-01-31 01:33:19 +02:00
|
|
|
static FILE *rwpipe_reader( rwpipe *this )
|
2003-09-03 17:20:33 +03:00
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
if ( this != NULL )
|
|
|
|
return this->reader;
|
|
|
|
else
|
|
|
|
return NULL;
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Write data to the pipe.
|
|
|
|
*/
|
|
|
|
|
2006-01-31 01:33:19 +02:00
|
|
|
static FILE *rwpipe_writer( rwpipe *this )
|
2003-09-03 17:20:33 +03:00
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
if ( this != NULL )
|
|
|
|
return this->writer;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a number from the pipe - assumes PNM style headers.
|
|
|
|
*/
|
|
|
|
|
2006-01-31 01:33:19 +02:00
|
|
|
static int rwpipe_read_number( rwpipe *rw )
|
2003-09-28 23:58:57 +03:00
|
|
|
{
|
|
|
|
int value = 0;
|
|
|
|
int c = 0;
|
|
|
|
FILE *in = rwpipe_reader( rw );
|
|
|
|
|
2005-12-17 20:14:38 +02:00
|
|
|
do
|
2003-09-28 23:58:57 +03:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2006-01-31 01:33:19 +02:00
|
|
|
static int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
|
2003-09-28 23:58:57 +03:00
|
|
|
{
|
|
|
|
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;
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Close the pipe and process.
|
|
|
|
*/
|
|
|
|
|
2006-01-31 01:33:19 +02:00
|
|
|
static void rwpipe_close( rwpipe *this )
|
2003-09-03 17:20:33 +03:00
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
if ( this != NULL )
|
|
|
|
{
|
|
|
|
fclose( this->reader );
|
|
|
|
fclose( this->writer );
|
|
|
|
waitpid( this->pid, NULL, 0 );
|
|
|
|
av_free( this );
|
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
/** Context info for this vhook - stores the pipe and image buffers.
|
|
|
|
*/
|
|
|
|
|
2005-12-17 20:14:38 +02:00
|
|
|
typedef struct
|
2003-09-03 17:20:33 +03:00
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
rwpipe *rw;
|
|
|
|
int size1;
|
|
|
|
char *buf1;
|
|
|
|
int size2;
|
|
|
|
char *buf2;
|
2006-09-03 22:25:06 +03:00
|
|
|
|
|
|
|
// This vhook first converts frame to RGB ...
|
|
|
|
struct SwsContext *toRGB_convert_ctx;
|
|
|
|
// ... then processes it via a PPM command pipe ...
|
|
|
|
// ... and finally converts back frame from RGB to initial format
|
|
|
|
struct SwsContext *fromRGB_convert_ctx;
|
2005-12-17 20:14:38 +02:00
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
ContextInfo;
|
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
/** Initialise the context info for this vhook.
|
|
|
|
*/
|
2003-09-03 17:20:33 +03:00
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
int Configure(void **ctxp, int argc, char *argv[])
|
2003-09-03 17:20:33 +03:00
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
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;
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
/** Process a frame.
|
|
|
|
*/
|
2003-09-03 17:20:33 +03:00
|
|
|
|
|
|
|
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
|
|
|
|
{
|
2003-09-28 23:58:57 +03:00
|
|
|
int err = 0;
|
2003-09-03 17:20:33 +03:00
|
|
|
ContextInfo *ci = (ContextInfo *) ctx;
|
|
|
|
AVPicture picture1;
|
|
|
|
AVPicture picture2;
|
|
|
|
AVPicture *pict = picture;
|
2003-09-28 23:58:57 +03:00
|
|
|
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 */
|
2005-12-17 20:14:38 +02:00
|
|
|
if ( !err && pix_fmt != PIX_FMT_RGB24 )
|
2003-09-28 23:58:57 +03:00
|
|
|
{
|
|
|
|
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);
|
2006-09-03 22:25:06 +03:00
|
|
|
|
|
|
|
// if we already got a SWS context, let's realloc if is not re-useable
|
|
|
|
ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
|
|
|
|
width, height, pix_fmt,
|
|
|
|
width, height, PIX_FMT_RGB24,
|
|
|
|
sws_flags, NULL, NULL, NULL);
|
|
|
|
if (ci->toRGB_convert_ctx == NULL) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Cannot initialize the toRGB conversion context\n");
|
2007-04-01 20:23:15 +03:00
|
|
|
return;
|
2006-09-03 22:25:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// img_convert parameters are 2 first destination, then 4 source
|
|
|
|
// sws_scale parameters are context, 4 first source, then 2 destination
|
|
|
|
sws_scale(ci->toRGB_convert_ctx,
|
|
|
|
picture->data, picture->linesize, 0, height,
|
|
|
|
picture1.data, picture1.linesize);
|
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
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 ];
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
2003-09-28 23:58:57 +03:00
|
|
|
if ( !err )
|
|
|
|
err = fflush( out );
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
/* Read the PPM returned. */
|
|
|
|
if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
|
|
|
|
{
|
2003-09-03 17:20:33 +03:00
|
|
|
int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
|
2003-09-28 23:58:57 +03:00
|
|
|
|
|
|
|
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 ];
|
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
2003-09-28 23:58:57 +03:00
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
/* Convert the returned PPM back to the input format */
|
|
|
|
if ( !err )
|
|
|
|
{
|
2006-09-03 22:25:06 +03:00
|
|
|
/* The out_width/out_height returned from the PPM
|
|
|
|
* filter won't necessarily be the same as width and height
|
|
|
|
* but it will be scaled anyway to width/height.
|
2003-09-28 23:58:57 +03:00
|
|
|
*/
|
2006-09-03 22:25:06 +03:00
|
|
|
av_log(NULL, AV_LOG_DEBUG,
|
|
|
|
"PPM vhook: Input dimensions: %d x %d Output dimensions: %d x %d\n",
|
|
|
|
width, height, out_width, out_height);
|
|
|
|
ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
|
|
|
|
out_width, out_height, PIX_FMT_RGB24,
|
|
|
|
width, height, pix_fmt,
|
|
|
|
sws_flags, NULL, NULL, NULL);
|
|
|
|
if (ci->fromRGB_convert_ctx == NULL) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Cannot initialize the fromRGB conversion context\n");
|
2007-04-01 20:23:15 +03:00
|
|
|
return;
|
2003-09-28 23:58:57 +03:00
|
|
|
}
|
2006-09-03 22:25:06 +03:00
|
|
|
|
|
|
|
// img_convert parameters are 2 first destination, then 4 source
|
|
|
|
// sws_scale parameters are context, 4 first source, then 2 destination
|
|
|
|
sws_scale(ci->fromRGB_convert_ctx,
|
|
|
|
picture2.data, picture2.linesize, 0, out_height,
|
|
|
|
picture->data, picture->linesize);
|
2003-09-28 23:58:57 +03:00
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|
2003-09-28 23:58:57 +03:00
|
|
|
/** Clean up the effect.
|
|
|
|
*/
|
|
|
|
|
2003-09-03 17:20:33 +03:00
|
|
|
void Release(void *ctx)
|
|
|
|
{
|
|
|
|
ContextInfo *ci;
|
|
|
|
ci = (ContextInfo *) ctx;
|
|
|
|
|
|
|
|
if (ctx)
|
2003-09-28 23:58:57 +03:00
|
|
|
{
|
|
|
|
rwpipe_close( ci->rw );
|
|
|
|
av_free( ci->buf1 );
|
|
|
|
av_free( ci->buf2 );
|
2006-09-03 22:25:06 +03:00
|
|
|
sws_freeContext(ci->toRGB_convert_ctx);
|
|
|
|
sws_freeContext(ci->fromRGB_convert_ctx);
|
2003-09-03 17:20:33 +03:00
|
|
|
av_free(ctx);
|
2003-09-28 23:58:57 +03:00
|
|
|
}
|
2003-09-03 17:20:33 +03:00
|
|
|
}
|
|
|
|
|