amd64 as canonical system type patch by (Jacob Meuser <jakemsr jakemsr com )
[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"
cf344ec7 27#include "avformat.h"
bee0d9e5
CY
28
29/** Bi-directional pipe structure.
30*/
31
32typedef struct rwpipe
33{
1e4ddde2
CY
34 int pid;
35 FILE *reader;
36 FILE *writer;
bee0d9e5
CY
37}
38rwpipe;
39
40/** Create a bidirectional pipe for the given command.
41*/
42
43rwpipe *rwpipe_open( int argc, char *argv[] )
44{
1e4ddde2 45 rwpipe *this = av_mallocz( sizeof( rwpipe ) );
bee0d9e5 46
1e4ddde2
CY
47 if ( this != NULL )
48 {
bee0d9e5
CY
49 int input[ 2 ];
50 int output[ 2 ];
51
52 pipe( input );
53 pipe( output );
54
55 this->pid = fork();
56
57 if ( this->pid == 0 )
58 {
1f3f9507
MN
59#define COMMAND_SIZE 10240
60 char *command = av_mallocz( COMMAND_SIZE );
1e4ddde2 61 int i;
bee0d9e5 62
1e4ddde2
CY
63 strcpy( command, "" );
64 for ( i = 0; i < argc; i ++ )
65 {
1f3f9507
MN
66 pstrcat( command, COMMAND_SIZE, argv[ i ] );
67 pstrcat( command, COMMAND_SIZE, " " );
1e4ddde2 68 }
bee0d9e5
CY
69
70 dup2( output[ 0 ], STDIN_FILENO );
71 dup2( input[ 1 ], STDOUT_FILENO );
72
73 close( input[ 0 ] );
74 close( input[ 1 ] );
75 close( output[ 0 ] );
76 close( output[ 1 ] );
77
78 execl("/bin/sh", "sh", "-c", command, NULL );
1e4ddde2 79 exit( 255 );
bee0d9e5
CY
80 }
81 else
82 {
83 close( input[ 1 ] );
84 close( output[ 0 ] );
85
86 this->reader = fdopen( input[ 0 ], "r" );
87 this->writer = fdopen( output[ 1 ], "w" );
88 }
1e4ddde2 89 }
bee0d9e5 90
1e4ddde2 91 return this;
bee0d9e5
CY
92}
93
94/** Read data from the pipe.
95*/
96
97FILE *rwpipe_reader( rwpipe *this )
98{
1e4ddde2
CY
99 if ( this != NULL )
100 return this->reader;
101 else
102 return NULL;
bee0d9e5
CY
103}
104
105/** Write data to the pipe.
106*/
107
108FILE *rwpipe_writer( rwpipe *this )
109{
1e4ddde2
CY
110 if ( this != NULL )
111 return this->writer;
112 else
113 return NULL;
114}
115
116/* Read a number from the pipe - assumes PNM style headers.
117*/
118
119int rwpipe_read_number( rwpipe *rw )
120{
121 int value = 0;
122 int c = 0;
123 FILE *in = rwpipe_reader( rw );
124
125 do
126 {
127 c = fgetc( in );
128
129 while( c != EOF && !isdigit( c ) && c != '#' )
130 c = fgetc( in );
131
132 if ( c == '#' )
133 while( c != EOF && c != '\n' )
134 c = fgetc( in );
135 }
136 while ( c != EOF && !isdigit( c ) );
137
138 while( c != EOF && isdigit( c ) )
139 {
140 value = value * 10 + ( c - '0' );
141 c = fgetc( in );
142 }
143
144 return value;
145}
146
147/** Read a PPM P6 header.
148*/
149
150int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
151{
152 char line[ 3 ];
153 FILE *in = rwpipe_reader( rw );
154 int max;
155
156 fgets( line, 3, in );
157 if ( !strncmp( line, "P6", 2 ) )
158 {
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;
163 }
164 return 1;
bee0d9e5
CY
165}
166
167/** Close the pipe and process.
168*/
169
170void rwpipe_close( rwpipe *this )
171{
1e4ddde2
CY
172 if ( this != NULL )
173 {
174 fclose( this->reader );
175 fclose( this->writer );
176 waitpid( this->pid, NULL, 0 );
177 av_free( this );
178 }
bee0d9e5
CY
179}
180
1e4ddde2
CY
181/** Context info for this vhook - stores the pipe and image buffers.
182*/
183
bee0d9e5
CY
184typedef struct
185{
1e4ddde2
CY
186 rwpipe *rw;
187 int size1;
188 char *buf1;
189 int size2;
190 char *buf2;
bee0d9e5
CY
191}
192ContextInfo;
193
1e4ddde2
CY
194/** Initialise the context info for this vhook.
195*/
bee0d9e5 196
1e4ddde2 197int Configure(void **ctxp, int argc, char *argv[])
bee0d9e5 198{
1e4ddde2
CY
199 if ( argc > 1 )
200 {
201 *ctxp = av_mallocz(sizeof(ContextInfo));
202 if ( ctxp != NULL && argc > 1 )
203 {
204 ContextInfo *info = (ContextInfo *)*ctxp;
205 info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
206 return 0;
207 }
208 }
209 return 1;
bee0d9e5
CY
210}
211
1e4ddde2
CY
212/** Process a frame.
213*/
bee0d9e5
CY
214
215void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
216{
1e4ddde2 217 int err = 0;
bee0d9e5 218 ContextInfo *ci = (ContextInfo *) ctx;
bee0d9e5
CY
219 AVPicture picture1;
220 AVPicture picture2;
221 AVPicture *pict = picture;
1e4ddde2
CY
222 int out_width;
223 int out_height;
224 int i;
225 uint8_t *ptr = NULL;
226 FILE *in = rwpipe_reader( ci->rw );
227 FILE *out = rwpipe_writer( ci->rw );
228
229 /* Check that we have a pipe to talk to. */
230 if ( in == NULL || out == NULL )
231 err = 1;
232
233 /* Convert to RGB24 if necessary */
234 if ( !err && pix_fmt != PIX_FMT_RGB24 )
235 {
236 int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
237
238 if ( size != ci->size1 )
239 {
240 av_free( ci->buf1 );
241 ci->buf1 = av_malloc(size);
242 ci->size1 = size;
243 err = ci->buf1 == NULL;
244 }
245
246 if ( !err )
247 {
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)
250 err = 1;
251 pict = &picture1;
252 }
253 }
254
255 /* Write out the PPM */
256 if ( !err )
257 {
258 ptr = pict->data[ 0 ];
259 fprintf( out, "P6\n%d %d\n255\n", width, height );
260 for ( i = 0; !err && i < height; i ++ )
261 {
262 err = !fwrite( ptr, width * 3, 1, out );
263 ptr += pict->linesize[ 0 ];
bee0d9e5 264 }
1e4ddde2
CY
265 if ( !err )
266 err = fflush( out );
bee0d9e5
CY
267 }
268
1e4ddde2
CY
269 /* Read the PPM returned. */
270 if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
271 {
bee0d9e5 272 int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
1e4ddde2
CY
273
274 if ( size != ci->size2 )
275 {
276 av_free( ci->buf2 );
277 ci->buf2 = av_malloc(size);
278 ci->size2 = size;
279 err = ci->buf2 == NULL;
280 }
281
282 if ( !err )
283 {
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 ++ )
287 {
288 err = !fread( ptr, out_width * 3, 1, in );
289 ptr += picture2.linesize[ 0 ];
290 }
bee0d9e5 291 }
1e4ddde2 292 }
bee0d9e5 293
1e4ddde2
CY
294 /* Convert the returned PPM back to the input format */
295 if ( !err )
296 {
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.
302 *
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.
305 *
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.
309 */
310 if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0)
311 {
312 }
313 }
bee0d9e5
CY
314}
315
1e4ddde2
CY
316/** Clean up the effect.
317*/
318
bee0d9e5
CY
319void Release(void *ctx)
320{
321 ContextInfo *ci;
322 ci = (ContextInfo *) ctx;
323
324 if (ctx)
1e4ddde2
CY
325 {
326 rwpipe_close( ci->rw );
327 av_free( ci->buf1 );
328 av_free( ci->buf2 );
bee0d9e5 329 av_free(ctx);
1e4ddde2 330 }
bee0d9e5
CY
331}
332