mux:ts: convert vlc_tick_t to seconds explicitly using SEC_FROM_VLC_TICK()
[vlc.git] / modules / misc / stats.c
blob892311c338669d4ee712df1afa6b174cb4e56ee6
1 /*****************************************************************************
2 * stats.c : stats plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2008 the VideoLAN team
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Pierre d'Herbemont <pdherbemont@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 /* Example usage:
29 * $ vlc movie.avi --sout="#transcode{aenc=dummy,venc=stats}:\
30 * std{access=http,mux=dummy,dst=0.0.0.0:8081}"
31 * $ vlc -vv http://127.0.0.1:8081 --demux=stats --vout=stats --codec=stats
34 #define kBufferSize 0x500
36 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_codec.h>
40 #include <vlc_demux.h>
42 /*** Decoder ***/
43 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
45 picture_t * p_pic = NULL;
47 if( p_block == NULL ) /* No Drain */
48 return VLCDEC_SUCCESS;
50 if( !decoder_UpdateVideoFormat( p_dec ) )
51 p_pic = decoder_NewPicture( p_dec );
52 if( !p_pic )
53 goto error;
55 if( p_block->i_buffer == kBufferSize )
57 msg_Dbg( p_dec, "got %"PRIu64" ms",
58 *(vlc_tick_t *)p_block->p_buffer / 1000 );
59 msg_Dbg( p_dec, "got %"PRIu64" ms offset",
60 (vlc_tick_now() - *(vlc_tick_t *)p_block->p_buffer) / 1000 );
61 *(vlc_tick_t *)(p_pic->p->p_pixels) = *(vlc_tick_t *)p_block->p_buffer;
63 else
65 msg_Dbg( p_dec, "got a packet not from stats demuxer" );
66 *(vlc_tick_t *)(p_pic->p->p_pixels) = vlc_tick_now();
69 p_pic->date = p_block->i_pts != VLC_TICK_INVALID ?
70 p_block->i_pts : p_block->i_dts;
71 p_pic->b_force = true;
73 error:
74 block_Release( p_block );
75 decoder_QueueVideo( p_dec, p_pic );
76 return VLCDEC_SUCCESS;
79 static int OpenDecoder ( vlc_object_t *p_this )
81 decoder_t *p_dec = (decoder_t*)p_this;
83 msg_Dbg( p_this, "opening stats decoder" );
85 /* Set callbacks */
86 p_dec->pf_decode = DecodeBlock;
88 /* */
89 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
90 p_dec->fmt_out.video.i_width = 100;
91 p_dec->fmt_out.video.i_height = 100;
92 p_dec->fmt_out.video.i_sar_num = 1;
93 p_dec->fmt_out.video.i_sar_den = 1;
95 return VLC_SUCCESS;
98 /*** Encoder ***/
99 #ifdef ENABLE_SOUT
100 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
102 (void)p_pict;
103 block_t * p_block = block_Alloc( kBufferSize );
105 *(vlc_tick_t*)p_block->p_buffer = vlc_tick_now();
106 p_block->i_buffer = kBufferSize;
107 p_block->i_length = kBufferSize;
108 p_block->i_dts = p_pict->date;
110 msg_Dbg( p_enc, "putting %"PRIu64"ms",
111 *(vlc_tick_t*)p_block->p_buffer / 1000 );
112 return p_block;
115 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_abuff )
117 (void)p_abuff;
118 (void)p_enc;
119 return NULL;
122 static int OpenEncoder ( vlc_object_t *p_this )
124 encoder_t *p_enc = (encoder_t *)p_this;
126 msg_Dbg( p_this, "opening stats encoder" );
128 p_enc->pf_encode_video = EncodeVideo;
129 p_enc->pf_encode_audio = EncodeAudio;
131 return VLC_SUCCESS;
133 #endif
135 /*** Demuxer ***/
136 typedef struct
138 es_format_t fmt;
139 es_out_id_t *p_es;
141 date_t pts;
142 } demux_sys_t;
144 static int Demux( demux_t *p_demux )
146 demux_sys_t *p_sys = p_demux->p_sys;
148 block_t * p_block = vlc_stream_Block( p_demux->s, kBufferSize );
150 if( !p_block ) return 1;
152 p_block->i_dts = p_block->i_pts = date_Increment( &p_sys->pts, kBufferSize );
154 msg_Dbg( p_demux, "demux got %"PRId64" ms offset",
155 (vlc_tick_now() - *(vlc_tick_t *)p_block->p_buffer) / 1000 );
157 //es_out_SetPCR( p_demux->out, p_block->i_pts );
159 es_out_Send( p_demux->out, p_sys->p_es, p_block );
161 return 1;
164 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
166 return demux_vaControlHelper( p_demux->s,
167 0, 0, 0, 1,
168 i_query, args );
171 static int OpenDemux ( vlc_object_t *p_this )
173 demux_t *p_demux = (demux_t*)p_this;
174 demux_sys_t *p_sys;
176 p_demux->p_sys = NULL;
178 /* Only when selected */
179 if( *p_demux->psz_name == '\0' )
180 return VLC_EGENERIC;
182 msg_Dbg( p_demux, "Init Stat demux" );
184 p_demux->pf_demux = Demux;
185 p_demux->pf_control = DemuxControl;
187 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
188 if( !p_demux->p_sys )
189 return VLC_ENOMEM;
191 date_Init( &p_sys->pts, 1, 1 );
192 date_Set( &p_sys->pts, VLC_TICK_0 );
194 es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_FOURCC('s','t','a','t') );
195 p_sys->fmt.video.i_width = 720;
196 p_sys->fmt.video.i_height= 480;
198 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
200 return VLC_SUCCESS;
203 static void CloseDemux ( vlc_object_t *p_this )
205 demux_t *p_demux = (demux_t*)p_this;
207 msg_Dbg( p_demux, "Closing Stat demux" );
209 free( p_demux->p_sys );
212 vlc_module_begin ()
213 set_shortname( N_("Stats"))
214 #ifdef ENABLE_SOUT
215 set_description( N_("Stats encoder function") )
216 set_capability( "encoder", 0 )
217 add_shortcut( "stats" )
218 set_callbacks( OpenEncoder, NULL )
219 add_submodule ()
220 #endif
221 set_section( N_( "Stats decoder" ), NULL )
222 set_description( N_("Stats decoder function") )
223 set_capability( "video decoder", 0 )
224 add_shortcut( "stats" )
225 set_callbacks( OpenDecoder, NULL )
226 add_submodule()
227 set_section( N_( "Stats decoder" ), NULL )
228 set_description( N_("Stats decoder function") )
229 set_capability( "audio decoder", 0 )
230 add_shortcut( "stats" )
231 set_callbacks( OpenDecoder, NULL )
232 add_submodule()
233 set_section( N_( "Stats decoder" ), NULL )
234 set_description( N_("Stats decoder function") )
235 set_capability( "spu decoder", 0 )
236 add_shortcut( "stats" )
237 set_callbacks( OpenDecoder, NULL )
238 add_submodule ()
239 set_section( N_( "Stats demux" ), NULL )
240 set_description( N_("Stats demux function") )
241 set_capability( "demux", 0 )
242 add_shortcut( "stats" )
243 set_callbacks( OpenDemux, CloseDemux )
244 vlc_module_end ()