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 ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
56 mlt_producer producer
= &this->parent
;
58 // Register transport implementation with the producer
59 producer
->close
= producer_close
;
61 // Register our get_frame implementation with the producer
62 producer
->get_frame
= producer_get_frame
;
64 // Create the dv_decoder
65 this->dv_decoder
= dv_decoder_new( FALSE
, FALSE
, FALSE
);
66 this->dv_decoder
->quality
= DV_QUALITY_BEST
;
67 this->dv_decoder
->audio
->arg_audio_emphasis
= 2;
68 dv_set_audio_correction( this->dv_decoder
, DV_AUDIO_CORRECT_AVERAGE
);
70 // Open the file if specified
71 if ( filename
!= NULL
)
73 this->fd
= open( filename
, O_RDONLY
);
74 producer_collect_info( this );
77 // Return the producer
84 static int read_frame( int fd
, uint8_t* frame_buf
, int *isPAL
)
86 int result
= read( fd
, frame_buf
, frame_size_525_60
) == frame_size_525_60
;
89 *isPAL
= ( frame_buf
[3] & 0x80 );
93 int diff
= frame_size_625_50
- frame_size_525_60
;
94 result
= read( fd
, frame_buf
+ frame_size_525_60
, diff
) == diff
;
101 static int producer_collect_info( producer_libdv
this )
104 uint8_t *dv_data
= malloc( frame_size_625_50
);
106 if ( dv_data
!= NULL
)
108 // Read the first frame
109 valid
= read_frame( this->fd
, dv_data
, &this->is_pal
);
111 // If it looks like a valid frame, the get stats
114 // Get the properties
115 mlt_properties properties
= mlt_producer_properties( &this->parent
);
117 // Determine the file size
119 fstat( this->fd
, &buf
);
121 // Store the file size
122 this->file_size
= buf
.st_size
;
124 // Determine the frame size
125 this->frame_size
= this->is_pal ? frame_size_625_50
: frame_size_525_60
;
127 // Determine the number of frames in the file
128 this->frames_in_file
= this->file_size
/ this->frame_size
;
130 // Calculate default in/out points
131 double fps
= this->is_pal ?
25 : 30000 / 1001;
132 mlt_timecode length
= ( mlt_timecode
)( this->frames_in_file
) / fps
;
133 mlt_properties_set_double( properties
, "fps", fps
);
134 mlt_properties_set_timecode( properties
, "playtime", length
);
135 mlt_properties_set_timecode( properties
, "length", length
);
136 mlt_properties_set_timecode( properties
, "in", 0.0 );
137 mlt_properties_set_timecode( properties
, "out", length
);
139 // Set the speed to normal
140 mlt_properties_set_double( properties
, "speed", 1 );
149 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
151 int pitches
[3] = { 0, 0, 0 };
152 uint8_t *pixels
[3] = { NULL
, NULL
, NULL
};
154 // Get the frames properties
155 mlt_properties properties
= mlt_frame_properties( this );
157 // Get the dv decoder
158 dv_decoder_t
*decoder
= mlt_properties_get_data( properties
, "dv_decoder", NULL
);
161 uint8_t *dv_data
= mlt_properties_get_data( properties
, "dv_data", NULL
);
163 // Assign width and height from properties
164 *width
= mlt_properties_get_int( properties
, "width" );
165 *height
= mlt_properties_get_int( properties
, "height" );
168 dv_parse_header( decoder
, dv_data
);
170 // Extract an image of the format requested
171 if ( *format
== mlt_image_yuv422
)
174 uint8_t *image
= malloc( *width
* *height
* 2 );
176 // Pass to properties for clean up
177 mlt_properties_set_data( properties
, "image", image
, *width
* *height
* 2, free
, NULL
);
180 pitches
[ 0 ] = *width
* 2;
182 dv_decode_full_frame( decoder
, dv_data
, e_dv_color_yuv
, pixels
, pitches
);
187 else if ( *format
== mlt_image_rgb24
)
190 uint8_t *image
= malloc( *width
* *height
* 3 );
192 // Pass to properties for clean up
193 mlt_properties_set_data( properties
, "image", image
, *width
* *height
* 3, free
, NULL
);
196 pitches
[ 0 ] = 720 * 3;
198 dv_decode_full_frame( decoder
, dv_data
, e_dv_color_rgb
, pixels
, pitches
);
207 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
211 int16_t *audio_channels
[ 4 ];
213 // Get the frames properties
214 mlt_properties properties
= mlt_frame_properties( this );
216 // Get the dv decoder
217 dv_decoder_t
*decoder
= mlt_properties_get_data( properties
, "dv_decoder", NULL
);
220 uint8_t *dv_data
= mlt_properties_get_data( properties
, "dv_data", NULL
);
222 // Parse the header for meta info
223 dv_parse_header( decoder
, dv_data
);
225 // Obtain required values
226 *frequency
= decoder
->audio
->frequency
;
227 *samples
= decoder
->audio
->samples_this_frame
;
228 *channels
= decoder
->audio
->num_channels
;
230 // Create a temporary workspace
231 for ( i
= 0; i
< 4; i
++ )
232 audio_channels
[ i
] = malloc( DV_AUDIO_MAX_SAMPLES
* sizeof( int16_t ) );
234 // Create a workspace for the result
235 *buffer
= malloc( *channels
* DV_AUDIO_MAX_SAMPLES
* sizeof( int16_t ) );
237 // Pass the allocated audio buffer as a property
238 mlt_properties_set_data( properties
, "audio", *buffer
, *channels
* DV_AUDIO_MAX_SAMPLES
* sizeof( int16_t ), free
, NULL
);
241 dv_decode_full_audio( decoder
, dv_data
, audio_channels
);
243 // Interleave the audio
245 for ( i
= 0; i
< *samples
; i
++ )
246 for ( j
= 0; j
< *channels
; j
++ )
247 *p
++ = audio_channels
[ j
][ i
];
249 // Free the temporary work space
250 for ( i
= 0; i
< 4; i
++ )
251 free( audio_channels
[ i
] );
256 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
258 producer_libdv
this = producer
->child
;
259 uint8_t *data
= malloc( frame_size_625_50
);
261 // Obtain the current frame number
262 uint64_t position
= mlt_producer_frame( producer
);
264 // Convert timecode to a file position (ensuring that we're on a frame boundary)
265 uint64_t offset
= position
* this->frame_size
;
267 // Create an empty frame
268 *frame
= mlt_frame_init( );
271 if ( this->fd
!= 0 &&
272 lseek( this->fd
, offset
, SEEK_SET
) == offset
&&
273 read_frame( this->fd
, data
, &this->is_pal
) )
275 // Get the frames properties
276 mlt_properties properties
= mlt_frame_properties( *frame
);
278 // Pass the dv decoder
279 mlt_properties_set_data( properties
, "dv_decoder", this->dv_decoder
, 0, NULL
, NULL
);
282 mlt_properties_set_data( properties
, "dv_data", data
, frame_size_625_50
, free
, NULL
);
284 // Update other info on the frame
285 mlt_properties_set_int( properties
, "width", 720 );
286 mlt_properties_set_int( properties
, "height", this->is_pal ?
576 : 480 );
288 // Hmm - register audio callback
289 ( *frame
)->get_audio
= producer_get_audio
;
291 // Push the get_image method on to the stack
292 mlt_frame_push_get_image( *frame
, producer_get_image
);
299 // Update timecode on the frame we're creating
300 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
302 // Calculate the next timecode
303 mlt_producer_prepare_next( producer
);
308 static void producer_close( mlt_producer parent
)
311 producer_libdv
this = parent
->child
;
313 // Free the dv deconder
314 dv_decoder_free( this->dv_decoder
);
321 parent
->close
= NULL
;
322 mlt_producer_close( parent
);