]> git.sesse.net Git - mlt/blob - src/modules/dv/producer_libdv.c
pooling and properties checks; dv decoder stack; factory cleanup registering
[mlt] / 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 <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <libdv/dv.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35 /** To conserve resources, we maintain a stack of dv decoders.
36 */
37
38 static pthread_mutex_t decoder_lock = PTHREAD_MUTEX_INITIALIZER;
39 static mlt_properties dv_decoders = NULL;
40
41 dv_decoder_t *dv_decoder_alloc( )
42 {
43         // We'll return a dv_decoder
44         dv_decoder_t *this = NULL;
45
46         // Lock the mutex
47         pthread_mutex_lock( &decoder_lock );
48
49         // Create the properties if necessary
50         if ( dv_decoders == NULL )
51         {
52                 // Create the properties
53                 dv_decoders = mlt_properties_new( );
54
55                 // Create the stack
56                 mlt_properties_set_data( dv_decoders, "stack", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
57
58                 // Register the properties for clean up
59                 mlt_factory_register_for_clean_up( dv_decoders, ( mlt_destructor )mlt_properties_close );
60         }
61
62         // Now try to obtain a decoder
63         if ( dv_decoders != NULL )
64         {
65                 // Obtain the stack
66                 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
67
68                 // Pop the top of the stack
69                 this = mlt_deque_pop_back( stack );
70
71                 // Create a new decoder if none available
72                 if ( this == NULL )
73                 {
74                         // We'll need a unique property ID for this
75                         char label[ 256 ];
76
77                         // Configure the decoder
78                         this = dv_decoder_new( FALSE, FALSE, FALSE );
79                         this->quality = DV_QUALITY_COLOR | DV_QUALITY_AC_1;
80                         this->audio->arg_audio_emphasis = 2;
81                         dv_set_audio_correction( this, DV_AUDIO_CORRECT_AVERAGE );
82
83                         // Register it with the properties to ensure clean up
84                         // BUG: dv_decoder_free core dumps here
85                         sprintf( label, "%p", this );
86                         //mlt_properties_set_data( dv_decoders, label, this, 0, ( mlt_destructor )dv_decoder_free, NULL );
87                         mlt_properties_set_data( dv_decoders, label, this, 0, NULL, NULL );
88                 }
89         }
90
91         // Unlock the mutex
92         pthread_mutex_unlock( &decoder_lock );
93
94         return this;
95 }
96
97 void dv_decoder_return( dv_decoder_t *this )
98 {
99         // Lock the mutex
100         pthread_mutex_lock( &decoder_lock );
101
102         // Now try to return the decoder
103         if ( dv_decoders != NULL )
104         {
105                 // Obtain the stack
106                 mlt_deque stack = mlt_properties_get_data( dv_decoders, "stack", NULL );
107
108                 // Push it back
109                 mlt_deque_push_back( stack, this );
110         }
111
112         // Unlock the mutex
113         pthread_mutex_unlock( &decoder_lock );
114 }
115
116
117 typedef struct producer_libdv_s *producer_libdv;
118
119 struct producer_libdv_s
120 {
121         struct mlt_producer_s parent;
122         int fd;
123         int is_pal;
124         uint64_t file_size;
125         int frame_size;
126         long frames_in_file;
127 };
128
129 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
130 static void producer_close( mlt_producer parent );
131
132 static int producer_collect_info( producer_libdv this );
133
134 mlt_producer producer_libdv_init( char *filename )
135 {
136         producer_libdv this = calloc( sizeof( struct producer_libdv_s ), 1 );
137
138         if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
139         {
140                 mlt_producer producer = &this->parent;
141                 mlt_properties properties = mlt_producer_properties( producer );
142
143                 // Register transport implementation with the producer
144                 producer->close = producer_close;
145
146                 // Register our get_frame implementation with the producer
147                 producer->get_frame = producer_get_frame;
148
149                 // Open the file if specified
150                 this->fd = open( filename, O_RDONLY );
151
152                 // Collect info
153                 if ( this->fd != -1 && producer_collect_info( this ) )
154                 {
155                         // Set the resource property (required for all producers)
156                         mlt_properties_set( properties, "resource", filename );
157                 }
158                 else
159                 {
160                         // Reject this file
161                         mlt_producer_close( producer );
162                         producer = NULL;
163                 }
164
165                 // Return the producer
166                 return producer;
167         }
168         free( this );
169         return NULL;
170 }
171
172 static int read_frame( int fd, uint8_t* frame_buf, int *isPAL )
173 {
174         int result = read( fd, frame_buf, frame_size_525_60 ) == frame_size_525_60;
175         if ( result )
176         {
177                 *isPAL = ( frame_buf[3] & 0x80 );
178
179                 if ( *isPAL )
180                 {
181                         int diff = frame_size_625_50 - frame_size_525_60;
182                         result = read( fd, frame_buf + frame_size_525_60, diff ) == diff;
183                 }
184         }
185         
186         return result;
187 }
188
189 static int producer_collect_info( producer_libdv this )
190 {
191         int valid = 0;
192
193         uint8_t *dv_data = mlt_pool_alloc( frame_size_625_50 );
194
195         if ( dv_data != NULL )
196         {
197                 // Read the first frame
198                 valid = read_frame( this->fd, dv_data, &this->is_pal );
199
200                 // If it looks like a valid frame, the get stats
201                 if ( valid )
202                 {
203                         // Get the properties
204                         mlt_properties properties = mlt_producer_properties( &this->parent );
205
206                         // Get a dv_decoder
207                         dv_decoder_t *dv_decoder = dv_decoder_alloc( );
208
209                         // Determine the file size
210                         struct stat buf;
211                         fstat( this->fd, &buf );
212
213                         // Store the file size
214                         this->file_size = buf.st_size;
215
216                         // Determine the frame size
217                         this->frame_size = this->is_pal ? frame_size_625_50 : frame_size_525_60;
218
219                         // Determine the number of frames in the file
220                         this->frames_in_file = this->file_size / this->frame_size;
221
222                         // Calculate default in/out points
223                         double fps = this->is_pal ? 25 : 30000.0 / 1001.0;
224                         if ( mlt_properties_get_double( properties, "fps" ) == fps )
225                         {
226                                 mlt_properties_set_position( properties, "length", this->frames_in_file );
227                                 mlt_properties_set_position( properties, "in", 0 );
228                                 mlt_properties_set_position( properties, "out", this->frames_in_file - 1 );
229                         }
230                         else
231                         {
232                                 valid = 0;
233                         }
234
235                         // Parse the header for meta info
236                         dv_parse_header( dv_decoder, dv_data );
237                         mlt_properties_set_double( properties, "aspect_ratio", 
238                                 dv_format_wide( dv_decoder ) ? ( this->is_pal ? 512.0/351.0 : 96.0/79.0 ) : ( this->is_pal ? 128.0/117.0 : 72.0/79.0 ) );
239
240                         // Return the decoder
241                         dv_decoder_return( dv_decoder );
242                 }
243
244                 mlt_pool_release( dv_data );
245         }
246
247         return valid;
248 }
249
250 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
251 {
252         int pitches[3] = { 0, 0, 0 };
253         uint8_t *pixels[3] = { NULL, NULL, NULL };
254         
255         // Get the frames properties
256         mlt_properties properties = mlt_frame_properties( this );
257
258         // Get a dv_decoder
259         dv_decoder_t *decoder = dv_decoder_alloc( );
260
261         // Get the dv data
262         uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
263
264         // Get and set the quality request
265         char *quality = mlt_frame_pop_service( this );
266
267         if ( quality != NULL )
268         {
269                 if ( strncmp( quality, "fast", 4 ) == 0 )
270                         decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_DC );
271                 else if ( strncmp( quality, "best", 4 ) == 0 )
272                         decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_2 );
273                 else
274                         decoder->quality = ( DV_QUALITY_COLOR | DV_QUALITY_AC_1 );
275         }
276
277         // Parse the header for meta info
278         dv_parse_header( decoder, dv_data );
279         
280         // Assign width and height from properties
281         *width = mlt_properties_get_int( properties, "width" );
282         *height = mlt_properties_get_int( properties, "height" );
283
284         // Extract an image of the format requested
285         if ( *format == mlt_image_yuv422 )
286         {
287                 // Allocate an image
288                 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 2 );
289
290                 // Pass to properties for clean up
291                 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
292
293                 // Decode the image
294                 pitches[ 0 ] = *width * 2;
295                 pixels[ 0 ] = image;
296                 dv_decode_full_frame( decoder, dv_data, e_dv_color_yuv, pixels, pitches );
297
298                 // Assign result
299                 *buffer = image;
300         }
301         else if ( *format == mlt_image_rgb24 )
302         {
303                 // Allocate an image
304                 uint8_t *image = mlt_pool_alloc( *width * ( *height + 1 ) * 3 );
305
306                 // Pass to properties for clean up
307                 mlt_properties_set_data( properties, "image", image, *width * ( *height + 1 ) * 3, ( mlt_destructor )mlt_pool_release, NULL );
308
309                 // Decode the frame
310                 pitches[ 0 ] = 720 * 3;
311                 pixels[ 0 ] = image;
312                 dv_decode_full_frame( decoder, dv_data, e_dv_color_rgb, pixels, pitches );
313
314                 // Assign result
315                 *buffer = image;
316         }
317
318         // Return the decoder
319         dv_decoder_return( decoder );
320
321         return 0;
322 }
323
324 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
325 {
326         int16_t *p;
327         int i, j;
328         int16_t *audio_channels[ 4 ];
329         
330         // Get the frames properties
331         mlt_properties properties = mlt_frame_properties( this );
332
333         // Get a dv_decoder
334         dv_decoder_t *decoder = dv_decoder_alloc( );
335
336         // Get the dv data
337         uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
338
339         // Parse the header for meta info
340         dv_parse_header( decoder, dv_data );
341
342         // Check that we have audio
343         if ( decoder->audio->num_channels > 0 )
344         {
345                 // Obtain required values
346                 *frequency = decoder->audio->frequency;
347                 *samples = decoder->audio->samples_this_frame;
348                 *channels = decoder->audio->num_channels;
349
350                 // Create a temporary workspace
351                 for ( i = 0; i < 4; i++ )
352                         audio_channels[ i ] = mlt_pool_alloc( DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
353         
354                 // Create a workspace for the result
355                 *buffer = mlt_pool_alloc( *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
356         
357                 // Pass the allocated audio buffer as a property
358                 mlt_properties_set_data( properties, "audio", *buffer, *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), ( mlt_destructor )mlt_pool_release, NULL );
359         
360                 // Decode the audio
361                 dv_decode_full_audio( decoder, dv_data, audio_channels );
362                 
363                 // Interleave the audio
364                 p = *buffer;
365                 for ( i = 0; i < *samples; i++ )
366                         for ( j = 0; j < *channels; j++ )
367                                 *p++ = audio_channels[ j ][ i ];
368         
369                 // Free the temporary work space
370                 for ( i = 0; i < 4; i++ )
371                         mlt_pool_release( audio_channels[ i ] );
372         }
373         else
374         {
375                 // No audio available on the frame, so get test audio (silence)
376                 this->get_audio = NULL;
377                 mlt_frame_get_audio( this, buffer, format, frequency, channels, samples );
378         }
379
380         // Return the decoder
381         dv_decoder_return( decoder );
382
383         return 0;
384 }
385
386 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
387 {
388         producer_libdv this = producer->child;
389         uint8_t *data = mlt_pool_alloc( frame_size_625_50 );
390         
391         // Obtain the current frame number
392         uint64_t position = mlt_producer_frame( producer );
393         
394         // Convert timecode to a file position (ensuring that we're on a frame boundary)
395         uint64_t offset = position * this->frame_size;
396
397         // Create an empty frame
398         *frame = mlt_frame_init( );
399
400         // Seek and fetch
401         if ( this->fd != 0 && 
402                  lseek( this->fd, offset, SEEK_SET ) == offset &&
403                  read_frame( this->fd, data, &this->is_pal ) )
404         {
405                 // Get the frames properties
406                 mlt_properties properties = mlt_frame_properties( *frame );
407
408                 // Get a dv_decoder
409                 dv_decoder_t *dv_decoder = dv_decoder_alloc( );
410
411                 // Pass the dv data
412                 mlt_properties_set_data( properties, "dv_data", data, frame_size_625_50, ( mlt_destructor )mlt_pool_release, NULL );
413
414                 // Update other info on the frame
415                 mlt_properties_set_int( properties, "width", 720 );
416                 mlt_properties_set_int( properties, "height", this->is_pal ? 576 : 480 );
417                 mlt_properties_set_int( properties, "top_field_first", 0 );
418
419                 // Parse the header for meta info
420                 dv_parse_header( dv_decoder, data );
421                 mlt_properties_set_int( properties, "progressive", dv_is_progressive( dv_decoder ) );
422                 mlt_properties_set_double( properties, "aspect_ratio", 
423                         dv_format_wide( dv_decoder ) ? ( this->is_pal ? 512.0/351.0 : 96.0/79.0 ) : ( this->is_pal ? 128.0/117.0 : 72.0/79.0 ) );
424
425                 // Hmm - register audio callback
426                 ( *frame )->get_audio = producer_get_audio;
427
428                 // Push the quality string
429                 mlt_frame_push_service( *frame, mlt_properties_get( mlt_producer_properties( producer ), "quality" ) );
430
431                 // Push the get_image method on to the stack
432                 mlt_frame_push_get_image( *frame, producer_get_image );
433
434                 // Return the decoder
435                 dv_decoder_return( dv_decoder );
436         }
437         else
438         {
439                 mlt_pool_release( data );
440         }
441
442         // Update timecode on the frame we're creating
443         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
444
445         // Calculate the next timecode
446         mlt_producer_prepare_next( producer );
447
448         return 0;
449 }
450
451 static void producer_close( mlt_producer parent )
452 {
453         // Obtain this
454         producer_libdv this = parent->child;
455
456         // Close the file
457         if ( this->fd > 0 )
458                 close( this->fd );
459
460         // Close the parent
461         parent->close = NULL;
462         mlt_producer_close( parent );
463
464         // Free the memory
465         free( this );
466 }