<div dir="auto">As all the experts are back from vacation, can some one have a quick look on my issue. <div dir="auto"><br></div><div dir="auto">Thanks</div><div dir="auto">Benji</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Jan 1, 2018 5:37 PM, "Benji Amello" <<a href="mailto:benji.amello@gmail.com">benji.amello@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Happy New Year!<br>
Can any libav experts have a look on the quality of streaming issue I am facing?<br>
<br>
Thanks<br>
Benji<br>
<br>
On Wed, Dec 27, 2017 at 1:12 PM, Benji Amello <<a href="mailto:benji.amello@gmail.com">benji.amello@gmail.com</a>> wrote:<br>
> Any help is much appreciated<br>
><br>
> On Mon, Dec 25, 2017 at 6:13 PM, Benji Amello <<a href="mailto:benji.amello@gmail.com">benji.amello@gmail.com</a>><br>
> wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> I am struggling with issue for past 2 weeks & making almost no progress.<br>
>> I am trying to do codec copy of a stream(testing with file & later<br>
>> going to use live stream) with format rtp_mpegts over network & play<br>
>> using VLC player. Started my proof of concept code with slightly<br>
>> modified remuxing.c in the examples.<br>
>><br>
>><br>
>> I am essentially trying to do is to replicate<br>
>> ./ffmpeg -re -i TEST_VIDEO.ts -acodec copy -vcodec copy -f rtp_mpegts<br>
>> rtp://<a href="http://239.245.0.2:5002" rel="noreferrer" target="_blank">239.245.0.2:5002</a><br>
>><br>
>> Streaming is happening, but the quality is terrible.<br>
>> Looks like many frames are skipped plus streaming is happening really<br>
>> slow(buffer underflow reported by VLC player)<br>
>><br>
>> File plays perfectly fine directly on VLC player.<br>
>> Please help.<br>
>><br>
>> Stream details.<br>
>> Input #0, mpegts, from ' TEST_VIDEO.ts':<br>
>> Duration: 00:10:00.40, start: 41313.400811, bitrate: 2840 kb/s<br>
>> Program 1<br>
>> Stream #0:0[0x11]: Video: h264 (High) ([27][0][0][0] / 0x001B),<br>
>> yuv420p(tv, bt709, top first), 1440x1080 [SAR 4:3 DAR 16:9], 29.97<br>
>> fps, 59.94 tbr, 90k tbn, 59.94 tbc<br>
>> Stream #0:1[0x14]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz,<br>
>> stereo, fltp, 448 kb/s<br>
>> Output #0, rtp_mpegts, to 'rtp://<a href="http://239.255.0.2:5004" rel="noreferrer" target="_blank">239.255.0.2:5004</a>':<br>
>> Metadata:<br>
>> encoder : Lavf57.83.100<br>
>> Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B),<br>
>> yuv420p(tv, bt709, top first), 1440x1080 [SAR 4:3 DAR 16:9], q=2-31,<br>
>> 29.97 fps, 59.94 tbr, 90k tbn, 29.97 tbc<br>
>> Stream #0:1: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo,<br>
>> fltp, 448 kb/s<br>
>> Stream mapping:<br>
>> Stream #0:0 -> #0:0 (copy)<br>
>> Stream #0:1 -> #0:1 (copy)<br>
>> Press [q] to stop, [?] for help<br>
>> frame= 418 fps=5.2 q=-1.0 size= 3346kB time=00:00:08.50<br>
>> bitrate=3223.5kbits/s speed=0.106x<br>
>> Thanks<br>
>> Benji<br>
>><br>
>><br>
>> My complete source code(This is almost same as remuxing.c)<br>
>><br>
>><br>
>> #include <libavutil/timestamp.h><br>
>> #include <libavformat/avformat.h><br>
>><br>
>> static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket<br>
>> *pkt, const char *tag)<br>
>> {<br>
>> AVRational *time_base =<br>
>> &fmt_ctx->streams[pkt->stream_<wbr>index]->time_base;<br>
>><br>
>> printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s<br>
>> duration_time:%s stream_index:%d\n",<br>
>> tag,<br>
>> av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),<br>
>> av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),<br>
>> av_ts2str(pkt->duration), av_ts2timestr(pkt->duration,<br>
>> time_base),<br>
>> pkt->stream_index);<br>
>> }<br>
>><br>
>><br>
>> int main(int argc, char **argv)<br>
>> {<br>
>> AVOutputFormat *ofmt = NULL;<br>
>> AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;<br>
>> AVPacket pkt;<br>
>> const char *in_filename, *out_filename;<br>
>> int ret, i;<br>
>> int stream_index = 0;<br>
>> int *stream_mapping = NULL;<br>
>> int stream_mapping_size = 0;<br>
>> AVRational mux_timebase;<br>
>> int64_t start_time = 0; //(of->start_time == AV_NOPTS_VALUE) ? 0 :<br>
>> of->start_time;<br>
>> int64_t ost_tb_start_time = 0; //av_rescale_q(start_time,<br>
>> AV_TIME_BASE_Q, ost->mux_timebase);<br>
>><br>
>> if (argc < 3) {<br>
>> printf("usage: %s input output\n"<br>
>> "API example program to remux a media file with<br>
>> libavformat and libavcodec.\n"<br>
>> "The output format is guessed according to the file<br>
>> extension.\n"<br>
>> "\n", argv[0]);<br>
>> return 1;<br>
>> }<br>
>><br>
>> in_filename = argv[1];<br>
>> out_filename = argv[2];<br>
>><br>
>> av_register_all();<br>
>> avcodec_register_all();<br>
>> avformat_network_init();<br>
>><br>
>> if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {<br>
>> fprintf(stderr, "Could not open input file '%s'", in_filename);<br>
>> goto end;<br>
>> }<br>
>><br>
>> if ((ret = avformat_find_stream_info(<wbr>ifmt_ctx, 0)) < 0) {<br>
>> fprintf(stderr, "Failed to retrieve input stream information");<br>
>> goto end;<br>
>> }<br>
>><br>
>> av_dump_format(ifmt_ctx, 0, in_filename, 0);<br>
>><br>
>> avformat_alloc_output_<wbr>context2(&ofmt_ctx, NULL, "rtp_mpegts",<br>
>> out_filename);<br>
>> if (!ofmt_ctx) {<br>
>> fprintf(stderr, "Could not create output context\n");<br>
>> ret = AVERROR_UNKNOWN;<br>
>> goto end;<br>
>> }<br>
>><br>
>> stream_mapping_size = ifmt_ctx->nb_streams;<br>
>> stream_mapping = av_mallocz_array(stream_<wbr>mapping_size,<br>
>> sizeof(*stream_mapping));<br>
>> if (!stream_mapping) {<br>
>> ret = AVERROR(ENOMEM);<br>
>> goto end;<br>
>> }<br>
>><br>
>> ofmt = ofmt_ctx->oformat;<br>
>><br>
>> for (i = 0; i < ifmt_ctx->nb_streams; i++)<br>
>> {<br>
>> AVStream *out_stream;<br>
>> AVStream *in_stream = ifmt_ctx->streams[i];<br>
>> AVCodecParameters *in_codecpar = in_stream->codecpar;<br>
>><br>
>> if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&<br>
>> in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&<br>
>> in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {<br>
>> stream_mapping[i] = -1;<br>
>> continue;<br>
>> }<br>
>><br>
>> stream_mapping[i] = stream_index++;<br>
>><br>
>><br>
>> out_stream = avformat_new_stream(ofmt_ctx, NULL);<br>
>> if (!out_stream) {<br>
>> fprintf(stderr, "Failed allocating output stream\n");<br>
>> ret = AVERROR_UNKNOWN;<br>
>> goto end;<br>
>> }<br>
>><br>
>> //out_stream->codecpar->codec_<wbr>tag = 0;<br>
>> if (0 == out_stream->codecpar->codec_<wbr>tag)<br>
>> {<br>
>> unsigned int codec_tag_tmp;<br>
>><br>
>> if (!out_stream->codecpar->codec_<wbr>tag ||<br>
>> av_codec_get_id (ofmt->codec_tag,<br>
>> in_codecpar->codec_tag) == in_codecpar->codec_id ||<br>
>> !av_codec_get_tag2(ofmt-><wbr>codec_tag,<br>
>> in_codecpar->codec_id, &codec_tag_tmp))<br>
>> out_stream->codecpar->codec_<wbr>tag = in_codecpar->codec_tag;<br>
>> }<br>
>> //ret = avcodec_parameters_to_context(<wbr>ost->enc_ctx,<br>
>> ist->st->codecpar);<br>
>><br>
>> ret = avcodec_parameters_copy(out_<wbr>stream->codecpar, in_codecpar);<br>
>> if (ret < 0) {<br>
>> fprintf(stderr, "Failed to copy codec parameters\n");<br>
>> goto end;<br>
>> }<br>
>> //out_stream->codecpar->codec_<wbr>tag = codec_tag;<br>
>> // copy timebase while removing common factors<br>
>><br>
>> printf("bit_rate %lld sample_rate %d frame_size %d\n",<br>
>> in_codecpar->bit_rate, in_codecpar->sample_rate,<br>
>> in_codecpar->frame_size);<br>
>><br>
>> out_stream->avg_frame_rate = in_stream->avg_frame_rate;<br>
>><br>
>> ret = avformat_transfer_internal_<wbr>stream_timing_info(ofmt,<br>
>><br>
>> out_stream, in_stream,<br>
>><br>
>> AVFMT_TBCF_AUTO);<br>
>> if (ret < 0) {<br>
>> fprintf(stderr,<br>
>> "avformat_transfer_internal_<wbr>stream_timing_info failed\n");<br>
>> goto end;<br>
>> }<br>
>><br>
>> if (out_stream->time_base.num <= 0 || out_stream->time_base.den <=<br>
>> 0)<br>
>> out_stream->time_base =<br>
>> av_add_q(av_stream_get_codec_<wbr>timebase(out_stream), (AVRational){0,<br>
>> 1});<br>
>><br>
>> // copy estimated duration as a hint to the muxer<br>
>> if (out_stream->duration <= 0 && in_stream->duration > 0)<br>
>> out_stream->duration = av_rescale_q(in_stream-><wbr>duration,<br>
>> in_stream->time_base, out_stream->time_base);<br>
>><br>
>> // copy disposition<br>
>> out_stream->disposition = in_stream->disposition;<br>
>><br>
>> out_stream->sample_aspect_<wbr>ratio = in_stream->sample_aspect_<wbr>ratio;<br>
>> out_stream->avg_frame_rate = in_stream->avg_frame_rate;<br>
>> out_stream->r_frame_rate = in_stream->r_frame_rate;<br>
>><br>
>> if ( in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO)<br>
>> {<br>
>><br>
>> mux_timebase = in_stream->time_base;<br>
>> }<br>
>><br>
>><br>
>> if (in_stream->nb_side_data) {<br>
>> for (i = 0; i < in_stream->nb_side_data; i++) {<br>
>> const AVPacketSideData *sd_src = &in_stream->side_data[i];<br>
>> uint8_t *dst_data;<br>
>><br>
>> dst_data = av_stream_new_side_data(out_<wbr>stream,<br>
>> sd_src->type, sd_src->size);<br>
>> if (!dst_data)<br>
>> return AVERROR(ENOMEM);<br>
>> memcpy(dst_data, sd_src->data, sd_src->size);<br>
>> }<br>
>> }<br>
>> }<br>
>><br>
>> av_dump_format(ofmt_ctx, 0, out_filename, 1);<br>
>><br>
>> start_time = ofmt_ctx->duration;<br>
>> ost_tb_start_time = av_rescale_q(ofmt_ctx-><wbr>duration,<br>
>> AV_TIME_BASE_Q, mux_timebase);<br>
>><br>
>> if (!(ofmt->flags & AVFMT_NOFILE))<br>
>> {<br>
>> ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);<br>
>> if (ret < 0) {<br>
>> fprintf(stderr, "Could not open output file '%s'",<br>
>> out_filename);<br>
>> goto end;<br>
>> }<br>
>> }<br>
>><br>
>> ret = avformat_write_header(ofmt_<wbr>ctx, NULL);<br>
>> if (ret < 0) {<br>
>> fprintf(stderr, "Error occurred when opening output file\n");<br>
>> goto end;<br>
>> }<br>
>><br>
>> while (1)<br>
>> {<br>
>> AVStream *in_stream, *out_stream;<br>
>><br>
>> ret = av_read_frame(ifmt_ctx, &pkt);<br>
>> if (ret < 0)<br>
>> break;<br>
>><br>
>> in_stream = ifmt_ctx->streams[pkt.stream_<wbr>index];<br>
>> if (pkt.stream_index >= stream_mapping_size ||<br>
>> stream_mapping[pkt.stream_<wbr>index] < 0) {<br>
>> av_packet_unref(&pkt);<br>
>> continue;<br>
>> }<br>
>><br>
>> pkt.stream_index = stream_mapping[pkt.stream_<wbr>index];<br>
>> out_stream = ofmt_ctx->streams[pkt.stream_<wbr>index];<br>
>><br>
>> //log_packet(ifmt_ctx, &pkt, "in");<br>
>><br>
>><br>
>> //ofmt_ctx->bit_rate = ifmt_ctx->bit_rate;<br>
>> ofmt_ctx->duration = ifmt_ctx->duration;<br>
>> /* copy packet */<br>
>> //pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base,<br>
>> out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_<wbr>PASS_MINMAX);<br>
>> //pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base,<br>
>> out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_<wbr>PASS_MINMAX);<br>
>><br>
>> if (pkt.pts != AV_NOPTS_VALUE)<br>
>> pkt.pts = av_rescale_q(pkt.pts,<br>
>> in_stream->time_base,mux_<wbr>timebase) - ost_tb_start_time;<br>
>> else<br>
>> pkt.pts = AV_NOPTS_VALUE;<br>
>><br>
>> if (pkt.dts == AV_NOPTS_VALUE)<br>
>> pkt.dts = av_rescale_q(pkt.dts, AV_TIME_BASE_Q, mux_timebase);<br>
>> else<br>
>> pkt.dts = av_rescale_q(pkt.dts, in_stream->time_base,<br>
>> mux_timebase);<br>
>> pkt.dts -= ost_tb_start_time;<br>
>><br>
>> pkt.duration = av_rescale_q(pkt.duration,<br>
>> in_stream->time_base, mux_timebase);<br>
>> //pkt.duration = av_rescale_q(1,<br>
>> av_inv_q(out_stream->avg_<wbr>frame_rate), mux_timebase);<br>
>> pkt.pos = -1;<br>
>> //log_packet(ofmt_ctx, &pkt, "out");<br>
>><br>
>><br>
>> ret = av_interleaved_write_frame(<wbr>ofmt_ctx, &pkt);<br>
>> if (ret < 0) {<br>
>> fprintf(stderr, "Error muxing packet\n");<br>
>> break;<br>
>> }<br>
>> av_packet_unref(&pkt);<br>
>> }<br>
>><br>
>> av_write_trailer(ofmt_ctx);<br>
>> end:<br>
>><br>
>> avformat_close_input(&ifmt_<wbr>ctx);<br>
>><br>
>> /* close output */<br>
>> if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))<br>
>> avio_closep(&ofmt_ctx->pb);<br>
>> avformat_free_context(ofmt_<wbr>ctx);<br>
>><br>
>> av_freep(&stream_mapping);<br>
>><br>
>> if (ret < 0 && ret != AVERROR_EOF) {<br>
>> fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));<br>
>> return 1;<br>
>> }<br>
>><br>
>> return 0;<br>
>> }<br>
>> ______________________________<wbr>_________________<br>
>> Libav-user mailing list<br>
>> <a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
>> <a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/<wbr>listinfo/libav-user</a><br>
><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> Libav-user mailing list<br>
> <a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
> <a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/<wbr>listinfo/libav-user</a><br>
><br>
</blockquote></div></div>