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