[FFmpeg-devel] [PATCH] lavc: add a sample_aspect_ratio field to AVFrame

Stefano Sabatini stefano.sabatini-lala at poste.it
Fri Apr 29 13:14:06 CEST 2011


Simplify application-level sample aspect ratio information extraction,
and allow further simplifications.
---
 cmdutils.c                |    1 +
 ffmpeg.c                  |    7 +++----
 ffplay.c                  |    2 +-
 libavcodec/avcodec.h      |    8 ++++++++
 libavcodec/utils.c        |    3 +++
 libavfilter/vsrc_buffer.c |   10 +++++-----
 libavfilter/vsrc_buffer.h |    4 ++--
 libavfilter/vsrc_movie.c  |    4 ++--
 8 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/cmdutils.c b/cmdutils.c
index f885a7c..21238be 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -902,6 +902,7 @@ int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
     frame->top_field_first  = picref->video->top_field_first;
     frame->key_frame        = picref->video->key_frame;
     frame->pict_type        = picref->video->pict_type;
+    frame->sample_aspect_ratio = picref->video->pixel_aspect;
 
     return 1;
 }
diff --git a/ffmpeg.c b/ffmpeg.c
index bf0133e..52e4764 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1631,13 +1631,12 @@ static int output_packet(AVInputStream *ist, int ist_index,
             for(i=0;i<nb_ostreams;i++) {
                 ost = ost_table[i];
                 if (ost->input_video_filter && ost->source_index == ist_index) {
-                    AVRational sar;
-                    if (ist->st->sample_aspect_ratio.num) sar = ist->st->sample_aspect_ratio;
-                    else                                  sar = ist->st->codec->sample_aspect_ratio;
+                    if (!picture.sample_aspect_ratio.num)
+                        picture.sample_aspect_ratio = ist->st->sample_aspect_ratio;
                     // add it to be filtered
                     av_vsrc_buffer_add_frame2(ost->input_video_filter, &picture,
                                              ist->pts,
-                                             sar, ist->st->codec->width, ist->st->codec->height,
+                                             ist->st->codec->width, ist->st->codec->height,
                                              ist->st->codec->pix_fmt, ""); //TODO user setable params
                 }
             }
diff --git a/ffplay.c b/ffplay.c
index 35500d0..fcb9418 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -1688,7 +1688,7 @@ static int input_request_frame(AVFilterLink *link)
 
     picref->pts = pts;
     picref->pos = priv->frame->pkt_pos;
-    picref->video->pixel_aspect = priv->is->video_st->codec->sample_aspect_ratio;
+    picref->video->pixel_aspect = priv->frame->sample_aspect_ratio;
     avfilter_start_frame(link, picref);
     avfilter_draw_slice(link, 0, link->h, 1);
     avfilter_end_frame(link);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 251a038..fb02b1e 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1011,6 +1011,14 @@ typedef struct AVPanScan{
      * - decoding: Read by user.\
      */\
     int64_t pkt_pos;\
+\
+    /**\
+     * reordered sample aspect ratio for the video frame, 0/1 if unknown\unspecified
+     * - encoding: unused\
+     * - decoding: Read by user.\
+     */\
+    AVRational sample_aspect_ratio;\
+\
 
 
 #define FF_QSCALE_TYPE_MPEG1 0
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 992bd95..bb8f2fb 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -455,6 +455,7 @@ void avcodec_get_frame_defaults(AVFrame *pic){
     pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
     pic->pkt_pos = -1;
     pic->key_frame= 1;
+    pic->sample_aspect_ratio = (AVRational){0, 1};
 }
 
 AVFrame *avcodec_alloc_frame(void){
@@ -754,6 +755,8 @@ int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *pi
                               avpkt);
             picture->pkt_dts= avpkt->dts;
             picture->pkt_pos= avpkt->pos;
+            if (!picture->sample_aspect_ratio.num)
+                picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
         }
 
         emms_c(); //needed to avoid an emms_c() call before every return;
diff --git a/libavfilter/vsrc_buffer.c b/libavfilter/vsrc_buffer.c
index c683d51..48c2f23 100644
--- a/libavfilter/vsrc_buffer.c
+++ b/libavfilter/vsrc_buffer.c
@@ -39,7 +39,7 @@ typedef struct {
 } BufferSourceContext;
 
 int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
-                              int64_t pts, AVRational pixel_aspect, int width,
+                              int64_t pts, int width,
                               int height, enum PixelFormat  pix_fmt,
                               const char *sws_param)
 {
@@ -104,20 +104,20 @@ int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
     c->frame.top_field_first = frame->top_field_first;
     c->frame.key_frame = frame->key_frame;
     c->frame.pict_type = frame->pict_type;
+    c->frame.sample_aspect_ratio = frame->sample_aspect_ratio;
     c->pts = pts;
-    c->pixel_aspect = pixel_aspect;
     c->has_frame = 1;
 
     return 0;
 }
 
 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
-                             int64_t pts, AVRational pixel_aspect)
+                             int64_t pts)
 {
     BufferSourceContext *c = buffer_filter->priv;
 
     return av_vsrc_buffer_add_frame2(buffer_filter, frame,
-                              pts, pixel_aspect, c->w,
+                              pts, c->w,
                               c->h, c->pix_fmt, "");
 }
 
@@ -190,7 +190,7 @@ static int request_frame(AVFilterLink *link)
                   picref->format, link->w, link->h);
 
     picref->pts                    = c->pts;
-    picref->video->pixel_aspect    = c->pixel_aspect;
+    picref->video->pixel_aspect    = c->frame.sample_aspect_ratio;
     picref->video->interlaced      = c->frame.interlaced_frame;
     picref->video->top_field_first = c->frame.top_field_first;
     picref->video->key_frame       = c->frame.key_frame;
diff --git a/libavfilter/vsrc_buffer.h b/libavfilter/vsrc_buffer.h
index 79a9908..68f7847 100644
--- a/libavfilter/vsrc_buffer.h
+++ b/libavfilter/vsrc_buffer.h
@@ -31,10 +31,10 @@
 #include "avfilter.h"
 
 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
-                             int64_t pts, AVRational pixel_aspect);
+                             int64_t pts);
 
 int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
-                              int64_t pts, AVRational pixel_aspect, int width,
+                              int64_t pts, int width,
                               int height, enum PixelFormat  pix_fmt,
                               const char *sws_param);
 
diff --git a/libavfilter/vsrc_movie.c b/libavfilter/vsrc_movie.c
index 31a70a7..19536a9 100644
--- a/libavfilter/vsrc_movie.c
+++ b/libavfilter/vsrc_movie.c
@@ -247,8 +247,8 @@ static int movie_get_frame(AVFilterLink *outlink)
                     movie->frame->pkt_dts : movie->frame->pkt_pts;
 
                 movie->picref->pos                    = movie->frame->pkt_pos;
-                movie->picref->video->pixel_aspect = st->sample_aspect_ratio.num ?
-                    st->sample_aspect_ratio : movie->codec_ctx->sample_aspect_ratio;
+                if (!movie->frame->sample_aspect_ratio.num)
+                    movie->picref->video->pixel_aspect = st->sample_aspect_ratio;
                 movie->picref->video->interlaced      = movie->frame->interlaced_frame;
                 movie->picref->video->top_field_first = movie->frame->top_field_first;
                 movie->picref->video->key_frame       = movie->frame->key_frame;
-- 
1.7.2.3



More information about the ffmpeg-devel mailing list