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