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 = 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 )
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 );
92 else if ( *format == mlt_image_rgb24 )
99 mlt_frame_get_image( this, buffer, format, width, height, writable );
105 FILE *producer_ppm_run_video( producer_ppm this )
107 if ( this->video == NULL )
109 if ( this->command == NULL )
111 this->video = popen( "image2raw -k -r 25 -ppm /usr/share/pixmaps/*.png", "r" );
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" );
125 FILE *producer_ppm_run_audio( producer_ppm this )
127 if ( this->audio == NULL )
129 if ( this->command != NULL )
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" );
140 static void producer_ppm_position( producer_ppm this, uint64_t requested )
142 if ( requested != this->expected )
144 if ( this->video != NULL )
145 pclose( this->video );
147 if ( this->audio != NULL )
148 pclose( this->audio );
152 // This is the next frame we expect
153 this->expected = mlt_producer_frame( &this->parent ) + 1;
156 this->video = producer_ppm_run_video( this );
158 // Open the audio pipe
159 this->audio = producer_ppm_run_audio( this );
163 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
165 // Get the frames properties
166 mlt_properties properties = mlt_frame_properties( this );
168 FILE *pipe = mlt_properties_get_data( properties, "audio.pipe", NULL );
175 int size = *samples * *channels * 2;
178 *buffer = malloc( size );
182 fread( *buffer, size, 1, pipe );
184 memset( *buffer, 0, size );
186 // Pass the data on the frame properties
187 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
192 static int read_ppm_header( FILE *video, int *width, int *height )
197 fgets( temp, 132, video );
198 fgets( temp, 132, video );
199 count += sscanf( temp, "%d %d", width, height );
200 fgets( temp, 132, video );
205 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
207 producer_ppm this = producer->child;
211 // Construct a test frame
212 *frame = mlt_frame_init( );
214 // Are we at the position expected?
215 producer_ppm_position( this, mlt_producer_frame( producer ) );
217 // Get the frames properties
218 mlt_properties properties = mlt_frame_properties( *frame );
220 FILE *video = this->video;
221 FILE *audio = this->audio;
224 if ( video != NULL && read_ppm_header( video, &width, &height ) == 2 )
229 uint8_t *image = malloc( width * ( height + 1 ) * 3 );
232 fread( image, width * height * 3, 1, video );
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 );
242 // Push the image callback
243 mlt_frame_push_get_image( *frame, producer_get_image );
247 // Push the image callback
248 mlt_frame_push_get_image( *frame, producer_get_image );
251 // Set the audio pipe
252 mlt_properties_set_data( properties, "audio.pipe", audio, 0, NULL, NULL );
254 // Hmm - register audio callback
255 ( *frame )->get_audio = producer_get_audio;
257 // Update timecode on the frame we're creating
258 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
260 // Calculate the next timecode
261 mlt_producer_prepare_next( producer );
266 static void producer_close( mlt_producer parent )
268 producer_ppm this = parent->child;
270 pclose( this->video );
272 pclose( this->audio );
273 free( this->command );
274 parent->close = NULL;
275 mlt_producer_close( parent );