ffmpeg audio dub
[melted] / mlt / 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 int ( *get_audio )( mlt_frame, int16_t **, mlt_audio_format *, int *, int *, int * ) = mlt_properties_get_data( frame_properties, "get_audio", NULL );
42
43 // Call the original get_audio
44 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 double position = mlt_properties_get_double( producer_properties, "dub_position" );
51
52 // We need a frame from the producer
53 mlt_frame producer_frame;
54
55 // Seek to the position
56 mlt_producer_seek_frame( producer, ( int64_t )position );
57
58 // Get the next frame
59 producer->get_frame( producer, &producer_frame, 0 );
60
61 if ( !mlt_properties_get_int( producer_properties, "end_of_clip" ) )
62 {
63 // Now get the audio from this frame
64 producer_frame->get_audio( producer_frame, buffer, format, frequency, channels, samples );
65
66 // This producer frame will be destroyed automatically when frame is destroyed
67 mlt_properties_set_data( frame_properties, "ffmpeg_dub_frame", producer_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
68
69 // Incrment the position
70 mlt_properties_set_double( producer_properties, "dub_position", position + 1 );
71 }
72 }
73
74 return 0;
75 }
76
77 /** Filter processing.
78 */
79
80 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
81 {
82 // Obtain the filter properties
83 mlt_properties filter_properties = mlt_filter_properties( this );
84
85 // Get the producer used.
86 mlt_producer producer = mlt_properties_get_data( filter_properties, "producer", NULL );
87
88 // Obtain the frame properties
89 mlt_properties frame_properties = mlt_frame_properties( frame );
90
91 // Backup the original get_audio (it's still needed)
92 mlt_properties_set_data( frame_properties, "get_audio", frame->get_audio, 0, NULL, NULL );
93
94 // Pass the producer on the frame
95 mlt_properties_set_data( frame_properties, "producer", producer, 0, NULL, NULL );
96
97 // Override the get_audio method
98 frame->get_audio = filter_get_audio;
99
100 return frame;
101 }
102
103 /** Constructor for the filter.
104 */
105
106 mlt_filter filter_ffmpeg_dub_init( char *file )
107 {
108 // Create the filter object
109 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
110
111 // Initialise it
112 mlt_filter_init( this, NULL );
113
114 // Overide the filter process method
115 this->process = filter_process;
116
117 // Obtain the properties
118 mlt_properties properties = mlt_filter_properties( this );
119
120 // Create an ffmpeg producer
121 mlt_producer producer = mlt_factory_producer( "ffmpeg", file );
122
123 // Pass the producer
124 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
125
126 // Initialise the audio frame position
127 mlt_properties_set_double( properties, "dub_position", 0 );
128
129 return this;
130 }
131