]> git.sesse.net Git - vlc/blob - modules/packetizer/copy.c
Added stream output. (common work with titer).
[vlc] / modules / packetizer / copy.c
1 /*****************************************************************************
2  * copy.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: copy.c,v 1.1 2002/12/14 21:32:41 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/aout.h>
30 #include <vlc/decoder.h>
31 #include <vlc/input.h>
32 #include <vlc/sout.h>
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>                                              /* strdup() */
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 typedef struct packetizer_thread_s
41 {
42     /* Input properties */
43     decoder_fifo_t          *p_fifo;
44
45     /* Output properties */
46     sout_input_t            *p_sout_input;
47     sout_packet_format_t    output_format;
48
49 } packetizer_thread_t;
50
51 static int  Open    ( vlc_object_t * );
52 static int  Run     ( decoder_fifo_t * );
53
54 static int  InitThread     ( packetizer_thread_t * );
55 static void PacketizeThread   ( packetizer_thread_t * );
56 static void EndThread      ( packetizer_thread_t * );
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61
62 vlc_module_begin();
63     set_description( _("Copy packetizer") );
64     set_capability( "packetizer", 0 );
65     set_callbacks( Open, NULL );
66 vlc_module_end();
67
68
69 /*****************************************************************************
70  * OpenDecoder: probe the packetizer and return score
71  *****************************************************************************
72  * Tries to launch a decoder and return score so that the interface is able
73  * to choose.
74  *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
76 {
77     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
78
79     p_fifo->pf_run = Run;
80
81     return VLC_SUCCESS;
82
83 #if 0
84     if( p_fifo->i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'a') )
85         ....
86 #endif
87 }
88
89 /*****************************************************************************
90  * RunDecoder: this function is called just after the thread is created
91  *****************************************************************************/
92 static int Run( decoder_fifo_t *p_fifo )
93 {
94     packetizer_thread_t *p_pack;
95     int b_error;
96
97     msg_Info( p_fifo, "Running copy packetizer" );
98     if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
99     {
100         msg_Err( p_fifo, "out of memory" );
101         DecoderError( p_fifo );
102         return( -1 );
103     }
104     memset( p_pack, 0, sizeof( packetizer_thread_t ) );
105
106     p_pack->p_fifo = p_fifo;
107
108     if( InitThread( p_pack ) != 0 )
109     {
110         DecoderError( p_fifo );
111         return( -1 );
112     }
113
114     while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
115     {
116         PacketizeThread( p_pack );
117     }
118
119
120     if( ( b_error = p_pack->p_fifo->b_error ) )
121     {
122         DecoderError( p_pack->p_fifo );
123     }
124
125     EndThread( p_pack );
126     if( b_error )
127     {
128         return( -1 );
129     }
130
131     return( 0 );
132 }
133
134
135 #define FREE( p ) if( p ) free( p ); p = NULL
136
137 /*****************************************************************************
138  * InitThread: initialize data before entering main loop
139  *****************************************************************************/
140
141 static int InitThread( packetizer_thread_t *p_pack )
142 {
143
144 //    p_pack->output_format.i_cat = p_pack->p_fifo->i_cat;
145     p_pack->output_format.i_fourcc = p_pack->p_fifo->i_fourcc;
146
147     p_pack->p_sout_input = 
148         sout_InputNew( p_pack->p_fifo,
149                        &p_pack->output_format );
150
151     if( !p_pack->p_sout_input )
152     {
153         msg_Err( p_pack->p_fifo, 
154                  "cannot add a new stream" );
155         return( -1 );
156     }
157
158     return( 0 );
159 }
160
161 /*****************************************************************************
162  * PacketizeThread: packetize an unit (here copy a complete pes)
163  *****************************************************************************/
164 static void PacketizeThread( packetizer_thread_t *p_pack )
165 {
166     sout_buffer_t   *p_sout_buffer;
167     pes_packet_t    *p_pes;
168     size_t          i_size;
169
170     /* **** get samples count **** */
171     input_ExtractPES( p_pack->p_fifo, &p_pes );
172     if( !p_pes )
173     {
174         p_pack->p_fifo->b_error = 1;
175         return;
176     }
177     i_size = p_pes->i_pes_size;
178
179     if( i_size > 0 )
180     {
181         data_packet_t   *p_data;
182         size_t          i_buffer;
183
184         p_sout_buffer = 
185             sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
186         if( !p_sout_buffer )
187         {
188             p_pack->p_fifo->b_error = 1;
189             return;
190         }
191         /* TODO: memcpy of the pes packet */
192         for( i_buffer = 0, p_data = p_pes->p_first;
193              p_data != NULL && i_buffer < i_size;
194              p_data = p_data->p_next)
195         {
196             size_t          i_copy;
197
198             i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start, 
199                             i_size - i_buffer );
200             if( i_copy > 0 )
201             {
202                 p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
203                                                   p_data->p_payload_start,
204                                                   i_copy );
205             }
206             i_buffer += i_copy;
207         }
208
209         sout_InputSendBuffer( p_pack->p_sout_input,
210                                p_sout_buffer );
211     }
212
213     input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
214 }
215
216
217 /*****************************************************************************
218  * EndThread : packetizer thread destruction
219  *****************************************************************************/
220 static void EndThread ( packetizer_thread_t *p_pack)
221 {
222     if( p_pack->p_sout_input )
223     {
224         sout_InputDelete( p_pack->p_sout_input );
225     }
226 }
227