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