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