Initialize correctly optind.
[libav.git] / vhook / ppm.c
CommitLineData
bee0d9e5 1/*
115329f1 2 * PPM Video Hook
bee0d9e5
CY
3 * Copyright (c) 2003 Charles Yates
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
bee0d9e5
CY
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
bee0d9e5 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
bee0d9e5
CY
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bee0d9e5
CY
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include <ctype.h>
28#include "framehook.h"
cf344ec7 29#include "avformat.h"
a4dd5fb7 30#include "swscale.h"
272605c7 31#include "avstring.h"
a4dd5fb7
VP
32
33static int sws_flags = SWS_BICUBIC;
bee0d9e5
CY
34
35/** Bi-directional pipe structure.
36*/
37
38typedef struct rwpipe
39{
1e4ddde2
CY
40 int pid;
41 FILE *reader;
42 FILE *writer;
bee0d9e5
CY
43}
44rwpipe;
45
46/** Create a bidirectional pipe for the given command.
47*/
48
ceaf1909 49static rwpipe *rwpipe_open( int argc, char *argv[] )
bee0d9e5 50{
1e4ddde2 51 rwpipe *this = av_mallocz( sizeof( rwpipe ) );
bee0d9e5 52
1e4ddde2
CY
53 if ( this != NULL )
54 {
bee0d9e5
CY
55 int input[ 2 ];
56 int output[ 2 ];
57
58 pipe( input );
59 pipe( output );
60
61 this->pid = fork();
62
63 if ( this->pid == 0 )
64 {
1f3f9507
MN
65#define COMMAND_SIZE 10240
66 char *command = av_mallocz( COMMAND_SIZE );
1e4ddde2 67 int i;
bee0d9e5 68
1e4ddde2
CY
69 strcpy( command, "" );
70 for ( i = 0; i < argc; i ++ )
71 {
272605c7
RD
72 av_strlcat( command, argv[ i ], COMMAND_SIZE );
73 av_strlcat( command, " ", COMMAND_SIZE );
1e4ddde2 74 }
bee0d9e5
CY
75
76 dup2( output[ 0 ], STDIN_FILENO );
77 dup2( input[ 1 ], STDOUT_FILENO );
78
79 close( input[ 0 ] );
80 close( input[ 1 ] );
81 close( output[ 0 ] );
82 close( output[ 1 ] );
83
efe41983 84 execl("/bin/sh", "sh", "-c", command, (char*)NULL );
f321635a 85 _exit( 255 );
bee0d9e5
CY
86 }
87 else
88 {
89 close( input[ 1 ] );
90 close( output[ 0 ] );
91
92 this->reader = fdopen( input[ 0 ], "r" );
93 this->writer = fdopen( output[ 1 ], "w" );
94 }
1e4ddde2 95 }
bee0d9e5 96
1e4ddde2 97 return this;
bee0d9e5
CY
98}
99
100/** Read data from the pipe.
101*/
102
ceaf1909 103static FILE *rwpipe_reader( rwpipe *this )
bee0d9e5 104{
1e4ddde2
CY
105 if ( this != NULL )
106 return this->reader;
107 else
108 return NULL;
bee0d9e5
CY
109}
110
111/** Write data to the pipe.
112*/
113
ceaf1909 114static FILE *rwpipe_writer( rwpipe *this )
bee0d9e5 115{
1e4ddde2
CY
116 if ( this != NULL )
117 return this->writer;
118 else
119 return NULL;
120}
121
122/* Read a number from the pipe - assumes PNM style headers.
123*/
124
ceaf1909 125static int rwpipe_read_number( rwpipe *rw )
1e4ddde2
CY
126{
127 int value = 0;
128 int c = 0;
129 FILE *in = rwpipe_reader( rw );
130
115329f1 131 do
1e4ddde2
CY
132 {
133 c = fgetc( in );
134
135 while( c != EOF && !isdigit( c ) && c != '#' )
136 c = fgetc( in );
137
138 if ( c == '#' )
139 while( c != EOF && c != '\n' )
140 c = fgetc( in );
141 }
142 while ( c != EOF && !isdigit( c ) );
143
144 while( c != EOF && isdigit( c ) )
145 {
146 value = value * 10 + ( c - '0' );
147 c = fgetc( in );
148 }
149
150 return value;
151}
152
153/** Read a PPM P6 header.
154*/
155
ceaf1909 156static int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
1e4ddde2
CY
157{
158 char line[ 3 ];
159 FILE *in = rwpipe_reader( rw );
160 int max;
161
162 fgets( line, 3, in );
163 if ( !strncmp( line, "P6", 2 ) )
164 {
165 *width = rwpipe_read_number( rw );
166 *height = rwpipe_read_number( rw );
167 max = rwpipe_read_number( rw );
168 return max != 255 || *width <= 0 || *height <= 0;
169 }
170 return 1;
bee0d9e5
CY
171}
172
173/** Close the pipe and process.
174*/
175
ceaf1909 176static void rwpipe_close( rwpipe *this )
bee0d9e5 177{
1e4ddde2
CY
178 if ( this != NULL )
179 {
180 fclose( this->reader );
181 fclose( this->writer );
182 waitpid( this->pid, NULL, 0 );
183 av_free( this );
184 }
bee0d9e5
CY
185}
186
1e4ddde2
CY
187/** Context info for this vhook - stores the pipe and image buffers.
188*/
189
115329f1 190typedef struct
bee0d9e5 191{
1e4ddde2
CY
192 rwpipe *rw;
193 int size1;
194 char *buf1;
195 int size2;
196 char *buf2;
a4dd5fb7
VP
197
198 // This vhook first converts frame to RGB ...
199 struct SwsContext *toRGB_convert_ctx;
200 // ... then processes it via a PPM command pipe ...
201 // ... and finally converts back frame from RGB to initial format
202 struct SwsContext *fromRGB_convert_ctx;
115329f1 203}
bee0d9e5
CY
204ContextInfo;
205
1e4ddde2
CY
206/** Initialise the context info for this vhook.
207*/
bee0d9e5 208
1e4ddde2 209int Configure(void **ctxp, int argc, char *argv[])
bee0d9e5 210{
1e4ddde2
CY
211 if ( argc > 1 )
212 {
213 *ctxp = av_mallocz(sizeof(ContextInfo));
214 if ( ctxp != NULL && argc > 1 )
215 {
216 ContextInfo *info = (ContextInfo *)*ctxp;
217 info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
218 return 0;
219 }
220 }
221 return 1;
bee0d9e5
CY
222}
223
1e4ddde2
CY
224/** Process a frame.
225*/
bee0d9e5
CY
226
227void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
228{
1e4ddde2 229 int err = 0;
bee0d9e5 230 ContextInfo *ci = (ContextInfo *) ctx;
bee0d9e5
CY
231 AVPicture picture1;
232 AVPicture picture2;
233 AVPicture *pict = picture;
1e4ddde2
CY
234 int out_width;
235 int out_height;
236 int i;
237 uint8_t *ptr = NULL;
238 FILE *in = rwpipe_reader( ci->rw );
239 FILE *out = rwpipe_writer( ci->rw );
240
241 /* Check that we have a pipe to talk to. */
242 if ( in == NULL || out == NULL )
243 err = 1;
244
245 /* Convert to RGB24 if necessary */
115329f1 246 if ( !err && pix_fmt != PIX_FMT_RGB24 )
1e4ddde2
CY
247 {
248 int size = avpicture_get_size(PIX_FMT_RGB24, width, height);
249
250 if ( size != ci->size1 )
251 {
252 av_free( ci->buf1 );
253 ci->buf1 = av_malloc(size);
254 ci->size1 = size;
255 err = ci->buf1 == NULL;
256 }
257
258 if ( !err )
259 {
260 avpicture_fill(&picture1, ci->buf1, PIX_FMT_RGB24, width, height);
a4dd5fb7
VP
261
262 // if we already got a SWS context, let's realloc if is not re-useable
263 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
264 width, height, pix_fmt,
265 width, height, PIX_FMT_RGB24,
266 sws_flags, NULL, NULL, NULL);
267 if (ci->toRGB_convert_ctx == NULL) {
268 av_log(NULL, AV_LOG_ERROR,
269 "Cannot initialize the toRGB conversion context\n");
ca345e44 270 return;
a4dd5fb7
VP
271 }
272
273// img_convert parameters are 2 first destination, then 4 source
274// sws_scale parameters are context, 4 first source, then 2 destination
275 sws_scale(ci->toRGB_convert_ctx,
276 picture->data, picture->linesize, 0, height,
277 picture1.data, picture1.linesize);
278
1e4ddde2
CY
279 pict = &picture1;
280 }
281 }
282
283 /* Write out the PPM */
284 if ( !err )
285 {
286 ptr = pict->data[ 0 ];
287 fprintf( out, "P6\n%d %d\n255\n", width, height );
288 for ( i = 0; !err && i < height; i ++ )
289 {
290 err = !fwrite( ptr, width * 3, 1, out );
291 ptr += pict->linesize[ 0 ];
bee0d9e5 292 }
1e4ddde2
CY
293 if ( !err )
294 err = fflush( out );
bee0d9e5
CY
295 }
296
1e4ddde2
CY
297 /* Read the PPM returned. */
298 if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
299 {
bee0d9e5 300 int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
1e4ddde2
CY
301
302 if ( size != ci->size2 )
303 {
304 av_free( ci->buf2 );
305 ci->buf2 = av_malloc(size);
306 ci->size2 = size;
307 err = ci->buf2 == NULL;
308 }
309
310 if ( !err )
311 {
312 avpicture_fill(&picture2, ci->buf2, PIX_FMT_RGB24, out_width, out_height);
313 ptr = picture2.data[ 0 ];
314 for ( i = 0; !err && i < out_height; i ++ )
315 {
316 err = !fread( ptr, out_width * 3, 1, in );
317 ptr += picture2.linesize[ 0 ];
318 }
bee0d9e5 319 }
1e4ddde2 320 }
bee0d9e5 321
1e4ddde2
CY
322 /* Convert the returned PPM back to the input format */
323 if ( !err )
324 {
a4dd5fb7
VP
325 /* The out_width/out_height returned from the PPM
326 * filter won't necessarily be the same as width and height
327 * but it will be scaled anyway to width/height.
1e4ddde2 328 */
a4dd5fb7
VP
329 av_log(NULL, AV_LOG_DEBUG,
330 "PPM vhook: Input dimensions: %d x %d Output dimensions: %d x %d\n",
331 width, height, out_width, out_height);
332 ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
333 out_width, out_height, PIX_FMT_RGB24,
334 width, height, pix_fmt,
335 sws_flags, NULL, NULL, NULL);
336 if (ci->fromRGB_convert_ctx == NULL) {
337 av_log(NULL, AV_LOG_ERROR,
338 "Cannot initialize the fromRGB conversion context\n");
ca345e44 339 return;
1e4ddde2 340 }
a4dd5fb7
VP
341
342// img_convert parameters are 2 first destination, then 4 source
343// sws_scale parameters are context, 4 first source, then 2 destination
344 sws_scale(ci->fromRGB_convert_ctx,
345 picture2.data, picture2.linesize, 0, out_height,
346 picture->data, picture->linesize);
1e4ddde2 347 }
bee0d9e5
CY
348}
349
1e4ddde2
CY
350/** Clean up the effect.
351*/
352
bee0d9e5
CY
353void Release(void *ctx)
354{
355 ContextInfo *ci;
356 ci = (ContextInfo *) ctx;
357
358 if (ctx)
1e4ddde2
CY
359 {
360 rwpipe_close( ci->rw );
361 av_free( ci->buf1 );
362 av_free( ci->buf2 );
a4dd5fb7
VP
363 sws_freeContext(ci->toRGB_convert_ctx);
364 sws_freeContext(ci->fromRGB_convert_ctx);
bee0d9e5 365 av_free(ctx);
1e4ddde2 366 }
bee0d9e5
CY
367}
368