Memory pooling
[melted] / src / modules / dv / producer_libdv.c
1 /*
2 * producer_libdv.c -- simple libdv 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_libdv.h"
22 #include <framework/mlt_frame.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <libdv/dv.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 typedef struct producer_libdv_s *producer_libdv;
33
34 struct producer_libdv_s
35 {
36 struct mlt_producer_s parent;
37 int fd;
38 dv_decoder_t *dv_decoder;
39 int is_pal;
40 uint64_t file_size;
41 int frame_size;
42 long frames_in_file;
43 };
44
45 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
46 static void producer_close( mlt_producer parent );
47
48 static int producer_collect_info( producer_libdv this );
49
50 mlt_producer producer_libdv_init( char *filename )
51 {
52 producer_libdv this = calloc( sizeof( struct producer_libdv_s ), 1 );
53
54 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
55 {
56 mlt_producer producer = &this->parent;
57 mlt_properties properties = mlt_producer_properties( producer );
58
59 // Register transport implementation with the producer
60 producer->close = producer_close;
61
62 // Register our get_frame implementation with the producer
63 producer->get_frame = producer_get_frame;
64
65 // Create the dv_decoder
66 this->dv_decoder = dv_decoder_new( FALSE, FALSE, FALSE );
67 this->dv_decoder->quality = DV_QUALITY_BEST;
68 this->dv_decoder->audio->arg_audio_emphasis = 2;
69 dv_set_audio_correction( this->dv_decoder, DV_AUDIO_CORRECT_AVERAGE );
70
71 // Open the file if specified
72 this->fd = open( filename, O_RDONLY );
73
74 // Collect info
75 if ( this->fd != -1 && producer_collect_info( this ) )
76 {
77 // Set the resource property (required for all producers)
78 mlt_properties_set( properties, "resource", filename );
79 }
80 else
81 {
82 // Reject this file
83 mlt_producer_close( producer );
84 producer = NULL;
85 }
86
87 // Return the producer
88 return producer;
89 }
90 free( this );
91 return NULL;
92 }
93
94 static int read_frame( int fd, uint8_t* frame_buf, int *isPAL )
95 {
96 int result = read( fd, frame_buf, frame_size_525_60 ) == frame_size_525_60;
97 if ( result )
98 {
99 *isPAL = ( frame_buf[3] & 0x80 );
100
101 if ( *isPAL )
102 {
103 int diff = frame_size_625_50 - frame_size_525_60;
104 result = read( fd, frame_buf + frame_size_525_60, diff ) == diff;
105 }
106 }
107
108 return result;
109 }
110
111 static int producer_collect_info( producer_libdv this )
112 {
113 int valid = 0;
114
115 void *release = NULL;
116 uint8_t *dv_data = mlt_pool_allocate( frame_size_625_50, &release );
117
118 if ( dv_data != NULL )
119 {
120 // Read the first frame
121 valid = read_frame( this->fd, dv_data, &this->is_pal );
122
123 // If it looks like a valid frame, the get stats
124 if ( valid )
125 {
126 // Get the properties
127 mlt_properties properties = mlt_producer_properties( &this->parent );
128
129 // Determine the file size
130 struct stat buf;
131 fstat( this->fd, &buf );
132
133 // Store the file size
134 this->file_size = buf.st_size;
135
136 // Determine the frame size
137 this->frame_size = this->is_pal ? frame_size_625_50 : frame_size_525_60;
138
139 // Determine the number of frames in the file
140 this->frames_in_file = this->file_size / this->frame_size;
141
142 // Calculate default in/out points
143 double fps = this->is_pal ? 25 : 30000.0 / 1001.0;
144 if ( mlt_properties_get_double( properties, "fps" ) == fps )
145 {
146 mlt_properties_set_position( properties, "length", this->frames_in_file );
147 mlt_properties_set_position( properties, "in", 0 );
148 mlt_properties_set_position( properties, "out", this->frames_in_file - 1 );
149 }
150 else
151 {
152 valid = 0;
153 }
154
155 // Parse the header for meta info
156 dv_parse_header( this->dv_decoder, dv_data );
157 mlt_properties_set_double( properties, "aspect_ratio", dv_format_wide( this->dv_decoder ) ? 16.0/9.0 : 4.0/3.0 );
158 }
159
160 mlt_pool_release( release );
161 }
162
163 return valid;
164 }
165
166 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
167 {
168 int pitches[3] = { 0, 0, 0 };
169 uint8_t *pixels[3] = { NULL, NULL, NULL };
170
171 // Get the frames properties
172 mlt_properties properties = mlt_frame_properties( this );
173
174 // Get the dv decoder
175 dv_decoder_t *decoder = mlt_properties_get_data( properties, "dv_decoder", NULL );
176
177 // Get the dv data
178 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
179
180 // Parse the header for meta info
181 dv_parse_header( decoder, dv_data );
182
183 // Assign width and height from properties
184 *width = mlt_properties_get_int( properties, "width" );
185 *height = mlt_properties_get_int( properties, "height" );
186
187 // Extract an image of the format requested
188 if ( *format == mlt_image_yuv422 )
189 {
190 // Allocate an image
191 void *release = NULL;
192 uint8_t *image = mlt_pool_allocate( *width * ( *height + 1 ) * 2, &release );
193
194 // Pass to properties for clean up
195 mlt_properties_set_data( properties, "image_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
196 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 2, NULL, NULL );
197
198 // Decode the image
199 pitches[ 0 ] = *width * 2;
200 pixels[ 0 ] = image;
201 dv_decode_full_frame( decoder, dv_data, e_dv_color_yuv, pixels, pitches );
202
203 // Assign result
204 *buffer = image;
205 }
206 else if ( *format == mlt_image_rgb24 )
207 {
208 // Allocate an image
209 void *release = NULL;
210 uint8_t *image = mlt_pool_allocate( *width * ( *height + 1 ) * 3, &release );
211
212 // Pass to properties for clean up
213 mlt_properties_set_data( properties, "image_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
214 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 3, NULL, NULL );
215
216 // Decode the frame
217 pitches[ 0 ] = 720 * 3;
218 pixels[ 0 ] = image;
219 dv_decode_full_frame( decoder, dv_data, e_dv_color_rgb, pixels, pitches );
220
221 // Assign result
222 *buffer = image;
223 }
224
225 return 0;
226 }
227
228 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
229 {
230 void *release = NULL;
231 int16_t *p;
232 int i, j;
233 void *audio_release[ 4 ] = { NULL, NULL, NULL, NULL };
234 int16_t *audio_channels[ 4 ];
235
236 // Get the frames properties
237 mlt_properties properties = mlt_frame_properties( this );
238
239 // Get the dv decoder
240 dv_decoder_t *decoder = mlt_properties_get_data( properties, "dv_decoder", NULL );
241
242 // Get the dv data
243 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
244
245 // Parse the header for meta info
246 dv_parse_header( decoder, dv_data );
247
248 // Obtain required values
249 //fprintf( stderr, "libdv: frequency %d\n", decoder->audio->frequency );
250 *frequency = decoder->audio->frequency;
251 *samples = decoder->audio->samples_this_frame;
252 *channels = decoder->audio->num_channels;
253
254 // Create a temporary workspace
255 for ( i = 0; i < 4; i++ )
256 audio_channels[ i ] = mlt_pool_allocate( DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), &audio_release[ i ] );
257
258 // Create a workspace for the result
259 *buffer = mlt_pool_allocate( *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), &release );
260
261 // Pass the allocated audio buffer as a property
262 mlt_properties_set_data( properties, "audio_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
263 mlt_properties_set_data( properties, "audio", *buffer, *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), NULL, NULL );
264
265 // Decode the audio
266 dv_decode_full_audio( decoder, dv_data, audio_channels );
267
268 // Interleave the audio
269 p = *buffer;
270 for ( i = 0; i < *samples; i++ )
271 for ( j = 0; j < *channels; j++ )
272 *p++ = audio_channels[ j ][ i ];
273
274 // Free the temporary work space
275 for ( i = 0; i < 4; i++ )
276 mlt_pool_release( audio_release[ i ] );
277
278 return 0;
279 }
280
281 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
282 {
283 producer_libdv this = producer->child;
284 void *release = NULL;
285 uint8_t *data = mlt_pool_allocate( frame_size_625_50, &release );
286
287 // Obtain the current frame number
288 uint64_t position = mlt_producer_frame( producer );
289
290 // Convert timecode to a file position (ensuring that we're on a frame boundary)
291 uint64_t offset = position * this->frame_size;
292
293 // Create an empty frame
294 *frame = mlt_frame_init( );
295
296 // Seek and fetch
297 if ( this->fd != 0 &&
298 lseek( this->fd, offset, SEEK_SET ) == offset &&
299 read_frame( this->fd, data, &this->is_pal ) )
300 {
301 // Get the frames properties
302 mlt_properties properties = mlt_frame_properties( *frame );
303
304 // Pass the dv decoder
305 mlt_properties_set_data( properties, "dv_decoder", this->dv_decoder, 0, NULL, NULL );
306
307 // Pass the dv data
308 mlt_properties_set_data( properties, "dv_data_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
309 mlt_properties_set_data( properties, "dv_data", data, frame_size_625_50, NULL, NULL );
310
311 // Update other info on the frame
312 mlt_properties_set_int( properties, "width", 720 );
313 mlt_properties_set_int( properties, "height", this->is_pal ? 576 : 480 );
314 mlt_properties_set_int( properties, "top_field_first", 0 );
315
316 // Parse the header for meta info
317 dv_parse_header( this->dv_decoder, data );
318 mlt_properties_set_int( properties, "progressive", dv_is_progressive( this->dv_decoder ) );
319 mlt_properties_set_double( properties, "aspect_ratio", dv_format_wide( this->dv_decoder ) ? 16.0/9.0 : 4.0/3.0 );
320
321 // Hmm - register audio callback
322 ( *frame )->get_audio = producer_get_audio;
323
324 // Push the get_image method on to the stack
325 mlt_frame_push_get_image( *frame, producer_get_image );
326 }
327 else
328 {
329 mlt_pool_release( release );
330 }
331
332 // Update timecode on the frame we're creating
333 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
334
335 // Calculate the next timecode
336 mlt_producer_prepare_next( producer );
337
338 return 0;
339 }
340
341 static void producer_close( mlt_producer parent )
342 {
343 // Obtain this
344 producer_libdv this = parent->child;
345
346 // Free the dv deconder
347 //dv_decoder_free( this->dv_decoder );
348
349 // Close the file
350 if ( this->fd > 0 )
351 close( this->fd );
352
353 // Close the parent
354 parent->close = NULL;
355 mlt_producer_close( parent );
356
357 // Free the memory
358 free( this );
359 }
360