qtdemux.c 22.4 KB
Newer Older
Artyom Baginski's avatar
Artyom Baginski committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* GStreamer
 * 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.
 */

20 21

#include <stdlib.h>
Artyom Baginski's avatar
Artyom Baginski committed
22 23 24 25 26 27 28 29
#include <string.h>
#include "qtdemux.h"

/* elementfactory information */
static GstElementDetails 
gst_qtdemux_details = 
{
  "quicktime parser",
30
  "Codec/Parser",
31
  "LGPL",
Artyom Baginski's avatar
Artyom Baginski committed
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 69 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 103 104 105 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 157 158 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 207 208 209 210 211 212 213
  "Parses a quicktime stream into audio and video substreams",
  VERSION,
  "A.Baguinski <artm@v2.nl>",
  "(C) 2002",
};

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

/* typefactory for 'quicktime' */
static GstTypeDefinition quicktimedefinition = {
  "qtdemux_video/quicktime",
  "video/quicktime",
  ".mov",
  quicktime_type_find,
};

enum {
  LAST_SIGNAL
};

enum {
  ARG_0
};

GST_PAD_TEMPLATE_FACTORY (sink_templ,
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "qtdemux_sink",
     "video/quicktime",
     NULL
  )
);

/* 
 * so far i only support Photo Jpeg videos and no audio. 
 * after this one works ok, i'll see what's next.
 */
GST_PAD_TEMPLATE_FACTORY (src_video_templ,
  "video_%02d",
  GST_PAD_SRC,
  GST_PAD_SOMETIMES,
  GST_CAPS_NEW (
    "qtdemux_src_video",
    "video/jpeg",
      "width", GST_PROPS_INT_RANGE (16, 4096),
      "height", GST_PROPS_INT_RANGE (16, 4096)
  )
);

static GstElementClass *parent_class = NULL;
/* 
 * contains correspondence between atom types and
 * GstQtpAtomType structures 
 */
static GHashTable * gst_qtp_type_registry; 

typedef struct {
  guint32 type;
  GstQtpAtomType atype;
} GstQtpTypePair;

static void gst_qtp_trak_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_tkhd_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_hdlr_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_stsd_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_stts_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_stsc_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_stsz_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_stco_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_mdhd_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);
static void gst_qtp_mdat_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter);

GstQtpTypePair gst_qtp_type_table[] = {
  { GST_MAKE_FOURCC('m','o','o','v'), {GST_QTP_CONTAINER_ATOM,NULL} },
  { GST_MAKE_FOURCC('t','r','a','k'), {GST_QTP_CONTAINER_ATOM,gst_qtp_trak_handler} },
  { GST_MAKE_FOURCC('e','d','t','s'), {GST_QTP_CONTAINER_ATOM,NULL} },
  { GST_MAKE_FOURCC('m','d','i','a'), {GST_QTP_CONTAINER_ATOM,NULL} },
  { GST_MAKE_FOURCC('m','i','n','f'), {GST_QTP_CONTAINER_ATOM,NULL} },
  { GST_MAKE_FOURCC('d','i','n','f'), {GST_QTP_CONTAINER_ATOM,NULL} },
  { GST_MAKE_FOURCC('s','t','b','l'), {GST_QTP_CONTAINER_ATOM,NULL} },
  { GST_MAKE_FOURCC('m','d','a','t'), {0,gst_qtp_mdat_handler} },
  { GST_MAKE_FOURCC('m','v','h','d'), {0,NULL} },
  { GST_MAKE_FOURCC('t','k','h','d'), {0,gst_qtp_tkhd_handler} },
  { GST_MAKE_FOURCC('e','l','s','t'), {0,NULL} },
  { GST_MAKE_FOURCC('m','d','h','d'), {0,gst_qtp_mdhd_handler} },
  { GST_MAKE_FOURCC('h','d','l','r'), {0,gst_qtp_hdlr_handler} },
  { GST_MAKE_FOURCC('v','m','h','d'), {0,NULL} },
  { GST_MAKE_FOURCC('d','r','e','f'), {0,NULL} },
  { GST_MAKE_FOURCC('s','t','t','s'), {0,gst_qtp_stts_handler} },
  { GST_MAKE_FOURCC('s','t','s','d'), {0,gst_qtp_stsd_handler} },
  { GST_MAKE_FOURCC('s','t','s','z'), {0,gst_qtp_stsz_handler} },
  { GST_MAKE_FOURCC('s','t','s','c'), {0,gst_qtp_stsc_handler} },
  { GST_MAKE_FOURCC('s','t','c','o'), {0,gst_qtp_stco_handler} }
};

#define GST_QTP_TYPE_CNT sizeof(gst_qtp_type_table)/sizeof(GstQtpTypePair)

static void gst_qtdemux_class_init (GstQTDemuxClass *klass);
static void gst_qtdemux_init (GstQTDemux *quicktime_demux);
static void gst_qtdemux_loop (GstElement *element);
static GstElementStateReturn gst_qtdemux_change_state (GstElement * element);
static gint gst_guint32_compare(gconstpointer _a, gconstpointer _b);

static GType
gst_qtdemux_get_type (void) 
{
  static GType qtdemux_type = 0;

  if (!qtdemux_type) {
    static const GTypeInfo qtdemux_info = {
      sizeof(GstQTDemuxClass), NULL, NULL,
      (GClassInitFunc)gst_qtdemux_class_init,
      NULL, NULL, sizeof(GstQTDemux), 0,
      (GInstanceInitFunc)gst_qtdemux_init,
    };
    qtdemux_type = g_type_register_static (GST_TYPE_ELEMENT, "GstQTDemux", &qtdemux_info, 0);
  }
  return qtdemux_type;
}

static void
gst_qtdemux_class_init (GstQTDemuxClass *klass) 
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
  int i;

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

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

  gstelement_class->change_state = gst_qtdemux_change_state;

  gst_qtp_type_registry = g_hash_table_new(g_int_hash,g_int_equal);
  for(i=0;i<GST_QTP_TYPE_CNT;i++) {
    g_hash_table_insert(gst_qtp_type_registry,&(gst_qtp_type_table[i].type),&(gst_qtp_type_table[i].atype));
  }
}

static GstElementStateReturn
gst_qtdemux_change_state (GstElement * element)
{
  GstQTDemux * qtdemux = GST_QTDEMUX (element);

  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_READY_TO_PAUSED:
      qtdemux->bs = gst_bytestream_new (qtdemux->sinkpad);
      break;
    case GST_STATE_PAUSED_TO_READY:
      gst_bytestream_destroy (qtdemux->bs);
      break;
    default:
      break;
  }
  parent_class->change_state (element);
  return GST_STATE_SUCCESS;
}

static void 
gst_qtdemux_init (GstQTDemux *qtdemux) 
{
  guint i;
		
  qtdemux->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_templ), "sink");
  gst_element_set_loop_function (GST_ELEMENT (qtdemux), gst_qtdemux_loop);
  gst_element_add_pad (GST_ELEMENT (qtdemux), qtdemux->sinkpad);

  for (i=0; i<GST_QTDEMUX_MAX_VIDEO_PADS; i++) 
	  qtdemux->video_pad[i] = NULL;
  qtdemux->num_video_pads = 0;

  qtdemux->bs_pos = 0;
  qtdemux->nested = NULL;
  qtdemux->nested_cnt = 0;
  qtdemux->tracks = NULL;
  qtdemux->samples = NULL;
}

static GstCaps*
214
quicktime_type_find (GstBuffer *buf, gpointer private)
Artyom Baginski's avatar
Artyom Baginski committed
215 216 217
{
  gchar *data = GST_BUFFER_DATA (buf);

218 219 220 221
  /* we could get a NULL buffer, for example when the input could not be
   * mmap'd */
  g_return_val_if_fail (data != NULL, NULL);
  
Artyom Baginski's avatar
Artyom Baginski committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  /* exactly like in the old version */
  if (!strncmp (&data[4], "wide", 4) ||
      !strncmp (&data[4], "moov", 4) ||
      !strncmp (&data[4], "mdat", 4))  {
    return gst_caps_new ("quicktime_type_find",
		         "video/quicktime", 
			 NULL);
  }
  return NULL;
}

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

239
  if (!gst_library_load ("gstbytestream"))
Artyom Baginski's avatar
Artyom Baginski committed
240 241
    return FALSE;

242 243
  factory = gst_element_factory_new ("qtdemux", GST_TYPE_QTDEMUX,
                                     &gst_qtdemux_details);
Artyom Baginski's avatar
Artyom Baginski committed
244
  g_return_val_if_fail(factory != NULL, FALSE);
245
  gst_element_factory_set_rank (factory, GST_ELEMENT_RANK_PRIMARY);
Artyom Baginski's avatar
Artyom Baginski committed
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_templ));
  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_video_templ));

  type = gst_type_factory_new (&quicktimedefinition);
  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,
  "qtdemux",
  plugin_init
};

static gboolean
gst_qtdemux_handle_event (GstQTDemux * qtdemux)
{
  guint32 remaining;
  GstEvent * event;
  GstEventType type;

  gst_bytestream_get_status (qtdemux->bs,&remaining,&event);
  type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;

  switch (type) {
    case GST_EVENT_EOS:
      gst_pad_event_default (qtdemux->sinkpad,event);
      break;
    case GST_EVENT_DISCONTINUOUS:
      gst_bytestream_flush_fast (qtdemux->bs, remaining);
    default:
      gst_pad_event_default (qtdemux->sinkpad,event);
      break;
  }
  return TRUE;
}

static gboolean gst_qtp_read_bytes_atom_head(GstQTDemux * qtdemux,GstQtpAtom * atom);
static gboolean gst_qtp_skip(GstQTDemux * qtdemux, guint64 skip);
static gboolean gst_qtp_skip_atom(GstQTDemux * qtdemux, GstQtpAtom * atom);
static gboolean gst_qtp_skip_container(GstQTDemux * qtdemux, guint32 type);

/* new track emerges here */
static GstQtpTrack * track_to_be = NULL;

/* 
 * - gst_qtp_* functions together with gst_qtdemux_loop implement quicktime
 * parser.
 */

static void
gst_qtdemux_loop (GstElement *element)
{
  GstQTDemux * qtdemux = GST_QTDEMUX (element);
  GstQtpAtom atom;
  GstQtpAtomType * atom_type;

  /* ain't we out of the current container? */
  if (qtdemux->nested) {
    GstQtpAtom * current = (GstQtpAtom *) qtdemux->nested->data;
    while (current && (current->size!=0) && (current->start+current->size <= qtdemux->bs_pos)) {
      /* indeed we are! */
      qtdemux->nested = qtdemux->nested->next;
      qtdemux->nested_cnt--;
      /* if atom type has a handler call it with enter=FALSE (i.e. leave) */
      atom_type = g_hash_table_lookup (gst_qtp_type_registry,&(current->type));
      if (atom_type && atom_type->handler) 
	atom_type->handler(qtdemux,current,FALSE);
      free(current);
      current = qtdemux->nested?(GstQtpAtom *) qtdemux->nested->data:NULL;
    }
  }

  gst_qtp_read_bytes_atom_head(qtdemux,&atom);
325
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtdemux_loop: atom(%c%c%c%c,%" G_GUINT64_FORMAT ",%" G_GUINT64_FORMAT")\n",GST_FOURCC_TO_CHARSEQ(atom.type),atom.start,atom.size);
Artyom Baginski's avatar
Artyom Baginski committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

  atom_type = g_hash_table_lookup (gst_qtp_type_registry,&atom.type);
  if (!atom_type) {
    gst_qtp_skip_atom(qtdemux,&atom);
    return;
  }

  if (atom_type->flags & GST_QTP_CONTAINER_ATOM) {
    GstQtpAtom * new_atom;
    new_atom = malloc(sizeof(GstQtpAtom));
    memcpy(new_atom,&atom,sizeof(GstQtpAtom));
    qtdemux->nested_cnt++;
    qtdemux->nested = g_slist_prepend (qtdemux->nested, new_atom);
    if (atom_type->handler) 
      atom_type->handler(qtdemux,&atom,TRUE);
  } else {
    /* leaf atom */
    if (atom_type->handler)
      atom_type->handler(qtdemux,&atom,TRUE);
    /*
     * if there wasn't a handler - we skip the whole atom
     * if there was - ensure that next thing read will be after the atom
     * (handler isn't obligated to read anything)
     */
    gst_qtp_skip_atom(qtdemux,&atom);
    return;
  }
}

/* 
 * peeks an atom header,
 * advances qtdemux->bs_pos (cause bytestream won't tell)
 * flushes bytestream
 */
static gboolean
gst_qtp_read_bytes_atom_head(GstQTDemux * qtdemux,GstQtpAtom * atom)
{
  GstByteStream * bs = qtdemux->bs;
  GstQtpAtomMinHeader * amh = NULL;
  guint64 * esize=NULL;

  /* FIXME this can't be right, rewrite with _read */
  do { /* do ... while (event()) is necessary for bytestream events */
    if (!amh) {
Steve Baker Baker's avatar
Steve Baker Baker committed
370
      if (gst_bytestream_peek_bytes (bs, (guint8**)&amh, 8) == 8)  {
Artyom Baginski's avatar
Artyom Baginski committed
371 372 373 374 375 376 377 378 379
	atom->size = GUINT32_FROM_BE(amh->size);
	atom->type = amh->type; /* don't need to turn this around magicly FIXME this can depend on endiannes */
	atom->start = qtdemux->bs_pos;
	gst_bytestream_flush (bs, 8);
	qtdemux->bs_pos += 8;
      }
    }
    if (amh) {
      if (atom->size == 1) { /* need to peek extended size field */
Steve Baker Baker's avatar
Steve Baker Baker committed
380
	if (gst_bytestream_peek_bytes (bs, (guint8**)&esize, 8) == 8) {
Artyom Baginski's avatar
Artyom Baginski committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	  atom->size = GUINT64_FROM_BE(*esize);
	  gst_bytestream_flush (bs, 8);
	  qtdemux->bs_pos += 8;
	  return TRUE;
	}
      } else {
	return TRUE;
      }
    }
  } while (gst_qtdemux_handle_event (qtdemux));
  return TRUE;
}

static void
gst_qtp_read_bytes(GstQTDemux * qtdemux, void * buffer, size_t size)
{
  void * data;
  GstByteStream * bs = qtdemux->bs;
Steve Baker Baker's avatar
Steve Baker Baker committed
399

Artyom Baginski's avatar
Artyom Baginski committed
400
  do {
Steve Baker Baker's avatar
Steve Baker Baker committed
401
    if (gst_bytestream_peek_bytes (bs, (guint8**)&data, size) == size) {
Artyom Baginski's avatar
Artyom Baginski committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415
      memcpy(buffer,data,size);
      gst_bytestream_flush(bs,size);
      qtdemux->bs_pos += size;
      return;
    }
  } while (gst_qtdemux_handle_event (qtdemux));
}

static GstBuffer *
gst_qtp_read(GstQTDemux * qtdemux, size_t size)
{
  GstBuffer * buf;
  GstByteStream * bs = qtdemux->bs;
  do {
Steve Baker Baker's avatar
Steve Baker Baker committed
416
    if (gst_bytestream_read (bs, &buf, size) == size) {
Artyom Baginski's avatar
Artyom Baginski committed
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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
      qtdemux->bs_pos += size;
      return buf;
    }
  } while (gst_qtdemux_handle_event (qtdemux));
  return NULL;
}

/*
 * skips some input (e.g. to ignore unknown atom)
 */
static gboolean
gst_qtp_skip(GstQTDemux * qtdemux, guint64 skip)
{
  GstByteStream * bs = qtdemux->bs;
  
  if (skip) {
    gst_bytestream_flush(bs,skip);
    qtdemux->bs_pos += skip;
  }
  return TRUE;
}

/* convenience function for skipping the given atom */
static gboolean
gst_qtp_skip_atom(GstQTDemux * qtdemux, GstQtpAtom * atom)
{
  if (qtdemux->bs_pos < atom->start + atom->size) {
    guint64 skip = atom->start + atom->size - qtdemux->bs_pos;
    return gst_qtp_skip(qtdemux,skip);
  } else 
    return FALSE;
}

/* skips the container with type 'type' if finds it in the nesting stack */
static gboolean
gst_qtp_skip_container(GstQTDemux * qtdemux, guint32 type)
{
  GSList * iter = qtdemux->nested;

  while (iter && ((GstQtpAtom*)(iter->data))->type != type)
    iter = iter->next;

  if (iter) 
    return gst_qtp_skip_atom(qtdemux,(GstQtpAtom*)iter->data);
  else
    return FALSE;
}

static gint 
gst_guint32_compare(gconstpointer a, gconstpointer b)
{
  if ((guint32*)a < (guint32*)b) 
    return -1;
  else if ((guint32*)a > (guint32*)b)
    return 1;
  else
    return 0;
}

static void 
gst_qtp_trak_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  if (enter) { /* enter trak */
    GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_trak_handler: enter\n");
    track_to_be = malloc(sizeof(GstQtpTrack));
    track_to_be->stsd = NULL;
    track_to_be->stts = NULL;
    track_to_be->stsc = NULL;
    track_to_be->stsz = NULL;
    track_to_be->stco = NULL;
    track_to_be->samples = NULL;
    track_to_be->pad = NULL;
  } else { /* leave trak */
    GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_trak_handler: leave\n");
    if (track_to_be) { /* if we didnt discard this track earlier */
      GstQtpStscRec * stsc;
      guint32 * stsz, * stco, offset;
      int chunk,sample,nchunks,nsamples,stsc_idx,nstsc;
      GstCaps * newcaps = NULL;

      /* process sample tables */

      /*
       * FIXME have to check which sample tables are present and which are not
       * and skip the track if there's not enough tables or set default values
       * if some optional tables are missing
       */

      /*
       * FIXME i assume that there's only one of each stsd record and stts
       * record in the tables that's not always true, this must be changed
       * later, as soon as i encounter qt file with bigger tables.
       */
      track_to_be->format = ((GstQtpStsdRec*)GST_BUFFER_DATA(track_to_be->stsd))->format;
      GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_trak_handler: format: %c%c%c%c\n",GST_FOURCC_TO_CHARSEQ(track_to_be->format));
      track_to_be->sample_duration = GUINT32_FROM_BE(((GstQtpSttsRec*)GST_BUFFER_DATA(track_to_be->stts))->duration);
      GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_trak_handler: sample duration: %d\n",track_to_be->sample_duration);
      /* 
       * depending on format we can decide to refuse this track all together 
       * if we don't know what for format that it. 
       */
      switch (track_to_be->format) {
	case GST_MAKE_FOURCC('j','p','e','g'):
	  track_to_be->pad = gst_pad_new_from_template(
	      GST_PAD_TEMPLATE_GET(src_video_templ),
	      g_strdup_printf("video_%02d",qtdemux->num_video_pads++));
	  newcaps = GST_CAPS_NEW(
	      "qtdemux_video_src",
	      "video/jpeg",
	      "width", GST_PROPS_INT(track_to_be->width),
	      "height", GST_PROPS_INT(track_to_be->height));
	  gst_pad_try_set_caps(track_to_be->pad,newcaps);
	  gst_element_add_pad(GST_ELEMENT(qtdemux),track_to_be->pad);
	  break;
      }

      /* 
       * now let's find all about individual samples and put them into samples
       * tree
       */
      if (!qtdemux->samples) { 
	qtdemux->samples = g_tree_new(gst_guint32_compare);
      }
      stsc = (GstQtpStscRec*)GST_BUFFER_DATA(track_to_be->stsc);
      stsz = (guint32*)GST_BUFFER_DATA(track_to_be->stsz);
      stco = (guint32*)GST_BUFFER_DATA(track_to_be->stco);
      nchunks = GST_BUFFER_SIZE(track_to_be->stco)/sizeof(guint32);
      nsamples = GST_BUFFER_SIZE(track_to_be->stsz)/sizeof(guint32);
      nstsc = GST_BUFFER_SIZE(track_to_be->stsc)/sizeof(GstQtpStscRec);

      track_to_be->samples = malloc(nsamples*sizeof(GstQtpSample));
      for(chunk=0,sample=0,stsc_idx=0;
	  chunk<nchunks;
	  chunk++) {
	int i;
	offset = GUINT32_FROM_BE(stco[chunk]);
	if (stsc_idx+1<nstsc && chunk+1==GUINT32_FROM_BE(stsc[stsc_idx+1].first_chunk)) {
	  stsc_idx++;
	}
	for(i=0;i<GUINT32_FROM_BE(stsc[stsc_idx].samples_per_chunk);i++,sample++) {
	  guint32 size = GUINT32_FROM_BE(stsz[sample]);
	  track_to_be->samples[sample].offset = offset;
	  track_to_be->samples[sample].size = size;
	  track_to_be->samples[sample].timestamp = sample*((1000000*track_to_be->sample_duration)/track_to_be->time_scale);
	  track_to_be->samples[sample].track = track_to_be;
	  g_tree_insert(qtdemux->samples,&(track_to_be->samples[sample].offset),&(track_to_be->samples[sample]));
	  offset += size;
	}
      }

      GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_trak_handler: trak added to the list\n");
      qtdemux->tracks = g_list_prepend(qtdemux->tracks,track_to_be);

      gst_buffer_unref(track_to_be->stsd);
      gst_buffer_unref(track_to_be->stts);
      gst_buffer_unref(track_to_be->stsc);
      gst_buffer_unref(track_to_be->stsz);
      gst_buffer_unref(track_to_be->stco);
      track_to_be = 0;
    }
  }
}

/* 
 * weird formats they apple guys are using 
 * weird conversion copied from openquicktime 
 * FIXME either it can be done more beautiful/fast way or this fixme has to go
 */
static float 
fixed32_to_float(guint32 fixed)
{
  unsigned char * data = (unsigned char*)&fixed;
  guint32 a, b, c, d;
  a = data[0];
  b = data[1];
  c = data[2];
  d = data[3];
  a = (a << 8) + b;
  b = (c << 8) + d;
  return (float)a + (float)b / 65536;
}

static void 
gst_qtp_tkhd_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  guint32 wh[2];
  /* if we get here track_to_be must be not NULL */
  g_assert(track_to_be);

  gst_qtp_skip(qtdemux,76); /* don't need those values */
  gst_qtp_read_bytes(qtdemux,wh,8);
  track_to_be->width = (guint32) fixed32_to_float(wh[0]);
  track_to_be->height = (guint32) fixed32_to_float(wh[1]);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_tkhd_handler: track dimmensions: %dx%d\n",track_to_be->width,track_to_be->height);
}

static void
gst_qtp_hdlr_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  guint32 a[3];

  gst_qtp_read_bytes(qtdemux,a,12);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_hdlr_handler: %c%c%c%c %c%c%c%c\n",GST_FOURCC_TO_CHARSEQ(a[1]),GST_FOURCC_TO_CHARSEQ(a[2]));
  if (a[1]==GST_MAKE_FOURCC('m','h','l','r') && a[2]!=GST_MAKE_FOURCC('v','i','d','e')) {
    GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_hdlr_handler: rejecting the track\n");
    /* forget about this track! */
    free(track_to_be);
    track_to_be = NULL;
    gst_qtp_skip_container(qtdemux,GST_MAKE_FOURCC('t','r','a','k'));
  }
  return;
}

static void
gst_qtp_stsd_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  guint32 a[2];
  gst_qtp_read_bytes(qtdemux,a,8);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_stsd_handler: %d entries in the table\n",GUINT32_FROM_BE(a[1]));
  /* just put the rest of the atom into sample description table */
  track_to_be->stsd = gst_qtp_read(qtdemux,atom->start + atom->size - qtdemux->bs_pos);
}

static void
gst_qtp_stts_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  guint32 a[2];
  gst_qtp_read_bytes(qtdemux,a,8);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_stts_handler: %d entries in the table\n",GUINT32_FROM_BE(a[1]));
  track_to_be->stts = gst_qtp_read(qtdemux,GUINT32_FROM_BE(a[1])*sizeof(GstQtpSttsRec));
}
static void
gst_qtp_stsc_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  guint32 a[2];
  gst_qtp_read_bytes(qtdemux,a,8);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_stsc_handler: %d entries in the table\n",GUINT32_FROM_BE(a[1]));
  track_to_be->stsc = gst_qtp_read(qtdemux,GUINT32_FROM_BE(a[1])*sizeof(GstQtpStscRec));
}

static void
gst_qtp_stsz_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  guint32 a[3];
  gst_qtp_read_bytes(qtdemux,a,12);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_stsz_handler: %d entries in the table\n",GUINT32_FROM_BE(a[2]));
  /* FIXME have to chech a[2], it contains size if all samples if they are the same size */
  track_to_be->stsz = gst_qtp_read(qtdemux,GUINT32_FROM_BE(a[2])*sizeof(guint32));
}

static void
gst_qtp_stco_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter)
{
  guint32 a[2];
  gst_qtp_read_bytes(qtdemux,a,8);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_stco_handler: %d entries in the table\n",GUINT32_FROM_BE(a[1]));
  track_to_be->stco = gst_qtp_read(qtdemux,GUINT32_FROM_BE(a[1])*sizeof(guint32));
}

static void
gst_qtp_mdhd_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter) 
{
  guint32 a[4];
  gst_qtp_read_bytes(qtdemux,a,16);
  track_to_be->time_scale = GUINT32_FROM_BE(a[3]);
  GST_INFO (GST_CAT_PLUGIN_INFO,"gst_qtp_mdhd_handler: time scale: %d\n",track_to_be->time_scale);
}

static gboolean
gst_qtp_traverse(gpointer poffs,gpointer value,gpointer data)
{
  GstQtpSample * sample = (GstQtpSample*)value;
  GstQTDemux * qtdemux = (GstQTDemux*)data;

  if (qtdemux->bs_pos < sample->offset) {
    gst_qtp_skip(qtdemux,sample->offset - qtdemux->bs_pos);
693
    if (sample->track->pad && GST_PAD_IS_LINKED(sample->track->pad)) {
Artyom Baginski's avatar
Artyom Baginski committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
      GstBuffer * buf;
      buf = gst_qtp_read(qtdemux,sample->size);
      GST_BUFFER_TIMESTAMP(buf) = sample->timestamp;
      gst_pad_push(sample->track->pad,buf);
    }
  }
  return FALSE; /* == keep going (TRUE to stop) */
}

static void
gst_qtp_mdat_handler (GstQTDemux * qtdemux,GstQtpAtom * atom,gboolean enter) 
{
  /* actually playing */
  g_tree_foreach(qtdemux->samples,gst_qtp_traverse,qtdemux);
}