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 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.
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.
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.
21 #include "producer_ppm.h"
23 #include <framework/mlt_frame.h>
28 typedef struct producer_ppm_s *producer_ppm;
32 struct mlt_producer_s parent;
39 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
40 static void producer_close( mlt_producer parent );
42 mlt_producer producer_ppm_init( void *command )
44 producer_ppm this = calloc( sizeof( struct producer_ppm_s ), 1 );
45 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
47 mlt_producer producer = &this->parent;
48 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
50 producer->get_frame = producer_get_frame;
51 producer->close = ( mlt_destructor )producer_close;
53 if ( command != NULL )
55 mlt_properties_set( properties, "resource", command );
56 this->command = strdup( command );
60 mlt_properties_set( properties, "resource", "ppm test" );
69 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
71 // Get the frames properties
72 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
74 if ( mlt_properties_get_int( properties, "has_image" ) )
77 uint8_t *rgb = mlt_properties_get_data( properties, "image", NULL );
79 // Get width and height
80 *width = mlt_properties_get_int( properties, "width" );
81 *height = mlt_properties_get_int( properties, "height" );
83 // Convert to requested format
84 if ( *format == mlt_image_yuv422 )
86 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 2 );
87 mlt_convert_rgb24_to_yuv422( rgb, *width, *height, *width * 3, image );
88 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
91 else if ( *format == mlt_image_rgb24 )
98 mlt_frame_get_image( this, buffer, format, width, height, writable );
104 FILE *producer_ppm_run_video( producer_ppm this )
106 if ( this->video == NULL )
108 if ( this->command == NULL )
110 this->video = popen( "image2raw -k -r 25 -ppm /usr/share/pixmaps/*.png", "r" );
114 char command[ 1024 ];
115 float fps = mlt_producer_get_fps( &this->parent );
116 float position = mlt_producer_position( &this->parent );
117 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f imagepipe -r %f -img ppm - 2>/dev/null", this->command, position, fps );
118 this->video = popen( command, "r" );
124 FILE *producer_ppm_run_audio( producer_ppm this )
126 if ( this->audio == NULL )
128 if ( this->command != NULL )
130 char command[ 1024 ];
131 float position = mlt_producer_position( &this->parent );
132 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f s16le -ar 48000 -ac 2 - 2>/dev/null", this->command, position );
133 this->audio = popen( command, "r" );
139 static void producer_ppm_position( producer_ppm this, uint64_t requested )
141 if ( requested != this->expected )
143 if ( this->video != NULL )
144 pclose( this->video );
146 if ( this->audio != NULL )
147 pclose( this->audio );
151 // This is the next frame we expect
152 this->expected = mlt_producer_frame( &this->parent ) + 1;
155 this->video = producer_ppm_run_video( this );
157 // Open the audio pipe
158 this->audio = producer_ppm_run_audio( this );
162 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
164 // Get the frames properties
165 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
167 FILE *pipe = mlt_properties_get_data( properties, "audio.pipe", NULL );
174 int size = *samples * *channels * 2;
177 *buffer = malloc( size );
181 fread( *buffer, size, 1, pipe );
183 memset( *buffer, 0, size );
185 // Pass the data on the frame properties
186 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
191 static int read_ppm_header( FILE *video, int *width, int *height )
196 fgets( temp, 132, video );
197 fgets( temp, 132, video );
198 count += sscanf( temp, "%d %d", width, height );
199 fgets( temp, 132, video );
204 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
206 producer_ppm this = producer->child;
210 // Construct a test frame
211 *frame = mlt_frame_init( );
213 // Are we at the position expected?
214 producer_ppm_position( this, mlt_producer_frame( producer ) );
216 // Get the frames properties
217 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
219 FILE *video = this->video;
220 FILE *audio = this->audio;
223 if ( video != NULL && read_ppm_header( video, &width, &height ) == 2 )
226 uint8_t *image = mlt_pool_alloc( width * ( height + 1 ) * 3 );
229 fread( image, width * height * 3, 1, video );
231 // Pass the data on the frame properties
232 mlt_properties_set_data( properties, "image", image, width * ( height + 1 ) * 3, ( mlt_destructor )mlt_pool_release, NULL );
233 mlt_properties_set_int( properties, "width", width );
234 mlt_properties_set_int( properties, "height", height );
235 mlt_properties_set_int( properties, "has_image", 1 );
236 mlt_properties_set_int( properties, "progressive", 1 );
237 mlt_properties_set_double( properties, "aspect_ratio", 1 );
239 // Push the image callback
240 mlt_frame_push_get_image( *frame, producer_get_image );
244 // Push the image callback
245 mlt_frame_push_get_image( *frame, producer_get_image );
248 // Set the audio pipe
249 mlt_properties_set_data( properties, "audio.pipe", audio, 0, NULL, NULL );
251 // Hmm - register audio callback
252 mlt_frame_push_audio( *frame, producer_get_audio );
254 // Update timecode on the frame we're creating
255 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
257 // Calculate the next timecode
258 mlt_producer_prepare_next( producer );
263 static void producer_close( mlt_producer parent )
265 producer_ppm this = parent->child;
267 pclose( this->video );
269 pclose( this->audio );
270 free( this->command );
271 parent->close = NULL;
272 mlt_producer_close( parent );