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