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 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
39 #include <vlc_input.h>
40 #include <vlc_block.h>
42 #include "transrate.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
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 * );
54 static int transrate_video_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
56 /*****************************************************************************
58 *****************************************************************************/
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" \
66 #define SHAPING_TEXT N_("Shaping delay")
67 #define SHAPING_LONGTEXT N_( \
68 "Amount of data used for transrating in ms." )
70 #define MPEG4_MATRIX_TEXT N_("Use MPEG4 matrix")
71 #define MPEG4_MATRIX_LONGTEXT N_( \
72 "Use the MPEG4 quantification matrix." )
74 #define SOUT_CFG_PREFIX "sout-transrate-"
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 )
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 );
92 static const char *const ppsz_sout_options[] = {
93 "vb", "shaping", "mpeg4-matrix", NULL
96 struct sout_stream_sys_t
101 mtime_t i_shaping_delay;
104 mtime_t i_dts, i_pts;
107 /*****************************************************************************
109 *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
112 sout_stream_t *p_stream = (sout_stream_t*)p_this;
113 sout_stream_sys_t *p_sys;
115 p_sys = malloc( sizeof( sout_stream_sys_t ) );
116 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
118 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
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;
124 p_sys->i_shaping_delay = var_CreateGetInteger( p_stream,
125 SOUT_CFG_PREFIX "shaping" ) * 1000;
126 if( p_sys->i_shaping_delay <= 0 )
129 "invalid shaping (%"PRId64"ms) reseting to 500ms",
130 p_sys->i_shaping_delay / 1000 );
131 p_sys->i_shaping_delay = 500000;
134 p_sys->b_mpeg4_matrix = var_CreateGetBool( p_stream,
135 SOUT_CFG_PREFIX "mpeg4-matrix" );
137 msg_Dbg( p_stream, "codec video %dkb/s max gop=%"PRId64"us",
138 p_sys->i_vbitrate / 1024, p_sys->i_shaping_delay );
142 msg_Err( p_stream, "cannot create chain" );
146 p_stream->pf_add = Add;
147 p_stream->pf_del = Del;
148 p_stream->pf_send = Send;
150 p_stream->p_sys = p_sys;
155 /*****************************************************************************
157 *****************************************************************************/
158 static void Close( vlc_object_t * p_this )
160 sout_stream_t *p_stream = (sout_stream_t *)p_this;
161 sout_stream_sys_t *p_sys = p_stream->p_sys;
163 sout_StreamDelete( p_sys->p_out );
168 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
170 sout_stream_sys_t *p_sys = p_stream->p_sys;
171 sout_stream_id_t *id;
173 id = malloc( sizeof( sout_stream_id_t ) );
176 if( p_fmt->i_cat == VIDEO_ES
177 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') )
180 "creating video transrating for fcc=`%4.4s'",
181 (char*)&p_fmt->i_codec );
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;
191 /* open output stream */
192 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
193 id->b_transrate = true;
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;
211 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
213 sout_stream_sys_t *p_sys = p_stream->p_sys;
217 p_sys->p_out->pf_del( p_sys->p_out, id->id );
224 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
227 sout_stream_sys_t *p_sys = p_stream->p_sys;
229 if( id->b_transrate )
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 );
237 transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
241 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
245 else if( id->id != NULL )
247 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
251 block_Release( p_buffer );
256 static int transrate_video_process( sout_stream_t *p_stream,
257 sout_stream_id_t *id, block_t *in, block_t **out )
259 transrate_t *tr = &id->tr;
260 bs_transrate_t *bs = &tr->bs;
266 block_t * p_next = in->p_next;
267 int i_flags = in->i_flags;
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;
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) )
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;
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;
289 id->p_current_buffer = id->p_next_gop;
291 while ( id->p_current_buffer != NULL )
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 )
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 );
303 if ( process_frame( p_stream, id, id->p_current_buffer,
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;
313 block_Release( id->p_current_buffer );
316 id->p_current_buffer = p_next;
319 if ( id->tr.i_wanted_output < id->tr.i_total_input )
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);
328 msg_Dbg(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
329 i_bitrate, i_new_bitrate,
330 id->i_next_gop_duration);
333 id->p_next_gop = NULL;
334 id->i_next_gop_duration = 0;
335 id->i_next_gop_size = 0;