transcode.c 98.3 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
2
 * transcode.c: transcoding stream output module
3
 *****************************************************************************
4
 * Copyright (C) 2003-2004 the VideoLAN team
5
 * $Id: f9dc400b8ad7bfcfce8fb34629b588353d176a3b $
6 7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9
 *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29 30
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <string.h>
31
#include <math.h>
32 33 34 35

#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/sout.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
36 37
#include <vlc/vout.h>
#include <vlc/decoder.h>
38
#include "vlc_filter.h"
39
#include "vlc_osd.h"
40

41 42
#define MASTER_SYNC_MAX_DRIFT 100000

43
/*****************************************************************************
44
 * Module descriptor
45
 *****************************************************************************/
46 47
#define VENC_TEXT N_("Video encoder")
#define VENC_LONGTEXT N_( \
48 49
    "This is the video encoder module that will be used (and its associated "\
    "options).")
50 51
#define VCODEC_TEXT N_("Destination video codec")
#define VCODEC_LONGTEXT N_( \
52
    "This is the video codec that will be used.")
53 54
#define VB_TEXT N_("Video bitrate")
#define VB_LONGTEXT N_( \
55
    "Target bitrate of the transcoded video stream." )
56 57
#define SCALE_TEXT N_("Video scaling")
#define SCALE_LONGTEXT N_( \
58
    "Scale factor to apply to the video while transcoding (eg: 0.25)")
59 60
#define FPS_TEXT N_("Video frame-rate")
#define FPS_LONGTEXT N_( \
61
    "Target output frame rate for the video stream." )
62 63
#define DEINTERLACE_TEXT N_("Deinterlace video")
#define DEINTERLACE_LONGTEXT N_( \
64
    "Deinterlace the video before encoding." )
65 66
#define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
#define DEINTERLACE_MODULE_LONGTEXT N_( \
67
    "Specify the deinterlace module to use." )
68 69
#define WIDTH_TEXT N_("Video width")
#define WIDTH_LONGTEXT N_( \
70
    "Output video width." )
71 72
#define HEIGHT_TEXT N_("Video height")
#define HEIGHT_LONGTEXT N_( \
73
    "Output video height." )
74 75
#define MAXWIDTH_TEXT N_("Maximum video width")
#define MAXWIDTH_LONGTEXT N_( \
76
    "Maximum output video width." )
77 78
#define MAXHEIGHT_TEXT N_("Maximum video height")
#define MAXHEIGHT_LONGTEXT N_( \
79
    "Maximum output video height." )
80 81
#define VFILTER_TEXT N_("Video filter")
#define VFILTER_LONGTEXT N_( \
82 83
    "Video filters will be applied to the video streams (after overlays " \
    "are applied). You must enter a comma-separated list of filters." )
84

85
#define CROPTOP_TEXT N_("Video crop (top)")
86
#define CROPTOP_LONGTEXT N_( \
87 88
    "Number of pixels to crop at the top of the video." )
#define CROPLEFT_TEXT N_("Video crop (left)")
89
#define CROPLEFT_LONGTEXT N_( \
90 91
    "Number of pixels to crop at the left of the video." )
#define CROPBOTTOM_TEXT N_("Video crop (bottom)")
92
#define CROPBOTTOM_LONGTEXT N_( \
93 94
    "Number of pixels to crop at the bottom of the video." )
#define CROPRIGHT_TEXT N_("Video crop (right)")
95
#define CROPRIGHT_LONGTEXT N_( \
96
    "Number of pixels to crop at the right of the video." )
97

98
#define PADDTOP_TEXT N_("Video padding (top)")
99
#define PADDTOP_LONGTEXT N_( \
100 101
    "Size of the black border to add at the top of the video." )
#define PADDLEFT_TEXT N_("Video padding (left)")
102
#define PADDLEFT_LONGTEXT N_( \
103 104
    "Size of the black border to add at the left of the video." )
#define PADDBOTTOM_TEXT N_("Video padding (bottom)")
105
#define PADDBOTTOM_LONGTEXT N_( \
106 107
    "Size of the black border to add at the bottom of the video." )
#define PADDRIGHT_TEXT N_("Video padding (right)")
108
#define PADDRIGHT_LONGTEXT N_( \
109
    "Size of the black border to add at the right of the video." )
110 111

#define CANVAS_WIDTH_TEXT N_("Video canvas width")
112
//\bug [strings] crod -> crop (twice)
113
#define CANVAS_WIDTH_LONGTEXT N_( \
114
    "This will automatically crod and pad the video to a specified width." )
115 116
#define CANVAS_HEIGHT_TEXT N_("Video canvas height")
#define CANVAS_HEIGHT_LONGTEXT N_( \
117
    "This will automatically crod and pad the video to a specified height." )
118 119
#define CANVAS_ASPECT_TEXT N_("Video canvas aspect ratio")
#define CANVAS_ASPECT_LONGTEXT N_( \
120 121
    "This sets aspect (like 4:3) of the video canvas and letterbox the video "\
    "accordingly." )
122

123 124
#define AENC_TEXT N_("Audio encoder")
#define AENC_LONGTEXT N_( \
125 126
    "This is the audio encoder module that will be used (and its associated "\
    "options).")
127 128
#define ACODEC_TEXT N_("Destination audio codec")
#define ACODEC_LONGTEXT N_( \
129
    "This is the audio codec that will be used.")
130 131
#define AB_TEXT N_("Audio bitrate")
#define AB_LONGTEXT N_( \
132
    "Target bitrate of the transcoded audio stream." )
133 134
#define ARATE_TEXT N_("Audio sample rate")
#define ARATE_LONGTEXT N_( \
135
 "Sample rate of the transcoded audio stream (11250, 22500, 44100 or 48000).")
136 137
#define ACHANS_TEXT N_("Audio channels")
#define ACHANS_LONGTEXT N_( \
138
    "Number of audio channels in the transcoded streams." )
139

140 141
#define SENC_TEXT N_("Subtitles encoder")
#define SENC_LONGTEXT N_( \
142 143
    "This is the subtitles encoder module that will be used (and its " \
    "associated options)." )
144 145
#define SCODEC_TEXT N_("Destination subtitles codec")
#define SCODEC_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
146
    "This is the subtitles codec that will be used." )
147 148

#define SFILTER_TEXT N_("Overlays")
149
#define SFILTER_LONGTEXT N_( \
150 151 152 153
    "This allows you to add overlays (also known as \"subpictures\" on the "\
    "transcoded video stream. The subpictures produced by the filters will "\
    "be overlayed directly onto the video. You must specify a comma-separated "\
    "list of subpicture modules" )
154

155 156
#define OSD_TEXT N_("OSD menu")
#define OSD_LONGTEXT N_(\
157
    "Stream the On Screen Display menu (using the osdmenu subpicture module)." )
158

159 160
#define THREADS_TEXT N_("Number of threads")
#define THREADS_LONGTEXT N_( \
161
    "Number of threads used for the transcoding." )
162 163 164 165
#define HP_TEXT N_("High priority")
#define HP_LONGTEXT N_( \
    "Runs the optional encoder thread at the OUTPUT priority instead of " \
    "VIDEO." )
166

167 168 169 170 171
#define ASYNC_TEXT N_("Synchronise on audio track")
#define ASYNC_LONGTEXT N_( \
    "This option will drop/duplicate video frames to synchronise the video " \
    "track on the audio track." )

172
#define HURRYUP_TEXT N_( "Hurry up" )
173 174
#define HURRYUP_LONGTEXT N_( "The transcoder will drop frames if your CPU " \
                "can't keep up with the encoding rate." )
175

176 177 178 179 180
static char *ppsz_deinterlace_type[] =
{
    "deinterlace", "ffmpeg-deinterlace"
};

181 182 183
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

184 185
#define SOUT_CFG_PREFIX "sout-transcode-"

186
vlc_module_begin();
187
    set_shortname( _("Transcode"));
188 189 190 191
    set_description( _("Transcode stream output") );
    set_capability( "sout stream", 50 );
    add_shortcut( "transcode" );
    set_callbacks( Open, Close );
192 193
    set_category( CAT_SOUT );
    set_subcategory( SUBCAT_SOUT_STREAM );
194
    set_section( N_("Video"), NULL );
195 196
    add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
                VENC_LONGTEXT, VLC_FALSE );
197 198 199 200 201 202
    add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
                VCODEC_LONGTEXT, VLC_FALSE );
    add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
                 VB_LONGTEXT, VLC_FALSE );
    add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
               SCALE_LONGTEXT, VLC_FALSE );
203 204
    add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
               FPS_LONGTEXT, VLC_FALSE );
205 206
    add_bool( SOUT_CFG_PREFIX "hurry-up", VLC_TRUE, NULL, HURRYUP_TEXT,
               HURRYUP_LONGTEXT, VLC_FALSE );
207 208
    add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
              DEINTERLACE_LONGTEXT, VLC_FALSE );
209 210 211
    add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
                DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
                VLC_FALSE );
212
        change_string_list( ppsz_deinterlace_type, 0, 0 );
213 214 215 216
    add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
                 WIDTH_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
                 HEIGHT_LONGTEXT, VLC_TRUE );
217 218 219 220
    add_integer( SOUT_CFG_PREFIX "maxwidth", 0, NULL, MAXWIDTH_TEXT,
                 MAXWIDTH_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "maxheight", 0, NULL, MAXHEIGHT_TEXT,
                 MAXHEIGHT_LONGTEXT, VLC_TRUE );
221 222 223
    add_module_list_cat( SOUT_CFG_PREFIX "vfilter", SUBCAT_VIDEO_VFILTER,
                     NULL, NULL,
                     VFILTER_TEXT, VFILTER_LONGTEXT, VLC_FALSE );
224 225 226 227 228 229 230 231 232 233

    add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
                 CROPTOP_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
                 CROPLEFT_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
                 CROPBOTTOM_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
                 CROPRIGHT_LONGTEXT, VLC_TRUE );

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    add_integer( SOUT_CFG_PREFIX "paddtop", 0, NULL, PADDTOP_TEXT,
                 PADDTOP_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "paddleft", 0, NULL, PADDLEFT_TEXT,
                 PADDLEFT_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "paddbottom", 0, NULL, PADDBOTTOM_TEXT,
                 PADDBOTTOM_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "paddright", 0, NULL, PADDRIGHT_TEXT,
                 PADDRIGHT_LONGTEXT, VLC_TRUE );

    add_integer( SOUT_CFG_PREFIX "canvas-width", 0, NULL, CANVAS_WIDTH_TEXT,
                 CANVAS_WIDTH_LONGTEXT, VLC_TRUE );
    add_integer( SOUT_CFG_PREFIX "canvas-height", 0, NULL, CANVAS_HEIGHT_TEXT,
                 CANVAS_HEIGHT_LONGTEXT, VLC_TRUE );
    add_string( SOUT_CFG_PREFIX "canvas-aspect", NULL, NULL, CANVAS_ASPECT_TEXT,
                CANVAS_ASPECT_LONGTEXT, VLC_FALSE );

250
    set_section( N_("Audio"), NULL );
251 252
    add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
                AENC_LONGTEXT, VLC_FALSE );
253 254 255 256 257 258 259 260
    add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
                ACODEC_LONGTEXT, VLC_FALSE );
    add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
                 AB_LONGTEXT, VLC_FALSE );
    add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
                 ACHANS_LONGTEXT, VLC_FALSE );
    add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
                 ARATE_LONGTEXT, VLC_TRUE );
261 262
    add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
              ASYNC_LONGTEXT, VLC_FALSE );
263

264
    set_section( N_("Overlays/Subtitles"), NULL );
265 266 267 268 269 270
    add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
                SENC_LONGTEXT, VLC_FALSE );
    add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
                SCODEC_LONGTEXT, VLC_FALSE );
    add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
               SCODEC_LONGTEXT, VLC_FALSE );
271 272 273
    add_module_list_cat( SOUT_CFG_PREFIX "sfilter", SUBCAT_VIDEO_SUBPIC,
                     NULL, NULL,
                     SFILTER_TEXT, SFILTER_LONGTEXT, VLC_FALSE );
274

275 276 277 278
    set_section( N_("On Screen Display"), NULL );
    add_bool( SOUT_CFG_PREFIX "osd", 0, NULL, OSD_TEXT,
              OSD_LONGTEXT, VLC_FALSE );
    
279
    set_section( N_("Miscellaneous"), NULL );
280 281
    add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
                 THREADS_LONGTEXT, VLC_TRUE );
282 283
    add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
              VLC_TRUE );
284

285
vlc_module_end();
286

287 288
static const char *ppsz_sout_options[] = {
    "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
289 290
    "paddtop", "paddbottom", "paddleft", "paddright", 
    "canvas-width", "canvas-height", "canvas-aspect", 
291 292 293
    "scale", "fps", "width", "height", "vfilter", "deinterlace",
    "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
    "samplerate", "channels", "senc", "scodec", "soverlay", "sfilter",
294
    "osd", "audio-sync", "high-priority", "maxwidth", "maxheight", NULL
295 296
};

297 298 299
/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/
300
static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
301
static int               Del ( sout_stream_t *, sout_stream_id_t * );
302
static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
303

304 305 306 307 308
static int  transcode_audio_new    ( sout_stream_t *, sout_stream_id_t * );
static void transcode_audio_close  ( sout_stream_t *, sout_stream_id_t * );
static int  transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
                                     block_t *, block_t ** );

309 310 311
static aout_buffer_t *audio_new_buffer( decoder_t *, int );
static void audio_del_buffer( decoder_t *, aout_buffer_t * );

312 313 314 315 316 317
static int  transcode_video_new    ( sout_stream_t *, sout_stream_id_t * );
static void transcode_video_close  ( sout_stream_t *, sout_stream_id_t * );
static int  transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
static int  transcode_video_process( sout_stream_t *, sout_stream_id_t *,
                                     block_t *, block_t ** );

318 319 320 321 322 323 324
static void video_del_buffer( vlc_object_t *, picture_t * );
static picture_t *video_new_buffer_decoder( decoder_t * );
static void video_del_buffer_decoder( decoder_t *, picture_t * );
static void video_link_picture_decoder( decoder_t *, picture_t * );
static void video_unlink_picture_decoder( decoder_t *, picture_t * );
static picture_t *video_new_buffer_filter( filter_t * );
static void video_del_buffer_filter( filter_t *, picture_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
325

326 327 328 329 330
static int  transcode_spu_new    ( sout_stream_t *, sout_stream_id_t * );
static void transcode_spu_close  ( sout_stream_t *, sout_stream_id_t * );
static int  transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
                                   block_t *, block_t ** );

331 332 333 334
static int  transcode_osd_new    ( sout_stream_t *, sout_stream_id_t * );
static void transcode_osd_close  ( sout_stream_t *, sout_stream_id_t * );
static int  transcode_osd_process( sout_stream_t *, sout_stream_id_t *,
                                   block_t *, block_t ** );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
335

336 337
static int  EncoderThread( struct sout_stream_sys_t * p_sys );

Gildas Bazin's avatar
 
Gildas Bazin committed
338 339 340 341 342 343 344 345 346 347 348
static int pi_channels_maps[6] =
{
    0,
    AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
     | AOUT_CHAN_REARRIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
};

349
#define PICTURE_RING_SIZE 64
350
#define SUBPICTURE_RING_SIZE 20
351

352 353
struct sout_stream_sys_t
{
354 355
    VLC_COMMON_MEMBERS

356 357 358
    sout_stream_t   *p_out;
    sout_stream_id_t *id_video;
    block_t         *p_buffers;
359 360 361 362
    vlc_mutex_t     lock_out;
    vlc_cond_t      cond;
    picture_t *     pp_pics[PICTURE_RING_SIZE];
    int             i_first_pic, i_last_pic;
363

364
    /* Audio */
365
    vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
366
    char            *psz_aenc;
367
    sout_cfg_t      *p_audio_cfg;
368 369 370 371
    int             i_sample_rate;
    int             i_channels;
    int             i_abitrate;

372 373
    /* Video */
    vlc_fourcc_t    i_vcodec;   /* codec video (0 if not transcode) */
374
    char            *psz_venc;
375
    sout_cfg_t      *p_video_cfg;
376
    int             i_vbitrate;
377
    double          f_scale;
378
    double          f_fps;
379 380
    unsigned int    i_width, i_maxwidth;
    unsigned int    i_height, i_maxheight;
381
    vlc_bool_t      b_deinterlace;
382 383
    char            *psz_deinterlace;
    sout_cfg_t      *p_deinterlace_cfg;
384
    int             i_threads;
385
    vlc_bool_t      b_high_priority;
386
    vlc_bool_t      b_hurry_up;
387 388 389
    char            *psz_vfilters[10];
    sout_cfg_t      *p_vfilters_cfg[10];
    int             i_vfilters;
390 391 392 393 394

    int             i_crop_top;
    int             i_crop_bottom;
    int             i_crop_right;
    int             i_crop_left;
Gildas Bazin's avatar
 
Gildas Bazin committed
395

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    int             i_padd_top;
    int             i_padd_bottom;
    int             i_padd_right;
    int             i_padd_left;

    int             i_canvas_width;
    int             i_canvas_height;
    int             i_canvas_aspect;
    
    /* Video, calculated */
    int             i_src_x_offset;
    int             i_src_y_offset;
    int             i_crop_width;
    int             i_crop_height;

    int             i_dst_x_offset;
    int             i_dst_y_offset;
    int             i_nopadd_width;
    int             i_nopadd_height;

416 417 418 419 420
    /* SPU */
    vlc_fourcc_t    i_scodec;   /* codec spu (0 if not transcode) */
    char            *psz_senc;
    vlc_bool_t      b_soverlay;
    sout_cfg_t      *p_spu_cfg;
421
    spu_t           *p_spu;
422

423 424 425 426 427
    /* OSD Menu */
    sout_stream_id_t *id_osd;   /* extension for streaming OSD menus */
    vlc_fourcc_t    i_osdcodec; /* codec osd menu (0 if not transcode) */
    char            *psz_osdenc;
    sout_cfg_t      *p_osd_cfg;
428 429
    vlc_bool_t      b_es_osd;      /* VLC_TRUE when osd es is registered */
    vlc_bool_t      b_sout_osd;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
430

431
    /* Sync */
432
    vlc_bool_t      b_master_sync;
433
    mtime_t         i_master_drift;
434 435
};

436 437 438
struct decoder_owner_sys_t
{
    picture_t *pp_pics[PICTURE_RING_SIZE];
439
    sout_stream_sys_t *p_sys;
440 441 442 443
};
struct filter_owner_sys_t
{
    picture_t *pp_pics[PICTURE_RING_SIZE];
444
    sout_stream_sys_t *p_sys;
445 446
};

447 448 449 450 451 452 453
/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    sout_stream_t     *p_stream = (sout_stream_t*)p_this;
    sout_stream_sys_t *p_sys;
454
    vlc_value_t       val;
455

456
    p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
457

Laurent Aimar's avatar
Laurent Aimar committed
458
    p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
459 460 461
    if( !p_sys->p_out )
    {
        msg_Err( p_stream, "cannot create chain" );
462
        vlc_object_destroy( p_sys );
463 464
        return VLC_EGENERIC;
    }
465

466
    p_sys->i_master_drift = 0;
467

Laurent Aimar's avatar
Laurent Aimar committed
468
    sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
469
                   p_stream->p_cfg );
470

471 472 473 474 475
    /* Audio transcoding parameters */
    var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
    p_sys->psz_aenc = NULL;
    p_sys->p_audio_cfg = NULL;
    if( val.psz_string && *val.psz_string )
476
    {
477
        char *psz_next;
Laurent Aimar's avatar
Laurent Aimar committed
478 479
        psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
                                   val.psz_string );
480 481
        if( psz_next ) free( psz_next );
    }
482
    if( val.psz_string ) free( val.psz_string );
483 484 485 486 487 488 489

    var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
    p_sys->i_acodec = 0;
    if( val.psz_string && *val.psz_string )
    {
        char fcc[4] = "    ";
        memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
490
        p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
491 492
    }
    if( val.psz_string ) free( val.psz_string );
493

494 495 496 497 498 499
    var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
    p_sys->i_abitrate = val.i_int;
    if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;

    var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
    p_sys->i_sample_rate = val.i_int;
500

501 502 503 504 505
    var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
    p_sys->i_channels = val.i_int;

    if( p_sys->i_acodec )
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
506 507
        if( p_sys->i_acodec == VLC_FOURCC('m','p','3',0) &&
            p_sys->i_channels > 2 )
508 509 510 511
        {
            msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
                      p_sys->i_channels );
            p_sys->i_channels = 2;
512
        }
513 514 515
        msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
                 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
                 p_sys->i_channels, p_sys->i_abitrate / 1000 );
516 517
    }

518
    /* Video transcoding parameters */
519 520 521 522 523 524
    var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
    p_sys->psz_venc = NULL;
    p_sys->p_video_cfg = NULL;
    if( val.psz_string && *val.psz_string )
    {
        char *psz_next;
Laurent Aimar's avatar
Laurent Aimar committed
525 526
        psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
                                   val.psz_string );
527 528 529 530
        if( psz_next ) free( psz_next );
    }
    if( val.psz_string ) free( val.psz_string );

531 532 533
    var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
    p_sys->i_vcodec = 0;
    if( val.psz_string && *val.psz_string )
534 535
    {
        char fcc[4] = "    ";
536 537 538 539
        memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
        p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
    }
    if( val.psz_string ) free( val.psz_string );
540

541 542 543
    var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
    p_sys->i_vbitrate = val.i_int;
    if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
544

545 546
    var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
    p_sys->f_scale = val.f_float;
547

548 549 550
    var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
    p_sys->f_fps = val.f_float;

551 552 553
    var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
    p_sys->b_hurry_up = val.b_bool;

554 555
    var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
    p_sys->i_width = val.i_int;
556

557 558
    var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
    p_sys->i_height = val.i_int;
559

560 561 562 563 564 565
    var_Get( p_stream, SOUT_CFG_PREFIX "maxwidth", &val );
    p_sys->i_maxwidth = val.i_int;

    var_Get( p_stream, SOUT_CFG_PREFIX "maxheight", &val );
    p_sys->i_maxheight = val.i_int;

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
    var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
    p_sys->i_vfilters = 0;
    if( val.psz_string && *val.psz_string )
    {
        char *psz_parser = val.psz_string;

        while( psz_parser != NULL && *psz_parser != '\0' )
        {
            psz_parser = sout_CfgCreate(
                                   &p_sys->psz_vfilters[p_sys->i_vfilters],
                                   &p_sys->p_vfilters_cfg[p_sys->i_vfilters],
                                   psz_parser );
            p_sys->i_vfilters++;
            if( psz_parser != NULL && *psz_parser != '\0' ) psz_parser++;
        }
    }
    if( val.psz_string ) free( val.psz_string );
    p_sys->psz_vfilters[p_sys->i_vfilters] = NULL;
    p_sys->p_vfilters_cfg[p_sys->i_vfilters] = NULL;

586 587 588
    var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
    p_sys->b_deinterlace = val.b_bool;

589 590 591 592 593 594 595 596 597 598 599 600 601
    var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
    p_sys->psz_deinterlace = NULL;
    p_sys->p_deinterlace_cfg = NULL;
    if( val.psz_string && *val.psz_string )
    {
        char *psz_next;
        psz_next = sout_CfgCreate( &p_sys->psz_deinterlace,
                                   &p_sys->p_deinterlace_cfg,
                                   val.psz_string );
        if( psz_next ) free( psz_next );
    }
    if( val.psz_string ) free( val.psz_string );

602 603 604 605 606 607 608 609 610
    var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
    p_sys->i_crop_top = val.i_int;
    var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
    p_sys->i_crop_bottom = val.i_int;
    var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
    p_sys->i_crop_left = val.i_int;
    var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
    p_sys->i_crop_right = val.i_int;

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    var_Get( p_stream, SOUT_CFG_PREFIX "paddtop", &val );
    p_sys->i_padd_top = val.i_int;
    var_Get( p_stream, SOUT_CFG_PREFIX "paddbottom", &val );
    p_sys->i_padd_bottom = val.i_int;
    var_Get( p_stream, SOUT_CFG_PREFIX "paddleft", &val );
    p_sys->i_padd_left = val.i_int;
    var_Get( p_stream, SOUT_CFG_PREFIX "paddright", &val );
    p_sys->i_padd_right = val.i_int;
    
    var_Get( p_stream, SOUT_CFG_PREFIX "canvas-width", &val );
    p_sys->i_canvas_width = val.i_int;
    var_Get( p_stream, SOUT_CFG_PREFIX "canvas-height", &val );
    p_sys->i_canvas_height = val.i_int;
    
    var_Get( p_stream, SOUT_CFG_PREFIX "canvas-aspect", &val );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
626 627
    p_sys->i_canvas_aspect = 0;
    if( val.psz_string && *val.psz_string )
628 629 630 631 632
    {
        char *psz_parser = strchr( val.psz_string, ':' );
        if( psz_parser )
        {
            *psz_parser++ = '\0';
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
633 634
            p_sys->i_canvas_aspect = atoi( val.psz_string ) *
                VOUT_ASPECT_FACTOR / atoi( psz_parser );
635
        }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
636
        else msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
637 638

    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
639
    if( val.psz_string ) free( val.psz_string );
640

641 642
    var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
    p_sys->i_threads = val.i_int;
643 644
    var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
    p_sys->b_high_priority = val.b_bool;
645 646

    if( p_sys->i_vcodec )
647
    {
648 649 650
        msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
                 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
                 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
651
    }
652

653
    /* Subpictures transcoding parameters */
654
    p_sys->p_spu = NULL;
655 656
    p_sys->psz_senc = NULL;
    p_sys->p_spu_cfg = NULL;
657 658 659
    p_sys->i_scodec = 0;

    var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    if( val.psz_string && *val.psz_string )
    {
        char *psz_next;
        psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
                                   val.psz_string );
        if( psz_next ) free( psz_next );
    }
    if( val.psz_string ) free( val.psz_string );

    var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
    if( val.psz_string && *val.psz_string )
    {
        char fcc[4] = "    ";
        memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
        p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
    }
    if( val.psz_string ) free( val.psz_string );

    if( p_sys->i_scodec )
    {
680
        msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
681 682 683 684
    }

    var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
    p_sys->b_soverlay = val.b_bool;
685 686 687 688 689 690 691 692 693 694

    var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
    if( val.psz_string && *val.psz_string )
    {
        p_sys->p_spu = spu_Create( p_stream );
        var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
        var_Set( p_sys->p_spu, "sub-filter", val );
        spu_Init( p_sys->p_spu );
    }
    if( val.psz_string ) free( val.psz_string );
695

696 697 698 699
    /* OSD menu transcoding parameters */
    p_sys->psz_osdenc = NULL;
    p_sys->p_osd_cfg  = NULL;
    p_sys->i_osdcodec = 0;
700 701 702 703 704
    p_sys->b_es_osd   = VLC_FALSE;

    var_Get( p_stream, SOUT_CFG_PREFIX "osd", &val );
    p_sys->b_sout_osd = val.b_bool;
    if( p_sys->b_sout_osd )
705
    {
706
        vlc_value_t osd_val;
707
        char *psz_next;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
708

709 710 711
        psz_next = sout_CfgCreate( &p_sys->psz_osdenc,
                                   &p_sys->p_osd_cfg, strdup( "dvbsub") );
        if( psz_next ) free( psz_next );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
712 713

        p_sys->i_osdcodec = VLC_FOURCC('Y','U','V','P' );
714 715 716 717 718

        msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );

        if( !p_sys->p_spu )
        {
719
            osd_val.psz_string = strdup("osdmenu");
720 721
            p_sys->p_spu = spu_Create( p_stream );
            var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
722
            var_Set( p_sys->p_spu, "sub-filter", osd_val );
723
            spu_Init( p_sys->p_spu );
724 725 726 727 728 729 730
            if( osd_val.psz_string ) free( osd_val.psz_string );
        }
        else
        {
            osd_val.psz_string = strdup("osdmenu");
            var_Set( p_sys->p_spu, "sub-filter", osd_val );
            if( osd_val.psz_string ) free( osd_val.psz_string );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
731
        }
732 733 734
    }

    /* Audio settings */
735
    var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
736 737
    p_sys->b_master_sync = val.b_bool;
    if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
738

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    p_stream->pf_add    = Add;
    p_stream->pf_del    = Del;
    p_stream->pf_send   = Send;
    p_stream->p_sys     = p_sys;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    sout_stream_t       *p_stream = (sout_stream_t*)p_this;
    sout_stream_sys_t   *p_sys = p_stream->p_sys;

Laurent Aimar's avatar
Laurent Aimar committed
755
    sout_StreamDelete( p_sys->p_out );
756 757 758 759 760 761 762 763 764 765 766 767 768

    while( p_sys->p_audio_cfg != NULL )
    {
        sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;

        if( p_sys->p_audio_cfg->psz_name )
            free( p_sys->p_audio_cfg->psz_name );
        if( p_sys->p_audio_cfg->psz_value )
            free( p_sys->p_audio_cfg->psz_value );
        free( p_sys->p_audio_cfg );

        p_sys->p_audio_cfg = p_next;
    }
769
    if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
770 771 772 773 774 775 776 777 778 779 780 781 782

    while( p_sys->p_video_cfg != NULL )
    {
        sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;

        if( p_sys->p_video_cfg->psz_name )
            free( p_sys->p_video_cfg->psz_name );
        if( p_sys->p_video_cfg->psz_value )
            free( p_sys->p_video_cfg->psz_value );
        free( p_sys->p_video_cfg );

        p_sys->p_video_cfg = p_next;
    }
783
    if( p_sys->psz_venc ) free( p_sys->psz_venc );
784

785 786 787 788 789 790 791 792 793 794 795 796 797 798
    while( p_sys->p_deinterlace_cfg != NULL )
    {
        sout_cfg_t *p_next = p_sys->p_deinterlace_cfg->p_next;

        if( p_sys->p_deinterlace_cfg->psz_name )
            free( p_sys->p_deinterlace_cfg->psz_name );
        if( p_sys->p_deinterlace_cfg->psz_value )
            free( p_sys->p_deinterlace_cfg->psz_value );
        free( p_sys->p_deinterlace_cfg );

        p_sys->p_deinterlace_cfg = p_next;
    }
    if( p_sys->psz_deinterlace ) free( p_sys->psz_deinterlace );

799 800 801 802 803 804 805 806 807 808 809 810 811 812
    while( p_sys->p_spu_cfg != NULL )
    {
        sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;

        if( p_sys->p_spu_cfg->psz_name )
            free( p_sys->p_spu_cfg->psz_name );
        if( p_sys->p_spu_cfg->psz_value )
            free( p_sys->p_spu_cfg->psz_value );
        free( p_sys->p_spu_cfg );

        p_sys->p_spu_cfg = p_next;
    }
    if( p_sys->psz_senc ) free( p_sys->psz_senc );

813
    if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
814

815 816 817 818 819 820 821 822 823 824 825 826
    while( p_sys->p_osd_cfg != NULL )
    {
        sout_cfg_t *p_next = p_sys->p_osd_cfg->p_next;

        if( p_sys->p_osd_cfg->psz_name )
            free( p_sys->p_osd_cfg->psz_name );
        if( p_sys->p_osd_cfg->psz_value )
            free( p_sys->p_osd_cfg->psz_value );
        free( p_sys->p_osd_cfg );

        p_sys->p_osd_cfg = p_next;
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
827
    if( p_sys->psz_osdenc ) free( p_sys->psz_osdenc );
828

829
    vlc_object_destroy( p_sys );
830 831 832 833 834
}

struct sout_stream_id_t
{
    vlc_fourcc_t  b_transcode;
835

836 837 838
    /* id of the out stream */
    void *id;

839 840 841
    /* Decoder */
    decoder_t       *p_decoder;

842 843 844
    /* Filters */
    filter_t        *pp_filter[10];
    int             i_filter;
845 846
    filter_t        *pp_vfilter[10];
    int             i_vfilter;
847

Gildas Bazin's avatar
 
Gildas Bazin committed
848
    /* Encoder */
Gildas Bazin's avatar
 
Gildas Bazin committed
849
    encoder_t       *p_encoder;
Gildas Bazin's avatar
 
Gildas Bazin committed
850

851 852
    /* Sync */
    date_t          interpolated_pts;
853 854
};

855
static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
856
{
857 858
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    sout_stream_id_t *id;
859 860

    id = malloc( sizeof( sout_stream_id_t ) );
861 862
    memset( id, 0, sizeof(sout_stream_id_t) );

Gildas Bazin's avatar
 
Gildas Bazin committed
863
    id->id = NULL;
864
    id->p_decoder = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
865 866
    id->p_encoder = NULL;

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    /* Create decoder object */
    id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
    if( !id->p_decoder )
    {
        msg_Err( p_stream, "out of memory" );
        goto error;
    }
    vlc_object_attach( id->p_decoder, p_stream );
    id->p_decoder->p_module = NULL;
    id->p_decoder->fmt_in = *p_fmt;
    id->p_decoder->b_pace_control = VLC_TRUE;

    /* Create encoder object */
    id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
    if( !id->p_encoder )
    {
        msg_Err( p_stream, "out of memory" );
        goto error;
    }
    vlc_object_attach( id->p_encoder, p_stream );
    id->p_encoder->p_module = NULL;
888 889

    /* Create destination format */
890 891 892 893 894
    es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
    id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
    id->p_encoder->fmt_out.i_group = p_fmt->i_group;
    if( p_fmt->psz_language )
        id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
895

896
    if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
897 898 899
    {
        msg_Dbg( p_stream,
                 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
900 901 902
                 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );

        /* Complete destination format */
903 904 905 906 907 908
        id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
        id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
            p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
        id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
        id->p_encoder->fmt_out.audio.i_bitspersample =
            p_fmt->audio.i_bitspersample;
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
        id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
            p_sys->i_channels : p_fmt->audio.i_channels;
        /* Sanity check for audio channels */
        id->p_encoder->fmt_out.audio.i_channels =
            __MIN( id->p_encoder->fmt_out.audio.i_channels,
                   id->p_decoder->fmt_in.audio.i_channels );
        id->p_encoder->fmt_out.audio.i_original_channels =
            id->p_decoder->fmt_in.audio.i_physical_channels;
        if( id->p_decoder->fmt_in.audio.i_channels ==
            id->p_encoder->fmt_out.audio.i_channels )
        {
            id->p_encoder->fmt_out.audio.i_physical_channels =
                id->p_decoder->fmt_in.audio.i_physical_channels;
        }
        else
        {
            id->p_encoder->fmt_out.audio.i_physical_channels =
                pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
        }
928 929 930

        /* Build decoder -> filter -> encoder chain */
        if( transcode_audio_new( p_stream, id ) )
931 932
        {
            msg_Err( p_stream, "cannot create audio chain" );
933
            goto error;
934 935
        }

936 937
        /* Open output stream */
        id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
938
        id->b_transcode = VLC_TRUE;
939

940 941 942 943 944
        if( !id->id )
        {
            transcode_audio_close( p_stream, id );
            goto error;
        }
945 946

        date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
947
    }
948 949
    else if( p_fmt->i_cat == VIDEO_ES &&
             (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
950 951 952
    {
        msg_Dbg( p_stream,
                 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
953
                 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
954

955
        /* Complete destination format */
956
        id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
957 958
        id->p_encoder->fmt_out.video.i_width  = p_sys->i_width & ~1;
        id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
959
        id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
960

961 962
        /* Build decoder -> filter -> encoder chain */
        if( transcode_video_new( p_stream, id ) )
963 964
        {
            msg_Err( p_stream, "cannot create video chain" );
965
            goto error;
966
        }
967 968 969

        /* Stream will be added later on because we don't know
         * all the characteristics of the decoded stream yet */
970
        id->b_transcode = VLC_TRUE;
971

972
        if( p_sys->f_fps > 0 )
973
        {
974
            id->p_encoder->fmt_out.video.i_frame_rate =
975
                (p_sys->f_fps * 1001) + 0.5;
976
            id->p_encoder->fmt_out.video.i_frame_rate_base = 1001;
977
        }
978
    }
979 980 981 982 983 984
    else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
    {
        msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
                 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
                 (char*)&p_sys->i_scodec );

985
        /* Complete destination format */
986
        id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
987 988 989 990 991

        /* build decoder -> filter -> encoder */
        if( transcode_spu_new( p_stream, id ) )
        {
            msg_Err( p_stream, "cannot create subtitles chain" );
992
            goto error;
993 994 995
        }

        /* open output stream */
996
        id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
997 998
        id->b_transcode = VLC_TRUE;

999 1000 1001 1002 1003
        if( !id->id )
        {
            transcode_spu_close( p_stream, id );
            goto error;
        }
1004 1005 1006 1007 1008 1009 1010 1011
    }
    else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
    {
        msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
                 (char*)&p_fmt->i_codec );

        id->b_transcode = VLC_TRUE;

1012
        /* Build decoder -> filter -> overlaying chain */
1013 1014 1015
        if( transcode_spu_new( p_stream, id ) )
        {
            msg_Err( p_stream, "cannot create subtitles chain" );
1016
            goto error;
1017 1018
        }
    }
1019 1020
    else
    {
1021 1022
        msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
                 (char*)&p_fmt->i_codec );
1023 1024 1025
        id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
        id->b_transcode = VLC_FALSE;

1026
        if( !id->id ) goto error;
1027 1028
    }

1029
    if( p_sys->b_sout_osd )
1030 1031
    {
        /* Create a fake OSD menu elementary stream */
1032
        if( !p_sys->b_es_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
1033 1034 1035 1036 1037 1038
        {
            if( transcode_osd_new( p_stream, p_sys->id_osd ) )
            {
                msg_Err( p_stream, "cannot create osd chain" );
                goto error;
            }
1039
            p_sys->b_es_osd = VLC_TRUE;
1040 1041
        }
    }
1042
    return id;
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

 error:
    if( id->p_decoder )
    {
        vlc_object_detach( id->p_decoder );
        vlc_object_destroy( id->p_decoder );
    }

    if( id->p_encoder )
    {
        vlc_object_detach( id->p_encoder );
1054
        es_format_Clean( &id->p_encoder->fmt_out );
1055 1056 1057 1058 1059
        vlc_object_destroy( id->p_encoder );
    }

    free( id );
    return NULL;
1060 1061
}

1062
static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1063
{
1064
    sout_stream_sys_t *p_sys = p_stream->p_sys;
1065

1066
    if( p_sys->b_es_osd )
1067 1068
        transcode_osd_close( p_stream, p_sys->id_osd );

1069 1070
    if( id->b_transcode )
    {
1071
        switch( id->p_decoder->fmt_in.i_cat )
1072
        {
1073 1074 1075 1076 1077 1078 1079
        case AUDIO_ES:
            transcode_audio_close( p_stream, id );
            break;
        case VIDEO_ES:
            transcode_video_close( p_stream, id );
            break;
        case SPU_ES:
1080
            transcode_spu_close( p_stream, id );
1081
            break;
1082
        }
1083 1084
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1085
    if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

    if( id->p_decoder )
    {
        vlc_object_detach( id->p_decoder );
        vlc_object_destroy( id->p_decoder );
    }

    if( id->p_encoder )
    {
        vlc_object_detach( id->p_encoder );
1096
        es_format_Clean( &id->p_encoder->fmt_out );
1097 1098 1099
        vlc_object_destroy( id->p_encoder );
    }

1100 1101 1102 1103 1104
    free( id );

    return VLC_SUCCESS;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
1105
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1106
                 block_t *p_buffer )
1107
{
1108
    sout_stream_sys_t *p_sys = p_stream->p_sys;
1109
    block_t *p_out = NULL;
1110

1111 1112
    if( !id->b_transcode && id->id )
    {
1113
        /* Transcode OSD menu pictures. */
1114
        if( p_sys->b_es_osd )
1115
        {
1116
            transcode_osd_process( p_stream, id, p_buffer, &p_out );
1117
        }
1118 1119 1120 1121 1122 1123 1124 1125 1126
        return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
    }
    else if( !id->b_transcode )
    {
        block_Release( p_buffer );
        return VLC_EGENERIC;
    }

    switch( id->p_decoder->fmt_in.i_cat )
1127
    {
1128 1129 1130
    case AUDIO_ES:
        transcode_audio_process( p_stream, id, p_buffer, &p_out );
        break;
1131

1132 1133 1134
    case VIDEO_ES:
        if( transcode_video_process( p_stream, id, p_buffer, &p_out )
            != VLC_SUCCESS )
1135 1136 1137
        {
            return VLC_EGENERIC;
        }
1138
        break;
1139

1140 1141 1142
    case SPU_ES:
        if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
            VLC_SUCCESS )
1143
        {
1144
            return VLC_EGENERIC;
1145
        }
1146
        break;
1147

1148
    default:
1149
        p_out = NULL;
1150
        block_Release( p_buffer );
1151
        break;
1152
    }
1153 1154 1155

    if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
    return VLC_SUCCESS;
1156 1157 1158
}

/****************************************************************************
1159
 * decoder reencoder part
1160
 ****************************************************************************/
1161
int audio_BitsPerSample( vlc_fourcc_t i_format )
1162
{
1163
    switch( i_format )
1164
    {
1165 1166
    case VLC_FOURCC('u','8',' ',' '):
    case VLC_FOURCC('s','8',' ',' '):
1167
        return 8;
1168 1169 1170 1171 1172

    case VLC_FOURCC('u','1','6','l'):
    case VLC_FOURCC('s','1','6','l'):
    case VLC_FOURCC('u','1','6','b'):
    case VLC_FOURCC('s','1','6','b'):
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
        return 16;

    case VLC_FOURCC('u','2','4','l'):
    case VLC_FOURCC('s','2','4','l'):
    case VLC_FOURCC('u','2','4','b'):
    case VLC_FOURCC('s','2','4','b'):
        return 24;

    case VLC_FOURCC('u','3','2','l'):
    case VLC_FOURCC('s','3','2','l'):
    case VLC_FOURCC('u','3','2','b'):
    case VLC_FOURCC('s','3','2','b'):
1185 1186
    case VLC_FOURCC('f','l','3','2'):
    case VLC_FOURCC('f','i','3','2'):
1187 1188 1189 1190
        return 32;

    case VLC_FOURCC('f','l','6','4'):
        return 64;
1191 1192 1193 1194 1195
    }

    return 0;
}

1196 1197 1198 1199 1200 1201 1202 1203
static filter_t *transcode_audio_filter_new( sout_stream_t *p_stream,
                                             sout_stream_id_t *id,
                                             es_format_t *p_fmt_in,
                                             es_format_t *p_fmt_out )
{
    filter_t *p_filter = vlc_object_create( p_stream, VLC_OBJECT_FILTER );

    vlc_object_attach( p_filter, p_stream );
1204
    p_filter->pf_audio_buffer_new = (block_t* (*) (filter_t*, int))__block_New;
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

    p_filter->fmt_in = *p_fmt_in;
    p_filter->fmt_out = *p_fmt_out;

    p_filter->p_module = module_Need( p_filter, "audio filter2", 0, 0 );
    if( p_filter->p_module )
    {
        p_filter->fmt_out.audio.i_bitspersample = 
            audio_BitsPerSample( p_filter->fmt_out.i_codec );
        *p_fmt_in = p_filter->fmt_out;
    }
    else
    {
        vlc_object_detach( p_filter );
        vlc_object_destroy( p_filter );
        p_filter = 0;
    }

    return p_filter;
}

1226 1227
static int transcode_audio_new( sout_stream_t *p_stream,
                                sout_stream_id_t *id )
1228
{
1229
    sout_stream_sys_t *p_sys = p_stream->p_sys;
1230
    es_format_t fmt_last;
1231
    int i_pass = 6;
1232

1233 1234 1235
    /*
     * Open decoder
     */
1236

1237
    /* Initialization of decoder structures */
1238 1239 1240
    id->p_decoder->fmt_out = id->p_decoder->fmt_in;
    id->p_decoder->fmt_out.i_extra = 0;
    id->p_decoder->fmt_out.p_extra = 0;
1241 1242 1243
    id->p_decoder->pf_decode_audio = 0;
    id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
    id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
1244
    /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
1245

1246 1247
    id->p_decoder->p_module =
        module_Need( id->p_decoder, "decoder", "$codec", 0 );
1248

1249 1250 1251 1252
    if( !id->p_decoder->p_module )
    {
        msg_Err( p_stream, "cannot find decoder" );
        return VLC_EGENERIC;
1253
    }
1254 1255
    id->p_decoder->fmt_out.audio.i_bitspersample = 
        audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
1256 1257 1258
    fmt_last = id->p_decoder->fmt_out;
    /* FIX decoders so we don't have to do this */
    fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
1259

1260 1261 1262 1263 1264 1265 1266 1267
    /*
     * Open encoder
     */

    /* Initialization of encoder format structures */
    es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
                    id->p_decoder->fmt_out.i_codec );
    id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
Gildas Bazin's avatar
 
Gildas Bazin committed
1268

Gildas Bazin's avatar
 
Gildas Bazin committed
1269 1270 1271
    /* Initialization of encoder format structures */
    es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
    id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1272
    id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
1273
    id->p_encoder->fmt_in.audio.i_physical_channels =
1274 1275 1276
        id->p_encoder->fmt_out.audio.i_physical_channels;
    id->p_encoder->fmt_in.audio.i_original_channels =
        id->p_encoder->fmt_out.audio.i_original_channels;
1277 1278
    id->p_encoder->fmt_in.audio.i_channels =
        id->p_encoder->fmt_out.audio.i_channels;
1279 1280
    id->p_encoder->fmt_in.audio.i_bitspersample =
        audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
1281

1282 1283
    id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;

Gildas Bazin's avatar
 
Gildas Bazin committed
1284
    id->p_encoder->p_module =
1285
        module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
1286
    if( !id->p_encoder->p_module )
Laurent Aimar's avatar
Laurent Aimar committed
1287
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1288
        msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_aenc );
1289
        module_Unneed( id->p_decoder, id->p_decoder->p_module );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1290
        id->p_decoder->p_module = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
1291
        return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
1292
    }
1293
    id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
1294 1295
    id->p_encoder->fmt_in.audio.i_bitspersample =
        audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1296

1297
    /* Load conversion filters */
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels ||
        fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
    {
        /* We'll have to go through fl32 first */
        es_format_t fmt_out = id->p_encoder->fmt_in;
        fmt_out.i_codec = fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');

        id->pp_filter[id->i_filter] =
            transcode_audio_filter_new( p_stream, id, &fmt_last, &fmt_out );

        if( id->pp_filter[id->i_filter] ) id->i_filter++;
    }

1311
    while( i_pass-- )
1312
    {
1313 1314 1315 1316
        if( fmt_last.audio.i_channels !=
            id->p_encoder->fmt_in.audio.i_channels ||
            fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate ||
            fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1317
        {
1318 1319 1320
            id->pp_filter[id->i_filter] =
                transcode_audio_filter_new( p_stream, id, &fmt_last,
                                            &id->p_encoder->fmt_in );
1321

1322 1323
            if( id->pp_filter[id->i_filter] ) id->i_filter++;
            else break;
1324 1325 1326
        }
    }

1327 1328
    /* Final checks to see if conversions were successful */
    if( fmt_last.i_codec != id->p_encoder->fmt_in.i_codec )
1329
    {
1330
        msg_Err( p_stream, "no audio filter found (%4.4s->%4.4s)",
1331 1332 1333 1334 1335
                 (char *)&fmt_last.i_codec,
                 (char *)&id->p_encoder->fmt_in.i_codec );
        transcode_audio_close( p_stream, id );
        return VLC_EGENERIC;
    }
1336

1337
    if( fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels )
1338
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1339 1340 1341 1342 1343
#if 1
        module_Unneed( id->p_encoder, id->p_encoder->p_module );
        id->p_encoder->p_module = NULL;

        /* This might work, but only if the encoder is restarted */
1344 1345
        id->p_encoder->fmt_in.audio.i_channels = fmt_last.audio.i_channels;
        id->p_encoder->fmt_out.audio.i_channels = fmt_last.audio.i_channels;
1346

1347 1348 1349 1350 1351 1352
        id->p_encoder->fmt_in.audio.i_physical_channels =
            id->p_encoder->fmt_in.audio.i_original_channels =
                fmt_last.audio.i_physical_channels;
        id->p_encoder->fmt_out.audio.i_physical_channels =
            id->p_encoder->fmt_out.audio.i_original_channels =
                fmt_last.audio.i_physical_channels;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371

        msg_Dbg( p_stream, "number of audio channels for mixing changed, "
                 "trying to reopen the encoder for mixing %i to %i channels",
                 fmt_last.audio.i_channels,
                 id->p_encoder->fmt_in.audio.i_channels );

        /* reload encoder */
        id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
        id->p_encoder->p_module =
            module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
        if( !id->p_encoder->p_module )
        {
            msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_aenc );
            transcode_audio_close( p_stream, id );
            return VLC_EGENERIC;
        }
        id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
        id->p_encoder->fmt_in.audio.i_bitspersample =
            audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
1372
#else
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1373 1374 1375 1376
        msg_Err( p_stream, "no audio filter found for mixing from"
                 " %i to %i channels", fmt_last.audio.i_channels,
                 id->p_encoder->fmt_in.audio.i_channels );

1377 1378 1379
        transcode_audio_close( p_stream, id );
        return VLC_EGENERIC;
#endif
1380 1381 1382 1383
    }

    if( fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate )
    {
1384
        msg_Err( p_stream, "no audio filter found for resampling from"
1385 1386
                 " %iHz to %iHz", fmt_last.audio.i_rate,
                 id->p_encoder->fmt_in.audio.i_rate );
1387 1388
#if 0
        /* FIXME : this might work, but only if the encoder is restarted */
1389 1390
        id->p_encoder->fmt_in.audio.i_rate = fmt_last.audio.i_rate;
        id->p_encoder->fmt_out.audio.i_rate = fmt_last.audio.i_rate;
1391 1392 1393 1394
#else
        transcode_audio_close( p_stream, id );
        return VLC_EGENERIC;
#endif
1395 1396
    }

1397 1398 1399
    /* FIXME: Hack for mp3 transcoding support */
    if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
        id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
Gildas Bazin's avatar
 
Gildas Bazin committed
1400

1401 1402 1403
    return VLC_SUCCESS;
}

1404 1405
static void transcode_audio_close( sout_stream_t *p_stream,
                                   sout_stream_id_t *id )
1406
{
1407
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
1408

1409 1410 1411
    /* Close decoder */
    if( id->p_decoder->p_module )
        module_Unneed( id->p_decoder, id->p_decoder->p_module );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1412
    id->p_decoder->p_module = NULL;
1413

1414 1415 1416
    /* Close encoder */
    if( id->p_encoder->p_module )
        module_Unneed( id->p_encoder, id->p_encoder->p_module );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1417
    id->p_encoder->p_module = NULL;
1418

1419 1420 1421 1422 1423 1424 1425 1426
    /* Close filters */
    for( i = 0; i < id->i_filter; i++ )
    {
        vlc_object_detach( id->pp_filter[i] );
        if( id->pp_filter[i]->p_module )
            module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
        vlc_object_destroy( id->pp_filter[i] );
    }
1427
    id->i_filter = 0;
1428 1429
}

1430 1431 1432
static int transcode_audio_process( sout_stream_t *p_stream,
                                    sout_stream_id_t *id,
                                    block_t *in, block_t **out )
1433
{
1434
    sout_stream_sys_t *p_sys = p_stream->p_sys;
1435 1436 1437
    aout_buffer_t *p_audio_buf;
    block_t *p_block, *p_audio_block;
    int i;
1438 1439
    *out = NULL;

1440 1441
    while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
                                                          &in )) )
1442
    {
1443 1444
        stats_UpdateInteger( p_stream->p_parent->p_parent, STATS_DECODED_AUDIO,
                             1, NULL );
1445
        if( p_sys->b_master_sync )
1446
        {
1447
            mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
1448 1449
            if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
                  || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
1450
            {
1451
                msg_Dbg( p_stream, "drift is too high, resetting master sync" );
1452 1453 1454
                date_Set( &id->interpolated_pts, p_audio_buf->start_date );
                i_dts = p_audio_buf->start_date + 1;
            }
1455 1456 1457 1458
            p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
            date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
            p_audio_buf->start_date -= p_sys->i_master_drift;
            p_audio_buf->end_date -= p_sys->i_master_drift;
1459
        }
1460

1461 1462 1463 1464 1465 1466 1467
        p_audio_block = p_audio_buf->p_sys;
        p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
        p_audio_block->i_dts = p_audio_block->i_pts =
            p_audio_buf->start_date;
        p_audio_block->i_length = p_audio_buf->end_date -
            p_audio_buf->start_date;
        p_audio_block->i_samples = p_audio_buf->i_nb_samples;
1468

1469 1470
        /* Run filter chain */
        for( i = 0; i < id->i_filter; i++ )
1471
        {
1472 1473 1474
            p_audio_block =
                id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
                                                   p_audio_block );
1475 1476
        }

1477 1478 1479 1480 1481
        p_audio_buf->p_buffer = p_audio_block->p_buffer;
        p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
        p_audio_buf->i_nb_samples = p_audio_block->i_samples;
        p_audio_buf->start_date = p_audio_block->i_dts;
        p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
1482

1483 1484 1485 1486 1487
        p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
        block_ChainAppend( out, p_block );
        block_Release( p_audio_block );
        free( p_audio_buf );
    }
1488

1489 1490
    return VLC_SUCCESS;
}
1491

1492 1493 1494 1495 1496
static void audio_release_buffer( aout_buffer_t *p_buffer )
{
    if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
    if( p_buffer ) free( p_buffer );
}
1497

1498 1499 1500 1501 1502
static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
{
    aout_buffer_t *p_buffer;
    block_t *p_block;
    int i_size;
1503

1504 1505
    if( p_dec->fmt_out.audio.i_bitspersample )
    {
1506
        i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
            p_dec->fmt_out.audio.i_channels;
    }
    else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
             p_dec->fmt_out.audio.i_frame_length )
    {
        i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
            p_dec->fmt_out.audio.i_frame_length;
    }
    else
    {
        i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
    }
1519

1520 1521 1522
    p_buffer = malloc( sizeof(aout_buffer_t) );
    p_buffer->pf_release = audio_release_buffer;
    p_buffer->p_sys = p_block = block_New( p_dec, i_size );
Gildas Bazin's avatar
 
Gildas Bazin committed
1523

1524 1525 1526 1527
    p_buffer->p_buffer = p_block->p_buffer;
    p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
    p_buffer->i_nb_samples = i_samples;
    p_block->i_samples = i_samples;
1528

1529
    return p_buffer;
1530 1531
}

1532 1533 1534 1535 1536
static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
{
    if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
    if( p_buffer ) free( p_buffer );
}
1537 1538 1539 1540

/*
 * video
 */
1541
static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1542
{
1543
    sout_stream_sys_t *p_sys = p_stream->p_sys;
1544
    int i;
1545

1546 1547 1548
    /*
     * Open decoder
     */
1549

1550
    /* Initialization of decoder structures */
1551 1552 1553
    id->p_decoder->fmt_out = id->p_decoder->fmt_in;
    id->p_decoder->fmt_out.i_extra = 0;
    id->p_decoder->fmt_out.p_extra = 0;
1554
    id->p_decoder->pf_decode_video = 0;
1555 1556 1557 1558 1559 1560 1561
    id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
    id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
    id->p_decoder->pf_picture_link    = video_link_picture_decoder;
    id->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
    id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
    for( i = 0; i < PICTURE_RING_SIZE; i++ )
        id->p_decoder->p_owner->pp_pics[i] = 0;
1562
    id->p_decoder->p_owner->p_sys = p_sys;
1563
    /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
1564

1565 1566
    id->p_decoder->p_module =
        module_Need( id->p_decoder, "decoder", "$codec", 0 );
1567

1568 1569 1570 1571
    if( !id->p_decoder->p_module )
    {
        msg_Err( p_stream, "cannot find decoder" );
        return VLC_EGENERIC;
1572
    }
1573

1574 1575 1576 1577 1578 1579
    /*
     * Open encoder.
     * Because some info about the decoded input will only be available
     * once the first frame is decoded, we actually only test the availability
     * of the encoder here.
     */
Gildas Bazin's avatar
 
Gildas Bazin committed
1580

Gildas Bazin's avatar
 
Gildas Bazin committed
1581
    /* Initialization of encoder format structures */
1582 1583 1584
    es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
                    id->p_decoder->fmt_out.i_codec );
    id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
Gildas Bazin's avatar
 
Gildas Bazin committed
1585

1586
    /* The dimensions will be set properly later on.
1587
     * Just put sensible values so we can test an encoder is available. */
1588
    id->p_encoder->fmt_in.video.i_width =
1589 1590 1591 1592
        id->p_encoder->fmt_out.video.i_width ?
        id->p_encoder->fmt_out.video.i_width :
        id->p_decoder->fmt_in.video.i_width ?
        id->p_decoder->fmt_in.video.i_width : 16;
1593
    id->p_encoder->fmt_in.video.i_height =
1594 1595 1596 1597 1598 1599
        id->p_encoder->fmt_out.video.i_height ?
        id->p_encoder->fmt_out.video.i_height :
        id->p_decoder->fmt_in.video.i_height ?
        id->p_decoder->fmt_in.video.i_height : 16;
    id->p_encoder->fmt_in.video.i_frame_rate = 25;
    id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1600

1601
    id->p_encoder->i_threads = p_sys->i_threads;
1602 1603
    id->p_encoder->p_cfg = p_sys->p_video_cfg;

Gildas Bazin's avatar
 
Gildas Bazin committed
1604
    id->p_encoder->p_module =
1605
        module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
1606
    if( !id->p_encoder->p_module )
1607
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1608
        msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_venc );
1609 1610
        module_Unneed( id->p_decoder, id->p_decoder->p_module );
        id->p_decoder->p_module = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1611
        return VLC_EGENERIC;
1612
    }
Laurent Aimar's avatar
Laurent Aimar committed
1613

Gildas Bazin's avatar
 
Gildas Bazin committed
1614
    /* Close the encoder.
1615
     * We'll open it only when we have the first frame. */
Gildas Bazin's avatar
 
Gildas Bazin committed
1616
    module_Unneed( id->p_encoder, id->p_encoder->p_module );
1617 1618
    if( id->p_encoder->fmt_out.p_extra )
        free( id->p_encoder->fmt_out.p_extra );
Gildas Bazin's avatar
 
Gildas Bazin committed
1619
    id->p_encoder->p_module = NULL;
1620

1621
    if( p_sys->i_threads >= 1 )
1622
    {
1623 1624
        int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
                           VLC_THREAD_PRIORITY_VIDEO;
1625 1626 1627 1628 1629 1630 1631 1632
        p_sys->id_video = id;
        vlc_mutex_init( p_stream, &p_sys->lock_out );
        vlc_cond_init( p_stream, &p_sys->cond );
        memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
        p_sys->i_first_pic = 0;
        p_sys->i_last_pic = 0;
        p_sys->p_buffers = NULL;
        p_sys->b_die = p_sys->b_error = 0;
1633 1634
        if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority,
                               VLC_FALSE ) )
1635 1636
        {
            msg_Err( p_stream, "cannot spawn encoder thread" );
1637 1638
            module_Unneed( id->p_decoder, id->p_decoder->p_module );
            id->p_decoder->p_module = 0;
1639 1640 1641 1642
            return VLC_EGENERIC;
        }
    }

1643 1644 1645
    return VLC_SUCCESS;
}

1646 1647
static int transcode_video_encoder_open( sout_stream_t *p_stream,
                                         sout_stream_id_t *id )
1648
{
1649 1650
    sout_stream_sys_t *p_sys = p_stream->p_sys;

1651
     /* Calculate scaling, padding, cropping etc. */
1652

1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
     /* width/height of source */
     int i_src_width = id->p_decoder->fmt_out.video.i_width;
     int i_src_height = id->p_decoder->fmt_out.video.i_height;

     /* with/height scaling */
     float f_scale_width = 1;
     float f_scale_height = 1;

     /* width/height of output stream */
     int i_dst_width;
     int i_dst_height;

     /* aspect ratio */
     float f_aspect = (float)id->p_decoder->fmt_out.video.i_aspect /
                             VOUT_ASPECT_FACTOR;

     msg_Dbg( p_stream, "decoder aspect is %i:%i",
                  id->p_decoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );

     /* Change f_aspect from source frame to source pixel */
     f_aspect = f_aspect * i_src_height / i_src_width;
     msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );

     /* width/height after cropping */
     p_sys->i_src_x_offset = p_sys->i_crop_left & ~1;
     p_sys->i_src_y_offset = p_sys->i_crop_top & ~1;
     p_sys->i_crop_width = i_src_width - ( p_sys->i_crop_left & ~1 ) -
                            ( p_sys->i_crop_right & ~1 );
     p_sys->i_crop_height = i_src_height - ( p_sys->i_crop_top & ~1 ) -
                            ( p_sys->i_crop_bottom & ~1 );

    /* Calculate scaling factor for specified parameters */
1685 1686
    if( id->p_encoder->fmt_out.video.i_width <= 0 &&
        id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1687
    {
1688
        /* Global scaling. Make sure width will remain a factor of 16 */
1689
        float f_real_scale;
1690 1691 1692 1693 1694
        int  i_new_height;
        int i_new_width = i_src_width * p_sys->f_scale;

        if( i_new_width % 16 <= 7 && i_new_width >= 16 )
            i_new_width -= i_new_width % 16;
1695
        else
1696 1697 1698
            i_new_width += 16 - i_new_width % 16;

        f_real_scale = (float)( i_new_width ) / (float) i_src_width;
1699

1700
        i_new_height = __MAX( 16, i_src_height * (float)f_real_scale );
1701

1702 1703
        f_scale_width = f_real_scale;
        f_scale_height = (float) i_new_height / (float) i_src_height;
1704 1705 1706 1707
    }
    else if( id->p_encoder->fmt_out.video.i_width > 0 &&
             id->p_encoder->fmt_out.video.i_height <= 0 )
    {
1708 1709 1710 1711
        /* Only width specified */
        f_scale_width = (float)id->p_encoder->fmt_out.video.i_width /
                             p_sys->i_crop_width;
        f_scale_height = f_scale_width;
1712 1713 1714 1715
    }
    else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
             id->p_encoder->fmt_out.video.i_height > 0 )
    {
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
         /* Only height specified */
         f_scale_height = (float)id->p_encoder->fmt_out.video.i_height /
                              p_sys->i_crop_height;
         f_scale_width = f_scale_height;
     }
     else if( id->p_encoder->fmt_out.video.i_width > 0 &&
              id->p_encoder->fmt_out.video.i_height > 0 )
     {
         /* Width and height specified */
         f_scale_width = (float)id->p_encoder->fmt_out.video.i_width
                               / p_sys->i_crop_width;
         f_scale_height = (float)id->p_encoder->fmt_out.video.i_height
                               / p_sys->i_crop_height;
     }

     /* check maxwidth and maxheight */
     /* note: maxwidth and maxheight currently does not handle
      * canvas and padding, just scaling and cropping. */

     if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
                                                     p_sys->i_crop_width )
     {
         f_scale_width = (float)p_sys->i_maxwidth / p_sys->i_crop_width;
     }
     if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
                                                       p_sys->i_crop_height )
     {
         f_scale_height = (float)p_sys->i_maxheight / p_sys->i_crop_height;
     }

     /* Change aspect ratio from source pixel to scaled pixel */
     f_aspect = f_aspect * f_scale_height / f_scale_width;
     msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );

     /* Correct scaling for target aspect ratio */
     /* Shrink video if necessary */
     if ( p_sys->i_canvas_aspect > 0 )
     {
         float f_target_aspect = (float)p_sys->i_canvas_aspect /
                                        VOUT_ASPECT_FACTOR;

         if( p_sys->i_canvas_width > 0 && p_sys->i_canvas_height > 0)
         {
             /* Calculate pixel aspect of canvas */
             f_target_aspect = f_target_aspect / p_sys->i_canvas_width *
                               p_sys->i_canvas_height;
         }
         if( f_target_aspect > f_aspect )
         {
             /* Reduce width scale to increase aspect */
             f_scale_width = f_scale_width * f_aspect / f_target_aspect;
         }
         else
         {
             /* Reduce height scale to decrease aspect */
             f_scale_height = f_scale_height * f_target_aspect / f_aspect;
         }
         f_aspect = f_target_aspect;
1774
         msg_Dbg( p_stream, "canvas scaled pixel aspect is %f:1", f_aspect );
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
     }

     /* f_scale_width and f_scale_height are now final */

     /* Calculate width, height from scaling */
     /* Make sure its multiple of 2 */

     i_dst_width = 2 * (int)( p_sys->i_crop_width * f_scale_width / 2 + 0.5 );
     i_dst_height = 2 *
                    (int)( p_sys->i_crop_height * f_scale_height / 2 + 0.5 );

     p_sys->i_nopadd_width = i_dst_width;
     p_sys->i_nopadd_height = i_dst_height;
     p_sys->i_dst_x_offset = 0;
     p_sys->i_dst_y_offset = 0;

     /* Handle canvas and padding */

     if( p_sys->i_canvas_width <= 0 )
     {
         /* No canvas width set, add explicit padding border */
         i_dst_width = p_sys->i_nopadd_width + ( p_sys->i_padd_left & ~1 ) +
                      ( p_sys->i_padd_right & ~1 );
         p_sys->i_dst_x_offset = ( p_sys->i_padd_left & ~1 );
     }
     else
     {
         /* Canvas set, check if we have to padd or crop */

         if( p_sys->i_canvas_width < p_sys->i_nopadd_width )
         {
             /* need to crop more, but keep same scaling */
             int i_crop = 2 * (int)( ( p_sys->i_canvas_width & ~1 ) /
                                       f_scale_width / 2 + 0.5 );

             p_sys->i_src_x_offset += ( ( p_sys->i_crop_width - i_crop ) / 2 )
                                           & ~1;
             p_sys->i_crop_width = i_crop;
             i_dst_width = p_sys->i_canvas_width & ~1;
             p_sys->i_nopadd_width = i_dst_width;
         }
         else if( p_sys->i_canvas_width > p_sys->i_nopadd_width )
         {
             /* need to padd */
             i_dst_width = p_sys->i_canvas_width & ~1;
             p_sys->i_dst_x_offset = ( i_dst_width - p_sys->i_nopadd_width )/2;
             p_sys->i_dst_x_offset = p_sys->i_dst_x_offset & ~1;
         }
     }

     if( p_sys->i_canvas_height <= 0 )
     {
         /* No canvas set, add padding border */
         i_dst_height = p_sys->i_nopadd_height + ( p_sys->i_padd_top & ~1 ) +
                        ( p_sys->i_padd_bottom & ~1 );
         p_sys->i_dst_y_offset = ( p_sys->i_padd_top & ~1 );
     }
     else
     {
         /* Canvas set, check if we have to padd or crop */

         if( p_sys->i_canvas_height < p_sys->i_nopadd_height )
         {
             /* need to crop more, but keep same scaling */
             int i_crop = 2 * (int)( ( p_sys->i_canvas_height & ~1 ) /
                                        f_scale_height / 2 + 0.5 );

             p_sys->i_src_y_offset += ( ( p_sys->i_crop_height - i_crop ) / 2 )
                                                & ~1;
             p_sys->i_crop_height = i_crop;
             i_dst_height = p_sys->i_canvas_height & ~1;
             p_sys->i_nopadd_height = i_dst_height;
         }
         else if( p_sys->i_canvas_height > p_sys->i_nopadd_height )
         {
             /* need to padd */
             i_dst_height = p_sys->i_canvas_height & ~1;
             p_sys->i_dst_y_offset = ( i_dst_height - p_sys->i_nopadd_height )
                                        /2;
             p_sys->i_dst_y_offset = p_sys->i_dst_y_offset & ~1;
         }
     }


     /* Change aspect ratio from scaled pixel to output frame */

     f_aspect = f_aspect * i_dst_width / i_dst_height;

     /* Store calculated values */
     id->p_encoder->fmt_out.video.i_width = i_dst_width;
     id->p_encoder->fmt_out.video.i_height = i_dst_height;

     id->p_encoder->fmt_in.video.i_width = i_dst_width;
     id->p_encoder->fmt_in.video.i_height = i_dst_height;

1870
     msg_Dbg( p_stream, "source %ix%i, crop %ix%i, "
1871 1872 1873 1874 1875 1876 1877 1878
                        "destination %ix%i, padding %ix%i",
         i_src_width, i_src_height,
         p_sys->i_crop_width, p_sys->i_crop_height,
         p_sys->i_nopadd_width, p_sys->i_nopadd_height,
         i_dst_width, i_dst_height
     );

    /* Handle frame rate conversion */
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
    if( !id->p_encoder->fmt_out.video.i_frame_rate ||
        !id->p_encoder->fmt_out.video.i_frame_rate_base )
    {
        if( id->p_decoder->fmt_out.video.i_frame_rate &&
            id->p_decoder->fmt_out.video.i_frame_rate_base )
        {
            id->p_encoder->fmt_out.video.i_frame_rate =
                id->p_decoder->fmt_out.video.i_frame_rate;
            id->p_encoder->fmt_out.video.i_frame_rate_base =
                id->p_decoder->fmt_out.video.i_frame_rate_base;
        }
        else
        {
            /* Pick a sensible default value */
            id->p_encoder->fmt_out.video.i_frame_rate = 25;
            id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
        }
    }

    id->p_encoder->fmt_in.video.i_frame_rate =
        id->p_encoder->fmt_out.video.i_frame_rate;
    id->p_encoder->fmt_in.video.i_frame_rate_base =
        id->p_encoder->fmt_out.video.i_frame_rate_base;
1902

1903 1904 1905 1906 1907 1908
    date_Init( &id->interpolated_pts,
               id->p_encoder->fmt_out.video.i_frame_rate,
               id->p_encoder->fmt_out.video.i_frame_rate_base );

    /* Check whether a particular aspect ratio was requested */
    if( !id->p_encoder->fmt_out.video.i_aspect )
1909
    {
1910
        id->p_encoder->fmt_out.video.i_aspect = (int)( f_aspect * VOUT_ASPECT_FACTOR + 0.5 );
1911
    }
1912 1913
    id->p_encoder->fmt_in.video.i_aspect =
        id->p_encoder->fmt_out.video.i_aspect;
1914

1915 1916
    msg_Dbg( p_stream, "encoder aspect is %i:%i", id->p_encoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );

1917 1918 1919
    id->p_encoder->p_module =
        module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
    if( !id->p_encoder->p_module )
1920
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1921
        msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_venc );
1922
        return VLC_EGENERIC;
1923
    }
1924 1925 1926 1927 1928 1929

    id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;

    /* Hack for mp2v/mp1v transcoding support */
    if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
        id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1930
    {
1931
        id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1932
    }
1933 1934 1935 1936

    id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
                                             &id->p_encoder->fmt_out );
    if( !id->id )
1937
    {
1938 1939
        msg_Err( p_stream, "cannot add this stream" );
        return VLC_EGENERIC;
1940
    }
1941 1942 1943 1944 1945 1946 1947

    return VLC_SUCCESS;
}

static void transcode_video_close( sout_stream_t *p_stream,
                                   sout_stream_id_t *id )
{
1948
    int i, j;
1949 1950

    if( p_stream->p_sys->i_threads >= 1 )
1951
    {
1952 1953 1954 1955 1956 1957 1958
        vlc_mutex_lock( &p_stream->p_sys->lock_out );
        p_stream->p_sys->b_die = 1;
        vlc_cond_signal( &p_stream->p_sys->cond );
        vlc_mutex_unlock( &p_stream->p_sys->lock_out );
        vlc_thread_join( p_stream->p_sys );
        vlc_mutex_destroy( &p_stream->p_sys->lock_out );
        vlc_cond_destroy( &p_stream->p_sys->cond );
1959
    }
1960 1961 1962 1963 1964

    /* Close decoder */
    if( id->p_decoder->p_module )
        module_Unneed( id->p_decoder, id->p_decoder->p_module );

1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
    if( id->p_decoder->p_owner )
    {
        /* Clean-up pictures ring buffer */
        for( i = 0; i < PICTURE_RING_SIZE; i++ )
        {
            if( id->p_decoder->p_owner->pp_pics[i] )
                video_del_buffer( VLC_OBJECT(id->p_decoder),
                                  id->p_decoder->p_owner->pp_pics[i] );
        }
        free( id->p_decoder->p_owner );
    }

1977 1978 1979 1980 1981 1982
    /* Close encoder */
    if( id->p_encoder->p_module )
        module_Unneed( id->p_encoder, id->p_encoder->p_module );

    /* Close filters */
    for( i = 0; i < id->i_filter; i++ )
1983
    {
1984 1985 1986
        vlc_object_detach( id->pp_filter[i] );
        if( id->pp_filter[i]->p_module )
            module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

        /* Clean-up pictures ring buffer */
        for( j = 0; j < PICTURE_RING_SIZE; j++ )
        {
            if( id->pp_filter[i]->p_owner->pp_pics[j] )
                video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
                                  id->pp_filter[i]->p_owner->pp_pics[j] );
        }
        free( id->pp_filter[i]->p_owner );

1997
        vlc_object_destroy( id->pp_filter[i] );
1998
    }
1999 2000
    id->i_filter = 0;

2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
    for( i = 0; i < id->i_vfilter; i++ )
    {
        vlc_object_detach( id->pp_vfilter[i] );
        if( id->pp_vfilter[i]->p_module )
            module_Unneed( id->pp_vfilter[i], id->pp_vfilter[i]->p_module );

        /* Clean-up pictures ring buffer */
        for( j = 0; j < PICTURE_RING_SIZE; j++ )
        {
            if( id->pp_vfilter[i]->p_owner->pp_pics[j] )
                video_del_buffer( VLC_OBJECT(id->pp_vfilter[i]),
                                  id->pp_vfilter[i]->p_owner->pp_pics[j] );
        }
        free( id->pp_vfilter[i]->p_owner );

        vlc_object_destroy( id->pp_vfilter[i] );
    }
2018
    id->i_vfilter = 0;
2019 2020
}

2021 2022 2023
static int transcode_video_process( sout_stream_t *p_stream,
                                    sout_stream_id_t *id,
                                    block_t *in, block_t **out )
2024
{
2025
    sout_stream_sys_t *p_sys = p_stream->p_sys;
2026
    int i_duplicate = 1, i;
2027
    picture_t *p_pic, *p_pic2 = NULL;
2028 2029
    *out = NULL;

2030
    while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
2031
    {
2032
        subpicture_t *p_subpic = 0;
2033 2034
        stats_UpdateInteger( p_stream->p_parent->p_parent, STATS_DECODED_VIDEO,
                              1, NULL );
2035

2036 2037 2038
        if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
        {
            mtime_t current_date = mdate();
2039
            if( current_date + 50000 > p_pic->date )
2040 2041
            {
                msg_Dbg( p_stream, "late picture skipped ("I64Fd")",
2042
                         current_date + 50000 - p_pic->date );
2043 2044
                if( p_pic->pf_release )
                    p_pic->pf_release( p_pic );
2045 2046 2047 2048
                continue;
            }
        }

2049
        if( p_sys->b_master_sync )
2050 2051
        {
            mtime_t i_video_drift;
2052
            mtime_t i_master_drift = p_sys->i_master_drift;
2053
            mtime_t i_pts;
2054

2055
            i_pts = date_Get( &id->interpolated_pts ) + 1;
2056 2057
            if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
                  || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2058
            {
2059
                msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2060 2061
                date_Set( &id->interpolated_pts, p_pic->date );
                i_pts = p_pic->date + 1;
2062
            }
2063
            i_video_drift = p_pic->date - i_pts;
2064 2065
            i_duplicate = 1;

2066 2067 2068
            /* Set the pts of the frame being encoded */
            p_pic->date = i_pts;

2069
            if( i_video_drift < i_master_drift - 50000 )
2070
            {
2071
#if 0
2072
                msg_Dbg( p_stream, "dropping frame (%i)",
2073
                         (int)(i_video_drift - i_master_drift) );
2074
#endif
2075 2076
                if( p_pic->pf_release )
                    p_pic->pf_release( p_pic );
2077
                continue;
2078
            }
2079
            else if( i_video_drift > i_master_drift + 50000 )
2080
            {
2081
#if 0
2082
                msg_Dbg( p_stream, "adding frame (%i)",
2083
                         (int)(i_video_drift - i_master_drift) );
2084
#endif
2085 2086 2087 2088
                i_duplicate = 2;
            }
        }

2089
        if( !id->p_encoder->p_module )
2090
        {
2091
            if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
2092
            {
2093 2094
                if( p_pic->pf_release )
                    p_pic->pf_release( p_pic );
2095
                transcode_video_close( p_stream, id );
Gildas Bazin's avatar
 
Gildas Bazin committed
2096
                id->b_transcode = VLC_FALSE;
2097
                return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
2098 2099
            }

2100 2101 2102 2103 2104 2105 2106 2107
            /* Deinterlace */
            if( p_stream->p_sys->b_deinterlace )
            {
                id->pp_filter[id->i_filter] =
                    vlc_object_create( p_stream, VLC_OBJECT_FILTER );
                vlc_object_attach( id->pp_filter[id->i_filter], p_stream );

                id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2108
                    video_new_buffer_filter;
2109
                id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2110
                    video_del_buffer_filter;
2111 2112 2113

                id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
                id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
2114
                id->pp_filter[id->i_filter]->p_cfg = p_sys->p_deinterlace_cfg;
2115 2116
                id->pp_filter[id->i_filter]->p_module =
                    module_Need( id->pp_filter[id->i_filter],
2117
                                 "video filter2", p_sys->psz_deinterlace, 0 );
2118 2119 2120 2121 2122 2123
                if( id->pp_filter[id->i_filter]->p_module )
                {
                    id->pp_filter[id->i_filter]->p_owner =
                        malloc( sizeof(filter_owner_sys_t) );
                    for( i = 0; i < PICTURE_RING_SIZE; i++ )
                        id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2124
                    id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2125 2126 2127

                    id->i_filter++;
                }
2128 2129 2130 2131 2132 2133 2134 2135
                else
                {
                    msg_Dbg( p_stream, "no video filter found" );
                    vlc_object_detach( id->pp_filter[id->i_filter] );
                    vlc_object_destroy( id->pp_filter[id->i_filter] );
                }
            }

2136 2137 2138
            /* Check if we need a filter for chroma conversion or resizing */
            if( id->p_decoder->fmt_out.video.i_chroma !=
                id->p_encoder->fmt_in.video.i_chroma ||
2139 2140 2141 2142 2143 2144 2145 2146
                
                (int)id->p_decoder->fmt_out.video.i_width != p_sys->i_crop_width ||
                p_sys->i_crop_width != p_sys->i_nopadd_width ||
                p_sys->i_nopadd_width != (int)id->p_encoder->fmt_out.video.i_width ||
                
                (int)id->p_decoder->fmt_out.video.i_height != p_sys->i_crop_height ||
                p_sys->i_crop_height != p_sys->i_nopadd_height ||
                p_sys->i_nopadd_height != (int)id->p_encoder->fmt_out.video.i_height)
Gildas Bazin's avatar
 
Gildas Bazin committed
2147
            {
2148
                id->pp_filter[id->i_filter] =
2149
                    vlc_object_create( p_stream, VLC_OBJECT_FILTER );
2150 2151 2152
                vlc_object_attach( id->pp_filter[id->i_filter], p_stream );

                id->pp_filter[id->i_filter]->pf_vout_buffer_new =
2153
                    video_new_buffer_filter;
2154
                id->pp_filter[id->i_filter]->pf_vout_buffer_del =
2155
                    video_del_buffer_filter;
2156 2157 2158

                id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
                id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
2159
                id->pp_filter[id->i_filter]->p_cfg = NULL;
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171


                id->pp_filter[id->i_filter]->fmt_in.video.i_x_offset = p_sys->i_src_x_offset;
                id->pp_filter[id->i_filter]->fmt_in.video.i_y_offset = p_sys->i_src_y_offset;
                id->pp_filter[id->i_filter]->fmt_in.video.i_visible_width = p_sys->i_crop_width;
                id->pp_filter[id->i_filter]->fmt_in.video.i_visible_height = p_sys->i_crop_height;

                id->pp_filter[id->i_filter]->fmt_out.video.i_x_offset = p_sys->i_dst_x_offset;
                id->pp_filter[id->i_filter]->fmt_out.video.i_y_offset = p_sys->i_dst_y_offset;
                id->pp_filter[id->i_filter]->fmt_out.video.i_visible_width = p_sys->i_nopadd_width;
                id->pp_filter[id->i_filter]->fmt_out.video.i_visible_height = p_sys->i_nopadd_height;

2172 2173
                id->pp_filter[id->i_filter]->p_module =
                    module_Need( id->pp_filter[id->i_filter],
2174
                                 "crop padd", 0, 0 );
2175 2176 2177 2178 2179 2180
                if( id->pp_filter[id->i_filter]->p_module )
                {
                    id->pp_filter[id->i_filter]->p_owner =
                        malloc( sizeof(filter_owner_sys_t) );
                    for( i = 0; i < PICTURE_RING_SIZE; i++ )
                        id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
2181
                    id->pp_filter[id->i_filter]->p_owner->p_sys = p_sys;
2182 2183 2184

                    id->i_filter++;
                }
2185
                else
Gildas Bazin's avatar
 
Gildas Bazin committed
2186
                {
2187
                    msg_Dbg( p_stream, "no video filter found" );
2188 2189
                    vlc_object_detach( id->pp_filter[id->i_filter] );
                    vlc_object_destroy( id->pp_filter[id->i_filter] );
2190

2191 2192
                    if( p_pic->pf_release )
                        p_pic->pf_release( p_pic );
2193 2194 2195
                    transcode_video_close( p_stream, id );
                    id->b_transcode = VLC_FALSE;
                    return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
2196
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
2197
            }
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221

            for( i = 0; i < p_sys->i_vfilters; i++ )
            {
                id->pp_vfilter[id->i_vfilter] =
                    vlc_object_create( p_stream, VLC_OBJECT_FILTER );
                vlc_object_attach( id->pp_vfilter[id->i_vfilter], p_stream );

                id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_new =
                    video_new_buffer_filter;
                id->pp_vfilter[id->i_vfilter]->pf_vout_buffer_del =
                    video_del_buffer_filter;

                id->pp_vfilter[id->i_vfilter]->fmt_in = id->p_encoder->fmt_in;
                id->pp_vfilter[id->i_vfilter]->fmt_out = id->p_encoder->fmt_in;
                id->pp_vfilter[id->i_vfilter]->p_cfg = p_sys->p_vfilters_cfg[i];
                id->pp_vfilter[id->i_vfilter]->p_module =
                    module_Need( id->pp_vfilter[id->i_vfilter],
                          "video filter2", p_sys->psz_vfilters[i], 0 );
                if( id->pp_vfilter[id->i_vfilter]->p_module )
                {
                    id->pp_vfilter[id->i_vfilter]->p_owner =
                        malloc( sizeof(filter_owner_sys_t) );
                    for( i = 0; i < PICTURE_RING_SIZE; i++ )
                        id->pp_vfilter[id->i_vfilter]->p_owner->pp_pics[i] = 0;
2222
                    id->pp_vfilter[id->i_vfilter]->p_owner->p_sys = p_sys;
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232

                    id->i_vfilter++;
                }
                else
                {
                    msg_Dbg( p_stream, "no video filter found" );
                    vlc_object_detach( id->pp_vfilter[id->i_vfilter] );
                    vlc_object_destroy( id->pp_vfilter[id->i_vfilter] );
                }
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
2233
        }
2234

2235 2236
        /* Run filter chain */
        for( i = 0; i < id->i_filter; i++ )
2237
        {
2238
            p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
2239 2240
        }

2241 2242 2243
        /*
         * Encoding
         */
Gildas Bazin's avatar
 
Gildas Bazin committed
2244

2245
        /* Check if we have a subpicture to overlay */
2246
        if( p_sys->p_spu )
2247
        {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
2248 2249
            p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date,
                       VLC_FALSE /* Fixme: check if stream is paused */ );
2250
            /* TODO: get another pic */
2251
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
2252

2253 2254 2255
        /* Overlay subpicture */
        if( p_subpic )
        {
2256 2257
            int i_scale_width, i_scale_height;
            video_format_t *p_fmt;
2258

2259 2260 2261 2262
            i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
                id->p_decoder->fmt_out.video.i_width;
            i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
                id->p_decoder->fmt_out.video.i_height;
2263

2264
            if( p_pic->i_refcount && !id->i_filter )
2265 2266
            {
                /* We can't modify the picture, we need to duplicate it */
2267
                picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
2268 2269
                if( p_tmp )
                {
2270
                    vout_CopyPicture( p_stream, p_tmp, p_pic );
2271 2272
                    if( p_pic->pf_release )
                        p_pic->pf_release( p_pic );
2273 2274 2275 2276
                    p_pic = p_tmp;
                }
            }

2277 2278 2279 2280
            if( id->i_filter )
                p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
            else
                p_fmt = &id->p_decoder->fmt_out.video;
2281

2282 2283 2284 2285 2286
            /* FIXME (shouldn't have to be done here) */
            p_fmt->i_sar_num = p_fmt->i_aspect *
                p_fmt->i_height / p_fmt->i_width;
            p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;

2287 2288
            spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
                                   i_scale_width, i_scale_height );
2289 2290
        }

2291 2292 2293 2294 2295 2296
        /* Run vfilter chain */
        for( i = 0; i < id->i_vfilter; i++ )
        {
            p_pic = id->pp_vfilter[i]->pf_video_filter(id->pp_vfilter[i], p_pic);
        }

2297
        if( p_sys->i_threads == 0 )
2298 2299 2300
        {
            block_t *p_block;
            p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2301
            block_ChainAppend( out, p_block );
2302
        }
2303

2304 2305 2306
        if( p_sys->b_master_sync )
        {
            mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2307 2308
            if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
                  || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2309
            {
2310
                msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2311 2312 2313 2314 2315
                date_Set( &id->interpolated_pts, p_pic->date );
                i_pts = p_pic->date + 1;
            }
            date_Increment( &id->interpolated_pts, 1 );
        }
2316

2317 2318 2319
        if( p_sys->b_master_sync && i_duplicate > 1 )
        {
            mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
2320 2321
            if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
                  || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
2322
            {
2323
                msg_Dbg( p_stream, "drift is too high, resetting master sync" );
2324 2325 2326 2327 2328 2329 2330 2331
                date_Set( &id->interpolated_pts, p_pic->date );
                i_pts = p_pic->date + 1;
            }
            date_Increment( &id->interpolated_pts, 1 );

            if( p_sys->i_threads >= 1 )
            {
                /* We can't modify the picture, we need to duplicate it */
2332 2333
                p_pic2 = video_new_buffer_decoder( id->p_decoder );
                if( p_pic2 != NULL )
2334
                {
2335 2336
                    vout_CopyPicture( p_stream, p_pic2, p_pic );
                    p_pic2->date = i_pts;
2337 2338 2339
                }
            }
            else
2340
            {
2341
                block_t *p_block;
2342
                p_pic->date = i_pts;
2343
                p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
2344 2345
                block_ChainAppend( out, p_block );
            }
2346
        }
2347

2348
        if( p_sys->i_threads == 0 )
2349
        {
2350 2351
            if( p_pic->pf_release )
                p_pic->pf_release( p_pic );
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
        }
        else
        {
            vlc_mutex_lock( &p_sys->lock_out );
            p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
            p_sys->i_last_pic %= PICTURE_RING_SIZE;
            *out = p_sys->p_buffers;
            p_sys->p_buffers = NULL;
            if( p_pic2 != NULL )
            {
                p_sys->pp_pics[p_sys->i_last_pic++] = p_pic2;
                p_sys->i_last_pic %= PICTURE_RING_SIZE;
            }
            vlc_cond_signal( &p_sys->cond );
            vlc_mutex_unlock( &p_sys->lock_out );
        }
2368 2369 2370 2371 2372
    }

    return VLC_SUCCESS;
}

2373
static int EncoderThread( sout_stream_sys_t *p_sys )
2374
{
2375 2376
    sout_stream_id_t *id = p_sys->id_video;
    picture_t *p_pic;
2377

2378
    while( !p_sys->b_die && !p_sys->b_error )
2379 2380 2381 2382
    {
        block_t *p_block;

        vlc_mutex_lock( &p_sys->lock_out );
2383
        while( p_sys->i_last_pic == p_sys->i_first_pic )
2384 2385
        {
            vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2386
            if( p_sys->b_die || p_sys->b_error ) break;
2387
        }
2388
        if( p_sys->b_die || p_sys->b_error )
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
        {
            vlc_mutex_unlock( &p_sys->lock_out );
            break;
        }

        p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
        p_sys->i_first_pic %= PICTURE_RING_SIZE;
        vlc_mutex_unlock( &p_sys->lock_out );

        p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
        vlc_mutex_lock( &p_sys->lock_out );
2400
        block_ChainAppend( &p_sys->p_buffers, p_block );
2401

2402 2403
        vlc_mutex_unlock( &p_sys->lock_out );

2404 2405
        if( p_pic->pf_release )
            p_pic->pf_release( p_pic );
2406 2407
    }

2408
    while( p_sys->i_last_pic != p_sys->i_first_pic )
2409 2410 2411 2412
    {
        p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
        p_sys->i_first_pic %= PICTURE_RING_SIZE;

2413 2414
        if( p_pic->pf_release )
            p_pic->pf_release( p_pic );
2415 2416
    }

2417
    block_ChainRelease( p_sys->p_buffers );
2418 2419

    return 0;
2420
}
Gildas Bazin's avatar
 
Gildas Bazin committed
2421

2422
struct picture_sys_t
Gildas Bazin's avatar
 
Gildas Bazin committed
2423
{
2424
    vlc_object_t *p_owner;
2425
};
Gildas Bazin's avatar
 
Gildas Bazin committed
2426

2427 2428 2429
static void video_release_buffer( picture_t *p_pic )
{
    if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
2430
    {
2431
        video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
2432
    }
2433 2434
    else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
}
2435

2436 2437
static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
                                    sout_stream_sys_t *p_sys )
2438
{
2439 2440 2441
    decoder_t *p_dec = (decoder_t *)p_this;
    picture_t *p_pic;
    int i;
2442

2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
    /* Find an empty space in the picture ring buffer */
    for( i = 0; i < PICTURE_RING_SIZE; i++ )
    {
        if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
        {
            pp_ring[i]->i_status = RESERVED_PICTURE;
            return pp_ring[i];
        }
    }
    for( i = 0; i < PICTURE_RING_SIZE; i++ )
    {
        if( pp_ring[i] == 0 ) break;
    }

2457
    if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
2458
    {
2459
        int i_first_pic = p_sys->i_first_pic;
2460

2461
        if( p_sys->i_first_pic != p_sys->i_last_pic )
2462 2463
        {
            /* Encoder still has stuff to encode, wait to clear-up the list */
2464
            while( p_sys->i_first_pic == i_first_pic )
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
                msleep( 100000 );
        }

        /* Find an empty space in the picture ring buffer */
        for( i = 0; i < PICTURE_RING_SIZE; i++ )
        {
            if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
            {
                pp_ring[i]->i_status = RESERVED_PICTURE;
                return pp_ring[i];
            }
        }
        for( i = 0; i < PICTURE_RING_SIZE; i++ )
        {
            if( pp_ring[i] == 0 ) break;
        }
    }

2483 2484 2485 2486 2487 2488 2489
    if( i == PICTURE_RING_SIZE )
    {
        msg_Err( p_this, "decoder/filter is leaking pictures, "
                 "resetting its ring buffer" );

        for( i = 0; i < PICTURE_RING_SIZE; i++ )
        {
2490 2491
            if( pp_ring[i]->pf_release )
                pp_ring[i]->pf_release( pp_ring[i] );
2492 2493 2494 2495 2496 2497
        }

        i = 0;
    }

    p_pic = malloc( sizeof(picture_t) );
2498 2499 2500 2501 2502 2503 2504 2505
    p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
    vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
                          p_dec->fmt_out.video.i_chroma,
                          p_dec->fmt_out.video.i_width,
                          p_dec->fmt_out.video.i_height,
                          p_dec->fmt_out.video.i_aspect );

    if( !p_pic->i_planes )
2506
    {
2507 2508
        free( p_pic );
        return 0;
2509
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
2510

2511 2512
    p_pic->pf_release = video_release_buffer;
    p_pic->p_sys = malloc( sizeof(picture_sys_t) );
2513 2514 2515 2516
    p_pic->p_sys->p_owner = p_this;
    p_pic->i_status = RESERVED_PICTURE;

    pp_ring[i] = p_pic;
2517 2518 2519 2520

    return p_pic;
}

2521 2522 2523
static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
{
    return video_new_buffer( VLC_OBJECT(p_dec),
2524
                             p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
2525 2526 2527 2528 2529
}

static picture_t *video_new_buffer_filter( filter_t *p_filter )
{
    return video_new_buffer( VLC_OBJECT(p_filter),
2530 2531
                             p_filter->p_owner->pp_pics,
                             p_filter->p_owner->p_sys );
2532 2533 2534
}

static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
2535
{
2536
    if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
2537 2538 2539 2540
    if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
    if( p_pic ) free( p_pic );
}

2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
{
    p_pic->i_refcount = 0;
    p_pic->i_status = DESTROYED_PICTURE;
}

static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
{
    p_pic->i_refcount = 0;
    p_pic->i_status = DESTROYED_PICTURE;
}

static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2554 2555 2556 2557
{
    p_pic->i_refcount++;
}

2558
static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
2559 2560
{
    video_release_buffer( p_pic );
Gildas Bazin's avatar
 
Gildas Bazin committed
2561
}
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572

/*
 * SPU
 */
static subpicture_t *spu_new_buffer( decoder_t * );
static void spu_del_buffer( decoder_t *, subpicture_t * );

static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;

2573 2574 2575
    /*
     * Open decoder
     */
2576

2577
    /* Initialization of decoder structures */
2578 2579
    id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
    id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2580
    id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
2581
    /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
2582 2583

    id->p_decoder->p_module =
2584
        module_Need( id->p_decoder, "decoder", "$codec", 0 );
2585 2586 2587 2588 2589 2590 2591 2592 2593

    if( !id->p_decoder->p_module )
    {
        msg_Err( p_stream, "cannot find decoder" );
        return VLC_EGENERIC;
    }

    if( !p_sys->b_soverlay )
    {
2594 2595 2596
        /*
         * Open encoder
         */
2597 2598

        /* Initialization of encoder format structures */
2599 2600
        es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
                        id->p_decoder->fmt_in.i_codec );
2601 2602 2603 2604 2605 2606 2607 2608 2609

        id->p_encoder->p_cfg = p_sys->p_spu_cfg;

        id->p_encoder->p_module =
            module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );

        if( !id->p_encoder->p_module )
        {
            module_Unneed( id->p_decoder, id->p_decoder->p_module );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
2610
            msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_senc );
2611 2612 2613
            return VLC_EGENERIC;
        }
    }
2614 2615

    if( !p_sys->p_spu )
2616
    {
2617 2618
        p_sys->p_spu = spu_Create( p_stream );
        spu_Init( p_sys->p_spu );
2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
    }

    return VLC_SUCCESS;
}

static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
{
    /* Close decoder */
    if( id->p_decoder->p_module )
        module_Unneed( id->p_decoder, id->p_decoder->p_module );

    /* Close encoder */
2631 2632
    if( id->p_encoder->p_module )
        module_Unneed( id->p_encoder, id->p_encoder->p_module );
2633 2634 2635 2636 2637 2638 2639 2640
}

static int transcode_spu_process( sout_stream_t *p_stream,
                                  sout_stream_id_t *id,
                                  block_t *in, block_t **out )
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    subpicture_t *p_subpic;
2641
    *out = NULL;
2642 2643

    p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2644 2645
    if( !p_subpic ) return VLC_EGENERIC;

2646
    if( p_sys->b_master_sync && p_sys->i_master_drift )
2647
    {
2648 2649
        p_subpic->i_start -= p_sys->i_master_drift;
        if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
2650 2651
    }

2652 2653 2654 2655 2656
    if( p_sys->b_soverlay )
    {
        spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
    }
    else
2657 2658
    {
        block_t *p_block;
2659

2660
        p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2661 2662 2663 2664 2665 2666 2667
        spu_del_buffer( id->p_decoder, p_subpic );

        if( p_block )
        {
            block_ChainAppend( out, p_block );
            return VLC_SUCCESS;
        }
2668 2669 2670 2671 2672 2673 2674
    }

    return VLC_EGENERIC;
}

static subpicture_t *spu_new_buffer( decoder_t *p_dec )
{
2675 2676
    sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
    return spu_CreateSubpicture( p_stream->p_sys->p_spu );
2677 2678 2679 2680
}

static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
{
2681 2682
    sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
    spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
2683
}
2684 2685 2686 2687 2688 2689 2690 2691

/*
 * OSD menu
 */
static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    es_format_t fmt;
2692

2693 2694 2695 2696 2697
    fmt.i_cat = SPU_ES;
    fmt.i_id = 0xbd1f; /* pid ?? */
    fmt.i_group = 3;   /* pmt entry ?? */
    fmt.i_codec = VLC_FOURCC( 'Y', 'U', 'V', 'A' );
    fmt.psz_language = strdup( "osd" );
2698

2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
    id = malloc( sizeof( sout_stream_id_t ) );
    memset( id, 0, sizeof(sout_stream_id_t) );

    id->id = NULL;
    id->p_decoder = NULL;
    id->p_encoder = NULL;

    /* Create encoder object */
    id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
    if( !id->p_encoder )
    {
        msg_Err( p_stream, "out of memory" );
        goto error;
    }
    vlc_object_attach( id->p_encoder, p_stream );
    id->p_encoder->p_module = NULL;

    /* Create fake destination format */
    es_format_Init( &id->p_encoder->fmt_out, fmt.i_cat, 0 );
    id->p_encoder->fmt_out.i_id    = fmt.i_id;
    id->p_encoder->fmt_out.i_group = fmt.i_group;
    id->p_encoder->fmt_out.psz_language = strdup( fmt.psz_language );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2721

2722 2723 2724 2725 2726 2727 2728 2729
    if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
    {
        msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
                 "to fcc=`%4.4s'", (char*)&fmt.i_codec,
                 (char*)&p_sys->i_osdcodec );

        /* Complete destination format */
        id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2730

2731 2732 2733
        /*
         * Open encoder
         */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2734

2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745
        /* Initialization of encoder format structures */
        es_format_Init( &id->p_encoder->fmt_in, fmt.i_cat, fmt.i_codec );
        id->p_encoder->fmt_in.psz_language = strdup( fmt.psz_language );

        id->p_encoder->p_cfg = p_sys->p_osd_cfg;

        id->p_encoder->p_module =
            module_Need( id->p_encoder, "encoder", p_sys->psz_osdenc, VLC_TRUE );

        if( !id->p_encoder->p_module )
        {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
2746
            msg_Err( p_stream, "cannot find encoder (%s)", p_sys->psz_osdenc );
2747 2748
            goto error;
        }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2749

2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
        /* open output stream */
        id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
        id->b_transcode = VLC_TRUE;

        if( !id->id ) goto error;
    }
    else
    {
        msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
                 (char*)&fmt.i_codec );
        id->id = p_sys->p_out->pf_add( p_sys->p_out, &fmt );
        id->b_transcode = VLC_FALSE;

        if( !id->id ) goto error;
    }

    p_sys->id_osd = id;
2767
    p_sys->b_es_osd = VLC_TRUE;
2768 2769 2770 2771 2772 2773 2774

    if( !p_sys->p_spu )
    {
        p_sys->p_spu = spu_Create( p_stream );
        if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
            msg_Err( p_sys, "spu initialisation failed" );
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2775

2776 2777
    if( fmt.psz_language )
        free( fmt.psz_language );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2778

2779
    return VLC_SUCCESS;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2780

2781 2782 2783 2784
 error:
    msg_Err( p_stream, "starting osd encoding thread failed" );
    if( id->p_encoder )
    {
2785 2786
        if( id->p_encoder->p_module )
            module_Unneed( id->p_encoder, id->p_encoder->p_module );
2787 2788 2789
        vlc_object_detach( id->p_encoder );
        vlc_object_destroy( id->p_encoder );
    }
2790 2791
    free( fmt.psz_language );
    free( id );
2792
    p_sys->id_osd = NULL;
2793
    p_sys->b_es_osd = VLC_FALSE;
2794 2795
    return VLC_EGENERIC;
}
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2796

2797
static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2798
{
2799
    sout_stream_sys_t *p_sys = p_stream->p_sys;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2800

2801
    /* Close encoder */
2802
    if( id )
2803
    {
2804
        if( id->p_encoer && id->p_encoder->p_module )
2805
            module_Unneed( id->p_encoder, id->p_encoder->p_module );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2806

2807
        if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2808

2809 2810 2811 2812 2813 2814
        if( id->p_encoder )
        {
            vlc_object_detach( id->p_encoder );
            vlc_object_destroy( id->p_encoder );
        }
    }
2815
    p_sys->b_es_osd = VLC_FALSE;
2816
    free( id );
2817 2818 2819 2820 2821 2822 2823 2824
}

static int transcode_osd_process( sout_stream_t *p_stream,
                                  sout_stream_id_t *id,
                                  block_t *in, block_t **out )
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    subpicture_t *p_subpic = NULL;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2825

2826 2827 2828
    /* Check if we have a subpicture to send */
    if( p_sys->p_spu && in->i_dts > 0)
    {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
2829
        p_subpic = spu_SortSubpictures( p_sys->p_spu, in->i_dts, VLC_FALSE );
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
    }
    else
    {
        msg_Warn( p_stream, "spu channel not initialized, doing it now" );
        if( !p_sys->p_spu )
        {
            p_sys->p_spu = spu_Create( p_stream );
            if( spu_Init( p_sys->p_spu ) != VLC_SUCCESS )
                msg_Err( p_stream, "spu initialisation failed" );
        }
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2841

2842 2843 2844
    if( p_subpic )
    {
        block_t *p_block = NULL;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2845

2846 2847 2848 2849 2850
        if( p_sys->b_master_sync && p_sys->i_master_drift )
        {
            p_subpic->i_start -= p_sys->i_master_drift;
            if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
        }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2851

2852 2853 2854 2855 2856 2857 2858 2859 2860
        p_block = p_sys->id_osd->p_encoder->pf_encode_sub( p_sys->id_osd->p_encoder, p_subpic );
        if( p_block )
        {
            p_block->i_dts = p_block->i_pts = in->i_dts;
            block_ChainAppend( out, p_block );
            if( *out )
            {
                if( p_sys->p_out->pf_send( p_sys->p_out, p_sys->id_osd->id, *out ) == VLC_SUCCESS )
                    spu_DestroySubpicture( p_sys->p_spu, p_subpic );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
2861 2862
            }
            return VLC_SUCCESS;
2863 2864 2865 2866
        }
    }
    return VLC_EGENERIC;
}