[FFmpeg-devel] [PATCH]Write aspect ratio to gif files

Carl Eugen Hoyos cehoyos at ag.or.at
Tue Apr 1 23:07:16 CEST 2014


On Tuesday 01 April 2014 10:16:40 pm Michael Niedermayer wrote:
> On Tue, Apr 01, 2014 at 01:38:51PM +0200, Carl Eugen Hoyos wrote:
> > Hi!
> >
> > Attached patch writes the sample aspect ratio to gif.
> > Tested with xv and FFmpeg.
> >
> > Please comment, Carl Eugen
> >
> >  libavformat/gif.c  |   21 ++++++++++++++-------
> >  tests/ref/lavf/gif |    2 +-
> >  2 files changed, 15 insertions(+), 8 deletions(-)
> > b7060e85048dce022b89f41bf8da258211c88856  patchgifaspect.diff
> > diff --git a/libavformat/gif.c b/libavformat/gif.c
> > index e52498d..7eab31f 100644
> > --- a/libavformat/gif.c
> > +++ b/libavformat/gif.c
> > @@ -28,10 +28,18 @@
> >  #include "libavutil/log.h"
> >  #include "libavutil/opt.h"
> >
> > -static int gif_image_write_header(AVIOContext *pb, int width, int
> > height, +static int gif_image_write_header(AVFormatContext *s, int width,
> > int height, int loop_count, uint32_t *palette) {
> > -    int i;
> > +    AVIOContext *pb = s->pb;
> > +    AVRational sar = s->streams[0]->codec->sample_aspect_ratio;
> > +    int i, aspect = 0;
> > +
> > +    if (sar.den > 0) {
>
> this should also check sar.num >0
>
> > +        aspect = sar.num * 64 / sar.den - 15;
> > +        if (aspect > 255)
> > +            aspect = 0;
>
> i suspect this needs a aspect < 0 check too

New patch attached.

Thank you, Carl Eugen
-------------- next part --------------
diff --git a/libavformat/gif.c b/libavformat/gif.c
index e52498d..68320c6 100644
--- a/libavformat/gif.c
+++ b/libavformat/gif.c
@@ -28,10 +28,18 @@
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 
-static int gif_image_write_header(AVIOContext *pb, int width, int height,
+static int gif_image_write_header(AVFormatContext *s, int width, int height,
                                   int loop_count, uint32_t *palette)
 {
-    int i;
+    AVIOContext *pb = s->pb;
+    AVRational sar = s->streams[0]->codec->sample_aspect_ratio;
+    int i, aspect = 0;
+
+    if (sar.num > 0 && sar.den > 0) {
+        aspect = sar.num * 64 / sar.den - 15;
+        if (aspect < 0 || aspect > 255)
+            aspect = 0;
+    }
 
     avio_write(pb, "GIF", 3);
     avio_write(pb, "89a", 3);
@@ -41,7 +49,7 @@ static int gif_image_write_header(AVIOContext *pb, int width, int height,
     if (palette) {
         avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
         avio_w8(pb, 0x1f); /* background color index */
-        avio_w8(pb, 0);    /* aspect ratio */
+        avio_w8(pb, aspect);
         for (i = 0; i < 256; i++) {
             const uint32_t v = palette[i] & 0xffffff;
             avio_wb24(pb, v);
@@ -49,7 +57,7 @@ static int gif_image_write_header(AVIOContext *pb, int width, int height,
     } else {
         avio_w8(pb, 0); /* flags */
         avio_w8(pb, 0); /* background color index */
-        avio_w8(pb, 0); /* aspect ratio */
+        avio_w8(pb, aspect);
     }
 
 
@@ -79,7 +87,6 @@ typedef struct {
 static int gif_write_header(AVFormatContext *s)
 {
     GIFContext *gif = s->priv_data;
-    AVIOContext *pb = s->pb;
     AVCodecContext *video_enc;
     int width, height;
     uint32_t palette[AVPALETTE_COUNT];
@@ -99,9 +106,9 @@ static int gif_write_header(AVFormatContext *s)
     avpriv_set_pts_info(s->streams[0], 64, 1, 100);
     if (avpriv_set_systematic_pal2(palette, video_enc->pix_fmt) < 0) {
         av_assert0(video_enc->pix_fmt == AV_PIX_FMT_PAL8);
-        gif_image_write_header(pb, width, height, gif->loop, NULL);
+        gif_image_write_header(s, width, height, gif->loop, NULL);
     } else {
-        gif_image_write_header(pb, width, height, gif->loop, palette);
+        gif_image_write_header(s, width, height, gif->loop, palette);
     }
 
     avio_flush(s->pb);
diff --git a/tests/ref/lavf/gif b/tests/ref/lavf/gif
index 4d90abe..07506df 100644
--- a/tests/ref/lavf/gif
+++ b/tests/ref/lavf/gif
@@ -1,3 +1,3 @@
-8aef8081e8afa445f63f320f4a1c5edb *./tests/data/lavf/lavf.gif
+a7365d5015b227f495210e9846d6f3ed *./tests/data/lavf/lavf.gif
 2030198 ./tests/data/lavf/lavf.gif
 ./tests/data/lavf/lavf.gif CRC=0x0dc5477c


More information about the ffmpeg-devel mailing list