From 30476f13ad6fee7392993e848f2b89519521df39 Mon Sep 17 00:00:00 2001
From: Jonas Karlman <jonas@kwiboo.se>
Date: Sat, 15 Sep 2018 11:24:28 +0200
Subject: [PATCH] [m2vd]: export aspect ratio information

---
 inc/mpp_frame.h                 | 10 ++++++++++
 mpp/base/inc/mpp_frame_impl.h   |  2 ++
 mpp/base/mpp_frame.cpp          |  1 +
 mpp/codec/dec/m2v/m2vd_parser.c | 22 ++++++++++++++++++++++
 4 files changed, 35 insertions(+)

diff --git a/inc/mpp_frame.h b/inc/mpp_frame.h
index 39763f37..fd1358d3 100644
--- a/inc/mpp_frame.h
+++ b/inc/mpp_frame.h
@@ -189,6 +189,14 @@ typedef enum {
     MPP_FMT_BUTT            = MPP_FMT_COMPLEX_BUTT,
 } MppFrameFormat;
 
+/**
+ * Rational number (pair of numerator and denominator).
+ */
+typedef struct MppFrameRational {
+    RK_S32 num; ///< Numerator
+    RK_S32 den; ///< Denominator
+} MppFrameRational;
+
 typedef enum {
     MPP_FRAME_ERR_UNKNOW           = 0x0001,
     MPP_FRAME_ERR_UNSUPPORT        = 0x0002,
@@ -266,6 +274,8 @@ MppFrameChromaLocation mpp_frame_get_chroma_location(const MppFrame frame);
 void    mpp_frame_set_chroma_location(MppFrame frame, MppFrameChromaLocation chroma_location);
 MppFrameFormat mpp_frame_get_fmt(MppFrame frame);
 void    mpp_frame_set_fmt(MppFrame frame, MppFrameFormat fmt);
+MppFrameRational mpp_frame_get_sar(const MppFrame frame);
+void    mpp_frame_set_sar(MppFrame frame, MppFrameRational sar);
 
 /*
  * HDR parameter
diff --git a/mpp/base/inc/mpp_frame_impl.h b/mpp/base/inc/mpp_frame_impl.h
index 2b512fcb..30898a1a 100644
--- a/mpp/base/inc/mpp_frame_impl.h
+++ b/mpp/base/inc/mpp_frame_impl.h
@@ -88,6 +88,8 @@ struct MppFrameImpl_t {
 
     MppFrameFormat fmt;
 
+    MppFrameRational sar;
+
     /*
      * buffer information
      * NOTE: buf_size only access internally
diff --git a/mpp/base/mpp_frame.cpp b/mpp/base/mpp_frame.cpp
index 1452604a..b94d1622 100644
--- a/mpp/base/mpp_frame.cpp
+++ b/mpp/base/mpp_frame.cpp
@@ -199,5 +199,6 @@ MPP_FRAME_ACCESSORS(MppFrameColorTransferCharacteristic, color_trc)
 MPP_FRAME_ACCESSORS(MppFrameColorSpace, colorspace)
 MPP_FRAME_ACCESSORS(MppFrameChromaLocation, chroma_location)
 MPP_FRAME_ACCESSORS(MppFrameFormat, fmt)
+MPP_FRAME_ACCESSORS(MppFrameRational, sar)
 MPP_FRAME_ACCESSORS(size_t, buf_size)
 MPP_FRAME_ACCESSORS(RK_U32, errinfo)
diff --git a/mpp/codec/dec/m2v/m2vd_parser.c b/mpp/codec/dec/m2v/m2vd_parser.c
index 619e4fb2..561c4acf 100755
--- a/mpp/codec/dec/m2v/m2vd_parser.c
+++ b/mpp/codec/dec/m2v/m2vd_parser.c
@@ -82,6 +82,25 @@ static int frame_period_Table[16] = {
     1
 };
 
+static const MppFrameRational mpeg2_aspect[16] = {
+    {0,1},
+    {1,1},
+    {4,3},
+    {16,9},
+    {221,100},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+    {0,1},
+};
+
 static inline RK_S32 m2vd_get_readbits(BitReadCtx_t *bx)
 {
     return bx->used_bits;
@@ -1164,6 +1183,9 @@ static MPP_RET m2vd_alloc_frame(M2VDParserContext *ctx)
             }
             mpp_frame_set_mode(ctx->frame_cur->f, frametype);
 
+            if (ctx->seq_head.aspect_ratio_information >= 0 && ctx->seq_head.aspect_ratio_information < 16)
+                mpp_frame_set_sar(ctx->frame_cur->f, mpeg2_aspect[ctx->seq_head.aspect_ratio_information]);
+
             mpp_buf_slot_get_unused(ctx->frame_slots, &ctx->frame_cur->slot_index);
             mpp_buf_slot_set_prop(ctx->frame_slots, ctx->frame_cur->slot_index, SLOT_FRAME, ctx->frame_cur->f);
             mpp_buf_slot_set_flag(ctx->frame_slots, ctx->frame_cur->slot_index, SLOT_CODEC_USE);
