Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / ext / theora / gsttheoraenc.c
1 /* GStreamer
2  * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-theoraenc
22  * @see_also: theoradec, oggmux
23  *
24  * This element encodes raw video into a Theora stream.
25  * <ulink url="http://www.theora.org/">Theora</ulink> is a royalty-free
26  * video codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
27  * Foundation</ulink>, based on the VP3 codec.
28  *
29  * The theora codec internally only supports encoding of images that are a
30  * multiple of 16 pixels in both X and Y direction. It is however perfectly
31  * possible to encode images with other dimensions because an arbitrary
32  * rectangular cropping region can be set up. This element will automatically
33  * set up a correct cropping region if the dimensions are not multiples of 16
34  * pixels.
35  *
36  * To control the quality of the encoding, the #GstTheoraEnc::bitrate and
37  * #GstTheoraEnc::quality properties can be used. These two properties are
38  * mutualy exclusive. Setting the bitrate property will produce a constant
39  * bitrate (CBR) stream while setting the quality property will produce a
40  * variable bitrate (VBR) stream.
41  *
42  * <refsect2>
43  * <title>Example pipeline</title>
44  * |[
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
47  * ogg container. Refer to the theoradec documentation to decode the create
48  * stream.
49  * </refsect2>
50  *
51  * Last reviewed on 2006-03-01 (0.10.4)
52  */
53
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57
58 #include "gsttheoraenc.h"
59
60 #include <string.h>
61 #include <stdlib.h>             /* free */
62
63 #include <gst/tag/tag.h>
64 #include <gst/video/video.h>
65
66 #define GST_CAT_DEFAULT theoraenc_debug
67 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
68
69 #define GST_TYPE_MULTIPASS_MODE (gst_multipass_mode_get_type())
70 static GType
71 gst_multipass_mode_get_type (void)
72 {
73   static GType multipass_mode_type = 0;
74   static const GEnumValue multipass_mode[] = {
75     {MULTIPASS_MODE_SINGLE_PASS, "Single pass", "single-pass"},
76     {MULTIPASS_MODE_FIRST_PASS, "First pass", "first-pass"},
77     {MULTIPASS_MODE_SECOND_PASS, "Second pass", "second-pass"},
78     {0, NULL, NULL},
79   };
80
81   if (!multipass_mode_type) {
82     multipass_mode_type =
83         g_enum_register_static ("GstTheoraEncMultipassMode", multipass_mode);
84   }
85   return multipass_mode_type;
86 }
87
88 /* taken from theora/lib/toplevel.c */
89 static int
90 _ilog (unsigned int v)
91 {
92   int ret = 0;
93
94   while (v) {
95     ret++;
96     v >>= 1;
97   }
98   return (ret);
99 }
100
101 #define THEORA_DEF_BITRATE              0
102 #define THEORA_DEF_QUALITY              48
103 #define THEORA_DEF_KEYFRAME_AUTO        TRUE
104 #define THEORA_DEF_KEYFRAME_FREQ        64
105 #define THEORA_DEF_KEYFRAME_FREQ_FORCE  64
106 #define THEORA_DEF_SPEEDLEVEL           1
107 #define THEORA_DEF_VP3_COMPATIBLE       FALSE
108 #define THEORA_DEF_DROP_FRAMES          TRUE
109 #define THEORA_DEF_CAP_OVERFLOW         TRUE
110 #define THEORA_DEF_CAP_UNDERFLOW        FALSE
111 #define THEORA_DEF_RATE_BUFFER          0
112 #define THEORA_DEF_MULTIPASS_CACHE_FILE NULL
113 #define THEORA_DEF_MULTIPASS_MODE       MULTIPASS_MODE_SINGLE_PASS
114 #define THEORA_DEF_DUP_ON_GAP           FALSE
115 enum
116 {
117   PROP_0,
118   PROP_BITRATE,
119   PROP_QUALITY,
120   PROP_KEYFRAME_AUTO,
121   PROP_KEYFRAME_FREQ,
122   PROP_KEYFRAME_FREQ_FORCE,
123   PROP_SPEEDLEVEL,
124   PROP_VP3_COMPATIBLE,
125   PROP_DROP_FRAMES,
126   PROP_CAP_OVERFLOW,
127   PROP_CAP_UNDERFLOW,
128   PROP_RATE_BUFFER,
129   PROP_MULTIPASS_CACHE_FILE,
130   PROP_MULTIPASS_MODE,
131   PROP_DUP_ON_GAP
132       /* FILL ME */
133 };
134
135 /* this function does a straight granulepos -> timestamp conversion */
136 static GstClockTime
137 granulepos_to_timestamp (GstTheoraEnc * theoraenc, ogg_int64_t granulepos)
138 {
139   guint64 iframe, pframe;
140   int shift = theoraenc->info.keyframe_granule_shift;
141
142   if (granulepos < 0)
143     return GST_CLOCK_TIME_NONE;
144
145   iframe = granulepos >> shift;
146   pframe = granulepos - (iframe << shift);
147
148   /* num and den are 32 bit, so we can safely multiply with GST_SECOND */
149   return gst_util_uint64_scale ((guint64) (iframe + pframe),
150       GST_SECOND * theoraenc->info.fps_denominator,
151       theoraenc->info.fps_numerator);
152 }
153
154 /* Generate a dummy encoder context for use in th_encode_ctl queries
155    Release with th_encode_free()
156    This and the next routine from theora/examples/libtheora_info.c */
157 static th_enc_ctx *
158 dummy_encode_ctx (void)
159 {
160   th_enc_ctx *ctx;
161   th_info info;
162
163   /* set the minimal video parameters */
164   th_info_init (&info);
165   info.frame_width = 320;
166   info.frame_height = 240;
167   info.fps_numerator = 1;
168   info.fps_denominator = 1;
169
170   /* allocate and initialize a context object */
171   ctx = th_encode_alloc (&info);
172   if (!ctx)
173     GST_WARNING ("Failed to allocate dummy encoder context.");
174
175   /* clear the info struct */
176   th_info_clear (&info);
177
178   return ctx;
179 }
180
181 /* Query the current and maximum values for the 'speed level' setting.
182    This can be used to ask the encoder to trade off encoding quality
183    vs. performance cost, for example to adapt to realtime constraints. */
184 static int
185 check_speed_level (th_enc_ctx * ctx, int *current, int *max)
186 {
187   int ret;
188
189   /* query the current speed level */
190   ret = th_encode_ctl (ctx, TH_ENCCTL_GET_SPLEVEL, current, sizeof (int));
191   if (ret) {
192     GST_WARNING ("Error %d getting current speed level.", ret);
193     return ret;
194   }
195   /* query the maximum speed level, which varies by encoder version */
196   ret = th_encode_ctl (ctx, TH_ENCCTL_GET_SPLEVEL_MAX, max, sizeof (int));
197   if (ret) {
198     GST_WARNING ("Error %d getting maximum speed level.", ret);
199     return ret;
200   }
201
202   return 0;
203 }
204
205 static GstStaticPadTemplate theora_enc_sink_factory =
206 GST_STATIC_PAD_TEMPLATE ("sink",
207     GST_PAD_SINK,
208     GST_PAD_ALWAYS,
209     GST_STATIC_CAPS ("video/x-raw, "
210         "format = (string) { I420, Y42B, Y444 }, "
211         "framerate = (fraction) [1/MAX, MAX], "
212         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
213     );
214
215 static GstStaticPadTemplate theora_enc_src_factory =
216 GST_STATIC_PAD_TEMPLATE ("src",
217     GST_PAD_SRC,
218     GST_PAD_ALWAYS,
219     GST_STATIC_CAPS ("video/x-theora, "
220         "framerate = (fraction) [1/MAX, MAX], "
221         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
222     );
223
224 #define gst_theora_enc_parent_class parent_class
225 G_DEFINE_TYPE_WITH_CODE (GstTheoraEnc, gst_theora_enc,
226     GST_TYPE_ELEMENT, G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
227
228 static GstCaps *theora_enc_src_caps;
229
230 static gboolean theora_enc_sink_event (GstPad * pad, GstObject * parent,
231     GstEvent * event);
232 static gboolean theora_enc_src_event (GstPad * pad, GstObject * parent,
233     GstEvent * event);
234 static GstFlowReturn theora_enc_chain (GstPad * pad, GstObject * parent,
235     GstBuffer * buffer);
236 static GstStateChangeReturn theora_enc_change_state (GstElement * element,
237     GstStateChange transition);
238 static gboolean theora_enc_sink_query (GstPad * pad, GstObject * parent,
239     GstQuery * query);
240 static gboolean theora_enc_sink_setcaps (GstTheoraEnc * enc, GstCaps * caps);
241 static void theora_enc_get_property (GObject * object, guint prop_id,
242     GValue * value, GParamSpec * pspec);
243 static void theora_enc_set_property (GObject * object, guint prop_id,
244     const GValue * value, GParamSpec * pspec);
245 static void theora_enc_finalize (GObject * object);
246
247 static gboolean theora_enc_write_multipass_cache (GstTheoraEnc * enc,
248     gboolean begin, gboolean eos);
249
250 static char *theora_enc_get_supported_formats (void);
251
252 static void theora_timefifo_free (GstTheoraEnc * enc);
253 static GstFlowReturn
254 theora_enc_encode_and_push (GstTheoraEnc * enc, ogg_packet op,
255     GstBuffer * buffer);
256
257 static void
258 gst_theora_enc_class_init (GstTheoraEncClass * klass)
259 {
260   GObjectClass *gobject_class = (GObjectClass *) klass;
261   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
262   char *caps_string;
263
264   /* query runtime encoder properties */
265   th_enc_ctx *th_ctx;
266   int default_speed_level = THEORA_DEF_SPEEDLEVEL;
267   int max_speed_level = default_speed_level;
268
269   GST_DEBUG_CATEGORY_INIT (theoraenc_debug, "theoraenc", 0, "Theora encoder");
270
271   th_ctx = dummy_encode_ctx ();
272   if (th_ctx) {
273     if (check_speed_level (th_ctx, &default_speed_level, &max_speed_level))
274       GST_WARNING
275           ("Failed to determine settings for the speed-level property.");
276     th_encode_free (th_ctx);
277   }
278
279   gobject_class->set_property = theora_enc_set_property;
280   gobject_class->get_property = theora_enc_get_property;
281   gobject_class->finalize = theora_enc_finalize;
282
283   /* general encoding stream options */
284   g_object_class_install_property (gobject_class, PROP_BITRATE,
285       g_param_spec_int ("bitrate", "Bitrate", "Compressed video bitrate (kbps)",
286           0, (1 << 24) - 1, THEORA_DEF_BITRATE,
287           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
288           GST_PARAM_MUTABLE_PLAYING));
289   g_object_class_install_property (gobject_class, PROP_QUALITY,
290       g_param_spec_int ("quality", "Quality", "Video quality", 0, 63,
291           THEORA_DEF_QUALITY,
292           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
293           GST_PARAM_MUTABLE_PLAYING));
294   g_object_class_install_property (gobject_class, PROP_KEYFRAME_AUTO,
295       g_param_spec_boolean ("keyframe-auto", "Keyframe Auto",
296           "Automatic keyframe detection", THEORA_DEF_KEYFRAME_AUTO,
297           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298   g_object_class_install_property (gobject_class, PROP_KEYFRAME_FREQ,
299       g_param_spec_int ("keyframe-freq", "Keyframe frequency",
300           "Keyframe frequency", 1, 32768, THEORA_DEF_KEYFRAME_FREQ,
301           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
302   g_object_class_install_property (gobject_class, PROP_KEYFRAME_FREQ_FORCE,
303       g_param_spec_int ("keyframe-force", "Keyframe force",
304           "Force keyframe every N frames", 1, 32768,
305           THEORA_DEF_KEYFRAME_FREQ_FORCE,
306           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307   g_object_class_install_property (gobject_class, PROP_SPEEDLEVEL,
308       g_param_spec_int ("speed-level", "Speed level",
309           "Controls the amount of analysis performed when encoding."
310           " Higher values trade compression quality for speed."
311           " This property requires libtheora version >= 1.0"
312           ", and the maximum value may vary based on encoder version.",
313           0, max_speed_level, default_speed_level,
314           (GParamFlags) G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
315           G_PARAM_STATIC_STRINGS));
316   g_object_class_install_property (gobject_class, PROP_VP3_COMPATIBLE,
317       g_param_spec_boolean ("vp3-compatible", "VP3 Compatible",
318           "Disables non-VP3 compatible features",
319           THEORA_DEF_VP3_COMPATIBLE,
320           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321   g_object_class_install_property (gobject_class, PROP_DROP_FRAMES,
322       g_param_spec_boolean ("drop-frames", "Drop Frames",
323           "Allow or disallow frame dropping",
324           THEORA_DEF_DROP_FRAMES,
325           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326   g_object_class_install_property (gobject_class, PROP_CAP_OVERFLOW,
327       g_param_spec_boolean ("cap-overflow", "Cap Overflow",
328           "Enable capping of bit reservoir overflows",
329           THEORA_DEF_CAP_OVERFLOW,
330           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331   g_object_class_install_property (gobject_class, PROP_CAP_UNDERFLOW,
332       g_param_spec_boolean ("cap-underflow", "Cap Underflow",
333           "Enable capping of bit reservoir underflows",
334           THEORA_DEF_CAP_UNDERFLOW,
335           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
336   g_object_class_install_property (gobject_class, PROP_RATE_BUFFER,
337       g_param_spec_int ("rate-buffer", "Rate Control Buffer",
338           "Sets the size of the rate control buffer, in units of frames.  "
339           "The default value of 0 instructs the encoder to automatically "
340           "select an appropriate value",
341           0, 1000, THEORA_DEF_RATE_BUFFER,
342           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343   g_object_class_install_property (gobject_class, PROP_MULTIPASS_CACHE_FILE,
344       g_param_spec_string ("multipass-cache-file", "Multipass Cache File",
345           "Multipass cache file", THEORA_DEF_MULTIPASS_CACHE_FILE,
346           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347   g_object_class_install_property (gobject_class, PROP_MULTIPASS_MODE,
348       g_param_spec_enum ("multipass-mode", "Multipass mode",
349           "Single pass or first/second pass", GST_TYPE_MULTIPASS_MODE,
350           THEORA_DEF_MULTIPASS_MODE,
351           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352   g_object_class_install_property (gobject_class, PROP_DUP_ON_GAP,
353       g_param_spec_boolean ("dup-on-gap", "Create DUP frame on GAP flag",
354           "Allow codec to handle frames with GAP flag as duplicates "
355           "of previous frame. "
356           "This is good to work with variable frame rate stabilized "
357           "by videorate element. It will add variable latency with maximal "
358           "size of keyframe distance, this way it is a bad idea "
359           "to use with live streams.",
360           THEORA_DEF_DUP_ON_GAP,
361           (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362
363   gst_element_class_add_pad_template (gstelement_class,
364       gst_static_pad_template_get (&theora_enc_src_factory));
365   gst_element_class_add_pad_template (gstelement_class,
366       gst_static_pad_template_get (&theora_enc_sink_factory));
367   gst_element_class_set_details_simple (gstelement_class,
368       "Theora video encoder", "Codec/Encoder/Video",
369       "encode raw YUV video to a theora stream",
370       "Wim Taymans <wim@fluendo.com>");
371
372   caps_string = g_strdup_printf ("video/x-raw, "
373       "format = (string) { %s }, "
374       "framerate = (fraction) [1/MAX, MAX], "
375       "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]",
376       theora_enc_get_supported_formats ());
377   theora_enc_src_caps = gst_caps_from_string (caps_string);
378   g_free (caps_string);
379
380   gstelement_class->change_state = theora_enc_change_state;
381 }
382
383 static void
384 gst_theora_enc_init (GstTheoraEnc * enc)
385 {
386   enc->sinkpad =
387       gst_pad_new_from_static_template (&theora_enc_sink_factory, "sink");
388   gst_pad_set_chain_function (enc->sinkpad, theora_enc_chain);
389   gst_pad_set_event_function (enc->sinkpad, theora_enc_sink_event);
390   gst_pad_set_query_function (enc->sinkpad, theora_enc_sink_query);
391   gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
392
393   enc->srcpad =
394       gst_pad_new_from_static_template (&theora_enc_src_factory, "src");
395   gst_pad_set_event_function (enc->srcpad, theora_enc_src_event);
396   gst_pad_use_fixed_caps (enc->srcpad);
397   gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
398
399   gst_segment_init (&enc->segment, GST_FORMAT_UNDEFINED);
400
401   enc->video_bitrate = THEORA_DEF_BITRATE;
402   enc->video_quality = THEORA_DEF_QUALITY;
403   enc->keyframe_auto = THEORA_DEF_KEYFRAME_AUTO;
404   enc->keyframe_freq = THEORA_DEF_KEYFRAME_FREQ;
405   enc->keyframe_force = THEORA_DEF_KEYFRAME_FREQ_FORCE;
406
407   enc->expected_ts = GST_CLOCK_TIME_NONE;
408
409   /* enc->speed_level is set to the libtheora default by the constructor */
410   enc->vp3_compatible = THEORA_DEF_VP3_COMPATIBLE;
411   enc->drop_frames = THEORA_DEF_DROP_FRAMES;
412   enc->cap_overflow = THEORA_DEF_CAP_OVERFLOW;
413   enc->cap_underflow = THEORA_DEF_CAP_UNDERFLOW;
414   enc->rate_buffer = THEORA_DEF_RATE_BUFFER;
415   enc->dup_on_gap = THEORA_DEF_DUP_ON_GAP;
416
417   enc->multipass_mode = THEORA_DEF_MULTIPASS_MODE;
418   enc->multipass_cache_file = THEORA_DEF_MULTIPASS_CACHE_FILE;
419 }
420
421 static void
422 theora_enc_clear_multipass_cache (GstTheoraEnc * enc)
423 {
424   if (enc->multipass_cache_fd) {
425     g_io_channel_shutdown (enc->multipass_cache_fd, TRUE, NULL);
426     g_io_channel_unref (enc->multipass_cache_fd);
427     enc->multipass_cache_fd = NULL;
428   }
429
430   if (enc->multipass_cache_adapter) {
431     gst_object_unref (enc->multipass_cache_adapter);
432     enc->multipass_cache_adapter = NULL;
433   }
434 }
435
436 static void
437 theora_enc_finalize (GObject * object)
438 {
439   GstTheoraEnc *enc = GST_THEORA_ENC (object);
440
441   GST_DEBUG_OBJECT (enc, "Finalizing");
442   if (enc->encoder)
443     th_encode_free (enc->encoder);
444   th_comment_clear (&enc->comment);
445   th_info_clear (&enc->info);
446   g_free (enc->multipass_cache_file);
447
448   theora_enc_clear_multipass_cache (enc);
449
450   G_OBJECT_CLASS (parent_class)->finalize (object);
451 }
452
453 static void
454 theora_enc_reset (GstTheoraEnc * enc)
455 {
456   ogg_uint32_t keyframe_force;
457   int rate_flags;
458
459   GST_OBJECT_LOCK (enc);
460   enc->info.target_bitrate = enc->video_bitrate;
461   if (enc->quality_changed) {
462     enc->info.quality = enc->video_quality;
463   } else {
464     if (enc->video_bitrate == 0) {
465       enc->info.quality = enc->video_quality;
466     }
467   }
468   enc->bitrate_changed = FALSE;
469   enc->quality_changed = FALSE;
470   GST_OBJECT_UNLOCK (enc);
471
472   if (enc->encoder)
473     th_encode_free (enc->encoder);
474   enc->encoder = th_encode_alloc (&enc->info);
475   /* We ensure this function cannot fail. */
476   g_assert (enc->encoder != NULL);
477   th_encode_ctl (enc->encoder, TH_ENCCTL_SET_SPLEVEL, &enc->speed_level,
478       sizeof (enc->speed_level));
479   th_encode_ctl (enc->encoder, TH_ENCCTL_SET_VP3_COMPATIBLE,
480       &enc->vp3_compatible, sizeof (enc->vp3_compatible));
481
482   rate_flags = 0;
483   if (enc->drop_frames)
484     rate_flags |= TH_RATECTL_DROP_FRAMES;
485   if (enc->drop_frames)
486     rate_flags |= TH_RATECTL_CAP_OVERFLOW;
487   if (enc->drop_frames)
488     rate_flags |= TH_RATECTL_CAP_UNDERFLOW;
489   th_encode_ctl (enc->encoder, TH_ENCCTL_SET_RATE_FLAGS,
490       &rate_flags, sizeof (rate_flags));
491
492   if (enc->rate_buffer) {
493     th_encode_ctl (enc->encoder, TH_ENCCTL_SET_RATE_BUFFER,
494         &enc->rate_buffer, sizeof (enc->rate_buffer));
495   } else {
496     /* FIXME */
497   }
498
499   keyframe_force = enc->keyframe_auto ?
500       enc->keyframe_force : enc->keyframe_freq;
501   th_encode_ctl (enc->encoder, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
502       &keyframe_force, sizeof (keyframe_force));
503
504   /* Get placeholder data */
505   if (enc->multipass_cache_fd
506       && enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS)
507     theora_enc_write_multipass_cache (enc, TRUE, FALSE);
508 }
509
510 static void
511 theora_enc_clear (GstTheoraEnc * enc)
512 {
513   enc->packetno = 0;
514   enc->bytes_out = 0;
515   enc->granulepos_offset = 0;
516   enc->timestamp_offset = 0;
517
518   theora_timefifo_free (enc);
519
520   enc->next_ts = GST_CLOCK_TIME_NONE;
521   enc->next_discont = FALSE;
522   enc->expected_ts = GST_CLOCK_TIME_NONE;
523 }
524
525 static char *
526 theora_enc_get_supported_formats (void)
527 {
528   th_enc_ctx *encoder;
529   th_info info;
530   static const struct
531   {
532     th_pixel_fmt pixelformat;
533     const char fourcc[];
534   } formats[] = {
535     {
536     TH_PF_420, "I420"}, {
537     TH_PF_422, "Y42B"}, {
538     TH_PF_444, "Y444"}
539   };
540   GString *string = NULL;
541   guint i;
542
543   th_info_init (&info);
544   info.frame_width = 16;
545   info.frame_height = 16;
546   info.fps_numerator = 25;
547   info.fps_denominator = 1;
548   for (i = 0; i < G_N_ELEMENTS (formats); i++) {
549     info.pixel_fmt = formats[i].pixelformat;
550
551     encoder = th_encode_alloc (&info);
552     if (encoder == NULL)
553       continue;
554
555     GST_LOG ("format %s is supported", formats[i].fourcc);
556     th_encode_free (encoder);
557
558     if (string == NULL) {
559       string = g_string_new (formats[i].fourcc);
560     } else {
561       g_string_append (string, ", ");
562       g_string_append (string, formats[i].fourcc);
563     }
564   }
565   th_info_clear (&info);
566
567   return string == NULL ? NULL : g_string_free (string, FALSE);
568 }
569
570 static GstCaps *
571 theora_enc_sink_getcaps (GstPad * pad, GstCaps * filter)
572 {
573   GstTheoraEnc *encoder;
574   GstPad *peer;
575   GstCaps *caps;
576
577   /* If we already have caps return them */
578   if ((caps = gst_pad_get_current_caps (pad)) != NULL)
579     return caps;
580
581   encoder = GST_THEORA_ENC (gst_pad_get_parent (pad));
582   if (!encoder)
583     return gst_caps_new_empty ();
584
585   peer = gst_pad_get_peer (encoder->srcpad);
586   if (peer) {
587     const GstCaps *templ_caps;
588     GstCaps *peer_caps, *tmp_caps;
589     GstStructure *s;
590     guint i, n;
591
592     peer_caps = gst_pad_query_caps (peer, NULL);
593
594     /* Translate peercaps to YUV */
595     peer_caps = gst_caps_make_writable (peer_caps);
596     n = gst_caps_get_size (peer_caps);
597     for (i = 0; i < n; i++) {
598       s = gst_caps_get_structure (peer_caps, i);
599
600       gst_structure_set_name (s, "video/x-raw");
601       gst_structure_remove_field (s, "streamheader");
602     }
603
604     templ_caps = gst_pad_get_pad_template_caps (pad);
605
606     tmp_caps = gst_caps_intersect (peer_caps, templ_caps);
607     caps = gst_caps_intersect (tmp_caps, theora_enc_src_caps);
608     gst_caps_unref (tmp_caps);
609     gst_caps_unref (peer_caps);
610     gst_object_unref (peer);
611     peer = NULL;
612   } else {
613     caps = gst_caps_ref (theora_enc_src_caps);
614   }
615
616   gst_object_unref (encoder);
617
618   if (filter) {
619     GstCaps *intersection;
620
621     intersection =
622         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
623     gst_caps_unref (caps);
624     caps = intersection;
625   }
626
627   return caps;
628 }
629
630 static gboolean
631 theora_enc_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
632 {
633   gboolean res = FALSE;
634
635   switch (GST_QUERY_TYPE (query)) {
636     case GST_QUERY_CAPS:
637     {
638       GstCaps *filter, *caps;
639
640       gst_query_parse_caps (query, &filter);
641       caps = theora_enc_sink_getcaps (pad, filter);
642       gst_query_set_caps_result (query, caps);
643       gst_caps_unref (caps);
644       res = TRUE;
645       break;
646     }
647     default:
648       res = gst_pad_query_default (pad, parent, query);
649       break;
650   }
651
652   return res;
653 }
654
655 static gboolean
656 theora_enc_sink_setcaps (GstTheoraEnc * enc, GstCaps * caps)
657 {
658   GstVideoInfo info;
659
660   th_info_clear (&enc->info);
661   th_info_init (&enc->info);
662
663   if (!gst_video_info_from_caps (&info, caps))
664     goto invalid_caps;
665
666   enc->vinfo = info;
667
668   /* Theora has a divisible-by-sixteen restriction for the encoded video size but
669    * we can define a picture area using pic_width/pic_height */
670   enc->info.frame_width = GST_ROUND_UP_16 (info.width);
671   enc->info.frame_height = GST_ROUND_UP_16 (info.height);
672   enc->info.pic_width = info.width;
673   enc->info.pic_height = info.height;
674
675   switch (GST_VIDEO_INFO_FORMAT (&info)) {
676     case GST_VIDEO_FORMAT_I420:
677       enc->info.pixel_fmt = TH_PF_420;
678       break;
679     case GST_VIDEO_FORMAT_Y42B:
680       enc->info.pixel_fmt = TH_PF_422;
681       break;
682     case GST_VIDEO_FORMAT_Y444:
683       enc->info.pixel_fmt = TH_PF_444;
684       break;
685     default:
686       g_assert_not_reached ();
687   }
688
689   enc->info.fps_numerator = info.fps_n;
690   enc->info.fps_denominator = info.fps_d;
691
692   enc->info.aspect_numerator = info.par_n;
693   enc->info.aspect_denominator = info.par_d;
694 #if 0
695   /* setting them to 0 indicates that the decoder can chose a good aspect
696    * ratio, defaulting to 1/1 */
697   enc->info.aspect_numerator = 0;
698   enc->par_n = 1;
699   enc->info.aspect_denominator = 0;
700   enc->par_d = 1;
701 #endif
702
703   enc->info.colorspace = TH_CS_UNSPECIFIED;
704
705   /* as done in theora */
706   enc->info.keyframe_granule_shift = _ilog (enc->keyframe_force - 1);
707   GST_DEBUG_OBJECT (enc,
708       "keyframe_frequency_force is %d, granule shift is %d",
709       enc->keyframe_force, enc->info.keyframe_granule_shift);
710
711   theora_enc_reset (enc);
712   enc->initialised = TRUE;
713
714   return TRUE;
715
716   /* ERRORS */
717 invalid_caps:
718   {
719     GST_DEBUG_OBJECT (enc, "could not parse caps");
720     return FALSE;
721   }
722 }
723
724 static guint64
725 granulepos_add (guint64 granulepos, guint64 addend, gint shift)
726 {
727   guint64 iframe, pframe;
728
729   iframe = granulepos >> shift;
730   pframe = granulepos - (iframe << shift);
731   iframe += addend;
732
733   return (iframe << shift) + pframe;
734 }
735
736 /* prepare a buffer for transmission by passing data through libtheora */
737 static GstFlowReturn
738 theora_buffer_from_packet (GstTheoraEnc * enc, ogg_packet * packet,
739     GstClockTime timestamp, GstClockTime running_time,
740     GstClockTime duration, GstBuffer ** buffer)
741 {
742   GstBuffer *buf;
743   GstFlowReturn ret = GST_FLOW_OK;
744
745   buf = gst_buffer_new_and_alloc (packet->bytes);
746   if (!buf) {
747     GST_WARNING_OBJECT (enc, "Could not allocate buffer");
748     ret = GST_FLOW_ERROR;
749     goto done;
750   }
751
752   gst_buffer_fill (buf, 0, packet->packet, packet->bytes);
753   /* see ext/ogg/README; OFFSET_END takes "our" granulepos, OFFSET its
754    * time representation */
755   GST_BUFFER_OFFSET_END (buf) =
756       granulepos_add (packet->granulepos, enc->granulepos_offset,
757       enc->info.keyframe_granule_shift);
758   GST_BUFFER_OFFSET (buf) = granulepos_to_timestamp (enc,
759       GST_BUFFER_OFFSET_END (buf));
760
761   GST_BUFFER_TIMESTAMP (buf) = timestamp;
762   GST_BUFFER_DURATION (buf) = duration;
763
764   if (enc->next_discont) {
765     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
766     enc->next_discont = FALSE;
767   }
768
769   /* th_packet_iskeyframe returns positive for keyframes */
770   if (th_packet_iskeyframe (packet) > 0) {
771     GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
772   } else {
773     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
774   }
775   enc->packetno++;
776
777 done:
778   *buffer = buf;
779   return ret;
780 }
781
782 /* push out the buffer and do internal bookkeeping */
783 static GstFlowReturn
784 theora_push_buffer (GstTheoraEnc * enc, GstBuffer * buffer)
785 {
786   GstFlowReturn ret;
787
788   enc->bytes_out += gst_buffer_get_size (buffer);
789
790   ret = gst_pad_push (enc->srcpad, buffer);
791
792   return ret;
793 }
794
795 static GstFlowReturn
796 theora_push_packet (GstTheoraEnc * enc, ogg_packet * packet,
797     GstClockTime timestamp, GstClockTime running_time, GstClockTime duration)
798 {
799   GstBuffer *buf;
800   GstFlowReturn ret;
801
802   ret =
803       theora_buffer_from_packet (enc, packet, timestamp, running_time, duration,
804       &buf);
805   if (ret == GST_FLOW_OK)
806     ret = theora_push_buffer (enc, buf);
807
808   return ret;
809 }
810
811 static GstCaps *
812 theora_set_header_on_caps (GstCaps * caps, GSList * buffers)
813 {
814   GstStructure *structure;
815   GValue array = { 0 };
816   GValue value = { 0 };
817   GstBuffer *buffer;
818   GSList *walk;
819
820   caps = gst_caps_make_writable (caps);
821   structure = gst_caps_get_structure (caps, 0);
822
823   /* put copies of the buffers in a fixed list */
824   g_value_init (&array, GST_TYPE_ARRAY);
825
826   for (walk = buffers; walk; walk = walk->next) {
827     buffer = walk->data;
828
829     /* mark buffer */
830     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_HEADER);
831
832     /* Copy buffer, because we can't use the original -
833      * it creates a circular refcount with the caps<->buffers */
834     buffer = gst_buffer_copy (buffer);
835
836     g_value_init (&value, GST_TYPE_BUFFER);
837     gst_value_set_buffer (&value, buffer);
838     gst_value_array_append_value (&array, &value);
839     g_value_unset (&value);
840
841     /* Unref our copy */
842     gst_buffer_unref (buffer);
843   }
844
845   gst_structure_set_value (structure, "streamheader", &array);
846   g_value_unset (&array);
847
848   return caps;
849 }
850
851 static void
852 theora_enc_force_keyframe (GstTheoraEnc * enc)
853 {
854   GstClockTime next_ts;
855
856   /* make sure timestamps increment after resetting the decoder */
857   next_ts = enc->next_ts + enc->timestamp_offset;
858
859   theora_enc_reset (enc);
860   enc->granulepos_offset =
861       gst_util_uint64_scale (next_ts, enc->vinfo.fps_n,
862       GST_SECOND * enc->vinfo.fps_d);
863   enc->timestamp_offset = next_ts;
864   enc->next_ts = 0;
865 }
866
867 static gboolean
868 theora_enc_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
869 {
870   GstTheoraEnc *enc;
871   ogg_packet op;
872   gboolean res;
873
874   enc = GST_THEORA_ENC (parent);
875
876   switch (GST_EVENT_TYPE (event)) {
877     case GST_EVENT_CAPS:
878     {
879       GstCaps *caps;
880
881       gst_event_parse_caps (event, &caps);
882       res = theora_enc_sink_setcaps (enc, caps);
883       gst_event_unref (event);
884       break;
885     }
886     case GST_EVENT_SEGMENT:
887     {
888       gst_event_copy_segment (event, &enc->segment);
889
890       res = gst_pad_push_event (enc->srcpad, event);
891       break;
892     }
893     case GST_EVENT_EOS:
894       if (enc->initialised) {
895         /* clear all standing buffers */
896         if (enc->dup_on_gap)
897           theora_enc_encode_and_push (enc, op, NULL);
898         /* push last packet with eos flag, should not be called */
899         while (th_encode_packetout (enc->encoder, 1, &op)) {
900           GstClockTime next_time =
901               th_granule_time (enc->encoder, op.granulepos) * GST_SECOND;
902
903           theora_push_packet (enc, &op, GST_CLOCK_TIME_NONE, enc->next_ts,
904               next_time - enc->next_ts);
905           enc->next_ts = next_time;
906         }
907       }
908       if (enc->initialised && enc->multipass_cache_fd
909           && enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS)
910         theora_enc_write_multipass_cache (enc, TRUE, TRUE);
911
912       theora_enc_clear_multipass_cache (enc);
913
914       res = gst_pad_push_event (enc->srcpad, event);
915       break;
916     case GST_EVENT_FLUSH_STOP:
917       gst_segment_init (&enc->segment, GST_FORMAT_UNDEFINED);
918       res = gst_pad_push_event (enc->srcpad, event);
919       theora_timefifo_free (enc);
920       break;
921     case GST_EVENT_CUSTOM_DOWNSTREAM:
922     {
923       const GstStructure *s;
924
925       s = gst_event_get_structure (event);
926
927       if (gst_structure_has_name (s, "GstForceKeyUnit"))
928         theora_enc_force_keyframe (enc);
929       res = gst_pad_push_event (enc->srcpad, event);
930       break;
931     }
932     default:
933       res = gst_pad_push_event (enc->srcpad, event);
934       break;
935   }
936   return res;
937 }
938
939 static gboolean
940 theora_enc_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
941 {
942   GstTheoraEnc *enc;
943   gboolean res = TRUE;
944
945   enc = GST_THEORA_ENC (parent);
946
947   switch (GST_EVENT_TYPE (event)) {
948     case GST_EVENT_CUSTOM_UPSTREAM:
949     {
950       const GstStructure *s;
951
952       s = gst_event_get_structure (event);
953
954       if (gst_structure_has_name (s, "GstForceKeyUnit")) {
955         GST_OBJECT_LOCK (enc);
956         enc->force_keyframe = TRUE;
957         GST_OBJECT_UNLOCK (enc);
958         /* consume the event */
959         res = TRUE;
960         gst_event_unref (event);
961       } else {
962         res = gst_pad_push_event (enc->sinkpad, event);
963       }
964       break;
965     }
966     default:
967       res = gst_pad_push_event (enc->sinkpad, event);
968       break;
969   }
970
971   return res;
972 }
973
974 static gboolean
975 theora_enc_is_discontinuous (GstTheoraEnc * enc, GstClockTime timestamp,
976     GstClockTime duration)
977 {
978   GstClockTimeDiff max_diff;
979   gboolean ret = FALSE;
980
981   /* Allow 3/4 a frame off */
982   max_diff = (enc->info.fps_denominator * GST_SECOND * 3) /
983       (enc->info.fps_numerator * 4);
984
985   if (timestamp != GST_CLOCK_TIME_NONE
986       && enc->expected_ts != GST_CLOCK_TIME_NONE) {
987     if ((GstClockTimeDiff) (timestamp - enc->expected_ts) > max_diff) {
988       GST_DEBUG_OBJECT (enc, "Incoming TS %" GST_TIME_FORMAT
989           " exceeds expected value %" GST_TIME_FORMAT
990           " by too much, marking discontinuity",
991           GST_TIME_ARGS (timestamp), GST_TIME_ARGS (enc->expected_ts));
992       ret = TRUE;
993     }
994   }
995
996   if (GST_CLOCK_TIME_IS_VALID (duration))
997     enc->expected_ts = timestamp + duration;
998   else
999     enc->expected_ts = GST_CLOCK_TIME_NONE;
1000
1001   return ret;
1002 }
1003
1004 static void
1005 theora_enc_init_buffer (th_ycbcr_buffer buf, GstVideoFrame * frame)
1006 {
1007   GstVideoInfo info;
1008   guint i;
1009
1010   /* According to Theora developer Timothy Terriberry, the Theora 
1011    * encoder will not use memory outside of pic_width/height, even when
1012    * the frame size is bigger. The values outside this region will be encoded
1013    * to default values.
1014    * Due to this, setting the frame's width/height as the buffer width/height
1015    * is perfectly ok, even though it does not strictly look ok.
1016    */
1017
1018   gst_video_info_init (&info);
1019   gst_video_info_set_format (&info, GST_VIDEO_FRAME_FORMAT (frame),
1020       GST_ROUND_UP_16 (GST_VIDEO_FRAME_WIDTH (frame)),
1021       GST_ROUND_UP_16 (GST_VIDEO_FRAME_HEIGHT (frame)));
1022
1023   for (i = 0; i < 3; i++) {
1024     buf[i].width = GST_VIDEO_INFO_COMP_WIDTH (&info, i);
1025     buf[i].height = GST_VIDEO_INFO_COMP_HEIGHT (&info, i);
1026     buf[i].data = GST_VIDEO_FRAME_COMP_DATA (frame, i);
1027     buf[i].stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, i);
1028   }
1029 }
1030
1031 static gboolean
1032 theora_enc_read_multipass_cache (GstTheoraEnc * enc)
1033 {
1034   GstBuffer *cache_buf;
1035   const guint8 *cache_data;
1036   gsize bytes_read = 0;
1037   gint bytes_consumed = 0;
1038   GIOStatus stat = G_IO_STATUS_NORMAL;
1039   gboolean done = FALSE;
1040
1041   while (!done) {
1042     if (gst_adapter_available (enc->multipass_cache_adapter) == 0) {
1043       GstMapInfo map;
1044
1045       cache_buf = gst_buffer_new_and_alloc (512);
1046
1047       gst_buffer_map (cache_buf, &map, GST_MAP_READ);
1048       stat = g_io_channel_read_chars (enc->multipass_cache_fd,
1049           (gchar *) map.data, map.size, &bytes_read, NULL);
1050
1051       if (bytes_read <= 0) {
1052         gst_buffer_unmap (cache_buf, &map);
1053         gst_buffer_unref (cache_buf);
1054         break;
1055       } else {
1056         gst_buffer_unmap (cache_buf, &map);
1057         gst_buffer_resize (cache_buf, 0, bytes_read);
1058         gst_adapter_push (enc->multipass_cache_adapter, cache_buf);
1059       }
1060     }
1061     if (gst_adapter_available (enc->multipass_cache_adapter) == 0)
1062       break;
1063
1064     bytes_read =
1065         MIN (gst_adapter_available (enc->multipass_cache_adapter), 512);
1066
1067     cache_data = gst_adapter_map (enc->multipass_cache_adapter, bytes_read);
1068
1069     bytes_consumed =
1070         th_encode_ctl (enc->encoder, TH_ENCCTL_2PASS_IN, (guint8 *) cache_data,
1071         bytes_read);
1072     gst_adapter_unmap (enc->multipass_cache_adapter);
1073
1074     done = bytes_consumed <= 0;
1075     if (bytes_consumed > 0)
1076       gst_adapter_flush (enc->multipass_cache_adapter, bytes_consumed);
1077   }
1078
1079   if (stat == G_IO_STATUS_ERROR || (stat == G_IO_STATUS_EOF && bytes_read == 0)
1080       || bytes_consumed < 0) {
1081     GST_ELEMENT_ERROR (enc, RESOURCE, READ, (NULL),
1082         ("Failed to read multipass cache file"));
1083     return FALSE;
1084   }
1085   return TRUE;
1086 }
1087
1088 static gboolean
1089 theora_enc_write_multipass_cache (GstTheoraEnc * enc, gboolean begin,
1090     gboolean eos)
1091 {
1092   GError *err = NULL;
1093   GIOStatus stat = G_IO_STATUS_NORMAL;
1094   gint bytes_read = 0;
1095   gsize bytes_written = 0;
1096   gchar *buf;
1097
1098   if (begin)
1099     stat = g_io_channel_seek_position (enc->multipass_cache_fd, 0, G_SEEK_SET,
1100         &err);
1101   if (stat != G_IO_STATUS_ERROR) {
1102     do {
1103       bytes_read =
1104           th_encode_ctl (enc->encoder, TH_ENCCTL_2PASS_OUT, &buf, sizeof (buf));
1105       if (bytes_read > 0)
1106         g_io_channel_write_chars (enc->multipass_cache_fd, buf, bytes_read,
1107             &bytes_written, NULL);
1108     } while (bytes_read > 0 && bytes_written > 0);
1109
1110   }
1111
1112   if (stat == G_IO_STATUS_ERROR || bytes_read < 0) {
1113     if (begin) {
1114       if (eos)
1115         GST_ELEMENT_WARNING (enc, RESOURCE, WRITE, (NULL),
1116             ("Failed to seek to beginning of multipass cache file: %s",
1117                 err->message));
1118       else
1119         GST_ELEMENT_ERROR (enc, RESOURCE, WRITE, (NULL),
1120             ("Failed to seek to beginning of multipass cache file: %s",
1121                 err->message));
1122     } else {
1123       GST_ELEMENT_ERROR (enc, RESOURCE, WRITE, (NULL),
1124           ("Failed to write multipass cache file"));
1125     }
1126     if (err)
1127       g_error_free (err);
1128
1129     return FALSE;
1130   }
1131   return TRUE;
1132 }
1133
1134 /**
1135  * g_slice_free can't be used with g_queue_foreach.
1136  * so we create new function with predefined GstClockTime size.
1137  */
1138 static void
1139 theora_free_gstclocktime (gpointer mem)
1140 {
1141   g_slice_free (GstClockTime, mem);
1142 }
1143
1144 static void
1145 theora_timefifo_in (GstTheoraEnc * enc, const GstClockTime * timestamp)
1146 {
1147   GstClockTime *ptr;
1148
1149   if (!enc->t_queue)
1150     enc->t_queue = g_queue_new ();
1151
1152   g_assert (enc->t_queue != NULL);
1153
1154   ptr = g_slice_new (GstClockTime);
1155   *ptr = *timestamp;
1156
1157   g_queue_push_head (enc->t_queue, ptr);
1158 }
1159
1160 static GstClockTime
1161 theora_timefifo_out (GstTheoraEnc * enc)
1162 {
1163   GstClockTime ret, *ptr;
1164
1165   g_assert (enc->t_queue != NULL);
1166
1167   ptr = g_queue_pop_tail (enc->t_queue);
1168   g_assert (ptr != NULL);
1169
1170   ret = *ptr;
1171   theora_free_gstclocktime (ptr);
1172
1173   return ret;
1174 }
1175
1176 /**
1177  * theora_timefifo_truncate - truncate the timestamp queue.
1178  * After frame encoding we should have only one buffer for next time.
1179  * The count of timestamps should be the same. If it is less,
1180  * some thing really bad has happened. If it is bigger, encoder
1181  * decided to return less then we ordered.
1182  * TODO: for now we will just drop this timestamps. The better solution
1183  * probably will be to recovery frames by recovery timestamps with
1184  * last buffer.
1185  */
1186 static void
1187 theora_timefifo_truncate (GstTheoraEnc * enc)
1188 {
1189   if (enc->dup_on_gap) {
1190     guint length;
1191     g_assert (enc->t_queue != NULL);
1192     length = g_queue_get_length (enc->t_queue);
1193
1194     if (length > 1) {
1195       /* it is also not good if we have more then 1. */
1196       GST_DEBUG_OBJECT (enc, "Dropping %u time stamps", length - 1);
1197       while (g_queue_get_length (enc->t_queue) > 1) {
1198         theora_timefifo_out (enc);
1199       }
1200     }
1201   }
1202 }
1203
1204 static void
1205 theora_timefifo_free (GstTheoraEnc * enc)
1206 {
1207   if (enc->t_queue) {
1208     if (g_queue_get_length (enc->t_queue))
1209       g_queue_foreach (enc->t_queue, (GFunc) theora_free_gstclocktime, NULL);
1210     g_queue_free (enc->t_queue);
1211     enc->t_queue = NULL;
1212   }
1213   /* prevbuf makes no sense without timestamps,
1214    * so clear it too. */
1215   if (enc->prevbuf) {
1216     gst_buffer_unref (enc->prevbuf);
1217     enc->prevbuf = NULL;
1218   }
1219
1220 }
1221
1222 static void
1223 theora_update_prevbuf (GstTheoraEnc * enc, GstBuffer * buffer)
1224 {
1225   if (enc->prevbuf) {
1226     gst_buffer_unref (enc->prevbuf);
1227     enc->prevbuf = NULL;
1228   }
1229   enc->prevbuf = gst_buffer_ref (buffer);
1230 }
1231
1232 /**
1233  * theora_enc_encode_and_push - encode buffer or queued previous buffer
1234  * buffer - buffer to encode. If set to NULL it should encode only
1235  *          queued buffers and produce dups if needed.
1236  */
1237
1238 static GstFlowReturn
1239 theora_enc_encode_and_push (GstTheoraEnc * enc, ogg_packet op,
1240     GstBuffer * buffer)
1241 {
1242   GstFlowReturn ret;
1243   GstVideoFrame frame;
1244   th_ycbcr_buffer ycbcr;
1245   gint res;
1246
1247   if (enc->dup_on_gap) {
1248     guint t_queue_length;
1249
1250     if (enc->t_queue)
1251       t_queue_length = g_queue_get_length (enc->t_queue);
1252     else
1253       t_queue_length = 0;
1254
1255     if (buffer) {
1256       GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
1257
1258       /* videorate can easy create 200 dup frames in one shot.
1259        * In this case th_encode_ctl will just return TH_EINVAL
1260        * and we will generate only one frame as result.
1261        * To make us more bullet proof, make sure we have no
1262        * more dup frames than keyframe interval.
1263        */
1264       if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_GAP) &&
1265           enc->keyframe_force > t_queue_length) {
1266         GST_DEBUG_OBJECT (enc, "Got GAP frame, queue as duplicate.");
1267
1268         theora_timefifo_in (enc, &timestamp);
1269         gst_buffer_unref (buffer);
1270         return GST_FLOW_OK;
1271       } else {
1272         theora_timefifo_in (enc, &timestamp);
1273         /* We should have one frame delay to create correct frame order.
1274          * First time we got buffer, prevbuf should be empty. Nothing else
1275          * should be done here.
1276          */
1277         if (!enc->prevbuf) {
1278           theora_update_prevbuf (enc, buffer);
1279           gst_buffer_unref (buffer);
1280           return GST_FLOW_OK;
1281         } else {
1282           theora_update_prevbuf (enc, buffer);
1283           /* after theora_update_prevbuf t_queue_length was changed */
1284           t_queue_length++;
1285
1286           if (t_queue_length > 2) {
1287             /* now in t_queue_length should be two real buffers: current and
1288              * previous. All others are timestamps of duplicate frames. */
1289             t_queue_length -= 2;
1290             res = th_encode_ctl (enc->encoder, TH_ENCCTL_SET_DUP_COUNT,
1291                 &t_queue_length, sizeof (t_queue_length));
1292             if (res < 0)
1293               GST_WARNING_OBJECT (enc, "Failed marking dups for last frame");
1294           }
1295         }
1296       }
1297     } else {
1298       /* if there is no buffer, then probably we got EOS or discontinuous.
1299        * We need to encode every thing what was left in the queue
1300        */
1301       GST_DEBUG_OBJECT (enc, "Encode collected buffers.");
1302       if (t_queue_length > 1) {
1303         t_queue_length--;
1304         res = th_encode_ctl (enc->encoder, TH_ENCCTL_SET_DUP_COUNT,
1305             &t_queue_length, sizeof (t_queue_length));
1306         if (res < 0)
1307           GST_WARNING_OBJECT (enc, "Failed marking dups for last frame.");
1308       } else {
1309         GST_DEBUG_OBJECT (enc, "Prevbuffer is empty. Nothing to encode.");
1310         return GST_FLOW_OK;
1311       }
1312     }
1313     gst_video_frame_map (&frame, &enc->vinfo, enc->prevbuf, GST_MAP_READ);
1314     theora_enc_init_buffer (ycbcr, &frame);
1315   } else {
1316     gst_video_frame_map (&frame, &enc->vinfo, buffer, GST_MAP_READ);
1317     theora_enc_init_buffer (ycbcr, &frame);
1318   }
1319
1320   /* check for buffer, it can be optional */
1321   if (enc->current_discont && buffer) {
1322     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
1323     GstClockTime running_time =
1324         gst_segment_to_running_time (&enc->segment, GST_FORMAT_TIME, timestamp);
1325     theora_enc_reset (enc);
1326     enc->granulepos_offset =
1327         gst_util_uint64_scale (running_time, enc->vinfo.fps_n,
1328         GST_SECOND * enc->vinfo.fps_d);
1329     enc->timestamp_offset = running_time;
1330     enc->next_ts = 0;
1331     enc->next_discont = TRUE;
1332   }
1333
1334   if (enc->multipass_cache_fd
1335       && enc->multipass_mode == MULTIPASS_MODE_SECOND_PASS) {
1336     if (!theora_enc_read_multipass_cache (enc)) {
1337       ret = GST_FLOW_ERROR;
1338       goto multipass_read_failed;
1339     }
1340   }
1341 #ifdef TH_ENCCTL_SET_DUPLICATE_FLAG
1342   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_GAP)) {
1343     th_encode_ctl (enc->encoder, TH_ENCCTL_SET_DUPLICATE_FLAG, NULL, 0);
1344   }
1345 #endif
1346
1347   res = th_encode_ycbcr_in (enc->encoder, ycbcr);
1348   /* none of the failure cases can happen here */
1349   g_assert (res == 0);
1350
1351   if (enc->multipass_cache_fd
1352       && enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS) {
1353     if (!theora_enc_write_multipass_cache (enc, FALSE, FALSE)) {
1354       ret = GST_FLOW_ERROR;
1355       goto multipass_write_failed;
1356     }
1357   }
1358
1359   ret = GST_FLOW_OK;
1360   while (th_encode_packetout (enc->encoder, 0, &op)) {
1361     GstClockTime next_time, duration;
1362     GstClockTime timestamp = 0;
1363     GST_DEBUG_OBJECT (enc, "encoded. granule:%" G_GINT64_FORMAT ", packet:%p, "
1364         "bytes:%ld", (gint64) op.granulepos, op.packet, op.bytes);
1365
1366     next_time = th_granule_time (enc->encoder, op.granulepos) * GST_SECOND;
1367     duration = next_time - enc->next_ts;
1368
1369     if (enc->dup_on_gap && !enc->current_discont)
1370       timestamp = theora_timefifo_out (enc);
1371     else
1372       timestamp = GST_BUFFER_TIMESTAMP (buffer);
1373
1374     ret = theora_push_packet (enc, &op, timestamp, enc->next_ts, duration);
1375
1376     enc->next_ts = next_time;
1377     if (ret != GST_FLOW_OK) {
1378       theora_timefifo_truncate (enc);
1379       goto data_push;
1380     }
1381   }
1382
1383   theora_timefifo_truncate (enc);
1384 done:
1385   gst_video_frame_unmap (&frame);
1386   if (buffer)
1387     gst_buffer_unref (buffer);
1388   enc->current_discont = FALSE;
1389
1390   return ret;
1391
1392   /* ERRORS */
1393 multipass_read_failed:
1394   {
1395     GST_DEBUG_OBJECT (enc, "multipass read failed");
1396     goto done;
1397   }
1398 multipass_write_failed:
1399   {
1400     GST_DEBUG_OBJECT (enc, "multipass write failed");
1401     goto done;
1402   }
1403 data_push:
1404   {
1405     GST_DEBUG_OBJECT (enc, "error pushing buffer: %s", gst_flow_get_name (ret));
1406     goto done;
1407   }
1408 }
1409
1410 static GstFlowReturn
1411 theora_enc_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1412 {
1413   GstTheoraEnc *enc;
1414   ogg_packet op;
1415   GstClockTime timestamp, duration, running_time;
1416   GstFlowReturn ret;
1417   gboolean force_keyframe;
1418
1419   enc = GST_THEORA_ENC (parent);
1420
1421   /* we keep track of two timelines.
1422    * - The timestamps from the incoming buffers, which we copy to the outgoing
1423    *   encoded buffers as-is. We need to do this as we simply forward the
1424    *   newsegment events.
1425    * - The running_time of the buffers, which we use to construct the granulepos
1426    *   in the packets.
1427    */
1428   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1429   duration = GST_BUFFER_DURATION (buffer);
1430
1431   running_time =
1432       gst_segment_to_running_time (&enc->segment, GST_FORMAT_TIME, timestamp);
1433   if ((gint64) running_time < 0) {
1434     GST_DEBUG_OBJECT (enc, "Dropping buffer, timestamp: %" GST_TIME_FORMAT,
1435         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
1436     gst_buffer_unref (buffer);
1437     return GST_FLOW_OK;
1438   }
1439
1440   GST_OBJECT_LOCK (enc);
1441   if (enc->bitrate_changed) {
1442     long int bitrate = enc->video_bitrate;
1443
1444     th_encode_ctl (enc->encoder, TH_ENCCTL_SET_BITRATE, &bitrate,
1445         sizeof (long int));
1446     enc->bitrate_changed = FALSE;
1447   }
1448
1449   if (enc->quality_changed) {
1450     long int quality = enc->video_quality;
1451
1452     th_encode_ctl (enc->encoder, TH_ENCCTL_SET_QUALITY, &quality,
1453         sizeof (long int));
1454     enc->quality_changed = FALSE;
1455   }
1456
1457   /* see if we need to schedule a keyframe */
1458   force_keyframe = enc->force_keyframe;
1459   enc->force_keyframe = FALSE;
1460   GST_OBJECT_UNLOCK (enc);
1461
1462   if (force_keyframe) {
1463     GstClockTime stream_time;
1464     GstStructure *s;
1465
1466     stream_time = gst_segment_to_stream_time (&enc->segment,
1467         GST_FORMAT_TIME, timestamp);
1468
1469     s = gst_structure_new ("GstForceKeyUnit",
1470         "timestamp", G_TYPE_UINT64, timestamp,
1471         "stream-time", G_TYPE_UINT64, stream_time,
1472         "running-time", G_TYPE_UINT64, running_time, NULL);
1473
1474     theora_enc_force_keyframe (enc);
1475
1476     gst_pad_push_event (enc->srcpad,
1477         gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s));
1478   }
1479
1480   /* make sure we copy the discont flag to the next outgoing buffer when it's
1481    * set on the incoming buffer */
1482   if (GST_BUFFER_IS_DISCONT (buffer)) {
1483     enc->next_discont = TRUE;
1484   }
1485
1486   if (enc->packetno == 0) {
1487     /* no packets written yet, setup headers */
1488     GstCaps *caps;
1489     GstBuffer *buf;
1490     GSList *buffers = NULL;
1491     int result;
1492
1493     enc->granulepos_offset = 0;
1494     enc->timestamp_offset = 0;
1495
1496     GST_DEBUG_OBJECT (enc, "output headers");
1497     /* Theora streams begin with three headers; the initial header (with
1498        most of the codec setup parameters) which is mandated by the Ogg
1499        bitstream spec.  The second header holds any comment fields.  The
1500        third header holds the bitstream codebook.  We merely need to
1501        make the headers, then pass them to libtheora one at a time;
1502        libtheora handles the additional Ogg bitstream constraints */
1503
1504     /* create the remaining theora headers */
1505     th_comment_clear (&enc->comment);
1506     th_comment_init (&enc->comment);
1507
1508     while ((result =
1509             th_encode_flushheader (enc->encoder, &enc->comment, &op)) > 0) {
1510       ret =
1511           theora_buffer_from_packet (enc, &op, GST_CLOCK_TIME_NONE,
1512           GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, &buf);
1513       if (ret != GST_FLOW_OK) {
1514         goto header_buffer_alloc;
1515       }
1516       buffers = g_slist_prepend (buffers, buf);
1517     }
1518     if (result < 0) {
1519       g_slist_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
1520       g_slist_free (buffers);
1521       goto encoder_disabled;
1522     }
1523
1524     buffers = g_slist_reverse (buffers);
1525
1526     /* mark buffers and put on caps */
1527     caps = gst_caps_new_simple ("video/x-theora",
1528         "width", G_TYPE_INT, enc->vinfo.width,
1529         "height", G_TYPE_INT, enc->vinfo.height,
1530         "framerate", GST_TYPE_FRACTION, enc->vinfo.fps_n, enc->vinfo.fps_d,
1531         "pixel-aspect-ratio", GST_TYPE_FRACTION, enc->vinfo.par_n,
1532         enc->vinfo.par_d, NULL);
1533     caps = theora_set_header_on_caps (caps, buffers);
1534     GST_DEBUG ("here are the caps: %" GST_PTR_FORMAT, caps);
1535     gst_pad_set_caps (enc->srcpad, caps);
1536     gst_caps_unref (caps);
1537
1538     /* push out the header buffers */
1539     while (buffers) {
1540       buf = buffers->data;
1541       buffers = g_slist_delete_link (buffers, buffers);
1542       if ((ret = theora_push_buffer (enc, buf)) != GST_FLOW_OK) {
1543         g_slist_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
1544         g_slist_free (buffers);
1545         goto header_push;
1546       }
1547     }
1548
1549     enc->granulepos_offset =
1550         gst_util_uint64_scale (running_time, enc->vinfo.fps_n,
1551         GST_SECOND * enc->vinfo.fps_d);
1552     enc->timestamp_offset = running_time;
1553     enc->next_ts = 0;
1554   }
1555
1556   enc->current_discont = theora_enc_is_discontinuous (enc,
1557       running_time, duration);
1558
1559   /* empty queue if discontinuous */
1560   if (enc->current_discont && enc->dup_on_gap)
1561     theora_enc_encode_and_push (enc, op, NULL);
1562
1563   ret = theora_enc_encode_and_push (enc, op, buffer);
1564
1565   return ret;
1566
1567   /* ERRORS */
1568 header_buffer_alloc:
1569   {
1570     gst_buffer_unref (buffer);
1571     return ret;
1572   }
1573 header_push:
1574   {
1575     gst_buffer_unref (buffer);
1576     return ret;
1577   }
1578 encoder_disabled:
1579   {
1580     GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
1581         ("libtheora has been compiled with the encoder disabled"));
1582     gst_buffer_unref (buffer);
1583     return GST_FLOW_ERROR;
1584   }
1585 }
1586
1587 static GstStateChangeReturn
1588 theora_enc_change_state (GstElement * element, GstStateChange transition)
1589 {
1590   GstTheoraEnc *enc;
1591   GstStateChangeReturn ret;
1592   th_enc_ctx *th_ctx;
1593
1594   enc = GST_THEORA_ENC (element);
1595
1596   switch (transition) {
1597     case GST_STATE_CHANGE_NULL_TO_READY:
1598       th_ctx = dummy_encode_ctx ();
1599       if (!th_ctx) {
1600         GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
1601             ("libtheora has been compiled with the encoder disabled"));
1602         return GST_STATE_CHANGE_FAILURE;
1603       }
1604       th_encode_free (th_ctx);
1605       break;
1606     case GST_STATE_CHANGE_READY_TO_PAUSED:
1607       GST_DEBUG_OBJECT (enc, "READY->PAUSED Initing theora state");
1608       th_info_init (&enc->info);
1609       th_comment_init (&enc->comment);
1610       enc->packetno = 0;
1611       enc->force_keyframe = FALSE;
1612
1613       if (enc->multipass_mode >= MULTIPASS_MODE_FIRST_PASS) {
1614         GError *err = NULL;
1615
1616         if (!enc->multipass_cache_file) {
1617           ret = GST_STATE_CHANGE_FAILURE;
1618           GST_ELEMENT_ERROR (enc, LIBRARY, SETTINGS, (NULL), (NULL));
1619           return ret;
1620         }
1621         enc->multipass_cache_fd =
1622             g_io_channel_new_file (enc->multipass_cache_file,
1623             (enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS ? "w" : "r"),
1624             &err);
1625
1626         if (enc->multipass_mode == MULTIPASS_MODE_SECOND_PASS)
1627           enc->multipass_cache_adapter = gst_adapter_new ();
1628
1629         if (!enc->multipass_cache_fd) {
1630           ret = GST_STATE_CHANGE_FAILURE;
1631           GST_ELEMENT_ERROR (enc, RESOURCE, OPEN_READ, (NULL),
1632               ("Failed to open multipass cache file: %s", err->message));
1633           g_error_free (err);
1634           return ret;
1635         }
1636
1637         g_io_channel_set_encoding (enc->multipass_cache_fd, NULL, NULL);
1638       }
1639       break;
1640     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1641       break;
1642     default:
1643       break;
1644   }
1645
1646   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1647
1648   switch (transition) {
1649     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1650       break;
1651     case GST_STATE_CHANGE_PAUSED_TO_READY:
1652       GST_DEBUG_OBJECT (enc, "PAUSED->READY Clearing theora state");
1653       if (enc->encoder) {
1654         th_encode_free (enc->encoder);
1655         enc->encoder = NULL;
1656       }
1657       th_comment_clear (&enc->comment);
1658       th_info_clear (&enc->info);
1659
1660       theora_enc_clear (enc);
1661       enc->initialised = FALSE;
1662       break;
1663     case GST_STATE_CHANGE_READY_TO_NULL:
1664       break;
1665     default:
1666       break;
1667   }
1668
1669   return ret;
1670 }
1671
1672 static void
1673 theora_enc_set_property (GObject * object, guint prop_id,
1674     const GValue * value, GParamSpec * pspec)
1675 {
1676   GstTheoraEnc *enc = GST_THEORA_ENC (object);
1677
1678   switch (prop_id) {
1679     case PROP_BITRATE:
1680       GST_OBJECT_LOCK (enc);
1681       enc->video_bitrate = g_value_get_int (value) * 1000;
1682       enc->bitrate_changed = TRUE;
1683       GST_OBJECT_UNLOCK (enc);
1684       break;
1685     case PROP_QUALITY:
1686       GST_OBJECT_LOCK (enc);
1687       if (GST_STATE (enc) >= GST_STATE_PAUSED && enc->video_bitrate > 0) {
1688         GST_WARNING_OBJECT (object, "Can't change from bitrate to quality mode"
1689             " while playing");
1690       } else {
1691         enc->video_quality = g_value_get_int (value);
1692         enc->video_bitrate = 0;
1693         enc->quality_changed = TRUE;
1694       }
1695       GST_OBJECT_UNLOCK (enc);
1696       break;
1697     case PROP_KEYFRAME_AUTO:
1698       enc->keyframe_auto = g_value_get_boolean (value);
1699       break;
1700     case PROP_KEYFRAME_FREQ:
1701       enc->keyframe_freq = g_value_get_int (value);
1702       break;
1703     case PROP_KEYFRAME_FREQ_FORCE:
1704       enc->keyframe_force = g_value_get_int (value);
1705       break;
1706     case PROP_SPEEDLEVEL:
1707       enc->speed_level = g_value_get_int (value);
1708       if (enc->encoder) {
1709         th_encode_ctl (enc->encoder, TH_ENCCTL_SET_SPLEVEL, &enc->speed_level,
1710             sizeof (enc->speed_level));
1711       }
1712       break;
1713     case PROP_VP3_COMPATIBLE:
1714       enc->vp3_compatible = g_value_get_boolean (value);
1715       break;
1716     case PROP_DROP_FRAMES:
1717       enc->drop_frames = g_value_get_boolean (value);
1718       break;
1719     case PROP_CAP_OVERFLOW:
1720       enc->cap_overflow = g_value_get_boolean (value);
1721       break;
1722     case PROP_CAP_UNDERFLOW:
1723       enc->cap_underflow = g_value_get_boolean (value);
1724       break;
1725     case PROP_RATE_BUFFER:
1726       enc->rate_buffer = g_value_get_int (value);
1727       break;
1728     case PROP_MULTIPASS_CACHE_FILE:
1729       enc->multipass_cache_file = g_value_dup_string (value);
1730       break;
1731     case PROP_MULTIPASS_MODE:
1732       enc->multipass_mode = g_value_get_enum (value);
1733       break;
1734     case PROP_DUP_ON_GAP:
1735       enc->dup_on_gap = g_value_get_boolean (value);
1736       break;
1737     default:
1738       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1739       break;
1740   }
1741 }
1742
1743 static void
1744 theora_enc_get_property (GObject * object, guint prop_id,
1745     GValue * value, GParamSpec * pspec)
1746 {
1747   GstTheoraEnc *enc = GST_THEORA_ENC (object);
1748
1749   switch (prop_id) {
1750     case PROP_BITRATE:
1751       GST_OBJECT_LOCK (enc);
1752       g_value_set_int (value, enc->video_bitrate / 1000);
1753       GST_OBJECT_UNLOCK (enc);
1754       break;
1755     case PROP_QUALITY:
1756       GST_OBJECT_LOCK (enc);
1757       g_value_set_int (value, enc->video_quality);
1758       GST_OBJECT_UNLOCK (enc);
1759       break;
1760     case PROP_KEYFRAME_AUTO:
1761       g_value_set_boolean (value, enc->keyframe_auto);
1762       break;
1763     case PROP_KEYFRAME_FREQ:
1764       g_value_set_int (value, enc->keyframe_freq);
1765       break;
1766     case PROP_KEYFRAME_FREQ_FORCE:
1767       g_value_set_int (value, enc->keyframe_force);
1768       break;
1769     case PROP_SPEEDLEVEL:
1770       g_value_set_int (value, enc->speed_level);
1771       break;
1772     case PROP_VP3_COMPATIBLE:
1773       g_value_set_boolean (value, enc->vp3_compatible);
1774       break;
1775     case PROP_DROP_FRAMES:
1776       g_value_set_boolean (value, enc->drop_frames);
1777       break;
1778     case PROP_CAP_OVERFLOW:
1779       g_value_set_boolean (value, enc->cap_overflow);
1780       break;
1781     case PROP_CAP_UNDERFLOW:
1782       g_value_set_boolean (value, enc->cap_underflow);
1783       break;
1784     case PROP_RATE_BUFFER:
1785       g_value_set_int (value, enc->rate_buffer);
1786       break;
1787     case PROP_MULTIPASS_CACHE_FILE:
1788       g_value_set_string (value, enc->multipass_cache_file);
1789       break;
1790     case PROP_MULTIPASS_MODE:
1791       g_value_set_enum (value, enc->multipass_mode);
1792       break;
1793     case PROP_DUP_ON_GAP:
1794       g_value_set_boolean (value, enc->dup_on_gap);
1795       break;
1796     default:
1797       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1798       break;
1799   }
1800 }