<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Dear libav users, <div><br></div><div>I'm trying to do the following:</div><div><br></div><div><div>1. Decode a packet from a H264 encoded video (orig_packet),</div><div>2. Scale the resulting YUV frame to RGB24,</div><div>3. Draw a watermark on the resulting frame,</div><div>4. Scale it back to YUV420P,</div><div>5. Encode yuv frame in a new packet (styl_packet),</div><div>6. Copy dts and pts time from original packet to the stylized packet</div></div><div>7. Write this packet in a MPEG4 container</div><div><br></div><div>So far, and it's been a painful and long way to reach this point, everything works (almost)</div><div>great but I'm facing two issues:</div><div><br></div><div>1) Video and audio are not synched, it seems that the video is a bit slower than it should be (see packet duration & timestamps below)</div><div>2) The resulting image quality is poor, we can see macro blocks covering the whole images (even in still parts of the frames).</div><div><br></div><div>Any help would be a big relief !</div><div>Thank you!</div><div><br></div><div><br></div><div>Here is the code for the stylization:</div><div>_______________________________</div><div><div>void vs_stylize(VS_VideoContext *in_video_ctx, VS_VideoContext *out_video_ctx)</div><div>{</div><div>    AVStream *in_stream, *out_stream;</div><div>    AVPacket orig_pkt, styl_pkt;</div><div><br></div><div>    VS_Picture *yuv_pix=vs_alloc_picture(in_video_ctx, VS_PIX_FMT_YUV420P);</div><div>    VS_Picture *rgb_pix=vs_alloc_picture(in_video_ctx, VS_PIX_FMT_RGB24);</div><div><br></div><div>    int ret, got_something;</div><div>    int idx=0;//saved frame index in name</div><div><br></div><div>    av_init_packet(&orig_pkt);</div><div>    av_init_packet(&styl_pkt);</div><div><br></div><div>    while(1)</div><div>    {</div><div>        ret=av_read_frame(in_video_ctx->format_ctx, &orig_pkt);</div><div>        if(ret<0)</div><div>        break;</div><div><br></div><div>        in_stream=in_video_ctx->format_ctx->streams[orig_pkt.stream_index];</div><div>        out_stream=out_video_ctx->format_ctx->streams[orig_pkt.stream_index];</div><div><br></div><div>        log_packet(in_video_ctx->format_ctx, &orig_pkt, "in");</div><div><br></div><div>        if(in_stream->codec->codec->type==AVMEDIA_TYPE_AUDIO)</div><div>        {</div><div>            // simply copy audio packet</div><div>            orig_pkt.pts = av_rescale_q_rnd(orig_pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);</div><div>            orig_pkt.dts = av_rescale_q_rnd(orig_pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);</div><div>            orig_pkt.duration = av_rescale_q(orig_pkt.duration, in_stream->time_base, out_stream->time_base);</div><div>            orig_pkt.pos = -1;</div><div><br></div><div>            ret = av_interleaved_write_frame(out_video_ctx->format_ctx, &orig_pkt);</div><div>            if (ret < 0) {</div><div>                fprintf(stderr, "Error muxing packet\n");</div><div>                break;</div><div>            }</div><div>        }</div><div>        else</div><div>        if(in_stream->codec->codec->type==AVMEDIA_TYPE_VIDEO)</div><div>        {</div><div>            //Decode packet (orig_packet),</div><div>            //Scale to RGB24,</div><div>            //Put a watermark on the frame,</div><div>            //Scale back to YUV420P,</div><div>            //Copy dts and pts time from original packet</div><div>            //Encode yuv frame in a new packet (styl_packet);</div><div>            ret=avcodec_decode_video2(in_stream->codec, yuv_pix->av_frame, &got_something, &orig_pkt);</div><div>            if(got_something!=0)</div><div>            {</div><div>                rgb_pix->av_frame=YUVtoRGB(yuv_pix->av_frame, in_stream->codec);</div><div>                waterMark(rgb_pix->av_frame, in_stream->codec);</div><div>                yuv_pix->av_frame=RGBtoYUV(rgb_pix->av_frame, in_stream->codec);</div><div>                avcodec_encode_video2(out_stream->codec, &styl_pkt, yuv_pix->av_frame, &got_something);</div><div>                if(!got_something)</div><div>                {</div><div>                    INFO(stderr, ":-( Unable to encode yuv frame.\n");</div><div>                    exit(0);</div><div>                }      </div><div>            }</div><div><br></div><div>            //copy timestamps</div><div>            //Note: dans transcodeing .c they use av_packet_rescale_ts()</div><div>            styl_pkt.pts = av_rescale_q_rnd(orig_pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);</div><div>            styl_pkt.dts = av_rescale_q_rnd(orig_pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);</div><div>            styl_pkt.duration = av_rescale_q(orig_pkt.duration, in_stream->time_base, out_stream->time_base);</div><div>            styl_pkt.pos = -1;</div><div>            log_packet(out_video_ctx->format_ctx, &orig_pkt, "ORIG");</div><div>            log_packet(out_video_ctx->format_ctx, &styl_pkt, "STYL");</div><div>            //I noticed timestamps are totally different even when both codec contexts share the same time base.</div><div>            ret = av_interleaved_write_frame(out_video_ctx->format_ctx, &styl_pkt);</div><div>            if (ret < 0) {</div><div>                fprintf(stderr, "Error muxing packet\n");</div><div>                break;</div><div>            }</div><div>        }</div><div>        av_free_packet(&orig_pkt);</div><div>        av_free_packet(&styl_pkt);</div><div>    }</div><div>    av_write_trailer(out_video_ctx->format_ctx);</div><div><br></div><div>    vs_free(in_video_ctx);//free and close everything</div><div>    vs_free(out_video_ctx);</div><div>}</div></div><div><br></div><div>And the output with packet infos:</div><div>______________________________</div><div><br></div><div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">dhcp10:vs_lib-5 Fix$ ./execute small.mp4 out_</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 95, 255); background-color: rgb(0, 0, 0);">[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f841200a000] <span style="color: #fffb01">overread end of atom 'colr' by 1 bytes</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'small.mp4':</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">  Metadata:</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    major_brand     : mp42</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    minor_version   : 0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    compatible_brands: mp42isomavc1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    creation_time   : 2010-03-20 21:29:11</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    encoder         : HandBrake 0.9.4 2009112300</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">  Duration: 00:00:05.57, start: 0.000000, bitrate: 551 kb/s</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, bt709), 560x320, 465 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    Metadata:</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">      creation_time   : 2010-03-20 21:29:11</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">      encoder         : JVT/AVC Coding</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 83 kb/s (default)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    Metadata:</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">      creation_time   : 2010-03-20 21:29:11</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">----------------------------------------Codec for output codec context is mpeg4</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">Successfully opened mpeg4 codec</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 251, 1); background-color: rgb(0, 0, 0);"><span style="color: #ff87ff">[mp4 @ 0x7f8412887200] </span>Using AVStream.codec.time_base as a timebase hint to the muxer is deprecated. Set AVStream.time_base instead.</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 251, 1); background-color: rgb(0, 0, 0);"><span style="color: #ff87ff">[mp4 @ 0x7f8412887200] </span>Codec for stream 0 does not use global headers but container format requires global headers</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 251, 1); background-color: rgb(0, 0, 0);"><span style="color: #ff87ff">[mp4 @ 0x7f8412887200] </span>Using AVStream.codec.time_base as a timebase hint to the muxer is deprecated. Set AVStream.time_base instead.</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">Output #0, mp4, to 'out_small.mp4':</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">  Metadata:</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    encoder         : Lavf56.15.102</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    Stream #0:0: Video: mpeg4 ( [0][0][0] / 0x0020), yuv420p, 560x320, q=2-31, 465 kb/s, 15360 tbn, 60 tbc</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">    Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, mono, fltp, 83 kb/s</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">----------------------------------------in: pts:0 pts_time:0 dts:0 dts_time:0 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:0 pts_time:0 dts:0 dts_time:0 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:0 pts_time:0 dts:0 dts_time:0 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:3000 pts_time:0.0333333 dts:3000 dts_time:0.0333333 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:3000 pts_time:0.195312 dts:3000 dts_time:0.195312 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:512 pts_time:0.0333333 dts:512 dts_time:0.0333333 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:6000 pts_time:0.0666667 dts:6000 dts_time:0.0666667 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:6000 pts_time:0.390625 dts:6000 dts_time:0.390625 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:1024 pts_time:0.0666667 dts:1024 dts_time:0.0666667 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:9000 pts_time:0.1 dts:9000 dts_time:0.1 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:9000 pts_time:0.585938 dts:9000 dts_time:0.585938 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:1536 pts_time:0.1 dts:1536 dts_time:0.1 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:0 pts_time:0 dts:0 dts_time:0 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:1024 pts_time:0.0213333 dts:1024 dts_time:0.0213333 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:2048 pts_time:0.0426667 dts:2048 dts_time:0.0426667 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:3072 pts_time:0.064 dts:3072 dts_time:0.064 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:4096 pts_time:0.0853333 dts:4096 dts_time:0.0853333 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:5120 pts_time:0.106667 dts:5120 dts_time:0.106667 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:6144 pts_time:0.128 dts:6144 dts_time:0.128 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:12000 pts_time:0.133333 dts:12000 dts_time:0.133333 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:12000 pts_time:0.78125 dts:12000 dts_time:0.78125 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:2048 pts_time:0.133333 dts:2048 dts_time:0.133333 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:15000 pts_time:0.166667 dts:15000 dts_time:0.166667 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:15000 pts_time:0.976562 dts:15000 dts_time:0.976562 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:2560 pts_time:0.166667 dts:2560 dts_time:0.166667 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:18000 pts_time:0.2 dts:18000 dts_time:0.2 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:18000 pts_time:1.17188 dts:18000 dts_time:1.17188 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:3072 pts_time:0.2 dts:3072 dts_time:0.2 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:21000 pts_time:0.233333 dts:21000 dts_time:0.233333 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:21000 pts_time:1.36719 dts:21000 dts_time:1.36719 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:3584 pts_time:0.233333 dts:3584 dts_time:0.233333 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:7168 pts_time:0.149333 dts:7168 dts_time:0.149333 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:8192 pts_time:0.170667 dts:8192 dts_time:0.170667 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:9216 pts_time:0.192 dts:9216 dts_time:0.192 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:10240 pts_time:0.213333 dts:10240 dts_time:0.213333 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:11264 pts_time:0.234667 dts:11264 dts_time:0.234667 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:12288 pts_time:0.256 dts:12288 dts_time:0.256 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:13312 pts_time:0.277333 dts:13312 dts_time:0.277333 duration:1024 duration_time:0.0213333 stream_index:1</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:24000 pts_time:0.266667 dts:24000 dts_time:0.266667 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ret 320</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><-------------------- Watermarking frame</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">----------------------------------------</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 251, 1); background-color: rgb(0, 0, 0);"><span style="color: #afd7ff">[swscaler @ 0x7f8413186000] </span>Warning: data is not aligned! This can lead to a speedloss</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ret 320</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:24000 pts_time:1.5625 dts:24000 dts_time:1.5625 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:4096 pts_time:0.266667 dts:4096 dts_time:0.266667 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:27000 pts_time:0.3 dts:27000 dts_time:0.3 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ret 320</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><-------------------- Watermarking frame</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">----------------------------------------</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ret 320</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ORIG: pts:27000 pts_time:1.75781 dts:27000 dts_time:1.75781 duration:3000 duration_time:0.195312 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">STYL: pts:4608 pts_time:0.3 dts:4608 dts_time:0.3 duration:512 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">in: pts:30000 pts_time:0.333333 dts:30000 dts_time:0.333333 duration:3000 duration_time:0.0333333 stream_index:0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">ret 320</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><-------------------- Watermarking frame</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">----------------------------------------</div></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">.../... The rest is OK till the end of process. A mpeg4 movie is generated.</div><div><br></div><div><br></div><div>And finally the way I set the AVFormatContext with mpeg4 init:</div><div>________________________________________________________</div><div><div>VS_VideoContext * get_out_video_ctx(VS_VideoContext *in_ctx, char *filename)</div><div>{</div><div>    AVFormatContext *out_fmt_ctx=NULL;</div><div>    AVOutputFormat *o_format=NULL;</div><div>    AVCodecContext *o_codec_ctx=NULL;</div><div>    AVCodec *o_codec=NULL;</div><div><br></div><div>    AVDictionary    *optionsDict = NULL;//Mandatory way of setting options. Initializes with sws_getcontext()</div><div><br></div><div>    VS_VideoContext *out_video_ctx=(VS_VideoContext *) malloc(sizeof(VS_VideoContext));</div><div><br></div><div>    int i, ret;</div><div>    </div><div>    av_register_all();</div><div>    avcodec_register_all();</div><div><br></div><div>    //Allocating MPEG4 format context</div><div>    avformat_alloc_output_context2(&out_fmt_ctx, NULL, NULL, filename);</div><div>    if (!out_fmt_ctx)</div><div>    {</div><div>        fprintf(stderr, "Could not create output AVFormatContext\n");</div><div>        ret = AVERROR_UNKNOWN;</div><div>        exit(0);</div><div>    }</div><div><br></div><div>    //find MPEG4 output codec</div><div>    o_codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);</div><div>    if (!o_codec)</div><div>    {</div><div>        fprintf(stderr, "Segmenter error: Could not find video decoder, key frames will not be honored.\n");</div><div>    }</div><div>    o_codec_ctx=avcodec_alloc_context3(o_codec);</div><div>    if (!o_codec_ctx)</div><div>    {</div><div>        fprintf(stderr, "Could not allocate output video codec context\n");</div><div>        exit(1);</div><div>    }</div><div><br></div><div>    //codec found, now we param it</div><div>    o_codec_ctx->codec_id=AV_CODEC_ID_MPEG4;</div><div>    o_codec_ctx->bit_rate=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->bit_rate;</div><div>    o_codec_ctx->width=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->width;</div><div>    o_codec_ctx->height=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->height;</div><div>    o_codec_ctx->time_base.num=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->time_base.num;</div><div>    o_codec_ctx->time_base.den=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->time_base.den;//Frames per second</div><div>    o_codec_ctx->gop_size=12;//in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->gop_size;</div><div>    o_codec_ctx->pix_fmt=AV_PIX_FMT_YUV420P;</div><div><br></div><div>    //Now we open it</div><div>    if(avcodec_open2(o_codec_ctx, o_codec, &optionsDict)<0)</div><div>    {</div><div>        INFO(stderr, "Unable to open codec! [%s]\n", o_codec->name);</div><div>        INFO(stderr, "Terminate program.\n");</div><div>        exit(0);</div><div>    }</div><div><br></div><div>    //INFO(stderr, "%s %s %d\n", __FILE__, __func__, __LINE__);</div><div>    INFO(stdout, "Codec for output codec context is %s\n", o_codec->name);</div><div>    INFO(stdout, "Successfully opened %s codec\n", o_codec->name); </div><div><br></div><div>    //Copying  audio streams and codecs from source to destination streams</div><div>    //Affecting MPEG4 foramt context to VIDEO stream</div><div>    for (i = 0; i < in_ctx->nb_streams; i++)</div><div>    {</div><div>        AVStream *in_stream = in_ctx->format_ctx->streams[i];</div><div><br></div><div>        if(in_stream->codec->codec_type==AVMEDIA_TYPE_AUDIO)</div><div>        {</div><div>            AVStream *out_stream = avformat_new_stream(out_fmt_ctx, in_stream->codec->codec);</div><div>            if (!out_stream)</div><div>            {</div><div>                fprintf(stderr, "Failed allocating output stream\n");</div><div>                ret = AVERROR_UNKNOWN;</div><div>                exit(0);</div><div>            }</div><div>            ret = avcodec_copy_context(out_stream->codec, in_stream->codec);</div><div>            if (ret < 0)</div><div>            {</div><div>                fprintf(stderr, "Failed to copy context from input to output stream codec context\n");</div><div>                exit(0);</div><div>            }</div><div>            out_stream->codec->codec_tag = 0;</div><div>            if (out_fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)</div><div>                out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div>        }</div><div><br></div><div>        if(in_stream->codec->codec_type==AVMEDIA_TYPE_VIDEO)</div><div>        {</div><div>            AVStream *out_stream = avformat_new_stream(out_fmt_ctx, o_codec_ctx->codec);</div><div>            if (!out_stream)</div><div>            {</div><div>                fprintf(stderr, "Failed allocating output stream\n");</div><div>                ret = AVERROR_UNKNOWN;</div><div>                exit(0);</div><div>            }</div><div>            out_stream->codec->codec_tag = 0;</div><div>            if (out_fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)</div><div>            {</div><div>                out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div>            }          </div><div>        }</div><div>    }</div><div><br></div><div>    //Setting AVOutputFormat of AVFormatContext</div><div>    o_format=out_fmt_ctx->oformat;</div><div>    //Setting codec context for out stream</div><div>    out_fmt_ctx->streams[in_ctx->video_stream_idx]->codec=o_codec_ctx;</div><div><br></div><div>    //Open file to write if not already opened</div><div>    if(!(o_format->flags&AVFMT_NOFILE))</div><div>    {</div><div>        ret=avio_open(&out_fmt_ctx->pb, filename, AVIO_FLAG_WRITE);</div><div>        if(ret<0)</div><div>        {</div><div>            INFO(stderr, "Unable to open output filename. Abort program.\n");</div><div>            vs_free(in_ctx);</div><div>            exit(0);</div><div>        }</div><div>    }</div><div><br></div><div>    //write header for output file</div><div>    ret=avformat_write_header(out_fmt_ctx, NULL);</div><div>    if(ret<0)</div><div>    {</div><div>        INFO(stderr, "Unable to write header for output file. Abort program.\n");</div><div>        vs_free(in_ctx);</div><div>        exit(0);</div><div>    }</div><div><br></div><div>    //This is just a straight encapsulation of some libav stuff</div><div>    out_video_ctx->format_ctx=out_fmt_ctx;</div><div>    out_video_ctx->filename=filename;</div><div>    out_video_ctx->codec_name=(char *) out_fmt_ctx->streams[in_ctx->video_stream_idx]->codec->codec->long_name;</div><div>    out_video_ctx->video_stream_idx=in_ctx->video_stream_idx;</div><div>    out_video_ctx->audio_stream_idx=in_ctx->audio_stream_idx;</div><div>    out_video_ctx->picture_width=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->width;</div><div>    out_video_ctx->picture_height=in_ctx->format_ctx->streams[in_ctx->video_stream_idx]->codec->height;</div><div>    out_video_ctx->nb_streams=in_ctx->nb_streams;</div><div><br></div><div>    av_dump_format(out_fmt_ctx, 0, filename, 1);</div><div>    INFO(stdout, "----------------------------------------");</div><div><br></div><div>    return out_video_ctx;</div><div>}</div></div><div><br></div><div>Thanks!</div><div><br></div></body></html>