summaryrefslogtreecommitdiff
path: root/ext/mplex/gstmplex.cc
blob: 8f2c93853f8a8770f33bec4748e8988048b020f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* GStreamer mplex (mjpegtools) wrapper
 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * gstmplex.cc: gstreamer mplex wrapper
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

#include "gstmplex.hh"
#include "gstmplexoutputstream.hh"
#include "gstmplexibitstream.hh"
#include "gstmplexjob.hh"

static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/mpeg, " "systemstream = (boolean) true")
    );

static GstStaticPadTemplate video_sink_templ =
GST_STATIC_PAD_TEMPLATE ("video_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("video/mpeg, "
	"mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) false")
    );

static GstStaticPadTemplate audio_sink_templ =
    GST_STATIC_PAD_TEMPLATE ("audio_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("audio/mpeg, "
	"mpegversion = (int) 1, "
	"layer = (int) [ 1, 2 ]; "
	"audio/x-ac3; "
	"audio/x-dts; "
	"audio/x-raw-int, "
	"endianness = (int) BYTE_ORDER, "
	"signed = (boolean) TRUE, "
	"width = (int) { 16, 20, 24 }, "
	"depth = (int) { 16, 20, 24 }, "
	"rate = (int) { 48000, 96000 }, " "channels = (int) [ 1, 6 ]")
    );

/* FIXME: subtitles */

static void gst_mplex_base_init (GstMplexClass * klass);
static void gst_mplex_class_init (GstMplexClass * klass);
static void gst_mplex_init (GstMplex * enc);
static void gst_mplex_dispose (GObject * object);

static void gst_mplex_loop (GstElement * element);

static GstPad *gst_mplex_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name);

static GstElementStateReturn gst_mplex_change_state (GstElement * element);

static void gst_mplex_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_mplex_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);

static GstElementClass *parent_class = NULL;

GType
gst_mplex_get_type (void)
{
  static GType gst_mplex_type = 0;

  if (!gst_mplex_type) {
    static const GTypeInfo gst_mplex_info = {
      sizeof (GstMplexClass),
      (GBaseInitFunc) gst_mplex_base_init,
      NULL,
      (GClassInitFunc) gst_mplex_class_init,
      NULL,
      NULL,
      sizeof (GstMplex),
      0,
      (GInstanceInitFunc) gst_mplex_init,
    };

    gst_mplex_type =
	g_type_register_static (GST_TYPE_ELEMENT,
	"GstMplex", &gst_mplex_info, (GTypeFlags) 0);
  }

  return gst_mplex_type;
}

static void
gst_mplex_base_init (GstMplexClass * klass)
{
  static GstElementDetails gst_mplex_details = {
    "mplex video multiplexer",
    "Codec/Muxer",
    "High-quality MPEG/DVD/SVCD/VCD video/audio multiplexer",
    "Andrew Stevens <andrew.stevens@nexgo.de>\n"
	"Ronald Bultje <rbultje@ronald.bitfreak.net>"
  };
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_templ));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&video_sink_templ));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&audio_sink_templ));
  gst_element_class_set_details (element_class, &gst_mplex_details);
}

static void
gst_mplex_class_init (GstMplexClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  parent_class = GST_ELEMENT_CLASS (g_type_class_ref (GST_TYPE_ELEMENT));

  /* register arguments */
  mjpeg_default_handler_verbosity (0);
  GstMplexJob::initProperties (object_class);

  object_class->set_property = gst_mplex_set_property;
  object_class->get_property = gst_mplex_get_property;

  object_class->dispose = gst_mplex_dispose;

  element_class->change_state = gst_mplex_change_state;
  element_class->request_new_pad = gst_mplex_request_new_pad;
}

static void
gst_mplex_dispose (GObject * object)
{
  GstMplex *mplex = GST_MPLEX (object);

  if (mplex->mux) {
    delete mplex->mux;

    mplex->mux = NULL;
  }
  delete mplex->job;
}

static void
gst_mplex_init (GstMplex * mplex)
{
  GstElement *element = GST_ELEMENT (mplex);

  GST_FLAG_SET (element, GST_ELEMENT_EVENT_AWARE);

  mplex->srcpad =
      gst_pad_new_from_template (gst_element_get_pad_template (element, "src"),
      "src");
  gst_element_add_pad (element, mplex->srcpad);

  mplex->job = new GstMplexJob ();
  mplex->mux = NULL;
  mplex->num_apads = 0;
  mplex->num_vpads = 0;

  gst_element_set_loop_function (element, gst_mplex_loop);
}

static void
gst_mplex_loop (GstElement * element)
{
  GstMplex *mplex = GST_MPLEX (element);

  if (!mplex->mux) {
    GstMplexOutputStream *out;
    const GList *item;

    for (item = gst_element_get_pad_list (element);
	item != NULL; item = item->next) {
      StreamKind type;
      GstMplexIBitStream *inputstream;
      JobStream *jobstream;
      GstPad *pad = GST_PAD (item->data);
      GstStructure *structure;
      const GstCaps *caps;
      const gchar *mime;

      /* skip our source pad */
      if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
	continue;

      /* create inputstream, assure we've got caps */
      inputstream = new GstMplexIBitStream (pad);

      /* skip unnegotiated pads */
      if (!(caps = GST_PAD_CAPS (pad))) {
	delete inputstream;

	continue;
      }

      /* get format */
      structure = gst_caps_get_structure (caps, 0);
      mime = gst_structure_get_name (structure);

      if (!strcmp (mime, "video/mpeg")) {
	VideoParams *params;

	type = MPEG_VIDEO;

	params = VideoParams::Default (mplex->job->mux_format);
	mplex->job->video_param.push_back (params);
	mplex->job->video_tracks++;
      } else if (!strcmp (mime, "audio/mpeg")) {
	type = MPEG_AUDIO;
	mplex->job->audio_tracks++;
      } else if (!strcmp (mime, "audio/x-ac3")) {
	type = AC3_AUDIO;
	mplex->job->audio_tracks++;
      } else if (!strcmp (mime, "audio/x-dts")) {
	type = DTS_AUDIO;
	mplex->job->audio_tracks++;
      } else if (!strcmp (mime, "audio/x-raw-int")) {
	LpcmParams *params;
	gint bits, chans, rate;

	type = LPCM_AUDIO;

	/* set LPCM params */
	gst_structure_get_int (structure, "depth", &bits);
	gst_structure_get_int (structure, "rate", &rate);
	gst_structure_get_int (structure, "channels", &chans);
	params = LpcmParams::Checked (rate, chans, bits);

	mplex->job->lpcm_param.push_back (params);
	mplex->job->audio_tracks++;
	mplex->job->lpcm_tracks++;
      } else {
	delete inputstream;

	continue;
      }

      jobstream = new JobStream (inputstream, type);
      mplex->job->streams.push_back (jobstream);
    }

    if (!mplex->job->video_tracks && !mplex->job->audio_tracks) {
      GST_ELEMENT_ERROR (element, CORE, NEGOTIATION, (NULL),
	  ("no input video or audio tracks set up before loop function"));
      return;
    }

    /* create new encoder with inputs/output */
    out = new GstMplexOutputStream (element, mplex->srcpad);
    mplex->mux = new Multiplexor (*mplex->job, *out);
  }

  mplex->mux->Multiplex ();
}

static GstPadLinkReturn
gst_mplex_sink_link (GstPad * pad, const GstCaps * caps)
{
  GstStructure *structure = gst_caps_get_structure (caps, 0);
  const gchar *mime = gst_structure_get_name (structure);

  /* raw audio caps needs to be fixed */
  if (!strcmp (mime, "audio/x-raw-int")) {
    gint width, depth;

    if (!gst_caps_is_fixed (caps))
      return GST_PAD_LINK_DELAYED;

    gst_structure_get_int (structure, "width", &width);
    gst_structure_get_int (structure, "depth", &depth);

    if (depth != width)
      return GST_PAD_LINK_REFUSED;
  }

  /* we do the actual inputstream setup in our first loopfunc cycle */
  return GST_PAD_LINK_OK;
}

static GstPad *
gst_mplex_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
  GstMplex *mplex = GST_MPLEX (element);
  gchar *padname;
  GstPad *newpad;

  if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
    padname = g_strdup_printf ("audio_%d", mplex->num_apads++);
  } else if (templ == gst_element_class_get_pad_template (klass, "video_%d")) {
    padname = g_strdup_printf ("video_%d", mplex->num_vpads++);
  } else {
    g_warning ("mplex: this is not our template!");
    return NULL;
  }

  newpad = gst_pad_new_from_template (templ, padname);
  gst_pad_set_link_function (newpad, gst_mplex_sink_link);
  gst_element_add_pad (element, newpad);
  g_free (padname);

  return newpad;
}

static void
gst_mplex_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec)
{
  GST_MPLEX (object)->job->getProperty (prop_id, value);
}

static void
gst_mplex_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  GST_MPLEX (object)->job->setProperty (prop_id, value);
}

static GstElementStateReturn
gst_mplex_change_state (GstElement * element)
{
  GstMplex *mplex = GST_MPLEX (element);

  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_PAUSED_TO_READY:
      delete mplex->mux;
      mplex->mux = NULL;
      mplex->num_apads = 0;
      mplex->num_vpads = 0;
      break;
    default:
      break;
  }

  if (parent_class->change_state)
    return parent_class->change_state (element);

  return GST_STATE_SUCCESS;
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  if (!gst_library_load ("gstbytestream"))
    return FALSE;

  return gst_element_register (plugin, "mplex", GST_RANK_NONE, GST_TYPE_MPLEX);
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "mplex",
    "High-quality MPEG/DVD/SVCD/VCD video/audio multiplexer",
    plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)