]> git.sesse.net Git - vlc/blob - modules/stream_out/transrate/transrate.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / stream_out / transrate / transrate.c
1 /*****************************************************************************
2  * transrate.c: MPEG2 video transrating module
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          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 #define NDEBUG 1
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <assert.h>
34 #include <math.h>
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_sout.h>
39 #include <vlc_input.h>
40 #include <vlc_block.h>
41
42 #include "transrate.h"
43
44 /*****************************************************************************
45  * Exported prototypes
46  *****************************************************************************/
47 static int      Open    ( vlc_object_t * );
48 static void     Close   ( vlc_object_t * );
49
50 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
51 static int               Del ( sout_stream_t *, sout_stream_id_t * );
52 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
53
54 static int  transrate_video_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
60 #define VB_TEXT N_("Video bitrate")
61 /*xgettext:no-c-format*/
62 #define VB_LONGTEXT N_( \
63     "New target video bitrate. Quality is ok for -10/15\% of the original" \
64     "bitrate." )
65
66 #define SHAPING_TEXT N_("Shaping delay")
67 #define SHAPING_LONGTEXT N_( \
68     "Amount of data used for transrating in ms." )
69
70 #define MPEG4_MATRIX_TEXT N_("Use MPEG4 matrix")
71 #define MPEG4_MATRIX_LONGTEXT N_( \
72     "Use the MPEG4 quantification matrix." )
73
74 #define SOUT_CFG_PREFIX "sout-transrate-"
75
76 vlc_module_begin ()
77     set_category( CAT_SOUT )
78     set_subcategory( SUBCAT_SOUT_STREAM )
79     set_description( N_("MPEG2 video transrating stream output") )
80     set_capability( "sout stream", 50 )
81     add_shortcut( "transrate" )
82     set_callbacks( Open, Close )
83
84     add_integer( SOUT_CFG_PREFIX "vb", 3 * 100 * 1000, NULL,
85                  VB_TEXT, VB_LONGTEXT, false );
86     add_integer( SOUT_CFG_PREFIX "shaping", 500, NULL,
87                  SHAPING_TEXT, SHAPING_LONGTEXT, false );
88     add_bool( SOUT_CFG_PREFIX "mpeg4-matrix", false, NULL,
89               MPEG4_MATRIX_TEXT, MPEG4_MATRIX_LONGTEXT, false );
90 vlc_module_end ()
91
92 static const char *const ppsz_sout_options[] = {
93     "vb", "shaping", "mpeg4-matrix", NULL
94 };
95
96 struct sout_stream_sys_t
97 {
98     sout_stream_t   *p_out;
99
100     int             i_vbitrate;
101     mtime_t         i_shaping_delay;
102     int             b_mpeg4_matrix;
103
104     mtime_t         i_dts, i_pts;
105 };
106
107 /*****************************************************************************
108  * Open:
109  *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
111 {
112     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
113     sout_stream_sys_t *p_sys;
114
115     p_sys = malloc( sizeof( sout_stream_sys_t ) );
116     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
117
118     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
119                        p_stream->p_cfg );
120     p_sys->i_vbitrate = var_CreateGetInteger( p_stream, SOUT_CFG_PREFIX "vb" );
121     if( p_sys->i_vbitrate < 16000 )
122         p_sys->i_vbitrate *= 1000;
123
124     p_sys->i_shaping_delay = var_CreateGetInteger( p_stream,
125                                 SOUT_CFG_PREFIX "shaping" ) * 1000;
126     if( p_sys->i_shaping_delay <= 0 )
127     {
128         msg_Err( p_stream,
129                  "invalid shaping (%"PRId64"ms) reseting to 500ms",
130                  p_sys->i_shaping_delay / 1000 );
131         p_sys->i_shaping_delay = 500000;
132     }
133
134     p_sys->b_mpeg4_matrix = var_CreateGetBool( p_stream,
135                                                SOUT_CFG_PREFIX "mpeg4-matrix" );
136
137     msg_Dbg( p_stream, "codec video %dkb/s max gop=%"PRId64"us",
138              p_sys->i_vbitrate / 1024, p_sys->i_shaping_delay );
139
140     if( !p_sys->p_out )
141     {
142         msg_Err( p_stream, "cannot create chain" );
143         free( p_sys );
144         return VLC_EGENERIC;
145     }
146     p_stream->pf_add    = Add;
147     p_stream->pf_del    = Del;
148     p_stream->pf_send   = Send;
149
150     p_stream->p_sys     = p_sys;
151
152     return VLC_SUCCESS;
153 }
154
155 /*****************************************************************************
156  * Close:
157  *****************************************************************************/
158 static void Close( vlc_object_t * p_this )
159 {
160     sout_stream_t       *p_stream = (sout_stream_t *)p_this;
161     sout_stream_sys_t   *p_sys = p_stream->p_sys;
162
163     sout_StreamDelete( p_sys->p_out );
164     free( p_sys );
165 }
166
167
168 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
169 {
170     sout_stream_sys_t   *p_sys = p_stream->p_sys;
171     sout_stream_id_t    *id;
172
173     id = malloc( sizeof( sout_stream_id_t ) );
174     id->id = NULL;
175
176     if( p_fmt->i_cat == VIDEO_ES
177             && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') )
178     {
179         msg_Dbg( p_stream,
180                  "creating video transrating for fcc=`%4.4s'",
181                  (char*)&p_fmt->i_codec );
182
183         id->p_current_buffer = NULL;
184         id->p_next_gop = NULL;
185         id->i_next_gop_duration = 0;
186         id->i_next_gop_size = 0;
187         memset( &id->tr, 0, sizeof( transrate_t ) );
188         id->tr.bs.i_byte_in = id->tr.bs.i_byte_out = 0;
189         id->tr.mpeg4_matrix = p_sys->b_mpeg4_matrix;
190
191         /* open output stream */
192         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
193         id->b_transrate = true;
194     }
195     else
196     {
197         msg_Dbg( p_stream, "not transrating a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
198         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
199         id->b_transrate = false;
200
201         if( id->id == NULL )
202         {
203             free( id );
204             return NULL;
205         }
206     }
207
208     return id;
209 }
210
211 static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
212 {
213     sout_stream_sys_t   *p_sys = p_stream->p_sys;
214
215     if( id->id )
216     {
217         p_sys->p_out->pf_del( p_sys->p_out, id->id );
218     }
219     free( id );
220
221     return VLC_SUCCESS;
222 }
223
224 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
225                  block_t *p_buffer )
226 {
227     sout_stream_sys_t   *p_sys = p_stream->p_sys;
228
229     if( id->b_transrate )
230     {
231         block_t *p_buffer_out;
232         /* be sure to have at least 8 bytes of padding (maybe only 4) */
233         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + 8 );
234         p_buffer->i_buffer -= 8;
235         memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, 8 );
236
237         transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
238
239         if( p_buffer_out )
240         {
241             return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
242         }
243         return VLC_SUCCESS;
244     }
245     else if( id->id != NULL )
246     {
247         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
248     }
249     else
250     {
251         block_Release( p_buffer );
252         return VLC_EGENERIC;
253     }
254 }
255
256 static int transrate_video_process( sout_stream_t *p_stream,
257                sout_stream_id_t *id, block_t *in, block_t **out )
258 {
259     transrate_t    *tr = &id->tr;
260     bs_transrate_t *bs = &tr->bs;
261
262     *out = NULL;
263
264     while ( in != NULL )
265     {
266         block_t * p_next = in->p_next;
267         int i_flags = in->i_flags;
268
269         in->p_next = NULL;
270         block_ChainAppend( &id->p_next_gop, in );
271         id->i_next_gop_duration += in->i_length;
272         id->i_next_gop_size += in->i_buffer;
273         in = p_next;
274
275         if( ((i_flags & BLOCK_FLAG_TYPE_I )
276                 && id->i_next_gop_duration >= 300000)
277               || (id->i_next_gop_duration > p_stream->p_sys->i_shaping_delay) )
278         {
279             mtime_t i_bitrate = (mtime_t)id->i_next_gop_size * 8000
280                                     / (id->i_next_gop_duration / 1000);
281             mtime_t i_new_bitrate;
282
283             id->tr.i_total_input = id->i_next_gop_size;
284             id->tr.i_remaining_input = id->i_next_gop_size;
285             id->tr.i_wanted_output = (p_stream->p_sys->i_vbitrate)
286                                     * (id->i_next_gop_duration / 1000) / 8000;
287             id->tr.i_current_output = 0;
288
289             id->p_current_buffer = id->p_next_gop;
290
291             while ( id->p_current_buffer != NULL )
292             {
293                 block_t * p_next = id->p_current_buffer->p_next;
294                 if ( !p_stream->p_sys->b_mpeg4_matrix
295                        && id->tr.i_wanted_output >= id->tr.i_total_input )
296                 {
297                     bs->i_byte_out += id->p_current_buffer->i_buffer;
298                     id->p_current_buffer->p_next = NULL;
299                     block_ChainAppend( out, id->p_current_buffer );
300                 }
301                 else
302                 {
303                     if ( process_frame( p_stream, id, id->p_current_buffer,
304                                         out, 0 ) < 0 )
305                     {
306                         id->p_current_buffer->p_next = NULL;
307                         block_ChainAppend( out, id->p_current_buffer );
308                         if ( p_stream->p_sys->b_mpeg4_matrix )
309                             id->tr.i_wanted_output = id->tr.i_total_input;
310                     }
311                     else
312                     {
313                         block_Release( id->p_current_buffer );
314                     }
315                 }
316                 id->p_current_buffer = p_next;
317             }
318
319             if ( id->tr.i_wanted_output < id->tr.i_total_input )
320             {
321                 i_new_bitrate = (mtime_t)tr->i_current_output * 8000
322                                     / (id->i_next_gop_duration / 1000);
323                 if (i_new_bitrate > p_stream->p_sys->i_vbitrate + 300000)
324                     msg_Err(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
325                             i_bitrate, i_new_bitrate,
326                             id->i_next_gop_duration);
327                 else
328                     msg_Dbg(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
329                             i_bitrate, i_new_bitrate,
330                             id->i_next_gop_duration);
331             }
332
333             id->p_next_gop = NULL;
334             id->i_next_gop_duration = 0;
335             id->i_next_gop_size = 0;
336         }
337     }
338
339     return VLC_SUCCESS;
340 }
341