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