Initial revision
[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 ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
55 {
56 mlt_producer producer = &this->parent;
57
58 // Register transport implementation with the producer
59 producer->close = producer_close;
60
61 // Register our get_frame implementation with the producer
62 producer->get_frame = producer_get_frame;
63
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 );
69
70 // Open the file if specified
71 if ( filename != NULL )
72 {
73 this->fd = open( filename, O_RDONLY );
74 producer_collect_info( this );
75 }
76
77 // Return the producer
78 return producer;
79 }
80 free( this );
81 return NULL;
82 }
83
84 static int read_frame( int fd, uint8_t* frame_buf, int *isPAL )
85 {
86 int result = read( fd, frame_buf, frame_size_525_60 ) == frame_size_525_60;
87 if ( result )
88 {
89 *isPAL = ( frame_buf[3] & 0x80 );
90
91 if ( *isPAL )
92 {
93 int diff = frame_size_625_50 - frame_size_525_60;
94 result = read( fd, frame_buf + frame_size_525_60, diff ) == diff;
95 }
96 }
97
98 return result;
99 }
100
101 static int producer_collect_info( producer_libdv this )
102 {
103 int valid = 0;
104 uint8_t *dv_data = malloc( frame_size_625_50 );
105
106 if ( dv_data != NULL )
107 {
108 // Read the first frame
109 valid = read_frame( this->fd, dv_data, &this->is_pal );
110
111 // If it looks like a valid frame, the get stats
112 if ( valid )
113 {
114 // Get the properties
115 mlt_properties properties = mlt_producer_properties( &this->parent );
116
117 // Determine the file size
118 struct stat buf;
119 fstat( this->fd, &buf );
120
121 // Store the file size
122 this->file_size = buf.st_size;
123
124 // Determine the frame size
125 this->frame_size = this->is_pal ? frame_size_625_50 : frame_size_525_60;
126
127 // Determine the number of frames in the file
128 this->frames_in_file = this->file_size / this->frame_size;
129
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 );
138
139 // Set the speed to normal
140 mlt_properties_set_double( properties, "speed", 1 );
141 }
142
143 free( dv_data );
144 }
145
146 return valid;
147 }
148
149 #if 0
150 static int mc_producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
151 {
152 // Get the frames properties
153 mlt_properties properties = mlt_frame_properties( this );
154
155 // Get the dv data
156 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
157
158 // Determine bytes in frame
159 int bytes_in_frame = dv_data[ 3 ] & 0x80 ? frame_size_625_50 : frame_size_525_60;
160
161 // Assign width and height from properties
162 *width = mlt_properties_get_int( properties, "width" );
163 *height = mlt_properties_get_int( properties, "height" );
164
165 if ( *format == mlt_image_yuv422 )
166 {
167 // Allocate image
168 *buffer = malloc( *width * *height * 2 );
169
170 // Decompress
171 DecompressBuffer_DV( dv_data, bytes_in_frame, *buffer, *width * 2, *width, *height, 0, FOURCC_YUYV, 0, NULL );
172
173 // Set the image on the properties
174 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, free, NULL );
175 }
176 else
177 {
178 // Allocate image
179 *buffer = malloc( *width * *height * 3 );
180
181 // Decompress
182 DecompressBuffer_DV( dv_data, bytes_in_frame, *buffer, *width * 3, *width, *height, 0, FOURCC_R24C, 0, NULL );
183
184 // Set the image on the properties
185 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, free, NULL );
186 }
187
188 return 0;
189 }
190 #endif
191
192 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
193 {
194 int pitches[3] = { 0, 0, 0 };
195 uint8_t *pixels[3] = { NULL, NULL, NULL };
196
197 // Get the frames properties
198 mlt_properties properties = mlt_frame_properties( this );
199
200 // Get the dv decoder
201 dv_decoder_t *decoder = mlt_properties_get_data( properties, "dv_decoder", NULL );
202
203 // Get the dv data
204 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
205
206 // Assign width and height from properties
207 *width = mlt_properties_get_int( properties, "width" );
208 *height = mlt_properties_get_int( properties, "height" );
209
210 // Parse the header
211 dv_parse_header( decoder, dv_data );
212
213 // Extract an image of the format requested
214 if ( *format == mlt_image_yuv422 )
215 {
216 // Allocate an image
217 uint8_t *image = malloc( *width * *height * 2 );
218
219 // Pass to properties for clean up
220 mlt_properties_set_data( properties, "image", image, *width * *height * 2, free, NULL );
221
222 // Decode the image
223 pitches[ 0 ] = *width * 2;
224 pixels[ 0 ] = image;
225 dv_decode_full_frame( decoder, dv_data, e_dv_color_yuv, pixels, pitches );
226
227 // Assign result
228 *buffer = image;
229 }
230 else if ( *format == mlt_image_rgb24 )
231 {
232 // Allocate an image
233 uint8_t *image = malloc( *width * *height * 3 );
234
235 // Pass to properties for clean up
236 mlt_properties_set_data( properties, "image", image, *width * *height * 3, free, NULL );
237
238 // Decode the frame
239 pitches[ 0 ] = 720 * 3;
240 pixels[ 0 ] = image;
241 dv_decode_full_frame( decoder, dv_data, e_dv_color_rgb, pixels, pitches );
242
243 // Assign result
244 *buffer = image;
245 }
246
247 return 0;
248 }
249
250 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
251 {
252 int16_t *p;
253 int i, j;
254 int16_t *audio_channels[ 4 ];
255
256 // Get the frames properties
257 mlt_properties properties = mlt_frame_properties( this );
258
259 // Get the dv decoder
260 dv_decoder_t *decoder = mlt_properties_get_data( properties, "dv_decoder", NULL );
261
262 // Get the dv data
263 uint8_t *dv_data = mlt_properties_get_data( properties, "dv_data", NULL );
264
265 // Parse the header for meta info
266 dv_parse_header( decoder, dv_data );
267
268 // Obtain required values
269 *frequency = decoder->audio->frequency;
270 *samples = decoder->audio->samples_this_frame;
271 *channels = decoder->audio->num_channels;
272
273 // Create a temporary workspace
274 for ( i = 0; i < 4; i++ )
275 audio_channels[ i ] = malloc( DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
276
277 // Create a workspace for the result
278 *buffer = malloc( *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ) );
279
280 // Pass the allocated audio buffer as a property
281 mlt_properties_set_data( properties, "audio", *buffer, *channels * DV_AUDIO_MAX_SAMPLES * sizeof( int16_t ), free, NULL );
282
283 // Decode the audio
284 dv_decode_full_audio( decoder, dv_data, audio_channels );
285
286 // Interleave the audio
287 p = *buffer;
288 for ( i = 0; i < *samples; i++ )
289 for ( j = 0; j < *channels; j++ )
290 *p++ = audio_channels[ j ][ i ];
291
292 // Free the temporary work space
293 for ( i = 0; i < 4; i++ )
294 free( audio_channels[ i ] );
295
296 return 0;
297 }
298
299 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
300 {
301 producer_libdv this = producer->child;
302 uint8_t *data = malloc( frame_size_625_50 );
303
304 // Obtain the current frame number
305 uint64_t position = mlt_producer_frame( producer );
306
307 // Convert timecode to a file position (ensuring that we're on a frame boundary)
308 uint64_t offset = position * this->frame_size;
309
310 // Create an empty frame
311 *frame = mlt_frame_init( );
312
313 // Seek and fetch
314 if ( this->fd != 0 &&
315 lseek( this->fd, offset, SEEK_SET ) == offset &&
316 read_frame( this->fd, data, &this->is_pal ) )
317 {
318 // Get the frames properties
319 mlt_properties properties = mlt_frame_properties( *frame );
320
321 // Pass the dv decoder
322 mlt_properties_set_data( properties, "dv_decoder", this->dv_decoder, 0, NULL, NULL );
323
324 // Pass the dv data
325 mlt_properties_set_data( properties, "dv_data", data, frame_size_625_50, free, NULL );
326
327 // Update other info on the frame
328 mlt_properties_set_int( properties, "width", 720 );
329 mlt_properties_set_int( properties, "height", this->is_pal ? 576 : 480 );
330
331 // Hmm - register audio callback
332 ( *frame )->get_audio = producer_get_audio;
333
334 // Push the get_image method on to the stack
335 mlt_frame_push_get_image( *frame, producer_get_image );
336 }
337 else
338 {
339 free( data );
340 }
341
342 // Update timecode on the frame we're creating
343 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
344
345 // Calculate the next timecode
346 mlt_producer_prepare_next( producer );
347
348 return 0;
349 }
350
351 static void producer_close( mlt_producer parent )
352 {
353 // Obtain this
354 producer_libdv this = parent->child;
355
356 // Free the dv deconder
357 dv_decoder_free( this->dv_decoder );
358
359 // Close the file
360 if ( this->fd != 0 )
361 close( this->fd );
362
363 // Close the parent
364 parent->close = NULL;
365 mlt_producer_close( parent );
366
367 // Free the memory
368 free( this );
369 }
370