]> git.sesse.net Git - vlc/blob - modules/misc/stats.c
c98932780d3dd6f7edff38f9d3b8431609e2c116
[vlc] / modules / misc / stats.c
1 /*****************************************************************************
2  * stats.c : stats plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2008 the VideoLAN team
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          Pierre d'Herbemont <pdherbemont@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
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
32  */
33
34 #define kBufferSize 0x500
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
39 #include <vlc_demux.h>
40
41 /*** Decoder ***/
42 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
43 {
44     block_t *p_block;
45     picture_t * p_pic = NULL;
46
47     if( !pp_block || !*pp_block ) return NULL;
48     p_block = *pp_block;
49
50     p_pic = decoder_NewPicture( p_dec );
51
52     if( p_block->i_buffer == kBufferSize )
53     {
54         msg_Dbg( p_dec, "got %"PRIu64" ms",
55                  *(mtime_t *)p_block->p_buffer  / 1000 );
56         msg_Dbg( p_dec, "got %"PRIu64" ms offset",
57                  (mdate() - *(mtime_t *)p_block->p_buffer) / 1000 );
58         *(mtime_t *)(p_pic->p->p_pixels) = *(mtime_t *)p_block->p_buffer;
59     }
60     else
61     {
62         msg_Dbg( p_dec, "got a packet not from stats demuxer" );
63         *(mtime_t *)(p_pic->p->p_pixels) = mdate();
64     }
65
66     p_pic->date = p_block->i_pts > VLC_TS_INVALID ?
67             p_block->i_pts : p_block->i_dts;
68     p_pic->b_force = true;
69
70     block_Release( p_block );
71     *pp_block = NULL;
72     return p_pic;
73 }
74
75 static int OpenDecoder ( vlc_object_t *p_this )
76 {
77     decoder_t *p_dec = (decoder_t*)p_this;
78
79     msg_Dbg( p_this, "opening stats decoder" );
80
81     /* Set callbacks */
82     p_dec->pf_decode_video = DecodeBlock;
83     p_dec->pf_decode_audio = NULL;
84     p_dec->pf_decode_sub = NULL;
85
86     /* */
87     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_I420 );
88     p_dec->fmt_out.video.i_width = 100;
89     p_dec->fmt_out.video.i_height = 100;
90     p_dec->fmt_out.video.i_sar_num = 1;
91     p_dec->fmt_out.video.i_sar_den = 1;
92
93     return VLC_SUCCESS;
94 }
95
96 /*** Encoder ***/
97 #ifdef ENABLE_SOUT
98 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
99 {
100     (void)p_pict;
101     block_t * p_block = block_Alloc( kBufferSize );
102
103     *(mtime_t*)p_block->p_buffer = mdate();
104     p_block->i_buffer = kBufferSize;
105     p_block->i_length = kBufferSize;
106     p_block->i_dts = p_pict->date;
107
108     msg_Dbg( p_enc, "putting %"PRIu64"ms",
109              *(mtime_t*)p_block->p_buffer / 1000 );
110     return p_block;
111 }
112
113 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_abuff )
114 {
115     (void)p_abuff;
116     (void)p_enc;
117     return NULL;
118 }
119
120 static int OpenEncoder ( vlc_object_t *p_this )
121 {
122     encoder_t *p_enc = (encoder_t *)p_this;
123
124     msg_Dbg( p_this, "opening stats encoder" );
125
126     p_enc->pf_encode_video = EncodeVideo;
127     p_enc->pf_encode_audio = EncodeAudio;
128
129     return VLC_SUCCESS;
130 }
131 #endif
132
133 /*** Demuxer ***/
134 struct demux_sys_t
135 {
136     es_format_t     fmt;
137     es_out_id_t     *p_es;
138
139     date_t          pts;
140 };
141
142 static int Demux( demux_t *p_demux )
143 {
144     demux_sys_t *p_sys = p_demux->p_sys;
145
146     block_t * p_block = stream_Block( p_demux->s, kBufferSize );
147
148     if( !p_block ) return 1;
149
150     p_block->i_dts = p_block->i_pts =
151         date_Increment( &p_sys->pts, kBufferSize );
152
153     msg_Dbg( p_demux, "demux got %d ms offset", (int)(mdate() - *(mtime_t *)p_block->p_buffer) / 1000 );
154
155     //es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
156
157     es_out_Send( p_demux->out, p_sys->p_es, p_block );
158
159     return 1;
160 }
161
162 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
163 {
164     return demux_vaControlHelper( p_demux->s,
165                                    0, 0, 0, 1,
166                                    i_query, args );
167 }
168
169 static int OpenDemux ( vlc_object_t *p_this )
170 {
171     demux_t *p_demux = (demux_t*)p_this;
172     demux_sys_t *p_sys;
173
174     p_demux->p_sys = NULL;
175
176     /* Only when selected */
177     if( *p_demux->psz_demux == '\0' )
178         return VLC_EGENERIC;
179
180     msg_Dbg( p_demux, "Init Stat demux" );
181
182     p_demux->pf_demux   = Demux;
183     p_demux->pf_control = DemuxControl;
184
185     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
186     if( !p_demux->p_sys )
187         return VLC_ENOMEM;
188
189     date_Init( &p_sys->pts, 1, 1 );
190     date_Set( &p_sys->pts, 1 );
191
192     es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_FOURCC('s','t','a','t') );
193     p_sys->fmt.video.i_width = 720;
194     p_sys->fmt.video.i_height= 480;
195
196     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
197
198     return VLC_SUCCESS;
199 }
200
201 static void CloseDemux ( vlc_object_t *p_this )
202 {
203     demux_t *p_demux = (demux_t*)p_this;
204
205     msg_Dbg( p_demux, "Closing Stat demux" );
206
207     free( p_demux->p_sys );
208 }
209
210 vlc_module_begin ()
211     set_shortname( N_("Stats"))
212 #ifdef ENABLE_SOUT
213     set_description( N_("Stats encoder function") )
214     set_capability( "encoder", 0 )
215     add_shortcut( "stats" )
216     set_callbacks( OpenEncoder, NULL )
217     add_submodule ()
218 #endif
219         set_section( N_( "Stats decoder" ), NULL )
220         set_description( N_("Stats decoder function") )
221         set_capability( "decoder", 0 )
222         add_shortcut( "stats" )
223         set_callbacks( OpenDecoder, NULL )
224     add_submodule ()
225         set_section( N_( "Stats demux" ), NULL )
226         set_description( N_("Stats demux function") )
227         set_capability( "demux", 0 )
228         add_shortcut( "stats" )
229         set_callbacks( OpenDemux, CloseDemux )
230 vlc_module_end ()