]> git.sesse.net Git - vlc/blob - modules/misc/stats.c
mediacodec: factorize release_output_buffer
[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 #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>
41
42 /*** Decoder ***/
43 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
44 {
45     block_t *p_block;
46     picture_t * p_pic = NULL;
47
48     if( !pp_block || !*pp_block ) return NULL;
49     p_block = *pp_block;
50
51     p_pic = decoder_NewPicture( p_dec );
52
53     if( p_block->i_buffer == kBufferSize )
54     {
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;
60     }
61     else
62     {
63         msg_Dbg( p_dec, "got a packet not from stats demuxer" );
64         *(mtime_t *)(p_pic->p->p_pixels) = mdate();
65     }
66
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;
70
71     block_Release( p_block );
72     *pp_block = NULL;
73     return p_pic;
74 }
75
76 static int OpenDecoder ( vlc_object_t *p_this )
77 {
78     decoder_t *p_dec = (decoder_t*)p_this;
79
80     msg_Dbg( p_this, "opening stats decoder" );
81
82     /* Set callbacks */
83     p_dec->pf_decode_video = DecodeBlock;
84     p_dec->pf_decode_audio = NULL;
85     p_dec->pf_decode_sub = NULL;
86
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;
93
94     return VLC_SUCCESS;
95 }
96
97 /*** Encoder ***/
98 #ifdef ENABLE_SOUT
99 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict )
100 {
101     (void)p_pict;
102     block_t * p_block = block_Alloc( kBufferSize );
103
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;
108
109     msg_Dbg( p_enc, "putting %"PRIu64"ms",
110              *(mtime_t*)p_block->p_buffer / 1000 );
111     return p_block;
112 }
113
114 static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_abuff )
115 {
116     (void)p_abuff;
117     (void)p_enc;
118     return NULL;
119 }
120
121 static int OpenEncoder ( vlc_object_t *p_this )
122 {
123     encoder_t *p_enc = (encoder_t *)p_this;
124
125     msg_Dbg( p_this, "opening stats encoder" );
126
127     p_enc->pf_encode_video = EncodeVideo;
128     p_enc->pf_encode_audio = EncodeAudio;
129
130     return VLC_SUCCESS;
131 }
132 #endif
133
134 /*** Demuxer ***/
135 struct demux_sys_t
136 {
137     es_format_t     fmt;
138     es_out_id_t     *p_es;
139
140     date_t          pts;
141 };
142
143 static int Demux( demux_t *p_demux )
144 {
145     demux_sys_t *p_sys = p_demux->p_sys;
146
147     block_t * p_block = stream_Block( p_demux->s, kBufferSize );
148
149     if( !p_block ) return 1;
150
151     p_block->i_dts = p_block->i_pts =
152         date_Increment( &p_sys->pts, kBufferSize );
153
154     msg_Dbg( p_demux, "demux got %d ms offset", (int)(mdate() - *(mtime_t *)p_block->p_buffer) / 1000 );
155
156     //es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
157
158     es_out_Send( p_demux->out, p_sys->p_es, p_block );
159
160     return 1;
161 }
162
163 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
164 {
165     return demux_vaControlHelper( p_demux->s,
166                                    0, 0, 0, 1,
167                                    i_query, args );
168 }
169
170 static int OpenDemux ( vlc_object_t *p_this )
171 {
172     demux_t *p_demux = (demux_t*)p_this;
173     demux_sys_t *p_sys;
174
175     p_demux->p_sys = NULL;
176
177     /* Only when selected */
178     if( *p_demux->psz_demux == '\0' )
179         return VLC_EGENERIC;
180
181     msg_Dbg( p_demux, "Init Stat demux" );
182
183     p_demux->pf_demux   = Demux;
184     p_demux->pf_control = DemuxControl;
185
186     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
187     if( !p_demux->p_sys )
188         return VLC_ENOMEM;
189
190     date_Init( &p_sys->pts, 1, 1 );
191     date_Set( &p_sys->pts, 1 );
192
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;
196
197     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
198
199     return VLC_SUCCESS;
200 }
201
202 static void CloseDemux ( vlc_object_t *p_this )
203 {
204     demux_t *p_demux = (demux_t*)p_this;
205
206     msg_Dbg( p_demux, "Closing Stat demux" );
207
208     free( p_demux->p_sys );
209 }
210
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 ()