Initial version
[melted] / src / modules / kino / producer_kino.c
1 /*
2 * producer_kino.c -- a DV file format parser
3 * Copyright (C) 2005 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 <stdlib.h>
22 #include "producer_kino.h"
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_deque.h>
25 #include <framework/mlt_factory.h>
26 #include "kino_wrapper.h"
27
28 /* NB: This is an abstract producer - it provides no codec support whatsoever. */
29
30 #define FRAME_SIZE_525_60 10 * 150 * 80
31 #define FRAME_SIZE_625_50 12 * 150 * 80
32
33 typedef struct producer_kino_s *producer_kino;
34
35 struct producer_kino_s
36 {
37 struct mlt_producer_s parent;
38 kino_wrapper wrapper;
39 };
40
41 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
42 static void producer_close( mlt_producer parent );
43
44 mlt_producer producer_kino_init( char *filename )
45 {
46 kino_wrapper wrapper = kino_wrapper_init( );
47
48 if ( kino_wrapper_open( wrapper, filename ) )
49 {
50 producer_kino this = calloc( sizeof( struct producer_kino_s ), 1 );
51
52 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
53 {
54 mlt_producer producer = &this->parent;
55 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
56 double fps = kino_wrapper_is_pal( wrapper ) ? 25 : 30000.0 / 1001.0;
57
58 // Assign the wrapper
59 this->wrapper = wrapper;
60
61 // Pass wrapper properties (frame rate, count etc)
62 mlt_properties_set_position( properties, "length", kino_wrapper_get_frame_count( wrapper ) );
63 mlt_properties_set_position( properties, "in", 0 );
64 mlt_properties_set_position( properties, "out", kino_wrapper_get_frame_count( wrapper ) - 1 );
65 mlt_properties_set_double( properties, "real_fps", fps );
66
67 // Register transport implementation with the producer
68 producer->close = ( mlt_destructor )producer_close;
69
70 // Register our get_frame implementation with the producer
71 producer->get_frame = producer_get_frame;
72
73 // Return the producer
74 return producer;
75 }
76 free( this );
77 }
78
79 kino_wrapper_close( wrapper );
80
81 return NULL;
82 }
83
84 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
85 {
86 producer_kino this = producer->child;
87 uint8_t *data = mlt_pool_alloc( FRAME_SIZE_625_50 );
88
89 // Obtain the current frame number
90 uint64_t position = mlt_producer_frame( producer );
91
92 // Create an empty frame
93 *frame = mlt_frame_init( );
94
95 // Seek and fetch
96 if ( kino_wrapper_get_frame( this->wrapper, data, position ) )
97 {
98 // Get the frames properties
99 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
100
101 // Determine if we're PAL or NTSC
102 int is_pal = kino_wrapper_is_pal( this->wrapper );
103
104 // Pass the dv data
105 mlt_properties_set_data( properties, "dv_data", data, FRAME_SIZE_625_50, ( mlt_destructor )mlt_pool_release, NULL );
106
107 // Update other info on the frame
108 mlt_properties_set_int( properties, "width", 720 );
109 mlt_properties_set_int( properties, "height", is_pal ? 576 : 480 );
110 mlt_properties_set_int( properties, "top_field_first", is_pal ? 0 : ( data[ 5 ] & 0x07 ) == 0 ? 0 : 1 );
111 }
112 else
113 {
114 mlt_pool_release( data );
115 }
116
117 // Update timecode on the frame we're creating
118 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
119
120 // Calculate the next timecode
121 mlt_producer_prepare_next( producer );
122
123 return 0;
124 }
125
126 static void producer_close( mlt_producer parent )
127 {
128 if ( parent != NULL )
129 {
130 // Obtain this
131 producer_kino this = parent->child;
132
133 // Close the file
134 if ( this != NULL )
135 kino_wrapper_close( this->wrapper );
136
137 // Close the parent
138 parent->close = NULL;
139 mlt_producer_close( parent );
140
141 // Free the memory
142 free( this );
143 }
144 }