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