move audio sample calculator to mlt_frame and use from ffmpeg and mcmpeg, add mlt_fra...
[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 <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <libdv/dv.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 typedef struct producer_libdv_s *producer_libdv;
33
34 struct producer_libdv_s
35 {
36 struct mlt_producer_s parent;
37 int fd;
38 dv_decoder_t *dv_decoder;
39 int is_pal;
40 uint64_t file_size;
41 int frame_size;
42 long frames_in_file;
43 };
44
45 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
46 static void producer_close( mlt_producer parent );
47
48 static int producer_collect_info( producer_libdv this );
49
50 mlt_producer producer_libdv_init( char *filename )
51 {
52 producer_libdv this = calloc( sizeof( struct producer_libdv_s ), 1 );
53
54 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
55 {
56 mlt_producer producer = &this->parent;
57 mlt_properties properties = mlt_producer_properties( producer );
58
59 // Register transport implementation with the producer
60 producer->close = producer_close;
61
62 // Register our get_frame implementation with the producer
63 producer->get_frame = producer_get_frame;
64
65 // Create the dv_decoder
66 this->dv_decoder = dv_decoder_new( FALSE, FALSE, FALSE );
67 this->dv_decoder->quality = DV_QUALITY_BEST;
68 this->dv_decoder->audio->arg_audio_emphasis = 2;
69 dv_set_audio_correction( this->dv_decoder, DV_AUDIO_CORRECT_AVERAGE );
70
71 // Open the file if specified
72 this->fd = open( filename, O_RDONLY );
73 producer_collect_info( this );
74
75 // Set the resource property (required for all producers)
76 mlt_properties_set( properties, "resource", filename );
77
78 // Return the producer
79 return producer;
80 }
81 free( this );
82 return NULL;
83 }
84
85 static int read_frame( int fd, uint8_t* frame_buf, int *isPAL )
86 {
87 int result = read( fd, frame_buf, frame_size_525_60 ) == frame_size_525_60;
88 if ( result )
89 {
90 *isPAL = ( frame_buf[3] & 0x80 );
91
92 if ( *isPAL )
93 {
94 int diff = frame_size_625_50 - frame_size_525_60;
95 result = read( fd, frame_buf + frame_size_525_60, diff ) == diff;
96 }
97 }
98
99 return result;
100 }
101
102 static int producer_collect_info( producer_libdv this )
103 {
104 int valid = 0;
105 uint8_t *dv_data = malloc( frame_size_625_50 );
106
107 if ( dv_data != NULL )
108 {
109 // Read the first frame
110 valid = read_frame( this->fd, dv_data, &this->is_pal );
111
112 // If it looks like a valid frame, the get stats
113 if ( valid )
114 {
115 // Get the properties
116 mlt_properties properties = mlt_producer_properties( &this->parent );
117
118 // Determine the file size
119 struct stat buf;
120 fstat( this->fd, &buf );
121
122 // Store the file size
123 this->file_size = buf.st_size;
124
125 // Determine the frame size
126 this->frame_size = this->is_pal ? frame_size_625_50 : frame_size_525_60;
127
128 // Determine the number of frames in the file
129 this->frames_in_file = this->file_size / this->frame_size;
130
131 // Calculate default in/out points
132 double fps = this->is_pal ? 25 : 30000.0 / 1001.0;
133 mlt_timecode length = ( mlt_timecode )( this->frames_in_file ) / fps;
134 mlt_properties_set_double( properties, "fps", fps );
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 );
138
139 // Parse the header for meta info
140 dv_parse_header( this->dv_decoder, data );
141 mlt_properties_set_double( properties, "aspect_ratio", dv_format_wide( this->dv_decoder ) ? 16.0/9.0 : 4.0/3.0 );
142
143 // Set the speed to normal
144 mlt_properties_set_double( properties, "speed", 1 );
145 }
146
147 free( dv_data );
148 }
149
150 return valid;
151 }
152
153 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
154 {
155 int pitches[3] = { 0, 0, 0 };
156 uint8_t *pixels[3] = { NULL, NULL, NULL };
157
158 // Get the frames properties
159 mlt_properties properties = mlt_frame_properties( this );
160
161 // Get the dv decoder
162 dv_decoder_t *decoder = mlt_properties_get_data( properties, "dv_decoder", NULL );
163
164 // Get the dv data
165 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
166
167 // Parse the header for meta info
168 dv_parse_header( this->dv_decoder, data );
169
170 // Assign width and height from properties
171 *width = mlt_properties_get_int( properties, "width" );
172 *height = mlt_properties_get_int( properties, "height" );
173
174 // Extract an image of the format requested
175 if ( *format == mlt_image_yuv422 )
176 {
177 // Allocate an image
178 uint8_t *image = malloc( *width * *height * 2 );
179
180 // Pass to properties for clean up
181 mlt_properties_set_data( properties, "image", image, *width * *height * 2, free, NULL );
182
183 // Decode the image
184 pitches[ 0 ] = *width * 2;
185 pixels[ 0 ] = image;
186 dv_decode_full_frame( decoder, dv_data, e_dv_color_yuv, pixels, pitches );
187
188 // Assign result
189 *buffer = image;
190 }
191 else if ( *format == mlt_image_rgb24 )
192 {
193 // Allocate an image
194 uint8_t *image = malloc( *width * *height * 3 );
195
196 // Pass to properties for clean up
197 mlt_properties_set_data( properties, "image", image, *width * *height * 3, free, NULL );
198
199 // Decode the frame
200 pitches[ 0 ] = 720 * 3;
201 pixels[ 0 ] = image;
202 dv_decode_full_frame( decoder, dv_data, e_dv_color_rgb, pixels, pitches );
203
204 // Assign result
205 *buffer = image;
206 }
207
208 return 0;
209 }
210
211 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
212 {
213 int16_t *p;
214 int i, j;
215 int16_t *audio_channels[ 4 ];
216
217 // Get the frames properties
218 mlt_properties properties = mlt_frame_properties( this );
219
220 // Get the dv decoder
221 dv_decoder_t *decoder = mlt_properties_get_data( properties, "dv_decoder", NULL );
222
223 // Get the dv data
224 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
225
226 // Parse the header for meta info
227 dv_parse_header( this->dv_decoder, data );
228
229 // Obtain required values
230 *frequency = decoder->audio->frequency;
231 *samples = decoder->audio->samples_this_frame;
232 *channels = decoder->audio->num_channels;
233
234 // Create a temporary workspace
235 for ( i = 0; i < 4; i++ )
236 audio_channels[ i ] = malloc( DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
237
238 // Create a workspace for the result
239 *buffer = malloc( *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
240
241 // Pass the allocated audio buffer as a property
242 mlt_properties_set_data( properties, "audio", *buffer, *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), free, NULL );
243
244 // Decode the audio
245 dv_decode_full_audio( decoder, dv_data, audio_channels );
246
247 // Interleave the audio
248 p = *buffer;
249 for ( i = 0; i < *samples; i++ )
250 for ( j = 0; j < *channels; j++ )
251 *p++ = audio_channels[ j ][ i ];
252
253 // Free the temporary work space
254 for ( i = 0; i < 4; i++ )
255 free( audio_channels[ i ] );
256
257 return 0;
258 }
259
260 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
261 {
262 producer_libdv this = producer->child;
263 uint8_t *data = malloc( frame_size_625_50 );
264
265 // Obtain the current frame number
266 uint64_t position = mlt_producer_frame( producer );
267
268 // Convert timecode to a file position (ensuring that we're on a frame boundary)
269 uint64_t offset = position * this->frame_size;
270
271 // Create an empty frame
272 *frame = mlt_frame_init( );
273
274 // Seek and fetch
275 if ( this->fd != 0 &&
276 lseek( this->fd, offset, SEEK_SET ) == offset &&
277 read_frame( this->fd, data, &this->is_pal ) )
278 {
279 // Get the frames properties
280 mlt_properties properties = mlt_frame_properties( *frame );
281
282 // Pass the dv decoder
283 mlt_properties_set_data( properties, "dv_decoder", this->dv_decoder, 0, NULL, NULL );
284
285 // Pass the dv data
286 mlt_properties_set_data( properties, "dv_data", data, frame_size_625_50, free, NULL );
287
288 // Update other info on the frame
289 mlt_properties_set_int( properties, "width", 720 );
290 mlt_properties_set_int( properties, "height", this->is_pal ? 576 : 480 );
291 mlt_properties_set_int( properties, "top_field_first", 0 );
292
293 // Parse the header for meta info
294 dv_parse_header( this->dv_decoder, data );
295 mlt_properties_set_int( properties, "progressive", dv_is_progressive( this->dv_decoder ) );
296 mlt_properties_set_double( properties, "aspect_ratio", dv_format_wide( this->dv_decoder ) ? 16.0/9.0 : 4.0/3.0 );
297
298 // Hmm - register audio callback
299 ( *frame )->get_audio = producer_get_audio;
300
301 // Push the get_image method on to the stack
302 mlt_frame_push_get_image( *frame, producer_get_image );
303 }
304 else
305 {
306 free( data );
307 }
308
309 // Update timecode on the frame we're creating
310 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
311
312 // Calculate the next timecode
313 mlt_producer_prepare_next( producer );
314
315 return 0;
316 }
317
318 static void producer_close( mlt_producer parent )
319 {
320 // Obtain this
321 producer_libdv this = parent->child;
322
323 // Free the dv deconder
324 dv_decoder_free( this->dv_decoder );
325
326 // Close the file
327 if ( this->fd != 0 )
328 close( this->fd );
329
330 // Close the parent
331 parent->close = NULL;
332 mlt_producer_close( parent );
333
334 // Free the memory
335 free( this );
336 }
337