westley serialises with entry in/out; full field, aspect, and colour space normalisat...
[melted] / src / modules / core / producer_ppm.c
1 /*
2 * producer_ppm.c -- simple ppm test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "producer_ppm.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 typedef struct producer_ppm_s *producer_ppm;
29
30 struct producer_ppm_s
31 {
32 struct mlt_producer_s parent;
33 char *command;
34 FILE *video;
35 FILE *audio;
36 uint64_t expected;
37 };
38
39 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
40 static void producer_close( mlt_producer parent );
41
42 mlt_producer producer_ppm_init( void *command )
43 {
44 producer_ppm this = calloc( sizeof( struct producer_ppm_s ), 1 );
45 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
46 {
47 mlt_producer producer = &this->parent;
48 mlt_properties properties = mlt_producer_properties( producer );
49
50 producer->get_frame = producer_get_frame;
51 producer->close = producer_close;
52
53 if ( command != NULL )
54 {
55 mlt_properties_set( properties, "resource", command );
56 this->command = strdup( command );
57 }
58 else
59 {
60 mlt_properties_set( properties, "resource", "ppm test" );
61 }
62
63 return producer;
64 }
65 free( this );
66 return NULL;
67 }
68
69 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
70 {
71 // Get the frames properties
72 mlt_properties properties = mlt_frame_properties( this );
73
74 if ( mlt_properties_get_int( properties, "has_image" ) )
75 {
76 // Get the RGB image
77 uint8_t *rgb = mlt_properties_get_data( properties, "image", NULL );
78
79 // Get width and height
80 *width = mlt_properties_get_int( properties, "width" );
81 *height = mlt_properties_get_int( properties, "height" );
82
83 // Convert to requested format
84 if ( *format == mlt_image_yuv422 )
85 {
86 // IRRIGATE ME
87 uint8_t *image = malloc( *width * ( *height + 1 ) * 2 );
88 mlt_convert_rgb24_to_yuv422( rgb, *width, *height, *width * 3, image );
89 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 2, free, NULL );
90 *buffer = image;
91 }
92 else if ( *format == mlt_image_rgb24 )
93 {
94 *buffer = rgb;
95 }
96 }
97 else
98 {
99 mlt_frame_get_image( this, buffer, format, width, height, writable );
100 }
101
102 return 0;
103 }
104
105 FILE *producer_ppm_run_video( producer_ppm this )
106 {
107 if ( this->video == NULL )
108 {
109 if ( this->command == NULL )
110 {
111 this->video = popen( "image2raw -k -r 25 -ppm /usr/share/pixmaps/*.png", "r" );
112 }
113 else
114 {
115 char command[ 1024 ];
116 float fps = mlt_producer_get_fps( &this->parent );
117 float position = mlt_producer_position( &this->parent );
118 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f imagepipe -r %f -img ppm - 2>/dev/null", this->command, position, fps );
119 this->video = popen( command, "r" );
120 }
121 }
122 return this->video;
123 }
124
125 FILE *producer_ppm_run_audio( producer_ppm this )
126 {
127 if ( this->audio == NULL )
128 {
129 if ( this->command != NULL )
130 {
131 char command[ 1024 ];
132 float position = mlt_producer_position( &this->parent );
133 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f s16le -ar 48000 -ac 2 - 2>/dev/null", this->command, position );
134 this->audio = popen( command, "r" );
135 }
136 }
137 return this->audio;
138 }
139
140 static void producer_ppm_position( producer_ppm this, uint64_t requested )
141 {
142 if ( requested != this->expected )
143 {
144 if ( this->video != NULL )
145 pclose( this->video );
146 this->video = NULL;
147 if ( this->audio != NULL )
148 pclose( this->audio );
149 this->audio = NULL;
150 }
151
152 // This is the next frame we expect
153 this->expected = mlt_producer_frame( &this->parent ) + 1;
154
155 // Open the pipe
156 this->video = producer_ppm_run_video( this );
157
158 // Open the audio pipe
159 this->audio = producer_ppm_run_audio( this );
160
161 }
162
163 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
164 {
165 // Get the frames properties
166 mlt_properties properties = mlt_frame_properties( this );
167
168 FILE *pipe = mlt_properties_get_data( properties, "audio.pipe", NULL );
169
170 *frequency = 48000;
171 *channels = 2;
172 *samples = 1920;
173
174 // Size
175 int size = *samples * *channels * 2;
176
177 // Allocate an image
178 *buffer = malloc( size );
179
180 // Read it
181 if ( pipe != NULL )
182 fread( *buffer, size, 1, pipe );
183 else
184 memset( *buffer, 0, size );
185
186 // Pass the data on the frame properties
187 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
188
189 return 0;
190 }
191
192 static int read_ppm_header( FILE *video, int *width, int *height )
193 {
194 int count = 0;
195 {
196 char temp[ 132 ];
197 fgets( temp, 132, video );
198 fgets( temp, 132, video );
199 count += sscanf( temp, "%d %d", width, height );
200 fgets( temp, 132, video );
201 }
202 return count;
203 }
204
205 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
206 {
207 producer_ppm this = producer->child;
208 int width;
209 int height;
210
211 // Construct a test frame
212 *frame = mlt_frame_init( );
213
214 // Are we at the position expected?
215 producer_ppm_position( this, mlt_producer_frame( producer ) );
216
217 // Get the frames properties
218 mlt_properties properties = mlt_frame_properties( *frame );
219
220 FILE *video = this->video;
221 FILE *audio = this->audio;
222
223 // Read the video
224 if ( video != NULL && read_ppm_header( video, &width, &height ) == 2 )
225 {
226
227 // Allocate an image
228 // IRRIGATE ME
229 uint8_t *image = malloc( width * ( height + 1 ) * 3 );
230
231 // Read it
232 fread( image, width * height * 3, 1, video );
233
234 // Pass the data on the frame properties
235 mlt_properties_set_data( properties, "image", image, width * ( height + 1 ) * 3, free, NULL );
236 mlt_properties_set_int( properties, "width", width );
237 mlt_properties_set_int( properties, "height", height );
238 mlt_properties_set_int( properties, "has_image", 1 );
239 mlt_properties_set_int( properties, "progressive", 1 );
240 mlt_properties_set_double( properties, "aspect_ratio", ( double )width / height );
241
242 // Push the image callback
243 mlt_frame_push_get_image( *frame, producer_get_image );
244 }
245 else
246 {
247 // Push the image callback
248 mlt_frame_push_get_image( *frame, producer_get_image );
249 }
250
251 // Set the audio pipe
252 mlt_properties_set_data( properties, "audio.pipe", audio, 0, NULL, NULL );
253
254 // Hmm - register audio callback
255 ( *frame )->get_audio = producer_get_audio;
256
257 // Update timecode on the frame we're creating
258 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
259
260 // Calculate the next timecode
261 mlt_producer_prepare_next( producer );
262
263 return 0;
264 }
265
266 static void producer_close( mlt_producer parent )
267 {
268 producer_ppm this = parent->child;
269 if ( this->video )
270 pclose( this->video );
271 if ( this->audio )
272 pclose( this->audio );
273 free( this->command );
274 parent->close = NULL;
275 mlt_producer_close( parent );
276 free( this );
277 }
278