]> git.sesse.net Git - vlc/blob - modules/demux/dirac.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / demux / dirac.c
1 /*****************************************************************************
2  * dirac.c : Dirac Video demuxer
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: David Flynn <davidf@rd.bbc.co.uk>
8  * Based on vc1.c by: Laurent Aimar <fenrir@via.ecp.fr>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_codec.h>
37
38 #define DEMUX_CFG_PREFIX "dirac-"
39
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
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
49
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, NULL,
57                  DEMUX_DTSOFFSET_TEXT, DEMUX_DTSOFFSET_LONGTEXT, false )
58     set_callbacks( Open, Close );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 struct demux_sys_t
65 {
66     mtime_t i_dts;
67     mtime_t i_dtsoffset;
68     mtime_t i_pts_offset_lowtide;
69     es_out_id_t *p_es;
70
71     enum {
72         /* demuxer states, do not reorder (++ is used) */
73         DIRAC_DEMUX_DISCONT = 0, /* signal a discontinuity to packetizer */
74         DIRAC_DEMUX_FIRST, /* provide an origin timestamp for the packetizer */
75         DIRAC_DEMUX_STEADY, /* normal operation */
76     } i_state;
77
78     decoder_t *p_packetizer;
79 };
80
81 static int Demux( demux_t * );
82 static int Control( demux_t *, int, va_list );
83
84 #define DIRAC_PACKET_SIZE 4096
85
86 /*****************************************************************************
87  * Open: initializes demux structures
88  *****************************************************************************/
89 static int Open( vlc_object_t * p_this )
90 {
91     demux_t     *p_demux = (demux_t*)p_this;
92     demux_sys_t *p_sys;
93     const uint8_t *p_peek;
94     es_format_t fmt;
95
96     if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
97
98     if( p_peek[0] != 'B' || p_peek[1] != 'B' ||
99         p_peek[2] != 'C' || p_peek[3] != 'D') /* start of ParseInfo */
100     {
101         if( !p_demux->b_force ) return VLC_EGENERIC;
102
103         msg_Err( p_demux, "This doesn't look like a Dirac stream (incorrect parsecode)" );
104         msg_Warn( p_demux, "continuing anyway" );
105     }
106
107     p_demux->pf_demux = Demux;
108     p_demux->pf_control = Control;
109     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
110     if( !p_sys ) return VLC_ENOMEM;
111
112     p_sys->i_pts_offset_lowtide = INT64_MAX;
113     p_sys->i_state = DIRAC_DEMUX_FIRST;
114
115     p_sys->i_dtsoffset = var_CreateGetInteger( p_demux, DEMUX_CFG_PREFIX DEMUX_DTSOFFSET );
116
117     /* Load the packetizer */
118     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_DIRAC );
119     p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, "dirac" );
120     if( !p_sys->p_packetizer )
121     {
122         free( p_sys );
123         return VLC_EGENERIC;
124     }
125
126     return VLC_SUCCESS;
127 }
128
129 /*****************************************************************************
130  * Close: frees unused data
131  *****************************************************************************/
132 static void Close( vlc_object_t * p_this )
133 {
134     demux_t     *p_demux = (demux_t*)p_this;
135     demux_sys_t *p_sys = p_demux->p_sys;
136
137     demux_PacketizerDestroy( p_sys->p_packetizer );
138
139     if( p_sys->i_pts_offset_lowtide < INT64_MAX &&
140         p_sys->i_pts_offset_lowtide > 0 )
141     {
142         msg_Warn( p_demux, "For all packets seen, pts-dts (%"PRId64") could be reduced to 0",
143                   p_sys->i_pts_offset_lowtide );
144     }
145     free( p_sys );
146 }
147
148 /*****************************************************************************
149  * Demux: reads and demuxes data packets
150  *****************************************************************************
151  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
152  *****************************************************************************/
153 static int Demux( demux_t *p_demux)
154 {
155     demux_sys_t *p_sys = p_demux->p_sys;
156     block_t *p_block_in, *p_block_out;
157
158     if( p_sys->i_state == DIRAC_DEMUX_DISCONT )
159     {
160         p_sys->i_state++;
161         p_block_in = block_Alloc( 128 );
162         if( p_block_in )
163         {
164             p_block_in->i_flags = BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED;
165         }
166     }
167     else
168     {
169         p_block_in = stream_Block( p_demux->s, DIRAC_PACKET_SIZE );
170         if( !p_block_in )
171         {
172             return 0;
173         }
174         if ( p_sys->i_state == DIRAC_DEMUX_FIRST)
175         {
176             p_sys->i_state++;
177             /* by default, timestamps are invalid.
178              * Except when we need an anchor point */
179             p_block_in->i_dts = VLC_TS_0;
180         }
181     }
182
183     while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
184     {
185         while( p_block_out )
186         {
187             block_t *p_next = p_block_out->p_next;
188             p_block_out->p_next = NULL;
189
190             if( p_sys->p_es == NULL )
191                 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
192
193             p_block_out->i_dts += p_sys->i_dtsoffset;
194             p_sys->i_dts = p_block_out->i_dts;
195
196             /* track low watermark for pts_offset -- can be used to show
197              * when it is too large */
198             mtime_t i_delay = p_block_out->i_pts - p_block_out->i_dts;
199             if( p_sys->i_pts_offset_lowtide > i_delay )
200             {
201                 p_sys->i_pts_offset_lowtide = i_delay;
202             }
203
204             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
205             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
206
207             p_block_out = p_next;
208         }
209     }
210     return 1;
211 }
212
213 /*****************************************************************************
214  * Control:
215  *****************************************************************************/
216 static int Control( demux_t *p_demux, int i_query, va_list args )
217 {
218     demux_sys_t *p_sys  = p_demux->p_sys;
219     if( DEMUX_GET_TIME == i_query )
220     {
221         int64_t *pi64 = (int64_t*)va_arg( args, int64_t * );
222         *pi64 = p_sys->i_dts;
223         return VLC_SUCCESS;
224     }
225     else if( DEMUX_GET_FPS == i_query )
226     {
227         if( !p_sys->p_packetizer->fmt_out.video.i_frame_rate )
228         {
229             return VLC_EGENERIC;
230         }
231         double *pd = (double*)va_arg( args, double * );
232         *pd = (float) p_sys->p_packetizer->fmt_out.video.i_frame_rate
233             / p_sys->p_packetizer->fmt_out.video.i_frame_rate_base;
234         return VLC_SUCCESS;
235     }
236     else
237     {
238         if( DEMUX_SET_POSITION == i_query || DEMUX_SET_TIME == i_query )
239         {
240             p_sys->i_state = DIRAC_DEMUX_DISCONT;
241         }
242         return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
243     }
244 }
245