2 * producer_ppm.c -- simple ppm test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
27 typedef struct producer_ppm_s *producer_ppm;
31 struct mlt_producer_s parent;
38 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
39 static void producer_close( mlt_producer parent );
41 mlt_producer producer_ppm_init( mlt_profile profile, mlt_service_type type, const char *id, char *command )
43 producer_ppm this = calloc( sizeof( struct producer_ppm_s ), 1 );
44 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
46 mlt_producer producer = &this->parent;
47 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
49 producer->get_frame = producer_get_frame;
50 producer->close = ( mlt_destructor )producer_close;
52 if ( command != NULL )
54 mlt_properties_set( properties, "resource", command );
55 this->command = strdup( command );
59 mlt_properties_set( properties, "resource", "ppm test" );
68 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
70 // Get the frames properties
71 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
73 if ( mlt_properties_get_int( properties, "has_image" ) )
76 *buffer = mlt_properties_get_data( properties, "image", NULL );
77 *width = mlt_properties_get_int( properties, "width" );
78 *height = mlt_properties_get_int( properties, "height" );
79 *format = mlt_image_rgb24;
83 mlt_frame_get_image( this, buffer, format, width, height, writable );
89 FILE *producer_ppm_run_video( producer_ppm this )
91 if ( this->video == NULL )
93 if ( this->command == NULL )
95 this->video = popen( "image2raw -k -r 25 -ppm /usr/share/pixmaps/*.png", "r" );
100 float fps = mlt_producer_get_fps( &this->parent );
101 float position = mlt_producer_position( &this->parent );
102 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f image2pipe -r %f -vcodec ppm - 2>/dev/null", this->command, position, fps );
103 this->video = popen( command, "r" );
109 FILE *producer_ppm_run_audio( producer_ppm this )
111 if ( this->audio == NULL )
113 if ( this->command != NULL )
115 char command[ 1024 ];
116 float position = mlt_producer_position( &this->parent );
117 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f s16le -ar 48000 -ac 2 - 2>/dev/null", this->command, position );
118 this->audio = popen( command, "r" );
124 static void producer_ppm_position( producer_ppm this, uint64_t requested )
126 if ( requested != this->expected )
128 if ( this->video != NULL )
129 pclose( this->video );
131 if ( this->audio != NULL )
132 pclose( this->audio );
136 // This is the next frame we expect
137 this->expected = mlt_producer_frame( &this->parent ) + 1;
140 this->video = producer_ppm_run_video( this );
142 // Open the audio pipe
143 this->audio = producer_ppm_run_audio( this );
147 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
149 // Get the frames properties
150 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
152 FILE *pipe = mlt_properties_get_data( properties, "audio.pipe", NULL );
159 int size = *samples * *channels * 2;
162 *buffer = malloc( size );
166 fread( *buffer, size, 1, pipe );
168 memset( *buffer, 0, size );
170 // Pass the data on the frame properties
171 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
176 static int read_ppm_header( FILE *video, int *width, int *height )
181 fgets( temp, 132, video );
182 fgets( temp, 132, video );
183 count += sscanf( temp, "%d %d", width, height );
184 fgets( temp, 132, video );
189 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
191 producer_ppm this = producer->child;
195 // Construct a test frame
196 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
198 // Are we at the position expected?
199 producer_ppm_position( this, mlt_producer_frame( producer ) );
201 // Get the frames properties
202 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
204 FILE *video = this->video;
205 FILE *audio = this->audio;
208 if ( video != NULL && read_ppm_header( video, &width, &height ) == 2 )
211 uint8_t *image = mlt_pool_alloc( width * ( height + 1 ) * 3 );
214 fread( image, width * height * 3, 1, video );
216 // Pass the data on the frame properties
217 mlt_properties_set_data( properties, "image", image, width * ( height + 1 ) * 3, ( mlt_destructor )mlt_pool_release, NULL );
218 mlt_properties_set_int( properties, "width", width );
219 mlt_properties_set_int( properties, "height", height );
220 mlt_properties_set_int( properties, "has_image", 1 );
221 mlt_properties_set_int( properties, "progressive", 1 );
222 mlt_properties_set_double( properties, "aspect_ratio", 1 );
224 // Push the image callback
225 mlt_frame_push_get_image( *frame, producer_get_image );
229 // Push the image callback
230 mlt_frame_push_get_image( *frame, producer_get_image );
233 // Set the audio pipe
234 mlt_properties_set_data( properties, "audio.pipe", audio, 0, NULL, NULL );
236 // Hmm - register audio callback
237 mlt_frame_push_audio( *frame, producer_get_audio );
239 // Update timecode on the frame we're creating
240 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
242 // Calculate the next timecode
243 mlt_producer_prepare_next( producer );
248 static void producer_close( mlt_producer parent )
250 producer_ppm this = parent->child;
252 pclose( this->video );
254 pclose( this->audio );
255 free( this->command );
256 parent->close = NULL;
257 mlt_producer_close( parent );