1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_block.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
46 set_category( CAT_SOUT )
47 set_subcategory( SUBCAT_SOUT_PACKETIZER )
48 set_description( N_("Copy packetizer") )
49 set_capability( "packetizer", 1 )
50 set_callbacks( Open, Close )
53 /*****************************************************************************
55 *****************************************************************************/
59 void (*pf_parse)( decoder_t *, block_t * );
62 static block_t *Packetize ( decoder_t *, block_t ** );
63 static block_t *PacketizeSub( decoder_t *, block_t ** );
64 static void Flush( decoder_t * );
66 static void ParseWMV3( decoder_t *, block_t * );
68 /*****************************************************************************
69 * Open: probe the packetizer and return score
70 *****************************************************************************
71 * Tries to launch a decoder and return score so that the interface is able
73 *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
76 decoder_t *p_dec = (decoder_t*)p_this;
79 if( p_dec->fmt_in.i_cat != AUDIO_ES &&
80 p_dec->fmt_in.i_cat != VIDEO_ES &&
81 p_dec->fmt_in.i_cat != SPU_ES )
83 msg_Err( p_dec, "invalid ES type" );
87 if( p_dec->fmt_in.i_cat == SPU_ES )
88 p_dec->pf_packetize = PacketizeSub;
90 p_dec->pf_packetize = Packetize;
91 p_dec->pf_flush = Flush;
93 /* Create the output format */
94 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
96 p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
97 if (unlikely(p_sys == NULL))
100 p_sys->p_block = NULL;
101 switch( p_dec->fmt_in.i_codec )
104 p_sys->pf_parse = ParseWMV3;
107 p_sys->pf_parse = NULL;
111 /* Fix the value of the fourcc for audio */
112 if( p_dec->fmt_in.i_cat == AUDIO_ES )
114 vlc_fourcc_t fcc = vlc_fourcc_GetCodecAudio( p_dec->fmt_in.i_codec,
115 p_dec->fmt_in.audio.i_bitspersample );
118 msg_Err( p_dec, "unknown raw audio sample size" );
122 p_dec->fmt_out.i_codec = fcc;
128 /*****************************************************************************
130 *****************************************************************************/
131 static void Close( vlc_object_t *p_this )
133 decoder_t *p_dec = (decoder_t*)p_this;
135 if( p_dec->p_sys->p_block )
137 block_ChainRelease( p_dec->p_sys->p_block );
140 free( p_dec->p_sys );
143 static void Flush( decoder_t *p_dec )
145 block_t *p_ret = p_dec->p_sys->p_block;
148 block_Release( p_ret );
149 p_dec->p_sys->p_block = NULL;
153 /*****************************************************************************
154 * Packetize: packetize an unit (here copy a complete block )
155 *****************************************************************************/
156 static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
159 block_t *p_ret = p_dec->p_sys->p_block;
161 if( pp_block == NULL || *pp_block == NULL )
163 if( (*pp_block)->i_flags&(BLOCK_FLAG_CORRUPTED) )
165 block_Release( *pp_block );
172 if( p_block->i_dts <= VLC_TS_INVALID )
174 p_block->i_dts = p_block->i_pts;
177 if( p_block->i_dts <= VLC_TS_INVALID )
179 msg_Dbg( p_dec, "need valid dts" );
180 block_Release( p_block );
184 if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
186 if (p_dec->fmt_in.i_codec != VLC_CODEC_OPUS)
187 p_ret->i_length = p_block->i_pts - p_ret->i_pts;
189 p_dec->p_sys->p_block = p_block;
191 if( p_ret && p_dec->p_sys->pf_parse )
192 p_dec->p_sys->pf_parse( p_dec, p_ret );
196 /*****************************************************************************
197 * PacketizeSub: packetize an unit (here copy a complete block )
198 *****************************************************************************/
199 static block_t *PacketizeSub( decoder_t *p_dec, block_t **pp_block )
203 if( pp_block == NULL || *pp_block == NULL )
205 if( (*pp_block)->i_flags&(BLOCK_FLAG_CORRUPTED) )
207 block_Release( *pp_block );
214 if( p_block->i_dts <= VLC_TS_INVALID )
216 p_block->i_dts = p_block->i_pts;
219 if( p_block->i_dts <= VLC_TS_INVALID )
221 msg_Dbg( p_dec, "need valid dts" );
222 block_Release( p_block );
229 /* Parse WMV3 packet and extract frame type information */
230 static void ParseWMV3( decoder_t *p_dec, block_t *p_block )
234 /* Parse Sequence header */
235 bs_init( &s, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
236 if( bs_read( &s, 2 ) == 3 )
239 const bool b_range_reduction = bs_read( &s, 1 );
240 const bool b_has_frames = bs_read( &s, 3 ) > 0;
242 const bool b_frame_interpolation = bs_read( &s, 1 );
246 /* Parse frame type */
247 bs_init( &s, p_block->p_buffer, p_block->i_buffer );
248 bs_skip( &s, b_frame_interpolation +
252 p_block->i_flags &= ~BLOCK_FLAG_TYPE_MASK;
253 if( bs_read( &s, 1 ) )
254 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
255 else if( !b_has_frames || bs_read( &s, 1 ) )
256 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
258 p_block->i_flags |= BLOCK_FLAG_TYPE_B;