gstogmparse.c 22.7 KB
Newer Older
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
/* GStreamer
 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * gstogmparse.c: OGM stream header parsing (and data passthrough)
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include <gst/gst.h>
#include <gst/riff/riff-media.h>

GST_DEBUG_CATEGORY_STATIC (gst_ogm_parse_debug);
#define GST_CAT_DEFAULT gst_ogm_parse_debug

#define GST_TYPE_OGM_VIDEO_PARSE (gst_ogm_video_parse_get_type())
35 36 37
#define GST_IS_OGM_VIDEO_PARSE(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_VIDEO_PARSE))

38
#define GST_TYPE_OGM_AUDIO_PARSE (gst_ogm_audio_parse_get_type())
39 40
#define GST_IS_OGM_AUDIO_PARSE(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_AUDIO_PARSE))
41

42 43 44 45
#define GST_TYPE_OGM_TEXT_PARSE (gst_ogm_text_parse_get_type())
#define GST_IS_OGM_TEXT_PARSE(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_TEXT_PARSE))

46 47 48 49 50 51 52 53 54
#define GST_TYPE_OGM_PARSE (gst_ogm_parse_get_type())
#define GST_OGM_PARSE(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_OGM_PARSE, GstOgmParse))
#define GST_OGM_PARSE_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_OGM_PARSE, GstOgmParse))
#define GST_IS_OGM_PARSE(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_OGM_PARSE))
#define GST_IS_OGM_PARSE_CLASS(obj) \
  (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_OGM_PARSE))
55 56
#define GST_OGM_PARSE_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_OGM_PARSE, GstOgmParseClass))
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

typedef struct _stream_header_video
{
  gint32 width;
  gint32 height;
} stream_header_video;

typedef struct _stream_header_audio
{
  gint16 channels;
  gint16 blockalign;
  gint32 avgbytespersec;
} stream_header_audio;

typedef struct _stream_header
{
  gchar streamtype[8];
  gchar subtype[4];

  /* size of the structure */
  gint32 size;

  /* in reference time */
  gint64 time_unit;

  gint64 samples_per_unit;

  /* in media time */
  gint32 default_len;

  gint32 buffersize;
  gint32 bits_per_sample;

  union
  {
    stream_header_video video;
    stream_header_audio audio;
94
    /* text has no additional data */
95 96 97 98 99 100 101 102 103
  } s;
} stream_header;

typedef struct _GstOgmParse
{
  GstElement element;

  /* pads */
  GstPad *srcpad, *sinkpad;
104
  GstPadTemplate *srcpadtempl;
105 106 107

  /* audio or video */
  stream_header hdr;
108 109 110

  /* expected next granulepos (used for timestamp guessing) */
  guint64 next_granulepos;
111 112 113 114 115 116 117 118 119 120 121 122 123
} GstOgmParse;

typedef struct _GstOgmParseClass
{
  GstElementClass parent_class;
} GstOgmParseClass;

static GstStaticPadTemplate ogm_video_parse_sink_template_factory =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-ogm-video"));
static GstStaticPadTemplate ogm_audio_parse_sink_template_factory =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-ogm-audio"));
124 125 126 127
static GstStaticPadTemplate ogm_text_parse_sink_template_factory =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-ogm-text"));
static GstPadTemplate *video_src_templ, *audio_src_templ, *text_src_templ;
128 129 130

static GType gst_ogm_audio_parse_get_type (void);
static GType gst_ogm_video_parse_get_type (void);
131
static GType gst_ogm_text_parse_get_type (void);
132 133 134 135
static GType gst_ogm_parse_get_type (void);

static void gst_ogm_audio_parse_base_init (GstOgmParseClass * klass);
static void gst_ogm_video_parse_base_init (GstOgmParseClass * klass);
136
static void gst_ogm_text_parse_base_init (GstOgmParseClass * klass);
137 138
static void gst_ogm_parse_class_init (GstOgmParseClass * klass);
static void gst_ogm_parse_init (GstOgmParse * ogm);
139 140
static void gst_ogm_video_parse_init (GstOgmParse * ogm);
static void gst_ogm_audio_parse_init (GstOgmParse * ogm);
141
static void gst_ogm_text_parse_init (GstOgmParse * ogm);
142

Wim Taymans's avatar
Wim Taymans committed
143
#if 0
144
static const GstFormat *gst_ogm_parse_get_sink_formats (GstPad * pad);
Wim Taymans's avatar
Wim Taymans committed
145 146
#endif

147
static const GstQueryType *gst_ogm_parse_get_sink_querytypes (GstPad * pad);
Wim Taymans's avatar
Wim Taymans committed
148
static gboolean gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query);
149 150 151
static gboolean gst_ogm_parse_sink_convert (GstPad * pad, GstFormat src_format,
    gint64 src_value, GstFormat * dest_format, gint64 * dest_value);

152
static GstFlowReturn gst_ogm_parse_chain (GstPad * pad, GstBuffer * buffer);
153

154 155
static GstStateChangeReturn gst_ogm_parse_change_state (GstElement * element,
    GstStateChange transition);
156

157
static GstElementClass *parent_class = NULL;
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

static GType
gst_ogm_parse_get_type (void)
{
  static GType ogm_parse_type = 0;

  if (!ogm_parse_type) {
    static const GTypeInfo ogm_parse_info = {
      sizeof (GstOgmParseClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_ogm_parse_class_init,
      NULL,
      NULL,
      sizeof (GstOgmParse),
      0,
      (GInstanceInitFunc) gst_ogm_parse_init,
    };

    ogm_parse_type =
        g_type_register_static (GST_TYPE_ELEMENT,
        "GstOgmParse", &ogm_parse_info, 0);
  }

  return ogm_parse_type;
}

static GType
gst_ogm_audio_parse_get_type (void)
{
  static GType ogm_audio_parse_type = 0;

  if (!ogm_audio_parse_type) {
    static const GTypeInfo ogm_audio_parse_info = {
      sizeof (GstOgmParseClass),
      (GBaseInitFunc) gst_ogm_audio_parse_base_init,
      NULL,
      NULL,
      NULL,
      NULL,
      sizeof (GstOgmParse),
      0,
200
      (GInstanceInitFunc) gst_ogm_audio_parse_init,
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    };

    ogm_audio_parse_type =
        g_type_register_static (GST_TYPE_OGM_PARSE,
        "GstOgmAudioParse", &ogm_audio_parse_info, 0);
  }

  return ogm_audio_parse_type;
}

GType
gst_ogm_video_parse_get_type (void)
{
  static GType ogm_video_parse_type = 0;

  if (!ogm_video_parse_type) {
    static const GTypeInfo ogm_video_parse_info = {
      sizeof (GstOgmParseClass),
      (GBaseInitFunc) gst_ogm_video_parse_base_init,
      NULL,
      NULL,
      NULL,
      NULL,
      sizeof (GstOgmParse),
      0,
226
      (GInstanceInitFunc) gst_ogm_video_parse_init,
227 228 229 230 231 232 233 234 235 236
    };

    ogm_video_parse_type =
        g_type_register_static (GST_TYPE_OGM_PARSE,
        "GstOgmVideoParse", &ogm_video_parse_info, 0);
  }

  return ogm_video_parse_type;
}

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
GType
gst_ogm_text_parse_get_type (void)
{
  static GType ogm_text_parse_type = 0;

  if (!ogm_text_parse_type) {
    static const GTypeInfo ogm_text_parse_info = {
      sizeof (GstOgmParseClass),
      (GBaseInitFunc) gst_ogm_text_parse_base_init,
      NULL,
      NULL,
      NULL,
      NULL,
      sizeof (GstOgmParse),
      0,
      (GInstanceInitFunc) gst_ogm_text_parse_init,
    };

    ogm_text_parse_type =
        g_type_register_static (GST_TYPE_OGM_PARSE,
        "GstOgmTextParse", &ogm_text_parse_info, 0);
  }

  return ogm_text_parse_type;
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
static void
gst_ogm_audio_parse_base_init (GstOgmParseClass * klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  static GstElementDetails gst_ogm_audio_parse_details =
      GST_ELEMENT_DETAILS ("OGM audio stream parser",
      "Codec/Decoder/Audio",
      "parse an OGM audio header and stream",
      "Ronald Bultje <rbultje@ronald.bitfreak.net>");
  GstCaps *caps = gst_riff_create_audio_template_caps ();

  gst_element_class_set_details (element_class, &gst_ogm_audio_parse_details);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&ogm_audio_parse_sink_template_factory));
  audio_src_templ = gst_pad_template_new ("src",
279
      GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  gst_element_class_add_pad_template (element_class, audio_src_templ);
}

static void
gst_ogm_video_parse_base_init (GstOgmParseClass * klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  static GstElementDetails gst_ogm_video_parse_details =
      GST_ELEMENT_DETAILS ("OGM video stream parser",
      "Codec/Decoder/Video",
      "parse an OGM video header and stream",
      "Ronald Bultje <rbultje@ronald.bitfreak.net>");
  GstCaps *caps = gst_riff_create_video_template_caps ();

  gst_element_class_set_details (element_class, &gst_ogm_video_parse_details);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&ogm_video_parse_sink_template_factory));
  video_src_templ = gst_pad_template_new ("src",
299
      GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
300 301 302
  gst_element_class_add_pad_template (element_class, video_src_templ);
}

303 304 305 306 307 308 309 310 311
static void
gst_ogm_text_parse_base_init (GstOgmParseClass * klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  static GstElementDetails gst_ogm_text_parse_details =
      GST_ELEMENT_DETAILS ("OGM text stream parser",
      "Codec/Decoder/Subtitle",
      "parse an OGM text header and stream",
      "Ronald Bultje <rbultje@ronald.bitfreak.net>");
312
  GstCaps *caps = gst_caps_new_simple ("text/plain", NULL, NULL);
313 314 315 316 317 318 319 320 321 322

  gst_element_class_set_details (element_class, &gst_ogm_text_parse_details);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&ogm_text_parse_sink_template_factory));
  text_src_templ = gst_pad_template_new ("src",
      GST_PAD_SRC, GST_PAD_SOMETIMES, caps);
  gst_element_class_add_pad_template (element_class, text_src_templ);
}

323 324 325 326 327 328 329 330 331 332 333 334
static void
gst_ogm_parse_class_init (GstOgmParseClass * klass)
{
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

  gstelement_class->change_state = gst_ogm_parse_change_state;
}

static void
gst_ogm_parse_init (GstOgmParse * ogm)
335 336 337 338
{
  /* initalize */
  memset (&ogm->hdr, 0, sizeof (ogm->hdr));
  ogm->next_granulepos = 0;
339
  ogm->srcpad = NULL;
340 341 342 343
}

static void
gst_ogm_audio_parse_init (GstOgmParse * ogm)
344 345
{
  /* create the pads */
346
  ogm->sinkpad =
347
      gst_pad_new_from_static_template (&ogm_audio_parse_sink_template_factory,
348
      "sink");
Wim Taymans's avatar
Wim Taymans committed
349
  gst_pad_set_query_function (ogm->sinkpad, gst_ogm_parse_sink_query);
350 351 352
  gst_pad_set_chain_function (ogm->sinkpad, gst_ogm_parse_chain);
  gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);

353 354
#if 0
  ogm->srcpad = gst_pad_new_from_template (audio_src_templ, "src");
355 356
  gst_pad_use_explicit_caps (ogm->srcpad);
  gst_element_add_pad (GST_ELEMENT (ogm), ogm->srcpad);
357
#endif
358
  ogm->srcpad = NULL;
359 360
  ogm->srcpadtempl = audio_src_templ;
}
361

362 363 364 365
static void
gst_ogm_video_parse_init (GstOgmParse * ogm)
{
  /* create the pads */
366 367 368
  ogm->sinkpad =
      gst_pad_new_from_static_template (&ogm_video_parse_sink_template_factory,
      "sink");
Wim Taymans's avatar
Wim Taymans committed
369
  gst_pad_set_query_function (ogm->sinkpad, gst_ogm_parse_sink_query);
370 371 372 373 374 375 376 377
  gst_pad_set_chain_function (ogm->sinkpad, gst_ogm_parse_chain);
  gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);

#if 0
  ogm->srcpad = gst_pad_new_from_template (video_src_templ, "src");
  gst_pad_use_explicit_caps (ogm->srcpad);
  gst_element_add_pad (GST_ELEMENT (ogm), ogm->srcpad);
#endif
378
  ogm->srcpad = NULL;
379
  ogm->srcpadtempl = video_src_templ;
380 381
}

382 383 384 385
static void
gst_ogm_text_parse_init (GstOgmParse * ogm)
{
  /* create the pads */
386 387 388
  ogm->sinkpad =
      gst_pad_new_from_static_template (&ogm_text_parse_sink_template_factory,
      "sink");
389 390 391 392 393 394 395 396 397 398 399
  gst_pad_set_query_type_function (ogm->sinkpad,
      gst_ogm_parse_get_sink_querytypes);
  gst_pad_set_query_function (ogm->sinkpad, gst_ogm_parse_sink_query);
  gst_pad_set_chain_function (ogm->sinkpad, gst_ogm_parse_chain);
  gst_element_add_pad (GST_ELEMENT (ogm), ogm->sinkpad);

#if 0
  ogm->srcpad = gst_pad_new_from_template (text_src_templ, "src");
  gst_pad_use_explicit_caps (ogm->srcpad);
  gst_element_add_pad (GST_ELEMENT (ogm), ogm->srcpad);
#endif
400
  ogm->srcpad = NULL;
401 402 403
  ogm->srcpadtempl = text_src_templ;
}

Wim Taymans's avatar
Wim Taymans committed
404
#if 0
405 406 407 408 409 410 411 412 413 414 415
static const GstFormat *
gst_ogm_parse_get_sink_formats (GstPad * pad)
{
  static GstFormat formats[] = {
    GST_FORMAT_DEFAULT,
    GST_FORMAT_TIME,
    0
  };

  return formats;
}
Wim Taymans's avatar
Wim Taymans committed
416
#endif
417

418 419 420 421 422 423 424 425 426 427 428
static const GstQueryType *
gst_ogm_parse_get_sink_querytypes (GstPad * pad)
{
  static const GstQueryType types[] = {
    GST_QUERY_POSITION,
    0
  };

  return types;
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442
static gboolean
gst_ogm_parse_sink_convert (GstPad * pad,
    GstFormat src_format, gint64 src_value,
    GstFormat * dest_format, gint64 * dest_value)
{
  gboolean res = FALSE;
  GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));

  switch (src_format) {
    case GST_FORMAT_DEFAULT:
      switch (*dest_format) {
        case GST_FORMAT_TIME:
          switch (ogm->hdr.streamtype[0]) {
            case 'a':
443 444
              *dest_value = GST_SECOND * src_value / ogm->hdr.samples_per_unit;
              res = TRUE;
445 446
              break;
            case 'v':
447
            case 't':
448 449 450 451 452 453 454 455 456 457 458 459
              *dest_value = (GST_SECOND / 10000000) *
                  ogm->hdr.time_unit * src_value;
              res = TRUE;
              break;
            default:
              break;
          }
          break;
        default:
          break;
      }
      break;
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    case GST_FORMAT_TIME:
      switch (*dest_format) {
        case GST_FORMAT_DEFAULT:
          switch (ogm->hdr.streamtype[0]) {
            case 'a':
              *dest_value = ogm->hdr.samples_per_unit * src_value / GST_SECOND;
              res = TRUE;
              break;
            case 'v':
            case 't':
              *dest_value = src_value /
                  ((GST_SECOND / 10000000) * ogm->hdr.time_unit);
              res = TRUE;
              break;
            default:
              break;
          }
          break;
        default:
          break;
      }
      break;
482 483 484 485 486 487 488
    default:
      break;
  }

  return res;
}

489
static gboolean
Wim Taymans's avatar
Wim Taymans committed
490
gst_ogm_parse_sink_query (GstPad * pad, GstQuery * query)
491
{
Wim Taymans's avatar
Wim Taymans committed
492 493 494 495 496 497 498 499
  GstOgmParse *ogm = GST_OGM_PARSE (GST_PAD_PARENT (pad));
  GstFormat format;
  gboolean res;

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_POSITION:
    {
      gint64 val;
500

Wim Taymans's avatar
Wim Taymans committed
501
      gst_query_parse_position (query, &format, NULL);
502

Wim Taymans's avatar
Wim Taymans committed
503 504 505 506 507 508
      if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME)
        return FALSE;

      if ((res = gst_ogm_parse_sink_convert (pad,
                  GST_FORMAT_DEFAULT, ogm->next_granulepos, &format, &val))) {
        /* don't know the total length here.. */
Wim Taymans's avatar
Wim Taymans committed
509
        gst_query_set_position (query, format, val);
Wim Taymans's avatar
Wim Taymans committed
510 511 512 513 514 515 516 517 518
      }
      break;
    }
    case GST_QUERY_CONVERT:
    {
      GstFormat src_fmt, dest_fmt;
      gint64 src_val, dest_val;

      /* peel off input */
519
      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
Wim Taymans's avatar
Wim Taymans committed
520 521 522 523 524 525 526
      if ((res = gst_ogm_parse_sink_convert (pad, src_fmt, src_val,
                  &dest_fmt, &dest_val))) {
        gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      }
      break;
    }
    default:
527
      res = gst_pad_query_default (pad, query);
Wim Taymans's avatar
Wim Taymans committed
528 529 530
      break;
  }
  return res;
531 532
}

533 534
static GstFlowReturn
gst_ogm_parse_chain (GstPad * pad, GstBuffer * buffer)
535 536
{
  GstOgmParse *ogm = GST_OGM_PARSE (gst_pad_get_parent (pad));
537
  GstBuffer *buf = GST_BUFFER (buffer);
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
  guint8 *data = GST_BUFFER_DATA (buf);
  guint size = GST_BUFFER_SIZE (buf);

  GST_DEBUG_OBJECT (ogm, "New packet with packet start code 0x%02x", data[0]);

  switch (data[0]) {
    case 0x01:{
      GstCaps *caps = NULL;

      /* stream header */
      if (size < sizeof (stream_header) + 1) {
        GST_ELEMENT_ERROR (ogm, STREAM, WRONG_TYPE,
            ("Buffer too small"), (NULL));
        break;
      }

      if (!memcmp (&data[1], "video\000\000\000", 8)) {
        ogm->hdr.s.video.width = GST_READ_UINT32_LE (&data[45]);
        ogm->hdr.s.video.height = GST_READ_UINT32_LE (&data[49]);
      } else if (!memcmp (&data[1], "audio\000\000\000", 8)) {
        ogm->hdr.s.audio.channels = GST_READ_UINT32_LE (&data[45]);
        ogm->hdr.s.audio.blockalign = GST_READ_UINT32_LE (&data[47]);
        ogm->hdr.s.audio.avgbytespersec = GST_READ_UINT32_LE (&data[49]);
561 562
      } else if (!memcmp (&data[1], "text\000\000\000\000", 8)) {
        /* nothing here */
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
      } else {
        GST_ELEMENT_ERROR (ogm, STREAM, WRONG_TYPE,
            ("Unknown stream type"), (NULL));
        break;
      }
      memcpy (ogm->hdr.streamtype, &data[1], 8);
      memcpy (ogm->hdr.subtype, &data[9], 4);
      ogm->hdr.size = GST_READ_UINT32_LE (&data[13]);
      ogm->hdr.time_unit = GST_READ_UINT64_LE (&data[17]);
      ogm->hdr.samples_per_unit = GST_READ_UINT64_LE (&data[25]);
      ogm->hdr.default_len = GST_READ_UINT32_LE (&data[33]);
      ogm->hdr.buffersize = GST_READ_UINT32_LE (&data[37]);
      ogm->hdr.bits_per_sample = GST_READ_UINT32_LE (&data[41]);

      switch (ogm->hdr.streamtype[0]) {
        case 'a':{
579 580 581 582 583 584
          guint codec_id;

          if (!sscanf (ogm->hdr.subtype, "%04x", &codec_id)) {
            caps = NULL;
            break;
          }
585 586
          caps = gst_riff_create_audio_caps (codec_id,
              NULL, NULL, NULL, NULL, NULL);
587 588 589 590 591 592 593 594
          gst_caps_set_simple (caps,
              "channels", G_TYPE_INT, ogm->hdr.s.audio.channels,
              "rate", G_TYPE_INT, ogm->hdr.samples_per_unit, NULL);
          GST_LOG_OBJECT (ogm, "Type: %s, subtype: 0x%04x, "
              "channels: %d, samplerate: %d, blockalign: %d, bps: %d",
              ogm->hdr.streamtype, codec_id, ogm->hdr.s.audio.channels,
              ogm->hdr.samples_per_unit,
              ogm->hdr.s.audio.blockalign, ogm->hdr.s.audio.avgbytespersec);
595 596 597 598 599 600 601
          break;
        }
        case 'v':{
          guint32 fcc;

          fcc = GST_MAKE_FOURCC (ogm->hdr.subtype[0],
              ogm->hdr.subtype[1], ogm->hdr.subtype[2], ogm->hdr.subtype[3]);
602
          GST_LOG_OBJECT (ogm, "Type: %s, subtype: %" GST_FOURCC_FORMAT
603 604 605 606 607 608 609 610
              ", size: %dx%d, timeunit: %" G_GINT64_FORMAT
              " (fps: %lf), s/u: %" G_GINT64_FORMAT ", "
              "def.len: %d, bufsize: %d, bps: %d",
              ogm->hdr.streamtype, GST_FOURCC_ARGS (fcc),
              ogm->hdr.s.video.width, ogm->hdr.s.video.height,
              ogm->hdr.time_unit, 10000000. / ogm->hdr.time_unit,
              ogm->hdr.samples_per_unit, ogm->hdr.default_len,
              ogm->hdr.buffersize, ogm->hdr.bits_per_sample);
611
          caps = gst_riff_create_video_caps (fcc, NULL, NULL, NULL, NULL, NULL);
612 613 614
          gst_caps_set_simple (caps,
              "width", G_TYPE_INT, ogm->hdr.s.video.width,
              "height", G_TYPE_INT, ogm->hdr.s.video.height,
615 616
              "framerate", GST_TYPE_FRACTION, 10000000, ogm->hdr.time_unit,
              NULL);
617 618
          break;
        }
619 620 621 622 623 624 625
        case 't':
          GST_LOG_OBJECT (ogm, "Type: %s, s/u: %" G_GINT64_FORMAT
              ", timeunit=%" G_GINT64_FORMAT,
              ogm->hdr.streamtype, ogm->hdr.samples_per_unit,
              ogm->hdr.time_unit);
          caps = gst_caps_new_simple ("text/plain", NULL);
          break;
626 627 628 629
        default:
          g_assert_not_reached ();
      }

630 631 632 633 634 635 636
      if (ogm->srcpad) {
        GST_WARNING_OBJECT (ogm, "Already an existing pad %s:%s",
            GST_DEBUG_PAD_NAME (ogm->srcpad));
        gst_element_remove_pad (GST_ELEMENT (ogm), ogm->srcpad);
        gst_object_unref (ogm->srcpad);

      }
Edward Hervey's avatar
Edward Hervey committed
637 638 639 640 641 642 643 644
      if (caps) {
        ogm->srcpad = gst_pad_new ("src", GST_PAD_SRC);
        gst_pad_set_caps (ogm->srcpad, caps);
      } else {
        GST_WARNING_OBJECT (ogm,
            "No fixed caps were found, carrying on with template");
        ogm->srcpad = gst_pad_new_from_template (ogm->srcpadtempl, "src");
      }
645
      gst_element_add_pad (GST_ELEMENT (ogm), ogm->srcpad);
646 647 648 649 650 651 652 653 654
      break;
    }
    case 0x03:
      /* comment - unused */
      break;
    default:
      if ((data[0] & 0x01) == 0) {
        /* data - push on */
        guint len = ((data[0] & 0xc0) >> 6) | ((data[0] & 0x02) << 1);
655
        guint xsize = 0, n;
656 657 658 659 660 661 662 663
        GstBuffer *sbuf;
        gboolean keyframe = (data[0] & 0x08) >> 3;

        if (size < len + 1) {
          GST_ELEMENT_ERROR (ogm, STREAM, WRONG_TYPE,
              ("Buffer too small"), (NULL));
          break;
        }
664 665
        for (n = len; n > 0; n--) {
          xsize = (xsize << 8) | data[n];
666 667 668
        }

        GST_DEBUG_OBJECT (ogm,
669 670 671 672 673 674
            "[0x%02x] samples: %d, hdrbytes: %d, datasize: %d",
            data[0], xsize, len, size - len - 1);
        sbuf = gst_buffer_create_sub (buf, len + 1, size - len - 1);
        if (GST_BUFFER_OFFSET_END_IS_VALID (buf)) {
          ogm->next_granulepos = GST_BUFFER_OFFSET_END (buf);
        }
675
        switch (ogm->hdr.streamtype[0]) {
676 677 678 679
          case 't':
          case 'v':{
            gint samples = (ogm->hdr.streamtype[0] == 'v') ? 1 : xsize;

680
            if (!keyframe)
681
              GST_BUFFER_FLAG_SET (sbuf, GST_BUFFER_FLAG_DELTA_UNIT);
682

683 684
            GST_BUFFER_TIMESTAMP (sbuf) = (GST_SECOND / 10000000) *
                ogm->next_granulepos * ogm->hdr.time_unit;
685
            GST_BUFFER_DURATION (sbuf) = (GST_SECOND / 10000000) *
686 687
                ogm->hdr.time_unit * samples;
            ogm->next_granulepos += samples;
688
            break;
689
          }
690
          case 'a':
691 692 693 694 695
            GST_BUFFER_TIMESTAMP (sbuf) = GST_SECOND *
                ogm->next_granulepos / ogm->hdr.samples_per_unit;
            GST_BUFFER_DURATION (sbuf) = GST_SECOND * xsize /
                ogm->hdr.samples_per_unit;
            ogm->next_granulepos += xsize;
696 697
            break;
          default:
698 699 700 701
            gst_buffer_unref (sbuf);
            sbuf = NULL;
            GST_ELEMENT_ERROR (ogm, RESOURCE, SYNC, (NULL), (NULL));
            break;
702
        }
Edward Hervey's avatar
Edward Hervey committed
703
        gst_buffer_set_caps (sbuf, GST_PAD_CAPS (ogm->srcpad));
704
        gst_pad_push (ogm->srcpad, sbuf);
705 706 707 708 709 710 711 712
      } else {
        GST_ELEMENT_ERROR (ogm, STREAM, WRONG_TYPE,
            ("Wrong packet startcode 0x%02x", data[0]), (NULL));
      }
      break;
  }

  gst_buffer_unref (buf);
713 714

  return GST_FLOW_OK;
715 716
}

717 718
static GstStateChangeReturn
gst_ogm_parse_change_state (GstElement * element, GstStateChange transition)
719 720 721
{
  GstOgmParse *ogm = GST_OGM_PARSE (element);

722 723
  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
724 725 726 727
      if (ogm->srcpad) {
        gst_element_remove_pad (element, ogm->srcpad);
        ogm->srcpad = NULL;
      }
728
      memset (&ogm->hdr, 0, sizeof (ogm->hdr));
729
      ogm->next_granulepos = 0;
730 731 732 733 734
      break;
    default:
      break;
  }

735
  return parent_class->change_state (element, transition);
736 737 738 739 740 741 742
}

gboolean
gst_ogm_parse_plugin_init (GstPlugin * plugin)
{
  GST_DEBUG_CATEGORY_INIT (gst_ogm_parse_debug, "ogmparse", 0, "ogm parser");

743
  return gst_element_register (plugin, "ogmaudioparse", GST_RANK_PRIMARY,
744 745 746 747 748
      GST_TYPE_OGM_AUDIO_PARSE) &&
      gst_element_register (plugin, "ogmvideoparse", GST_RANK_PRIMARY,
      GST_TYPE_OGM_VIDEO_PARSE) &&
      gst_element_register (plugin, "ogmtextparse", GST_RANK_PRIMARY,
      GST_TYPE_OGM_TEXT_PARSE);
749
}