]> git.sesse.net Git - mlt/blob - src/modules/core/producer_consumer.c
Massive refactoring of image conversion.
[mlt] / src / modules / core / producer_consumer.c
1 /*
2  * producer_consumer.c -- produce as a consumer of an encapsulated producer
3  * Copyright (C) 2008 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
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.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27
28 struct context_s {
29         mlt_producer this;
30         mlt_producer producer;
31         mlt_consumer consumer;
32         mlt_profile profile;
33         int is_close_profile;
34 };
35 typedef struct context_s *context; 
36
37
38 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
39 {
40         context cx = mlt_frame_pop_service( frame );
41         mlt_frame nested_frame = mlt_frame_pop_service( frame );
42
43         *width = cx->profile->width;
44         *height = cx->profile->height;
45
46         int result = mlt_frame_get_image( nested_frame, image, format, width, height, writable );
47
48         // Allocate the image
49         int size = *width * *height * ( *format == mlt_image_yuv422 ? 2 : *format == mlt_image_rgb24 ? 3 :
50                 ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) ? 4 : ( 3 / 2 ) );
51         uint8_t *new_image = mlt_pool_alloc( size );
52
53         // Update the frame
54         mlt_properties properties = mlt_frame_properties( frame );
55         mlt_properties_set_data( properties, "image", new_image, size, mlt_pool_release, NULL );
56         memcpy( new_image, *image, size );
57         mlt_frame_close( nested_frame );
58         *image = new_image;
59
60 //      mlt_properties_debug( properties, "frame", stderr );
61 //      mlt_properties_debug( mlt_frame_properties( nested_frame ), "nested_frame", stderr );
62
63         return result;
64 }
65
66 static int get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
67 {
68         mlt_frame nested_frame = mlt_frame_pop_audio( frame );
69         int result = mlt_frame_get_audio( nested_frame, buffer, format, frequency, channels, samples );
70         int size = *channels * *samples * sizeof( int16_t );
71         int16_t *new_buffer = mlt_pool_alloc( size );
72         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "audio", new_buffer, size, mlt_pool_release, NULL );
73         memcpy( new_buffer, *buffer, size );
74         *buffer = new_buffer;
75         mlt_frame_close( nested_frame );
76         return result;
77 }
78
79 static int get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
80 {
81         mlt_properties properties = MLT_PRODUCER_PROPERTIES(this);
82         context cx = mlt_properties_get_data( properties, "context", NULL );
83
84         if ( !cx )
85         {
86                 // Allocate and initialize our context
87                 cx = mlt_pool_alloc( sizeof( struct context_s ) );
88                 mlt_properties_set_data( properties, "context", cx, 0, mlt_pool_release, NULL );
89                 cx->this = this;
90                 char *profile_name = mlt_properties_get( properties, "profile" );
91                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
92
93                 if ( profile_name )
94                 {
95                         cx->profile = mlt_profile_init( profile_name );
96                         cx->is_close_profile = 1;
97                 }
98                 else
99                 {
100                         cx->profile = profile;
101                         cx->is_close_profile = 0;
102                 }
103
104                 // For now, we must conform the nested network's frame rate to the parent network's
105                 // framerate.
106                 cx->profile->frame_rate_num = profile->frame_rate_num;
107                 cx->profile->frame_rate_den = profile->frame_rate_den;
108
109                 // We will encapsulate a consumer
110                 cx->consumer = mlt_consumer_new( cx->profile );
111                 // Do not use _pass_list on real_time so that it defaults to 0 in the absence of
112                 // an explicit real_time property.
113                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( cx->consumer ), "real_time",
114                         mlt_properties_get_int( properties, "real_time" ) );
115                 mlt_properties_pass_list( MLT_CONSUMER_PROPERTIES( cx->consumer ), properties,
116                         "buffer, prefill" );
117         
118                 // Encapsulate a real producer for the resource
119                 cx->producer = mlt_factory_producer( cx->profile, NULL,
120                         mlt_properties_get( properties, "resource" ) );
121                 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( cx->producer ),
122                         "out, length" );
123
124                 // Since we control the seeking, prevent it from seeking on its own
125                 mlt_producer_set_speed( cx->producer, 0 );
126
127                 // Connect it all together
128                 mlt_consumer_connect( cx->consumer, MLT_PRODUCER_SERVICE( cx->producer ) );
129                 mlt_consumer_start( cx->consumer );
130         }
131
132         // Generate a frame
133         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
134         if ( frame )
135         {
136                 // Our "in" needs to be the same, keep it so
137                 mlt_properties_pass_list( MLT_PRODUCER_PROPERTIES( cx->producer ), properties, "in, out" );
138
139                 // Seek the producer to the correct place
140                 // Calculate our positions
141                 double actual_position = mlt_producer_get_speed( this ) * (double)mlt_producer_position( this );
142                 mlt_position need_first = floor( actual_position );
143                 mlt_producer_seek( cx->producer, need_first );
144                 
145                 // Get the nested frame
146                 mlt_frame nested_frame = mlt_consumer_rt_frame( cx->consumer );
147
148                 // Stack the producer and our methods on the nested frame
149                 mlt_frame_push_service( *frame, nested_frame );
150                 mlt_frame_push_service( *frame, cx );
151                 mlt_frame_push_get_image( *frame, get_image );
152                 mlt_frame_push_audio( *frame, nested_frame );
153                 mlt_frame_push_audio( *frame, get_audio );
154                 
155                 // Give the returned frame temporal identity
156                 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
157                 
158                 // Put additional references on the frame so both get_image and get_audio
159                 // methods can close it.
160                 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( nested_frame ) );
161
162                 // Inform the normalizers about our video properties
163                 mlt_properties frame_props = MLT_FRAME_PROPERTIES( *frame );
164                 mlt_properties_set_double( frame_props, "aspect_ratio", mlt_profile_sar( cx->profile ) );
165                 mlt_properties_set_int( frame_props, "width", cx->profile->width );
166                 mlt_properties_set_int( frame_props, "height", cx->profile->height );
167                 mlt_properties_set_int( frame_props, "real_width", cx->profile->width );
168                 mlt_properties_set_int( frame_props, "real_height", cx->profile->height );
169                 mlt_properties_set_int( frame_props, "progressive", cx->profile->progressive );
170         }
171
172         // Calculate the next timecode
173         mlt_producer_prepare_next( this );
174
175         return 0;
176 }
177
178 static void producer_close( mlt_producer this )
179 {
180         context cx = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( this ), "context", NULL );
181         
182         // Shut down all the encapsulated services
183         if ( cx )
184         {
185                 mlt_consumer_stop( cx->consumer );
186                 mlt_consumer_close( cx->consumer );
187                 mlt_producer_close( cx->producer );
188                 if ( cx->is_close_profile )
189                         mlt_profile_close( cx->profile );
190         }
191         
192         this->close = NULL;
193         mlt_producer_close( this );
194         free( this );
195 }
196
197 mlt_producer producer_consumer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
198 {
199         mlt_producer this = mlt_producer_new( );
200
201         // Encapsulate the real producer
202         mlt_producer real_producer = mlt_factory_producer( profile, NULL, arg );
203
204         if ( this && real_producer )
205         {
206                 // Override some producer methods
207                 this->close = ( mlt_destructor )producer_close;
208                 this->get_frame = get_frame;
209                 
210                 // Get the properties of this producer
211                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
212                 mlt_properties_set( properties, "resource", arg );
213                 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "out, length" );
214
215                 // Done with the producer - will re-open later when we have the profile property
216                 mlt_producer_close( real_producer );
217         }
218         else
219         {
220                 if ( this )
221                         mlt_producer_close( this );
222                 if ( real_producer )
223                         mlt_producer_close( real_producer );
224
225                 this = NULL;
226         }
227         return this;
228 }