[FFmpeg-devel] [PATCH] lavf: add av_guess_frame_sample_aspect_ratio function

Marton Balint cus at passwd.hu
Sun Apr 29 01:30:02 CEST 2012


Guesses the sample aspect ratio of a frame, based on both the stream and the
frame aspect ratio.

Since the frame aspect ratio is set by the codec but the stream aspect ratio
is set by the demuxer, these two may not be equal. This function tries to
return the value that you should use if you would like to display the frame.

Basic logic is to use the stream aspect ratio if it is set to something sane
and not 1:1, otherwise use the frame aspect ratio if it is set. I made some
tests with the files on samples.libav.org and this logic performed the best.

Signed-off-by: Marton Balint <cus at passwd.hu>
---
 doc/APIchanges         |    3 +++
 libavformat/avformat.h |   19 +++++++++++++++++++
 libavformat/utils.c    |   22 ++++++++++++++++++++++
 libavformat/version.h  |    2 +-
 4 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 0867fa4..93d616d 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:   2011-04-18
 
 API changes, most recent first:
 
+2012-04-28 - xxxxxxx - lavf 54.4.100
+  Add av_guess_sample_aspect_ratio() function.
+
 2012-04-20 - xxxxxxx - lavfi 2.70.100
   Add avfilter_unref_bufferp() to avfilter.h.
 
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index d2727d4..3de2697 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1910,6 +1910,25 @@ const struct AVCodecTag *avformat_get_riff_video_tags(void);
  * @return the table mapping RIFF FourCCs for audio to CodecID.
  */
 const struct AVCodecTag *avformat_get_riff_audio_tags(void);
+
+/**
+ * Guesses the sample aspect ratio of a frame, based on both the stream and the
+ * frame aspect ratio.
+ *
+ * Since the frame aspect ratio is set by the codec but the stream aspect ratio
+ * is set by the demuxer, these two may not be equal. This function tries to
+ * return the value that you should use if you would like to display the frame.
+ *
+ * Basic logic is to use the stream aspect ratio if it is set to something sane
+ * and not 1:1, otherwise use the frame aspect ratio if it is set.
+ *
+ * @param frame_sample_aspect_ratio aspect ratio of the AVFrame
+ * @param stream_sample_aspect_ratio aspect ratio of the AVStream
+ * @return the guessed (valid) sample_aspect_ratio, 0/1 if no idea.
+ */
+AVRational av_guess_sample_aspect_ratio(AVRational stream_sample_aspect_ratio,
+                                        AVRational frame_sample_aspect_ratio);
+
 /**
  * @}
  */
diff --git a/libavformat/utils.c b/libavformat/utils.c
index d6e7f69..36d0e59 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4392,3 +4392,25 @@ const struct AVCodecTag *avformat_get_riff_audio_tags(void)
 {
     return ff_codec_wav_tags;
 }
+
+AVRational av_guess_sample_aspect_ratio(AVRational stream_sample_aspect_ratio,
+                                        AVRational frame_sample_aspect_ratio)
+{
+    AVRational undef = {0, 1};
+
+    av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
+               stream_sample_aspect_ratio.num,  stream_sample_aspect_ratio.den, INT_MAX);
+    if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
+        stream_sample_aspect_ratio = undef;
+
+    av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
+               frame_sample_aspect_ratio.num,  frame_sample_aspect_ratio.den, INT_MAX);
+    if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
+        frame_sample_aspect_ratio = undef;
+
+    if ((stream_sample_aspect_ratio.num == 1 || stream_sample_aspect_ratio.num == 0) && stream_sample_aspect_ratio.den == 1)
+        if (frame_sample_aspect_ratio.num)
+            return frame_sample_aspect_ratio;
+
+    return stream_sample_aspect_ratio;
+}
diff --git a/libavformat/version.h b/libavformat/version.h
index 7c4d8e7..a3143c3 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 54
-#define LIBAVFORMAT_VERSION_MINOR  3
+#define LIBAVFORMAT_VERSION_MINOR  4
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
1.7.3.4



More information about the ffmpeg-devel mailing list