demux: libavi: force chunk type check
[vlc.git] / modules / demux / dirac.c
blobc6eb7180ce1bdb6774f37e718cfb6fef3e3d2911
1 /*****************************************************************************
2 * dirac.c : Dirac Video demuxer
3 *****************************************************************************
4 * Copyright (C) 2002-2008 VLC authors and VideoLAN
5 * $Id$
7 * Authors: David Flynn <davidf@rd.bbc.co.uk>
8 * Based on vc1.c by: Laurent Aimar <fenrir@via.ecp.fr>
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_codec.h>
38 #define DEMUX_CFG_PREFIX "dirac-"
40 #define DEMUX_DTSOFFSET "dts-offset"
41 #define DEMUX_DTSOFFSET_TEXT N_("Value to adjust dts by")
42 #define DEMUX_DTSOFFSET_LONGTEXT DEMUX_DTSOFFSET_TEXT
44 /*****************************************************************************
45 * Module descriptor
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
50 vlc_module_begin();
51 set_shortname( "Dirac");
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_DEMUX );
54 set_description( N_("Dirac video demuxer" ) );
55 set_capability( "demux", 50 );
56 add_integer( DEMUX_CFG_PREFIX DEMUX_DTSOFFSET, 0,
57 DEMUX_DTSOFFSET_TEXT, DEMUX_DTSOFFSET_LONGTEXT, false )
58 set_callbacks( Open, Close );
59 add_shortcut( "dirac" )
60 vlc_module_end();
62 /*****************************************************************************
63 * Local prototypes
64 *****************************************************************************/
65 struct demux_sys_t
67 mtime_t i_dts;
68 mtime_t i_dtsoffset;
69 mtime_t i_pts_offset_lowtide;
70 es_out_id_t *p_es;
72 enum {
73 /* demuxer states, do not reorder (++ is used) */
74 DIRAC_DEMUX_DISCONT = 0, /* signal a discontinuity to packetizer */
75 DIRAC_DEMUX_FIRST, /* provide an origin timestamp for the packetizer */
76 DIRAC_DEMUX_STEADY, /* normal operation */
77 } i_state;
79 decoder_t *p_packetizer;
82 static int Demux( demux_t * );
83 static int Control( demux_t *, int, va_list );
85 #define DIRAC_PACKET_SIZE 4096
87 /*****************************************************************************
88 * Open: initializes demux structures
89 *****************************************************************************/
90 static int Open( vlc_object_t * p_this )
92 demux_t *p_demux = (demux_t*)p_this;
93 demux_sys_t *p_sys;
94 const uint8_t *p_peek;
95 es_format_t fmt;
97 if( vlc_stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
99 if( p_peek[0] != 'B' || p_peek[1] != 'B' ||
100 p_peek[2] != 'C' || p_peek[3] != 'D') /* start of ParseInfo */
102 if( !p_demux->obj.force ) return VLC_EGENERIC;
104 msg_Err( p_demux, "This doesn't look like a Dirac stream (incorrect parsecode)" );
105 msg_Warn( p_demux, "continuing anyway" );
108 p_demux->pf_demux = Demux;
109 p_demux->pf_control = Control;
110 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
111 if( !p_sys ) return VLC_ENOMEM;
113 p_sys->i_pts_offset_lowtide = INT64_MAX;
114 p_sys->i_state = DIRAC_DEMUX_FIRST;
116 p_sys->i_dtsoffset = var_CreateGetInteger( p_demux, DEMUX_CFG_PREFIX DEMUX_DTSOFFSET );
118 /* Load the packetizer */
119 es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_DIRAC );
120 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, "dirac" );
121 if( !p_sys->p_packetizer )
123 free( p_sys );
124 return VLC_EGENERIC;
127 return VLC_SUCCESS;
130 /*****************************************************************************
131 * Close: frees unused data
132 *****************************************************************************/
133 static void Close( vlc_object_t * p_this )
135 demux_t *p_demux = (demux_t*)p_this;
136 demux_sys_t *p_sys = p_demux->p_sys;
138 demux_PacketizerDestroy( p_sys->p_packetizer );
140 if( p_sys->i_pts_offset_lowtide < INT64_MAX &&
141 p_sys->i_pts_offset_lowtide > 0 )
143 msg_Warn( p_demux, "For all packets seen, pts-dts (%"PRId64") could be reduced to 0",
144 p_sys->i_pts_offset_lowtide );
146 free( p_sys );
149 /*****************************************************************************
150 * Demux: reads and demuxes data packets
151 *****************************************************************************
152 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
153 *****************************************************************************/
154 static int Demux( demux_t *p_demux)
156 demux_sys_t *p_sys = p_demux->p_sys;
157 block_t *p_block_in, *p_block_out;
159 bool b_eof = false;
161 if( p_sys->i_state == DIRAC_DEMUX_DISCONT )
163 p_sys->i_state++;
164 p_block_in = block_Alloc( 128 );
165 if( p_block_in )
167 p_block_in->i_flags = BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED;
170 else
172 p_block_in = vlc_stream_Block( p_demux->s, DIRAC_PACKET_SIZE );
173 if( !p_block_in )
175 b_eof = true;
177 else if ( p_sys->i_state == DIRAC_DEMUX_FIRST)
179 p_sys->i_state++;
180 /* by default, timestamps are invalid.
181 * Except when we need an anchor point */
182 p_block_in->i_dts = VLC_TS_0;
186 while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer,
187 (p_block_in) ? &p_block_in : NULL )) )
189 while( p_block_out )
191 block_t *p_next = p_block_out->p_next;
192 p_block_out->p_next = NULL;
194 if( p_sys->p_es == NULL )
195 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
197 p_block_out->i_dts += p_sys->i_dtsoffset;
198 p_sys->i_dts = p_block_out->i_dts;
200 /* track low watermark for pts_offset -- can be used to show
201 * when it is too large */
202 mtime_t i_delay = p_block_out->i_pts - p_block_out->i_dts;
203 if( p_sys->i_pts_offset_lowtide > i_delay )
205 p_sys->i_pts_offset_lowtide = i_delay;
208 es_out_SetPCR( p_demux->out, p_block_out->i_dts );
209 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
211 p_block_out = p_next;
215 return b_eof ? VLC_DEMUXER_EOF : VLC_DEMUXER_SUCCESS;
218 /*****************************************************************************
219 * Control:
220 *****************************************************************************/
221 static int Control( demux_t *p_demux, int i_query, va_list args )
223 demux_sys_t *p_sys = p_demux->p_sys;
224 if( DEMUX_GET_TIME == i_query )
226 int64_t *pi64 = va_arg( args, int64_t * );
227 *pi64 = p_sys->i_dts;
228 return VLC_SUCCESS;
230 else if( DEMUX_GET_FPS == i_query )
232 if( !p_sys->p_packetizer->fmt_out.video.i_frame_rate )
234 return VLC_EGENERIC;
236 double *pd = va_arg( args, double * );
237 *pd = (float) p_sys->p_packetizer->fmt_out.video.i_frame_rate
238 / p_sys->p_packetizer->fmt_out.video.i_frame_rate_base;
239 return VLC_SUCCESS;
241 else
243 if( DEMUX_SET_POSITION == i_query || DEMUX_SET_TIME == i_query )
245 p_sys->i_state = DIRAC_DEMUX_DISCONT;
247 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );