mux.c 16.5 KB
Newer Older
1
/*****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
2
 * mux.c: muxer using libavformat
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2006 VLC authors and VideoLAN
Pierre's avatar
Pierre committed
5
 * $Id$
6 7 8
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
zorglub's avatar
zorglub committed
33 34
#include <vlc_block.h>
#include <vlc_sout.h>
35

36
#include <libavformat/avformat.h>
37

38
#include "avformat.h"
39
#include "../../codec/avcodec/avcodec.h"
40
#include "../../codec/avcodec/avcommon.h"
41
#include "../xiph.h"
42

43

44
//#define AVFORMAT_DEBUG 1
45

46
static const char *const ppsz_mux_options[] = {
47
    "mux", "options", NULL
48 49
};

50 51 52 53 54
/*****************************************************************************
 * mux_sys_t: mux descriptor
 *****************************************************************************/
struct sout_mux_sys_t
{
55
    AVIOContext     *io;
56 57 58 59 60
    int             io_buffer_size;
    uint8_t        *io_buffer;

    AVFormatContext *oc;

61
    bool     b_write_header;
62
    bool     b_write_keyframe;
63
    bool     b_error;
64
#if LIBAVFORMAT_VERSION_CHECK( 57, 7, 0, 40, 100 )
65 66
    bool     b_header_done;
#endif
67 68 69 70 71 72 73
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Control  ( sout_mux_t *, int, va_list );
static int AddStream( sout_mux_t *, sout_input_t * );
74
static void DelStream( sout_mux_t *, sout_input_t * );
75 76 77
static int Mux      ( sout_mux_t * );

static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
78
static int64_t IOSeek( void *opaque, int64_t offset, int whence );
79
#if LIBAVFORMAT_VERSION_CHECK( 57, 7, 0, 40, 100 )
80 81 82
static int IOWriteTyped(void *opaque, uint8_t *buf, int buf_size,
                              enum AVIODataMarkerType type, int64_t time);
#endif
83 84 85 86

/*****************************************************************************
 * Open
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
87
int OpenMux( vlc_object_t *p_this )
88
{
89
    AVOutputFormat *file_oformat;
90
    sout_mux_t *p_mux = (sout_mux_t*)p_this;
91 92 93 94 95
    bool dummy = !strcmp( p_mux->p_access->psz_access, "dummy");

    if( dummy && strlen(p_mux->p_access->psz_path)
                              >= sizeof (((AVFormatContext *)NULL)->filename) )
        return VLC_EGENERIC;
96

97 98
    msg_Dbg( p_mux, "using %s %s", AVPROVIDER(LIBAVFORMAT), LIBAVFORMAT_IDENT );

99
    vlc_init_avformat(p_this);
100

101
    config_ChainParse( p_mux, "sout-avformat-", ppsz_mux_options, p_mux->p_cfg );
102

103
    /* Find the requested muxer */
104
    char *psz_mux = var_InheritString( p_mux, "sout-avformat-mux" );
105 106
    if( psz_mux )
    {
107
        file_oformat = av_guess_format( psz_mux, NULL, NULL );
Rafaël Carré's avatar
Rafaël Carré committed
108
        free( psz_mux );
109 110 111 112
    }
    else
    {
        file_oformat =
113
            av_guess_format( NULL, p_mux->p_access->psz_path, NULL);
114
    }
115 116 117 118 119 120
    if (!file_oformat)
    {
      msg_Err( p_mux, "unable for find a suitable output format" );
      return VLC_EGENERIC;
    }

121 122
    sout_mux_sys_t *p_sys = malloc( sizeof( sout_mux_sys_t ) );
    if( unlikely(p_sys == NULL) )
123
        return VLC_ENOMEM;
124

125
    p_mux->p_sys = p_sys;
126
    p_sys->oc = avformat_alloc_context();
127
    p_sys->oc->oformat = file_oformat;
128
    /* If we use dummy access, let avformat write output */
129
    if( dummy )
130
        strcpy( p_sys->oc->filename, p_mux->p_access->psz_path );
131 132

    /* Create I/O wrapper */
133
    p_sys->io_buffer_size = 10 * 1024 * 1024;  /* FIXME */
134 135
    p_sys->io_buffer = malloc( p_sys->io_buffer_size );

136 137 138
    bool b_can_seek;
    if( sout_AccessOutControl( p_mux->p_access, ACCESS_OUT_CAN_SEEK, &b_can_seek ) )
        b_can_seek = false;
139 140
    p_sys->io = avio_alloc_context(
        p_sys->io_buffer, p_sys->io_buffer_size,
141
        1, p_mux, NULL, IOWrite, b_can_seek ? IOSeek : NULL );
142 143

    p_sys->oc->pb = p_sys->io;
144 145
    p_sys->oc->nb_streams = 0;

146
    p_sys->b_write_header = true;
147
    p_sys->b_write_keyframe = false;
148
    p_sys->b_error = false;
149
#if LIBAVFORMAT_VERSION_CHECK( 57, 7, 0, 40, 100 )
150 151 152
    p_sys->io->write_data_type = IOWriteTyped;
    p_sys->b_header_done = false;
#endif
153

154 155 156 157 158 159
    /* Fill p_mux fields */
    p_mux->pf_control   = Control;
    p_mux->pf_addstream = AddStream;
    p_mux->pf_delstream = DelStream;
    p_mux->pf_mux       = Mux;

160 161 162 163 164 165
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
166
void CloseMux( vlc_object_t *p_this )
167 168 169 170
{
    sout_mux_t *p_mux = (sout_mux_t*)p_this;
    sout_mux_sys_t *p_sys = p_mux->p_sys;

171
    if( !p_sys->b_write_header && !p_sys->b_error && av_write_trailer( p_sys->oc ) < 0 )
172 173 174 175
    {
        msg_Err( p_mux, "could not write trailer" );
    }

Rafaël Carré's avatar
typo  
Rafaël Carré committed
176
    avformat_free_context(p_sys->oc);
Adrien Maglo's avatar
Adrien Maglo committed
177
    av_free(p_sys->io);
178 179 180 181 182 183 184 185 186 187 188

    free( p_sys->io_buffer );
    free( p_sys );
}

/*****************************************************************************
 * AddStream
 *****************************************************************************/
static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
{
    sout_mux_sys_t *p_sys = p_mux->p_sys;
189
    const es_format_t *fmt = p_input->p_fmt;
190 191
    AVCodecContext *codec;
    AVStream *stream;
192
    unsigned i_codec_id;
193 194 195

    msg_Dbg( p_mux, "adding input" );

196
    if( !GetFfmpegCodec( fmt->i_cat, fmt->i_codec, &i_codec_id, NULL )
197
     || i_codec_id == AV_CODEC_ID_NONE )
198 199
    {
        msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
Rafaël Carré's avatar
Rafaël Carré committed
200
                 (char *)&fmt->i_codec );
201 202 203
        return VLC_EGENERIC;
    }

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    unsigned opus_size[XIPH_MAX_HEADER_COUNT];
    void     *opus_packet[XIPH_MAX_HEADER_COUNT];
    if( fmt->i_codec == VLC_CODEC_OPUS )
    {
        unsigned count;
        /* Only transmits the first packet (OpusHead) */
        if( xiph_SplitHeaders(opus_size, opus_packet, &count, fmt->i_extra, fmt->p_extra ) ) {
            count = 0;
        }
        if (count != 2 || opus_size[0] < 19) {
            msg_Err(p_mux, "Invalid Opus header");
            return VLC_EGENERIC;
        }
    }

Rafaël Carré's avatar
Rafaël Carré committed
219
    if( fmt->i_cat != VIDEO_ES && fmt->i_cat != AUDIO_ES)
220 221 222 223 224
    {
        msg_Warn( p_mux, "Unhandled ES category" );
        return VLC_EGENERIC;
    }

225
    /* */
226
    p_input->p_sys = malloc( sizeof( int ) );
227 228 229
    if( unlikely(p_input->p_sys == NULL) )
        return VLC_ENOMEM;

230 231
    *((int *)p_input->p_sys) = p_sys->oc->nb_streams;

232
    /* */
233
    stream = avformat_new_stream( p_sys->oc, NULL);
234 235 236 237 238 239 240
    if( !stream )
    {
        free( p_input->p_sys );
        return VLC_EGENERIC;
    }
    codec = stream->codec;

241 242 243
    unsigned int i_bitrate = fmt->i_bitrate;
    unsigned int i_frame_rate = fmt->video.i_frame_rate;
    unsigned int i_frame_rate_base = fmt->video.i_frame_rate_base;
Rafaël Carré's avatar
Rafaël Carré committed
244
    switch( fmt->i_cat )
245 246
    {
    case AUDIO_ES:
247
        codec->codec_type = AVMEDIA_TYPE_AUDIO;
Rafaël Carré's avatar
Rafaël Carré committed
248 249
        codec->channels = fmt->audio.i_channels;
        codec->sample_rate = fmt->audio.i_rate;
250
        stream->time_base = (AVRational){1, codec->sample_rate};
251 252
        if (fmt->i_bitrate == 0) {
            msg_Warn( p_mux, "Missing audio bitrate, assuming 64k" );
253
            i_bitrate = 64000;
254
        }
255 256 257
        break;

    case VIDEO_ES:
258
        if( !fmt->video.i_frame_rate || !fmt->video.i_frame_rate_base ) {
259
            msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
260 261
            i_frame_rate = 25;
            i_frame_rate_base = 1;
262 263 264 265 266 267
        } else
            msg_Dbg( p_mux, "Muxing framerate will be %d/%d = %.2f fps",
                    fmt->video.i_frame_rate,
                    fmt->video.i_frame_rate_base,
                    (double)fmt->video.i_frame_rate/(double)fmt->video.i_frame_rate_base );

268
        codec->codec_type = AVMEDIA_TYPE_VIDEO;
269 270
        codec->width = fmt->video.i_visible_width;
        codec->height = fmt->video.i_visible_height;
271 272
        av_reduce( &codec->sample_aspect_ratio.num,
                   &codec->sample_aspect_ratio.den,
Rafaël Carré's avatar
Rafaël Carré committed
273 274
                   fmt->video.i_sar_num,
                   fmt->video.i_sar_den, 1 << 30 /* something big */ );
275 276
        msg_Dbg(p_mux, "Muxing aspect ratio will be %d/%d",
                fmt->video.i_sar_num, fmt->video.i_sar_den);
277
        stream->sample_aspect_ratio.den = codec->sample_aspect_ratio.den;
278
        stream->sample_aspect_ratio.num = codec->sample_aspect_ratio.num;
279 280
        stream->time_base.den = i_frame_rate;
        stream->time_base.num = i_frame_rate_base;
281 282
        if (fmt->i_bitrate == 0) {
            msg_Warn( p_mux, "Missing video bitrate, assuming 512k" );
283
            i_bitrate = 512000;
284 285
        } else
            msg_Dbg( p_mux, "Muxing video bitrate will be %d", fmt->i_bitrate );
286
        break;
287

288 289
    default:
        vlc_assert_unreachable();
290 291
    }

292
    codec->bit_rate = i_bitrate;
293
    codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
294
    if( !codec->codec_tag && i_codec_id == AV_CODEC_ID_MP2 )
295
    {
296
        i_codec_id = AV_CODEC_ID_MP3;
297 298
        codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
    }
299 300
    codec->codec_id = i_codec_id;

Rafaël Carré's avatar
Rafaël Carré committed
301
    if( fmt->i_extra )
302
    {
303 304 305 306 307 308 309 310 311 312 313 314
        if( fmt->i_codec == VLC_CODEC_OPUS )
        {
            codec->extradata_size = opus_size[0];
            codec->extradata = av_malloc( opus_size[0] );
            memcpy( codec->extradata, opus_packet[0], opus_size[0] );
        }
        else
        {
            codec->extradata_size = fmt->i_extra;
            codec->extradata = av_malloc( fmt->i_extra );
            memcpy( codec->extradata, fmt->p_extra, fmt->i_extra );
        }
315 316 317 318 319 320 321 322
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * DelStream
 *****************************************************************************/
323
static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
324 325 326 327 328 329 330 331 332 333
{
    msg_Dbg( p_mux, "removing input" );
    free( p_input->p_sys );
}

static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
{
    sout_mux_sys_t *p_sys = p_mux->p_sys;
    block_t *p_data = block_FifoGet( p_input->p_fifo );
    int i_stream = *((int *)p_input->p_sys);
334
    AVStream *p_stream = p_sys->oc->streams[i_stream];
335 336 337
    AVPacket pkt;

    memset( &pkt, 0, sizeof(AVPacket) );
338

339
    av_init_packet(&pkt);
340 341 342 343
    pkt.data = p_data->p_buffer;
    pkt.size = p_data->i_buffer;
    pkt.stream_index = i_stream;

344 345 346 347 348 349 350 351 352 353 354
    if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
    {
#ifdef AVFMT_ALLOW_FLUSH
        /* Make sure we don't inadvertedly mark buffered data as keyframes. */
        if( p_sys->oc->oformat->flags & AVFMT_ALLOW_FLUSH )
            av_write_frame( p_sys->oc, NULL );
#endif

        p_sys->b_write_keyframe = true;
        pkt.flags |= AV_PKT_FLAG_KEY;
    }
355

356
    if( p_data->i_pts > 0 )
357
        pkt.pts = p_data->i_pts * p_stream->time_base.den /
358
            CLOCK_FREQ / p_stream->time_base.num;
359
    if( p_data->i_dts > 0 )
360
        pkt.dts = p_data->i_dts * p_stream->time_base.den /
361
            CLOCK_FREQ / p_stream->time_base.num;
362

363
    /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
364
    p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
365
            CLOCK_FREQ / p_stream->time_base.num ) - 1;
366

367 368
    if( av_write_frame( p_sys->oc, &pkt ) < 0 )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
369 370
        msg_Err( p_mux, "could not write frame (pts: %"PRId64", dts: %"PRId64") "
                 "(pkt pts: %"PRId64", dts: %"PRId64")",
371
                 p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
372
        block_Release( p_data );
373 374 375 376 377 378 379
        return VLC_EGENERIC;
    }

    block_Release( p_data );
    return VLC_SUCCESS;
}

380
#if LIBAVFORMAT_VERSION_CHECK( 57, 7, 0, 40, 100 )
381 382 383 384 385 386 387 388 389 390 391 392 393
int IOWriteTyped(void *opaque, uint8_t *buf, int buf_size,
                              enum AVIODataMarkerType type, int64_t time)
{
    VLC_UNUSED(time);

    sout_mux_t *p_mux = opaque;
    sout_mux_sys_t *p_sys = p_mux->p_sys;
    if ( !p_sys->b_header_done && type != AVIO_DATA_MARKER_HEADER )
        p_sys->b_header_done = true;
    return IOWrite(opaque, buf, buf_size);
}
#endif

394 395 396 397 398 399 400 401 402 403 404
/*****************************************************************************
 * Mux: multiplex available data in input fifos
 *****************************************************************************/
static int Mux( sout_mux_t *p_mux )
{
    sout_mux_sys_t *p_sys = p_mux->p_sys;

    if( p_sys->b_error ) return VLC_EGENERIC;

    if( p_sys->b_write_header )
    {
405
        int error;
406 407
        msg_Dbg( p_mux, "writing header" );

408 409
        char *psz_opts = var_GetNonEmptyString( p_mux, "sout-avformat-options" );
        AVDictionary *options = NULL;
410
        if (psz_opts) {
411
            vlc_av_get_options(psz_opts, &options);
412 413
            free(psz_opts);
        }
414
        av_dict_set( &p_sys->oc->metadata, "encoding_tool", "VLC "VERSION, 0 );
415 416 417 418 419 420
        error = avformat_write_header( p_sys->oc, options ? &options : NULL);
        AVDictionaryEntry *t = NULL;
        while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
            msg_Err( p_mux, "Unknown option \"%s\"", t->key );
        }
        av_dict_free(&options);
421
        if( error < 0 )
422
        {
423 424
            msg_Err( p_mux, "could not write header: %s",
                     vlc_strerror_c(AVUNERROR(error)) );
425 426
            p_sys->b_write_header = false;
            p_sys->b_error = true;
427 428
            return VLC_EGENERIC;
        }
429

430
        avio_flush( p_sys->oc->pb );
431
        p_sys->b_write_header = false;
432 433 434 435
    }

    for( ;; )
    {
436 437 438 439 440 441
        mtime_t i_dts;

        int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
        if( i_stream < 0 )
            return VLC_SUCCESS;

442 443 444 445 446 447 448 449 450 451 452
        MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( sout_mux_t *p_mux, int i_query, va_list args )
{
453
    bool *pb_bool;
454 455 456 457

    switch( i_query )
    {
    case MUX_CAN_ADD_STREAM_WHILE_MUXING:
458
        pb_bool = va_arg( args, bool * );
459
        *pb_bool = false;
460 461 462
        return VLC_SUCCESS;

    case MUX_GET_ADD_STREAM_WAIT:
463
        pb_bool = va_arg( args, bool * );
464
        *pb_bool = true;
465 466 467
        return VLC_SUCCESS;

    case MUX_GET_MIME:
468
    {
469
        char **ppsz = va_arg( args, char ** );
470 471 472 473
        *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
        return VLC_SUCCESS;
    }

474 475 476 477 478 479 480 481 482 483
    default:
        return VLC_EGENERIC;
    }
}

/*****************************************************************************
 * I/O wrappers for libavformat
 *****************************************************************************/
static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
{
484
    sout_mux_t *p_mux = opaque;
Rafaël Carré's avatar
Rafaël Carré committed
485
    sout_mux_sys_t *p_sys = p_mux->p_sys;
486 487 488 489 490 491
    int i_ret;

#ifdef AVFORMAT_DEBUG
    msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
#endif

492
    block_t *p_buf = block_Alloc( buf_size );
493 494
    if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );

Rafaël Carré's avatar
Rafaël Carré committed
495
    if( p_sys->b_write_header )
496
        p_buf->i_flags |= BLOCK_FLAG_HEADER;
497
#if LIBAVFORMAT_VERSION_CHECK( 57, 7, 0, 40, 100 )
498 499 500
    if( !p_sys->b_header_done )
        p_buf->i_flags |= BLOCK_FLAG_HEADER;
#endif
501

Rafaël Carré's avatar
Rafaël Carré committed
502
    if( p_sys->b_write_keyframe )
503 504
    {
        p_buf->i_flags |= BLOCK_FLAG_TYPE_I;
Rafaël Carré's avatar
Rafaël Carré committed
505
        p_sys->b_write_keyframe = false;
506 507
    }

508 509 510 511
    i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
    return i_ret ? i_ret : -1;
}

512
static int64_t IOSeek( void *opaque, int64_t offset, int whence )
513
{
514
    sout_mux_t *p_mux = opaque;
515 516

#ifdef AVFORMAT_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
517
    msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
518 519 520 521 522
#endif

    switch( whence )
    {
    case SEEK_SET:
523
        return sout_AccessOutSeek( p_mux->p_access, offset );
524 525 526 527 528 529
    case SEEK_CUR:
    case SEEK_END:
    default:
        return -1;
    }
}