1 /*****************************************************************************
2 * dirac.c : Dirac Video demuxer
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
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
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., 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_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 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
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, NULL,
57 DEMUX_DTSOFFSET_TEXT, DEMUX_DTSOFFSET_LONGTEXT, false )
58 set_callbacks( Open, Close );
61 /*****************************************************************************
63 *****************************************************************************/
68 mtime_t i_pts_offset_lowtide;
73 decoder_t *p_packetizer;
76 static int Demux( demux_t * );
77 static int Control( demux_t *, int, va_list );
79 #define DIRAC_PACKET_SIZE 4096
81 /*****************************************************************************
82 * Open: initializes demux structures
83 *****************************************************************************/
84 static int Open( vlc_object_t * p_this )
86 demux_t *p_demux = (demux_t*)p_this;
88 const uint8_t *p_peek;
91 if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
93 if( p_peek[0] != 'B' || p_peek[1] != 'B' ||
94 p_peek[2] != 'C' || p_peek[3] != 'D') /* start of ParseInfo */
96 if( !p_demux->b_force ) return VLC_EGENERIC;
98 msg_Err( p_demux, "This doesn't look like a Dirac stream (incorrect parsecode)" );
99 msg_Warn( p_demux, "continuing anyway" );
102 p_demux->pf_demux = Demux;
103 p_demux->pf_control = Control;
104 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
105 if( !p_sys ) return VLC_ENOMEM;
107 p_sys->i_pts_offset_lowtide = INT64_MAX;
108 p_sys->b_first = true;
110 p_sys->i_dtsoffset = var_CreateGetInteger( p_demux, DEMUX_CFG_PREFIX DEMUX_DTSOFFSET );
112 /* Load the packetizer */
113 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'd','r','a','c' ) );
114 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, "dirac" );
115 if( !p_sys->p_packetizer )
124 /*****************************************************************************
125 * Close: frees unused data
126 *****************************************************************************/
127 static void Close( vlc_object_t * p_this )
129 demux_t *p_demux = (demux_t*)p_this;
130 demux_sys_t *p_sys = p_demux->p_sys;
132 demux_PacketizerDestroy( p_sys->p_packetizer );
134 if( p_sys->i_pts_offset_lowtide < INT64_MAX &&
135 p_sys->i_pts_offset_lowtide > 0 )
137 msg_Warn( p_demux, "For all packets seen, pts-dts (%"PRId64") could be reduced to 0",
138 p_sys->i_pts_offset_lowtide );
143 /*****************************************************************************
144 * Demux: reads and demuxes data packets
145 *****************************************************************************
146 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
147 *****************************************************************************/
148 static int Demux( demux_t *p_demux)
150 demux_sys_t *p_sys = p_demux->p_sys;
151 block_t *p_block_in, *p_block_out;
153 if( ( p_block_in = stream_Block( p_demux->s, DIRAC_PACKET_SIZE ) ) == NULL )
160 p_sys->b_first = false;
161 /* by default, timestamps are invalid.
162 * Except when we need an anchor point */
163 #if VLC_TS_INVALID == 0
164 /* xxx: to be removed in 1.1 */
165 p_block_in->i_dts = 1;
167 p_block_in->i_dts = 0;
170 block_t *p_null = block_Alloc( 128 );
173 p_null->i_flags = BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED;
174 p_null->p_next = p_block_in;
179 while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
183 block_t *p_next = p_block_out->p_next;
184 p_block_out->p_next = NULL;
186 if( p_sys->p_es == NULL )
187 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
189 p_block_out->i_dts += p_sys->i_dtsoffset;
190 p_sys->i_dts = p_block_out->i_dts;
192 /* track low watermark for pts_offset -- can be used to show
193 * when it is too large */
194 mtime_t i_delay = p_block_out->i_pts - p_block_out->i_dts;
195 if( p_sys->i_pts_offset_lowtide > i_delay )
197 p_sys->i_pts_offset_lowtide = i_delay;
200 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
201 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
203 p_block_out = p_next;
209 /*****************************************************************************
211 *****************************************************************************/
212 static int Control( demux_t *p_demux, int i_query, va_list args )
214 demux_sys_t *p_sys = p_demux->p_sys;
215 if( DEMUX_GET_TIME == i_query )
217 int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
218 *pi64 = p_sys->i_dts;
221 else if( DEMUX_GET_FPS == i_query )
223 if( !p_sys->p_packetizer->fmt_out.video.i_frame_rate )
227 double *pd = (double*)va_arg( args, double * );
228 *pd = (float) p_sys->p_packetizer->fmt_out.video.i_frame_rate
229 / p_sys->p_packetizer->fmt_out.video.i_frame_rate_base;
234 if( DEMUX_SET_POSITION == i_query || DEMUX_SET_TIME == i_query )
236 p_sys->b_first = true;
238 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );