gsttheoradec.c 24.5 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
1 2
/* GStreamer
 * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
3 4 5
 * Copyright (c) 2012 Collabora Ltd.
 *	Author : Edward Hervey <edward@collabora.com>
 *      Author : Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
Benjamin Otte's avatar
Benjamin Otte committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */

Wim Taymans's avatar
Wim Taymans committed
23 24 25 26 27 28 29 30
/**
 * SECTION:element-theoradec
 * @see_also: theoraenc, oggdemux
 *
 * This element decodes theora streams into raw video
 * <ulink url="http://www.theora.org/">Theora</ulink> is a royalty-free
 * video codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
 * Foundation</ulink>, based on the VP3 codec.
31 32
 *
 * <refsect2>
Wim Taymans's avatar
Wim Taymans committed
33
 * <title>Example pipeline</title>
34
 * |[
Wim Taymans's avatar
Wim Taymans committed
35
 * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! xvimagesink
36
 * ]| This example pipeline will decode an ogg stream and decodes the theora video. Refer to
Wim Taymans's avatar
Wim Taymans committed
37 38 39 40 41 42
 * the theoraenc example to create the ogg file.
 * </refsect2>
 *
 * Last reviewed on 2006-03-01 (0.10.4)
 */

Benjamin Otte's avatar
Benjamin Otte committed
43 44 45 46
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

Wim Taymans's avatar
Wim Taymans committed
47
#include "gsttheoradec.h"
Benjamin Otte's avatar
Benjamin Otte committed
48
#include <gst/tag/tag.h>
49
#include <gst/video/video.h>
Benjamin Otte's avatar
Benjamin Otte committed
50

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
51
#define GST_CAT_DEFAULT theoradec_debug
52
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
Benjamin Otte's avatar
Benjamin Otte committed
53

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54
#define THEORA_DEF_CROP         TRUE
55 56 57 58 59
#define THEORA_DEF_TELEMETRY_MV 0
#define THEORA_DEF_TELEMETRY_MBMODE 0
#define THEORA_DEF_TELEMETRY_QI 0
#define THEORA_DEF_TELEMETRY_BITS 0

60 61
enum
{
62
  PROP_0,
63 64 65 66 67
  PROP_CROP,
  PROP_TELEMETRY_MV,
  PROP_TELEMETRY_MBMODE,
  PROP_TELEMETRY_QI,
  PROP_TELEMETRY_BITS
68 69
};

Benjamin Otte's avatar
Benjamin Otte committed
70
static GstStaticPadTemplate theora_dec_src_factory =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
71 72 73 74
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-yuv, "
75
        "format = (fourcc) { I420, Y42B, Y444 }, "
76
        "framerate = (fraction) [0/1, MAX], "
77
        "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78
    );
Benjamin Otte's avatar
Benjamin Otte committed
79 80

static GstStaticPadTemplate theora_dec_sink_factory =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
81 82 83 84 85
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-theora")
    );
Benjamin Otte's avatar
Benjamin Otte committed
86

87 88
GST_BOILERPLATE (GstTheoraDec, gst_theora_dec, GstVideoDecoder,
    GST_TYPE_VIDEO_DECODER);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89

90 91 92 93 94
static void theora_dec_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void theora_dec_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);

95 96 97 98 99 100 101 102 103 104 105 106
static gboolean theora_dec_start (GstVideoDecoder * decoder);
static gboolean theora_dec_stop (GstVideoDecoder * decoder);
static gboolean theora_dec_set_format (GstVideoDecoder * decoder,
    GstVideoCodecState * state);
static gboolean theora_dec_reset (GstVideoDecoder * decoder, gboolean hard);
static GstFlowReturn theora_dec_parse (GstVideoDecoder * decoder,
    GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
static GstFlowReturn theora_dec_handle_frame (GstVideoDecoder * decoder,
    GstVideoCodecFrame * frame);

static GstFlowReturn theora_dec_decode_buffer (GstTheoraDec * dec,
    GstBuffer * buf, GstVideoCodecFrame * frame);
Benjamin Otte's avatar
Benjamin Otte committed
107 108 109 110 111 112


static void
gst_theora_dec_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
113

114 115 116 117
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&theora_dec_src_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&theora_dec_sink_factory));
118 119 120 121
  gst_element_class_set_details_simple (element_class,
      "Theora video decoder", "Codec/Decoder/Video",
      "decode raw theora streams to raw YUV video",
      "Benjamin Otte <otte@gnome.org>, Wim Taymans <wim@fluendo.com>");
Benjamin Otte's avatar
Benjamin Otte committed
122 123
}

124 125 126 127 128 129 130
static gboolean
gst_theora_dec_ctl_is_supported (int req)
{
  /* should return TH_EFAULT or TH_EINVAL if supported, and TH_EIMPL if not */
  return (th_decode_ctl (NULL, req, NULL, 0) != TH_EIMPL);
}

Benjamin Otte's avatar
Benjamin Otte committed
131
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
132
gst_theora_dec_class_init (GstTheoraDecClass * klass)
Benjamin Otte's avatar
Benjamin Otte committed
133
{
134
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135
  GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
Benjamin Otte's avatar
Benjamin Otte committed
136

137 138 139
  gobject_class->set_property = theora_dec_set_property;
  gobject_class->get_property = theora_dec_get_property;

140
  g_object_class_install_property (gobject_class, PROP_CROP,
141 142
      g_param_spec_boolean ("crop", "Crop",
          "Crop the image to the visible region", THEORA_DEF_CROP,
143
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
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
  if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_MV)) {
    g_object_class_install_property (gobject_class, PROP_TELEMETRY_MV,
        g_param_spec_int ("visualize-motion-vectors",
            "Visualize motion vectors",
            "Show motion vector selection overlaid on image. "
            "Value gives a mask for motion vector (MV) modes to show",
            0, 0xffff, THEORA_DEF_TELEMETRY_MV,
            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  }

  if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_MBMODE)) {
    g_object_class_install_property (gobject_class, PROP_TELEMETRY_MBMODE,
        g_param_spec_int ("visualize-macroblock-modes",
            "Visualize macroblock modes",
            "Show macroblock mode selection overlaid on image. "
            "Value gives a mask for macroblock (MB) modes to show",
            0, 0xffff, THEORA_DEF_TELEMETRY_MBMODE,
            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  }

  if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_QI)) {
    g_object_class_install_property (gobject_class, PROP_TELEMETRY_QI,
        g_param_spec_int ("visualize-quantization-modes",
            "Visualize adaptive quantization modes",
            "Show adaptive quantization mode selection overlaid on image. "
            "Value gives a mask for quantization (QI) modes to show",
            0, 0xffff, THEORA_DEF_TELEMETRY_QI,
            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  }

  if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_BITS)) {
    /* FIXME: make this a boolean instead? The value scales the bars so
     * they're less wide. Default is to use full width, and anything else
     * doesn't seem particularly useful, since the smaller bars just disappear
     * then (they almost disappear for a value of 2 already). */
    g_object_class_install_property (gobject_class, PROP_TELEMETRY_BITS,
        g_param_spec_int ("visualize-bit-usage",
            "Visualize bitstream usage breakdown",
            "Sets the bitstream breakdown visualization mode. "
            "Values influence the width of the bit usage bars to show",
            0, 0xff, THEORA_DEF_TELEMETRY_BITS,
            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  }
188

189 190 191 192 193 194 195
  video_decoder_class->start = GST_DEBUG_FUNCPTR (theora_dec_start);
  video_decoder_class->stop = GST_DEBUG_FUNCPTR (theora_dec_stop);
  video_decoder_class->reset = GST_DEBUG_FUNCPTR (theora_dec_reset);
  video_decoder_class->set_format = GST_DEBUG_FUNCPTR (theora_dec_set_format);
  video_decoder_class->parse = GST_DEBUG_FUNCPTR (theora_dec_parse);
  video_decoder_class->handle_frame =
      GST_DEBUG_FUNCPTR (theora_dec_handle_frame);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
196 197

  GST_DEBUG_CATEGORY_INIT (theoradec_debug, "theoradec", 0, "Theora decoder");
Benjamin Otte's avatar
Benjamin Otte committed
198 199 200
}

static void
201
gst_theora_dec_init (GstTheoraDec * dec, GstTheoraDecClass * g_class)
Benjamin Otte's avatar
Benjamin Otte committed
202
{
203
  dec->crop = THEORA_DEF_CROP;
204 205 206 207
  dec->telemetry_mv = THEORA_DEF_TELEMETRY_MV;
  dec->telemetry_mbmode = THEORA_DEF_TELEMETRY_MBMODE;
  dec->telemetry_qi = THEORA_DEF_TELEMETRY_QI;
  dec->telemetry_bits = THEORA_DEF_TELEMETRY_BITS;
208 209 210

  /* input is packetized,
   * but is not marked that way so data gets parsed and keyframes marked */
Benjamin Otte's avatar
Benjamin Otte committed
211
}
212

213 214 215 216
static void
gst_theora_dec_reset (GstTheoraDec * dec)
{
  dec->need_keyframe = TRUE;
217 218
}

219
static gboolean
220
theora_dec_start (GstVideoDecoder * decoder)
221
{
222
  GstTheoraDec *dec = GST_THEORA_DEC (decoder);
Wim Taymans's avatar
Wim Taymans committed
223

224 225 226 227 228 229
  GST_DEBUG_OBJECT (dec, "start");
  th_info_clear (&dec->info);
  th_comment_clear (&dec->comment);
  GST_DEBUG_OBJECT (dec, "Setting have_header to FALSE");
  dec->have_header = FALSE;
  gst_theora_dec_reset (dec);
230

231
  return TRUE;
232 233
}

Benjamin Otte's avatar
Benjamin Otte committed
234
static gboolean
235
theora_dec_stop (GstVideoDecoder * decoder)
Benjamin Otte's avatar
Benjamin Otte committed
236
{
237 238 239 240 241 242 243 244 245 246 247 248 249
  GstTheoraDec *dec = GST_THEORA_DEC (decoder);

  GST_DEBUG_OBJECT (dec, "stop");
  th_info_clear (&dec->info);
  th_comment_clear (&dec->comment);
  th_setup_free (dec->setup);
  dec->setup = NULL;
  th_decode_free (dec->decoder);
  dec->decoder = NULL;
  gst_theora_dec_reset (dec);
  if (dec->tags) {
    gst_tag_list_free (dec->tags);
    dec->tags = NULL;
Benjamin Otte's avatar
Benjamin Otte committed
250
  }
251

252
  return TRUE;
Benjamin Otte's avatar
Benjamin Otte committed
253
}
254

255
/* FIXME : Do we want to handle hard resets differently ? */
256
static gboolean
257
theora_dec_reset (GstVideoDecoder * bdec, gboolean hard)
258
{
259 260
  gst_theora_dec_reset (GST_THEORA_DEC (bdec));
  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
261 262
}

263 264 265
static GstFlowReturn
theora_dec_parse (GstVideoDecoder * decoder,
    GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos)
Benjamin Otte's avatar
Benjamin Otte committed
266
{
267 268
  gint av;
  const guint8 *data;
Benjamin Otte's avatar
Benjamin Otte committed
269

270
  av = gst_adapter_available (adapter);
Benjamin Otte's avatar
Benjamin Otte committed
271

272 273 274 275
  data = gst_adapter_peek (adapter, 1);
  /* check for keyframe; must not be header packet */
  if (!(data[0] & 0x80) && (data[0] & 0x40) == 0)
    GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
276

277 278 279
  /* and pass along all */
  gst_video_decoder_add_to_frame (decoder, av);
  return gst_video_decoder_have_frame (decoder);
280 281
}

Benjamin Otte's avatar
Benjamin Otte committed
282

283
static gboolean
284
theora_dec_set_format (GstVideoDecoder * bdec, GstVideoCodecState * state)
285 286 287
{
  GstTheoraDec *dec;

288
  dec = GST_THEORA_DEC (bdec);
289

290 291 292 293
  /* Keep a copy of the input state */
  if (dec->input_state)
    gst_video_codec_state_unref (dec->input_state);
  dec->input_state = gst_video_codec_state_ref (state);
294

295 296 297 298 299 300
  /* FIXME : Interesting, we always accept any kind of caps ? */
  if (state->codec_data) {
    GstBuffer *buffer;
    guint8 *data;
    guint size;
    guint offset;
301

302
    buffer = state->codec_data;
303

304 305 306
    offset = 0;
    size = GST_BUFFER_SIZE (buffer);
    data = GST_BUFFER_DATA (buffer);
307

308 309 310
    while (size > 2) {
      guint psize;
      GstBuffer *buf;
311

312 313 314 315 316
      psize = (data[0] << 8) | data[1];
      /* skip header */
      data += 2;
      size -= 2;
      offset += 2;
317

318 319
      /* make sure we don't read too much */
      psize = MIN (psize, size);
320

321
      buf = gst_buffer_create_sub (buffer, offset, psize);
322

323 324 325
      /* first buffer is a discont buffer */
      if (offset == 2)
        GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
326

327 328 329
      /* now feed it to the decoder we can ignore the error */
      theora_dec_decode_buffer (dec, buf, NULL);
      gst_buffer_unref (buf);
330

331 332 333 334
      /* skip the data */
      size -= psize;
      data += psize;
      offset += psize;
335 336 337
    }
  }

338
  GST_DEBUG_OBJECT (dec, "Done");
339 340 341 342

  return TRUE;
}

343
static GstFlowReturn
344
theora_handle_comment_packet (GstTheoraDec * dec, ogg_packet * packet)
Benjamin Otte's avatar
Benjamin Otte committed
345
{
346
  gchar *encoder = NULL;
Benjamin Otte's avatar
Benjamin Otte committed
347
  GstBuffer *buf;
348 349
  GstTagList *list;

350
  GST_DEBUG_OBJECT (dec, "parsing comment packet");
351

352 353 354
  buf = gst_buffer_new ();
  GST_BUFFER_SIZE (buf) = packet->bytes;
  GST_BUFFER_DATA (buf) = packet->packet;
355

356 357
  list =
      gst_tag_list_from_vorbiscomment_buffer (buf, (guint8 *) "\201theora", 7,
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
      &encoder);

  gst_buffer_unref (buf);

  if (!list) {
    GST_ERROR_OBJECT (dec, "couldn't decode comments");
    list = gst_tag_list_new ();
  }
  if (encoder) {
    gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
        GST_TAG_ENCODER, encoder, NULL);
    g_free (encoder);
  }
  gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
      GST_TAG_ENCODER_VERSION, dec->info.version_major,
      GST_TAG_VIDEO_CODEC, "Theora", NULL);

375 376 377 378 379 380
  if (dec->info.target_bitrate > 0) {
    gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
        GST_TAG_BITRATE, dec->info.target_bitrate,
        GST_TAG_NOMINAL_BITRATE, dec->info.target_bitrate, NULL);
  }

381 382
  if (dec->tags)
    gst_tag_list_free (dec->tags);
383
  dec->tags = list;
384 385 386 387 388 389 390 391

  return GST_FLOW_OK;
}

static GstFlowReturn
theora_handle_type_packet (GstTheoraDec * dec, ogg_packet * packet)
{
  gint par_num, par_den;
392
  GstFlowReturn ret = GST_FLOW_OK;
393 394 395
  GstVideoCodecState *state;
  GstVideoFormat fmt;
  GstVideoInfo *info = &dec->input_state->info;
396 397 398 399 400 401 402

  GST_DEBUG_OBJECT (dec, "fps %d/%d, PAR %d/%d",
      dec->info.fps_numerator, dec->info.fps_denominator,
      dec->info.aspect_numerator, dec->info.aspect_denominator);

  /* calculate par
   * the info.aspect_* values reflect PAR;
403
   * 0:x and x:0 are allowed and can be interpreted as 1:1.
404
   */
405 406 407 408 409 410
  par_num = GST_VIDEO_INFO_PAR_N (info);
  par_den = GST_VIDEO_INFO_PAR_D (info);

  /* If we have a default PAR, see if the decoder specified a different one */
  if (par_num == 1 && par_den == 1 &&
      (dec->info.aspect_numerator != 0 && dec->info.aspect_denominator != 0)) {
411 412 413
    par_num = dec->info.aspect_numerator;
    par_den = dec->info.aspect_denominator;
  }
414 415 416
  /* theora has:
   *
   *  width/height : dimension of the encoded frame 
417 418
   *  pic_width/pic_height : dimension of the visible part
   *  pic_x/pic_y : offset in encoded frame where visible part starts
419
   */
420 421
  GST_DEBUG_OBJECT (dec, "dimension %dx%d, PAR %d/%d", dec->info.pic_width,
      dec->info.pic_height, par_num, par_den);
422
  GST_DEBUG_OBJECT (dec, "frame dimension %dx%d, offset %d:%d",
423 424
      dec->info.pic_width, dec->info.pic_height,
      dec->info.pic_x, dec->info.pic_y);
425

426 427 428 429 430 431 432 433 434 435 436 437
  switch (dec->info.pixel_fmt) {
    case TH_PF_420:
      fmt = GST_VIDEO_FORMAT_I420;
      break;
    case TH_PF_422:
      fmt = GST_VIDEO_FORMAT_Y42B;
      break;
    case TH_PF_444:
      fmt = GST_VIDEO_FORMAT_Y444;
      break;
    default:
      goto unsupported_format;
438
  }
439 440

  if (dec->crop) {
441 442
    GST_VIDEO_INFO_WIDTH (info) = dec->info.pic_width;
    GST_VIDEO_INFO_HEIGHT (info) = dec->info.pic_height;
443 444 445 446 447 448 449
    dec->offset_x = dec->info.pic_x;
    dec->offset_y = dec->info.pic_y;
    /* Ensure correct offsets in chroma for formats that need it
     * by rounding the offset. libtheora will add proper pixels,
     * so no need to handle them ourselves. */
    if (dec->offset_x & 1 && dec->info.pixel_fmt != TH_PF_444) {
      dec->offset_x--;
450
      GST_VIDEO_INFO_WIDTH (info)++;
451 452 453
    }
    if (dec->offset_y & 1 && dec->info.pixel_fmt == TH_PF_420) {
      dec->offset_y--;
454
      GST_VIDEO_INFO_HEIGHT (info)++;
455 456 457
    }
  } else {
    /* no cropping, use the encoded dimensions */
458 459
    GST_VIDEO_INFO_WIDTH (info) = dec->info.frame_width;
    GST_VIDEO_INFO_HEIGHT (info) = dec->info.frame_height;
460 461 462 463 464
    dec->offset_x = 0;
    dec->offset_y = 0;
  }

  GST_DEBUG_OBJECT (dec, "after fixup frame dimension %dx%d, offset %d:%d",
465
      info->width, info->height, dec->offset_x, dec->offset_y);
466 467

  /* done */
468
  dec->decoder = th_decode_alloc (&dec->info, dec->setup);
469

470
  if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_MV,
471
          &dec->telemetry_mv, sizeof (dec->telemetry_mv)) != TH_EIMPL) {
472
    GST_WARNING_OBJECT (dec, "Could not enable MV visualisation");
473
  }
474
  if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_MBMODE,
475
          &dec->telemetry_mbmode, sizeof (dec->telemetry_mbmode)) != TH_EIMPL) {
476
    GST_WARNING_OBJECT (dec, "Could not enable MB mode visualisation");
477
  }
478
  if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_QI,
479
          &dec->telemetry_qi, sizeof (dec->telemetry_qi)) != TH_EIMPL) {
480
    GST_WARNING_OBJECT (dec, "Could not enable QI mode visualisation");
481
  }
482
  if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_BITS,
483
          &dec->telemetry_bits, sizeof (dec->telemetry_bits)) != TH_EIMPL) {
484
    GST_WARNING_OBJECT (dec, "Could not enable BITS mode visualisation");
485
  }
486

487 488 489 490
  /* Create the output state */
  dec->output_state = state =
      gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec), fmt,
      info->width, info->height, dec->input_state);
491

492 493 494 495 496
  /* FIXME : Do we still need to set fps/par now that we pass the reference input stream ? */
  state->info.fps_n = dec->info.fps_numerator;
  state->info.fps_d = dec->info.fps_denominator;
  state->info.par_n = par_num;
  state->info.par_d = par_den;
Wim Taymans's avatar
Wim Taymans committed
497

498 499 500 501 502
  state->info.chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
  /* FIXME : Need to specify SDTV color-matrix ... once it's handled
   * with the backported GstVideoInfo */

  dec->have_header = TRUE;
503

504 505
  /* FIXME : Put this on the next outgoing frame */
  /* FIXME :  */
506
  if (dec->tags) {
507 508
    gst_element_found_tags_for_pad (GST_ELEMENT_CAST (dec),
        GST_VIDEO_DECODER_SRC_PAD (dec), dec->tags);
509 510 511 512
    dec->tags = NULL;
  }

  return ret;
513 514 515 516 517 518 519

  /* ERRORS */
unsupported_format:
  {
    GST_ERROR_OBJECT (dec, "Invalid pixel format %d", dec->info.pixel_fmt);
    return GST_FLOW_ERROR;
  }
520 521 522 523 524 525
}

static GstFlowReturn
theora_handle_header_packet (GstTheoraDec * dec, ogg_packet * packet)
{
  GstFlowReturn res;
526
  int ret;
527

528
  GST_DEBUG_OBJECT (dec, "parsing header packet");
529

530 531
  ret = th_decode_headerin (&dec->info, &dec->comment, &dec->setup, packet);
  if (ret < 0)
532 533
    goto header_read_error;

Wim Taymans's avatar
Wim Taymans committed
534 535
  switch (packet->packet[0]) {
    case 0x81:
536 537
      res = theora_handle_comment_packet (dec, packet);
      break;
Wim Taymans's avatar
Wim Taymans committed
538
    case 0x82:
539 540 541 542
      res = theora_handle_type_packet (dec, packet);
      break;
    default:
      /* ignore */
Wim Taymans's avatar
Wim Taymans committed
543 544 545
      g_warning ("unknown theora header packet found");
    case 0x80:
      /* nothing special, this is the identification header */
546 547 548 549 550 551 552 553 554 555 556 557 558 559
      res = GST_FLOW_OK;
      break;
  }
  return res;

  /* ERRORS */
header_read_error:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
        (NULL), ("couldn't read header packet"));
    return GST_FLOW_ERROR;
  }
}

Michael Smith's avatar
Michael Smith committed
560 561
/* Allocate buffer and copy image data into Y444 format */
static GstFlowReturn
562 563
theora_handle_image (GstTheoraDec * dec, th_ycbcr_buffer buf,
    GstVideoCodecFrame * frame)
Michael Smith's avatar
Michael Smith committed
564
{
565 566
  GstVideoDecoder *decoder = GST_VIDEO_DECODER (dec);
  GstVideoInfo *info;
567
  gint width, height, stride;
Michael Smith's avatar
Michael Smith committed
568 569
  GstFlowReturn result;
  int i, plane;
570
  guint8 *dest, *src;
571
  GstBuffer *out;
Michael Smith's avatar
Michael Smith committed
572

573
  result = gst_video_decoder_alloc_output_frame (decoder, frame);
Michael Smith's avatar
Michael Smith committed
574

575
  if (G_UNLIKELY (result != GST_FLOW_OK)) {
576 577 578 579 580
    GST_DEBUG_OBJECT (dec, "could not get buffer, reason: %s",
        gst_flow_get_name (result));
    return result;
  }

581 582 583 584
  out = frame->output_buffer;
  info = &dec->output_state->info;

  /* FIXME : Use GstVideoInfo */
585
  for (plane = 0; plane < 3; plane++) {
586 587 588
    width = GST_VIDEO_INFO_COMP_WIDTH (info, plane);
    height = GST_VIDEO_INFO_COMP_HEIGHT (info, plane);
    stride = GST_VIDEO_INFO_COMP_STRIDE (info, plane);
589

590
    dest = GST_BUFFER_DATA (out) + GST_VIDEO_INFO_COMP_OFFSET (info, plane);
591
    src = buf[plane].data;
592 593 594
    src +=
        ((height ==
            GST_VIDEO_INFO_HEIGHT (info)) ? dec->offset_y : dec->offset_y / 2)
595
        * buf[plane].stride;
596 597 598
    src +=
        (width ==
        GST_VIDEO_INFO_WIDTH (info)) ? dec->offset_x : dec->offset_x / 2;
599 600

    for (i = 0; i < height; i++) {
601
      memcpy (dest, src, width);
602

603 604
      dest += stride;
      src += buf[plane].stride;
605 606
    }
  }
Wim Taymans's avatar
Wim Taymans committed
607

608
  return GST_FLOW_OK;
609 610 611 612
}

static GstFlowReturn
theora_handle_data_packet (GstTheoraDec * dec, ogg_packet * packet,
613
    GstVideoCodecFrame * frame)
614 615
{
  /* normal data packet */
616
  th_ycbcr_buffer buf;
617 618
  gboolean keyframe;
  GstFlowReturn result;
619
  ogg_int64_t gp;
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

  if (G_UNLIKELY (!dec->have_header))
    goto not_initialized;

  /* the second most significant bit of the first data byte is cleared 
   * for keyframes. We can only check it if it's not a zero-length packet. */
  keyframe = packet->bytes && ((packet->packet[0] & 0x40) == 0);
  if (G_UNLIKELY (keyframe)) {
    GST_DEBUG_OBJECT (dec, "we have a keyframe");
    dec->need_keyframe = FALSE;
  } else if (G_UNLIKELY (dec->need_keyframe)) {
    goto dropping;
  }

  GST_DEBUG_OBJECT (dec, "parsing data packet");

  /* this does the decoding */
637
  if (G_UNLIKELY (th_decode_packetin (dec->decoder, packet, &gp) < 0))
638 639
    goto decode_error;

640 641 642 643
  if (frame &&
      (gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (dec),
              frame) < 0))
    goto dropping_qos;
644 645 646

  /* this does postprocessing and set up the decoded frame
   * pointers in our yuv variable */
647
  if (G_UNLIKELY (th_decode_ycbcr_out (dec->decoder, buf) < 0))
648 649
    goto no_yuv;

650 651
  if (G_UNLIKELY ((buf[0].width != dec->info.frame_width)
          || (buf[0].height != dec->info.frame_height)))
652 653
    goto wrong_dimensions;

654
  result = theora_handle_image (dec, buf, frame);
655 656
  if (result != GST_FLOW_OK)
    return result;
657 658 659 660 661 662 663

  return result;

  /* ERRORS */
not_initialized:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
Wim Taymans's avatar
Wim Taymans committed
664
        (NULL), ("no header sent yet"));
665 666 667 668 669
    return GST_FLOW_ERROR;
  }
dropping:
  {
    GST_WARNING_OBJECT (dec, "dropping frame because we need a keyframe");
670
    return GST_VIDEO_DECODER_FLOW_NEED_DATA;
671
  }
672 673 674
dropping_qos:
  {
    GST_WARNING_OBJECT (dec, "dropping frame because of QoS");
675
    return GST_VIDEO_DECODER_FLOW_NEED_DATA;
676 677
  }
decode_error:
678 679
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
680
        (NULL), ("theora decoder did not decode data packet"));
681 682
    return GST_FLOW_ERROR;
  }
683
no_yuv:
684 685 686 687 688 689 690
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
        (NULL), ("couldn't read out YUV image"));
    return GST_FLOW_ERROR;
  }
wrong_dimensions:
  {
691
    GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, FORMAT,
692 693 694 695 696 697
        (NULL), ("dimensions of image do not match header"));
    return GST_FLOW_ERROR;
  }
}

static GstFlowReturn
698 699
theora_dec_decode_buffer (GstTheoraDec * dec, GstBuffer * buf,
    GstVideoCodecFrame * frame)
700
{
Benjamin Otte's avatar
Benjamin Otte committed
701
  ogg_packet packet;
702
  GstFlowReturn result = GST_FLOW_OK;
Benjamin Otte's avatar
Benjamin Otte committed
703 704 705 706

  /* make ogg_packet out of the buffer */
  packet.packet = GST_BUFFER_DATA (buf);
  packet.bytes = GST_BUFFER_SIZE (buf);
Wim Taymans's avatar
Wim Taymans committed
707
  packet.granulepos = -1;
Wim Taymans's avatar
Wim Taymans committed
708 709
  packet.packetno = 0;          /* we don't really care */
  packet.b_o_s = dec->have_header ? 0 : 1;
710
  /* EOS does not matter for the decoder */
711
  packet.e_o_s = 0;
712

713
  GST_LOG_OBJECT (dec, "decode buffer of size %ld", packet.bytes);
714

715
  GST_DEBUG_OBJECT (dec, "header=%02x", packet.bytes ? packet.packet[0] : -1);
716

717 718 719
  /* switch depending on packet type. A zero byte packet is always a data
   * packet; we don't dereference it in that case. */
  if (packet.bytes && packet.packet[0] & 0x80) {
Wim Taymans's avatar
Wim Taymans committed
720
    if (dec->have_header) {
721
      GST_WARNING_OBJECT (GST_OBJECT (dec), "Ignoring header");
722
      goto done;
723
    }
724
    result = theora_handle_header_packet (dec, &packet);
725 726 727 728 729
    /* header packets are not meant to be displayed */
    /* FIXME : This is a temporary hack. The proper fix would be to
     * not call _finish_frame() for these types of packets */
    GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
        GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
Benjamin Otte's avatar
Benjamin Otte committed
730
  } else {
731
    result = theora_handle_data_packet (dec, &packet, frame);
Benjamin Otte's avatar
Benjamin Otte committed
732
  }
733

734
done:
735
  return result;
Benjamin Otte's avatar
Benjamin Otte committed
736 737
}

738
static GstFlowReturn
739
theora_dec_handle_frame (GstVideoDecoder * bdec, GstVideoCodecFrame * frame)
740 741 742 743
{
  GstTheoraDec *dec;
  GstFlowReturn res;

744
  dec = GST_THEORA_DEC (bdec);
745

746 747 748
  res = theora_dec_decode_buffer (dec, frame->input_buffer, frame);
  if (res == GST_FLOW_OK)
    res = gst_video_decoder_finish_frame (bdec, frame);
749 750 751 752

  return res;
}

753 754 755 756 757 758 759
static void
theora_dec_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstTheoraDec *dec = GST_THEORA_DEC (object);

  switch (prop_id) {
760
    case PROP_CROP:
761 762
      dec->crop = g_value_get_boolean (value);
      break;
763 764 765 766 767 768 769 770 771 772 773 774
    case PROP_TELEMETRY_MV:
      dec->telemetry_mv = g_value_get_int (value);
      break;
    case PROP_TELEMETRY_MBMODE:
      dec->telemetry_mbmode = g_value_get_int (value);
      break;
    case PROP_TELEMETRY_QI:
      dec->telemetry_qi = g_value_get_int (value);
      break;
    case PROP_TELEMETRY_BITS:
      dec->telemetry_bits = g_value_get_int (value);
      break;
775 776 777 778 779 780 781 782 783 784 785 786 787
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
theora_dec_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstTheoraDec *dec = GST_THEORA_DEC (object);

  switch (prop_id) {
788
    case PROP_CROP:
789 790
      g_value_set_boolean (value, dec->crop);
      break;
791 792 793 794 795 796 797 798 799 800 801 802
    case PROP_TELEMETRY_MV:
      g_value_set_int (value, dec->telemetry_mv);
      break;
    case PROP_TELEMETRY_MBMODE:
      g_value_set_int (value, dec->telemetry_mbmode);
      break;
    case PROP_TELEMETRY_QI:
      g_value_set_int (value, dec->telemetry_qi);
      break;
    case PROP_TELEMETRY_BITS:
      g_value_set_int (value, dec->telemetry_bits);
      break;
803 804 805 806 807
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}
808 809 810 811 812 813 814

gboolean
gst_theora_dec_register (GstPlugin * plugin)
{
  return gst_element_register (plugin, "theoradec",
      GST_RANK_PRIMARY, GST_TYPE_THEORA_DEC);
}