theoraenc.c 38.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* GStreamer
 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
 *
 * 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.
 */

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
20
/**
21
 * SECTION:element-theoraenc
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
22
23
24
25
26
27
 * @see_also: theoradec, oggmux
 *
 * This element encodes raw video into a Theora stream.
 * <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.
28
 *
Wim Taymans's avatar
Wim Taymans committed
29
 * The theora codec internally only supports encoding of images that are a
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
30
31
32
33
 * multiple of 16 pixels in both X and Y direction. It is however perfectly
 * possible to encode images with other dimensions because an arbitrary
 * rectangular cropping region can be set up. This element will automatically
 * set up a correct cropping region if the dimensions are not multiples of 16
34
 * pixels.
35
36
37
38
39
40
41
42
 *
 * To control the quality of the encoding, the #GstTheoraEnc::bitrate and
 * #GstTheoraEnc::quality properties can be used. These two properties are
 * mutualy exclusive. Setting the bitrate property will produce a constant
 * bitrate (CBR) stream while setting the quality property will produce a
 * variable bitrate (VBR) stream.
 *
 * <refsect2>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
43
 * <title>Example pipeline</title>
44
 * |[
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
45
 * gst-launch -v videotestsrc num-buffers=1000 ! theoraenc ! oggmux ! filesink location=videotestsrc.ogg
46
 * ]| This example pipeline will encode a test video source to theora muxed in an
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
47
48
 * ogg container. Refer to the theoradec documentation to decode the create
 * stream.
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
49
 * </refsect2>
Wim Taymans's avatar
Wim Taymans committed
50
51
 *
 * Last reviewed on 2006-03-01 (0.10.4)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
52
53
 */

54
55
56
57
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

58
#include "gsttheoraenc.h"
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
59

60
#include <string.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61
62
#include <stdlib.h>             /* free */

63
64
#include <gst/tag/tag.h>

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
65
#define GST_CAT_DEFAULT theoraenc_debug
66
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
67

68
69
70
71
72
#define GST_TYPE_BORDER_MODE (gst_border_mode_get_type())
static GType
gst_border_mode_get_type (void)
{
  static GType border_mode_type = 0;
73
  static const GEnumValue border_mode[] = {
74
75
76
    {BORDER_NONE, "No Border", "none"},
    {BORDER_BLACK, "Black Border", "black"},
    {BORDER_MIRROR, "Mirror image in borders", "mirror"},
77
78
79
80
81
82
83
84
85
86
    {0, NULL, NULL},
  };

  if (!border_mode_type) {
    border_mode_type =
        g_enum_register_static ("GstTheoraEncBorderMode", border_mode);
  }
  return border_mode_type;
}

87
88
89
90
91
92
93
94
95
96
97
98
99
/* taken from theora/lib/toplevel.c */
static int
_ilog (unsigned int v)
{
  int ret = 0;

  while (v) {
    ret++;
    v >>= 1;
  }
  return (ret);
}

Wim Taymans's avatar
Wim Taymans committed
100
#define THEORA_DEF_BITRATE              0
101
#define THEORA_DEF_QUALITY              48
Wim Taymans's avatar
Wim Taymans committed
102
103
104
105
106
#define THEORA_DEF_KEYFRAME_AUTO        TRUE
#define THEORA_DEF_KEYFRAME_FREQ        64
#define THEORA_DEF_KEYFRAME_FREQ_FORCE  64
#define THEORA_DEF_NOISE_SENSITIVITY    1
#define THEORA_DEF_SHARPNESS            0
107
#define THEORA_DEF_SPEEDLEVEL           1
108
109
110
enum
{
  ARG_0,
111
  ARG_CENTER,
112
  ARG_BORDER,
113
  ARG_BITRATE,
114
115
116
117
118
119
120
121
  ARG_QUALITY,
  ARG_QUICK,
  ARG_KEYFRAME_AUTO,
  ARG_KEYFRAME_FREQ,
  ARG_KEYFRAME_FREQ_FORCE,
  ARG_KEYFRAME_THRESHOLD,
  ARG_KEYFRAME_MINDISTANCE,
  ARG_NOISE_SENSITIVITY,
122
  ARG_SHARPNESS,
123
  ARG_SPEEDLEVEL,
124
  /* FILL ME */
125
126
};

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* this function does a straight granulepos -> timestamp conversion */
static GstClockTime
granulepos_to_timestamp (GstTheoraEnc * theoraenc, ogg_int64_t granulepos)
{
  guint64 iframe, pframe;
  int shift = theoraenc->granule_shift;

  if (granulepos < 0)
    return GST_CLOCK_TIME_NONE;

  iframe = granulepos >> shift;
  pframe = granulepos - (iframe << shift);

  /* num and den are 32 bit, so we can safely multiply with GST_SECOND */
  return gst_util_uint64_scale ((guint64) (iframe + pframe),
      GST_SECOND * theoraenc->info.fps_denominator,
      theoraenc->info.fps_numerator);
}

Stefan Kost's avatar
Stefan Kost committed
146
static const GstElementDetails theora_enc_details =
j^'s avatar
j^ committed
147
GST_ELEMENT_DETAILS ("Theora video encoder",
148
149
150
    "Codec/Encoder/Video",
    "encode raw YUV video to a theora stream",
    "Wim Taymans <wim@fluendo.com>");
151
152
153
154
155
156

static GstStaticPadTemplate theora_enc_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-yuv, "
157
        "format = (fourcc) { I420, Y42B, Y444 }, "
158
        "framerate = (fraction) [0/1, MAX], "
159
160
161
162
163
164
165
166
167
168
        "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
    );

static GstStaticPadTemplate theora_enc_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-theora")
    );

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
static void
_do_init (GType object_type)
{
  const GInterfaceInfo preset_interface_info = {
    NULL,                       /* interface_init */
    NULL,                       /* interface_finalize */
    NULL                        /* interface_data */
  };

  g_type_add_interface_static (object_type, GST_TYPE_PRESET,
      &preset_interface_info);
}

GST_BOILERPLATE_FULL (GstTheoraEnc, gst_theora_enc, GstElement,
    GST_TYPE_ELEMENT, _do_init);
184

185
static gboolean theora_enc_sink_event (GstPad * pad, GstEvent * event);
186
static gboolean theora_enc_src_event (GstPad * pad, GstEvent * event);
187
static GstFlowReturn theora_enc_chain (GstPad * pad, GstBuffer * buffer);
188
189
static GstStateChangeReturn theora_enc_change_state (GstElement * element,
    GstStateChange transition);
190
static GstCaps *theora_enc_sink_getcaps (GstPad * pad);
191
static gboolean theora_enc_sink_setcaps (GstPad * pad, GstCaps * caps);
192
193
194
195
static void theora_enc_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void theora_enc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
196
static void theora_enc_finalize (GObject * object);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

static void
gst_theora_enc_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&theora_enc_src_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&theora_enc_sink_factory));
  gst_element_class_set_details (element_class, &theora_enc_details);
}

static void
gst_theora_enc_class_init (GstTheoraEncClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);

  gobject_class->set_property = theora_enc_set_property;
  gobject_class->get_property = theora_enc_get_property;
218
  gobject_class->finalize = theora_enc_finalize;
219

220
221
  g_object_class_install_property (gobject_class, ARG_CENTER,
      g_param_spec_boolean ("center", "Center",
222
          "ignored and kept for API compat only", TRUE,
223
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224
225
  g_object_class_install_property (gobject_class, ARG_BORDER,
      g_param_spec_enum ("border", "Border",
Benjamin Otte's avatar
Benjamin Otte committed
226
227
          "ignored and kept for API compat only",
          GST_TYPE_BORDER_MODE, BORDER_BLACK,
228
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229
230
231
  /* general encoding stream options */
  g_object_class_install_property (gobject_class, ARG_BITRATE,
      g_param_spec_int ("bitrate", "Bitrate", "Compressed video bitrate (kbps)",
232
          0, (1 << 24) - 1, THEORA_DEF_BITRATE,
233
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234
  g_object_class_install_property (gobject_class, ARG_QUALITY,
235
236
237
      g_param_spec_int ("quality", "Quality", "Video quality", 0, 63,
          THEORA_DEF_QUALITY,
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238
  g_object_class_install_property (gobject_class, ARG_QUICK,
239
240
      g_param_spec_boolean ("quick", "Quick",
          "ignored and kept for API compat only", TRUE,
241
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242
243
244
  g_object_class_install_property (gobject_class, ARG_KEYFRAME_AUTO,
      g_param_spec_boolean ("keyframe-auto", "Keyframe Auto",
          "Automatic keyframe detection", THEORA_DEF_KEYFRAME_AUTO,
245
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246
247
248
  g_object_class_install_property (gobject_class, ARG_KEYFRAME_FREQ,
      g_param_spec_int ("keyframe-freq", "Keyframe frequency",
          "Keyframe frequency", 1, 32768, THEORA_DEF_KEYFRAME_FREQ,
249
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250
251
252
  g_object_class_install_property (gobject_class, ARG_KEYFRAME_FREQ_FORCE,
      g_param_spec_int ("keyframe-force", "Keyframe force",
          "Force keyframe every N frames", 1, 32768,
253
254
          THEORA_DEF_KEYFRAME_FREQ_FORCE,
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255
256
  g_object_class_install_property (gobject_class, ARG_KEYFRAME_THRESHOLD,
      g_param_spec_int ("keyframe-threshold", "Keyframe threshold",
257
          "ignored and kept for API compat only", 0, 32768, 80,
258
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259
260
  g_object_class_install_property (gobject_class, ARG_KEYFRAME_MINDISTANCE,
      g_param_spec_int ("keyframe-mindistance", "Keyframe mindistance",
261
          "ignored and kept for API compat only", 1, 32768, 8,
262
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263
264
265
  g_object_class_install_property (gobject_class, ARG_NOISE_SENSITIVITY,
      g_param_spec_int ("noise-sensitivity", "Noise sensitivity",
          "Noise sensitivity", 0, 32768, THEORA_DEF_NOISE_SENSITIVITY,
266
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267
  g_object_class_install_property (gobject_class, ARG_SHARPNESS,
268
269
270
      g_param_spec_int ("sharpness", "Sharpness", "Sharpness", 0, 2,
          THEORA_DEF_SHARPNESS,
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271
272
273
274
275
276
  g_object_class_install_property (gobject_class, ARG_SPEEDLEVEL,
      g_param_spec_int ("speed-level", "Speed level",
          "Controls the amount of motion vector searching done while "
          "encoding.  This property requires libtheora version >= 1.0",
          0, 2, THEORA_DEF_SPEEDLEVEL,
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277

278
  gstelement_class->change_state = theora_enc_change_state;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
279
  GST_DEBUG_CATEGORY_INIT (theoraenc_debug, "theoraenc", 0, "Theora encoder");
280
281
282
}

static void
283
gst_theora_enc_init (GstTheoraEnc * enc, GstTheoraEncClass * g_class)
284
285
{
  enc->sinkpad =
286
      gst_pad_new_from_static_template (&theora_enc_sink_factory, "sink");
287
  gst_pad_set_chain_function (enc->sinkpad, theora_enc_chain);
288
  gst_pad_set_event_function (enc->sinkpad, theora_enc_sink_event);
289
  gst_pad_set_getcaps_function (enc->sinkpad, theora_enc_sink_getcaps);
290
  gst_pad_set_setcaps_function (enc->sinkpad, theora_enc_sink_setcaps);
291
292
293
  gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);

  enc->srcpad =
294
      gst_pad_new_from_static_template (&theora_enc_src_factory, "src");
295
  gst_pad_set_event_function (enc->srcpad, theora_enc_src_event);
296
297
  gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);

298
299
  gst_segment_init (&enc->segment, GST_FORMAT_UNDEFINED);

300
301
  enc->video_bitrate = THEORA_DEF_BITRATE;
  enc->video_quality = THEORA_DEF_QUALITY;
302
303
304
305
  enc->keyframe_auto = THEORA_DEF_KEYFRAME_AUTO;
  enc->keyframe_freq = THEORA_DEF_KEYFRAME_FREQ;
  enc->keyframe_force = THEORA_DEF_KEYFRAME_FREQ_FORCE;
  enc->noise_sensitivity = THEORA_DEF_NOISE_SENSITIVITY;
306
  enc->sharpness = THEORA_DEF_SHARPNESS;
307
308
309
310
311

  enc->granule_shift = _ilog (enc->info.keyframe_frequency_force - 1);
  GST_DEBUG_OBJECT (enc,
      "keyframe_frequency_force is %d, granule shift is %d",
      enc->info.keyframe_frequency_force, enc->granule_shift);
312
  enc->expected_ts = GST_CLOCK_TIME_NONE;
313
314

  enc->speed_level = THEORA_DEF_SPEEDLEVEL;
315
316
}

317
318
319
320
321
static void
theora_enc_finalize (GObject * object)
{
  GstTheoraEnc *enc = GST_THEORA_ENC (object);

322
  GST_DEBUG_OBJECT (enc, "Finalizing");
323
324
325
  theora_clear (&enc->state);
  theora_comment_clear (&enc->comment);
  theora_info_clear (&enc->info);
326
327

  G_OBJECT_CLASS (parent_class)->finalize (object);
328
329
}

330
331
332
static void
theora_enc_reset (GstTheoraEnc * enc)
{
333
334
  int result;

335
  theora_clear (&enc->state);
336
337
338
  result = theora_encode_init (&enc->state, &enc->info);
  /* We ensure this function cannot fail. */
  g_assert (result == 0);
339
340
341
342
#ifdef TH_ENCCTL_SET_SPLEVEL
  theora_control (&enc->state, TH_ENCCTL_SET_SPLEVEL, &enc->speed_level,
      sizeof (enc->speed_level));
#endif
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
}

static void
theora_enc_clear (GstTheoraEnc * enc)
{
  enc->packetno = 0;
  enc->bytes_out = 0;
  enc->granulepos_offset = 0;
  enc->timestamp_offset = 0;

  enc->next_ts = GST_CLOCK_TIME_NONE;
  enc->next_discont = FALSE;
  enc->expected_ts = GST_CLOCK_TIME_NONE;
}

358
359
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
static char *
theora_enc_get_supported_formats (void)
{
  theora_state state;
  theora_info info;
  struct
  {
    theora_pixelformat pixelformat;
    char *fourcc;
  } formats[] = {
    {
    OC_PF_420, "I420"}, {
    OC_PF_422, "Y42B"}, {
    OC_PF_444, "Y444"}
  };
  GString *string = NULL;
  guint i;

  theora_info_init (&info);
  info.width = 16;
  info.height = 16;
  info.fps_numerator = 25;
  info.fps_denominator = 1;
  for (i = 0; i < G_N_ELEMENTS (formats); i++) {
    info.pixelformat = formats[i].pixelformat;

    if (theora_encode_init (&state, &info) != 0)
      continue;

    GST_LOG ("format %s is supported", formats[i].fourcc);
    theora_clear (&state);

    if (string == NULL) {
      string = g_string_new (formats[i].fourcc);
    } else {
      g_string_append (string, ", ");
      g_string_append (string, formats[i].fourcc);
    }
  }
  theora_info_clear (&info);

  return string == NULL ? NULL : g_string_free (string, FALSE);
}

static GstCaps *
theora_enc_sink_getcaps (GstPad * pad)
{
  GstCaps *caps;
  char *supported_formats, *caps_string;

  supported_formats = theora_enc_get_supported_formats ();
  if (!supported_formats) {
    GST_WARNING ("no supported formats found. Encoder disabled?");
    return gst_caps_new_empty ();
  }

  caps_string = g_strdup_printf ("video/x-raw-yuv, "
      "format = (fourcc) { %s }, "
      "framerate = (fraction) [0/1, MAX], "
      "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]",
      supported_formats);
  caps = gst_caps_from_string (caps_string);
  g_free (caps_string);
421
  g_free (supported_formats);
422
  GST_DEBUG ("Supported caps: %" GST_PTR_FORMAT, caps);
423
424
425
426

  return caps;
}

427
428
static gboolean
theora_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
429
430
431
{
  GstStructure *structure = gst_caps_get_structure (caps, 0);
  GstTheoraEnc *enc = GST_THEORA_ENC (gst_pad_get_parent (pad));
432
  guint32 fourcc;
433
  const GValue *par;
Jan Schmidt's avatar
Jan Schmidt committed
434
  gint fps_n, fps_d;
435

436
  gst_structure_get_fourcc (structure, "format", &fourcc);
437
438
  gst_structure_get_int (structure, "width", &enc->width);
  gst_structure_get_int (structure, "height", &enc->height);
Jan Schmidt's avatar
Jan Schmidt committed
439
  gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d);
440
  par = gst_structure_get_value (structure, "pixel-aspect-ratio");
441

442
  theora_info_clear (&enc->info);
443
  theora_info_init (&enc->info);
444
445
446
447
  /* Theora has a divisible-by-sixteen restriction for the encoded video size but
   * we can define a visible area using the frame_width/frame_height */
  enc->info_width = enc->info.width = (enc->width + 15) & ~15;
  enc->info_height = enc->info.height = (enc->height + 15) & ~15;
448
449
  enc->info.frame_width = enc->width;
  enc->info.frame_height = enc->height;
450
451
452
453
454
455
456
457
458
459
460
461
462
  switch (fourcc) {
    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
      enc->info.pixelformat = OC_PF_420;
      break;
    case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
      enc->info.pixelformat = OC_PF_422;
      break;
    case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
      enc->info.pixelformat = OC_PF_444;
      break;
    default:
      g_assert_not_reached ();
  }
463

464
465
  enc->info.offset_x = 0;
  enc->info.offset_y = 0;
466

467
468
  enc->info.fps_numerator = enc->fps_n = fps_n;
  enc->info.fps_denominator = enc->fps_d = fps_d;
469
470
471
472
473
474
475
476
477
  if (par) {
    enc->info.aspect_numerator = gst_value_get_fraction_numerator (par);
    enc->info.aspect_denominator = gst_value_get_fraction_denominator (par);
  } else {
    /* setting them to 0 indicates that the decoder can chose a good aspect
     * ratio, defaulting to 1/1 */
    enc->info.aspect_numerator = 0;
    enc->info.aspect_denominator = 0;
  }
478
479
480
481
482

  enc->info.colorspace = OC_CS_UNSPECIFIED;
  enc->info.target_bitrate = enc->video_bitrate;
  enc->info.quality = enc->video_quality;

483
484
485
  enc->info.keyframe_auto_p = (enc->keyframe_auto ? 1 : 0);
  enc->info.keyframe_frequency = enc->keyframe_freq;
  enc->info.keyframe_frequency_force = enc->keyframe_force;
486
  enc->info.keyframe_data_target_bitrate = enc->video_bitrate * 1.5;
487
  enc->info.noise_sensitivity = enc->noise_sensitivity;
488
  enc->info.sharpness = enc->sharpness;
489

490
491
492
493
494
495
  /* as done in theora */
  enc->granule_shift = _ilog (enc->info.keyframe_frequency_force - 1);
  GST_DEBUG_OBJECT (enc,
      "keyframe_frequency_force is %d, granule shift is %d",
      enc->info.keyframe_frequency_force, enc->granule_shift);

496
  theora_enc_reset (enc);
497
  enc->initialised = TRUE;
498

499
500
  gst_object_unref (enc);

501
  return TRUE;
502
503
}

504
505
506
static guint64
granulepos_add (guint64 granulepos, guint64 addend, gint shift)
{
507
  guint64 iframe, pframe;
508
509
510
511
512
513
514
515

  iframe = granulepos >> shift;
  pframe = granulepos - (iframe << shift);
  iframe += addend;

  return (iframe << shift) + pframe;
}

516
/* prepare a buffer for transmission by passing data through libtheora */
517
static GstFlowReturn
518
theora_buffer_from_packet (GstTheoraEnc * enc, ogg_packet * packet,
519
520
    GstClockTime timestamp, GstClockTime running_time,
    GstClockTime duration, GstBuffer ** buffer)
521
522
{
  GstBuffer *buf;
523
  GstFlowReturn ret = GST_FLOW_OK;
Wim Taymans's avatar
Wim Taymans committed
524

525
526
527
528
529
530
  buf = gst_buffer_new_and_alloc (packet->bytes);
  if (!buf) {
    GST_WARNING_OBJECT (enc, "Could not allocate buffer");
    ret = GST_FLOW_ERROR;
    goto done;
  }
531
532

  memcpy (GST_BUFFER_DATA (buf), packet->packet, packet->bytes);
533
  gst_buffer_set_caps (buf, GST_PAD_CAPS (enc->srcpad));
534
535
  /* see ext/ogg/README; OFFSET_END takes "our" granulepos, OFFSET its
   * time representation */
536
537
538
  GST_BUFFER_OFFSET_END (buf) =
      granulepos_add (packet->granulepos, enc->granulepos_offset,
      enc->granule_shift);
539
540
541
  GST_BUFFER_OFFSET (buf) = granulepos_to_timestamp (enc,
      GST_BUFFER_OFFSET_END (buf));

542
  GST_BUFFER_TIMESTAMP (buf) = timestamp;
543
  GST_BUFFER_DURATION (buf) = duration;
544

545
546
547
548
549
  if (enc->next_discont) {
    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
    enc->next_discont = FALSE;
  }

550
551
  /* the second most significant bit of the first data byte is cleared
   * for keyframes */
552
  if ((packet->packet[0] & 0x40) == 0) {
553
    GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
554
  } else {
555
    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
556
  }
557
  enc->packetno++;
558

559
done:
560
561
  *buffer = buf;
  return ret;
562
563
564
}

/* push out the buffer and do internal bookkeeping */
565
static GstFlowReturn
566
567
theora_push_buffer (GstTheoraEnc * enc, GstBuffer * buffer)
{
568
569
  GstFlowReturn ret;

570
571
  enc->bytes_out += GST_BUFFER_SIZE (buffer);

572
573
574
  ret = gst_pad_push (enc->srcpad, buffer);

  return ret;
575
576
}

577
static GstFlowReturn
578
theora_push_packet (GstTheoraEnc * enc, ogg_packet * packet,
579
    GstClockTime timestamp, GstClockTime running_time, GstClockTime duration)
580
581
{
  GstBuffer *buf;
582
  GstFlowReturn ret;
583

584
585
586
  ret =
      theora_buffer_from_packet (enc, packet, timestamp, running_time, duration,
      &buf);
587
588
  if (ret == GST_FLOW_OK)
    ret = theora_push_buffer (enc, buf);
589
590

  return ret;
591
592
}

Wim Taymans's avatar
Wim Taymans committed
593
static GstCaps *
594
595
596
theora_set_header_on_caps (GstCaps * caps, GstBuffer * buf1,
    GstBuffer * buf2, GstBuffer * buf3)
{
Wim Taymans's avatar
Wim Taymans committed
597
  GstStructure *structure;
598
  GValue array = { 0 };
599
600
  GValue value = { 0 };

Wim Taymans's avatar
Wim Taymans committed
601
602
603
  caps = gst_caps_make_writable (caps);
  structure = gst_caps_get_structure (caps, 0);

604
605
606
607
608
  /* mark buffers */
  GST_BUFFER_FLAG_SET (buf1, GST_BUFFER_FLAG_IN_CAPS);
  GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_IN_CAPS);
  GST_BUFFER_FLAG_SET (buf3, GST_BUFFER_FLAG_IN_CAPS);

609
610
611
612
613
614
615
  /* Copy buffers, because we can't use the originals -
   * it creates a circular refcount with the caps<->buffers */
  buf1 = gst_buffer_copy (buf1);
  buf2 = gst_buffer_copy (buf2);
  buf3 = gst_buffer_copy (buf3);

  /* put copies of the buffers in a fixed list */
616
  g_value_init (&array, GST_TYPE_ARRAY);
617

618
  g_value_init (&value, GST_TYPE_BUFFER);
619
  gst_value_set_buffer (&value, buf1);
620
  gst_value_array_append_value (&array, &value);
621
  g_value_unset (&value);
622

623
  g_value_init (&value, GST_TYPE_BUFFER);
624
  gst_value_set_buffer (&value, buf2);
625
  gst_value_array_append_value (&array, &value);
626
  g_value_unset (&value);
627

628
  g_value_init (&value, GST_TYPE_BUFFER);
629
  gst_value_set_buffer (&value, buf3);
630
  gst_value_array_append_value (&array, &value);
631
  g_value_unset (&value);
632
633

  gst_structure_set_value (structure, "streamheader", &array);
634
  g_value_unset (&array);
Wim Taymans's avatar
Wim Taymans committed
635

636
637
638
639
640
  /* Unref our copies */
  gst_buffer_unref (buf1);
  gst_buffer_unref (buf2);
  gst_buffer_unref (buf3);

Wim Taymans's avatar
Wim Taymans committed
641
  return caps;
642
643
}

644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
static void
theora_enc_force_keyframe (GstTheoraEnc * enc)
{
  GstClockTime next_ts;

  /* make sure timestamps increment after resetting the decoder */
  next_ts = enc->next_ts + enc->timestamp_offset;

  theora_enc_reset (enc);
  enc->granulepos_offset =
      gst_util_uint64_scale (next_ts, enc->fps_n, GST_SECOND * enc->fps_d);
  enc->timestamp_offset = next_ts;
  enc->next_ts = 0;
}

659
660
static gboolean
theora_enc_sink_event (GstPad * pad, GstEvent * event)
661
662
663
{
  GstTheoraEnc *enc;
  ogg_packet op;
664
  gboolean res;
665

666
667
668
  enc = GST_THEORA_ENC (GST_PAD_PARENT (pad));

  switch (GST_EVENT_TYPE (event)) {
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
    case GST_EVENT_NEWSEGMENT:
    {
      gboolean update;
      gdouble rate, applied_rate;
      GstFormat format;
      gint64 start, stop, time;

      gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
          &format, &start, &stop, &time);

      gst_segment_set_newsegment_full (&enc->segment, update, rate,
          applied_rate, format, start, stop, time);

      res = gst_pad_push_event (enc->srcpad, event);
      break;
    }
685
    case GST_EVENT_EOS:
686
      if (enc->initialised) {
687
        /* push last packet with eos flag, should not be called */
688
        while (theora_encode_packetout (&enc->state, 1, &op)) {
689
          GstClockTime next_time =
690
              theora_granule_time (&enc->state, op.granulepos) * GST_SECOND;
691

692
693
          theora_push_packet (enc, &op, GST_CLOCK_TIME_NONE, enc->next_ts,
              next_time - enc->next_ts);
694
695
          enc->next_ts = next_time;
        }
696
      }
697
698
      res = gst_pad_push_event (enc->srcpad, event);
      break;
699
700
701
702
    case GST_EVENT_FLUSH_STOP:
      gst_segment_init (&enc->segment, GST_FORMAT_UNDEFINED);
      res = gst_pad_push_event (enc->srcpad, event);
      break;
703
704
705
706
707
708
    case GST_EVENT_CUSTOM_DOWNSTREAM:
    {
      const GstStructure *s;

      s = gst_event_get_structure (event);

709
710
      if (gst_structure_has_name (s, "GstForceKeyUnit"))
        theora_enc_force_keyframe (enc);
711
712
713
      res = gst_pad_push_event (enc->srcpad, event);
      break;
    }
714
    default:
715
      res = gst_pad_push_event (enc->srcpad, event);
716
      break;
717
  }
718
  return res;
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
static gboolean
theora_enc_src_event (GstPad * pad, GstEvent * event)
{
  GstTheoraEnc *enc;
  gboolean res = TRUE;

  enc = GST_THEORA_ENC (GST_PAD_PARENT (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_CUSTOM_UPSTREAM:
    {
      const GstStructure *s;

      s = gst_event_get_structure (event);

      if (gst_structure_has_name (s, "GstForceKeyUnit")) {
        GST_OBJECT_LOCK (enc);
        enc->force_keyframe = TRUE;
        GST_OBJECT_UNLOCK (enc);
        /* consume the event */
        res = TRUE;
        gst_event_unref (event);
      } else {
        res = gst_pad_push_event (enc->sinkpad, event);
      }
      break;
    }
    default:
      res = gst_pad_push_event (enc->sinkpad, event);
      break;
  }

  return res;
}

756
static gboolean
757
758
theora_enc_is_discontinuous (GstTheoraEnc * enc, GstClockTime timestamp,
    GstClockTime duration)
759
760
{
  GstClockTimeDiff max_diff;
761
  gboolean ret = FALSE;
762
763
764
765
766

  /* Allow 3/4 a frame off */
  max_diff = (enc->info.fps_denominator * GST_SECOND * 3) /
      (enc->info.fps_numerator * 4);

767
768
769
  if (timestamp != GST_CLOCK_TIME_NONE
      && enc->expected_ts != GST_CLOCK_TIME_NONE) {
    if ((GstClockTimeDiff) (timestamp - enc->expected_ts) > max_diff) {
770
771
772
      GST_DEBUG_OBJECT (enc, "Incoming TS %" GST_TIME_FORMAT
          " exceeds expected value %" GST_TIME_FORMAT
          " by too much, marking discontinuity",
773
          GST_TIME_ARGS (timestamp), GST_TIME_ARGS (enc->expected_ts));
774
      ret = TRUE;
775
776
777
    }
  }

778
779
  if (GST_CLOCK_TIME_IS_VALID (duration))
    enc->expected_ts = timestamp + duration;
780
781
782
  else
    enc->expected_ts = GST_CLOCK_TIME_NONE;

783
  return ret;
784
785
}

786
static void
787
theora_enc_init_yuv_buffer (yuv_buffer * yuv, theora_pixelformat format,
788
789
    guint8 * data, gint width, gint height)
{
790
  yuv->y = data;
791
792
793
794
  yuv->y_width = width;
  yuv->y_height = height;
  yuv->y_stride = GST_ROUND_UP_4 (width);

795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
  switch (format) {
    case OC_PF_444:
      yuv->uv_width = width;
      yuv->uv_height = height;
      yuv->uv_stride = GST_ROUND_UP_4 (width);
      yuv->u = yuv->y + height * yuv->y_stride;
      yuv->v = yuv->u + height * yuv->uv_stride;
      break;
    case OC_PF_420:
      yuv->uv_width = width / 2;
      yuv->uv_height = height / 2;
      yuv->uv_stride = GST_ROUND_UP_8 (width) / 2;
      yuv->u = yuv->y + GST_ROUND_UP_2 (height) * yuv->y_stride;
      yuv->v = yuv->u + GST_ROUND_UP_2 (height) / 2 * yuv->uv_stride;
      break;
    case OC_PF_422:
      yuv->uv_width = width / 2;
      yuv->uv_height = height;
      yuv->uv_stride = GST_ROUND_UP_8 (width) / 2;
      yuv->u = yuv->y + height * yuv->y_stride;
      yuv->v = yuv->u + height * yuv->uv_stride;
      break;
    default:
      g_assert_not_reached ();
  }
820
821
}

822
823
824
825
/* NB: This function does no input checking */
static void
copy_plane (guint8 * dest, int dest_width, int dest_height, int dest_stride,
    const guint8 * src, int src_width, int src_height, int src_stride,
Benjamin Otte's avatar
Benjamin Otte committed
826
    int black)
827
{
828
  int right_border, i;
829

830
  right_border = dest_width - src_width;
831
832
833

  /* copy source */
  for (i = 0; i < src_height; i++) {
834
    memcpy (dest, src, src_width);
Benjamin Otte's avatar
Benjamin Otte committed
835
    memset (dest + src_width, black, right_border);
836
837
838
839
840
841

    dest += dest_stride;
    src += src_stride;
  }

  /* fill bottom border */
Benjamin Otte's avatar
Benjamin Otte committed
842
  memset (dest, black, dest_stride * (dest_height - src_height));
843
844
}

845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
static guint
theora_format_get_bits_per_pixel (theora_pixelformat format)
{
  switch (format) {
    case OC_PF_420:
      return 12;
    case OC_PF_422:
      return 16;
    case OC_PF_444:
      return 24;
    default:
      g_assert_not_reached ();
      return 0;
  }
}

861
862
863
static GstBuffer *
theora_enc_resize_buffer (GstTheoraEnc * enc, GstBuffer * buffer)
{
864
  yuv_buffer dest, src;
865
866
867
868
869
870
871
872
873
  GstBuffer *newbuf;

  if (enc->width == enc->info_width && enc->height == enc->info_height) {
    GST_LOG_OBJECT (enc, "no cropping/conversion needed");
    return buffer;
  }

  GST_LOG_OBJECT (enc, "cropping/conversion needed for strides");

874
875
  newbuf = gst_buffer_new_and_alloc (enc->info_width * enc->info_height *
      theora_format_get_bits_per_pixel (enc->info.pixelformat) / 8);
876
877
878
879
880
881
882
  if (!newbuf) {
    gst_buffer_unref (buffer);
    return NULL;
  }
  GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET_NONE;
  gst_buffer_set_caps (newbuf, GST_PAD_CAPS (enc->srcpad));

883
884
885
886
  theora_enc_init_yuv_buffer (&src, enc->info.pixelformat,
      GST_BUFFER_DATA (buffer), enc->width, enc->height);
  theora_enc_init_yuv_buffer (&dest, enc->info.pixelformat,
      GST_BUFFER_DATA (newbuf), enc->info_width, enc->info_height);
887
888

  copy_plane (dest.y, dest.y_width, dest.y_height, dest.y_stride,
Benjamin Otte's avatar
Benjamin Otte committed
889
      src.y, src.y_width, src.y_height, src.y_stride, 0);
890

891
  copy_plane (dest.u, dest.uv_width, dest.uv_height, dest.uv_stride,
Benjamin Otte's avatar
Benjamin Otte committed
892
      src.u, src.uv_width, src.uv_height, src.uv_stride, 128);
893
  copy_plane (dest.v, dest.uv_width, dest.uv_height, dest.uv_stride,
Benjamin Otte's avatar
Benjamin Otte committed
894
      src.v, src.uv_width, src.uv_height, src.uv_stride, 128);
895
896
897
898
899

  gst_buffer_unref (buffer);
  return newbuf;
}

900
901
902
903
904
static GstFlowReturn
theora_enc_chain (GstPad * pad, GstBuffer * buffer)
{
  GstTheoraEnc *enc;
  ogg_packet op;
905
  GstClockTime timestamp, duration, running_time;
906
  GstFlowReturn ret;
907
  gboolean force_keyframe;
908
909
910

  enc = GST_THEORA_ENC (GST_PAD_PARENT (pad));

911
912
913
914
915
916
917
918
919
  /* we keep track of two timelines.
   * - The timestamps from the incomming buffers, which we copy to the outgoing
   *   encoded buffers as-is. We need to do this as we simply forward the
   *   newsegment events.
   * - The running_time of the buffers, which we use to construct the granulepos
   *   in the packets.
   */
  timestamp = GST_BUFFER_TIMESTAMP (buffer);
  duration = GST_BUFFER_DURATION (buffer);
920

921
922
  running_time =
      gst_segment_to_running_time (&enc->segment, GST_FORMAT_TIME, timestamp);
923
924
925
926
927
928
  if ((gint64) running_time < 0) {
    GST_DEBUG_OBJECT (enc, "Dropping buffer, timestamp: %" GST_TIME_FORMAT,
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
    gst_buffer_unref (buffer);
    return GST_FLOW_OK;
  }
929

930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
  /* see if we need to schedule a keyframe */
  GST_OBJECT_LOCK (enc);
  force_keyframe = enc->force_keyframe;
  enc->force_keyframe = FALSE;
  GST_OBJECT_UNLOCK (enc);

  if (force_keyframe) {
    GstClockTime stream_time;
    GstStructure *s;

    stream_time = gst_segment_to_stream_time (&enc->segment,
        GST_FORMAT_TIME, timestamp);

    s = gst_structure_new ("GstForceKeyUnit",
        "timestamp", G_TYPE_UINT64, timestamp,
        "stream-time", G_TYPE_UINT64, stream_time,
        "running-time", G_TYPE_UINT64, running_time, NULL);

    theora_enc_force_keyframe (enc);

    gst_pad_push_event (enc->srcpad,
        gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s));
  }

954
955
956
957
958
  /* make sure we copy the discont flag to the next outgoing buffer when it's
   * set on the incomming buffer */
  if (GST_BUFFER_IS_DISCONT (buffer)) {
    enc->next_discont = TRUE;
  }
959

960
  if (enc->packetno == 0) {
961
    /* no packets written yet, setup headers */
962
    GstCaps *caps;
963
    GstBuffer *buf1, *buf2, *buf3;
964

965
966
967
    enc->granulepos_offset = 0;
    enc->timestamp_offset = 0;

968
    GST_DEBUG_OBJECT (enc, "output headers");
969
970
971
972
973
974
975
    /* Theora streams begin with three headers; the initial header (with
       most of the codec setup parameters) which is mandated by the Ogg
       bitstream spec.  The second header holds any comment fields.  The
       third header holds the bitstream codebook.  We merely need to
       make the headers, then pass them to libtheora one at a time;
       libtheora handles the additional Ogg bitstream constraints */

976
    /* first packet will get its own page automatically */
977
978
979
    if (theora_encode_header (&enc->state, &op) != 0)
      goto encoder_disabled;

980
981
982
    ret =
        theora_buffer_from_packet (enc, &op, GST_CLOCK_TIME_NONE,
        GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, &buf1);
983
984
985
    if (ret != GST_FLOW_OK) {
      goto header_buffer_alloc;
    }
986

987
    /* create the remaining theora headers */
988
    theora_comment_clear (&enc->comment);
989
    theora_comment_init (&enc->comment);
990

991
992
993
    if (theora_encode_comment (&enc->comment, &op) != 0)
      goto encoder_disabled;

994
995
996
    ret =
        theora_buffer_from_packet (enc, &op, GST_CLOCK_TIME_NONE,
        GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, &buf2);
997
998
999
1000
1001
1002
1003
    /* Theora expects us to put this packet buffer into an ogg page,
     * in which case it becomes the ogg library's responsibility to
     * free it. Since we're copying and outputting a gst_buffer,
     * we need to free it ourselves. */
    if (op.packet)
      free (op.packet);

1004
1005
1006
1007
1008
    if (ret != GST_FLOW_OK) {
      gst_buffer_unref (buf1);
      goto header_buffer_alloc;
    }

1009
1010
1011
    if (theora_encode_tables (&enc->state, &op) != 0)
      goto encoder_disabled;

1012
1013
1014
    ret =
        theora_buffer_from_packet (enc, &op, GST_CLOCK_TIME_NONE,
        GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, &buf3);
1015
1016
1017
1018
1019
    if (ret != GST_FLOW_OK) {
      gst_buffer_unref (buf1);
      gst_buffer_unref (buf2);
      goto header_buffer_alloc;
    }
1020
1021
1022

    /* mark buffers and put on caps */
    caps = gst_pad_get_caps (enc->srcpad);
Wim Taymans's avatar
Wim Taymans committed
1023
    caps = theora_set_header_on_caps (caps, buf1, buf2, buf3);
1024
    GST_DEBUG ("here are the caps: %" GST_PTR_FORMAT, caps);
1025
    gst_pad_set_caps (enc->srcpad, caps);
1026

1027
1028
1029
1030
    gst_buffer_set_caps (buf1, caps);
    gst_buffer_set_caps (buf2, caps);
    gst_buffer_set_caps (buf3, caps);

1031
1032
    gst_caps_unref (caps);

1033
    /* push out the header buffers */
1034
1035
1036
    if ((ret = theora_push_buffer (enc, buf1)) != GST_FLOW_OK) {
      gst_buffer_unref (buf2);
      gst_buffer_unref (buf3);
1037
      goto header_push;
1038
1039
1040
    }
    if ((ret = theora_push_buffer (enc, buf2)) != GST_FLOW_OK) {
      gst_buffer_unref (buf3);
1041
      goto header_push;
1042
1043
    }
    if ((ret = theora_push_buffer (enc, buf3)) != GST_FLOW_OK) {
1044
      goto header_push;
1045
    }
1046
1047

    enc->granulepos_offset =
1048
1049
1050
        gst_util_uint64_scale (running_time, enc->fps_n,
        GST_SECOND * enc->fps_d);
    enc->timestamp_offset = running_time;
1051
    enc->next_ts = 0;
1052
  }
1053

1054
1055
  {
    yuv_buffer yuv;
1056
1057
    gint res;

1058
1059
1060
    buffer = theora_enc_resize_buffer (enc, buffer);
    if (buffer == NULL)
      return GST_FLOW_ERROR;
1061

1062
1063
    theora_enc_init_yuv_buffer (&yuv, enc->info.pixelformat,
        GST_BUFFER_DATA (buffer), enc->info_width, enc->info_height);
1064

1065
    if (theora_enc_is_discontinuous (enc, running_time, duration)) {
1066
1067
      theora_enc_reset (enc);
      enc->granulepos_offset =
1068
1069
1070
          gst_util_uint64_scale (running_time, enc->fps_n,
          GST_SECOND * enc->fps_d);
      enc->timestamp_offset = running_time;
1071
1072
1073
1074
      enc->next_ts = 0;
      enc->next_discont = TRUE;
    }

1075
    res = theora_encode_YUVin (&enc->state, &yuv);
1076
1077
    /* none of the failure cases can happen here */
    g_assert (res == 0);
1078
1079

    ret = GST_FLOW_OK;
1080
    while (theora_encode_packetout (&enc->state, 0, &op)) {
1081
1082
      GstClockTime next_time;

1083
      next_time = theora_granule_time (&enc->state, op.granulepos) * GST_SECOND;
1084

1085
      ret =
1086
1087
1088
          theora_push_packet (enc, &op, timestamp, enc->next_ts,
          next_time - enc->next_ts);

1089
1090
      enc->next_ts = next_time;
      if (ret != GST_FLOW_OK)
1091
        goto data_push;
1092
    }
1093
    gst_buffer_unref (buffer);
1094
  }
1095
1096

  return ret;
1097

1098
  /* ERRORS */
1099
1100
1101
1102
1103
header_buffer_alloc:
  {
    gst_buffer_unref (buffer);
    return ret;
  }
1104
1105
1106
header_push:
  {
    gst_buffer_unref (buffer);
Wim Taymans's avatar
Wim Taymans committed
1107
1108
    return ret;
  }
1109
1110
1111
1112
1113
data_push:
  {
    gst_buffer_unref (buffer);
    return ret;
  }
1114
1115
1116
1117
1118
1119
1120
encoder