1
/*****************************************************************************
2
* transrate.c: MPEG2 video transrating module
3
*****************************************************************************
4
* Copyright (C) 2003 the VideoLAN team
5
* $Id: af73e7c2ef620e53947fb1ee04d43f1093717fbf $
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_shortname( N_("Transrate") )
83
set_callbacks( Open, Close )
85
add_integer( SOUT_CFG_PREFIX "vb", 3 * 100 * 1000, NULL,
86
VB_TEXT, VB_LONGTEXT, false )
87
add_integer( SOUT_CFG_PREFIX "shaping", 500, NULL,
88
SHAPING_TEXT, SHAPING_LONGTEXT, false )
89
add_bool( SOUT_CFG_PREFIX "mpeg4-matrix", false, NULL,
90
MPEG4_MATRIX_TEXT, MPEG4_MATRIX_LONGTEXT, false )
93
static const char *const ppsz_sout_options[] = {
94
"vb", "shaping", "mpeg4-matrix", NULL
97
struct sout_stream_sys_t
102
mtime_t i_shaping_delay;
105
mtime_t i_dts, i_pts;
108
/*****************************************************************************
110
*****************************************************************************/
111
static int Open( vlc_object_t *p_this )
113
sout_stream_t *p_stream = (sout_stream_t*)p_this;
114
sout_stream_sys_t *p_sys;
116
p_sys = malloc( sizeof( sout_stream_sys_t ) );
117
p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
119
config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
121
p_sys->i_vbitrate = var_CreateGetInteger( p_stream, SOUT_CFG_PREFIX "vb" );
122
if( p_sys->i_vbitrate < 16000 )
123
p_sys->i_vbitrate *= 1000;
125
p_sys->i_shaping_delay = var_CreateGetInteger( p_stream,
126
SOUT_CFG_PREFIX "shaping" ) * 1000;
127
if( p_sys->i_shaping_delay <= 0 )
130
"invalid shaping (%"PRId64"ms) reseting to 500ms",
131
p_sys->i_shaping_delay / 1000 );
132
p_sys->i_shaping_delay = 500000;
135
p_sys->b_mpeg4_matrix = var_CreateGetBool( p_stream,
136
SOUT_CFG_PREFIX "mpeg4-matrix" );
138
msg_Dbg( p_stream, "codec video %dkb/s max gop=%"PRId64"us",
139
p_sys->i_vbitrate / 1024, p_sys->i_shaping_delay );
143
msg_Err( p_stream, "cannot create chain" );
147
p_stream->pf_add = Add;
148
p_stream->pf_del = Del;
149
p_stream->pf_send = Send;
151
p_stream->p_sys = p_sys;
156
/*****************************************************************************
158
*****************************************************************************/
159
static void Close( vlc_object_t * p_this )
161
sout_stream_t *p_stream = (sout_stream_t *)p_this;
162
sout_stream_sys_t *p_sys = p_stream->p_sys;
164
sout_StreamDelete( p_sys->p_out );
169
static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
171
sout_stream_sys_t *p_sys = p_stream->p_sys;
172
sout_stream_id_t *id;
174
id = malloc( sizeof( sout_stream_id_t ) );
177
if( p_fmt->i_cat == VIDEO_ES
178
&& p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') )
181
"creating video transrating for fcc=`%4.4s'",
182
(char*)&p_fmt->i_codec );
184
id->p_current_buffer = NULL;
185
id->p_next_gop = NULL;
186
id->i_next_gop_duration = 0;
187
id->i_next_gop_size = 0;
188
memset( &id->tr, 0, sizeof( transrate_t ) );
189
id->tr.bs.i_byte_in = id->tr.bs.i_byte_out = 0;
190
id->tr.mpeg4_matrix = p_sys->b_mpeg4_matrix;
192
/* open output stream */
193
id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
194
id->b_transrate = true;
198
msg_Dbg( p_stream, "not transrating a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
199
id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
200
id->b_transrate = false;
212
static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
214
sout_stream_sys_t *p_sys = p_stream->p_sys;
218
p_sys->p_out->pf_del( p_sys->p_out, id->id );
225
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
228
sout_stream_sys_t *p_sys = p_stream->p_sys;
230
if( id->b_transrate )
232
block_t *p_buffer_out;
233
/* be sure to have at least 8 bytes of padding (maybe only 4) */
234
p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + 8 );
235
p_buffer->i_buffer -= 8;
236
memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, 8 );
238
transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
242
return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
246
else if( id->id != NULL )
248
return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
252
block_Release( p_buffer );
257
static int transrate_video_process( sout_stream_t *p_stream,
258
sout_stream_id_t *id, block_t *in, block_t **out )
260
transrate_t *tr = &id->tr;
261
bs_transrate_t *bs = &tr->bs;
267
block_t * p_next = in->p_next;
268
int i_flags = in->i_flags;
271
block_ChainAppend( &id->p_next_gop, in );
272
id->i_next_gop_duration += in->i_length;
273
id->i_next_gop_size += in->i_buffer;
276
if( ((i_flags & BLOCK_FLAG_TYPE_I )
277
&& id->i_next_gop_duration >= 300000)
278
|| (id->i_next_gop_duration > p_stream->p_sys->i_shaping_delay) )
280
mtime_t i_bitrate = (mtime_t)id->i_next_gop_size * 8000
281
/ (id->i_next_gop_duration / 1000);
282
mtime_t i_new_bitrate;
284
id->tr.i_total_input = id->i_next_gop_size;
285
id->tr.i_remaining_input = id->i_next_gop_size;
286
id->tr.i_wanted_output = (p_stream->p_sys->i_vbitrate)
287
* (id->i_next_gop_duration / 1000) / 8000;
288
id->tr.i_current_output = 0;
290
id->p_current_buffer = id->p_next_gop;
292
while ( id->p_current_buffer != NULL )
294
block_t * p_next = id->p_current_buffer->p_next;
295
if ( !p_stream->p_sys->b_mpeg4_matrix
296
&& id->tr.i_wanted_output >= id->tr.i_total_input )
298
bs->i_byte_out += id->p_current_buffer->i_buffer;
299
id->p_current_buffer->p_next = NULL;
300
block_ChainAppend( out, id->p_current_buffer );
304
if ( process_frame( p_stream, id, id->p_current_buffer,
307
id->p_current_buffer->p_next = NULL;
308
block_ChainAppend( out, id->p_current_buffer );
309
if ( p_stream->p_sys->b_mpeg4_matrix )
310
id->tr.i_wanted_output = id->tr.i_total_input;
314
block_Release( id->p_current_buffer );
317
id->p_current_buffer = p_next;
320
if ( id->tr.i_wanted_output < id->tr.i_total_input )
322
i_new_bitrate = (mtime_t)tr->i_current_output * 8000
323
/ (id->i_next_gop_duration / 1000);
324
if (i_new_bitrate > p_stream->p_sys->i_vbitrate + 300000)
325
msg_Err(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
326
i_bitrate, i_new_bitrate,
327
id->i_next_gop_duration);
329
msg_Dbg(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
330
i_bitrate, i_new_bitrate,
331
id->i_next_gop_duration);
334
id->p_next_gop = NULL;
335
id->i_next_gop_duration = 0;
336
id->i_next_gop_size = 0;