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