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