]> git.sesse.net Git - vlc/blob - modules/packetizer/copy.c
* modules/packetizer/*: bug fix (use es_format_Copy() when necessary).
[vlc] / modules / packetizer / copy.c
1 /*****************************************************************************
2  * copy.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
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 <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 static int  Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
39
40 vlc_module_begin();
41     set_description( _("Copy packetizer") );
42     set_capability( "packetizer", 1 );
43     set_callbacks( Open, Close );
44 vlc_module_end();
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 struct decoder_sys_t
50 {
51     block_t *p_block;
52 };
53
54 static block_t *Packetize ( decoder_t *, block_t ** );
55
56 /*****************************************************************************
57  * Open: probe the packetizer and return score
58  *****************************************************************************
59  * Tries to launch a decoder and return score so that the interface is able
60  * to choose.
61  *****************************************************************************/
62 static int Open( vlc_object_t *p_this )
63 {
64     decoder_t     *p_dec = (decoder_t*)p_this;
65     decoder_sys_t *p_sys;
66
67     if( p_dec->fmt_in.i_cat != AUDIO_ES &&
68         p_dec->fmt_in.i_cat != VIDEO_ES &&
69         p_dec->fmt_in.i_cat != SPU_ES )
70     {
71         msg_Err( p_dec, "invalid ES type" );
72         return VLC_EGENERIC;
73     }
74
75     p_dec->pf_packetize = Packetize;
76
77     /* Create the output format */
78     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
79
80     /* Fix the value of the fourcc */
81     switch( p_dec->fmt_in.i_codec )
82     {
83         /* video */
84         case VLC_FOURCC( 'm', '4', 's', '2'):
85         case VLC_FOURCC( 'M', '4', 'S', '2'):
86         case VLC_FOURCC( 'm', 'p', '4', 's'):
87         case VLC_FOURCC( 'M', 'P', '4', 'S'):
88         case VLC_FOURCC( 'D', 'I', 'V', 'X'):
89         case VLC_FOURCC( 'd', 'i', 'v', 'x'):
90         case VLC_FOURCC( 'X', 'V', 'I', 'D'):
91         case VLC_FOURCC( 'X', 'v', 'i', 'D'):
92         case VLC_FOURCC( 'x', 'v', 'i', 'd'):
93         case VLC_FOURCC( 'D', 'X', '5', '0'):
94         case VLC_FOURCC( 0x04, 0,   0,   0):
95         case VLC_FOURCC( '3', 'I', 'V', '2'):
96             p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v');
97             break;
98
99         case VLC_FOURCC( 'm', 'p', 'g', '1' ):
100         case VLC_FOURCC( 'm', 'p', 'g', '2' ):
101         case VLC_FOURCC( 'm', 'p', '1', 'v' ):
102         case VLC_FOURCC( 'm', 'p', '2', 'v' ):
103             p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
104             break;
105
106         case VLC_FOURCC( 'd', 'i', 'v', '1' ):
107         case VLC_FOURCC( 'M', 'P', 'G', '4' ):
108         case VLC_FOURCC( 'm', 'p', 'g', '4' ):
109             p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '1' );
110             break;
111
112         case VLC_FOURCC( 'd', 'i', 'v', '2' ):
113         case VLC_FOURCC( 'M', 'P', '4', '2' ):
114         case VLC_FOURCC( 'm', 'p', '4', '2' ):
115             p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '2' );
116             break;
117
118         case VLC_FOURCC( 'd', 'i', 'v', '3' ):
119         case VLC_FOURCC( 'd', 'i', 'v', '4' ):
120         case VLC_FOURCC( 'D', 'I', 'V', '4' ):
121         case VLC_FOURCC( 'd', 'i', 'v', '5' ):
122         case VLC_FOURCC( 'D', 'I', 'V', '5' ):
123         case VLC_FOURCC( 'd', 'i', 'v', '6' ):
124         case VLC_FOURCC( 'D', 'I', 'V', '6' ):
125         case VLC_FOURCC( 'M', 'P', '4', '3' ):
126         case VLC_FOURCC( 'm', 'p', '4', '3' ):
127         case VLC_FOURCC( 'm', 'p', 'g', '3' ):
128         case VLC_FOURCC( 'M', 'P', 'G', '3' ):
129         case VLC_FOURCC( 'A', 'P', '4', '1' ):
130             p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
131             break;
132
133         case VLC_FOURCC( 'h', '2', '6', '3' ):
134         case VLC_FOURCC( 'U', '2', '6', '3' ):
135         case VLC_FOURCC( 'u', '2', '6', '3' ):
136             p_dec->fmt_out.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
137             break;
138
139         case VLC_FOURCC( 'i', '2', '6', '3' ):
140             p_dec->fmt_out.i_codec = VLC_FOURCC( 'I', '2', '6', '3' );
141             break;
142
143         case VLC_FOURCC( 'm', 'j', 'p', 'g' ):
144         case VLC_FOURCC( 'm', 'j', 'p', 'a' ):
145         case VLC_FOURCC( 'j', 'p', 'e', 'g' ):
146         case VLC_FOURCC( 'J', 'P', 'E', 'G' ):
147         case VLC_FOURCC( 'J', 'F', 'I', 'F' ):
148             p_dec->fmt_out.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
149             break;
150
151         case VLC_FOURCC( 'd', 'v', 's', 'd' ):
152         case VLC_FOURCC( 'D', 'V', 'S', 'D' ):
153         case VLC_FOURCC( 'd', 'v', 'h', 'd' ):
154             p_dec->fmt_out.i_codec = VLC_FOURCC( 'd', 'v', 's', 'l' );
155             break;
156
157         /* audio */
158         case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
159             switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
160             {
161                 case 1:
162                     p_dec->fmt_out.i_codec = VLC_FOURCC('u','8',' ',' ');
163                     break;
164                 case 2:
165                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','l');
166                     break;
167                 case 3:
168                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','l');
169                     break;
170                 case 4:
171                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','l');
172                     break;
173                 default:
174                     msg_Err( p_dec, "unknown raw audio sample size" );
175                     return VLC_EGENERIC;
176             }
177             break;
178
179         case VLC_FOURCC( 't', 'w', 'o', 's' ):
180             switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
181             {
182                 case 1:
183                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
184                     break;
185                 case 2:
186                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
187                     break;
188                 case 3:
189                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
190                     break;
191                 case 4:
192                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','b');
193                     break;
194                 default:
195                     msg_Err( p_dec, "unknown raw audio sample size" );
196                     return VLC_EGENERIC;
197             }
198             break;
199
200         case VLC_FOURCC( 's', 'o', 'w', 't' ):
201             switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
202             {
203                 case 1:
204                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
205                     break;
206                 case 2:
207                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','l');
208                     break;
209                 case 3:
210                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','l');
211                     break;
212                 case 4:
213                     p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','l');
214                     break;
215                 default:
216                     msg_Err( p_dec, "unknown raw audio sample size" );
217                     return VLC_EGENERIC;
218             }
219             break;
220     }
221
222     p_dec->p_sys = p_sys = malloc( sizeof( block_t ) );
223     p_sys->p_block    = NULL;
224
225     return VLC_SUCCESS;
226 }
227
228 /*****************************************************************************
229  * Close:
230  *****************************************************************************/
231 static void Close( vlc_object_t *p_this )
232 {
233     decoder_t     *p_dec = (decoder_t*)p_this;
234
235     if( p_dec->p_sys->p_block )
236     {
237         block_ChainRelease( p_dec->p_sys->p_block );
238     }
239
240     free( p_dec->p_sys );
241 }
242
243 /*****************************************************************************
244  * PacketizeStd: packetize an unit (here copy a complete block )
245  *****************************************************************************/
246 static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
247 {
248     block_t *p_block;
249     block_t *p_ret = p_dec->p_sys->p_block;
250
251     if( pp_block == NULL || *pp_block == NULL )
252     {
253         return NULL;
254     }
255     p_block = *pp_block;
256     *pp_block = NULL;
257
258     if( p_block->i_dts <= 0 )
259     {
260         p_block->i_dts = p_block->i_pts;
261     }
262
263     if( p_block->i_dts <= 0 )
264     {
265         msg_Dbg( p_dec, "need dts > 0" );
266         block_Release( p_block );
267         return NULL;
268     }
269
270     if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
271     {
272         p_ret->i_length = p_block->i_pts - p_ret->i_pts;
273     }
274     p_dec->p_sys->p_block = p_block;
275
276     return p_ret;
277 }