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