| 1 |
/* GStreamer Adaptive Multi-Rate parser plugin |
| 2 |
* Copyright (C) 2006 Edgard Lima <[email protected]> |
| 3 |
* Copyright (C) 2008 Nokia Corporation. All rights reserved. |
| 4 |
* |
| 5 |
* Contact: Stefan Kost <[email protected]> |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library; if not, write to the |
| 19 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 20 |
* Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION:element-amrparse |
| 25 |
* @title: amrparse |
| 26 |
* @short_description: AMR parser |
| 27 |
* @see_also: #GstAmrnbDec, #GstAmrnbEnc |
| 28 |
* |
| 29 |
* This is an AMR parser capable of handling both narrow-band and wideband |
| 30 |
* formats. |
| 31 |
* |
| 32 |
* ## Example launch line |
| 33 |
* |[ |
| 34 |
* gst-launch-1.0 filesrc location=abc.amr ! amrparse ! amrdec ! audioresample ! audioconvert ! alsasink |
| 35 |
* ]| |
| 36 |
* |
| 37 |
*/ |
| 38 |
|
| 39 |
#ifdef HAVE_CONFIG_H |
| 40 |
#include "config.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
#include "gstaudioparserselements.h" |
| 46 |
#include "gstamrparse.h" |
| 47 |
#include <gst/pbutils/pbutils.h> |
| 48 |
|
| 49 |
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", |
| 50 |
GST_PAD_SRC, |
| 51 |
GST_PAD_ALWAYS, |
| 52 |
GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1;" |
| 53 |
"audio/AMR-WB, " "rate = (int) 16000, " "channels = (int) 1;") |
| 54 |
); |
| 55 |
|
| 56 |
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", |
| 57 |
GST_PAD_SINK, |
| 58 |
GST_PAD_ALWAYS, |
| 59 |
GST_STATIC_CAPS ("audio/x-amr-nb-sh; audio/x-amr-wb-sh")); |
| 60 |
|
| 61 |
GST_DEBUG_CATEGORY_STATIC (amrparse_debug); |
| 62 |
#define GST_CAT_DEFAULT amrparse_debug |
| 63 |
|
| 64 |
static const gint block_size_nb[16] = |
| 65 |
{ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; |
| 66 |
|
| 67 |
static const gint block_size_wb[16] = |
| 68 |
{ 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 }; |
| 69 |
|
| 70 |
/* AMR has a "hardcoded" framerate of 50fps */ |
| 71 |
#define AMR_FRAMES_PER_SECOND 50 |
| 72 |
#define AMR_FRAME_DURATION (GST_SECOND/AMR_FRAMES_PER_SECOND) |
| 73 |
#define AMR_MIME_HEADER_SIZE 9 |
| 74 |
|
| 75 |
static gboolean gst_amr_parse_start (GstBaseParse * parse); |
| 76 |
static gboolean gst_amr_parse_stop (GstBaseParse * parse); |
| 77 |
|
| 78 |
static gboolean gst_amr_parse_sink_setcaps (GstBaseParse * parse, |
| 79 |
GstCaps * caps); |
| 80 |
static GstCaps *gst_amr_parse_sink_getcaps (GstBaseParse * parse, |
| 81 |
GstCaps * filter); |
| 82 |
|
| 83 |
static GstFlowReturn gst_amr_parse_handle_frame (GstBaseParse * parse, |
| 84 |
GstBaseParseFrame * frame, gint * skipsize); |
| 85 |
static GstFlowReturn gst_amr_parse_pre_push_frame (GstBaseParse * parse, |
| 86 |
GstBaseParseFrame * frame); |
| 87 |
|
| 88 |
G_DEFINE_TYPE (GstAmrParse, gst_amr_parse, GST_TYPE_BASE_PARSE); |
| 89 |
GST_ELEMENT_REGISTER_DEFINE (amrparse, "amrparse", |
| 90 |
GST_RANK_PRIMARY + 1, GST_TYPE_AMR_PARSE); |
| 91 |
|
| 92 |
/** |
| 93 |
* gst_amr_parse_class_init: |
| 94 |
* @klass: GstAmrParseClass. |
| 95 |
* |
| 96 |
*/ |
| 97 |
static void |
| 98 |
gst_amr_parse_class_init (GstAmrParseClass * klass) |
| 99 |
{ |
| 100 |
GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
| 101 |
GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass); |
| 102 |
|
| 103 |
GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0, |
| 104 |
"AMR-NB audio stream parser"); |
| 105 |
|
| 106 |
gst_element_class_add_static_pad_template (element_class, &sink_template); |
| 107 |
gst_element_class_add_static_pad_template (element_class, &src_template); |
| 108 |
|
| 109 |
gst_element_class_set_static_metadata (element_class, |
| 110 |
"AMR audio stream parser", "Codec/Parser/Audio", |
| 111 |
"Adaptive Multi-Rate audio parser", |
| 112 |
"Ronald Bultje <[email protected]>"); |
| 113 |
|
| 114 |
parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start); |
| 115 |
parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop); |
| 116 |
parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps); |
| 117 |
parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_getcaps); |
| 118 |
parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amr_parse_handle_frame); |
| 119 |
parse_class->pre_push_frame = |
| 120 |
GST_DEBUG_FUNCPTR (gst_amr_parse_pre_push_frame); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* gst_amr_parse_init: |
| 126 |
* @amrparse: #GstAmrParse |
| 127 |
* @klass: #GstAmrParseClass. |
| 128 |
* |
| 129 |
*/ |
| 130 |
static void |
| 131 |
gst_amr_parse_init (GstAmrParse * amrparse) |
| 132 |
{ |
| 133 |
/* init rest */ |
| 134 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62); |
| 135 |
GST_DEBUG ("initialized"); |
| 136 |
GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (amrparse)); |
| 137 |
GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (amrparse)); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* gst_amr_parse_set_src_caps: |
| 143 |
* @amrparse: #GstAmrParse. |
| 144 |
* |
| 145 |
* Set source pad caps according to current knowledge about the |
| 146 |
* audio stream. |
| 147 |
* |
| 148 |
* Returns: TRUE if caps were successfully set. |
| 149 |
*/ |
| 150 |
static gboolean |
| 151 |
gst_amr_parse_set_src_caps (GstAmrParse * amrparse) |
| 152 |
{ |
| 153 |
GstCaps *src_caps = NULL; |
| 154 |
gboolean res = FALSE; |
| 155 |
|
| 156 |
if (amrparse->wide) { |
| 157 |
GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB"); |
| 158 |
src_caps = gst_caps_new_simple ("audio/AMR-WB", |
| 159 |
"channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL); |
| 160 |
} else { |
| 161 |
GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB"); |
| 162 |
/* Max. size of NB frame is 31 bytes, so we can set the min. frame |
| 163 |
size to 32 (+1 for next frame header) */ |
| 164 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32); |
| 165 |
src_caps = gst_caps_new_simple ("audio/AMR", |
| 166 |
"channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL); |
| 167 |
} |
| 168 |
gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad); |
| 169 |
res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps); |
| 170 |
gst_caps_unref (src_caps); |
| 171 |
return res; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* gst_amr_parse_sink_setcaps: |
| 177 |
* @sinkpad: GstPad |
| 178 |
* @caps: GstCaps |
| 179 |
* |
| 180 |
* Returns: TRUE on success. |
| 181 |
*/ |
| 182 |
static gboolean |
| 183 |
gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps) |
| 184 |
{ |
| 185 |
GstAmrParse *amrparse; |
| 186 |
GstStructure *structure; |
| 187 |
const gchar *name; |
| 188 |
|
| 189 |
amrparse = GST_AMR_PARSE (parse); |
| 190 |
structure = gst_caps_get_structure (caps, 0); |
| 191 |
name = gst_structure_get_name (structure); |
| 192 |
|
| 193 |
GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name); |
| 194 |
|
| 195 |
if (!strncmp (name, "audio/x-amr-wb-sh", 17)) { |
| 196 |
amrparse->block_size = block_size_wb; |
| 197 |
amrparse->wide = 1; |
| 198 |
} else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) { |
| 199 |
amrparse->block_size = block_size_nb; |
| 200 |
amrparse->wide = 0; |
| 201 |
} else { |
| 202 |
GST_WARNING ("Unknown caps"); |
| 203 |
return FALSE; |
| 204 |
} |
| 205 |
|
| 206 |
amrparse->need_header = FALSE; |
| 207 |
gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2); |
| 208 |
gst_amr_parse_set_src_caps (amrparse); |
| 209 |
return TRUE; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* gst_amr_parse_parse_header: |
| 214 |
* @amrparse: #GstAmrParse |
| 215 |
* @data: Header data to be parsed. |
| 216 |
* @skipsize: Output argument where the frame size will be stored. |
| 217 |
* |
| 218 |
* Check if the given data contains an AMR mime header. |
| 219 |
* |
| 220 |
* Returns: TRUE on success. |
| 221 |
*/ |
| 222 |
static gboolean |
| 223 |
gst_amr_parse_parse_header (GstAmrParse * amrparse, |
| 224 |
const guint8 * data, gint * skipsize) |
| 225 |
{ |
| 226 |
GST_DEBUG_OBJECT (amrparse, "Parsing header data"); |
| 227 |
|
| 228 |
if (!memcmp (data, "#!AMR-WB\n", 9)) { |
| 229 |
GST_DEBUG_OBJECT (amrparse, "AMR-WB detected"); |
| 230 |
amrparse->block_size = block_size_wb; |
| 231 |
amrparse->wide = TRUE; |
| 232 |
*skipsize = amrparse->header = 9; |
| 233 |
} else if (!memcmp (data, "#!AMR\n", 6)) { |
| 234 |
GST_DEBUG_OBJECT (amrparse, "AMR-NB detected"); |
| 235 |
amrparse->block_size = block_size_nb; |
| 236 |
amrparse->wide = FALSE; |
| 237 |
*skipsize = amrparse->header = 6; |
| 238 |
} else |
| 239 |
return FALSE; |
| 240 |
|
| 241 |
gst_amr_parse_set_src_caps (amrparse); |
| 242 |
return TRUE; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* gst_amr_parse_check_valid_frame: |
| 248 |
* @parse: #GstBaseParse. |
| 249 |
* @buffer: #GstBuffer. |
| 250 |
* @framesize: Output variable where the found frame size is put. |
| 251 |
* @skipsize: Output variable which tells how much data needs to be skipped |
| 252 |
* until a frame header is found. |
| 253 |
* |
| 254 |
* Implementation of "check_valid_frame" vmethod in #GstBaseParse class. |
| 255 |
* |
| 256 |
* Returns: TRUE if the given data contains valid frame. |
| 257 |
*/ |
| 258 |
static GstFlowReturn |
| 259 |
gst_amr_parse_handle_frame (GstBaseParse * parse, |
| 260 |
GstBaseParseFrame * frame, gint * skipsize) |
| 261 |
{ |
| 262 |
GstBuffer *buffer; |
| 263 |
GstMapInfo map; |
| 264 |
gint fsize = 0, mode, dsize; |
| 265 |
GstAmrParse *amrparse; |
| 266 |
gboolean found = FALSE; |
| 267 |
|
| 268 |
amrparse = GST_AMR_PARSE (parse); |
| 269 |
buffer = frame->buffer; |
| 270 |
|
| 271 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 272 |
dsize = map.size; |
| 273 |
|
| 274 |
GST_LOG ("buffer: %d bytes", dsize); |
| 275 |
|
| 276 |
if (amrparse->need_header) { |
| 277 |
if (dsize >= AMR_MIME_HEADER_SIZE && |
| 278 |
gst_amr_parse_parse_header (amrparse, map.data, skipsize)) { |
| 279 |
amrparse->need_header = FALSE; |
| 280 |
gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2); |
| 281 |
} else { |
| 282 |
GST_WARNING ("media doesn't look like a AMR format"); |
| 283 |
} |
| 284 |
/* We return FALSE, so this frame won't get pushed forward. Instead, |
| 285 |
the "skip" value is set, so next time we will receive a valid frame. */ |
| 286 |
goto done; |
| 287 |
} |
| 288 |
|
| 289 |
*skipsize = 1; |
| 290 |
/* Does this look like a possible frame header candidate? */ |
| 291 |
if ((map.data[0] & 0x83) == 0) { |
| 292 |
/* Yep. Retrieve the frame size */ |
| 293 |
mode = (map.data[0] >> 3) & 0x0F; |
| 294 |
fsize = amrparse->block_size[mode] + 1; /* +1 for the header byte */ |
| 295 |
|
| 296 |
/* We recognize this data as a valid frame when: |
| 297 |
* - We are in sync. There is no need for extra checks then |
| 298 |
* - We are in EOS. There might not be enough data to check next frame |
| 299 |
* - Sync is lost, but the following data after this frame seem |
| 300 |
* to contain a valid header as well (and there is enough data to |
| 301 |
* perform this check) |
| 302 |
*/ |
| 303 |
if (fsize) { |
| 304 |
*skipsize = 0; |
| 305 |
/* in sync, no further check */ |
| 306 |
if (!GST_BASE_PARSE_LOST_SYNC (parse)) { |
| 307 |
found = TRUE; |
| 308 |
} else if (dsize > fsize) { |
| 309 |
/* enough data, check for next sync */ |
| 310 |
if ((map.data[fsize] & 0x83) == 0) |
| 311 |
found = TRUE; |
| 312 |
} else if (GST_BASE_PARSE_DRAINING (parse)) { |
| 313 |
/* not enough, but draining, so ok */ |
| 314 |
found = TRUE; |
| 315 |
fsize = map.size; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
found = found && fsize <= map.size; |
| 321 |
|
| 322 |
done: |
| 323 |
gst_buffer_unmap (buffer, &map); |
| 324 |
|
| 325 |
if (found) { |
| 326 |
return gst_base_parse_finish_frame (parse, frame, fsize); |
| 327 |
} |
| 328 |
|
| 329 |
return GST_FLOW_OK; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* gst_amr_parse_start: |
| 334 |
* @parse: #GstBaseParse. |
| 335 |
* |
| 336 |
* Implementation of "start" vmethod in #GstBaseParse class. |
| 337 |
* |
| 338 |
* Returns: TRUE on success. |
| 339 |
*/ |
| 340 |
static gboolean |
| 341 |
gst_amr_parse_start (GstBaseParse * parse) |
| 342 |
{ |
| 343 |
GstAmrParse *amrparse; |
| 344 |
|
| 345 |
amrparse = GST_AMR_PARSE (parse); |
| 346 |
GST_DEBUG ("start"); |
| 347 |
amrparse->need_header = TRUE; |
| 348 |
amrparse->header = 0; |
| 349 |
amrparse->sent_codec_tag = FALSE; |
| 350 |
return TRUE; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
/** |
| 355 |
* gst_amr_parse_stop: |
| 356 |
* @parse: #GstBaseParse. |
| 357 |
* |
| 358 |
* Implementation of "stop" vmethod in #GstBaseParse class. |
| 359 |
* |
| 360 |
* Returns: TRUE on success. |
| 361 |
*/ |
| 362 |
static gboolean |
| 363 |
gst_amr_parse_stop (GstBaseParse * parse) |
| 364 |
{ |
| 365 |
GstAmrParse *amrparse; |
| 366 |
|
| 367 |
amrparse = GST_AMR_PARSE (parse); |
| 368 |
GST_DEBUG ("stop"); |
| 369 |
amrparse->need_header = TRUE; |
| 370 |
amrparse->header = 0; |
| 371 |
return TRUE; |
| 372 |
} |
| 373 |
|
| 374 |
static GstCaps * |
| 375 |
gst_amr_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter) |
| 376 |
{ |
| 377 |
GstCaps *peercaps, *templ; |
| 378 |
GstCaps *res; |
| 379 |
|
| 380 |
|
| 381 |
templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse)); |
| 382 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), filter); |
| 383 |
|
| 384 |
if (peercaps) { |
| 385 |
guint i, n; |
| 386 |
|
| 387 |
/* Rename structure names */ |
| 388 |
peercaps = gst_caps_make_writable (peercaps); |
| 389 |
n = gst_caps_get_size (peercaps); |
| 390 |
for (i = 0; i < n; i++) { |
| 391 |
GstStructure *s = gst_caps_get_structure (peercaps, i); |
| 392 |
|
| 393 |
if (gst_structure_has_name (s, "audio/AMR")) |
| 394 |
gst_structure_set_name (s, "audio/x-amr-nb-sh"); |
| 395 |
else |
| 396 |
gst_structure_set_name (s, "audio/x-amr-wb-sh"); |
| 397 |
} |
| 398 |
|
| 399 |
res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST); |
| 400 |
gst_caps_unref (peercaps); |
| 401 |
res = gst_caps_make_writable (res); |
| 402 |
/* Append the template caps because we still want to accept |
| 403 |
* caps without any fields in the case upstream does not |
| 404 |
* know anything. |
| 405 |
*/ |
| 406 |
gst_caps_append (res, templ); |
| 407 |
} else { |
| 408 |
res = templ; |
| 409 |
} |
| 410 |
|
| 411 |
if (filter) { |
| 412 |
GstCaps *intersection; |
| 413 |
|
| 414 |
intersection = |
| 415 |
gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST); |
| 416 |
gst_caps_unref (res); |
| 417 |
res = intersection; |
| 418 |
} |
| 419 |
|
| 420 |
return res; |
| 421 |
} |
| 422 |
|
| 423 |
static GstFlowReturn |
| 424 |
gst_amr_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 425 |
{ |
| 426 |
GstAmrParse *amrparse = GST_AMR_PARSE (parse); |
| 427 |
|
| 428 |
if (!amrparse->sent_codec_tag) { |
| 429 |
GstTagList *taglist; |
| 430 |
GstCaps *caps; |
| 431 |
|
| 432 |
/* codec tag */ |
| 433 |
caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse)); |
| 434 |
if (G_UNLIKELY (caps == NULL)) { |
| 435 |
if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) { |
| 436 |
GST_INFO_OBJECT (parse, "Src pad is flushing"); |
| 437 |
return GST_FLOW_FLUSHING; |
| 438 |
} else { |
| 439 |
GST_INFO_OBJECT (parse, "Src pad is not negotiated!"); |
| 440 |
return GST_FLOW_NOT_NEGOTIATED; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
taglist = gst_tag_list_new_empty (); |
| 445 |
gst_pb_utils_add_codec_description_to_tag_list (taglist, |
| 446 |
GST_TAG_AUDIO_CODEC, caps); |
| 447 |
gst_caps_unref (caps); |
| 448 |
|
| 449 |
gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE); |
| 450 |
gst_tag_list_unref (taglist); |
| 451 |
|
| 452 |
/* also signals the end of first-frame processing */ |
| 453 |
amrparse->sent_codec_tag = TRUE; |
| 454 |
} |
| 455 |
|
| 456 |
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP; |
| 457 |
|
| 458 |
return GST_FLOW_OK; |
| 459 |
} |