1 /*****************************************************************************
2 * transrate.c: MPEG2 video transrating module
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc_input.h>
40 #include "transrate.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
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 * );
52 static int transrate_video_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
54 /*****************************************************************************
56 *****************************************************************************/
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 );
66 struct sout_stream_sys_t
71 mtime_t i_shaping_delay;
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 sout_stream_t *p_stream = (sout_stream_t*)p_this;
83 sout_stream_sys_t *p_sys;
86 p_sys = malloc( sizeof( sout_stream_sys_t ) );
87 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
89 p_sys->i_vbitrate = 0;
91 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
93 p_sys->i_vbitrate = atoi( val );
94 if( p_sys->i_vbitrate < 16000 )
96 p_sys->i_vbitrate *= 1000;
101 p_sys->i_vbitrate = 3000000;
104 p_sys->i_shaping_delay = 500000;
105 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "shaping" ) ) )
107 p_sys->i_shaping_delay = (int64_t)atoi( val ) * 1000;
108 if( p_sys->i_shaping_delay <= 0 )
111 "invalid shaping (%"PRId64"ms) reseting to 500ms",
112 p_sys->i_shaping_delay / 1000 );
113 p_sys->i_shaping_delay = 500000;
117 p_sys->b_mpeg4_matrix = 0;
118 if( sout_cfg_find( p_stream->p_cfg, "mpeg4-matrix" ) )
120 p_sys->b_mpeg4_matrix = 1;
123 msg_Dbg( p_stream, "codec video %dkb/s max gop=%"PRId64"us",
124 p_sys->i_vbitrate / 1024, p_sys->i_shaping_delay );
128 msg_Err( p_stream, "cannot create chain" );
132 p_stream->pf_add = Add;
133 p_stream->pf_del = Del;
134 p_stream->pf_send = Send;
136 p_stream->p_sys = p_sys;
141 /*****************************************************************************
143 *****************************************************************************/
144 static void Close( vlc_object_t * p_this )
146 sout_stream_t *p_stream = (sout_stream_t *)p_this;
147 sout_stream_sys_t *p_sys = p_stream->p_sys;
149 sout_StreamDelete( p_sys->p_out );
154 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
156 sout_stream_sys_t *p_sys = p_stream->p_sys;
157 sout_stream_id_t *id;
159 id = malloc( sizeof( sout_stream_id_t ) );
162 if( p_fmt->i_cat == VIDEO_ES
163 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') )
166 "creating video transrating for fcc=`%4.4s'",
167 (char*)&p_fmt->i_codec );
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;
177 /* open output stream */
178 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
179 id->b_transrate = true;
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 = false;
197 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
199 sout_stream_sys_t *p_sys = p_stream->p_sys;
203 p_sys->p_out->pf_del( p_sys->p_out, id->id );
210 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
213 sout_stream_sys_t *p_sys = p_stream->p_sys;
215 if( id->b_transrate )
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 );
223 transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
227 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
231 else if( id->id != NULL )
233 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
237 block_Release( p_buffer );
242 static int transrate_video_process( sout_stream_t *p_stream,
243 sout_stream_id_t *id, block_t *in, block_t **out )
245 transrate_t *tr = &id->tr;
246 bs_transrate_t *bs = &tr->bs;
252 block_t * p_next = in->p_next;
253 int i_flags = in->i_flags;
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;
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) )
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;
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;
275 id->p_current_buffer = id->p_next_gop;
277 while ( id->p_current_buffer != NULL )
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 )
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 );
289 if ( process_frame( p_stream, id, id->p_current_buffer,
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;
299 block_Release( id->p_current_buffer );
302 id->p_current_buffer = p_next;
305 if ( id->tr.i_wanted_output < id->tr.i_total_input )
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);
314 msg_Dbg(p_stream, "%lld -> %lld d=%lld",
315 i_bitrate, i_new_bitrate,
316 id->i_next_gop_duration);
319 id->p_next_gop = NULL;
320 id->i_next_gop_duration = 0;
321 id->i_next_gop_size = 0;