2 * producer_libdv.c -- simple libdv test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
21 #include "producer_libdv.h"
22 #include <framework/mlt_frame.h>
28 #include <sys/types.h>
32 typedef struct producer_libdv_s
*producer_libdv
;
34 struct producer_libdv_s
36 struct mlt_producer_s parent
;
38 dv_decoder_t
*dv_decoder
;
45 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int index
);
46 static void producer_close( mlt_producer parent
);
48 static int producer_collect_info( producer_libdv
this );
50 mlt_producer
producer_libdv_init( char *filename
)
52 producer_libdv
this = calloc( sizeof( struct producer_libdv_s
), 1 );
54 if ( filename
!= NULL
&& this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
56 mlt_producer producer
= &this->parent
;
57 mlt_properties properties
= mlt_producer_properties( producer
);
59 // Register transport implementation with the producer
60 producer
->close
= producer_close
;
62 // Register our get_frame implementation with the producer
63 producer
->get_frame
= producer_get_frame
;
65 // Create the dv_decoder
66 this->dv_decoder
= dv_decoder_new( FALSE
, FALSE
, FALSE
);
67 this->dv_decoder
->quality
= DV_QUALITY_COLOR
| DV_QUALITY_AC_1
;
68 this->dv_decoder
->audio
->arg_audio_emphasis
= 2;
69 dv_set_audio_correction( this->dv_decoder
, DV_AUDIO_CORRECT_AVERAGE
);
71 // Open the file if specified
72 this->fd
= open( filename
, O_RDONLY
);
75 if ( this->fd
!= -1 && producer_collect_info( this ) )
77 // Set the resource property (required for all producers)
78 mlt_properties_set( properties
, "resource", filename
);
83 mlt_producer_close( producer
);
87 // Return the producer
94 static int read_frame( int fd
, uint8_t* frame_buf
, int *isPAL
)
96 int result
= read( fd
, frame_buf
, frame_size_525_60
) == frame_size_525_60
;
99 *isPAL
= ( frame_buf
[3] & 0x80 );
103 int diff
= frame_size_625_50
- frame_size_525_60
;
104 result
= read( fd
, frame_buf
+ frame_size_525_60
, diff
) == diff
;
111 static int producer_collect_info( producer_libdv
this )
115 uint8_t *dv_data
= mlt_pool_alloc( frame_size_625_50
);
117 if ( dv_data
!= NULL
)
119 // Read the first frame
120 valid
= read_frame( this->fd
, dv_data
, &this->is_pal
);
122 // If it looks like a valid frame, the get stats
125 // Get the properties
126 mlt_properties properties
= mlt_producer_properties( &this->parent
);
128 // Determine the file size
130 fstat( this->fd
, &buf
);
132 // Store the file size
133 this->file_size
= buf
.st_size
;
135 // Determine the frame size
136 this->frame_size
= this->is_pal ? frame_size_625_50
: frame_size_525_60
;
138 // Determine the number of frames in the file
139 this->frames_in_file
= this->file_size
/ this->frame_size
;
141 // Calculate default in/out points
142 double fps
= this->is_pal ?
25 : 30000.0 / 1001.0;
143 if ( mlt_properties_get_double( properties
, "fps" ) == fps
)
145 mlt_properties_set_position( properties
, "length", this->frames_in_file
);
146 mlt_properties_set_position( properties
, "in", 0 );
147 mlt_properties_set_position( properties
, "out", this->frames_in_file
- 1 );
154 // Parse the header for meta info
155 dv_parse_header( this->dv_decoder
, dv_data
);
156 mlt_properties_set_double( properties
, "aspect_ratio",
157 dv_format_wide( this->dv_decoder
) ?
( this->is_pal ?
512.0/351.0 : 96.0/79.0 ) : ( this->is_pal ?
128.0/117.0 : 72.0/79.0 ) );
160 mlt_pool_release( dv_data
);
166 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
168 int pitches
[3] = { 0, 0, 0 };
169 uint8_t *pixels
[3] = { NULL
, NULL
, NULL
};
171 // Get the frames properties
172 mlt_properties properties
= mlt_frame_properties( this );
174 // Get the dv decoder
175 dv_decoder_t
*decoder
= mlt_properties_get_data( properties
, "dv_decoder", NULL
);
178 uint8_t *dv_data
= mlt_properties_get_data( properties
, "dv_data", NULL
);
180 // Parse the header for meta info
181 dv_parse_header( decoder
, dv_data
);
183 // Assign width and height from properties
184 *width
= mlt_properties_get_int( properties
, "width" );
185 *height
= mlt_properties_get_int( properties
, "height" );
187 // Extract an image of the format requested
188 if ( *format
== mlt_image_yuv422
)
191 uint8_t *image
= mlt_pool_alloc( *width
* ( *height
+ 1 ) * 2 );
193 // Pass to properties for clean up
194 mlt_properties_set_data( properties
, "image", image
, *width
* ( *height
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
197 pitches
[ 0 ] = *width
* 2;
199 dv_decode_full_frame( decoder
, dv_data
, e_dv_color_yuv
, pixels
, pitches
);
204 else if ( *format
== mlt_image_rgb24
)
207 uint8_t *image
= mlt_pool_alloc( *width
* ( *height
+ 1 ) * 3 );
209 // Pass to properties for clean up
210 mlt_properties_set_data( properties
, "image", image
, *width
* ( *height
+ 1 ) * 3, ( mlt_destructor
)mlt_pool_release
, NULL
);
213 pitches
[ 0 ] = 720 * 3;
215 dv_decode_full_frame( decoder
, dv_data
, e_dv_color_rgb
, pixels
, pitches
);
224 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
228 int16_t *audio_channels
[ 4 ];
230 // Get the frames properties
231 mlt_properties properties
= mlt_frame_properties( this );
233 // Get the dv decoder
234 dv_decoder_t
*decoder
= mlt_properties_get_data( properties
, "dv_decoder", NULL
);
237 uint8_t *dv_data
= mlt_properties_get_data( properties
, "dv_data", NULL
);
239 // Parse the header for meta info
240 dv_parse_header( decoder
, dv_data
);
242 // Check that we have audio
243 if ( decoder
->audio
->num_channels
> 0 )
245 // Obtain required values
246 *frequency
= decoder
->audio
->frequency
;
247 *samples
= decoder
->audio
->samples_this_frame
;
248 *channels
= decoder
->audio
->num_channels
;
250 // Create a temporary workspace
251 for ( i
= 0; i
< 4; i
++ )
252 audio_channels
[ i
] = mlt_pool_alloc( DV_AUDIO_MAX_SAMPLES
* sizeof( int16_t ) );
254 // Create a workspace for the result
255 *buffer
= mlt_pool_alloc( *channels
* DV_AUDIO_MAX_SAMPLES
* sizeof( int16_t ) );
257 // Pass the allocated audio buffer as a property
258 mlt_properties_set_data( properties
, "audio", *buffer
, *channels
* DV_AUDIO_MAX_SAMPLES
* sizeof( int16_t ), ( mlt_destructor
)mlt_pool_release
, NULL
);
261 dv_decode_full_audio( decoder
, dv_data
, audio_channels
);
263 // Interleave the audio
265 for ( i
= 0; i
< *samples
; i
++ )
266 for ( j
= 0; j
< *channels
; j
++ )
267 *p
++ = audio_channels
[ j
][ i
];
269 // Free the temporary work space
270 for ( i
= 0; i
< 4; i
++ )
271 mlt_pool_release( audio_channels
[ i
] );
275 // No audio available on the frame, so get test audio (silence)
276 this->get_audio
= NULL
;
277 mlt_frame_get_audio( this, buffer
, format
, frequency
, channels
, samples
);
283 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
285 producer_libdv
this = producer
->child
;
286 uint8_t *data
= mlt_pool_alloc( frame_size_625_50
);
288 // Obtain the current frame number
289 uint64_t position
= mlt_producer_frame( producer
);
291 // Convert timecode to a file position (ensuring that we're on a frame boundary)
292 uint64_t offset
= position
* this->frame_size
;
294 // Create an empty frame
295 *frame
= mlt_frame_init( );
298 if ( this->fd
!= 0 &&
299 lseek( this->fd
, offset
, SEEK_SET
) == offset
&&
300 read_frame( this->fd
, data
, &this->is_pal
) )
302 // Get the frames properties
303 mlt_properties properties
= mlt_frame_properties( *frame
);
305 // Pass the dv decoder
306 mlt_properties_set_data( properties
, "dv_decoder", this->dv_decoder
, 0, NULL
, NULL
);
309 mlt_properties_set_data( properties
, "dv_data", data
, frame_size_625_50
, ( mlt_destructor
)mlt_pool_release
, NULL
);
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 );
316 char *quality
= mlt_properties_get( mlt_producer_properties( producer
), "quality" );
317 if ( quality
!= NULL
)
319 if ( strncmp( quality
, "fast", 4 ) == 0 )
320 this->dv_decoder
->quality
= ( DV_QUALITY_COLOR
| DV_QUALITY_DC
);
321 else if ( strncmp( quality
, "best", 4 ) == 0 )
322 this->dv_decoder
->quality
= ( DV_QUALITY_COLOR
| DV_QUALITY_AC_2
);
324 this->dv_decoder
->quality
= ( DV_QUALITY_COLOR
| DV_QUALITY_AC_1
);
327 // Parse the header for meta info
328 dv_parse_header( this->dv_decoder
, data
);
329 mlt_properties_set_int( properties
, "progressive", dv_is_progressive( this->dv_decoder
) );
330 mlt_properties_set_double( properties
, "aspect_ratio",
331 dv_format_wide( this->dv_decoder
) ?
( this->is_pal ?
512.0/351.0 : 96.0/79.0 ) : ( this->is_pal ?
128.0/117.0 : 72.0/79.0 ) );
333 // Hmm - register audio callback
334 ( *frame
)->get_audio
= producer_get_audio
;
336 // Push the get_image method on to the stack
337 mlt_frame_push_get_image( *frame
, producer_get_image
);
341 mlt_pool_release( data
);
344 // Update timecode on the frame we're creating
345 mlt_frame_set_position( *frame
, mlt_producer_position( producer
) );
347 // Calculate the next timecode
348 mlt_producer_prepare_next( producer
);
353 static void producer_close( mlt_producer parent
)
356 producer_libdv
this = parent
->child
;
358 // Free the dv deconder
359 //dv_decoder_free( this->dv_decoder );
366 parent
->close
= NULL
;
367 mlt_producer_close( parent
);