[Libav-user] libav rtp_mpegts not working with muxrate

Lance Henderson mr.lance.henderson at gmail.com
Wed Nov 8 20:45:55 EET 2017

To allow regular mpegts options to be used with the rtp_mpegts muxer

Current issue
Modified the rtpenc_mpegts.c source code to hardcode the "-mux_rate 8M"
option into an AVOption dictionary. This option is passed into the
av_write_header (see code #1) call that sets up the context for the mpegts
muxer. However, when the application is then run the output gives an ever
increasing bitrate (see code #2).

I have tracked the issue down to the get_pcr command in the mpegtsenc.c
code. This call seeks in the mpegts_ctx->pb to get the pcr. This is then
used to decide if a pcr or null packet should be entered into the stream. I
think the issue is coming about due to the incorrect data in the
mpegts_ctx->pb buffer (this is what the get_pcr command is seeking in).
However, when I copy the raw mpegts packet into that buffer it als does not
work (see code #3 for how I am copying). For reference running the mpegts
command on the same input file works correctly (see code #4) as it is
correctly muxed.

I will continue to look into why this may be happening but I am not 100%
clear on the reason for the mpegts_ctx->pb object. What does it serve? What
is it trying to achieve?

Hardcoded change to set the mucrate of the mpegts to 8M
av_dict_set(&d, "muxrate", "8000000", 0);

    for (i = 0; i < s->nb_streams; i++) {
        AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
        if (!st)
            goto fail;
        st->time_base           = s->streams[i]->time_base;
        st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
        avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
    if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
        goto fail;
    if ((ret = avformat_write_header(mpegts_ctx, &d)) < 0)
        goto fail;

The input command is
gdb --args ffmpeg_g -y -re -i remux_test.ts -c copy -muxrate 8M -f
rtp_mpegts rtp://

The output from running the above command is like the below; where the
bitrate constantly increases and the speed decreases.
frame=  272 fps= 17 q=-1.0 size=  960935kB time=00:00:05.91
bitrate=1331976.5kbits/s speed=0.361x

    if (!chain->mpegts_ctx->pb) {
        if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
            return ret;
chain->mpegts_ctx->pb->buffer = s->pb->buffer;
chain->mpegts_ctx->pb->buf_ptr_max = s->pb->buf_ptr_max ;
chain->mpegts_ctx->pb->buf_ptr = s->pb->buf_ptr ;
chain->mpegts_ctx->pb->buf_end = s->pb->buf_end;
chain->mpegts_ctx->pb->buffer_size = s->pb->buffer_size;
chain->mpegts_ctx->pb->orig_buffer_size = s->pb->orig_buffer_size;
chain->mpegts_ctx->pb->max_packet_size = s->pb->max_packet_size;
    if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
        return ret;
    size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
    chain->mpegts_ctx->pb = NULL;

gdb --args ffmpeg_g -y -re -i remux_test.ts -c copy -muxrate 8M -f mpegts
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20171108/72709394/attachment.html>

More information about the Libav-user mailing list