framework: remove global profile, rather share one mlt_profile across a service netwo...
[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 <framework/mlt_producer.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( mlt_profile profile, mlt_service_type type, const char *id, 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 mlt_properties_set( properties, "resource", filename );
67
68 // Register transport implementation with the producer
69 producer->close = ( mlt_destructor )producer_close;
70
71 // Register our get_frame implementation with the producer
72 producer->get_frame = producer_get_frame;
73
74 // Return the producer
75 return producer;
76 }
77 free( this );
78 }
79
80 kino_wrapper_close( wrapper );
81
82 return NULL;
83 }
84
85 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
86 {
87 producer_kino this = producer->child;
88 uint8_t *data = mlt_pool_alloc( FRAME_SIZE_625_50 );
89
90 // Obtain the current frame number
91 uint64_t position = mlt_producer_frame( producer );
92
93 // Create an empty frame
94 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
95
96 // Seek and fetch
97 if ( kino_wrapper_get_frame( this->wrapper, data, position ) )
98 {
99 // Get the frames properties
100 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
101
102 // Determine if we're PAL or NTSC
103 int is_pal = kino_wrapper_is_pal( this->wrapper );
104
105 // Pass the dv data
106 mlt_properties_set_data( properties, "dv_data", data, FRAME_SIZE_625_50, ( mlt_destructor )mlt_pool_release, NULL );
107
108 // Update other info on the frame
109 mlt_properties_set_int( properties, "width", 720 );
110 mlt_properties_set_int( properties, "height", is_pal ? 576 : 480 );
111 mlt_properties_set_int( properties, "top_field_first", is_pal ? 0 : ( data[ 5 ] & 0x07 ) == 0 ? 0 : 1 );
112 }
113 else
114 {
115 mlt_pool_release( data );
116 }
117
118 // Update timecode on the frame we're creating
119 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
120
121 // Calculate the next timecode
122 mlt_producer_prepare_next( producer );
123
124 return 0;
125 }
126
127 static void producer_close( mlt_producer parent )
128 {
129 if ( parent != NULL )
130 {
131 // Obtain this
132 producer_kino this = parent->child;
133
134 // Close the file
135 if ( this != NULL )
136 kino_wrapper_close( this->wrapper );
137
138 // Close the parent
139 parent->close = NULL;
140 mlt_producer_close( parent );
141
142 // Free the memory
143 free( this );
144 }
145 }