1
/* GStreamer Adaptive Multi-Rate Wide-Band (AMR-WB) plugin
2
* Copyright (C) 2006 Edgard Lima <edgard.lima@indt.org.br>
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.
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.
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.
21
* SECTION:element-amrwbparse
22
* @see_also: #GstAmrwbDec, #GstAmrwbEnc
24
* This is an AMR wideband parser.
27
* <title>Example launch line</title>
29
* gst-launch filesrc location=abc.amr ! amrwbparse ! amrwbdec ! audioresample ! audioconvert ! alsasink
39
#include "gstamrwbparse.h"
41
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
44
GST_STATIC_CAPS ("audio/AMR-WB, "
45
"rate = (int) 16000, " "channels = (int) 1")
48
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
51
GST_STATIC_CAPS ("audio/x-amr-wb-sh")
54
GST_DEBUG_CATEGORY_STATIC (gst_amrwbparse_debug);
55
#define GST_CAT_DEFAULT gst_amrwbparse_debug
57
extern const UWord8 block_size[];
59
#define AMRWB_HEADER_SIZE 9
60
#define AMRWB_HEADER_STR "#!AMR-WB\n"
62
static void gst_amrwbparse_base_init (gpointer klass);
63
static void gst_amrwbparse_class_init (GstAmrwbParseClass * klass);
64
static void gst_amrwbparse_init (GstAmrwbParse * amrwbparse,
65
GstAmrwbParseClass * klass);
67
static const GstQueryType *gst_amrwbparse_querytypes (GstPad * pad);
68
static gboolean gst_amrwbparse_query (GstPad * pad, GstQuery * query);
70
static gboolean gst_amrwbparse_sink_event (GstPad * pad, GstEvent * event);
71
static gboolean gst_amrwbparse_src_event (GstPad * pad, GstEvent * event);
72
static GstFlowReturn gst_amrwbparse_chain (GstPad * pad, GstBuffer * buffer);
73
static void gst_amrwbparse_loop (GstPad * pad);
74
static gboolean gst_amrwbparse_sink_activate (GstPad * sinkpad);
75
static gboolean gst_amrwbparse_sink_activate_pull (GstPad * sinkpad,
77
static gboolean gst_amrwbparse_sink_activate_push (GstPad * sinkpad,
79
static GstStateChangeReturn gst_amrwbparse_state_change (GstElement * element,
80
GstStateChange transition);
82
static void gst_amrwbparse_finalize (GObject * object);
84
#define _do_init(bla) \
85
GST_DEBUG_CATEGORY_INIT (gst_amrwbparse_debug, "amrwbparse", 0, "AMR-WB audio stream parser");
87
GST_BOILERPLATE_FULL (GstAmrwbParse, gst_amrwbparse, GstElement,
88
GST_TYPE_ELEMENT, _do_init);
91
gst_amrwbparse_base_init (gpointer klass)
93
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
94
GstElementDetails details = GST_ELEMENT_DETAILS ("AMR-WB audio stream parser",
96
"Adaptive Multi-Rate WideBand audio parser",
97
"Renato Filho <renato.filho@indt.org.br>");
99
gst_element_class_add_pad_template (element_class,
100
gst_static_pad_template_get (&sink_template));
101
gst_element_class_add_pad_template (element_class,
102
gst_static_pad_template_get (&src_template));
104
gst_element_class_set_details (element_class, &details);
109
gst_amrwbparse_class_init (GstAmrwbParseClass * klass)
111
GObjectClass *object_class = G_OBJECT_CLASS (klass);
112
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
114
object_class->finalize = gst_amrwbparse_finalize;
116
element_class->change_state = GST_DEBUG_FUNCPTR (gst_amrwbparse_state_change);
120
gst_amrwbparse_init (GstAmrwbParse * amrwbparse, GstAmrwbParseClass * klass)
122
/* create the sink pad */
123
amrwbparse->sinkpad =
124
gst_pad_new_from_static_template (&sink_template, "sink");
125
gst_pad_set_chain_function (amrwbparse->sinkpad,
126
GST_DEBUG_FUNCPTR (gst_amrwbparse_chain));
127
gst_pad_set_event_function (amrwbparse->sinkpad,
128
GST_DEBUG_FUNCPTR (gst_amrwbparse_sink_event));
129
gst_pad_set_activate_function (amrwbparse->sinkpad,
130
gst_amrwbparse_sink_activate);
131
gst_pad_set_activatepull_function (amrwbparse->sinkpad,
132
gst_amrwbparse_sink_activate_pull);
133
gst_pad_set_activatepush_function (amrwbparse->sinkpad,
134
gst_amrwbparse_sink_activate_push);
135
gst_element_add_pad (GST_ELEMENT (amrwbparse), amrwbparse->sinkpad);
137
/* create the src pad */
138
amrwbparse->srcpad = gst_pad_new_from_static_template (&src_template, "src");
139
gst_pad_set_event_function (amrwbparse->srcpad,
140
GST_DEBUG_FUNCPTR (gst_amrwbparse_src_event));
141
gst_pad_set_query_function (amrwbparse->srcpad,
142
GST_DEBUG_FUNCPTR (gst_amrwbparse_query));
143
gst_pad_set_query_type_function (amrwbparse->srcpad,
144
GST_DEBUG_FUNCPTR (gst_amrwbparse_querytypes));
145
gst_element_add_pad (GST_ELEMENT (amrwbparse), amrwbparse->srcpad);
147
amrwbparse->adapter = gst_adapter_new ();
154
gst_amrwbparse_finalize (GObject * object)
156
GstAmrwbParse *amrwbparse;
158
amrwbparse = GST_AMRWBPARSE (object);
160
gst_adapter_clear (amrwbparse->adapter);
161
g_object_unref (amrwbparse->adapter);
163
G_OBJECT_CLASS (parent_class)->finalize (object);
167
static const GstQueryType *
168
gst_amrwbparse_querytypes (GstPad * pad)
170
static const GstQueryType list[] = {
180
gst_amrwbparse_query (GstPad * pad, GstQuery * query)
182
GstAmrwbParse *amrwbparse;
185
amrwbparse = GST_AMRWBPARSE (gst_pad_get_parent (pad));
187
switch (GST_QUERY_TYPE (query)) {
188
case GST_QUERY_POSITION:
193
gst_query_parse_position (query, &format, NULL);
195
if (format != GST_FORMAT_TIME) {
200
cur = amrwbparse->ts;
202
gst_query_set_position (query, GST_FORMAT_TIME, cur);
206
case GST_QUERY_DURATION:
212
gst_query_parse_duration (query, &format, NULL);
214
if (format != GST_FORMAT_TIME) {
222
peer = gst_pad_get_peer (amrwbparse->sinkpad);
227
pformat = GST_FORMAT_BYTES;
228
res = gst_pad_query_duration (peer, &pformat, &ptot);
229
if (res && amrwbparse->block) {
230
tot = gst_util_uint64_scale_int (ptot, 20 * GST_MSECOND,
233
gst_object_unref (GST_OBJECT (peer));
235
gst_query_set_duration (query, GST_FORMAT_TIME, tot);
240
res = gst_pad_query_default (pad, query);
244
gst_object_unref (amrwbparse);
250
gst_amrwbparse_handle_pull_seek (GstAmrwbParse * amrwbparse, GstPad * pad,
256
GstSeekType cur_type, stop_type;
258
gint64 byte_cur = -1, byte_stop = -1;
261
gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
264
GST_DEBUG_OBJECT (amrwbparse, "Performing seek to %" GST_TIME_FORMAT,
265
GST_TIME_ARGS (cur));
267
/* For any format other than TIME, see if upstream handles
268
* it directly or fail. For TIME, try upstream, but do it ourselves if
269
* it fails upstream */
270
if (format != GST_FORMAT_TIME) {
271
return gst_pad_push_event (amrwbparse->sinkpad, event);
273
if (gst_pad_push_event (amrwbparse->sinkpad, event))
277
flush = flags & GST_SEEK_FLAG_FLUSH;
279
/* send flush start */
281
gst_pad_push_event (amrwbparse->sinkpad, gst_event_new_flush_start ());
282
/* we only handle FLUSH seeks at the moment */
286
/* grab streaming lock, this should eventually be possible, either
287
* because the task is paused or our streaming thread stopped
288
* because our peer is flushing. */
289
GST_PAD_STREAM_LOCK (amrwbparse->sinkpad);
291
/* Convert the TIME to the appropriate BYTE position at which to resume
293
cur = cur / (20 * GST_MSECOND) * (20 * GST_MSECOND);
295
byte_cur = amrwbparse->block * (cur / 20 / GST_MSECOND) + AMRWB_HEADER_SIZE;
298
amrwbparse->block * (stop / 20 / GST_MSECOND) + AMRWB_HEADER_SIZE;
299
amrwbparse->offset = byte_cur;
300
amrwbparse->ts = cur;
302
GST_DEBUG_OBJECT (amrwbparse, "Seeking to byte range %" G_GINT64_FORMAT
303
" to %" G_GINT64_FORMAT, byte_cur, cur);
305
/* and prepare to continue streaming */
306
/* send flush stop, peer will accept data and events again. We
307
* are not yet providing data as we still have the STREAM_LOCK. */
308
gst_pad_push_event (amrwbparse->sinkpad, gst_event_new_flush_stop ());
309
gst_pad_push_event (amrwbparse->srcpad, gst_event_new_new_segment (FALSE,
310
rate, format, cur, -1, cur));
312
/* and restart the task in case it got paused explicitely or by
313
* the FLUSH_START event we pushed out. */
314
gst_pad_start_task (amrwbparse->sinkpad,
315
(GstTaskFunction) gst_amrwbparse_loop, amrwbparse->sinkpad);
317
/* and release the lock again so we can continue streaming */
318
GST_PAD_STREAM_UNLOCK (amrwbparse->sinkpad);
324
gst_amrwbparse_handle_push_seek (GstAmrwbParse * amrwbparse, GstPad * pad,
330
GstSeekType cur_type, stop_type;
332
gint64 byte_cur = -1, byte_stop = -1;
334
gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
337
GST_DEBUG_OBJECT (amrwbparse, "Performing seek to %" GST_TIME_FORMAT,
338
GST_TIME_ARGS (cur));
340
/* For any format other than TIME, see if upstream handles
341
* it directly or fail. For TIME, try upstream, but do it ourselves if
342
* it fails upstream */
343
if (format != GST_FORMAT_TIME) {
344
return gst_pad_push_event (amrwbparse->sinkpad, event);
346
if (gst_pad_push_event (amrwbparse->sinkpad, event))
350
/* Convert the TIME to the appropriate BYTE position at which to resume
352
cur = cur / (20 * GST_MSECOND) * (20 * GST_MSECOND);
354
byte_cur = amrwbparse->block * (cur / 20 / GST_MSECOND) + AMRWB_HEADER_SIZE;
357
amrwbparse->block * (stop / 20 / GST_MSECOND) + AMRWB_HEADER_SIZE;
358
amrwbparse->ts = cur;
360
GST_DEBUG_OBJECT (amrwbparse, "Seeking to byte range %" G_GINT64_FORMAT
361
" to %" G_GINT64_FORMAT, byte_cur, byte_stop);
363
/* Send BYTE based seek upstream */
364
event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type,
365
byte_cur, stop_type, byte_stop);
367
return gst_pad_push_event (amrwbparse->sinkpad, event);
371
gst_amrwbparse_src_event (GstPad * pad, GstEvent * event)
373
GstAmrwbParse *amrwbparse = GST_AMRWBPARSE (gst_pad_get_parent (pad));
376
GST_DEBUG_OBJECT (amrwbparse, "handling event %d", GST_EVENT_TYPE (event));
378
switch (GST_EVENT_TYPE (event)) {
380
if (amrwbparse->seek_handler)
381
res = amrwbparse->seek_handler (amrwbparse, pad, event);
386
res = gst_pad_push_event (amrwbparse->sinkpad, event);
389
gst_object_unref (amrwbparse);
399
gst_amrwbparse_sink_event (GstPad * pad, GstEvent * event)
401
GstAmrwbParse *amrwbparse;
404
amrwbparse = GST_AMRWBPARSE (gst_pad_get_parent (pad));
406
GST_LOG ("handling event %d", GST_EVENT_TYPE (event));
408
switch (GST_EVENT_TYPE (event)) {
409
case GST_EVENT_FLUSH_START:
410
res = gst_pad_push_event (amrwbparse->srcpad, event);
412
case GST_EVENT_FLUSH_STOP:
413
gst_adapter_clear (amrwbparse->adapter);
414
gst_segment_init (&amrwbparse->segment, GST_FORMAT_TIME);
415
res = gst_pad_push_event (amrwbparse->srcpad, event);
418
res = gst_pad_push_event (amrwbparse->srcpad, event);
420
case GST_EVENT_NEWSEGMENT:
422
/* eat for now, we send a newsegment at start with infinite
424
gst_event_unref (event);
429
res = gst_pad_push_event (amrwbparse->srcpad, event);
432
gst_object_unref (amrwbparse);
439
gst_amrwbparse_chain (GstPad * pad, GstBuffer * buffer)
441
GstAmrwbParse *amrwbparse;
442
GstFlowReturn res = GST_FLOW_OK;
446
GstClockTime timestamp;
448
amrwbparse = GST_AMRWBPARSE (gst_pad_get_parent (pad));
450
timestamp = GST_BUFFER_TIMESTAMP (buffer);
451
if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
452
GST_DEBUG_OBJECT (amrwbparse, "Lock on timestamp %" GST_TIME_FORMAT,
453
GST_TIME_ARGS (timestamp));
454
amrwbparse->ts = timestamp;
457
gst_adapter_push (amrwbparse->adapter, buffer);
460
if (amrwbparse->need_header) {
464
if (gst_adapter_available (amrwbparse->adapter) < AMRWB_HEADER_SIZE)
467
data = gst_adapter_peek (amrwbparse->adapter, AMRWB_HEADER_SIZE);
468
if (memcmp (data, AMRWB_HEADER_STR, AMRWB_HEADER_SIZE) != 0)
471
gst_adapter_flush (amrwbparse->adapter, AMRWB_HEADER_SIZE);
473
amrwbparse->need_header = FALSE;
475
caps = gst_caps_new_simple ("audio/AMR-WB",
476
"rate", G_TYPE_INT, 16000, "channels", G_TYPE_INT, 1, NULL);
477
gst_pad_set_caps (amrwbparse->srcpad, caps);
478
gst_caps_unref (caps);
480
GST_DEBUG_OBJECT (amrwbparse, "Sending first segment");
481
segev = gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
482
GST_FORMAT_TIME, 0, -1, 0);
484
gst_pad_push_event (amrwbparse->srcpad, segev);
488
if (gst_adapter_available (amrwbparse->adapter) < 1)
491
data = gst_adapter_peek (amrwbparse->adapter, 1);
494
mode = (data[0] >> 3) & 0x0F;
495
amrwbparse->block = block_size[mode] + 1; /* add one for the mode */
497
if (gst_adapter_available (amrwbparse->adapter) < amrwbparse->block)
500
out = gst_buffer_new_and_alloc (amrwbparse->block);
502
data = gst_adapter_peek (amrwbparse->adapter, amrwbparse->block);
503
memcpy (GST_BUFFER_DATA (out), data, amrwbparse->block);
505
/* timestamp, all constants that won't overflow */
506
GST_BUFFER_DURATION (out) = GST_SECOND * L_FRAME16k / 16000;
507
GST_BUFFER_TIMESTAMP (out) = amrwbparse->ts;
508
if (GST_CLOCK_TIME_IS_VALID (amrwbparse->ts))
509
amrwbparse->ts += GST_BUFFER_DURATION (out);
511
gst_buffer_set_caps (out, GST_PAD_CAPS (amrwbparse->srcpad));
513
GST_DEBUG_OBJECT (amrwbparse, "Pushing %d bytes of data",
516
res = gst_pad_push (amrwbparse->srcpad, out);
518
gst_adapter_flush (amrwbparse->adapter, amrwbparse->block);
522
gst_object_unref (amrwbparse);
527
gst_amrwbparse_pull_header (GstAmrwbParse * amrwbparse)
534
ret = gst_pad_pull_range (amrwbparse->sinkpad, G_GUINT64_CONSTANT (0),
535
AMRWB_HEADER_SIZE, &buffer);
536
if (ret != GST_FLOW_OK)
539
data = GST_BUFFER_DATA (buffer);
540
size = GST_BUFFER_SIZE (buffer);
542
if (size < AMRWB_HEADER_SIZE)
545
if (memcmp (data, AMRWB_HEADER_STR, AMRWB_HEADER_SIZE))
548
gst_buffer_unref (buffer);
550
amrwbparse->offset = AMRWB_HEADER_SIZE;
555
gst_buffer_unref (buffer);
560
gst_buffer_unref (buffer);
565
/* random access mode, could just read a fixed size buffer and push it to
566
* the chain function but we don't... */
568
gst_amrwbparse_loop (GstPad * pad)
570
GstAmrwbParse *amrwbparse;
575
GstFlowReturn ret = GST_FLOW_OK;
577
amrwbparse = GST_AMRWBPARSE (GST_PAD_PARENT (pad));
580
if (G_UNLIKELY (amrwbparse->need_header)) {
583
if (!gst_amrwbparse_pull_header (amrwbparse)) {
584
GST_ELEMENT_ERROR (amrwbparse, STREAM, WRONG_TYPE, (NULL), (NULL));
585
GST_LOG_OBJECT (amrwbparse, "could not read header");
589
caps = gst_caps_new_simple ("audio/AMR-WB",
590
"rate", G_TYPE_INT, 16000, "channels", G_TYPE_INT, 1, NULL);
591
gst_pad_set_caps (amrwbparse->srcpad, caps);
592
gst_caps_unref (caps);
594
GST_DEBUG_OBJECT (amrwbparse, "Sending newsegment event");
595
gst_pad_push_event (amrwbparse->srcpad,
596
gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
597
GST_FORMAT_TIME, 0, -1, 0));
599
amrwbparse->need_header = FALSE;
603
gst_pad_pull_range (amrwbparse->sinkpad, amrwbparse->offset, 1, &buffer);
605
if (ret == GST_FLOW_UNEXPECTED)
607
else if (ret != GST_FLOW_OK)
610
data = GST_BUFFER_DATA (buffer);
611
size = GST_BUFFER_SIZE (buffer);
615
gst_buffer_unref (buffer);
620
mode = (data[0] >> 3) & 0x0F;
621
block = block_size[mode]; /* add one for the mode */
623
gst_buffer_unref (buffer);
625
ret = gst_pad_pull_range (amrwbparse->sinkpad,
626
amrwbparse->offset, block, &buffer);
628
if (ret == GST_FLOW_UNEXPECTED)
630
else if (ret != GST_FLOW_OK)
633
amrwbparse->offset += block;
636
GST_BUFFER_DURATION (buffer) = GST_SECOND * L_FRAME16k / 16000;
637
GST_BUFFER_TIMESTAMP (buffer) = amrwbparse->ts;
639
gst_buffer_set_caps (buffer,
640
(GstCaps *) gst_pad_get_pad_template_caps (amrwbparse->srcpad));
642
ret = gst_pad_push (amrwbparse->srcpad, buffer);
644
if (ret != GST_FLOW_OK) {
645
GST_DEBUG_OBJECT (amrwbparse, "Flow: %s", gst_flow_get_name (ret));
646
if (GST_FLOW_IS_FATAL (ret)) {
647
GST_ELEMENT_ERROR (amrwbparse, STREAM, FAILED, (NULL), /* _("Internal data flow error.")), */
648
("streaming task paused, reason: %s", gst_flow_get_name (ret)));
649
gst_pad_push_event (pad, gst_event_new_eos ());
654
amrwbparse->ts += GST_BUFFER_DURATION (buffer);
660
GST_LOG_OBJECT (amrwbparse, "pausing task");
661
gst_pad_pause_task (pad);
666
GST_LOG_OBJECT (amrwbparse, "pausing task %d", ret);
667
gst_pad_push_event (amrwbparse->srcpad, gst_event_new_eos ());
668
gst_pad_pause_task (pad);
674
gst_amrwbparse_sink_activate (GstPad * sinkpad)
676
gboolean result = FALSE;
677
GstAmrwbParse *amrwbparse;
679
amrwbparse = GST_AMRWBPARSE (gst_pad_get_parent (sinkpad));
681
if (gst_pad_check_pull_range (sinkpad)) {
682
GST_DEBUG ("Trying to activate in pull mode");
683
amrwbparse->seekable = TRUE;
685
result = gst_pad_activate_pull (sinkpad, TRUE);
687
GST_DEBUG ("Try to activate in push mode");
688
amrwbparse->seekable = FALSE;
689
result = gst_pad_activate_push (sinkpad, TRUE);
692
gst_object_unref (amrwbparse);
699
gst_amrwbparse_sink_activate_push (GstPad * sinkpad, gboolean active)
701
GstAmrwbParse *amrwbparse = GST_AMRWBPARSE (gst_pad_get_parent (sinkpad));
704
amrwbparse->seek_handler = gst_amrwbparse_handle_push_seek;
706
amrwbparse->seek_handler = NULL;
709
gst_object_unref (amrwbparse);
714
gst_amrwbparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
717
GstAmrwbParse *amrwbparse;
719
amrwbparse = GST_AMRWBPARSE (gst_pad_get_parent (sinkpad));
721
amrwbparse->seek_handler = gst_amrwbparse_handle_pull_seek;
722
result = gst_pad_start_task (sinkpad,
723
(GstTaskFunction) gst_amrwbparse_loop, sinkpad);
725
amrwbparse->seek_handler = NULL;
726
result = gst_pad_stop_task (sinkpad);
729
gst_object_unref (amrwbparse);
734
static GstStateChangeReturn
735
gst_amrwbparse_state_change (GstElement * element, GstStateChange transition)
737
GstAmrwbParse *amrwbparse;
738
GstStateChangeReturn ret;
740
amrwbparse = GST_AMRWBPARSE (element);
742
switch (transition) {
743
case GST_STATE_CHANGE_NULL_TO_READY:
745
case GST_STATE_CHANGE_READY_TO_PAUSED:
746
amrwbparse->need_header = TRUE;
748
amrwbparse->block = 0;
749
gst_segment_init (&amrwbparse->segment, GST_FORMAT_TIME);
755
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
757
switch (transition) {
758
case GST_STATE_CHANGE_PAUSED_TO_READY:
760
case GST_STATE_CHANGE_READY_TO_NULL: