Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / ffmpeg.C
index 2d441a79f6c2bcb244362d024305140cb61c6723..6685f468b205255061c670412dc2108135bf5b83 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>
@@ -585,6 +604,10 @@ int FFStream::write_packet(FFPacket &pkt)
                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;
@@ -593,6 +616,9 @@ int FFStream::write_packet(FFPacket &pkt)
                                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);
@@ -615,8 +641,8 @@ int FFStream::encode_frame(AVFrame *frame)
                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;
@@ -1134,7 +1160,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;
+               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);
+               }
+
                if( ret >= 0 ) {
                        avctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
                        ret = 1;
@@ -1150,6 +1183,7 @@ int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
 
 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;
@@ -1161,7 +1195,12 @@ AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev)
                }
        }
        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;
@@ -2505,6 +2544,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);
+
+               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);
@@ -2939,6 +3006,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;
+#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 ) {
@@ -2990,6 +3065,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 };
+                       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;
@@ -3631,7 +3713,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);
-        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,