| 1 |
/* GStreamer AAC parser plugin |
| 2 |
* Copyright (C) 2008 Nokia Corporation. All rights reserved. |
| 3 |
* |
| 4 |
* Contact: Stefan Kost <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Library General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Library General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Library General Public |
| 17 |
* License along with this library; if not, write to the |
| 18 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 19 |
* Boston, MA 02110-1301, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* SECTION:element-aacparse |
| 24 |
* @title: aacparse |
| 25 |
* @short_description: AAC parser |
| 26 |
* @see_also: #GstAmrParse |
| 27 |
* |
| 28 |
* This is an AAC parser which handles both ADIF and ADTS stream formats. |
| 29 |
* |
| 30 |
* As ADIF format is not framed, it is not seekable and stream duration cannot |
| 31 |
* be determined either. However, ADTS format AAC clips can be seeked, and parser |
| 32 |
* can also estimate playback position and clip duration. |
| 33 |
* |
| 34 |
* ## Example launch line |
| 35 |
* |[ |
| 36 |
* gst-launch-1.0 filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink |
| 37 |
* ]| |
| 38 |
* |
| 39 |
*/ |
| 40 |
|
| 41 |
#ifdef HAVE_CONFIG_H |
| 42 |
#include "config.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <string.h> |
| 46 |
|
| 47 |
#include <gst/base/gstbitreader.h> |
| 48 |
#include <gst/pbutils/pbutils.h> |
| 49 |
#include "gstaudioparserselements.h" |
| 50 |
#include "gstaacparse.h" |
| 51 |
|
| 52 |
|
| 53 |
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", |
| 54 |
GST_PAD_SRC, |
| 55 |
GST_PAD_ALWAYS, |
| 56 |
GST_STATIC_CAPS ("audio/mpeg, " |
| 57 |
"framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, " |
| 58 |
"stream-format = (string) { raw, adts, adif, loas };")); |
| 59 |
|
| 60 |
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", |
| 61 |
GST_PAD_SINK, |
| 62 |
GST_PAD_ALWAYS, |
| 63 |
GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) { 2, 4 };")); |
| 64 |
|
| 65 |
GST_DEBUG_CATEGORY_STATIC (aacparse_debug); |
| 66 |
#define GST_CAT_DEFAULT aacparse_debug |
| 67 |
|
| 68 |
|
| 69 |
#define ADIF_MAX_SIZE 40 /* Should be enough */ |
| 70 |
#define ADTS_MAX_SIZE 10 /* Should be enough */ |
| 71 |
#define LOAS_MAX_SIZE 3 /* Should be enough */ |
| 72 |
#define RAW_MAX_SIZE 1 /* Correct framing is required */ |
| 73 |
|
| 74 |
#define ADTS_HEADERS_LENGTH 7UL /* Total byte-length of fixed and variable |
| 75 |
headers prepended during raw to ADTS |
| 76 |
conversion */ |
| 77 |
|
| 78 |
#define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec) |
| 79 |
|
| 80 |
static const gint loas_sample_rate_table[16] = { |
| 81 |
96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, |
| 82 |
16000, 12000, 11025, 8000, 7350, 0, 0, 0 |
| 83 |
}; |
| 84 |
|
| 85 |
static const gint loas_channels_table[16] = { |
| 86 |
0, 1, 2, 3, 4, 5, 6, 8, |
| 87 |
0, 0, 0, 7, 8, 0, 8, 0 |
| 88 |
}; |
| 89 |
|
| 90 |
static gboolean gst_aac_parse_start (GstBaseParse * parse); |
| 91 |
static gboolean gst_aac_parse_stop (GstBaseParse * parse); |
| 92 |
|
| 93 |
static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse, |
| 94 |
GstCaps * caps); |
| 95 |
static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse, |
| 96 |
GstCaps * filter); |
| 97 |
|
| 98 |
static GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse, |
| 99 |
GstBaseParseFrame * frame, gint * skipsize); |
| 100 |
static GstFlowReturn gst_aac_parse_pre_push_frame (GstBaseParse * parse, |
| 101 |
GstBaseParseFrame * frame); |
| 102 |
static gboolean gst_aac_parse_src_event (GstBaseParse * parse, |
| 103 |
GstEvent * event); |
| 104 |
|
| 105 |
static gboolean gst_aac_parse_read_audio_specific_config (GstAacParse * |
| 106 |
aacparse, GstBitReader * br, gint * object_type, gint * sample_rate, |
| 107 |
gint * channels, gint * frame_samples); |
| 108 |
|
| 109 |
|
| 110 |
#define gst_aac_parse_parent_class parent_class |
| 111 |
G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE); |
| 112 |
GST_ELEMENT_REGISTER_DEFINE (aacparse, "aacparse", |
| 113 |
GST_RANK_PRIMARY + 1, GST_TYPE_AAC_PARSE); |
| 114 |
|
| 115 |
/** |
| 116 |
* gst_aac_parse_class_init: |
| 117 |
* @klass: #GstAacParseClass. |
| 118 |
* |
| 119 |
*/ |
| 120 |
static void |
| 121 |
gst_aac_parse_class_init (GstAacParseClass * klass) |
| 122 |
{ |
| 123 |
GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
| 124 |
GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass); |
| 125 |
|
| 126 |
GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0, |
| 127 |
"AAC audio stream parser"); |
| 128 |
|
| 129 |
gst_element_class_add_static_pad_template (element_class, &sink_template); |
| 130 |
gst_element_class_add_static_pad_template (element_class, &src_template); |
| 131 |
|
| 132 |
gst_element_class_set_static_metadata (element_class, |
| 133 |
"AAC audio stream parser", "Codec/Parser/Audio", |
| 134 |
"Advanced Audio Coding parser", "Stefan Kost <[email protected]>"); |
| 135 |
|
| 136 |
parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start); |
| 137 |
parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop); |
| 138 |
parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps); |
| 139 |
parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps); |
| 140 |
parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame); |
| 141 |
parse_class->pre_push_frame = |
| 142 |
GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame); |
| 143 |
parse_class->src_event = GST_DEBUG_FUNCPTR (gst_aac_parse_src_event); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* gst_aac_parse_init: |
| 149 |
* @aacparse: #GstAacParse. |
| 150 |
* @klass: #GstAacParseClass. |
| 151 |
* |
| 152 |
*/ |
| 153 |
static void |
| 154 |
gst_aac_parse_init (GstAacParse * aacparse) |
| 155 |
{ |
| 156 |
GST_DEBUG ("initialized"); |
| 157 |
GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (aacparse)); |
| 158 |
GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (aacparse)); |
| 159 |
|
| 160 |
aacparse->last_parsed_sample_rate = 0; |
| 161 |
aacparse->last_parsed_channels = 0; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* gst_aac_parse_set_src_caps: |
| 167 |
* @aacparse: #GstAacParse. |
| 168 |
* @sink_caps: (proposed) caps of sink pad |
| 169 |
* |
| 170 |
* Set source pad caps according to current knowledge about the |
| 171 |
* audio stream. |
| 172 |
* |
| 173 |
* Returns: TRUE if caps were successfully set. |
| 174 |
*/ |
| 175 |
static gboolean |
| 176 |
gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps) |
| 177 |
{ |
| 178 |
GstStructure *s; |
| 179 |
GstCaps *src_caps = NULL, *peercaps; |
| 180 |
gboolean res = FALSE; |
| 181 |
const gchar *stream_format; |
| 182 |
guint8 codec_data[2]; |
| 183 |
guint16 codec_data_data; |
| 184 |
gint sample_rate_idx; |
| 185 |
|
| 186 |
GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps); |
| 187 |
if (sink_caps) |
| 188 |
src_caps = gst_caps_copy (sink_caps); |
| 189 |
else |
| 190 |
src_caps = gst_caps_new_empty_simple ("audio/mpeg"); |
| 191 |
|
| 192 |
gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE, |
| 193 |
"mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL); |
| 194 |
|
| 195 |
aacparse->output_header_type = aacparse->header_type; |
| 196 |
switch (aacparse->header_type) { |
| 197 |
case DSPAAC_HEADER_NONE: |
| 198 |
stream_format = "raw"; |
| 199 |
break; |
| 200 |
case DSPAAC_HEADER_ADTS: |
| 201 |
stream_format = "adts"; |
| 202 |
break; |
| 203 |
case DSPAAC_HEADER_ADIF: |
| 204 |
stream_format = "adif"; |
| 205 |
break; |
| 206 |
case DSPAAC_HEADER_LOAS: |
| 207 |
stream_format = "loas"; |
| 208 |
break; |
| 209 |
default: |
| 210 |
stream_format = NULL; |
| 211 |
} |
| 212 |
|
| 213 |
/* Generate codec data to be able to set profile/level on the caps. |
| 214 |
* The codec_data data is according to AudioSpecificConfig, |
| 215 |
* ISO/IEC 14496-3, 1.6.2.1 */ |
| 216 |
sample_rate_idx = |
| 217 |
gst_codec_utils_aac_get_index_from_sample_rate (aacparse->sample_rate); |
| 218 |
if (sample_rate_idx < 0) |
| 219 |
goto not_a_known_rate; |
| 220 |
codec_data_data = |
| 221 |
(aacparse->object_type << 11) | |
| 222 |
(sample_rate_idx << 7) | (aacparse->channels << 3); |
| 223 |
GST_WRITE_UINT16_BE (codec_data, codec_data_data); |
| 224 |
gst_codec_utils_aac_caps_set_level_and_profile (src_caps, codec_data, 2); |
| 225 |
|
| 226 |
s = gst_caps_get_structure (src_caps, 0); |
| 227 |
if (aacparse->sample_rate > 0) |
| 228 |
gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL); |
| 229 |
if (aacparse->channels > 0) |
| 230 |
gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL); |
| 231 |
if (stream_format) |
| 232 |
gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL); |
| 233 |
|
| 234 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (aacparse), NULL); |
| 235 |
if (peercaps && !gst_caps_can_intersect (src_caps, peercaps)) { |
| 236 |
GstCaps *convcaps = gst_caps_copy (src_caps); |
| 237 |
GstStructure *cs = gst_caps_get_structure (convcaps, 0); |
| 238 |
|
| 239 |
GST_DEBUG_OBJECT (aacparse, "Caps do not intersect: parsed %" GST_PTR_FORMAT |
| 240 |
" and peer %" GST_PTR_FORMAT, src_caps, peercaps); |
| 241 |
|
| 242 |
if (aacparse->header_type == DSPAAC_HEADER_ADTS) { |
| 243 |
GstBuffer *codec_data_buffer = gst_buffer_new_and_alloc (2); |
| 244 |
|
| 245 |
gst_buffer_fill (codec_data_buffer, 0, codec_data, 2); |
| 246 |
gst_structure_set (cs, "stream-format", G_TYPE_STRING, "raw", |
| 247 |
"codec_data", GST_TYPE_BUFFER, codec_data_buffer, NULL); |
| 248 |
|
| 249 |
if (gst_caps_can_intersect (convcaps, peercaps)) { |
| 250 |
GST_DEBUG_OBJECT (aacparse, "Converting from ADTS to raw"); |
| 251 |
aacparse->output_header_type = DSPAAC_HEADER_NONE; |
| 252 |
gst_caps_replace (&src_caps, convcaps); |
| 253 |
} |
| 254 |
|
| 255 |
gst_buffer_unref (codec_data_buffer); |
| 256 |
} else if (aacparse->header_type == DSPAAC_HEADER_NONE) { |
| 257 |
gst_structure_set (cs, "stream-format", G_TYPE_STRING, "adts", NULL); |
| 258 |
gst_structure_remove_field (cs, "codec_data"); |
| 259 |
|
| 260 |
if (gst_caps_can_intersect (convcaps, peercaps)) { |
| 261 |
GST_DEBUG_OBJECT (aacparse, "Converting from raw to ADTS"); |
| 262 |
aacparse->output_header_type = DSPAAC_HEADER_ADTS; |
| 263 |
gst_caps_replace (&src_caps, convcaps); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
gst_caps_unref (convcaps); |
| 268 |
} |
| 269 |
if (peercaps) |
| 270 |
gst_caps_unref (peercaps); |
| 271 |
|
| 272 |
GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps); |
| 273 |
|
| 274 |
res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps); |
| 275 |
gst_caps_unref (src_caps); |
| 276 |
return res; |
| 277 |
|
| 278 |
not_a_known_rate: |
| 279 |
GST_ERROR_OBJECT (aacparse, "Not a known sample rate: %d", |
| 280 |
aacparse->sample_rate); |
| 281 |
gst_caps_unref (src_caps); |
| 282 |
return FALSE; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* gst_aac_parse_sink_setcaps: |
| 288 |
* @sinkpad: GstPad |
| 289 |
* @caps: GstCaps |
| 290 |
* |
| 291 |
* Implementation of "set_sink_caps" vmethod in #GstBaseParse class. |
| 292 |
* |
| 293 |
* Returns: TRUE on success. |
| 294 |
*/ |
| 295 |
static gboolean |
| 296 |
gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps) |
| 297 |
{ |
| 298 |
GstAacParse *aacparse; |
| 299 |
GstStructure *structure; |
| 300 |
gchar *caps_str; |
| 301 |
const GValue *value; |
| 302 |
|
| 303 |
aacparse = GST_AAC_PARSE (parse); |
| 304 |
structure = gst_caps_get_structure (caps, 0); |
| 305 |
caps_str = gst_caps_to_string (caps); |
| 306 |
|
| 307 |
GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str); |
| 308 |
g_free (caps_str); |
| 309 |
|
| 310 |
/* This is needed at least in case of RTP |
| 311 |
* Parses the codec_data information to get ObjectType, |
| 312 |
* number of channels and samplerate */ |
| 313 |
value = gst_structure_get_value (structure, "codec_data"); |
| 314 |
if (value) { |
| 315 |
GstBuffer *buf = gst_value_get_buffer (value); |
| 316 |
|
| 317 |
if (buf && gst_buffer_get_size (buf) >= 2) { |
| 318 |
GstMapInfo map; |
| 319 |
GstBitReader br; |
| 320 |
|
| 321 |
if (!gst_buffer_map (buf, &map, GST_MAP_READ)) |
| 322 |
return FALSE; |
| 323 |
gst_bit_reader_init (&br, map.data, map.size); |
| 324 |
gst_aac_parse_read_audio_specific_config (aacparse, &br, |
| 325 |
&aacparse->object_type, &aacparse->sample_rate, &aacparse->channels, |
| 326 |
&aacparse->frame_samples); |
| 327 |
|
| 328 |
aacparse->header_type = DSPAAC_HEADER_NONE; |
| 329 |
aacparse->mpegversion = 4; |
| 330 |
gst_buffer_unmap (buf, &map); |
| 331 |
|
| 332 |
GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, " |
| 333 |
"samples=%d", aacparse->object_type, aacparse->sample_rate, |
| 334 |
aacparse->channels, aacparse->frame_samples); |
| 335 |
|
| 336 |
/* arrange for metadata and get out of the way */ |
| 337 |
gst_aac_parse_set_src_caps (aacparse, caps); |
| 338 |
if (aacparse->header_type == aacparse->output_header_type) |
| 339 |
gst_base_parse_set_passthrough (parse, TRUE); |
| 340 |
|
| 341 |
/* input is already correctly framed */ |
| 342 |
gst_base_parse_set_min_frame_size (parse, RAW_MAX_SIZE); |
| 343 |
} else { |
| 344 |
return FALSE; |
| 345 |
} |
| 346 |
|
| 347 |
/* caps info overrides */ |
| 348 |
gst_structure_get_int (structure, "rate", &aacparse->sample_rate); |
| 349 |
gst_structure_get_int (structure, "channels", &aacparse->channels); |
| 350 |
} else { |
| 351 |
const gchar *stream_format = |
| 352 |
gst_structure_get_string (structure, "stream-format"); |
| 353 |
|
| 354 |
if (g_strcmp0 (stream_format, "raw") == 0) { |
| 355 |
GST_ERROR_OBJECT (parse, "Need codec_data for raw AAC"); |
| 356 |
return FALSE; |
| 357 |
} else { |
| 358 |
aacparse->sample_rate = 0; |
| 359 |
aacparse->channels = 0; |
| 360 |
aacparse->header_type = DSPAAC_HEADER_NOT_PARSED; |
| 361 |
gst_base_parse_set_passthrough (parse, FALSE); |
| 362 |
} |
| 363 |
} |
| 364 |
return TRUE; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
/** |
| 369 |
* gst_aac_parse_adts_get_frame_len: |
| 370 |
* @data: block of data containing an ADTS header. |
| 371 |
* |
| 372 |
* This function calculates ADTS frame length from the given header. |
| 373 |
* |
| 374 |
* Returns: size of the ADTS frame. |
| 375 |
*/ |
| 376 |
static inline guint |
| 377 |
gst_aac_parse_adts_get_frame_len (const guint8 * data) |
| 378 |
{ |
| 379 |
return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
/** |
| 384 |
* gst_aac_parse_check_adts_frame: |
| 385 |
* @aacparse: #GstAacParse. |
| 386 |
* @data: Data to be checked. |
| 387 |
* @avail: Amount of data passed. |
| 388 |
* @framesize: If valid ADTS frame was found, this will be set to tell the |
| 389 |
* found frame size in bytes. |
| 390 |
* @needed_data: If frame was not found, this may be set to tell how much |
| 391 |
* more data is needed in the next round to detect the frame |
| 392 |
* reliably. This may happen when a frame header candidate |
| 393 |
* is found but it cannot be guaranteed to be the header without |
| 394 |
* peeking the following data. |
| 395 |
* |
| 396 |
* Check if the given data contains contains ADTS frame. The algorithm |
| 397 |
* will examine ADTS frame header and calculate the frame size. Also, another |
| 398 |
* consecutive ADTS frame header need to be present after the found frame. |
| 399 |
* Otherwise the data is not considered as a valid ADTS frame. However, this |
| 400 |
* "extra check" is omitted when EOS has been received. In this case it is |
| 401 |
* enough when data[0] contains a valid ADTS header. |
| 402 |
* |
| 403 |
* This function may set the #needed_data to indicate that a possible frame |
| 404 |
* candidate has been found, but more data (#needed_data bytes) is needed to |
| 405 |
* be absolutely sure. When this situation occurs, FALSE will be returned. |
| 406 |
* |
| 407 |
* When a valid frame is detected, this function will use |
| 408 |
* gst_base_parse_set_min_frame_size() function from #GstBaseParse class |
| 409 |
* to set the needed bytes for next frame.This way next data chunk is already |
| 410 |
* of correct size. |
| 411 |
* |
| 412 |
* Returns: TRUE if the given data contains a valid ADTS header. |
| 413 |
*/ |
| 414 |
static gboolean |
| 415 |
gst_aac_parse_check_adts_frame (GstAacParse * aacparse, |
| 416 |
const guint8 * data, const guint avail, gboolean drain, |
| 417 |
guint * framesize, guint * needed_data) |
| 418 |
{ |
| 419 |
guint crc_size; |
| 420 |
|
| 421 |
*needed_data = 0; |
| 422 |
|
| 423 |
/* Absolute minimum to perform the ADTS syncword, |
| 424 |
layer and sampling frequency tests */ |
| 425 |
if (G_UNLIKELY (avail < 3)) { |
| 426 |
*needed_data = 3; |
| 427 |
return FALSE; |
| 428 |
} |
| 429 |
|
| 430 |
/* Syncword and layer tests */ |
| 431 |
if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) { |
| 432 |
|
| 433 |
/* Sampling frequency test */ |
| 434 |
if (G_UNLIKELY ((data[2] & 0x3C) >> 2 == 15)) |
| 435 |
return FALSE; |
| 436 |
|
| 437 |
/* This looks like an ADTS frame header but |
| 438 |
we need at least 6 bytes to proceed */ |
| 439 |
if (G_UNLIKELY (avail < 6)) { |
| 440 |
*needed_data = 6; |
| 441 |
return FALSE; |
| 442 |
} |
| 443 |
|
| 444 |
*framesize = gst_aac_parse_adts_get_frame_len (data); |
| 445 |
|
| 446 |
/* If frame has CRC, it needs 2 bytes |
| 447 |
for it at the end of the header */ |
| 448 |
crc_size = (data[1] & 0x01) ? 0 : 2; |
| 449 |
|
| 450 |
/* CRC size test */ |
| 451 |
if (*framesize < 7 + crc_size) { |
| 452 |
*needed_data = 7 + crc_size; |
| 453 |
return FALSE; |
| 454 |
} |
| 455 |
|
| 456 |
/* In EOS mode this is enough. No need to examine the data further. |
| 457 |
We also relax the check when we have sync, on the assumption that |
| 458 |
if we're not looking at random data, we have a much higher chance |
| 459 |
to get the correct sync, and this avoids losing two frames when |
| 460 |
a single bit corruption happens. */ |
| 461 |
if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) { |
| 462 |
return TRUE; |
| 463 |
} |
| 464 |
|
| 465 |
if (*framesize + ADTS_MAX_SIZE > avail) { |
| 466 |
/* We have found a possible frame header candidate, but can't be |
| 467 |
sure since we don't have enough data to check the next frame */ |
| 468 |
GST_DEBUG ("NEED MORE DATA: we need %d, available %d", |
| 469 |
*framesize + ADTS_MAX_SIZE, avail); |
| 470 |
*needed_data = *framesize + ADTS_MAX_SIZE; |
| 471 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), |
| 472 |
*framesize + ADTS_MAX_SIZE); |
| 473 |
return FALSE; |
| 474 |
} |
| 475 |
|
| 476 |
if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) { |
| 477 |
guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize)); |
| 478 |
|
| 479 |
GST_LOG ("ADTS frame found, len: %d bytes", *framesize); |
| 480 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), |
| 481 |
nextlen + ADTS_MAX_SIZE); |
| 482 |
return TRUE; |
| 483 |
} |
| 484 |
} |
| 485 |
return FALSE; |
| 486 |
} |
| 487 |
|
| 488 |
static gboolean |
| 489 |
gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br, |
| 490 |
guint32 * value) |
| 491 |
{ |
| 492 |
guint8 bytes, i, byte; |
| 493 |
|
| 494 |
*value = 0; |
| 495 |
if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2)) |
| 496 |
return FALSE; |
| 497 |
for (i = 0; i <= bytes; ++i) { |
| 498 |
*value <<= 8; |
| 499 |
if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8)) |
| 500 |
return FALSE; |
| 501 |
*value += byte; |
| 502 |
} |
| 503 |
return TRUE; |
| 504 |
} |
| 505 |
|
| 506 |
static gboolean |
| 507 |
gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br, |
| 508 |
guint8 * audio_object_type) |
| 509 |
{ |
| 510 |
if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5)) |
| 511 |
return FALSE; |
| 512 |
if (*audio_object_type == 31) { |
| 513 |
if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6)) |
| 514 |
return FALSE; |
| 515 |
*audio_object_type += 32; |
| 516 |
} |
| 517 |
GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type); |
| 518 |
return TRUE; |
| 519 |
} |
| 520 |
|
| 521 |
static gboolean |
| 522 |
gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br, |
| 523 |
gint * sample_rate) |
| 524 |
{ |
| 525 |
guint8 sampling_frequency_index; |
| 526 |
if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4)) |
| 527 |
return FALSE; |
| 528 |
GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u", |
| 529 |
sampling_frequency_index); |
| 530 |
if (sampling_frequency_index == 0xf) { |
| 531 |
guint32 sampling_rate; |
| 532 |
if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24)) |
| 533 |
return FALSE; |
| 534 |
*sample_rate = sampling_rate; |
| 535 |
} else { |
| 536 |
*sample_rate = loas_sample_rate_table[sampling_frequency_index]; |
| 537 |
if (!*sample_rate) |
| 538 |
return FALSE; |
| 539 |
} |
| 540 |
aacparse->last_parsed_sample_rate = *sample_rate; |
| 541 |
return TRUE; |
| 542 |
} |
| 543 |
|
| 544 |
static gboolean |
| 545 |
gst_aac_parse_program_config_element (GstAacParse * aacparse, |
| 546 |
GstBitReader * br, gint * channels) |
| 547 |
{ |
| 548 |
guint8 G_GNUC_UNUSED element_instance_tag; |
| 549 |
guint8 G_GNUC_UNUSED object_type; |
| 550 |
guint8 G_GNUC_UNUSED sampling_frequency_index; |
| 551 |
guint8 num_front_channel_elements; |
| 552 |
guint8 num_side_channel_elements; |
| 553 |
guint8 num_back_channel_elements; |
| 554 |
guint8 num_lfe_channel_elements; |
| 555 |
guint8 program_config_skipping_data; |
| 556 |
guint8 mixdown_present_skipflag; |
| 557 |
guint8 is_cpe; |
| 558 |
guint8 total_num_channel_elements; |
| 559 |
guint8 total_num_channel; |
| 560 |
guint8 channel_element_tag; |
| 561 |
|
| 562 |
if (!gst_bit_reader_get_bits_uint8 (br, &element_instance_tag, 4)) |
| 563 |
return FALSE; |
| 564 |
if (!gst_bit_reader_get_bits_uint8 (br, &object_type, 2)) |
| 565 |
return FALSE; |
| 566 |
if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4)) |
| 567 |
return FALSE; |
| 568 |
if (!gst_bit_reader_get_bits_uint8 (br, &num_front_channel_elements, 4)) |
| 569 |
return FALSE; |
| 570 |
if (!gst_bit_reader_get_bits_uint8 (br, &num_side_channel_elements, 4)) |
| 571 |
return FALSE; |
| 572 |
if (!gst_bit_reader_get_bits_uint8 (br, &num_back_channel_elements, 4)) |
| 573 |
return FALSE; |
| 574 |
if (!gst_bit_reader_get_bits_uint8 (br, &num_lfe_channel_elements, 2)) |
| 575 |
return FALSE; |
| 576 |
|
| 577 |
// skip num_assoc_data_elements + num_valid_cc_elements |
| 578 |
if (!gst_bit_reader_get_bits_uint8 (br, &program_config_skipping_data, 7)) |
| 579 |
return FALSE; |
| 580 |
|
| 581 |
if (!gst_bit_reader_get_bits_uint8 (br, &mixdown_present_skipflag, 1)) |
| 582 |
return FALSE; |
| 583 |
|
| 584 |
// skip mono_mixdown_element_number |
| 585 |
if (mixdown_present_skipflag) |
| 586 |
if (!gst_bit_reader_get_bits_uint8 (br, &program_config_skipping_data, 4)) |
| 587 |
return FALSE; |
| 588 |
|
| 589 |
if (!gst_bit_reader_get_bits_uint8 (br, &mixdown_present_skipflag, 1)) |
| 590 |
return FALSE; |
| 591 |
|
| 592 |
// skip stereo_mixdown_element_number |
| 593 |
if (mixdown_present_skipflag) |
| 594 |
if (!gst_bit_reader_get_bits_uint8 (br, &program_config_skipping_data, 4)) |
| 595 |
return FALSE; |
| 596 |
|
| 597 |
if (!gst_bit_reader_get_bits_uint8 (br, &mixdown_present_skipflag, 1)) |
| 598 |
return FALSE; |
| 599 |
|
| 600 |
// skip matrix_mixdown_idx + pseudo_surround_enable |
| 601 |
if (mixdown_present_skipflag) { |
| 602 |
if (!gst_bit_reader_get_bits_uint8 (br, &program_config_skipping_data, 3)) |
| 603 |
return FALSE; |
| 604 |
} |
| 605 |
|
| 606 |
total_num_channel_elements = |
| 607 |
num_front_channel_elements + num_side_channel_elements + |
| 608 |
num_back_channel_elements; |
| 609 |
|
| 610 |
total_num_channel = total_num_channel_elements + num_lfe_channel_elements; |
| 611 |
// If cpe (coupled), then each single channel element represents two channels |
| 612 |
for (guint8 i = 0; i < total_num_channel_elements; i++) { |
| 613 |
if (!gst_bit_reader_get_bits_uint8 (br, &is_cpe, 1)) |
| 614 |
return FALSE; |
| 615 |
if (is_cpe) |
| 616 |
total_num_channel += 1; |
| 617 |
if (!gst_bit_reader_get_bits_uint8 (br, &channel_element_tag, 4)) |
| 618 |
return FALSE; |
| 619 |
} |
| 620 |
|
| 621 |
*channels = total_num_channel; |
| 622 |
GST_LOG_OBJECT (aacparse, "total channels : %d", *channels); |
| 623 |
|
| 624 |
return TRUE; |
| 625 |
} |
| 626 |
|
| 627 |
static gboolean |
| 628 |
gst_aac_parse_ga_specific_config (GstAacParse * aacparse, |
| 629 |
GstBitReader * br, gint * channels, guint8 channel_configuration) |
| 630 |
{ |
| 631 |
guint8 G_GNUC_UNUSED frame_length_flag; |
| 632 |
guint8 depends_on_core_coder; |
| 633 |
guint32 G_GNUC_UNUSED core_coder_delay; |
| 634 |
guint8 G_GNUC_UNUSED extension_flag; |
| 635 |
|
| 636 |
if (!gst_bit_reader_get_bits_uint8 (br, &frame_length_flag, 1)) |
| 637 |
return FALSE; |
| 638 |
if (!gst_bit_reader_get_bits_uint8 (br, &depends_on_core_coder, 1)) |
| 639 |
return FALSE; |
| 640 |
|
| 641 |
if (depends_on_core_coder) { |
| 642 |
if (!gst_bit_reader_get_bits_uint32 (br, &core_coder_delay, 14)) |
| 643 |
return FALSE; |
| 644 |
} |
| 645 |
|
| 646 |
if (!gst_bit_reader_get_bits_uint8 (br, &extension_flag, 1)) |
| 647 |
return FALSE; |
| 648 |
|
| 649 |
if (!channel_configuration) { |
| 650 |
return gst_aac_parse_program_config_element (aacparse, br, channels); |
| 651 |
} |
| 652 |
|
| 653 |
return TRUE; |
| 654 |
} |
| 655 |
|
| 656 |
/* See table 1.13 in ISO/IEC 14496-3 */ |
| 657 |
static gboolean |
| 658 |
gst_aac_parse_read_audio_specific_config (GstAacParse * aacparse, |
| 659 |
GstBitReader * br, gint * object_type, gint * sample_rate, gint * channels, |
| 660 |
gint * frame_samples) |
| 661 |
{ |
| 662 |
guint8 audio_object_type; |
| 663 |
guint8 G_GNUC_UNUSED extension_audio_object_type; |
| 664 |
guint8 channel_configuration, extension_channel_configuration; |
| 665 |
gboolean G_GNUC_UNUSED sbr = FALSE, ps = FALSE; |
| 666 |
|
| 667 |
if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type)) |
| 668 |
return FALSE; |
| 669 |
if (object_type) |
| 670 |
*object_type = audio_object_type; |
| 671 |
|
| 672 |
if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate)) |
| 673 |
return FALSE; |
| 674 |
|
| 675 |
if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4)) |
| 676 |
return FALSE; |
| 677 |
*channels = loas_channels_table[channel_configuration]; |
| 678 |
GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration); |
| 679 |
|
| 680 |
if (audio_object_type == 5 || audio_object_type == 29) { |
| 681 |
extension_audio_object_type = 5; |
| 682 |
sbr = TRUE; |
| 683 |
if (audio_object_type == 29) { |
| 684 |
ps = TRUE; |
| 685 |
/* Parametric stereo. If we have a one-channel configuration, we can |
| 686 |
* override it to stereo */ |
| 687 |
if (*channels == 1) |
| 688 |
*channels = 2; |
| 689 |
} |
| 690 |
|
| 691 |
GST_LOG_OBJECT (aacparse, |
| 692 |
"Audio object type 5 or 29, so rereading sampling rate (was %d)...", |
| 693 |
*sample_rate); |
| 694 |
if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate)) |
| 695 |
return FALSE; |
| 696 |
|
| 697 |
if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type)) |
| 698 |
return FALSE; |
| 699 |
|
| 700 |
if (audio_object_type == 22) { |
| 701 |
/* extension channel configuration */ |
| 702 |
if (!gst_bit_reader_get_bits_uint8 (br, &extension_channel_configuration, |
| 703 |
4)) |
| 704 |
return FALSE; |
| 705 |
GST_LOG_OBJECT (aacparse, "extension channel_configuration: %d", |
| 706 |
extension_channel_configuration); |
| 707 |
*channels = loas_channels_table[extension_channel_configuration]; |
| 708 |
if (!*channels) |
| 709 |
return FALSE; |
| 710 |
} |
| 711 |
} else { |
| 712 |
extension_audio_object_type = 0; |
| 713 |
} |
| 714 |
|
| 715 |
GST_INFO_OBJECT (aacparse, "Parsed AudioSpecificConfig: %d Hz, %d channels", |
| 716 |
*sample_rate, *channels); |
| 717 |
|
| 718 |
if (frame_samples && audio_object_type == 23) { |
| 719 |
guint8 frame_flag; |
| 720 |
/* Read the Decoder Configuration (GASpecificConfig) if present */ |
| 721 |
/* We only care about the first bit to know what the number of samples |
| 722 |
* in a frame is */ |
| 723 |
if (!gst_bit_reader_get_bits_uint8 (br, &frame_flag, 1)) |
| 724 |
return FALSE; |
| 725 |
*frame_samples = frame_flag ? 960 : 1024; |
| 726 |
} |
| 727 |
|
| 728 |
switch (audio_object_type) { |
| 729 |
case 1: |
| 730 |
case 2: |
| 731 |
case 3: |
| 732 |
case 4: |
| 733 |
case 6: |
| 734 |
case 7: |
| 735 |
case 17: |
| 736 |
case 19: |
| 737 |
case 20: |
| 738 |
case 21: |
| 739 |
case 22: |
| 740 |
case 23: |
| 741 |
if (!gst_aac_parse_ga_specific_config (aacparse, br, channels, |
| 742 |
channel_configuration)) { |
| 743 |
GST_WARNING_OBJECT (aacparse, "Error parsing GASpecificConfig"); |
| 744 |
return FALSE; |
| 745 |
} |
| 746 |
break; |
| 747 |
default: |
| 748 |
break; |
| 749 |
} |
| 750 |
|
| 751 |
if (!*channels) |
| 752 |
return FALSE; |
| 753 |
|
| 754 |
/* There's LOTS of stuff next, but we ignore it for now as we have |
| 755 |
what we want (sample rate and number of channels */ |
| 756 |
GST_DEBUG_OBJECT (aacparse, |
| 757 |
"Need more code to parse humongous LOAS data, currently ignored"); |
| 758 |
aacparse->last_parsed_channels = *channels; |
| 759 |
return TRUE; |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
static gboolean |
| 764 |
gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data, |
| 765 |
guint avail, gint * sample_rate, gint * channels, gint * version) |
| 766 |
{ |
| 767 |
GstBitReader br; |
| 768 |
guint8 u8, v, vA; |
| 769 |
|
| 770 |
/* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */ |
| 771 |
if (version) |
| 772 |
*version = 4; |
| 773 |
|
| 774 |
gst_bit_reader_init (&br, data, avail); |
| 775 |
|
| 776 |
/* skip sync word (11 bits) and size (13 bits) */ |
| 777 |
if (!gst_bit_reader_skip (&br, 11 + 13)) |
| 778 |
return FALSE; |
| 779 |
|
| 780 |
/* First bit is "use last config" */ |
| 781 |
if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1)) |
| 782 |
return FALSE; |
| 783 |
if (u8) { |
| 784 |
GST_LOG_OBJECT (aacparse, "Frame uses previous config"); |
| 785 |
if (!aacparse->last_parsed_sample_rate || !aacparse->last_parsed_channels) { |
| 786 |
GST_DEBUG_OBJECT (aacparse, |
| 787 |
"No previous config to use. We'll look for more data."); |
| 788 |
return FALSE; |
| 789 |
} |
| 790 |
*sample_rate = aacparse->last_parsed_sample_rate; |
| 791 |
*channels = aacparse->last_parsed_channels; |
| 792 |
return TRUE; |
| 793 |
} |
| 794 |
|
| 795 |
GST_DEBUG_OBJECT (aacparse, "Frame contains new config"); |
| 796 |
|
| 797 |
/* audioMuxVersion */ |
| 798 |
if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1)) |
| 799 |
return FALSE; |
| 800 |
if (v) { |
| 801 |
/* audioMuxVersionA */ |
| 802 |
if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1)) |
| 803 |
return FALSE; |
| 804 |
} else |
| 805 |
vA = 0; |
| 806 |
|
| 807 |
GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA); |
| 808 |
if (vA == 0) { |
| 809 |
guint8 same_time, subframes, num_program, prog; |
| 810 |
if (v == 1) { |
| 811 |
guint32 value; |
| 812 |
/* taraBufferFullness */ |
| 813 |
if (!gst_aac_parse_latm_get_value (aacparse, &br, &value)) |
| 814 |
return FALSE; |
| 815 |
} |
| 816 |
if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1)) |
| 817 |
return FALSE; |
| 818 |
if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6)) |
| 819 |
return FALSE; |
| 820 |
if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4)) |
| 821 |
return FALSE; |
| 822 |
GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d", |
| 823 |
same_time, subframes, num_program); |
| 824 |
|
| 825 |
for (prog = 0; prog <= num_program; ++prog) { |
| 826 |
guint8 num_layer, layer; |
| 827 |
if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3)) |
| 828 |
return FALSE; |
| 829 |
GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer); |
| 830 |
|
| 831 |
for (layer = 0; layer <= num_layer; ++layer) { |
| 832 |
guint8 use_same_config; |
| 833 |
if (prog == 0 && layer == 0) { |
| 834 |
use_same_config = 0; |
| 835 |
} else { |
| 836 |
if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1)) |
| 837 |
return FALSE; |
| 838 |
} |
| 839 |
if (!use_same_config) { |
| 840 |
if (v == 0) { |
| 841 |
if (!gst_aac_parse_read_audio_specific_config (aacparse, &br, NULL, |
| 842 |
sample_rate, channels, NULL)) |
| 843 |
return FALSE; |
| 844 |
} else { |
| 845 |
guint32 asc_len; |
| 846 |
if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len)) |
| 847 |
return FALSE; |
| 848 |
if (!gst_aac_parse_read_audio_specific_config (aacparse, &br, NULL, |
| 849 |
sample_rate, channels, NULL)) |
| 850 |
return FALSE; |
| 851 |
if (!gst_bit_reader_skip (&br, asc_len)) |
| 852 |
return FALSE; |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
GST_LOG_OBJECT (aacparse, "More data ignored"); |
| 858 |
} else { |
| 859 |
GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"..."); |
| 860 |
return FALSE; |
| 861 |
} |
| 862 |
return TRUE; |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
* gst_aac_parse_loas_get_frame_len: |
| 867 |
* @data: block of data containing a LOAS header. |
| 868 |
* |
| 869 |
* This function calculates LOAS frame length from the given header. |
| 870 |
* |
| 871 |
* Returns: size of the LOAS frame. |
| 872 |
*/ |
| 873 |
static inline guint |
| 874 |
gst_aac_parse_loas_get_frame_len (const guint8 * data) |
| 875 |
{ |
| 876 |
return (((data[1] & 0x1f) << 8) | data[2]) + 3; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
/** |
| 881 |
* gst_aac_parse_check_loas_frame: |
| 882 |
* @aacparse: #GstAacParse. |
| 883 |
* @data: Data to be checked. |
| 884 |
* @avail: Amount of data passed. |
| 885 |
* @framesize: If valid LOAS frame was found, this will be set to tell the |
| 886 |
* found frame size in bytes. |
| 887 |
* @needed_data: If frame was not found, this may be set to tell how much |
| 888 |
* more data is needed in the next round to detect the frame |
| 889 |
* reliably. This may happen when a frame header candidate |
| 890 |
* is found but it cannot be guaranteed to be the header without |
| 891 |
* peeking the following data. |
| 892 |
* |
| 893 |
* Check if the given data contains contains LOAS frame. The algorithm |
| 894 |
* will examine LOAS frame header and calculate the frame size. Also, another |
| 895 |
* consecutive LOAS frame header need to be present after the found frame. |
| 896 |
* Otherwise the data is not considered as a valid LOAS frame. However, this |
| 897 |
* "extra check" is omitted when EOS has been received. In this case it is |
| 898 |
* enough when data[0] contains a valid LOAS header. |
| 899 |
* |
| 900 |
* This function may set the #needed_data to indicate that a possible frame |
| 901 |
* candidate has been found, but more data (#needed_data bytes) is needed to |
| 902 |
* be absolutely sure. When this situation occurs, FALSE will be returned. |
| 903 |
* |
| 904 |
* When a valid frame is detected, this function will use |
| 905 |
* gst_base_parse_set_min_frame_size() function from #GstBaseParse class |
| 906 |
* to set the needed bytes for next frame.This way next data chunk is already |
| 907 |
* of correct size. |
| 908 |
* |
| 909 |
* LOAS can have three different formats, if I read the spec correctly. Only |
| 910 |
* one of them is supported here, as the two samples I have use this one. |
| 911 |
* |
| 912 |
* Returns: TRUE if the given data contains a valid LOAS header. |
| 913 |
*/ |
| 914 |
static gboolean |
| 915 |
gst_aac_parse_check_loas_frame (GstAacParse * aacparse, |
| 916 |
const guint8 * data, const guint avail, gboolean drain, |
| 917 |
guint * framesize, guint * needed_data) |
| 918 |
{ |
| 919 |
*needed_data = 0; |
| 920 |
|
| 921 |
/* 3 byte header */ |
| 922 |
if (G_UNLIKELY (avail < 3)) { |
| 923 |
*needed_data = 3; |
| 924 |
return FALSE; |
| 925 |
} |
| 926 |
|
| 927 |
if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) { |
| 928 |
*framesize = gst_aac_parse_loas_get_frame_len (data); |
| 929 |
GST_DEBUG_OBJECT (aacparse, "Found possible %u byte LOAS frame", |
| 930 |
*framesize); |
| 931 |
|
| 932 |
/* In EOS mode this is enough. No need to examine the data further. |
| 933 |
We also relax the check when we have sync, on the assumption that |
| 934 |
if we're not looking at random data, we have a much higher chance |
| 935 |
to get the correct sync, and this avoids losing two frames when |
| 936 |
a single bit corruption happens. */ |
| 937 |
if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) { |
| 938 |
return TRUE; |
| 939 |
} |
| 940 |
|
| 941 |
if (*framesize + LOAS_MAX_SIZE > avail) { |
| 942 |
/* We have found a possible frame header candidate, but can't be |
| 943 |
sure since we don't have enough data to check the next frame */ |
| 944 |
GST_DEBUG ("NEED MORE DATA: we need %d, available %d", |
| 945 |
*framesize + LOAS_MAX_SIZE, avail); |
| 946 |
*needed_data = *framesize + LOAS_MAX_SIZE; |
| 947 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), |
| 948 |
*framesize + LOAS_MAX_SIZE); |
| 949 |
return FALSE; |
| 950 |
} |
| 951 |
|
| 952 |
if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) { |
| 953 |
guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize)); |
| 954 |
|
| 955 |
GST_LOG ("LOAS frame found, len: %d bytes", *framesize); |
| 956 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), |
| 957 |
nextlen + LOAS_MAX_SIZE); |
| 958 |
return TRUE; |
| 959 |
} else { |
| 960 |
GST_DEBUG_OBJECT (aacparse, "That was a false positive"); |
| 961 |
} |
| 962 |
} |
| 963 |
return FALSE; |
| 964 |
} |
| 965 |
|
| 966 |
static inline gboolean |
| 967 |
gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data, |
| 968 |
const guint avail, gint * rate, gint * channels, gint * object, |
| 969 |
gint * version) |
| 970 |
{ |
| 971 |
if (avail < 4) |
| 972 |
return FALSE; |
| 973 |
|
| 974 |
if (rate) { |
| 975 |
gint sr_idx = (data[2] & 0x3c) >> 2; |
| 976 |
|
| 977 |
*rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx); |
| 978 |
} |
| 979 |
if (channels) { |
| 980 |
*channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6); |
| 981 |
if (*channels == 7) |
| 982 |
*channels = 8; |
| 983 |
} |
| 984 |
|
| 985 |
if (version) |
| 986 |
*version = (data[1] & 0x08) ? 2 : 4; |
| 987 |
if (object) |
| 988 |
*object = ((data[2] & 0xc0) >> 6) + 1; |
| 989 |
|
| 990 |
return TRUE; |
| 991 |
} |
| 992 |
|
| 993 |
/** |
| 994 |
* gst_aac_parse_detect_stream: |
| 995 |
* @aacparse: #GstAacParse. |
| 996 |
* @data: A block of data that needs to be examined for stream characteristics. |
| 997 |
* @avail: Size of the given datablock. |
| 998 |
* @framesize: If valid stream was found, this will be set to tell the |
| 999 |
* first frame size in bytes. |
| 1000 |
* @skipsize: If valid stream was found, this will be set to tell the first |
| 1001 |
* audio frame position within the given data. |
| 1002 |
* |
| 1003 |
* Examines the given piece of data and try to detect the format of it. It |
| 1004 |
* checks for "ADIF" header (in the beginning of the clip) and ADTS frame |
| 1005 |
* header. If the stream is detected, TRUE will be returned and #framesize |
| 1006 |
* is set to indicate the found frame size. Additionally, #skipsize might |
| 1007 |
* be set to indicate the number of bytes that need to be skipped, a.k.a. the |
| 1008 |
* position of the frame inside given data chunk. |
| 1009 |
* |
| 1010 |
* Returns: TRUE on success. |
| 1011 |
*/ |
| 1012 |
static gboolean |
| 1013 |
gst_aac_parse_detect_stream (GstAacParse * aacparse, |
| 1014 |
const guint8 * data, const guint avail, gboolean drain, |
| 1015 |
guint * framesize, gint * skipsize) |
| 1016 |
{ |
| 1017 |
gboolean found = FALSE; |
| 1018 |
guint need_data_adts = 0, need_data_loas; |
| 1019 |
guint i = 0; |
| 1020 |
|
| 1021 |
GST_DEBUG_OBJECT (aacparse, "Parsing header data"); |
| 1022 |
|
| 1023 |
/* FIXME: No need to check for ADIF if we are not in the beginning of the |
| 1024 |
stream */ |
| 1025 |
|
| 1026 |
/* Can we even parse the header? */ |
| 1027 |
if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) { |
| 1028 |
GST_DEBUG_OBJECT (aacparse, "Not enough data to check"); |
| 1029 |
return FALSE; |
| 1030 |
} |
| 1031 |
|
| 1032 |
for (i = 0; i < avail - 4; i++) { |
| 1033 |
if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) || |
| 1034 |
((data[i] == 0x56) && ((data[i + 1] & 0xe0) == 0xe0)) || |
| 1035 |
strncmp ((char *) data + i, "ADIF", 4) == 0) { |
| 1036 |
GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i); |
| 1037 |
found = TRUE; |
| 1038 |
|
| 1039 |
if (i) { |
| 1040 |
/* Trick: tell the parent class that we didn't find the frame yet, |
| 1041 |
but make it skip 'i' amount of bytes. Next time we arrive |
| 1042 |
here we have full frame in the beginning of the data. */ |
| 1043 |
*skipsize = i; |
| 1044 |
return FALSE; |
| 1045 |
} |
| 1046 |
break; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
if (!found) { |
| 1050 |
if (i) |
| 1051 |
*skipsize = i; |
| 1052 |
return FALSE; |
| 1053 |
} |
| 1054 |
|
| 1055 |
if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain, |
| 1056 |
framesize, &need_data_adts)) { |
| 1057 |
gint rate, channels; |
| 1058 |
|
| 1059 |
GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize); |
| 1060 |
|
| 1061 |
if (!gst_aac_parse_parse_adts_header (aacparse, data, avail, &rate, |
| 1062 |
&channels, &aacparse->object_type, &aacparse->mpegversion)) { |
| 1063 |
GST_DEBUG_OBJECT (aacparse, |
| 1064 |
"impossible ADTS configuration or not enough data"); |
| 1065 |
return FALSE; |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (!channels || !framesize) { |
| 1069 |
GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration"); |
| 1070 |
return FALSE; |
| 1071 |
} |
| 1072 |
|
| 1073 |
aacparse->header_type = DSPAAC_HEADER_ADTS; |
| 1074 |
gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate, |
| 1075 |
aacparse->frame_samples, 2, 2); |
| 1076 |
|
| 1077 |
GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d", |
| 1078 |
rate, channels, aacparse->object_type, aacparse->mpegversion); |
| 1079 |
|
| 1080 |
gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE); |
| 1081 |
|
| 1082 |
return TRUE; |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain, |
| 1086 |
framesize, &need_data_loas)) { |
| 1087 |
gint rate = 0, channels = 0; |
| 1088 |
|
| 1089 |
GST_INFO ("LOAS, framesize: %d", *framesize); |
| 1090 |
|
| 1091 |
aacparse->header_type = DSPAAC_HEADER_LOAS; |
| 1092 |
|
| 1093 |
if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate, |
| 1094 |
&channels, &aacparse->mpegversion)) { |
| 1095 |
/* This is pretty normal when skipping data at the start of |
| 1096 |
* random stream (MPEG-TS capture for example) */ |
| 1097 |
GST_LOG_OBJECT (aacparse, "Error reading LOAS config"); |
| 1098 |
return FALSE; |
| 1099 |
} |
| 1100 |
|
| 1101 |
if (rate && channels) { |
| 1102 |
gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate, |
| 1103 |
aacparse->frame_samples, 2, 2); |
| 1104 |
|
| 1105 |
/* Don't store the sample rate and channels yet - |
| 1106 |
* this is just format detection. */ |
| 1107 |
GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d", |
| 1108 |
rate, channels, aacparse->object_type, aacparse->mpegversion); |
| 1109 |
} |
| 1110 |
|
| 1111 |
gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE); |
| 1112 |
|
| 1113 |
return TRUE; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (need_data_adts || need_data_loas) { |
| 1117 |
/* This tells the parent class not to skip any data */ |
| 1118 |
*skipsize = 0; |
| 1119 |
return FALSE; |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (avail < ADIF_MAX_SIZE) |
| 1123 |
return FALSE; |
| 1124 |
|
| 1125 |
if (memcmp (data + i, "ADIF", 4) == 0) { |
| 1126 |
const guint8 *adif; |
| 1127 |
int skip_size = 0; |
| 1128 |
int bitstream_type; |
| 1129 |
int sr_idx; |
| 1130 |
GstCaps *sinkcaps; |
| 1131 |
|
| 1132 |
aacparse->header_type = DSPAAC_HEADER_ADIF; |
| 1133 |
aacparse->mpegversion = 4; |
| 1134 |
|
| 1135 |
/* Skip the "ADIF" bytes */ |
| 1136 |
adif = data + i + 4; |
| 1137 |
|
| 1138 |
/* copyright string */ |
| 1139 |
if (adif[0] & 0x80) |
| 1140 |
skip_size += 9; /* skip 9 bytes */ |
| 1141 |
|
| 1142 |
bitstream_type = adif[0 + skip_size] & 0x10; |
| 1143 |
aacparse->bitrate = |
| 1144 |
((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) | |
| 1145 |
((unsigned int) adif[1 + skip_size] << 11) | |
| 1146 |
((unsigned int) adif[2 + skip_size] << 3) | |
| 1147 |
((unsigned int) adif[3 + skip_size] & 0xe0); |
| 1148 |
|
| 1149 |
/* CBR */ |
| 1150 |
if (bitstream_type == 0) { |
| 1151 |
#if 0 |
| 1152 |
/* Buffer fullness parsing. Currently not needed... */ |
| 1153 |
guint num_elems = 0; |
| 1154 |
guint fullness = 0; |
| 1155 |
|
| 1156 |
num_elems = (adif[3 + skip_size] & 0x1e); |
| 1157 |
GST_INFO ("ADIF num_config_elems: %d", num_elems); |
| 1158 |
|
| 1159 |
fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) | |
| 1160 |
((unsigned int) adif[4 + skip_size] << 11) | |
| 1161 |
((unsigned int) adif[5 + skip_size] << 3) | |
| 1162 |
((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5); |
| 1163 |
|
| 1164 |
GST_INFO ("ADIF buffer fullness: %d", fullness); |
| 1165 |
#endif |
| 1166 |
aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) | |
| 1167 |
((adif[7 + skip_size] & 0x80) >> 7); |
| 1168 |
sr_idx = (adif[7 + skip_size] & 0x78) >> 3; |
| 1169 |
} |
| 1170 |
/* VBR */ |
| 1171 |
else { |
| 1172 |
aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3; |
| 1173 |
sr_idx = ((adif[4 + skip_size] & 0x07) << 1) | |
| 1174 |
((adif[5 + skip_size] & 0x80) >> 7); |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* FIXME: This gives totally wrong results. Duration calculation cannot |
| 1178 |
be based on this */ |
| 1179 |
aacparse->sample_rate = |
| 1180 |
gst_codec_utils_aac_get_sample_rate_from_index (sr_idx); |
| 1181 |
|
| 1182 |
/* baseparse is not given any fps, |
| 1183 |
* so it will give up on timestamps, seeking, etc */ |
| 1184 |
|
| 1185 |
/* FIXME: Can we assume this? */ |
| 1186 |
aacparse->channels = 2; |
| 1187 |
|
| 1188 |
GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d", |
| 1189 |
aacparse->bitrate, aacparse->sample_rate, aacparse->object_type); |
| 1190 |
|
| 1191 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512); |
| 1192 |
|
| 1193 |
/* arrange for metadata and get out of the way */ |
| 1194 |
sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse)); |
| 1195 |
gst_aac_parse_set_src_caps (aacparse, sinkcaps); |
| 1196 |
if (sinkcaps) |
| 1197 |
gst_caps_unref (sinkcaps); |
| 1198 |
|
| 1199 |
/* not syncable, not easily seekable (unless we push data from start */ |
| 1200 |
gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE); |
| 1201 |
gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE); |
| 1202 |
gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0); |
| 1203 |
|
| 1204 |
*framesize = avail; |
| 1205 |
return TRUE; |
| 1206 |
} |
| 1207 |
|
| 1208 |
/* This should never happen */ |
| 1209 |
return FALSE; |
| 1210 |
} |
| 1211 |
|
| 1212 |
/** |
| 1213 |
* gst_aac_parse_get_audio_profile_object_type |
| 1214 |
* @aacparse: #GstAacParse. |
| 1215 |
* |
| 1216 |
* Gets the MPEG-2 profile or the MPEG-4 object type value corresponding to the |
| 1217 |
* mpegversion and profile of @aacparse's src pad caps, according to the |
| 1218 |
* values defined by table 1.A.11 in ISO/IEC 14496-3. |
| 1219 |
* |
| 1220 |
* Returns: the profile or object type value corresponding to @aacparse's src |
| 1221 |
* pad caps, if such a value exists; otherwise G_MAXUINT8. |
| 1222 |
*/ |
| 1223 |
static guint8 |
| 1224 |
gst_aac_parse_get_audio_profile_object_type (GstAacParse * aacparse) |
| 1225 |
{ |
| 1226 |
GstCaps *srccaps; |
| 1227 |
GstStructure *srcstruct; |
| 1228 |
const gchar *profile; |
| 1229 |
guint8 ret; |
| 1230 |
|
| 1231 |
srccaps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse)); |
| 1232 |
if (G_UNLIKELY (srccaps == NULL)) { |
| 1233 |
return G_MAXUINT8; |
| 1234 |
} |
| 1235 |
|
| 1236 |
srcstruct = gst_caps_get_structure (srccaps, 0); |
| 1237 |
profile = gst_structure_get_string (srcstruct, "profile"); |
| 1238 |
if (G_UNLIKELY (profile == NULL)) { |
| 1239 |
gst_caps_unref (srccaps); |
| 1240 |
return G_MAXUINT8; |
| 1241 |
} |
| 1242 |
|
| 1243 |
if (g_strcmp0 (profile, "main") == 0) { |
| 1244 |
ret = (guint8) 0U; |
| 1245 |
} else if (g_strcmp0 (profile, "lc") == 0) { |
| 1246 |
ret = (guint8) 1U; |
| 1247 |
} else if (g_strcmp0 (profile, "ssr") == 0) { |
| 1248 |
ret = (guint8) 2U; |
| 1249 |
} else if (g_strcmp0 (profile, "ltp") == 0) { |
| 1250 |
if (G_LIKELY (aacparse->mpegversion == 4)) |
| 1251 |
ret = (guint8) 3U; |
| 1252 |
else |
| 1253 |
ret = G_MAXUINT8; /* LTP Object Type allowed only for MPEG-4 */ |
| 1254 |
} else { |
| 1255 |
ret = G_MAXUINT8; |
| 1256 |
} |
| 1257 |
|
| 1258 |
gst_caps_unref (srccaps); |
| 1259 |
return ret; |
| 1260 |
} |
| 1261 |
|
| 1262 |
/** |
| 1263 |
* gst_aac_parse_get_audio_channel_configuration |
| 1264 |
* @num_channels: number of audio channels. |
| 1265 |
* |
| 1266 |
* Gets the Channel Configuration value, as defined by table 1.19 in ISO/IEC |
| 1267 |
* 14496-3, for a given number of audio channels. |
| 1268 |
* |
| 1269 |
* Returns: the Channel Configuration value corresponding to @num_channels, if |
| 1270 |
* such a value exists; otherwise G_MAXUINT8. |
| 1271 |
*/ |
| 1272 |
static guint8 |
| 1273 |
gst_aac_parse_get_audio_channel_configuration (gint num_channels) |
| 1274 |
{ |
| 1275 |
if (num_channels >= 1 && num_channels <= 6) /* Mono up to & including 5.1 */ |
| 1276 |
return (guint8) num_channels; |
| 1277 |
else if (num_channels == 8) /* 7.1 */ |
| 1278 |
return (guint8) 7U; |
| 1279 |
else |
| 1280 |
return G_MAXUINT8; |
| 1281 |
|
| 1282 |
/* FIXME: Add support for configurations 11, 12 and 14 from |
| 1283 |
* ISO/IEC 14496-3:2009/PDAM 4 based on the actual channel layout |
| 1284 |
*/ |
| 1285 |
} |
| 1286 |
|
| 1287 |
/** |
| 1288 |
* gst_aac_parse_get_audio_sampling_frequency_index: |
| 1289 |
* @sample_rate: audio sampling rate. |
| 1290 |
* |
| 1291 |
* Gets the Sampling Frequency Index value, as defined by table 1.18 in ISO/IEC |
| 1292 |
* 14496-3, for a given sampling rate. |
| 1293 |
* |
| 1294 |
* Returns: the Sampling Frequency Index value corresponding to @sample_rate, |
| 1295 |
* if such a value exists; otherwise G_MAXUINT8. |
| 1296 |
*/ |
| 1297 |
static guint8 |
| 1298 |
gst_aac_parse_get_audio_sampling_frequency_index (gint sample_rate) |
| 1299 |
{ |
| 1300 |
switch (sample_rate) { |
| 1301 |
case 96000: |
| 1302 |
return 0x0U; |
| 1303 |
case 88200: |
| 1304 |
return 0x1U; |
| 1305 |
case 64000: |
| 1306 |
return 0x2U; |
| 1307 |
case 48000: |
| 1308 |
return 0x3U; |
| 1309 |
case 44100: |
| 1310 |
return 0x4U; |
| 1311 |
case 32000: |
| 1312 |
return 0x5U; |
| 1313 |
case 24000: |
| 1314 |
return 0x6U; |
| 1315 |
case 22050: |
| 1316 |
return 0x7U; |
| 1317 |
case 16000: |
| 1318 |
return 0x8U; |
| 1319 |
case 12000: |
| 1320 |
return 0x9U; |
| 1321 |
case 11025: |
| 1322 |
return 0xAU; |
| 1323 |
case 8000: |
| 1324 |
return 0xBU; |
| 1325 |
case 7350: |
| 1326 |
return 0xCU; |
| 1327 |
default: |
| 1328 |
return G_MAXUINT8; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
/** |
| 1333 |
* gst_aac_parse_prepend_adts_headers: |
| 1334 |
* @aacparse: #GstAacParse. |
| 1335 |
* @frame: raw AAC frame to which ADTS headers shall be prepended. |
| 1336 |
* |
| 1337 |
* Prepends ADTS headers to a raw AAC audio frame. |
| 1338 |
* |
| 1339 |
* Returns: TRUE if ADTS headers were successfully prepended; FALSE otherwise. |
| 1340 |
*/ |
| 1341 |
static gboolean |
| 1342 |
gst_aac_parse_prepend_adts_headers (GstAacParse * aacparse, |
| 1343 |
GstBaseParseFrame * frame) |
| 1344 |
{ |
| 1345 |
GstMemory *mem; |
| 1346 |
guint8 *adts_headers; |
| 1347 |
gsize buf_size; |
| 1348 |
gsize frame_size; |
| 1349 |
guint8 id, profile, channel_configuration, sampling_frequency_index; |
| 1350 |
|
| 1351 |
id = (aacparse->mpegversion == 4) ? 0x0U : 0x1U; |
| 1352 |
profile = gst_aac_parse_get_audio_profile_object_type (aacparse); |
| 1353 |
if (profile == G_MAXUINT8) { |
| 1354 |
GST_ERROR_OBJECT (aacparse, "Unsupported audio profile or object type"); |
| 1355 |
return FALSE; |
| 1356 |
} |
| 1357 |
channel_configuration = |
| 1358 |
gst_aac_parse_get_audio_channel_configuration (aacparse->channels); |
| 1359 |
if (channel_configuration == G_MAXUINT8) { |
| 1360 |
GST_ERROR_OBJECT (aacparse, "Unsupported number of channels"); |
| 1361 |
return FALSE; |
| 1362 |
} |
| 1363 |
sampling_frequency_index = |
| 1364 |
gst_aac_parse_get_audio_sampling_frequency_index (aacparse->sample_rate); |
| 1365 |
if (sampling_frequency_index == G_MAXUINT8) { |
| 1366 |
GST_ERROR_OBJECT (aacparse, "Unsupported sampling frequency"); |
| 1367 |
return FALSE; |
| 1368 |
} |
| 1369 |
|
| 1370 |
frame->out_buffer = gst_buffer_copy (frame->buffer); |
| 1371 |
buf_size = gst_buffer_get_size (frame->out_buffer); |
| 1372 |
frame_size = buf_size + ADTS_HEADERS_LENGTH; |
| 1373 |
|
| 1374 |
if (G_UNLIKELY (frame_size >= 0x4000)) { |
| 1375 |
GST_ERROR_OBJECT (aacparse, "Frame size is too big for ADTS"); |
| 1376 |
return FALSE; |
| 1377 |
} |
| 1378 |
|
| 1379 |
adts_headers = (guint8 *) g_malloc0 (ADTS_HEADERS_LENGTH); |
| 1380 |
|
| 1381 |
/* Note: no error correction bits are added to the resulting ADTS frames */ |
| 1382 |
adts_headers[0] = 0xFFU; |
| 1383 |
adts_headers[1] = 0xF0U | (id << 3) | 0x1U; |
| 1384 |
adts_headers[2] = (profile << 6) | (sampling_frequency_index << 2) | 0x2U | |
| 1385 |
((channel_configuration & 0x4U) >> 2); |
| 1386 |
adts_headers[3] = ((channel_configuration & 0x3U) << 6) | 0x30U | |
| 1387 |
(guint8) (frame_size >> 11); |
| 1388 |
adts_headers[4] = (guint8) ((frame_size >> 3) & 0x00FF); |
| 1389 |
adts_headers[5] = (guint8) (((frame_size & 0x0007) << 5) + 0x1FU); |
| 1390 |
adts_headers[6] = 0xFCU; |
| 1391 |
|
| 1392 |
mem = gst_memory_new_wrapped (0, adts_headers, ADTS_HEADERS_LENGTH, 0, |
| 1393 |
ADTS_HEADERS_LENGTH, adts_headers, g_free); |
| 1394 |
gst_buffer_prepend_memory (frame->out_buffer, mem); |
| 1395 |
|
| 1396 |
return TRUE; |
| 1397 |
} |
| 1398 |
|
| 1399 |
/** |
| 1400 |
* gst_aac_parse_check_valid_frame: |
| 1401 |
* @parse: #GstBaseParse. |
| 1402 |
* @frame: #GstBaseParseFrame. |
| 1403 |
* @skipsize: How much data parent class should skip in order to find the |
| 1404 |
* frame header. |
| 1405 |
* |
| 1406 |
* Implementation of "handle_frame" vmethod in #GstBaseParse class. |
| 1407 |
* |
| 1408 |
* Also determines frame overhead. |
| 1409 |
* ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have |
| 1410 |
* a per-frame header. LOAS has 3 bytes. |
| 1411 |
* |
| 1412 |
* We're making a couple of simplifying assumptions: |
| 1413 |
* |
| 1414 |
* 1. We count Program Configuration Elements rather than searching for them |
| 1415 |
* in the streams to discount them - the overhead is negligible. |
| 1416 |
* |
| 1417 |
* 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16 |
| 1418 |
* bits, which should still not be significant enough to warrant the |
| 1419 |
* additional parsing through the headers |
| 1420 |
* |
| 1421 |
* Returns: a #GstFlowReturn. |
| 1422 |
*/ |
| 1423 |
static GstFlowReturn |
| 1424 |
gst_aac_parse_handle_frame (GstBaseParse * parse, |
| 1425 |
GstBaseParseFrame * frame, gint * skipsize) |
| 1426 |
{ |
| 1427 |
GstMapInfo map; |
| 1428 |
GstAacParse *aacparse; |
| 1429 |
gboolean ret = FALSE; |
| 1430 |
gboolean lost_sync; |
| 1431 |
GstBuffer *buffer; |
| 1432 |
guint framesize; |
| 1433 |
gint rate = 0, channels = 0; |
| 1434 |
|
| 1435 |
aacparse = GST_AAC_PARSE (parse); |
| 1436 |
buffer = frame->buffer; |
| 1437 |
|
| 1438 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 1439 |
|
| 1440 |
*skipsize = -1; |
| 1441 |
lost_sync = GST_BASE_PARSE_LOST_SYNC (parse); |
| 1442 |
|
| 1443 |
if (aacparse->header_type == DSPAAC_HEADER_ADIF || |
| 1444 |
aacparse->header_type == DSPAAC_HEADER_NONE) { |
| 1445 |
/* There is nothing to parse */ |
| 1446 |
framesize = map.size; |
| 1447 |
ret = TRUE; |
| 1448 |
|
| 1449 |
} else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) { |
| 1450 |
|
| 1451 |
ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size, |
| 1452 |
GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize); |
| 1453 |
|
| 1454 |
} else if (aacparse->header_type == DSPAAC_HEADER_ADTS) { |
| 1455 |
guint needed_data = 1024; |
| 1456 |
|
| 1457 |
ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size, |
| 1458 |
GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data); |
| 1459 |
|
| 1460 |
if (!ret && needed_data) { |
| 1461 |
GST_DEBUG ("buffer didn't contain valid frame"); |
| 1462 |
*skipsize = 0; |
| 1463 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), |
| 1464 |
needed_data); |
| 1465 |
} |
| 1466 |
|
| 1467 |
} else if (aacparse->header_type == DSPAAC_HEADER_LOAS) { |
| 1468 |
guint needed_data = 1024; |
| 1469 |
|
| 1470 |
ret = gst_aac_parse_check_loas_frame (aacparse, map.data, |
| 1471 |
map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data); |
| 1472 |
|
| 1473 |
if (!ret && needed_data) { |
| 1474 |
GST_DEBUG ("buffer didn't contain valid frame"); |
| 1475 |
*skipsize = 0; |
| 1476 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), |
| 1477 |
needed_data); |
| 1478 |
} |
| 1479 |
|
| 1480 |
} else { |
| 1481 |
GST_DEBUG ("buffer didn't contain valid frame"); |
| 1482 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), |
| 1483 |
ADTS_MAX_SIZE); |
| 1484 |
} |
| 1485 |
|
| 1486 |
if (G_UNLIKELY (!ret)) |
| 1487 |
goto exit; |
| 1488 |
|
| 1489 |
ret = framesize <= map.size; |
| 1490 |
|
| 1491 |
if (aacparse->header_type == DSPAAC_HEADER_ADTS) { |
| 1492 |
/* see above */ |
| 1493 |
frame->overhead = 7; |
| 1494 |
|
| 1495 |
if (!gst_aac_parse_parse_adts_header (aacparse, map.data, map.size, |
| 1496 |
&rate, &channels, NULL, NULL)) { |
| 1497 |
/* This is pretty normal when skipping data at the start of |
| 1498 |
* random stream (MPEG-TS capture for example) */ |
| 1499 |
GST_DEBUG_OBJECT (aacparse, "Error reading ADTS header. Skipping."); |
| 1500 |
/* Since we don't fully parsed the ADTS header, we don't know for sure |
| 1501 |
* how much to skip. Just skip 1 to end up to the next marker and |
| 1502 |
* resume parsing from there */ |
| 1503 |
*skipsize = 1; |
| 1504 |
goto exit; |
| 1505 |
} |
| 1506 |
|
| 1507 |
GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels); |
| 1508 |
|
| 1509 |
if (G_UNLIKELY (rate != aacparse->sample_rate |
| 1510 |
|| channels != aacparse->channels)) { |
| 1511 |
aacparse->sample_rate = rate; |
| 1512 |
aacparse->channels = channels; |
| 1513 |
|
| 1514 |
gst_aac_parse_set_src_caps (aacparse, NULL); |
| 1515 |
|
| 1516 |
gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), |
| 1517 |
aacparse->sample_rate, aacparse->frame_samples, 2, 2); |
| 1518 |
} |
| 1519 |
} else if (aacparse->header_type == DSPAAC_HEADER_LOAS) { |
| 1520 |
gboolean setcaps = FALSE; |
| 1521 |
|
| 1522 |
/* see above */ |
| 1523 |
frame->overhead = 3; |
| 1524 |
|
| 1525 |
if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate, |
| 1526 |
&channels, NULL) || !rate || !channels) { |
| 1527 |
/* This is pretty normal when skipping data at the start of |
| 1528 |
* random stream (MPEG-TS capture for example) */ |
| 1529 |
GST_DEBUG_OBJECT (aacparse, "Error reading LOAS config. Skipping."); |
| 1530 |
/* Since we don't fully parse the LOAS config, we don't know for sure |
| 1531 |
* how much to skip. Just skip 1 to end up to the next marker and |
| 1532 |
* resume parsing from there */ |
| 1533 |
*skipsize = 1; |
| 1534 |
goto exit; |
| 1535 |
} |
| 1536 |
|
| 1537 |
if (G_UNLIKELY (rate != aacparse->sample_rate |
| 1538 |
|| channels != aacparse->channels)) { |
| 1539 |
aacparse->sample_rate = rate; |
| 1540 |
aacparse->channels = channels; |
| 1541 |
setcaps = TRUE; |
| 1542 |
GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate, |
| 1543 |
channels); |
| 1544 |
} |
| 1545 |
|
| 1546 |
/* We want to set caps both at start, and when rate/channels change. |
| 1547 |
Since only some LOAS frames have that info, we may receive frames |
| 1548 |
before knowing about rate/channels. */ |
| 1549 |
if (setcaps |
| 1550 |
|| !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) { |
| 1551 |
gst_aac_parse_set_src_caps (aacparse, NULL); |
| 1552 |
|
| 1553 |
gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), |
| 1554 |
aacparse->sample_rate, aacparse->frame_samples, 2, 2); |
| 1555 |
} |
| 1556 |
} |
| 1557 |
|
| 1558 |
if (aacparse->header_type == DSPAAC_HEADER_NONE |
| 1559 |
&& aacparse->output_header_type == DSPAAC_HEADER_ADTS) { |
| 1560 |
if (!gst_aac_parse_prepend_adts_headers (aacparse, frame)) { |
| 1561 |
GST_ERROR_OBJECT (aacparse, "Failed to prepend ADTS headers to frame"); |
| 1562 |
gst_buffer_unmap (buffer, &map); |
| 1563 |
return GST_FLOW_ERROR; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
exit: |
| 1568 |
gst_buffer_unmap (buffer, &map); |
| 1569 |
|
| 1570 |
if (ret) { |
| 1571 |
/* found, skip if needed */ |
| 1572 |
if (*skipsize > 0) |
| 1573 |
return GST_FLOW_OK; |
| 1574 |
*skipsize = 0; |
| 1575 |
} else { |
| 1576 |
if (*skipsize < 0) |
| 1577 |
*skipsize = 1; |
| 1578 |
} |
| 1579 |
|
| 1580 |
if (ret) { |
| 1581 |
return gst_base_parse_finish_frame (parse, frame, framesize); |
| 1582 |
} |
| 1583 |
|
| 1584 |
return GST_FLOW_OK; |
| 1585 |
} |
| 1586 |
|
| 1587 |
static GstFlowReturn |
| 1588 |
gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 1589 |
{ |
| 1590 |
GstAacParse *aacparse = GST_AAC_PARSE (parse); |
| 1591 |
|
| 1592 |
if (!aacparse->sent_codec_tag) { |
| 1593 |
GstTagList *taglist; |
| 1594 |
GstCaps *caps; |
| 1595 |
|
| 1596 |
/* codec tag */ |
| 1597 |
caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse)); |
| 1598 |
if (caps == NULL) { |
| 1599 |
if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) { |
| 1600 |
GST_INFO_OBJECT (parse, "Src pad is flushing"); |
| 1601 |
return GST_FLOW_FLUSHING; |
| 1602 |
} else { |
| 1603 |
GST_INFO_OBJECT (parse, "Src pad is not negotiated!"); |
| 1604 |
return GST_FLOW_NOT_NEGOTIATED; |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
taglist = gst_tag_list_new_empty (); |
| 1609 |
gst_pb_utils_add_codec_description_to_tag_list (taglist, |
| 1610 |
GST_TAG_AUDIO_CODEC, caps); |
| 1611 |
gst_caps_unref (caps); |
| 1612 |
|
| 1613 |
gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE); |
| 1614 |
gst_tag_list_unref (taglist); |
| 1615 |
|
| 1616 |
/* also signals the end of first-frame processing */ |
| 1617 |
aacparse->sent_codec_tag = TRUE; |
| 1618 |
} |
| 1619 |
|
| 1620 |
/* As a special case, we can remove the ADTS framing and output raw AAC. */ |
| 1621 |
if (aacparse->header_type == DSPAAC_HEADER_ADTS |
| 1622 |
&& aacparse->output_header_type == DSPAAC_HEADER_NONE) { |
| 1623 |
guint header_size; |
| 1624 |
GstMapInfo map; |
| 1625 |
frame->out_buffer = gst_buffer_make_writable (frame->buffer); |
| 1626 |
frame->buffer = NULL; |
| 1627 |
gst_buffer_map (frame->out_buffer, &map, GST_MAP_READ); |
| 1628 |
header_size = (map.data[1] & 1) ? 7 : 9; /* optional CRC */ |
| 1629 |
gst_buffer_unmap (frame->out_buffer, &map); |
| 1630 |
gst_buffer_resize (frame->out_buffer, header_size, |
| 1631 |
gst_buffer_get_size (frame->out_buffer) - header_size); |
| 1632 |
} |
| 1633 |
|
| 1634 |
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP; |
| 1635 |
|
| 1636 |
return GST_FLOW_OK; |
| 1637 |
} |
| 1638 |
|
| 1639 |
|
| 1640 |
/** |
| 1641 |
* gst_aac_parse_start: |
| 1642 |
* @parse: #GstBaseParse. |
| 1643 |
* |
| 1644 |
* Implementation of "start" vmethod in #GstBaseParse class. |
| 1645 |
* |
| 1646 |
* Returns: TRUE if startup succeeded. |
| 1647 |
*/ |
| 1648 |
static gboolean |
| 1649 |
gst_aac_parse_start (GstBaseParse * parse) |
| 1650 |
{ |
| 1651 |
GstAacParse *aacparse; |
| 1652 |
|
| 1653 |
aacparse = GST_AAC_PARSE (parse); |
| 1654 |
GST_DEBUG ("start"); |
| 1655 |
aacparse->frame_samples = 1024; |
| 1656 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE); |
| 1657 |
aacparse->sent_codec_tag = FALSE; |
| 1658 |
aacparse->last_parsed_channels = 0; |
| 1659 |
aacparse->last_parsed_sample_rate = 0; |
| 1660 |
aacparse->object_type = 0; |
| 1661 |
aacparse->bitrate = 0; |
| 1662 |
aacparse->header_type = DSPAAC_HEADER_NOT_PARSED; |
| 1663 |
aacparse->output_header_type = DSPAAC_HEADER_NOT_PARSED; |
| 1664 |
aacparse->channels = 0; |
| 1665 |
aacparse->sample_rate = 0; |
| 1666 |
return TRUE; |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
/** |
| 1671 |
* gst_aac_parse_stop: |
| 1672 |
* @parse: #GstBaseParse. |
| 1673 |
* |
| 1674 |
* Implementation of "stop" vmethod in #GstBaseParse class. |
| 1675 |
* |
| 1676 |
* Returns: TRUE is stopping succeeded. |
| 1677 |
*/ |
| 1678 |
static gboolean |
| 1679 |
gst_aac_parse_stop (GstBaseParse * parse) |
| 1680 |
{ |
| 1681 |
GST_DEBUG ("stop"); |
| 1682 |
return TRUE; |
| 1683 |
} |
| 1684 |
|
| 1685 |
static void |
| 1686 |
remove_fields (GstCaps * caps) |
| 1687 |
{ |
| 1688 |
guint i, n; |
| 1689 |
|
| 1690 |
n = gst_caps_get_size (caps); |
| 1691 |
for (i = 0; i < n; i++) { |
| 1692 |
GstStructure *s = gst_caps_get_structure (caps, i); |
| 1693 |
|
| 1694 |
gst_structure_remove_field (s, "framed"); |
| 1695 |
} |
| 1696 |
} |
| 1697 |
|
| 1698 |
static void |
| 1699 |
add_conversion_fields (GstCaps * caps) |
| 1700 |
{ |
| 1701 |
guint i, n; |
| 1702 |
|
| 1703 |
n = gst_caps_get_size (caps); |
| 1704 |
for (i = 0; i < n; i++) { |
| 1705 |
GstStructure *s = gst_caps_get_structure (caps, i); |
| 1706 |
|
| 1707 |
if (gst_structure_has_field (s, "stream-format")) { |
| 1708 |
const GValue *v = gst_structure_get_value (s, "stream-format"); |
| 1709 |
|
| 1710 |
if (G_VALUE_HOLDS_STRING (v)) { |
| 1711 |
const gchar *str = g_value_get_string (v); |
| 1712 |
|
| 1713 |
if (strcmp (str, "adts") == 0 || strcmp (str, "raw") == 0) { |
| 1714 |
GValue va = G_VALUE_INIT; |
| 1715 |
GValue vs = G_VALUE_INIT; |
| 1716 |
|
| 1717 |
g_value_init (&va, GST_TYPE_LIST); |
| 1718 |
g_value_init (&vs, G_TYPE_STRING); |
| 1719 |
g_value_set_string (&vs, "adts"); |
| 1720 |
gst_value_list_append_value (&va, &vs); |
| 1721 |
g_value_set_string (&vs, "raw"); |
| 1722 |
gst_value_list_append_value (&va, &vs); |
| 1723 |
gst_structure_set_value (s, "stream-format", &va); |
| 1724 |
g_value_unset (&va); |
| 1725 |
g_value_unset (&vs); |
| 1726 |
} |
| 1727 |
} else if (GST_VALUE_HOLDS_LIST (v)) { |
| 1728 |
gboolean contains_raw = FALSE; |
| 1729 |
gboolean contains_adts = FALSE; |
| 1730 |
guint m = gst_value_list_get_size (v), j; |
| 1731 |
|
| 1732 |
for (j = 0; j < m; j++) { |
| 1733 |
const GValue *ve = gst_value_list_get_value (v, j); |
| 1734 |
const gchar *str; |
| 1735 |
|
| 1736 |
if (G_VALUE_HOLDS_STRING (ve) && (str = g_value_get_string (ve))) { |
| 1737 |
if (strcmp (str, "adts") == 0) |
| 1738 |
contains_adts = TRUE; |
| 1739 |
else if (strcmp (str, "raw") == 0) |
| 1740 |
contains_raw = TRUE; |
| 1741 |
} |
| 1742 |
} |
| 1743 |
|
| 1744 |
if (contains_adts || contains_raw) { |
| 1745 |
GValue va = G_VALUE_INIT; |
| 1746 |
GValue vs = G_VALUE_INIT; |
| 1747 |
|
| 1748 |
g_value_init (&va, GST_TYPE_LIST); |
| 1749 |
g_value_init (&vs, G_TYPE_STRING); |
| 1750 |
g_value_copy (v, &va); |
| 1751 |
|
| 1752 |
if (!contains_raw) { |
| 1753 |
g_value_set_string (&vs, "raw"); |
| 1754 |
gst_value_list_append_value (&va, &vs); |
| 1755 |
} |
| 1756 |
if (!contains_adts) { |
| 1757 |
g_value_set_string (&vs, "adts"); |
| 1758 |
gst_value_list_append_value (&va, &vs); |
| 1759 |
} |
| 1760 |
|
| 1761 |
gst_structure_set_value (s, "stream-format", &va); |
| 1762 |
|
| 1763 |
g_value_unset (&vs); |
| 1764 |
g_value_unset (&va); |
| 1765 |
} |
| 1766 |
} |
| 1767 |
} |
| 1768 |
} |
| 1769 |
} |
| 1770 |
|
| 1771 |
static GstCaps * |
| 1772 |
gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter) |
| 1773 |
{ |
| 1774 |
GstCaps *peercaps, *templ; |
| 1775 |
GstCaps *res; |
| 1776 |
|
| 1777 |
templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse)); |
| 1778 |
|
| 1779 |
if (filter) { |
| 1780 |
GstCaps *fcopy = gst_caps_copy (filter); |
| 1781 |
/* Remove the fields we convert */ |
| 1782 |
remove_fields (fcopy); |
| 1783 |
add_conversion_fields (fcopy); |
| 1784 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy); |
| 1785 |
gst_caps_unref (fcopy); |
| 1786 |
} else |
| 1787 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL); |
| 1788 |
|
| 1789 |
if (peercaps) { |
| 1790 |
peercaps = gst_caps_make_writable (peercaps); |
| 1791 |
/* Remove the fields we convert */ |
| 1792 |
remove_fields (peercaps); |
| 1793 |
add_conversion_fields (peercaps); |
| 1794 |
|
| 1795 |
res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST); |
| 1796 |
gst_caps_unref (peercaps); |
| 1797 |
gst_caps_unref (templ); |
| 1798 |
} else { |
| 1799 |
res = templ; |
| 1800 |
} |
| 1801 |
|
| 1802 |
if (filter) { |
| 1803 |
GstCaps *intersection; |
| 1804 |
|
| 1805 |
intersection = |
| 1806 |
gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST); |
| 1807 |
gst_caps_unref (res); |
| 1808 |
res = intersection; |
| 1809 |
} |
| 1810 |
|
| 1811 |
return res; |
| 1812 |
} |
| 1813 |
|
| 1814 |
static gboolean |
| 1815 |
gst_aac_parse_src_event (GstBaseParse * parse, GstEvent * event) |
| 1816 |
{ |
| 1817 |
GstAacParse *aacparse = GST_AAC_PARSE (parse); |
| 1818 |
|
| 1819 |
if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) { |
| 1820 |
aacparse->last_parsed_channels = 0; |
| 1821 |
aacparse->last_parsed_sample_rate = 0; |
| 1822 |
} |
| 1823 |
|
| 1824 |
return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event); |
| 1825 |
} |