[Cin] slightly better interlace/aspect ratio autodetect patch

Andrew Randrianasulu randrianasulu at gmail.com
Sat Nov 14 07:28:13 CET 2020


This one sets things correctly (?) for Terje's HDV file, but interlace auto-setting still not setting _project's_ interlace  mode

It still prints a lot of debug lines


diff --git a/cinelerra-5.1/cinelerra/ffmpeg.C b/cinelerra-5.1/cinelerra/ffmpeg.C
index 97b6698a..9eabe9bd 100644
--- a/cinelerra-5.1/cinelerra/ffmpeg.C
+++ b/cinelerra-5.1/cinelerra/ffmpeg.C
@@ -3468,7 +3468,22 @@ int FFMPEG::ff_coded_height(int stream)
 
 float FFMPEG::ff_aspect_ratio(int stream)
 {
-       return ffvideo[stream]->aspect_ratio;
+       //return ffvideo[stream]->aspect_ratio;
+       AVFormatContext *fmt_ctx = ffvideo[stream]->fmt_ctx;
+       AVStream *strm = ffvideo[stream]->st;
+       AVCodecParameters *par = ffvideo[stream]->st->codecpar;
+       AVRational dar;
+       AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, strm, NULL);
+        if (sar.num) {
+            printf("sample_aspect_ratio, %f \n", av_q2d(sar));
+            av_reduce(&dar.num, &dar.den,
+                      par->width  * sar.num,
+                      par->height * sar.den,
+                      1024*1024);
+                      printf("display_aspect_ratio, %f \n", av_q2d(dar));
+                      return av_q2d(dar);
+                      }
+        return ffvideo[stream]->aspect_ratio;
 }
 
 const char* FFMPEG::ff_video_codec(int stream)
@@ -3509,6 +3524,25 @@ int FFMPEG::ff_video_mpeg_color_range(int stream)
        return ffvideo[stream]->st->codecpar->color_range == AVCOL_RANGE_MPEG ? 1 : 0;
 }
 
+int FFMPEG::ff_interlace(int stream)
+{
+       int interlace = ffvideo[stream]->st->codecpar->field_order;
+       printf("interlace: %i\n", interlace);
+       switch (interlace)
+       {
+       case AV_FIELD_TT:
+           return ILACE_MODE_TOP_FIRST;
+       case AV_FIELD_BB:
+           return ILACE_MODE_BOTTOM_FIRST;
+       case AV_FIELD_PROGRESSIVE:
+           return ILACE_MODE_NOTINTERLACED;
+       default:
+           return ILACE_MODE_UNDETECTED;
+       }
+}
+
+
+
 int FFMPEG::ff_cpus()
 {
        return !file_base ? 1 : file_base->file->cpus;
diff --git a/cinelerra-5.1/cinelerra/ffmpeg.h b/cinelerra-5.1/cinelerra/ffmpeg.h
index 2e1f201f..d4bef2fd 100644
--- a/cinelerra-5.1/cinelerra/ffmpeg.h
+++ b/cinelerra-5.1/cinelerra/ffmpeg.h
@@ -463,6 +463,7 @@ public:
        float ff_aspect_ratio(int stream);
        int ff_color_range(int stream);
        int ff_color_space(int stream);
+       int ff_interlace(int sream);
        double ff_frame_rate(int stream);
        const char *ff_video_codec(int stream);
        int64_t ff_video_frames(int stream);
diff --git a/cinelerra-5.1/cinelerra/fileffmpeg.C b/cinelerra-5.1/cinelerra/fileffmpeg.C
index ff206b10..f8567adf 100644
--- a/cinelerra-5.1/cinelerra/fileffmpeg.C
+++ b/cinelerra-5.1/cinelerra/fileffmpeg.C
@@ -342,6 +342,9 @@ int FileFFMPEG::open_file(int rd, int wr)
                        int video_layers = ff->ff_total_video_layers();
                        if( video_layers > 0 ) {
                                asset->video_data = 1;
+                               asset->aspect_ratio = ff->ff_aspect_ratio(0);
+                               printf("ff_aspect_ratio, %f \n", asset->aspect_ratio);
+                               if (!asset->interlace_mode) asset->interlace_mode = ff->ff_interlace(0);
                                if( !asset->layers ) asset->layers = video_layers;
                                asset->actual_width = ff->ff_video_width(0);
                                asset->actual_height = ff->ff_video_height(0);
diff --git a/cinelerra-5.1/cinelerra/mwindow.C b/cinelerra-5.1/cinelerra/mwindow.C
index f245018a..cac84d10 100644
--- a/cinelerra-5.1/cinelerra/mwindow.C
+++ b/cinelerra-5.1/cinelerra/mwindow.C
@@ -4493,11 +4493,14 @@ static inline int gcd(int m, int n)
 int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
 {
        w = 1;  h = 1;
+       double ar;
+       
        if(!width || !height) return 1;
        if( width == 720 && (height == 480 || height == 576) ) {
                w = 4;  h = 3;  return 0; // for NTSC and PAL
        }
-       double ar = (double)width / height;
+       
+       ar = (double)width / height;
 // square-ish pixels
        if( EQUIV(ar, 1.0000) ) return 0;
        if( EQUIV(ar, 1.3333) ) { w = 4;  h = 3;  return 0; }
@@ -4505,6 +4508,7 @@ int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
        if( EQUIV(ar, 2.1111) ) { w = 19; h = 9;  return 0; }
        if( EQUIV(ar, 2.2222) ) { w = 20; h = 9;  return 0; }
        if( EQUIV(ar, 2.3333) ) { w = 21; h = 9;  return 0; }
+
        int ww = width, hh = height;
        // numerator, denominator must be under mx
        int mx = 255, n = gcd(ww, hh);
@@ -5052,8 +5056,20 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                asset->width = session->output_w;
                asset->height = session->output_h;
                asset->frame_rate = session->frame_rate;
+               
                create_aspect_ratio(session->aspect_w, session->aspect_h,
                        session->output_w, session->output_h);
+       float ar = asset->aspect_ratio;
+       if (ar) {
+       printf ("Aspect ratio from asset: %f \n", ar);
+       if( EQUIV(ar, 1.3333) ) { session->aspect_w = 4;  session->aspect_h = 3;  }
+       if( EQUIV(ar, 1.7777) ) { session->aspect_w = 16; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.1111) ) { session->aspect_w = 19; session->aspect_h = 9;  }
+       if( EQUIV(ar, 2.2222) ) { session->aspect_w = 20; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.3333) ) { session->aspect_w = 21; session->aspect_h = 9;   }
+       }
+                       
+                       
                Track *track = edl->tracks->first;
                for( Track *next_track=0; track; track=next_track ) {
                        next_track = track->next;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: interlace_aspect_autodetect-2.patch
Type: text/x-diff
Size: 5001 bytes
Desc: not available
URL: <https://lists.cinelerra-gg.org/pipermail/cin/attachments/20201114/87e66de0/attachment-0001.bin>


More information about the Cin mailing list