~ubuntu-branches/ubuntu/lucid/vlc/lucid-security

« back to all changes in this revision

Viewing changes to modules/stream_out/transrate/transrate.c

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2009-09-25 14:44:17 UTC
  • mfrom: (3.5.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090925144417-87vomt575d0agvqa
Tags: 1.0.2-1ubuntu1
* Merge from Debian unstable (LP: #435524), remaining changes:
  - build against xulrunner-dev instead of iceape-dev
  - build against libx264-dev and install libx264 plugin
  - add Xb-Npp header to vlc package
  - recommend vlc-plugin-pulse for vlc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 * transrate.c: MPEG2 video transrating module
3
 
 *****************************************************************************
4
 
 * Copyright (C) 2003 the VideoLAN team
5
 
 * $Id: af73e7c2ef620e53947fb1ee04d43f1093717fbf $
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 
 *****************************************************************************/
24
 
 
25
 
/*****************************************************************************
26
 
 * Preamble
27
 
 *****************************************************************************/
28
 
#define NDEBUG 1
29
 
#ifdef HAVE_CONFIG_H
30
 
# include "config.h"
31
 
#endif
32
 
 
33
 
#include <assert.h>
34
 
#include <math.h>
35
 
 
36
 
#include <vlc_common.h>
37
 
#include <vlc_plugin.h>
38
 
#include <vlc_sout.h>
39
 
#include <vlc_input.h>
40
 
#include <vlc_block.h>
41
 
 
42
 
#include "transrate.h"
43
 
 
44
 
/*****************************************************************************
45
 
 * Exported prototypes
46
 
 *****************************************************************************/
47
 
static int      Open    ( vlc_object_t * );
48
 
static void     Close   ( vlc_object_t * );
49
 
 
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 * );
53
 
 
54
 
static int  transrate_video_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
55
 
 
56
 
/*****************************************************************************
57
 
 * Module descriptor
58
 
 *****************************************************************************/
59
 
 
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" \
64
 
    "bitrate." )
65
 
 
66
 
#define SHAPING_TEXT N_("Shaping delay")
67
 
#define SHAPING_LONGTEXT N_( \
68
 
    "Amount of data used for transrating in ms." )
69
 
 
70
 
#define MPEG4_MATRIX_TEXT N_("Use MPEG4 matrix")
71
 
#define MPEG4_MATRIX_LONGTEXT N_( \
72
 
    "Use the MPEG4 quantification matrix." )
73
 
 
74
 
#define SOUT_CFG_PREFIX "sout-transrate-"
75
 
 
76
 
vlc_module_begin ()
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 )
84
 
 
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 )
91
 
vlc_module_end ()
92
 
 
93
 
static const char *const ppsz_sout_options[] = {
94
 
    "vb", "shaping", "mpeg4-matrix", NULL
95
 
};
96
 
 
97
 
struct sout_stream_sys_t
98
 
{
99
 
    sout_stream_t   *p_out;
100
 
 
101
 
    int             i_vbitrate;
102
 
    mtime_t         i_shaping_delay;
103
 
    int             b_mpeg4_matrix;
104
 
 
105
 
    mtime_t         i_dts, i_pts;
106
 
};
107
 
 
108
 
/*****************************************************************************
109
 
 * Open:
110
 
 *****************************************************************************/
111
 
static int Open( vlc_object_t *p_this )
112
 
{
113
 
    sout_stream_t     *p_stream = (sout_stream_t*)p_this;
114
 
    sout_stream_sys_t *p_sys;
115
 
 
116
 
    p_sys = malloc( sizeof( sout_stream_sys_t ) );
117
 
    p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
118
 
 
119
 
    config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
120
 
                       p_stream->p_cfg );
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;
124
 
 
125
 
    p_sys->i_shaping_delay = var_CreateGetInteger( p_stream,
126
 
                                SOUT_CFG_PREFIX "shaping" ) * 1000;
127
 
    if( p_sys->i_shaping_delay <= 0 )
128
 
    {
129
 
        msg_Err( p_stream,
130
 
                 "invalid shaping (%"PRId64"ms) reseting to 500ms",
131
 
                 p_sys->i_shaping_delay / 1000 );
132
 
        p_sys->i_shaping_delay = 500000;
133
 
    }
134
 
 
135
 
    p_sys->b_mpeg4_matrix = var_CreateGetBool( p_stream,
136
 
                                               SOUT_CFG_PREFIX "mpeg4-matrix" );
137
 
 
138
 
    msg_Dbg( p_stream, "codec video %dkb/s max gop=%"PRId64"us",
139
 
             p_sys->i_vbitrate / 1024, p_sys->i_shaping_delay );
140
 
 
141
 
    if( !p_sys->p_out )
142
 
    {
143
 
        msg_Err( p_stream, "cannot create chain" );
144
 
        free( p_sys );
145
 
        return VLC_EGENERIC;
146
 
    }
147
 
    p_stream->pf_add    = Add;
148
 
    p_stream->pf_del    = Del;
149
 
    p_stream->pf_send   = Send;
150
 
 
151
 
    p_stream->p_sys     = p_sys;
152
 
 
153
 
    return VLC_SUCCESS;
154
 
}
155
 
 
156
 
/*****************************************************************************
157
 
 * Close:
158
 
 *****************************************************************************/
159
 
static void Close( vlc_object_t * p_this )
160
 
{
161
 
    sout_stream_t       *p_stream = (sout_stream_t *)p_this;
162
 
    sout_stream_sys_t   *p_sys = p_stream->p_sys;
163
 
 
164
 
    sout_StreamDelete( p_sys->p_out );
165
 
    free( p_sys );
166
 
}
167
 
 
168
 
 
169
 
static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
170
 
{
171
 
    sout_stream_sys_t   *p_sys = p_stream->p_sys;
172
 
    sout_stream_id_t    *id;
173
 
 
174
 
    id = malloc( sizeof( sout_stream_id_t ) );
175
 
    id->id = NULL;
176
 
 
177
 
    if( p_fmt->i_cat == VIDEO_ES
178
 
            && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') )
179
 
    {
180
 
        msg_Dbg( p_stream,
181
 
                 "creating video transrating for fcc=`%4.4s'",
182
 
                 (char*)&p_fmt->i_codec );
183
 
 
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;
191
 
 
192
 
        /* open output stream */
193
 
        id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
194
 
        id->b_transrate = true;
195
 
    }
196
 
    else
197
 
    {
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;
201
 
 
202
 
        if( id->id == NULL )
203
 
        {
204
 
            free( id );
205
 
            return NULL;
206
 
        }
207
 
    }
208
 
 
209
 
    return id;
210
 
}
211
 
 
212
 
static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
213
 
{
214
 
    sout_stream_sys_t   *p_sys = p_stream->p_sys;
215
 
 
216
 
    if( id->id )
217
 
    {
218
 
        p_sys->p_out->pf_del( p_sys->p_out, id->id );
219
 
    }
220
 
    free( id );
221
 
 
222
 
    return VLC_SUCCESS;
223
 
}
224
 
 
225
 
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
226
 
                 block_t *p_buffer )
227
 
{
228
 
    sout_stream_sys_t   *p_sys = p_stream->p_sys;
229
 
 
230
 
    if( id->b_transrate )
231
 
    {
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 );
237
 
 
238
 
        transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
239
 
 
240
 
        if( p_buffer_out )
241
 
        {
242
 
            return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
243
 
        }
244
 
        return VLC_SUCCESS;
245
 
    }
246
 
    else if( id->id != NULL )
247
 
    {
248
 
        return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
249
 
    }
250
 
    else
251
 
    {
252
 
        block_Release( p_buffer );
253
 
        return VLC_EGENERIC;
254
 
    }
255
 
}
256
 
 
257
 
static int transrate_video_process( sout_stream_t *p_stream,
258
 
               sout_stream_id_t *id, block_t *in, block_t **out )
259
 
{
260
 
    transrate_t    *tr = &id->tr;
261
 
    bs_transrate_t *bs = &tr->bs;
262
 
 
263
 
    *out = NULL;
264
 
 
265
 
    while ( in != NULL )
266
 
    {
267
 
        block_t * p_next = in->p_next;
268
 
        int i_flags = in->i_flags;
269
 
 
270
 
        in->p_next = NULL;
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;
274
 
        in = p_next;
275
 
 
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) )
279
 
        {
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;
283
 
 
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;
289
 
 
290
 
            id->p_current_buffer = id->p_next_gop;
291
 
 
292
 
            while ( id->p_current_buffer != NULL )
293
 
            {
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 )
297
 
                {
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 );
301
 
                }
302
 
                else
303
 
                {
304
 
                    if ( process_frame( p_stream, id, id->p_current_buffer,
305
 
                                        out, 0 ) < 0 )
306
 
                    {
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;
311
 
                    }
312
 
                    else
313
 
                    {
314
 
                        block_Release( id->p_current_buffer );
315
 
                    }
316
 
                }
317
 
                id->p_current_buffer = p_next;
318
 
            }
319
 
 
320
 
            if ( id->tr.i_wanted_output < id->tr.i_total_input )
321
 
            {
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);
328
 
                else
329
 
                    msg_Dbg(p_stream, "%"PRId64" -> %"PRId64" d=%"PRId64,
330
 
                            i_bitrate, i_new_bitrate,
331
 
                            id->i_next_gop_duration);
332
 
            }
333
 
 
334
 
            id->p_next_gop = NULL;
335
 
            id->i_next_gop_duration = 0;
336
 
            id->i_next_gop_size = 0;
337
 
        }
338
 
    }
339
 
 
340
 
    return VLC_SUCCESS;
341
 
}
342