init AVFrame with defaults
[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{
33 int pid;
34 FILE *reader;
35 FILE *writer;
36}
37rwpipe;
38
39/** Create a bidirectional pipe for the given command.
40*/
41
42rwpipe *rwpipe_open( int argc, char *argv[] )
43{
44 rwpipe *this = av_mallocz( sizeof( rwpipe ) );
45
46 if ( this != NULL )
47 {
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 {
58 char *command = av_mallocz( 10240 );
59 int i;
60
61 strcpy( command, "" );
62 for ( i = 0; i < argc; i ++ )
63 {
64 strcat( command, argv[ i ] );
65 strcat( command, " " );
66 }
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 );
77 exit( 255 );
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 }
87 }
88
89 return this;
90}
91
92/** Read data from the pipe.
93*/
94
95FILE *rwpipe_reader( rwpipe *this )
96{
97 if ( this != NULL )
98 return this->reader;
99 else
100 return NULL;
101}
102
103/** Write data to the pipe.
104*/
105
106FILE *rwpipe_writer( rwpipe *this )
107{
108 if ( this != NULL )
109 return this->writer;
110 else
111 return NULL;
112}
113
114/** Close the pipe and process.
115*/
116
117void rwpipe_close( rwpipe *this )
118{
119 if ( this != NULL )
120 {
121 fclose( this->reader );
122 fclose( this->writer );
123 waitpid( this->pid, NULL, 0 );
124 av_free( this );
125 }
126}
127
128typedef struct
129{
130 rwpipe *rw;
131}
132ContextInfo;
133
134int Configure(void **ctxp, int argc, char *argv[])
135{
136 *ctxp = av_mallocz(sizeof(ContextInfo));
137 if ( ctxp != NULL && argc > 1 )
138 {
139 ContextInfo *info = (ContextInfo *)*ctxp;
140 info->rw = rwpipe_open( argc - 1, &argv[ 1 ] );
141 }
142 return 0;
143}
144
145int rwpipe_read_number( rwpipe *rw )
146{
147 int value = 0;
148 int c = 0;
149 FILE *in = rwpipe_reader( rw );
150
151 do
152 {
153 c = fgetc( in );
154
155 while( c != EOF && !isdigit( c ) && c != '#' )
156 c = fgetc( in );
157
158 if ( c == '#' )
159 while( c != EOF && c != '\n' )
160 c = fgetc( in );
161 }
162 while ( c != EOF && !isdigit( c ) );
163
164 while( c != EOF && isdigit( c ) )
165 {
166 value = value * 10 + ( c - '0' );
167 c = fgetc( in );
168 }
169
170 return value;
171}
172
173int rwpipe_read_ppm_header( rwpipe *rw, int *width, int *height )
174{
175 char line[ 3 ];
176 FILE *in = rwpipe_reader( rw );
177 int max;
178
179 fgets( line, 3, in );
180 if ( !strncmp( line, "P6", 2 ) )
181 {
182 *width = rwpipe_read_number( rw );
183 *height = rwpipe_read_number( rw );
184 max = rwpipe_read_number( rw );
185 return max != 255 || *width <= 0 || *height <= 0;
186 }
187 return 1;
188}
189
190void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
191{
192 int err = 0;
193 ContextInfo *ci = (ContextInfo *) ctx;
194 char *buf1 = 0;
195 char *buf2 = 0;
196 AVPicture picture1;
197 AVPicture picture2;
198 AVPicture *pict = picture;
199 int out_width;
200 int out_height;
201 int i;
202 uint8_t *ptr = NULL;
203 FILE *in = rwpipe_reader( ci->rw );
204 FILE *out = rwpipe_writer( ci->rw );
205
206 /* Convert to RGB24 if necessary */
207 if (pix_fmt != PIX_FMT_RGB24) {
208 int size;
209
210 size = avpicture_get_size(PIX_FMT_RGB24, width, height);
211 buf1 = av_malloc(size);
212
213 avpicture_fill(&picture1, buf1, PIX_FMT_RGB24, width, height);
214 if (img_convert(&picture1, PIX_FMT_RGB24,
215 picture, pix_fmt, width, height) < 0) {
216 err = 1;
217 }
218 pict = &picture1;
219 }
220
221 /* Write out the PPM */
222 if ( !err )
223 {
224 ptr = pict->data[ 0 ];
225 fprintf( out, "P6\n%d %d\n255\n", width, height );
226 for ( i = 0; !err && i < height; i ++ )
227 {
228 err = !fwrite( ptr, width * 3, 1, out );
229 ptr += pict->linesize[ 0 ];
230 }
231 if ( !err )
232 err = fflush( out );
233 }
234
235 /* Read the PPM returned. */
236 if ( !err && !rwpipe_read_ppm_header( ci->rw, &out_width, &out_height ) )
237 {
238 int size = avpicture_get_size(PIX_FMT_RGB24, out_width, out_height);
239 buf2 = av_malloc(size);
240 avpicture_fill(&picture2, buf2, PIX_FMT_RGB24, out_width, out_height);
241 ptr = picture2.data[ 0 ];
242 for ( i = 0; !err && i < out_height; i ++ )
243 {
244 err = !fread( ptr, out_width * 3, 1, in );
245 ptr += picture2.linesize[ 0 ];
246 }
247 }
248
249 /* Convert the returned PPM back to the input format */
250 if ( !err )
251 {
252 if (img_convert(picture, pix_fmt, &picture2, PIX_FMT_RGB24, width, height) < 0) {
253 }
254 }
255
256 av_free(buf1);
257 av_free(buf2);
258}
259
260void Release(void *ctx)
261{
262 ContextInfo *ci;
263 ci = (ContextInfo *) ctx;
264
265 if (ctx)
266 {
267 rwpipe_close( ci->rw );
268 av_free(ctx);
269 }
270}
271