[MPlayer-dev-eng] [PATCH] gif demux & decode support

Joey Parrish joey at nicewarrior.org
Mon Jan 6 23:54:39 CET 2003


Hello,

Attached below is a patch that adds GIF demux and decode support
to MPlayer.  Finally, there's something other than a browser that
will play back an animated GIF.  :)

First let me warn about the things wrong with this patch:
  - some values are not set in demux_open (still plays fine)
  - seeking not yet supported
  - framerate is hard-coded as constant 5 fps
  - no new documentation with this version of the patch

So only ~5fps GIFs look the right speed, and the values on MPlayer's
status line are mostly 0's... but it's a start.  Also, the next version
will include patches to the DOCS to mention this new demuxer.

Things I couldn't figure out from the source and DOCS:
  - what fields in demuxer_t should be set to what during demux_open
  - how to handle that GIFs are not constant framerate

Comments are much appreciated.

Thanks,
--Joey
-------------- next part --------------
diff -Nur main.cvs/Makefile main.dev/Makefile
--- main.cvs/Makefile	Mon Dec 30 17:27:23 2002
+++ main.dev/Makefile	Mon Jan  6 15:40:56 2003
@@ -33,9 +33,9 @@
 OBJS_MENCODER = $(SRCS_MENCODER:.c=.o)
 OBJS_MPLAYER = $(SRCS_MPLAYER:.c=.o)
 
-VO_LIBS = $(AA_LIB) $(X_LIB) $(SDL_LIB) $(GGI_LIB) $(MP1E_LIB) $(MLIB_LIB) $(SVGA_LIB) $(DIRECTFB_LIB) $(GIF_LIB) 
+VO_LIBS = $(AA_LIB) $(X_LIB) $(SDL_LIB) $(GGI_LIB) $(MP1E_LIB) $(MLIB_LIB) $(SVGA_LIB) $(DIRECTFB_LIB)
 AO_LIBS = $(ARTS_LIB) $(ESD_LIB) $(NAS_LIB) $(SGIAUDIO_LIB)
-CODEC_LIBS = $(AV_LIB) $(FAME_LIB) $(MAD_LIB) $(VORBIS_LIB) $(FAAD_LIB) $(LIBLZO_LIB) $(XVID_LIB) $(DECORE_LIB) $(PNG_LIB) $(Z_LIB) $(JPEG_LIB) $(ALSA_LIB) $(XMMS_LIB)
+CODEC_LIBS = $(AV_LIB) $(FAME_LIB) $(MAD_LIB) $(VORBIS_LIB) $(FAAD_LIB) $(LIBLZO_LIB) $(XVID_LIB) $(DECORE_LIB) $(PNG_LIB) $(Z_LIB) $(JPEG_LIB) $(ALSA_LIB) $(XMMS_LIB) $(GIF_LIB) 
 COMMON_LIBS = libmpcodecs/libmpcodecs.a mp3lib/libMP3.a liba52/liba52.a libmpeg2/libmpeg2.a $(W32_LIB) $(DS_LIB) libaf/libaf.a libmpdemux/libmpdemux.a input/libinput.a $(PP_LIB) postproc/libswscale.a linux/libosdep.a $(CSS_LIB) $(CODEC_LIBS) $(FREETYPE_LIB) $(TERMCAP_LIB) $(CDPARANOIA_LIB) $(STREAMING_LIB) $(WIN32_LIB)
 
 CFLAGS = $(OPTFLAGS) -Ilibmpdemux -Iloader -Ilibvo $(FREETYPE_INC) $(EXTRA_INC) $(CDPARANOIA_INC) $(SDL_INC) # -Wall
diff -Nur main.cvs/configure main.dev/configure
--- main.cvs/configure	Sun Jan  5 20:35:21 2003
+++ main.dev/configure	Mon Jan  6 15:40:56 2003
@@ -167,7 +167,7 @@
   --disable-sortsub      Disable subtitles sorting [enabled]
 
 Codecs:
-  --enable-gif		 enable gif89a output support [autodetect]
+  --enable-gif		 enable gif89a support [autodetect]
   --enable-png		 enable png input/output support [autodetect]
   --enable-jpeg		 enable jpeg input/output support [autodetect]
   --enable-liblzo	 enable external liblzo support [autodetect]
@@ -2913,8 +2913,9 @@
   _def_gif='#define HAVE_GIF 1'
   _vosrc="$_vosrc vo_gif89a.c"
   _vomodules="gif89a $_vomodules"
+  _codecmodules="gif $_codecmodules"
   _mkf_gif="yes"
-  _gif="yes (old version, some functions disabled)"
+  _gif="yes (old version, some encode functions disabled)"
   _def_gif_4='#undef HAVE_GIF_4'
 
   cat > $TMPC << EOF
@@ -2936,6 +2937,7 @@
   _def_gif='#undef HAVE_GIF'
   _def_gif_4='#undef HAVE_GIF_4'
   _novomodules="gif89a $_novomodules"
+  _nocodecmodules="gif $_codecmodules"
   _mkf_gif="no"
 fi
 echores "$_gif"
diff -Nur main.cvs/etc/codecs.conf main.dev/etc/codecs.conf
--- main.cvs/etc/codecs.conf	Fri Dec 27 21:43:58 2002
+++ main.dev/etc/codecs.conf	Mon Jan  6 15:40:56 2003
@@ -60,6 +60,13 @@
   driver mpng
   out BGR32,BGR24
 
+videocodec gif
+  info "GIF decoder"
+  status working
+  fourcc GIFV
+  driver gif
+  out RGB24
+
 videocodec mtga
   info "TGA images decoder"
   status working
diff -Nur main.cvs/libmpcodecs/Makefile main.dev/libmpcodecs/Makefile
--- main.cvs/libmpcodecs/Makefile	Mon Dec 16 13:24:45 2002
+++ main.dev/libmpcodecs/Makefile	Mon Jan  6 15:40:56 2003
@@ -10,7 +10,7 @@
 AUDIO_SRCS=dec_audio.c ad.c $(AUDIO_SRCS_LIB) $(AUDIO_SRCS_NAT) $(AUDIO_SRCS_OPT)
 
 VIDEO_SRCS_LIB=vd_libmpeg2.c vd_nuv.c vd_lzo.c
-VIDEO_SRCS_NAT=vd_null.c vd_cinepak.c vd_qtrpza.c vd_raw.c vd_msvidc.c vd_fli.c vd_qtrle.c vd_qtsmc.c vd_roqvideo.c vd_cyuv.c vd_msrle.c vd_huffyuv.c vd_mpegpes.c vd_svq1.c vd_lcl.c vd_mtga.c
+VIDEO_SRCS_NAT=vd_null.c vd_cinepak.c vd_qtrpza.c vd_raw.c vd_msvidc.c vd_fli.c vd_qtrle.c vd_qtsmc.c vd_roqvideo.c vd_cyuv.c vd_msrle.c vd_huffyuv.c vd_mpegpes.c vd_svq1.c vd_lcl.c vd_mtga.c vd_gif.c
 VIDEO_SRCS_OPT=vd_realvid.c vd_ffmpeg.c vd_dshow.c vd_dmo.c vd_vfw.c vd_vfwex.c vd_odivx.c vd_divx4.c vd_xanim.c vd_xvid.c vd_libdv.c vd_qtvideo.c
 VIDEO_SRCS=dec_video.c vd.c $(VIDEO_SRCS_NAT) $(VIDEO_SRCS_LIB) $(VIDEO_SRCS_OPT)
 
diff -Nur main.cvs/libmpcodecs/vd.c main.dev/libmpcodecs/vd.c
--- main.cvs/libmpcodecs/vd.c	Fri Dec 27 21:43:58 2002
+++ main.dev/libmpcodecs/vd.c	Mon Jan  6 15:40:56 2003
@@ -58,6 +58,7 @@
 extern vd_functions_t mpcodecs_vd_lcl;
 extern vd_functions_t mpcodecs_vd_lzo;
 extern vd_functions_t mpcodecs_vd_qtvideo;
+extern vd_functions_t mpcodecs_vd_gif;
 
 vd_functions_t* mpcodecs_vd_drivers[] = {
         &mpcodecs_vd_null,
@@ -120,6 +121,9 @@
 	&mpcodecs_vd_lcl,
 #ifdef USE_QTX_CODECS
 	&mpcodecs_vd_qtvideo,
+#endif
+#ifdef HAVE_GIF
+	&mpcodecs_vd_gif,
 #endif
 	NULL
 };
diff -Nur main.cvs/libmpcodecs/vd_gif.c main.dev/libmpcodecs/vd_gif.c
--- main.cvs/libmpcodecs/vd_gif.c	Wed Dec 31 18:00:00 1969
+++ main.dev/libmpcodecs/vd_gif.c	Mon Jan  6 15:40:56 2003
@@ -0,0 +1,66 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "config.h"
+#include "mp_msg.h"
+
+#ifdef HAVE_GIF
+
+#include <gif_lib.h>
+
+#include "vd_internal.h"
+
+static vd_info_t info = {
+	"GIF decoder",
+	"gif",
+	"Joey Parrish",
+	"http://www.w3.org/Graphics/GIF/spec-gif89a.txt",
+	"uses libungif and family"
+};
+
+LIBVD_EXTERN(gif)
+
+// to set/get/query special features/parameters
+static int control(sh_video_t *sh,int cmd,void* arg,...){
+    return CONTROL_UNKNOWN;
+}
+
+// init driver
+static int init(sh_video_t *sh){
+    return mpcodecs_config_vo(sh, sh->disp_w, sh->disp_h, IMGFMT_RGB24);
+}
+
+// uninit driver
+static void uninit(sh_video_t *sh){
+}
+
+// decode a frame
+static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
+    mp_image_t* mpi;
+    GifFileType *gif = (GifFileType *)sh->ds->demuxer->priv;
+    int x, y;
+    
+    if (len <= 0) return NULL; // skipped frame
+
+    mpi = mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, sh->disp_w, sh->disp_h);
+    if(!mpi) return NULL;
+
+    for (y = 0; y < gif->Image.Height; y++) {
+      unsigned char *drow = mpi->planes[0];
+      unsigned char *buf = data + (y * gif->Image.Width);
+      ColorMapObject *cmap = gif->Image.ColorMap;
+
+      drow += mpi->stride[0] * (y + gif->Image.Top);
+      drow += gif->Image.Left;
+
+      for (x = 0; x < gif->Image.Width; x++) {
+        drow[(x * 3) + 0] = cmap->Colors[buf[x]].Red;
+        drow[(x * 3) + 1] = cmap->Colors[buf[x]].Green;
+        drow[(x * 3) + 2] = cmap->Colors[buf[x]].Blue;
+      }
+    }
+
+    return mpi;
+}
+
+#endif /* HAVE_GIF */
diff -Nur main.cvs/libmpdemux/Makefile main.dev/libmpdemux/Makefile
--- main.cvs/libmpdemux/Makefile	Sun Jan  5 20:35:22 2003
+++ main.dev/libmpdemux/Makefile	Mon Jan  6 15:40:56 2003
@@ -3,7 +3,7 @@
 
 include ../config.mak
 
-SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_pva.c demux_viv.c demuxer.c dvdauth.c dvdnav_stream.c open.c parse_es.c stream.c tv.c tvi_dummy.c tvi_v4l.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c demux_ogg.c demux_bmp.c cdda.c demux_rawaudio.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_oss.c audio_in.c demux_smjpeg.c cue_read.c
+SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_pva.c demux_viv.c demuxer.c dvdauth.c dvdnav_stream.c open.c parse_es.c stream.c tv.c tvi_dummy.c tvi_v4l.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c demux_ogg.c demux_bmp.c cdda.c demux_rawaudio.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_oss.c audio_in.c demux_smjpeg.c cue_read.c demux_gif.c
 ifeq ($(XMMS_PLUGINS),yes)
 SRCS += demux_xmms.c
 endif 
diff -Nur main.cvs/libmpdemux/demux_gif.c main.dev/libmpdemux/demux_gif.c
--- main.cvs/libmpdemux/demux_gif.c	Wed Dec 31 18:00:00 1969
+++ main.dev/libmpdemux/demux_gif.c	Mon Jan  6 15:40:56 2003
@@ -0,0 +1,141 @@
+/*
+	GIF file parser for MPlayer
+	by Joey Parrish
+	based on demux_fli.c
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "mp_msg.h"
+#include "help_mp.h"
+
+#include "stream.h"
+#include "demuxer.h"
+#include "stheader.h"
+
+#ifdef HAVE_GIF
+
+#include <gif_lib.h>
+
+int gif_check_file(demuxer_t *demuxer)
+{
+  lseek(demuxer->stream->fd, 0, SEEK_SET);
+  if (stream_read_dword(demuxer->stream) ==
+      (('G' << 24) | ('I' << 16) | ('F' << 8) | '8'))
+    return 1;
+  return 0;
+}
+
+int demux_gif_fill_buffer(demuxer_t *demuxer){
+  GifFileType *gif = (GifFileType *)demuxer->priv;
+  GifRecordType type = UNDEFINED_RECORD_TYPE;
+  int len = 0;
+  demux_packet_t *dp = NULL;
+
+  while (type != IMAGE_DESC_RECORD_TYPE) {
+    if (DGifGetRecordType(gif, &type) == GIF_ERROR) {
+      PrintGifError();
+      return 0; // oops
+    }
+    if (type == TERMINATE_RECORD_TYPE)
+      return 0; // eof
+    if (type == SCREEN_DESC_RECORD_TYPE) {
+      if (DGifGetScreenDesc(gif) == GIF_ERROR) {
+        PrintGifError();
+        return 0; // oops
+      }
+    }
+    if (type == EXTENSION_RECORD_TYPE) {
+      int code;
+      unsigned char *p = NULL;
+      if (DGifGetExtension(gif, &code, &p) == GIF_ERROR) {
+        PrintGifError();
+        return 0; // oops
+      }
+      while (p != NULL) {
+        if (DGifGetExtensionNext(gif, &p) == GIF_ERROR) {
+          PrintGifError();
+          return 0; // oops
+	}
+      }
+    }
+  }
+  
+  if (DGifGetImageDesc(gif) == GIF_ERROR) {
+    PrintGifError();
+    return 0; // oops
+  }
+
+  len = gif->Image.Width * gif->Image.Height;
+  dp = new_demux_packet(len);
+  
+  if (DGifGetLine(gif, dp->buffer, len) == GIF_ERROR) {
+    PrintGifError();
+    return 0; // oops
+  }
+
+  demuxer->video->dpos++;
+  ds_add_packet(demuxer->video, dp);
+
+  return 1;
+}
+
+demuxer_t* demux_open_gif(demuxer_t* demuxer) {
+  sh_video_t *sh_video = NULL;
+  GifFileType *gif = NULL;
+
+  // go back to the beginning
+  lseek(demuxer->stream->fd, 0, SEEK_SET);
+
+  demuxer->movi_start = 0;  // FIXME
+  demuxer->movi_end = 100;  // FIXME
+  demuxer->seekable = 0;
+
+  gif = DGifOpenFileHandle(demuxer->stream->fd);
+  if (!gif) {
+    PrintGifError();
+    return NULL;
+  }
+
+  // create a new video stream header
+  sh_video = new_sh_video(demuxer, 0);
+
+  // make sure the demuxer knows about the new video stream header
+  // (even though new_sh_video() ought to take care of it)
+  demuxer->video->sh = sh_video;
+
+  // make sure that the video demuxer stream header knows about its
+  // parent video demuxer stream (this is getting wacky), or else
+  // video_read_properties() will choke
+  sh_video->ds = demuxer->video;
+
+  sh_video->disp_w = gif->SWidth;
+  sh_video->disp_h = gif->SHeight;
+
+  // custom fourcc for internal MPlayer use
+  sh_video->format = mmioFOURCC('G', 'I', 'F', 'V');
+
+  sh_video->frametime = 20 / 100; // FIXME
+  sh_video->fps = 1 / sh_video->frametime;
+
+  demuxer->priv = gif;
+
+  return demuxer;
+}
+
+void demux_close_gif(demuxer_t* demuxer) {
+  GifFileType *gif = (GifFileType *)demuxer->priv;
+
+  if(!gif)
+    return;
+
+  if (DGifCloseFile(gif) == GIF_ERROR)
+    PrintGifError();
+  
+  demuxer->stream->fd = 0;
+  demuxer->priv = NULL;
+}
+#endif /* HAVE_GIF */
diff -Nur main.cvs/libmpdemux/demuxer.c main.dev/libmpdemux/demuxer.c
--- main.cvs/libmpdemux/demuxer.c	Mon Jan  6 15:39:46 2003
+++ main.dev/libmpdemux/demuxer.c	Mon Jan  6 15:40:56 2003
@@ -133,6 +133,7 @@
 extern void demux_close_pva(demuxer_t* demuxer);
 extern void demux_close_smjpeg(demuxer_t* demuxer);
 extern void demux_close_xmms(demuxer_t* demuxer);
+extern void demux_close_gif(demuxer_t* demuxer);
 
 #ifdef USE_TV
 #include "tv.h"
@@ -199,6 +200,10 @@
     case DEMUXER_TYPE_XMMS:
       demux_close_xmms(demuxer); break;
 #endif
+#ifdef HAVE_GIF
+    case DEMUXER_TYPE_GIF:
+      demux_close_gif(demuxer); break;
+#endif
 
     }
     // free streams:
@@ -276,6 +281,7 @@
 int demux_audio_fill_buffer(demux_stream_t *ds);
 int demux_pva_fill_buffer(demuxer_t *demux);
 int demux_xmms_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
+int demux_gif_fill_buffer(demuxer_t *demux);
 
 extern int demux_demuxers_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
 extern int demux_ogg_fill_buffer(demuxer_t *d);
@@ -322,6 +328,9 @@
     case DEMUXER_TYPE_RTP: return demux_rtp_fill_buffer(demux, ds);
 #endif
     case DEMUXER_TYPE_SMJPEG: return demux_smjpeg_fill_buffer(demux);
+#ifdef HAVE_GIF
+    case DEMUXER_TYPE_GIF: return demux_gif_fill_buffer(demux);
+#endif
   }
   return 0;
 }
@@ -544,6 +553,8 @@
 extern int demux_open_smjpeg(demuxer_t* demuxer);
 extern int bmp_check_file(demuxer_t *demuxer);
 extern int demux_xmms_open(demuxer_t* demuxer);
+extern int gif_check_file(demuxer_t *demuxer);
+extern int demux_open_gif(demuxer_t* demuxer);
 
 extern demuxer_t* init_avi_with_ogg(demuxer_t* demuxer);
 
@@ -721,6 +732,19 @@
       demuxer = NULL;
   }
 }
+#ifdef HAVE_GIF
+//=============== Try to open as GIF file: =================
+if(file_format==DEMUXER_TYPE_UNKNOWN || file_format==DEMUXER_TYPE_GIF){
+  demuxer=new_demuxer(stream,DEMUXER_TYPE_GIF,audio_id,video_id,dvdsub_id);
+  if(gif_check_file(demuxer)){
+      mp_msg(MSGT_DEMUXER,MSGL_INFO,MSGTR_Detected_XXX_FileFormat,"GIF");
+      file_format=DEMUXER_TYPE_GIF;
+  } else {
+      free_demuxer(demuxer);
+      demuxer = NULL;
+  }
+}
+#endif
 //=============== Try to open as BMP file: =================
 if(file_format==DEMUXER_TYPE_UNKNOWN || file_format==DEMUXER_TYPE_BMP){
   demuxer=new_demuxer(stream,DEMUXER_TYPE_BMP,audio_id,video_id,dvdsub_id);
@@ -917,6 +941,12 @@
   if (!demux_open_film(demuxer)) return NULL;
   break;
  }
+#ifdef HAVE_GIF
+ case DEMUXER_TYPE_GIF: {
+  if (!demux_open_gif(demuxer)) return NULL;
+  break;
+ }
+#endif
  case DEMUXER_TYPE_BMP: {
   if (!demux_open_bmp(demuxer)) return NULL;
   break;
diff -Nur main.cvs/libmpdemux/demuxer.h main.dev/libmpdemux/demuxer.h
--- main.cvs/libmpdemux/demuxer.h	Mon Dec 23 16:52:21 2002
+++ main.dev/libmpdemux/demuxer.h	Mon Jan  6 15:40:56 2003
@@ -34,6 +34,7 @@
 #define DEMUXER_TYPE_PVA 23
 #define DEMUXER_TYPE_SMJPEG 24
 #define DEMUXER_TYPE_XMMS 25
+#define DEMUXER_TYPE_GIF 26
 
 // This should always match the higest demuxer type number.
 // Unless you want to disallow users to force the demuxer to some types


More information about the MPlayer-dev-eng mailing list