1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: copy.c,v 1.1 2002/12/14 21:32:41 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <vlc/decoder.h>
31 #include <vlc/input.h>
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* strdup() */
37 /*****************************************************************************
39 *****************************************************************************/
40 typedef struct packetizer_thread_s
42 /* Input properties */
43 decoder_fifo_t *p_fifo;
45 /* Output properties */
46 sout_input_t *p_sout_input;
47 sout_packet_format_t output_format;
49 } packetizer_thread_t;
51 static int Open ( vlc_object_t * );
52 static int Run ( decoder_fifo_t * );
54 static int InitThread ( packetizer_thread_t * );
55 static void PacketizeThread ( packetizer_thread_t * );
56 static void EndThread ( packetizer_thread_t * );
58 /*****************************************************************************
60 *****************************************************************************/
63 set_description( _("Copy packetizer") );
64 set_capability( "packetizer", 0 );
65 set_callbacks( Open, NULL );
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
74 *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
77 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
84 if( p_fifo->i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'a') )
89 /*****************************************************************************
90 * RunDecoder: this function is called just after the thread is created
91 *****************************************************************************/
92 static int Run( decoder_fifo_t *p_fifo )
94 packetizer_thread_t *p_pack;
97 msg_Info( p_fifo, "Running copy packetizer" );
98 if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
100 msg_Err( p_fifo, "out of memory" );
101 DecoderError( p_fifo );
104 memset( p_pack, 0, sizeof( packetizer_thread_t ) );
106 p_pack->p_fifo = p_fifo;
108 if( InitThread( p_pack ) != 0 )
110 DecoderError( p_fifo );
114 while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
116 PacketizeThread( p_pack );
120 if( ( b_error = p_pack->p_fifo->b_error ) )
122 DecoderError( p_pack->p_fifo );
135 #define FREE( p ) if( p ) free( p ); p = NULL
137 /*****************************************************************************
138 * InitThread: initialize data before entering main loop
139 *****************************************************************************/
141 static int InitThread( packetizer_thread_t *p_pack )
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;
147 p_pack->p_sout_input =
148 sout_InputNew( p_pack->p_fifo,
149 &p_pack->output_format );
151 if( !p_pack->p_sout_input )
153 msg_Err( p_pack->p_fifo,
154 "cannot add a new stream" );
161 /*****************************************************************************
162 * PacketizeThread: packetize an unit (here copy a complete pes)
163 *****************************************************************************/
164 static void PacketizeThread( packetizer_thread_t *p_pack )
166 sout_buffer_t *p_sout_buffer;
170 /* **** get samples count **** */
171 input_ExtractPES( p_pack->p_fifo, &p_pes );
174 p_pack->p_fifo->b_error = 1;
177 i_size = p_pes->i_pes_size;
181 data_packet_t *p_data;
185 sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
188 p_pack->p_fifo->b_error = 1;
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)
198 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
202 p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
203 p_data->p_payload_start,
209 sout_InputSendBuffer( p_pack->p_sout_input,
213 input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
217 /*****************************************************************************
218 * EndThread : packetizer thread destruction
219 *****************************************************************************/
220 static void EndThread ( packetizer_thread_t *p_pack)
222 if( p_pack->p_sout_input )
224 sout_InputDelete( p_pack->p_sout_input );