]> git.sesse.net Git - mlt/blob - src/modules/core/producer_ppm.c
framework: remove global profile, rather share one mlt_profile across a service netwo...
[mlt] / 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 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.
10  *
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.
15  *
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
19  */
20
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 typedef struct producer_ppm_s *producer_ppm;
28
29 struct producer_ppm_s
30 {
31         struct mlt_producer_s parent;
32         char *command;
33         FILE *video;
34         FILE *audio;
35         uint64_t expected;
36 };
37
38 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
39 static void producer_close( mlt_producer parent );
40
41 mlt_producer producer_ppm_init( mlt_profile profile, mlt_service_type type, const char *id, char *command )
42 {
43         producer_ppm this = calloc( sizeof( struct producer_ppm_s ), 1 );
44         if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
45         {
46                 mlt_producer producer = &this->parent;
47                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
48
49                 producer->get_frame = producer_get_frame;
50                 producer->close = ( mlt_destructor )producer_close;
51
52                 if ( command != NULL )
53                 {
54                         mlt_properties_set( properties, "resource", command );
55                         this->command = strdup( command );
56                 }
57                 else
58                 {
59                         mlt_properties_set( properties, "resource", "ppm test" );
60                 }
61
62                 return producer;
63         }
64         free( this );
65         return NULL;
66 }
67
68 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
69 {
70         // Get the frames properties
71         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
72
73         if ( mlt_properties_get_int( properties, "has_image" ) )
74         {
75                 // Get the RGB image
76                 uint8_t *rgb = mlt_properties_get_data( properties, "image", NULL );
77
78                 // Get width and height
79                 *width = mlt_properties_get_int( properties, "width" );
80                 *height = mlt_properties_get_int( properties, "height" );
81
82                 // Convert to requested format
83                 if ( *format == mlt_image_yuv422 )
84                 {
85                         uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 2 );
86                         mlt_convert_rgb24_to_yuv422( rgb, *width, *height, *width * 3, image );
87                         mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
88                         *buffer = image;
89                 }
90                 else if ( *format == mlt_image_rgb24 )
91                 {
92                         *buffer = rgb;
93                 }
94         }
95         else
96         {
97                 mlt_frame_get_image( this, buffer, format, width, height, writable );
98         }
99
100         return 0;
101 }
102
103 FILE *producer_ppm_run_video( producer_ppm this )
104 {
105         if ( this->video == NULL )
106         {
107                 if ( this->command == NULL )
108                 {
109                         this->video = popen( "image2raw -k -r 25 -ppm /usr/share/pixmaps/*.png", "r" );
110                 }
111                 else
112                 {
113                         char command[ 1024 ];
114                         float fps = mlt_producer_get_fps( &this->parent );
115                         float position = mlt_producer_position( &this->parent );
116                         sprintf( command, "ffmpeg -i \"%s\" -ss %f -f imagepipe -r %f -img ppm - 2>/dev/null", this->command, position, fps );
117                         this->video = popen( command, "r" );
118                 }
119         }
120         return this->video;
121 }
122
123 FILE *producer_ppm_run_audio( producer_ppm this )
124 {
125         if ( this->audio == NULL )
126         {
127                 if ( this->command != NULL )
128                 {
129                         char command[ 1024 ];
130                         float position = mlt_producer_position( &this->parent );
131                         sprintf( command, "ffmpeg -i \"%s\" -ss %f -f s16le -ar 48000 -ac 2 - 2>/dev/null", this->command, position );
132                         this->audio = popen( command, "r" );
133                 }
134         }
135         return this->audio;
136 }
137
138 static void producer_ppm_position( producer_ppm this, uint64_t requested )
139 {
140         if ( requested != this->expected )
141         {
142                 if ( this->video != NULL )
143                         pclose( this->video );
144                 this->video = NULL;
145                 if ( this->audio != NULL )
146                         pclose( this->audio );
147                 this->audio = NULL;
148         }
149
150         // This is the next frame we expect
151         this->expected = mlt_producer_frame( &this->parent ) + 1;
152
153         // Open the pipe
154         this->video = producer_ppm_run_video( this );
155
156         // Open the audio pipe
157         this->audio = producer_ppm_run_audio( this );
158
159 }
160
161 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
162 {
163         // Get the frames properties
164         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
165
166         FILE *pipe = mlt_properties_get_data( properties, "audio.pipe", NULL );
167
168         *frequency = 48000;
169         *channels = 2;
170         *samples = 1920;
171
172         // Size
173         int size = *samples * *channels * 2;
174
175         // Allocate an image
176         *buffer = malloc( size );
177                 
178         // Read it
179         if ( pipe != NULL )
180                 fread( *buffer, size, 1, pipe );
181         else
182                 memset( *buffer, 0, size );
183
184         // Pass the data on the frame properties
185         mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
186
187         return 0;
188 }
189
190 static int read_ppm_header( FILE *video, int *width, int *height )
191 {
192         int count = 0;
193         {
194                 char temp[ 132 ];
195                 fgets( temp, 132, video );
196                 fgets( temp, 132, video );
197                 count += sscanf( temp, "%d %d", width, height );
198                 fgets( temp, 132, video );
199         }
200         return count;
201 }
202
203 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
204 {
205         producer_ppm this = producer->child;
206         int width;
207         int height;
208
209         // Construct a test frame
210         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
211
212         // Are we at the position expected?
213         producer_ppm_position( this, mlt_producer_frame( producer ) );
214
215         // Get the frames properties
216         mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
217
218         FILE *video = this->video;
219         FILE *audio = this->audio;
220
221         // Read the video
222         if ( video != NULL && read_ppm_header( video, &width, &height ) == 2 )
223         {
224                 // Allocate an image
225                 uint8_t *image = mlt_pool_alloc( width * ( height + 1 ) * 3 );
226                 
227                 // Read it
228                 fread( image, width * height * 3, 1, video );
229
230                 // Pass the data on the frame properties
231                 mlt_properties_set_data( properties, "image", image, width * ( height + 1 ) * 3, ( mlt_destructor )mlt_pool_release, NULL );
232                 mlt_properties_set_int( properties, "width", width );
233                 mlt_properties_set_int( properties, "height", height );
234                 mlt_properties_set_int( properties, "has_image", 1 );
235                 mlt_properties_set_int( properties, "progressive", 1 );
236                 mlt_properties_set_double( properties, "aspect_ratio", 1 );
237
238                 // Push the image callback
239                 mlt_frame_push_get_image( *frame, producer_get_image );
240         }
241         else
242         {
243                 // Push the image callback
244                 mlt_frame_push_get_image( *frame, producer_get_image );
245         }
246
247         // Set the audio pipe
248         mlt_properties_set_data( properties, "audio.pipe", audio, 0, NULL, NULL );
249
250         // Hmm - register audio callback
251         mlt_frame_push_audio( *frame, producer_get_audio );
252
253         // Update timecode on the frame we're creating
254         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
255
256         // Calculate the next timecode
257         mlt_producer_prepare_next( producer );
258
259         return 0;
260 }
261
262 static void producer_close( mlt_producer parent )
263 {
264         producer_ppm this = parent->child;
265         if ( this->video )
266                 pclose( this->video );
267         if ( this->audio )
268                 pclose( this->audio );
269         free( this->command );
270         parent->close = NULL;
271         mlt_producer_close( parent );
272         free( this );
273 }