gstavidemux.c 26.1 KB
Newer Older
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/* Gnome-Streamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


//#define GST_DEBUG_ENABLED
#include <string.h>

#include "gstavidemux.h"


/* elementfactory information */
static GstElementDetails gst_avi_demux_details = {
  ".avi parser",
  "Parser/Video",
  "Parse a .avi file into audio and video",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>\n"
  "Wim Taymans <wim.taymans@tvd.be>",
  "(C) 1999",
};

static GstCaps* avi_typefind (GstBuffer *buf, gpointer private);

/* typefactory for 'avi' */
static GstTypeDefinition avidefinition = {
  "avidemux_video/avi",
  "video/avi",
  ".avi",
  avi_typefind,
};

/* AviDemux signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_BITRATE,
  ARG_MEDIA_TIME,
  ARG_CURRENT_TIME,
  /* FILL ME */
};

GST_PADTEMPLATE_FACTORY (sink_templ,
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "avidemux_sink",
     "video/avi",
69
      "RIFF", GST_PROPS_STRING ("AVI")
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  )
)

GST_PADTEMPLATE_FACTORY (src_video_templ,
  "video_[00-32]",
  GST_PAD_SRC,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "src_video",
    "video/avi",
      "format",  GST_PROPS_LIST (
	           GST_PROPS_STRING ("strf_vids"),
	           GST_PROPS_STRING ("strf_iavs")
      		 )
  )
)

GST_PADTEMPLATE_FACTORY (src_audio_templ,
  "src",
  GST_PAD_SRC,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "src_audio",
    "video/avi",
      "format", GST_PROPS_STRING ("strf_auds")
  )
)

static void 	gst_avi_demux_class_init	(GstAviDemuxClass *klass);
static void 	gst_avi_demux_init		(GstAviDemux *avi_demux);

static void 	gst_avi_demux_loop 		(GstElement *element);

Wim Taymans's avatar
Wim Taymans committed
103 104 105
static GstElementStateReturn
		gst_avi_demux_change_state 	(GstElement *element);

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static void     gst_avi_demux_get_property      (GObject *object, guint prop_id, 	
						 GValue *value, GParamSpec *pspec);


static GstElementClass *parent_class = NULL;
//static guint gst_avi_demux_signals[LAST_SIGNAL] = { 0 };

GType
gst_avi_demux_get_type(void) 
{
  static GType avi_demux_type = 0;

  if (!avi_demux_type) {
    static const GTypeInfo avi_demux_info = {
      sizeof(GstAviDemuxClass),      
      NULL,
      NULL,
      (GClassInitFunc)gst_avi_demux_class_init,
      NULL,
      NULL,
      sizeof(GstAviDemux),
      0,
      (GInstanceInitFunc)gst_avi_demux_init,
    };
    avi_demux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviDemux", &avi_demux_info, 0);
  }
  return avi_demux_type;
}

static void
gst_avi_demux_class_init (GstAviDemuxClass *klass) 
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_BITRATE,
    g_param_spec_long ("bitrate","bitrate","bitrate",
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); // CHECKME
  g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_MEDIA_TIME,
    g_param_spec_long ("media_time","media_time","media_time",
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); // CHECKME
  g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_CURRENT_TIME,
    g_param_spec_long ("current_time","current_time","current_time",
                       G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); // CHECKME

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
  
  gobject_class->get_property = gst_avi_demux_get_property;
Wim Taymans's avatar
Wim Taymans committed
157 158
  
  gstelement_class->change_state = gst_avi_demux_change_state;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

static void 
gst_avi_demux_init (GstAviDemux *avi_demux) 
{
  guint i;

  GST_FLAG_SET (avi_demux, GST_ELEMENT_EVENT_AWARE);
				
  avi_demux->sinkpad = gst_pad_new_from_template (
		  GST_PADTEMPLATE_GET (sink_templ), "sink");
  gst_element_add_pad (GST_ELEMENT (avi_demux), avi_demux->sinkpad);

  gst_element_set_loop_function (GST_ELEMENT (avi_demux), gst_avi_demux_loop);

  avi_demux->state = GST_AVI_DEMUX_UNKNOWN;
  avi_demux->num_audio_pads = 0;
  avi_demux->num_video_pads = 0;
  avi_demux->next_time = 0;
  avi_demux->flags = 0;
  avi_demux->index_entries = NULL;
  avi_demux->index_size = 0;
  avi_demux->resync_offset = 0;

  //GST_FLAG_SET( GST_OBJECT (avi_demux), GST_ELEMENT_NO_SEEK);

  for(i=0; i<GST_AVI_DEMUX_MAX_AUDIO_PADS; i++) 
    avi_demux->audio_pad[i] = NULL;

  for(i=0; i<GST_AVI_DEMUX_MAX_VIDEO_PADS; i++) 
    avi_demux->video_pad[i] = NULL;

}

static GstCaps*
avi_typefind (GstBuffer *buf,
              gpointer private)
{
  gchar *data = GST_BUFFER_DATA (buf);
  GstCaps *new;

  GST_DEBUG (0,"avi_demux: typefind\n");

  if (GUINT32_FROM_LE (((guint32 *)data)[0]) != GST_RIFF_TAG_RIFF)
    return NULL;
  if (GUINT32_FROM_LE (((guint32 *)data)[2]) != GST_RIFF_RIFF_AVI)
    return NULL;

207 208 209
  new = GST_CAPS_NEW ("avi_typefind",
		      "video/avi", 
		        "RIFF", GST_PROPS_STRING ("AVI"));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

  return new;
}

static gboolean
gst_avi_demux_avih (GstAviDemux *avi_demux)
{
  gst_riff_avih *avih;
  GstByteStream *bs = avi_demux->bs;

  avih = (gst_riff_avih *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_avih));
  if (avih) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: avih tag found");
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  us_frame    %d", GUINT32_FROM_LE (avih->us_frame));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  max_bps     %d", GUINT32_FROM_LE (avih->max_bps));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  pad_gran    %d", GUINT32_FROM_LE (avih->pad_gran));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  flags       0x%08x", GUINT32_FROM_LE (avih->flags));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  tot_frames  %d", GUINT32_FROM_LE (avih->tot_frames));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  init_frames %d", GUINT32_FROM_LE (avih->init_frames));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  streams     %d", GUINT32_FROM_LE (avih->streams));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bufsize     %d", GUINT32_FROM_LE (avih->bufsize));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  width       %d", GUINT32_FROM_LE (avih->width));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  height      %d", GUINT32_FROM_LE (avih->height));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  scale       %d", GUINT32_FROM_LE (avih->scale));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", GUINT32_FROM_LE (avih->rate));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  start       %d", GUINT32_FROM_LE (avih->start));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  length      %d", GUINT32_FROM_LE (avih->length));

    avi_demux->time_interval = GUINT32_FROM_LE (avih->us_frame);
    avi_demux->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
    avi_demux->flags = GUINT32_FROM_LE (avih->flags);

    return TRUE;
  }
  return FALSE;
}

static gboolean 
gst_avi_demux_strh (GstAviDemux *avi_demux)
{
  gst_riff_strh *strh;
  GstByteStream *bs = avi_demux->bs;

  strh = (gst_riff_strh *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strh));
  if (strh) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strh tag found");
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  type        0x%08x (%s)", 
  		  GUINT32_FROM_LE (strh->type), gst_riff_id_to_fourcc (strh->type));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  fcc_handler 0x%08x (%s)", 
		  GUINT32_FROM_LE (strh->fcc_handler), gst_riff_id_to_fourcc (strh->fcc_handler));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  flags       0x%08x", GUINT32_FROM_LE (strh->flags));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  priority    %d", GUINT32_FROM_LE (strh->priority));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  init_frames %d", GUINT32_FROM_LE (strh->init_frames));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  scale       %d", GUINT32_FROM_LE (strh->scale));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", GUINT32_FROM_LE (strh->rate));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  start       %d", GUINT32_FROM_LE (strh->start));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  length      %d", GUINT32_FROM_LE (strh->length));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bufsize     %d", GUINT32_FROM_LE (strh->bufsize));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  quality     %d", GUINT32_FROM_LE (strh->quality));
    GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  samplesize  %d", GUINT32_FROM_LE (strh->samplesize));

    avi_demux->fcc_type = GUINT32_FROM_LE (strh->type);

    return TRUE;
  }
  return FALSE;
}

static void 
gst_avi_demux_strf_vids (GstAviDemux *avi_demux)
{
  gst_riff_strf_vids *strf;
  GstPad *srcpad;
  GstByteStream *bs = avi_demux->bs;

  strf = (gst_riff_strf_vids *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strf_vids));

  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context vids");
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  size        %d", GUINT32_FROM_LE (strf->size));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  width       %d", GUINT32_FROM_LE (strf->width));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  height      %d", GUINT32_FROM_LE (strf->height));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  planes      %d", GUINT16_FROM_LE (strf->planes));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  bit_cnt     %d", GUINT16_FROM_LE (strf->bit_cnt));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  compression 0x%08x (%s)", 
		  GUINT32_FROM_LE (strf->compression), gst_riff_id_to_fourcc (strf->compression));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  image_size  %d", GUINT32_FROM_LE (strf->image_size));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  xpels_meter %d", GUINT32_FROM_LE (strf->xpels_meter));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  ypels_meter %d", GUINT32_FROM_LE (strf->ypels_meter));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  num_colors  %d", GUINT32_FROM_LE (strf->num_colors));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  imp_colors  %d", GUINT32_FROM_LE (strf->imp_colors));

  srcpad =  gst_pad_new_from_template (
		  GST_PADTEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d", 
			  avi_demux->num_video_pads));

305 306
  gst_pad_try_set_caps (srcpad, 
		        GST_CAPS_NEW (
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
307 308 309 310 311 312 313 314 315 316 317 318 319
			  "avidec_video_src",
			  "video/avi",
			    "format",    	GST_PROPS_STRING ("strf_vids"),
			      "size", 		GST_PROPS_INT (GUINT32_FROM_LE (strf->size)),
			      "width", 		GST_PROPS_INT (GUINT32_FROM_LE (strf->width)),
			      "height", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->height)),
			      "planes", 	GST_PROPS_INT (GUINT16_FROM_LE (strf->planes)),
			      "bit_cnt", 	GST_PROPS_INT (GUINT16_FROM_LE (strf->bit_cnt)),
			      "compression", 	GST_PROPS_FOURCC (GUINT32_FROM_LE (strf->compression)),
			      "image_size", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->image_size)),
			      "xpels_meter", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->xpels_meter)),
			      "ypels_meter", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->ypels_meter)),
			      "num_colors", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->num_colors)),
320 321
			      "imp_colors", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->imp_colors))
			      ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

  avi_demux->video_pad[avi_demux->num_video_pads++] = srcpad;
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void 
gst_avi_demux_strf_auds (GstAviDemux *avi_demux)
{
  gst_riff_strf_auds *strf;
  GstPad *srcpad;
  GstByteStream *bs = avi_demux->bs;

  strf = (gst_riff_strf_auds *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strf_auds));

  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context auds");
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  format      %d", GUINT16_FROM_LE (strf->format));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  channels    %d", GUINT16_FROM_LE (strf->channels));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  rate        %d", GUINT32_FROM_LE (strf->rate));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  av_bps      %d", GUINT32_FROM_LE (strf->av_bps));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  blockalign  %d", GUINT16_FROM_LE (strf->blockalign));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  size        %d", GUINT16_FROM_LE (strf->size));

  srcpad =  gst_pad_new_from_template (
		  GST_PADTEMPLATE_GET (src_audio_templ), g_strdup_printf ("audio_%02d", 
			  avi_demux->num_audio_pads));

348 349
  gst_pad_try_set_caps (srcpad, 
		        GST_CAPS_NEW (
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
350 351 352 353 354 355 356 357
			  "avidec_audio_src",
			  "video/avi",
			    "format",    	GST_PROPS_STRING ("strf_auds"),
			      "fmt", 		GST_PROPS_INT (GUINT16_FROM_LE (strf->format)),
			      "channels", 	GST_PROPS_INT (GUINT16_FROM_LE (strf->channels)),
			      "rate", 		GST_PROPS_INT (GUINT32_FROM_LE (strf->rate)),
			      "av_bps",		GST_PROPS_INT (GUINT32_FROM_LE (strf->av_bps)),
			      "blockalign",	GST_PROPS_INT (GUINT16_FROM_LE (strf->blockalign)),
358 359
			      "size",		GST_PROPS_INT (GUINT16_FROM_LE (strf->size))
			  ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

  avi_demux->audio_pad[avi_demux->num_audio_pads++] = srcpad;
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void 
gst_avi_demux_strf_iavs (GstAviDemux *avi_demux)
{
  gst_riff_strf_iavs *strf;
  GstPad *srcpad;
  GstByteStream *bs = avi_demux->bs;

  strf = (gst_riff_strf_iavs *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strf_iavs));

  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context iavs");
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxSrc   %08x", GUINT32_FROM_LE (strf->DVAAuxSrc));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxCtl   %08x", GUINT32_FROM_LE (strf->DVAAuxCtl));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxSrc1  %08x", GUINT32_FROM_LE (strf->DVAAuxSrc1));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVAAuxCtl1  %08x", GUINT32_FROM_LE (strf->DVAAuxCtl1));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVVAuxSrc   %08x", GUINT32_FROM_LE (strf->DVVAuxSrc));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVVAuxCtl   %08x", GUINT32_FROM_LE (strf->DVVAuxCtl));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVReserved1 %08x", GUINT32_FROM_LE (strf->DVReserved1));
  GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux:  DVReserved2 %08x", GUINT32_FROM_LE (strf->DVReserved2));

  srcpad =  gst_pad_new_from_template (
		  GST_PADTEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d", 
			  avi_demux->num_video_pads));

388 389
  gst_pad_try_set_caps (srcpad, 
		        GST_CAPS_NEW (
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
390 391 392 393 394 395 396 397 398 399
			  "avidec_iav_src",
			  "video/avi",
			    "format",    	GST_PROPS_STRING ("strf_iavs"),
                              "DVAAuxSrc", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxSrc)),
                              "DVAAuxCtl", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxCtl)),
                              "DVAAuxSrc1", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxSrc1)),
                              "DVAAuxCtl1", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxCtl1)),
                              "DVVAuxSrc", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVVAuxSrc)),
                              "DVVAuxCtl", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVVAuxCtl)),
                              "DVReserved1", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVReserved1)),
400 401
                              "DVReserved2", 	GST_PROPS_INT (GUINT32_FROM_LE (strf->DVReserved2))
			 ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

  avi_demux->video_pad[avi_demux->num_video_pads++] = srcpad;
  gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
}

static void
gst_avidemux_parse_index (GstAviDemux *avi_demux,
		            gulong offset)
{
  GstBuffer *buf;
  gulong index_size;

  buf = gst_pad_pullregion (avi_demux->sinkpad, GST_REGION_OFFSET_LEN, offset, 8);

  if (!buf || GST_BUFFER_OFFSET (buf) != offset || GST_BUFFER_SIZE (buf) != 8) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not get index");
    return;
  }

  if (gst_riff_fourcc_to_id (GST_BUFFER_DATA (buf)) != GST_RIFF_TAG_idx1) {
    GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: no index found");
    return;
  }

  index_size = GUINT32_FROM_LE(*(guint32 *)(GST_BUFFER_DATA (buf) + 4));

  buf = gst_pad_pullregion(avi_demux->sinkpad, GST_REGION_OFFSET_LEN, offset+8, index_size);

  avi_demux->index_size = index_size/sizeof(gst_riff_index_entry);

  GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: index size %lu", avi_demux->index_size);

  avi_demux->index_entries = g_malloc (GST_BUFFER_SIZE (buf));
  memcpy (avi_demux->index_entries, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));

  buf = gst_pad_pullregion(avi_demux->sinkpad, GST_REGION_OFFSET_LEN, avi_demux->index_offset, 0);
}

static void
gst_avidemux_forall_pads (GstAviDemux *avi_demux, GFunc func, gpointer user_data)
{
  gint i;
  GstPad *pad;
  
  for(i=0; i<GST_AVI_DEMUX_MAX_AUDIO_PADS; i++) {
    pad = avi_demux->audio_pad[i];
448
    if (pad && GST_PAD_IS_CONNECTED (pad)) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
449 450 451 452 453 454
      (*func) (pad, user_data);
    }
  }

  for(i=0; i<GST_AVI_DEMUX_MAX_VIDEO_PADS; i++) {
    pad = avi_demux->video_pad[i];
455
    if (pad && GST_PAD_IS_CONNECTED (pad)) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      (*func) (pad, user_data);
    }
  }
}

static gboolean
gst_avidemux_handle_event (GstAviDemux *avi_demux)
{
  guint32 remaining;
  GstEvent *event;
  GstEventType type;
  
  gst_bytestream_get_status (avi_demux->bs, &remaining, &event);

  type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;

  switch (type) {
    case GST_EVENT_EOS:
Wim Taymans's avatar
Wim Taymans committed
474
      gst_pad_event_default (avi_demux->sinkpad, event);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
      break;
    case GST_EVENT_SEEK:
      g_warning ("seek event\n");
      break;
    case GST_EVENT_FLUSH:
      g_warning ("flush event\n");
      break;
    case GST_EVENT_DISCONTINUOUS:
      g_warning ("discont event\n");
      break;
    default:
      g_warning ("unhandled event %d\n", type);
      break;
  }

  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
static inline gboolean 
gst_avidemux_read_chunk (GstAviDemux *avi_demux, guint32 *id, guint32 *size)
{
  gst_riff_chunk *chunk;
  GstByteStream *bs = avi_demux->bs;

  chunk = (gst_riff_chunk *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_chunk));
  if (chunk) {
    *id =   GUINT32_FROM_LE (chunk->id);
    *size = GUINT32_FROM_LE (chunk->size);

    gst_bytestream_flush (bs, sizeof (gst_riff_chunk));

    return TRUE;
  }
  return gst_avidemux_handle_event (avi_demux);
}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
511
static gboolean
512
gst_avidemux_process_chunk (GstAviDemux *avi_demux, guint64 *filepos,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
513 514 515 516 517 518
			     guint32 desired_tag,
			     gint rec_depth, guint32 *chunksize)
{
  guint32 chunkid;	
  GstByteStream *bs = avi_demux->bs;

Wim Taymans's avatar
Wim Taymans committed
519
  if (!gst_avidemux_read_chunk (avi_demux, &chunkid, chunksize)) {
520
    g_print ("  *****  Error reading chunk at filepos 0x%08llx\n", *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
521 522 523 524
    return FALSE;
  }
  if (desired_tag) {		/* do we have to test identity? */
    if (desired_tag != chunkid) {
525
      g_print ("\n\n *** Error: Expected chunk '%08x', found '%08x'\n",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
526 527 528 529 530 531
	      desired_tag, chunkid);
      return FALSE;
    }
  }

  GST_INFO (GST_CAT_PLUGIN_INFO, "chunkid %s, size %08x, filepos %08llx", 
532
		  gst_riff_id_to_fourcc (chunkid), *chunksize, *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
533

534
  *filepos += (sizeof (guint32) + sizeof (guint32));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
535 536 537 538 539 540

  switch (chunkid) {
    case GST_RIFF_TAG_RIFF:
    case GST_RIFF_TAG_LIST:
    {
      guint32 datashowed;
541
      guint32 subchunksize = 0;	/* size of a read subchunk             */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
542 543 544 545 546 547

      // flush the form type
      if (!gst_bytestream_flush (bs, sizeof (guint32)))
	return FALSE;

      datashowed = sizeof (guint32);	/* we showed the form type      */
548
      *filepos += datashowed;	/* for the rest of the routine  */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
549 550 551 552

      while (datashowed < *chunksize) {	/* while not showed all: */

	/* recurse for subchunks of RIFF and LIST chunks: */
553
	if (!gst_avidemux_process_chunk (avi_demux, filepos, 0,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
554 555 556
			   rec_depth + 1, &subchunksize))
	  return FALSE;

557
	subchunksize = ((subchunksize + 1) & ~1);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
558

559 560
	datashowed += (sizeof (guint32) + sizeof (guint32) + subchunksize);
	*filepos += subchunksize;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
561
      }
562 563 564 565
      if (datashowed != *chunksize) {
	g_warning ("error parsing AVI");
      }
      goto done;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    }
    case GST_RIFF_TAG_avih:
      gst_avi_demux_avih (avi_demux);
      break;
    case GST_RIFF_TAG_strh:
    {
      gst_avi_demux_strh (avi_demux);
      break;
    }
    case GST_RIFF_TAG_strf:
      switch (avi_demux->fcc_type) {
        case GST_RIFF_FCC_vids:
          gst_avi_demux_strf_vids (avi_demux);
	  break;
        case GST_RIFF_FCC_auds:
          gst_avi_demux_strf_auds (avi_demux);
	  break;
        case GST_RIFF_FCC_iavs:
          gst_avi_demux_strf_iavs (avi_demux);
	  break;
	case GST_RIFF_FCC_pads:
	case GST_RIFF_FCC_txts:
	default:
          GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux_chain: strh type %s not supported", gst_riff_id_to_fourcc (avi_demux->fcc_type));
	  break;
      }
      break;
    case GST_RIFF_00dc:
    case GST_RIFF_00db:
    case GST_RIFF_00__:
    {
      GST_DEBUG (0,"gst_avi_demux_chain: tag found %08x size %08x\n",
		    chunkid, *chunksize);

600
      if (GST_PAD_IS_CONNECTED (avi_demux->video_pad[0])) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
601 602 603 604 605 606 607 608 609 610 611 612
	GstBuffer *buf;

	if (*chunksize) {
          buf = gst_bytestream_peek (bs, *chunksize);
	}
	else {
          buf = gst_buffer_new ();
	}
        GST_BUFFER_TIMESTAMP (buf) = avi_demux->next_time;
        avi_demux->next_time += avi_demux->time_interval;

        if (avi_demux->video_need_flush[0]) {
Wim Taymans's avatar
Wim Taymans committed
613
           /* FIXME, do some flush event here */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
          avi_demux->video_need_flush[0] = FALSE;
        }

        GST_DEBUG (0,"gst_avi_demux_chain: send video buffer %08x\n", *chunksize);
        gst_pad_push(avi_demux->video_pad[0], buf);
        GST_DEBUG (0,"gst_avi_demux_chain: sent video buffer %08x %p\n",
	  	      *chunksize, &avi_demux->video_pad[0]);
        avi_demux->current_frame++;
      }
      *chunksize = (*chunksize + 1) & ~1;
      break;
    }
    case GST_RIFF_01wb:
    {
      GST_DEBUG (0,"gst_avi_demux_chain: tag found %08x size %08x\n",
		    chunkid, *chunksize);

631
      if (GST_PAD_IS_CONNECTED (avi_demux->audio_pad[0])) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
632 633 634 635 636 637 638 639 640 641 642 643
	GstBuffer *buf;

	if (*chunksize) {
          buf = gst_bytestream_peek (bs, *chunksize);
	}
	else {
          buf = gst_buffer_new ();
	}

        if (avi_demux->audio_need_flush[0]) {
  	  GST_DEBUG (0,"audio flush\n");
          avi_demux->audio_need_flush[0] = FALSE;
Wim Taymans's avatar
Wim Taymans committed
644
           /* FIXME, do some flush event here */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
645 646 647 648 649 650 651 652 653 654
        }

        GST_DEBUG (0,"gst_avi_demux_chain: send audio buffer %08x\n", *chunksize);
        gst_pad_push (avi_demux->audio_pad[0], buf);
        GST_DEBUG (0,"gst_avi_demux_chain: sent audio buffer %08x\n", *chunksize);
      }
      *chunksize = (*chunksize + 1) & ~1;
      break;
    }
    default:
655 656
      GST_DEBUG (0, "  *****  unknown chunkid %08x (%s)\n", chunkid, gst_riff_id_to_fourcc (chunkid));
      *chunksize = (*chunksize + 1) & ~1;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
657 658 659
      break;
  }
  GST_INFO (GST_CAT_PLUGIN_INFO, "chunkid %s, flush %08x, filepos %08llx", 
660
		  gst_riff_id_to_fourcc (chunkid), *chunksize, *filepos);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
661 662 663 664 665

  if (!gst_bytestream_flush (bs, *chunksize)) {
    return gst_avidemux_handle_event (avi_demux);
  }

666
done:
Wim Taymans's avatar
Wim Taymans committed
667 668 669 670
  /* we are running in an infinite loop, we need to _yield 
   * from time to time */
  gst_element_yield (GST_ELEMENT (avi_demux));

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
671 672 673 674 675 676 677 678
  return TRUE;
}

static void
gst_avi_demux_loop (GstElement *element)
{
  GstAviDemux *avi_demux;
  guint32 chunksize;
679
  guint64 filepos = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
680 681 682 683 684 685

  g_return_if_fail (element != NULL);
  g_return_if_fail (GST_IS_AVI_DEMUX (element));

  avi_demux = GST_AVI_DEMUX (element);

Wim Taymans's avatar
Wim Taymans committed
686
  /* this is basically an infinite loop */
687
  if (!gst_avidemux_process_chunk (avi_demux, &filepos, GST_RIFF_TAG_RIFF, 0, &chunksize)) {
Wim Taymans's avatar
Wim Taymans committed
688 689 690 691 692 693 694 695
    gst_element_error (element, "This doesn't appear to be an AVI file");
  }
}

static GstElementStateReturn
gst_avi_demux_change_state (GstElement *element)
{
  GstAviDemux *avi_demux = GST_AVI_DEMUX (element);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
696

Wim Taymans's avatar
Wim Taymans committed
697 698
  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_NULL_TO_READY:
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
699
      break;
Wim Taymans's avatar
Wim Taymans committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    case GST_STATE_READY_TO_PAUSED:
      avi_demux->bs = gst_bytestream_new (avi_demux->sinkpad);
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
      break;
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
      gst_bytestream_destroy (avi_demux->bs);
      break;
    case GST_STATE_READY_TO_NULL:
      break;
    default:
      break;
  }

  parent_class->change_state (element);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
717

Wim Taymans's avatar
Wim Taymans committed
718
  return GST_STATE_SUCCESS;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
}

static void
gst_avi_demux_get_property (GObject *object, guint prop_id, GValue *value,
			    GParamSpec *pspec)
{
  GstAviDemux *src;

  g_return_if_fail (GST_IS_AVI_DEMUX (object));

  src = GST_AVI_DEMUX (object);

  switch (prop_id) {
    case ARG_BITRATE:
      break;
    case ARG_MEDIA_TIME:
      g_value_set_long (value, (src->tot_frames * src->time_interval) / 1000000);
      break;
    case ARG_CURRENT_TIME:
      g_value_set_long (value, (src->current_frame * src->time_interval) / 1000000);
      break;
    default:
      break;
  }
}

static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstElementFactory *factory;
  GstTypeFactory *type;

  /* this filter needs the riff parser */
  if (!gst_library_load ("gstbytestream")) {
    gst_info("avidemux: could not load support library: 'gstbytestream'\n");
    return FALSE;
  }
  if (!gst_library_load ("gstriff")) {
    gst_info("avidemux: could not load support library: 'gstriff'\n");
    return FALSE;
  }

  /* create an elementfactory for the avi_demux element */
  factory = gst_elementfactory_new ("avidemux",GST_TYPE_AVI_DEMUX,
                                    &gst_avi_demux_details);
  g_return_val_if_fail (factory != NULL, FALSE);

  gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_audio_templ));
  gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_video_templ));
  gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_templ));

  type = gst_typefactory_new (&avidefinition);
  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));

  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

  return TRUE;
}

GstPluginDesc plugin_desc = {
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "avidemux",
  plugin_init
};