video_filter: erase: use C99 loop declarations
[vlc.git] / modules / misc / stats.c
blobc2cb2e6fd7125d4eea9a3f02c5192b6096000dcc
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 -vvv 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 picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
45 block_t *p_block;
46 picture_t * p_pic = NULL;
48 if( !pp_block || !*pp_block ) return NULL;
49 p_block = *pp_block;
51 p_pic = decoder_NewPicture( p_dec );
53 if( p_block->i_buffer == kBufferSize )
55 msg_Dbg( p_dec, "got %"PRIu64" ms",
56 *(mtime_t *)p_block->p_buffer / 1000 );
57 msg_Dbg( p_dec, "got %"PRIu64" ms offset",
58 (mdate() - *(mtime_t *)p_block->p_buffer) / 1000 );
59 *(mtime_t *)(p_pic->p->p_pixels) = *(mtime_t *)p_block->p_buffer;
61 else
63 msg_Dbg( p_dec, "got a packet not from stats demuxer" );
64 *(mtime_t *)(p_pic->p->p_pixels) = mdate();
67 p_pic->date = p_block->i_pts > VLC_TS_INVALID ?
68 p_block->i_pts : p_block->i_dts;
69 p_pic->b_force = true;
71 block_Release( p_block );
72 *pp_block = NULL;
73 return p_pic;
76 static int OpenDecoder ( vlc_object_t *p_this )
78 decoder_t *p_dec = (decoder_t*)p_this;
80 msg_Dbg( p_this, "opening stats decoder" );
82 /* Set callbacks */
83 p_dec->pf_decode_video = DecodeBlock;
84 p_dec->pf_decode_audio = NULL;
85 p_dec->pf_decode_sub = NULL;
87 /* */
88 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_I420 );
89 p_dec->fmt_out.video.i_width = 100;
90 p_dec->fmt_out.video.i_height = 100;
91 p_dec->fmt_out.video.i_sar_num = 1;
92 p_dec->fmt_out.video.i_sar_den = 1;
94 return VLC_SUCCESS;
97 /*** Encoder ***/
98 #ifdef ENABLE_SOUT
99 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
101 (void)p_pict;
102 block_t * p_block = block_Alloc( kBufferSize );
104 *(mtime_t*)p_block->p_buffer = mdate();
105 p_block->i_buffer = kBufferSize;
106 p_block->i_length = kBufferSize;
107 p_block->i_dts = p_pict->date;
109 msg_Dbg( p_enc, "putting %"PRIu64"ms",
110 *(mtime_t*)p_block->p_buffer / 1000 );
111 return p_block;
114 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_abuff )
116 (void)p_abuff;
117 (void)p_enc;
118 return NULL;
121 static int OpenEncoder ( vlc_object_t *p_this )
123 encoder_t *p_enc = (encoder_t *)p_this;
125 msg_Dbg( p_this, "opening stats encoder" );
127 p_enc->pf_encode_video = EncodeVideo;
128 p_enc->pf_encode_audio = EncodeAudio;
130 return VLC_SUCCESS;
132 #endif
134 /*** Demuxer ***/
135 struct demux_sys_t
137 es_format_t fmt;
138 es_out_id_t *p_es;
140 date_t pts;
143 static int Demux( demux_t *p_demux )
145 demux_sys_t *p_sys = p_demux->p_sys;
147 block_t * p_block = stream_Block( p_demux->s, kBufferSize );
149 if( !p_block ) return 1;
151 p_block->i_dts = p_block->i_pts =
152 date_Increment( &p_sys->pts, kBufferSize );
154 msg_Dbg( p_demux, "demux got %d ms offset", (int)(mdate() - *(mtime_t *)p_block->p_buffer) / 1000 );
156 //es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
158 es_out_Send( p_demux->out, p_sys->p_es, p_block );
160 return 1;
163 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
165 return demux_vaControlHelper( p_demux->s,
166 0, 0, 0, 1,
167 i_query, args );
170 static int OpenDemux ( vlc_object_t *p_this )
172 demux_t *p_demux = (demux_t*)p_this;
173 demux_sys_t *p_sys;
175 p_demux->p_sys = NULL;
177 /* Only when selected */
178 if( *p_demux->psz_demux == '\0' )
179 return VLC_EGENERIC;
181 msg_Dbg( p_demux, "Init Stat demux" );
183 p_demux->pf_demux = Demux;
184 p_demux->pf_control = DemuxControl;
186 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
187 if( !p_demux->p_sys )
188 return VLC_ENOMEM;
190 date_Init( &p_sys->pts, 1, 1 );
191 date_Set( &p_sys->pts, 1 );
193 es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_FOURCC('s','t','a','t') );
194 p_sys->fmt.video.i_width = 720;
195 p_sys->fmt.video.i_height= 480;
197 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
199 return VLC_SUCCESS;
202 static void CloseDemux ( vlc_object_t *p_this )
204 demux_t *p_demux = (demux_t*)p_this;
206 msg_Dbg( p_demux, "Closing Stat demux" );
208 free( p_demux->p_sys );
211 vlc_module_begin ()
212 set_shortname( N_("Stats"))
213 #ifdef ENABLE_SOUT
214 set_description( N_("Stats encoder function") )
215 set_capability( "encoder", 0 )
216 add_shortcut( "stats" )
217 set_callbacks( OpenEncoder, NULL )
218 add_submodule ()
219 #endif
220 set_section( N_( "Stats decoder" ), NULL )
221 set_description( N_("Stats decoder function") )
222 set_capability( "decoder", 0 )
223 add_shortcut( "stats" )
224 set_callbacks( OpenDecoder, NULL )
225 add_submodule ()
226 set_section( N_( "Stats demux" ), NULL )
227 set_description( N_("Stats demux function") )
228 set_capability( "demux", 0 )
229 add_shortcut( "stats" )
230 set_callbacks( OpenDemux, CloseDemux )
231 vlc_module_end ()