Credit Andrew - updating patches for FFmpeg 7.0 as needed since 6.1, now at 7.0,...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / ffmpeg.C
index 44bbe2fa0e423464058ecebefd75700046e8410b..9b8832dd718c94dd8b815edc7eb5c9d732953dd6 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * CINELERRA
+ * Copyright (C) 2012-2014 Paolo Rampino
+ *
+ * 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
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 
 #include <stdio.h>
 #include <stdint.h>
 
 #include <stdio.h>
 #include <stdint.h>
@@ -380,8 +399,11 @@ AVHWDeviceType FFStream::decode_hw_activate()
 {
        return AV_HWDEVICE_TYPE_NONE;
 }
 {
        return AV_HWDEVICE_TYPE_NONE;
 }
-
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+int FFStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type)
+#else
 int FFStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
 int FFStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
+#endif
 {
        return 0;
 }
 {
        return 0;
 }
@@ -406,7 +428,11 @@ int FFStream::decode_activate()
                }
                while( ret >= 0 && st != 0 && !reading ) {
                        AVCodecID codec_id = st->codecpar->codec_id;
                }
                while( ret >= 0 && st != 0 && !reading ) {
                        AVCodecID codec_id = st->codecpar->codec_id;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+                       const AVCodec *decoder = 0;
+#else
                        AVCodec *decoder = 0;
                        AVCodec *decoder = 0;
+#endif
                        if( is_video() ) {
                                if( ffmpeg->opt_video_decoder )
                                        decoder = avcodec_find_decoder_by_name(ffmpeg->opt_video_decoder);
                        if( is_video() ) {
                                if( ffmpeg->opt_video_decoder )
                                        decoder = avcodec_find_decoder_by_name(ffmpeg->opt_video_decoder);
@@ -578,6 +604,10 @@ int FFStream::write_packet(FFPacket &pkt)
                ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt);
        }
        else {
                ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt);
        }
        else {
+       bsfc->time_base_in = st->time_base;
+       avcodec_parameters_copy(bsfc->par_in, st->codecpar);
+       av_bsf_init(bsfc);
+       
                ret = av_bsf_send_packet(bsfc, pkt);
                while( ret >= 0 ) {
                        FFPacket bs;
                ret = av_bsf_send_packet(bsfc, pkt);
                while( ret >= 0 ) {
                        FFPacket bs;
@@ -586,6 +616,9 @@ int FFStream::write_packet(FFPacket &pkt)
                                if( ret == AVERROR_EOF ) return -1;
                                break;
                        }
                                if( ret == AVERROR_EOF ) return -1;
                                break;
                        }
+                       //printf(" filter name %s \n", bsfc->filter[0].name);
+                       //avcodec_parameters_copy(ffmpeg->fmt_ctx->streams[0]->codecpar, bsfc->par_out);
+                       //avcodec_parameters_copy(st->codecpar, bsfc->par_out);
                        av_packet_rescale_ts(bs, avctx->time_base, st->time_base);
                        bs->stream_index = st->index;
                        ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, bs);
                        av_packet_rescale_ts(bs, avctx->time_base, st->time_base);
                        bs->stream_index = st->index;
                        ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, bs);
@@ -608,8 +641,8 @@ int FFStream::encode_frame(AVFrame *frame)
                if ( ret == AVERROR(EAGAIN) && !frame ) continue;
                FFPacket opkt;
                ret = avcodec_receive_packet(avctx, opkt);
                if ( ret == AVERROR(EAGAIN) && !frame ) continue;
                FFPacket opkt;
                ret = avcodec_receive_packet(avctx, opkt);
-               if( !frame && ret == AVERROR_EOF ) return pkts;
-               if( ret < 0 ) break;
+               if( !frame && (ret == AVERROR_EOF || ret == AVERROR(EAGAIN) )) return pkts;
+               //if( ret < 0 ) break;
                ret = write_packet(opkt);
                if( ret < 0 ) break;
                ++pkts;
                ret = write_packet(opkt);
                if( ret < 0 ) break;
                ++pkts;
@@ -722,10 +755,19 @@ int FFStream::seek(int64_t no, double rate)
        double secs = pos < 0 ? 0. : pos / rate;
        AVRational time_base = st->time_base;
        int64_t tstmp = time_base.num > 0 ? secs * time_base.den/time_base.num : 0;
        double secs = pos < 0 ? 0. : pos / rate;
        AVRational time_base = st->time_base;
        int64_t tstmp = time_base.num > 0 ? secs * time_base.den/time_base.num : 0;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+       int nb_index_entries = avformat_index_get_entries_count(st);
+#endif
        if( !tstmp ) {
        if( !tstmp ) {
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+               if( nb_index_entries > 0 ) tstmp = (avformat_index_get_entry(st, 0))->timestamp;
+#else
                if( st->nb_index_entries > 0 ) tstmp = st->index_entries[0].timestamp;
                if( st->nb_index_entries > 0 ) tstmp = st->index_entries[0].timestamp;
+#endif
                else if( st->start_time != AV_NOPTS_VALUE ) tstmp = st->start_time;
                else if( st->start_time != AV_NOPTS_VALUE ) tstmp = st->start_time;
+#if LIBAVCODEC_VERSION_INT  <= AV_VERSION_INT(58,134,100)
                else if( st->first_dts != AV_NOPTS_VALUE ) tstmp = st->first_dts;
                else if( st->first_dts != AV_NOPTS_VALUE ) tstmp = st->first_dts;
+#endif
                else tstmp = INT64_MIN+1;
        }
        else if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
                else tstmp = INT64_MIN+1;
        }
        else if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
@@ -838,13 +880,17 @@ void FFAudioStream::init_swr(int ichs, int ifmt, int irate)
        swr_ichs = ichs;  swr_ifmt = ifmt;  swr_irate = irate;
        if( ichs == channels && ifmt == AV_SAMPLE_FMT_FLT && irate == sample_rate )
                return;
        swr_ichs = ichs;  swr_ifmt = ifmt;  swr_irate = irate;
        if( ichs == channels && ifmt == AV_SAMPLE_FMT_FLT && irate == sample_rate )
                return;
-       uint64_t ilayout = av_get_default_channel_layout(ichs);
-       if( !ilayout ) ilayout = ((uint64_t)1<<ichs) - 1;
-       uint64_t olayout = av_get_default_channel_layout(channels);
-       if( !olayout ) olayout = ((uint64_t)1<<channels) - 1;
-       resample_context = swr_alloc_set_opts(NULL,
-               olayout, AV_SAMPLE_FMT_FLT, sample_rate,
-               ilayout, (AVSampleFormat)ifmt, irate,
+       //uint64_t ilayout = av_get_default_channel_layout(ichs);
+       AVChannelLayout ilayout, olayout;
+       av_channel_layout_default(&ilayout, ichs);
+       //if( !ilayout ) ilayout = ((uint64_t)1<<ichs) - 1;
+       //uint64_t olayout = av_get_default_channel_layout(channels);
+       av_channel_layout_default(&olayout, channels);
+       //if( !olayout ) olayout = ((uint64_t)1<<channels) - 1;
+       
+       swr_alloc_set_opts2(&resample_context,
+               &olayout, AV_SAMPLE_FMT_FLT, sample_rate,
+               &ilayout, (AVSampleFormat)ifmt, irate,
                0, NULL);
        if( resample_context )
                swr_init(resample_context);
                0, NULL);
        if( resample_context )
                swr_init(resample_context);
@@ -921,7 +967,11 @@ int FFAudioStream::encode_activate()
 
 int64_t FFAudioStream::load_buffer(double ** const sp, int len)
 {
 
 int64_t FFAudioStream::load_buffer(double ** const sp, int len)
 {
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61,3,100)
+       reserve(len+1, st->codecpar->ch_layout.nb_channels);
+#else
        reserve(len+1, st->codecpar->channels);
        reserve(len+1, st->codecpar->channels);
+#endif
        for( int ch=0; ch<nch; ++ch )
                write(sp[ch], len, ch);
        return put_inp(len);
        for( int ch=0; ch<nch; ++ch )
                write(sp[ch], len, ch);
        return put_inp(len);
@@ -941,7 +991,12 @@ int FFAudioStream::init_frame(AVFrame *frame)
 {
        frame->nb_samples = frame_sz;
        frame->format = avctx->sample_fmt;
 {
        frame->nb_samples = frame_sz;
        frame->format = avctx->sample_fmt;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61,3,100)
+       frame->ch_layout.u.mask = avctx->ch_layout.u.mask;
+       av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
+#else
        frame->channel_layout = avctx->channel_layout;
        frame->channel_layout = avctx->channel_layout;
+#endif
        frame->sample_rate = avctx->sample_rate;
        int ret = av_frame_get_buffer(frame, 0);
        if (ret < 0)
        frame->sample_rate = avctx->sample_rate;
        int ret = av_frame_get_buffer(frame, 0);
        if (ret < 0)
@@ -962,7 +1017,7 @@ int FFAudioStream::load(int64_t pos, int len)
        while( ret>=0 && !flushed && curr_pos<end_pos && --i>=0 ) {
                ret = read_frame(frame);
                if( ret > 0 && frame->nb_samples > 0 ) {
        while( ret>=0 && !flushed && curr_pos<end_pos && --i>=0 ) {
                ret = read_frame(frame);
                if( ret > 0 && frame->nb_samples > 0 ) {
-                       init_swr(frame->channels, frame->format, frame->sample_rate);
+                       init_swr(frame->ch_layout.nb_channels, frame->format, frame->sample_rate);
                        load_history(&frame->extended_data[0], frame->nb_samples);
                        curr_pos += frame->nb_samples;
                }
                        load_history(&frame->extended_data[0], frame->nb_samples);
                        curr_pos += frame->nb_samples;
                }
@@ -1093,8 +1148,11 @@ AVHWDeviceType FFVideoStream::decode_hw_activate()
        }
        return type;
 }
        }
        return type;
 }
-
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+int FFVideoStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type)
+#else
 int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
 int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
+#endif
 {
        int ret = 0;
        hw_pix_fmt = AV_PIX_FMT_NONE;
 {
        int ret = 0;
        hw_pix_fmt = AV_PIX_FMT_NONE;
@@ -1115,7 +1173,14 @@ int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
        if( hw_pix_fmt >= 0 ) {
                hw_pixfmt = hw_pix_fmt;
                avctx->get_format  = get_hw_format;
        if( hw_pix_fmt >= 0 ) {
                hw_pixfmt = hw_pix_fmt;
                avctx->get_format  = get_hw_format;
+               const char *drm_node = getenv("CIN_DRM_DEC");
+               if(drm_node && type==AV_HWDEVICE_TYPE_VAAPI) {
+                   ret = av_hwdevice_ctx_create(&hw_device_ctx, type, drm_node, 0, 0);
+               }
+               else {
                ret = av_hwdevice_ctx_create(&hw_device_ctx, type, 0, 0, 0);
                ret = av_hwdevice_ctx_create(&hw_device_ctx, type, 0, 0, 0);
+               }
+
                if( ret >= 0 ) {
                        avctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
                        ret = 1;
                if( ret >= 0 ) {
                        avctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
                        ret = 1;
@@ -1131,6 +1196,7 @@ int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
 
 AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev)
 {
 
 AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev)
 {
+       const char *drm_node_enc = getenv("CIN_DRM_ENC");
        AVBufferRef *hw_device_ctx = 0;
        AVBufferRef *hw_frames_ref = 0;
        AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
        AVBufferRef *hw_device_ctx = 0;
        AVBufferRef *hw_frames_ref = 0;
        AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
@@ -1142,7 +1208,12 @@ AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev)
                }
        }
        if( type != AV_HWDEVICE_TYPE_NONE ) {
                }
        }
        if( type != AV_HWDEVICE_TYPE_NONE ) {
-               int ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, 0, 0, 0);
+               int ret = 0;
+               if (drm_node_enc) {
+               ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, drm_node_enc, 0, 0);
+               } else {
+               ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, 0, 0, 0);
+               }
                if( ret < 0 ) {
                        ff_err(ret, "Failed to create a HW device.\n");
                        type = AV_HWDEVICE_TYPE_NONE;
                if( ret < 0 ) {
                        ff_err(ret, "Failed to create a HW device.\n");
                        type = AV_HWDEVICE_TYPE_NONE;
@@ -1246,6 +1317,7 @@ int FFVideoStream::probe(int64_t pos)
                ret = -1;
 
        ret = ret > 0 ? 1 : ret < 0 ? -1 : 0;
                ret = -1;
 
        ret = ret > 0 ? 1 : ret < 0 ? -1 : 0;
+       av_frame_free(&frame);
        return ret;
 }
 
        return ret;
 }
 
@@ -1849,7 +1921,11 @@ FFMPEG::~FFMPEG()
        delete [] opt_hw_dev;
 }
 
        delete [] opt_hw_dev;
 }
 
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+int FFMPEG::check_sample_rate(const AVCodec *codec, int sample_rate)
+#else
 int FFMPEG::check_sample_rate(AVCodec *codec, int sample_rate)
 int FFMPEG::check_sample_rate(AVCodec *codec, int sample_rate)
+#endif
 {
        const int *p = codec->supported_samplerates;
        if( !p ) return sample_rate;
 {
        const int *p = codec->supported_samplerates;
        if( !p ) return sample_rate;
@@ -2099,7 +2175,11 @@ void FFMPEG::scan_audio_options(Asset *asset, EDL *edl)
                cin_fmt = (int)av_get_sample_fmt(cin_sample_fmt);
        if( cin_fmt < 0 ) {
                char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
                cin_fmt = (int)av_get_sample_fmt(cin_sample_fmt);
        if( cin_fmt < 0 ) {
                char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
+               const AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ?
+#else
                AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ?
                AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ?
+#endif
                        avcodec_find_encoder_by_name(audio_codec) : 0;
                if( av_codec && av_codec->sample_fmts )
                        cin_fmt = find_best_sample_fmt_of_list(av_codec->sample_fmts, AV_SAMPLE_FMT_FLT);
                        avcodec_find_encoder_by_name(audio_codec) : 0;
                if( av_codec && av_codec->sample_fmts )
                        cin_fmt = find_best_sample_fmt_of_list(av_codec->sample_fmts, AV_SAMPLE_FMT_FLT);
@@ -2135,7 +2215,11 @@ void FFMPEG::scan_video_options(Asset *asset, EDL *edl)
                        cin_fmt = (int)av_get_pix_fmt(cin_pix_fmt);
        if( cin_fmt < 0 ) {
                char video_codec[BCSTRLEN];  video_codec[0] = 0;
                        cin_fmt = (int)av_get_pix_fmt(cin_pix_fmt);
        if( cin_fmt < 0 ) {
                char video_codec[BCSTRLEN];  video_codec[0] = 0;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
+               const AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ?
+#else
                AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ?
                AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ?
+#endif
                        avcodec_find_encoder_by_name(video_codec) : 0;
                if( av_codec && av_codec->pix_fmts ) {
                        if( 0 && edl ) { // frequently picks a bad answer
                        avcodec_find_encoder_by_name(video_codec) : 0;
                if( av_codec && av_codec->pix_fmts ) {
                        if( 0 && edl ) { // frequently picks a bad answer
@@ -2329,10 +2413,15 @@ int FFCodecRemaps::add(const char *val)
        return 0;
 }
 
        return 0;
 }
 
-
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+int FFCodecRemaps::update(AVCodecID &codec_id, const AVCodec *&decoder)
+{
+       const AVCodec *codec = avcodec_find_decoder(codec_id);
+#else
 int FFCodecRemaps::update(AVCodecID &codec_id, AVCodec *&decoder)
 {
        AVCodec *codec = avcodec_find_decoder(codec_id);
 int FFCodecRemaps::update(AVCodecID &codec_id, AVCodec *&decoder)
 {
        AVCodec *codec = avcodec_find_decoder(codec_id);
+#endif
        if( !codec ) return -1;
        const char *name = codec->name;
        FFCodecRemaps &map = *this;
        if( !codec ) return -1;
        const char *name = codec->name;
        FFCodecRemaps &map = *this;
@@ -2468,6 +2557,34 @@ int FFMPEG::info(char *text, int len)
                enum AVColorRange range = st->codecpar->color_range;
                const char *rg = av_color_range_name(range);
                report("/ range:%s\n", rg ? rg : unkn);
                enum AVColorRange range = st->codecpar->color_range;
                const char *rg = av_color_range_name(range);
                report("/ range:%s\n", rg ? rg : unkn);
+
+               AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, st, NULL);
+               AVRational display_aspect_ratio;
+               if(sar.num) {
+               
+               av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
+                  st->codecpar->width  * (int64_t)sar.num,
+                  st->codecpar->height * (int64_t)sar.den,
+                  1024 * 1024);
+/*             report("  Guessed SAR: %d:%d, ", sar.num, sar.den );
+               report("DAR: %d:%d \n", display_aspect_ratio.num, display_aspect_ratio.den); */
+               }
+               if (st->sample_aspect_ratio.num)
+               {
+               av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
+                  st->codecpar->width  * (int64_t)st->sample_aspect_ratio.num,
+                  st->codecpar->height * (int64_t)st->sample_aspect_ratio.den,
+                  1024 * 1024);
+               report("  container Detected SAR: %d:%d , DAR %d:%d \n", st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, display_aspect_ratio.num, display_aspect_ratio.den);
+               }
+               if (st->codecpar->sample_aspect_ratio.num)
+               {
+               av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
+                  st->codecpar->width  * (int64_t)st->codecpar->sample_aspect_ratio.num,
+                  st->codecpar->height * (int64_t)st->codecpar->sample_aspect_ratio.den,
+                  1024 * 1024);
+               report("  codec Detected SAR: %d:%d , DAR %d:%d \n", st->codecpar->sample_aspect_ratio.num, st->codecpar->sample_aspect_ratio.den, display_aspect_ratio.num, display_aspect_ratio.den);
+               }
                double secs = to_secs(st->duration, st->time_base);
                int64_t length = secs * vid->frame_rate + 0.5;
                double ofs = to_secs((vid->nudge - st->start_time), st->time_base);
                double secs = to_secs(st->duration, st->time_base);
                int64_t length = secs * vid->frame_rate + 0.5;
                double ofs = to_secs((vid->nudge - st->start_time), st->time_base);
@@ -2552,7 +2669,11 @@ int FFMPEG::init_decoder(const char *filename)
        char *sp = strrchr(bp, '.');
        if( !sp ) sp = bp + strlen(bp);
        FILE *fp = 0;
        char *sp = strrchr(bp, '.');
        if( !sp ) sp = bp + strlen(bp);
        FILE *fp = 0;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
+       const AVInputFormat *ifmt = 0;
+#else
        AVInputFormat *ifmt = 0;
        AVInputFormat *ifmt = 0;
+#endif
        if( sp ) {
                strcpy(sp, ".opts");
                fp = fopen(file_opts, "r");
        if( sp ) {
                strcpy(sp, ".opts");
                fp = fopen(file_opts, "r");
@@ -2672,14 +2793,14 @@ int FFMPEG::open_decoder()
                        ret = vid->create_filter(opt_video_filter);
                        break; }
                case AVMEDIA_TYPE_AUDIO: {
                        ret = vid->create_filter(opt_video_filter);
                        break; }
                case AVMEDIA_TYPE_AUDIO: {
-                       if( avpar->channels < 1 ) continue;
+                       if( avpar->ch_layout.nb_channels < 1 ) continue;
                        if( avpar->sample_rate < 1 ) continue;
                        has_audio = 1;
                        int aidx = ffaudio.size();
                        FFAudioStream *aud = new FFAudioStream(this, st, aidx, i);
                        ffaudio.append(aud);
                        aud->channel0 = astrm_index.size();
                        if( avpar->sample_rate < 1 ) continue;
                        has_audio = 1;
                        int aidx = ffaudio.size();
                        FFAudioStream *aud = new FFAudioStream(this, st, aidx, i);
                        ffaudio.append(aud);
                        aud->channel0 = astrm_index.size();
-                       aud->channels = avpar->channels;
+                       aud->channels = avpar->ch_layout.nb_channels;
                        for( int ch=0; ch<aud->channels; ++ch )
                                astrm_index.append(ffidx(aidx, ch));
                        aud->sample_rate = avpar->sample_rate;
                        for( int ch=0; ch<aud->channels; ++ch )
                                astrm_index.append(ffidx(aidx, ch));
                        aud->sample_rate = avpar->sample_rate;
@@ -2773,7 +2894,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec)
        AVCodecContext *ctx = 0;
 
        const AVCodecDescriptor *codec_desc = 0;
        AVCodecContext *ctx = 0;
 
        const AVCodecDescriptor *codec_desc = 0;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+       const AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
+#else
        AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
        AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
+#endif
        if( !codec ) {
                eprintf(_("cant find codec %s:%s\n"), codec_name, filename);
                ret = 1;
        if( !codec ) {
                eprintf(_("cant find codec %s:%s\n"), codec_name, filename);
                ret = 1;
@@ -2831,10 +2956,13 @@ int FFMPEG::open_encoder(const char *type, const char *spec)
                        FFAudioStream *aud = new FFAudioStream(this, st, aidx, fidx);
                        aud->avctx = ctx;  ffaudio.append(aud);  fst = aud;
                        aud->sample_rate = asset->sample_rate;
                        FFAudioStream *aud = new FFAudioStream(this, st, aidx, fidx);
                        aud->avctx = ctx;  ffaudio.append(aud);  fst = aud;
                        aud->sample_rate = asset->sample_rate;
-                       ctx->channels = aud->channels = asset->channels;
+                       ctx->ch_layout.nb_channels = aud->channels = asset->channels;
                        for( int ch=0; ch<aud->channels; ++ch )
                                astrm_index.append(ffidx(aidx, ch));
                        for( int ch=0; ch<aud->channels; ++ch )
                                astrm_index.append(ffidx(aidx, ch));
-                       ctx->channel_layout =  av_get_default_channel_layout(ctx->channels);
+                       AVChannelLayout ch_layout;
+                       av_channel_layout_default(&ch_layout, ctx->ch_layout.nb_channels);
+                       ctx->ch_layout.u.mask =  ch_layout.u.mask;
+                       av_channel_layout_copy(&ctx->ch_layout, &ch_layout);
                        ctx->sample_rate = check_sample_rate(codec, asset->sample_rate);
                        if( !ctx->sample_rate ) {
                                eprintf(_("check_sample_rate failed %s\n"), filename);
                        ctx->sample_rate = check_sample_rate(codec, asset->sample_rate);
                        if( !ctx->sample_rate ) {
                                eprintf(_("check_sample_rate failed %s\n"), filename);
@@ -2846,10 +2974,12 @@ int FFMPEG::open_encoder(const char *type, const char *spec)
                        if( sample_fmt == AV_SAMPLE_FMT_NONE )
                                sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16;
                        ctx->sample_fmt = sample_fmt;
                        if( sample_fmt == AV_SAMPLE_FMT_NONE )
                                sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16;
                        ctx->sample_fmt = sample_fmt;
-                       uint64_t layout = av_get_default_channel_layout(ctx->channels);
-                       aud->resample_context = swr_alloc_set_opts(NULL,
-                               layout, ctx->sample_fmt, aud->sample_rate,
-                               layout, AV_SAMPLE_FMT_FLT, ctx->sample_rate,
+                       //uint64_t layout = av_get_default_channel_layout(ctx->ch_layout.nb_channels);
+                       AVChannelLayout layout;
+                       av_channel_layout_default(&layout, ctx->ch_layout.nb_channels);
+                       swr_alloc_set_opts2(&aud->resample_context,
+                               &layout, ctx->sample_fmt, aud->sample_rate,
+                               &layout, AV_SAMPLE_FMT_FLT, ctx->sample_rate,
                                0, NULL);
                        swr_init(aud->resample_context);
                        aud->writing = -1;
                                0, NULL);
                        swr_init(aud->resample_context);
                        aud->writing = -1;
@@ -2894,6 +3024,14 @@ int FFMPEG::open_encoder(const char *type, const char *spec)
                        vid->width = asset->width;
                        vid->height = asset->height;
                        vid->frame_rate = asset->frame_rate;
                        vid->width = asset->width;
                        vid->height = asset->height;
                        vid->frame_rate = asset->frame_rate;
+#if 0
+                       char tc_str[20] = "00:00:00:00";
+                       double tc_offset;
+                       if(asset->timecode > 0)
+                       Units::totext(tc_str, asset->timecode, TIME_HMSF, 0, asset->frame_rate, 0);
+                       //printf("tc: %s \n", tc_str);
+                       av_dict_set(&st->metadata, "timecode", tc_str, 0);
+#endif
                        if( (vid->color_range = asset->ff_color_range) < 0 )
                                vid->color_range = file_base->file->preferences->yuv_color_range;
                        switch( vid->color_range ) {
                        if( (vid->color_range = asset->ff_color_range) < 0 )
                                vid->color_range = file_base->file->preferences->yuv_color_range;
                        switch( vid->color_range ) {
@@ -2945,6 +3083,13 @@ int FFMPEG::open_encoder(const char *type, const char *spec)
                                frame_rate.num, frame_rate.den, INT_MAX);
                        ctx->framerate = (AVRational) { frame_rate.num, frame_rate.den };
                        ctx->time_base = (AVRational) { frame_rate.den, frame_rate.num };
                                frame_rate.num, frame_rate.den, INT_MAX);
                        ctx->framerate = (AVRational) { frame_rate.num, frame_rate.den };
                        ctx->time_base = (AVRational) { frame_rate.den, frame_rate.num };
+                       if(!strcmp(format_name, "webm") || !strcmp(format_name, "matroska") || !strcmp(format_name, "mov") ||
+                       !strcmp(format_name, "qt") || !strcmp(format_name, "mp4") || !strcmp(format_name, "avi") ||
+                       !strcmp(format_name, "dv") || !strcmp(format_name, "yuv4mpegpipe"))
+                       {
+                       if (to_sample_aspect_ratio(asset).den > 0)
+                       st->sample_aspect_ratio = to_sample_aspect_ratio(asset);
+                       }
                        st->avg_frame_rate = frame_rate;
                        st->time_base = ctx->time_base;
                        vid->writing = -1;
                        st->avg_frame_rate = frame_rate;
                        st->time_base = ctx->time_base;
                        vid->writing = -1;
@@ -3026,7 +3171,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec)
                }
                if( ret >= 0 ) {
 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
                }
                if( ret >= 0 ) {
 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
+#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
                        ret = avcodec_copy_context(st->codec, ctx);
                        ret = avcodec_copy_context(st->codec, ctx);
+#else
+                       ret = avcodec_parameters_to_context(ctx, st->codecpar);
+#endif
 _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"")
                        if( ret < 0 )
                                fprintf(stderr, "Could not copy the stream context\n");
 _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"")
                        if( ret < 0 )
                                fprintf(stderr, "Could not copy the stream context\n");
@@ -3582,7 +3731,7 @@ float FFMPEG::ff_aspect_ratio(int stream)
        AVCodecParameters *par = ffvideo[stream]->st->codecpar;
        AVRational dar;
        AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, strm, NULL);
        AVCodecParameters *par = ffvideo[stream]->st->codecpar;
        AVRational dar;
        AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, strm, NULL);
-        if (sar.num) {
+        if (sar.num && ffvideo[stream]->get_rotation_angle() == 0) {
             av_reduce(&dar.num, &dar.den,
                       par->width  * sar.num,
                       par->height * sar.den,
             av_reduce(&dar.num, &dar.den,
                       par->width  * sar.num,
                       par->height * sar.den,
@@ -3671,7 +3820,11 @@ Preferences *FFMPEG::ff_prefs()
 
 double FFVideoStream::get_rotation_angle()
 {
 
 double FFVideoStream::get_rotation_angle()
 {
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+       size_t size = 0;
+#else
        int size = 0;
        int size = 0;
+#endif
        int *matrix = (int*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, &size);
        int len = size/sizeof(*matrix);
        if( !matrix || len < 5 ) return 0;
        int *matrix = (int*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, &size);
        int len = size/sizeof(*matrix);
        if( !matrix || len < 5 ) return 0;
@@ -3794,7 +3947,7 @@ int FFAudioStream::create_filter(const char *filter_spec)
        snprintf(args, sizeof(args),
                "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx",
                st->time_base.num, st->time_base.den, avpar->sample_rate,
        snprintf(args, sizeof(args),
                "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx",
                st->time_base.num, st->time_base.den, avpar->sample_rate,
-               av_get_sample_fmt_name(sample_fmt), avpar->channel_layout);
+               av_get_sample_fmt_name(sample_fmt), avpar->ch_layout.u.mask);
        if( ret >= 0 ) {
                filt_ctx = 0;
                ret = insert_filter("abuffer", args, "in");
        if( ret >= 0 ) {
                filt_ctx = 0;
                ret = insert_filter("abuffer", args, "in");
@@ -3812,8 +3965,8 @@ int FFAudioStream::create_filter(const char *filter_spec)
                        AV_OPT_SEARCH_CHILDREN);
        if( ret >= 0 )
                ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
                        AV_OPT_SEARCH_CHILDREN);
        if( ret >= 0 )
                ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
-                       (uint8_t*)&avpar->channel_layout,
-                       sizeof(avpar->channel_layout), AV_OPT_SEARCH_CHILDREN);
+                       (uint8_t*)&avpar->ch_layout.u.mask,
+                       sizeof(avpar->ch_layout.u.mask), AV_OPT_SEARCH_CHILDREN);
        if( ret >= 0 )
                ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
                        (uint8_t*)&sample_rate, sizeof(sample_rate),
        if( ret >= 0 )
                ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
                        (uint8_t*)&sample_rate, sizeof(sample_rate),
@@ -3896,7 +4049,11 @@ AVCodecContext *FFMPEG::activate_decoder(AVStream *st)
        AVDictionary *copts = 0;
        av_dict_copy(&copts, opts, 0);
        AVCodecID codec_id = st->codecpar->codec_id;
        AVDictionary *copts = 0;
        av_dict_copy(&copts, opts, 0);
        AVCodecID codec_id = st->codecpar->codec_id;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+       const AVCodec *decoder = 0;
+#else
        AVCodec *decoder = 0;
        AVCodec *decoder = 0;
+#endif
        switch( st->codecpar->codec_type ) {
        case AVMEDIA_TYPE_VIDEO:
                if( opt_video_decoder )
        switch( st->codecpar->codec_type ) {
        case AVMEDIA_TYPE_VIDEO:
                if( opt_video_decoder )
@@ -4061,7 +4218,7 @@ printf("audio%d pad %jd %jd (%jd)\n", aud->idx, pos, aud->curr_pos, pos-aud->cur
                        }
                        while( (ret=aud->decode_frame(frame)) > 0 ) {
                                //if( frame->channels != nch ) break;
                        }
                        while( (ret=aud->decode_frame(frame)) > 0 ) {
                                //if( frame->channels != nch ) break;
-                               aud->init_swr(frame->channels, frame->format, frame->sample_rate);
+                               aud->init_swr(frame->ch_layout.nb_channels, frame->format, frame->sample_rate);
                                float *samples;
                                int len = aud->get_samples(samples,
                                         &frame->extended_data[0], frame->nb_samples);
                                float *samples;
                                int len = aud->get_samples(samples,
                                         &frame->extended_data[0], frame->nb_samples);
@@ -4088,10 +4245,20 @@ void FFStream::load_markers(IndexMarks &marks, double rate)
        int in = 0;
        int64_t sz = marks.size();
        int max_entries = fmt_ctx->max_index_size / sizeof(AVIndexEntry) - 1;
        int in = 0;
        int64_t sz = marks.size();
        int max_entries = fmt_ctx->max_index_size / sizeof(AVIndexEntry) - 1;
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+       int nb_ent = avformat_index_get_entries_count(st);
+#endif
+#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
        int nb_ent = st->nb_index_entries;
        int nb_ent = st->nb_index_entries;
+#endif
 // some formats already have an index
        if( nb_ent > 0 ) {
 // some formats already have an index
        if( nb_ent > 0 ) {
+#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
                AVIndexEntry *ep = &st->index_entries[nb_ent-1];
                AVIndexEntry *ep = &st->index_entries[nb_ent-1];
+#endif
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
+               const AVIndexEntry *ep = avformat_index_get_entry(st, nb_ent-1);
+#endif
                int64_t tstmp = ep->timestamp;
                if( nudge != AV_NOPTS_VALUE ) tstmp -= nudge;
                double secs = ffmpeg->to_secs(tstmp, st->time_base);
                int64_t tstmp = ep->timestamp;
                if( nudge != AV_NOPTS_VALUE ) tstmp -= nudge;
                double secs = ffmpeg->to_secs(tstmp, st->time_base);