0d678af208ea9720e96eca0499af70f085002eb5
[melted] / src / modules / ffmpeg / filter_ffmpeg_dub.c
1 /*
2 * filter_ffmpeg_dub.c -- simple ffmpeg dub 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 "filter_ffmpeg_dub.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <framework/mlt.h>
25
26 /** Do it.
27 */
28
29 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
30 {
31 // Obtain the frame properties
32 mlt_properties frame_properties = mlt_frame_properties( frame );
33
34 // Get the producer used.
35 mlt_producer producer = mlt_properties_get_data( frame_properties, "producer", NULL );
36
37 // Get the producer properties
38 mlt_properties producer_properties = mlt_producer_properties( producer );
39
40 // Get the original get_audio
41 frame->get_audio = mlt_properties_get_data( frame_properties, "get_audio", NULL );
42
43 // Call the original get_audio
44 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
45
46 // Now if our producer is still producing, override the audio
47 if ( !mlt_properties_get_int( producer_properties, "end_of_clip" ) )
48 {
49 // Get the position
50 mlt_position position = mlt_properties_get_position( producer_properties, "dub_position" );
51
52 // We need a frame from the producer
53 mlt_frame producer_frame;
54
55 // Set the FPS
56 mlt_properties_set_double( producer_properties, "fps", mlt_properties_get_double( frame_properties, "fps" ) );
57
58 // Seek to the position
59 mlt_producer_seek( producer, position );
60
61 // Get the next frame
62 producer->get_frame( producer, &producer_frame, 0 );
63
64 if ( !mlt_properties_get_int( producer_properties, "end_of_clip" ) )
65 {
66 // Now get the audio from this frame
67 producer_frame->get_audio( producer_frame, buffer, format, frequency, channels, samples );
68
69 // This producer frame will be destroyed automatically when frame is destroyed
70 mlt_properties_set_data( frame_properties, "ffmpeg_dub_frame", producer_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
71
72 // Incrment the position
73 mlt_properties_set_position( producer_properties, "dub_position", position + 1 );
74 }
75 }
76
77 return 0;
78 }
79
80 /** Filter processing.
81 */
82
83 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
84 {
85 // Obtain the filter properties
86 mlt_properties filter_properties = mlt_filter_properties( this );
87
88 // Get the producer used.
89 mlt_producer producer = mlt_properties_get_data( filter_properties, "producer", NULL );
90
91 // Obtain the frame properties
92 mlt_properties frame_properties = mlt_frame_properties( frame );
93
94 // Get the producer properties
95 mlt_properties producer_properties = mlt_producer_properties( producer );
96
97 // Only do this if we have not reached end of clip and ffmpeg_dub has not already been done
98 if ( !mlt_properties_get_int( producer_properties, "end_of_clip" ) &&
99 mlt_properties_get_data( frame_properties, "get_audio", NULL ) == NULL )
100 {
101 // Backup the original get_audio (it's still needed)
102 mlt_properties_set_data( frame_properties, "get_audio", frame->get_audio, 0, NULL, NULL );
103
104 // Pass the producer on the frame
105 mlt_properties_set_data( frame_properties, "producer", producer, 0, NULL, NULL );
106
107 // Override the get_audio method
108 frame->get_audio = filter_get_audio;
109 }
110
111 return frame;
112 }
113
114 /** Constructor for the filter.
115 */
116
117 mlt_filter filter_ffmpeg_dub_init( char *file )
118 {
119 // Create the filter object
120 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
121
122 // Initialise it
123 mlt_filter_init( this, NULL );
124
125 // Overide the filter process method
126 this->process = filter_process;
127
128 // Obtain the properties
129 mlt_properties properties = mlt_filter_properties( this );
130
131 // Create an ffmpeg producer
132 mlt_producer producer = mlt_factory_producer( "ffmpeg", file );
133
134 // Pass the producer
135 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
136
137 // Initialise the audio frame position
138 mlt_properties_set_position( properties, "dub_position", 0 );
139
140 return this;
141 }
142