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