cvs policy entry about security
[libav.git] / vhook / ppm.c
CommitLineData
bee0d9e5
CY
1/*
2 * PPM Video Hook
3 * Copyright (c) 2003 Charles Yates
4 *
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.
9 *
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.
14 *
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
18 */
19
20#include <stdio.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <ctype.h>
26#include "framehook.h"
27
28/** Bi-directional pipe structure.
29*/
30
31typedef struct rwpipe
32{
1e4ddde2
CY
33 int pid;
34 FILE *reader;
35 FILE *writer;
bee0d9e5
CY
36}
37rwpipe;
38
39/** Create a bidirectional pipe for the given command.
40*/
41
42rwpipe *rwpipe_open( int argc, char *argv[] )
43{
1e4ddde2 44 rwpipe *this = av_mallocz( sizeof( rwpipe ) );
bee0d9e5 45
1e4ddde2
CY
46 if ( this != NULL )
47 {
bee0d9e5
CY
48 int input[ 2 ];
49 int output[ 2 ];
50
51 pipe( input );
52 pipe( output );
53
54 this->pid = fork();
55
56 if ( this->pid == 0 )
57 {
1e4ddde2
CY
58 char *command = av_mallocz( 10240 );
59 int i;
bee0d9e5 60
1e4ddde2
CY
61 strcpy( command, "" );
62 for ( i = 0; i < argc; i ++ )
63 {
64 strcat( command, argv[ i ] );
65 strcat( command, " " );
66 }
bee0d9e5
CY
67
68 dup2( output[ 0 ], STDIN_FILENO );
69 dup2( input[ 1 ], STDOUT_FILENO );
70
71 close( input[ 0 ] );
72 close( input[ 1 ] );
73 close( output[ 0 ] );
74 close( output[ 1 ] );
75
76 execl("/bin/sh", "sh", "-c", command, NULL );
1e4ddde2 77 exit( 255 );
bee0d9e5
CY
78 }
79 else
80 {
81 close( input[ 1 ] );
82 close( output[ 0 ] );
83
84 this->reader = fdopen( input[ 0 ], "r" );
85 this->writer = fdopen( output[ 1 ], "w" );
86 }
1e4ddde2 87 }
bee0d9e5 88
1e4ddde2 89 return this;
bee0d9e5
CY
90}
91
92/** Read data from the pipe.
93*/
94
95FILE *rwpipe_reader( rwpipe *this )
96{
1e4ddde2
CY
97 if ( this != NULL )
98 return this->reader;
99 else
100 return NULL;
bee0d9e5
CY
101}
102
103/** Write data to the pipe.
104*/
105
106FILE *rwpipe_writer( rwpipe *this )
107{
1e4ddde2
CY
108 if ( this != NULL )
109 return this->writer;
110 else
111 return NULL;
112}
113
114/* Read a number from the pipe - assumes PNM style headers.
115*/
116
117int rwpipe_read_number( rwpipe *rw )
118{
119 int value = 0;
120 int c = 0;
121 FILE *in = rwpipe_reader( rw );
122
123 do
124 {
125 c = fgetc( in );
126
127 while( c != EOF && !isdigit( c ) && c != '#' )
128 c = fgetc( in );
129
130 if ( c == '#' )
131 while( c != EOF && c != '\n' )
132 c = fgetc( in );
133 }
134 while ( c != EOF && !isdigit( c ) );
135
136 while( c != EOF && isdigit( c ) )
137 {
138 value = value * 10 + ( c - '0' );
139 c = fgetc( in );
140 }
141
142 return value;
143}
144
145/** Read a PPM P6 header.
146*/
147
148int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
149{
150 char line[ 3 ];
151 FILE *in = rwpipe_reader( rw );
152 int max;
153
154 fgets( line, 3, in );
155 if ( !strncmp( line, "P6", 2 ) )
156 {
157 *width = rwpipe_read_number( rw );
158 *height = rwpipe_read_number( rw );
159 max = rwpipe_read_number( rw );
160 return max != 255 || *width <= 0 || *height <= 0;
161 }
162 return 1;
bee0d9e5
CY
163}
164
165/** Close the pipe and process.
166*/
167
168void rwpipe_close( rwpipe *this )
169{
1e4ddde2
CY
170 if ( this != NULL )
171 {
172 fclose( this->reader );
173 fclose( this->writer );
174 waitpid( this->pid, NULL, 0 );
175 av_free( this );
176 }
bee0d9e5
CY
177}
178
1e4ddde2
CY
179/** Context info for this vhook - stores the pipe and image buffers.
180*/
181
bee0d9e5
CY
182typedef struct
183{
1e4ddde2
CY
184 rwpipe *rw;
185 int size1;
186 char *buf1;
187 int size2;
188 char *buf2;
bee0d9e5
CY
189}
190ContextInfo;
191
1e4ddde2
CY
192/** Initialise the context info for this vhook.
193*/
bee0d9e5 194
1e4ddde2 195int Configure(void **ctxp, int argc, char *argv[])
bee0d9e5 196{
1e4ddde2
CY
197 if ( argc > 1 )
198 {
199 *ctxp = av_mallocz(sizeof(ContextInfo));
200 if ( ctxp != NULL && argc > 1 )
201 {
202 ContextInfo *info = (ContextInfo *)*ctxp;
203 info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
204 return 0;
205 }
206 }
207 return 1;
bee0d9e5
CY
208}
209
1e4ddde2
CY
210/** Process a frame.
211*/
bee0d9e5
CY
212
213void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
214{
1e4ddde2 215 int err = 0;
bee0d9e5 216 ContextInfo *ci = (ContextInfo *) ctx;
bee0d9e5
CY
217 AVPicture picture1;
218 AVPicture picture2;
219 AVPicture *pict = picture;
1e4ddde2
CY
220 int out_width;
221 int out_height;
222 int i;
223 uint8_t *ptr = NULL;
224 FILE *in = rwpipe_reader( ci->rw );
225 FILE *out = rwpipe_writer( ci->rw );
226
227 /* Check that we have a pipe to talk to. */
228 if ( in == NULL || out == NULL )
229 err = 1;
230
231 /* Convert to RGB24 if necessary */
232 if ( !err && pix_fmt != PIX_FMT_RGB24 )
233 {
234 int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
235
236 if ( size != ci->size1 )
237 {
238 av_free( ci->buf1 );
239 ci->buf1 = av_malloc(size);
240 ci->size1 = size;
241 err = ci->buf1 == NULL;
242 }
243
244 if ( !err )
245 {
246 avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
247 if (img_convert(&picture1, PIX_FMT_RGB24, picture, pix_fmt, width, height) < 0)
248 err = 1;
249 pict = &picture1;
250 }
251 }
252
253 /* Write out the PPM */
254 if ( !err )
255 {
256 ptr = pict->data[ 0 ];
257 fprintf( out, "P6\n%d %d\n255\n", width, height );
258 for ( i = 0; !err && i < height; i ++ )
259 {
260 err = !fwrite( ptr, width * 3, 1, out );
261 ptr += pict->linesize[ 0 ];
bee0d9e5 262 }
1e4ddde2
CY
263 if ( !err )
264 err = fflush( out );
bee0d9e5
CY
265 }
266
1e4ddde2
CY
267 /* Read the PPM returned. */
268 if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
269 {
bee0d9e5 270 int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
1e4ddde2
CY
271
272 if ( size != ci->size2 )
273 {
274 av_free( ci->buf2 );
275 ci->buf2 = av_malloc(size);
276 ci->size2 = size;
277 err = ci->buf2 == NULL;
278 }
279
280 if ( !err )
281 {
282 avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
283 ptr = picture2.data[ 0 ];
284 for ( i = 0; !err && i < out_height; i ++ )
285 {
286 err = !fread( ptr, out_width * 3, 1, in );
287 ptr += picture2.linesize[ 0 ];
288 }
bee0d9e5 289 }
1e4ddde2 290 }
bee0d9e5 291
1e4ddde2
CY
292 /* Convert the returned PPM back to the input format */
293 if ( !err )
294 {
295 /* Actually, this is wrong, since the out_width/out_height returned from the
296 * filter won't necessarily be the same as width and height - img_resample
297 * won't scale rgb24, so the only way out of this is to convert to something
298 * that img_resample does like [which may or may not be pix_fmt], rescale
299 * and finally convert to pix_fmt... slow, but would provide the most flexibility.
300 *
301 * Currently, we take the upper left width/height pixels from the filtered image,
302 * smaller images are going to be corrupted or cause a crash.
303 *
304 * Finally, what should we do in case of this call failing? Up to now, failures
305 * are gracefully ignored and the original image is returned - in this case, a
306 * failure may corrupt the input.
307 */
308 if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0)
309 {
310 }
311 }
bee0d9e5
CY
312}
313
1e4ddde2
CY
314/** Clean up the effect.
315*/
316
bee0d9e5
CY
317void Release(void *ctx)
318{
319 ContextInfo *ci;
320 ci = (ContextInfo *) ctx;
321
322 if (ctx)
1e4ddde2
CY
323 {
324 rwpipe_close( ci->rw );
325 av_free( ci->buf1 );
326 av_free( ci->buf2 );
bee0d9e5 327 av_free(ctx);
1e4ddde2 328 }
bee0d9e5
CY
329}
330