Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / audioparsers / gstaacparse.c
1 /* GStreamer AAC parser plugin
2  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3  *
4  * Contact: Stefan Kost <stefan.kost@nokia.com>
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-aacparse
24  * @short_description: AAC parser
25  * @see_also: #GstAmrParse
26  *
27  * This is an AAC parser which handles both ADIF and ADTS stream formats.
28  *
29  * As ADIF format is not framed, it is not seekable and stream duration cannot
30  * be determined either. However, ADTS format AAC clips can be seeked, and parser
31  * can also estimate playback position and clip duration.
32  *
33  * <refsect2>
34  * <title>Example launch line</title>
35  * |[
36  * gst-launch filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink
37  * ]|
38  * </refsect2>
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46
47 #include "gstaacparse.h"
48
49
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("audio/mpeg, "
54         "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, "
55         "stream-format = (string) { raw, adts, adif };"));
56
57 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("audio/mpeg, "
61         "framed = (boolean) false, " "mpegversion = (int) { 2, 4 };"));
62
63 GST_DEBUG_CATEGORY_STATIC (aacparse_debug);
64 #define GST_CAT_DEFAULT aacparse_debug
65
66
67 #define ADIF_MAX_SIZE 40        /* Should be enough */
68 #define ADTS_MAX_SIZE 10        /* Should be enough */
69
70
71 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
72
73 gboolean gst_aac_parse_start (GstBaseParse * parse);
74 gboolean gst_aac_parse_stop (GstBaseParse * parse);
75
76 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
77     GstCaps * caps);
78
79 gboolean gst_aac_parse_check_valid_frame (GstBaseParse * parse,
80     GstBaseParseFrame * frame, guint * size, gint * skipsize);
81
82 GstFlowReturn gst_aac_parse_parse_frame (GstBaseParse * parse,
83     GstBaseParseFrame * frame);
84
85 gboolean gst_aac_parse_convert (GstBaseParse * parse,
86     GstFormat src_format,
87     gint64 src_value, GstFormat dest_format, gint64 * dest_value);
88
89 gint gst_aac_parse_get_frame_overhead (GstBaseParse * parse,
90     GstBuffer * buffer);
91
92 gboolean gst_aac_parse_event (GstBaseParse * parse, GstEvent * event);
93
94 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
95
96 static inline gint
97 gst_aac_parse_get_sample_rate_from_index (guint sr_idx)
98 {
99   static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000, 44100,
100     32000, 24000, 22050, 16000, 12000, 11025, 8000
101   };
102
103   if (sr_idx < G_N_ELEMENTS (aac_sample_rates))
104     return aac_sample_rates[sr_idx];
105   GST_WARNING ("Invalid sample rate index %u", sr_idx);
106   return 0;
107 }
108
109 /**
110  * gst_aac_parse_class_init:
111  * @klass: #GstAacParseClass.
112  *
113  */
114 static void
115 gst_aac_parse_class_init (GstAacParseClass * klass)
116 {
117   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
118   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
119
120   GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
121       "AAC audio stream parser");
122
123   gst_element_class_add_pad_template (element_class,
124       gst_static_pad_template_get (&sink_template));
125   gst_element_class_add_pad_template (element_class,
126       gst_static_pad_template_get (&src_template));
127
128   gst_element_class_set_details_simple (element_class,
129       "AAC audio stream parser", "Codec/Parser/Audio",
130       "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
131
132   parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
133   parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
134   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
135   parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_parse_frame);
136   parse_class->check_valid_frame =
137       GST_DEBUG_FUNCPTR (gst_aac_parse_check_valid_frame);
138 }
139
140
141 /**
142  * gst_aac_parse_init:
143  * @aacparse: #GstAacParse.
144  * @klass: #GstAacParseClass.
145  *
146  */
147 static void
148 gst_aac_parse_init (GstAacParse * aacparse)
149 {
150   GST_DEBUG ("initialized");
151 }
152
153
154 /**
155  * gst_aac_parse_set_src_caps:
156  * @aacparse: #GstAacParse.
157  * @sink_caps: (proposed) caps of sink pad
158  *
159  * Set source pad caps according to current knowledge about the
160  * audio stream.
161  *
162  * Returns: TRUE if caps were successfully set.
163  */
164 static gboolean
165 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
166 {
167   GstStructure *s;
168   GstCaps *src_caps = NULL;
169   gboolean res = FALSE;
170   const gchar *stream_format;
171
172   GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
173   if (sink_caps)
174     src_caps = gst_caps_copy (sink_caps);
175   else
176     src_caps = gst_caps_new_simple ("audio/mpeg", NULL);
177
178   gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
179       "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
180
181   switch (aacparse->header_type) {
182     case DSPAAC_HEADER_NONE:
183       stream_format = "raw";
184       break;
185     case DSPAAC_HEADER_ADTS:
186       stream_format = "adts";
187       break;
188     case DSPAAC_HEADER_ADIF:
189       stream_format = "adif";
190       break;
191     default:
192       stream_format = NULL;
193   }
194
195   s = gst_caps_get_structure (src_caps, 0);
196   if (aacparse->sample_rate > 0)
197     gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
198   if (aacparse->channels > 0)
199     gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
200   if (stream_format)
201     gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
202
203   GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
204
205   res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
206   gst_caps_unref (src_caps);
207   return res;
208 }
209
210
211 /**
212  * gst_aac_parse_sink_setcaps:
213  * @sinkpad: GstPad
214  * @caps: GstCaps
215  *
216  * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
217  *
218  * Returns: TRUE on success.
219  */
220 static gboolean
221 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
222 {
223   GstAacParse *aacparse;
224   GstStructure *structure;
225   gchar *caps_str;
226   const GValue *value;
227
228   aacparse = GST_AAC_PARSE (parse);
229   structure = gst_caps_get_structure (caps, 0);
230   caps_str = gst_caps_to_string (caps);
231
232   GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
233   g_free (caps_str);
234
235   /* This is needed at least in case of RTP
236    * Parses the codec_data information to get ObjectType,
237    * number of channels and samplerate */
238   value = gst_structure_get_value (structure, "codec_data");
239   if (value) {
240     GstBuffer *buf = gst_value_get_buffer (value);
241
242     if (buf) {
243       guint8 *data;
244       gsize size;
245       guint sr_idx;
246
247       data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
248
249       sr_idx = ((data[0] & 0x07) << 1) | ((data[1] & 0x80) >> 7);
250       aacparse->object_type = (data[0] & 0xf8) >> 3;
251       aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
252       aacparse->channels = (data[1] & 0x78) >> 3;
253       aacparse->header_type = DSPAAC_HEADER_NONE;
254       aacparse->mpegversion = 4;
255       gst_buffer_unmap (buf, data, size);
256
257       GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d",
258           aacparse->object_type, aacparse->sample_rate, aacparse->channels);
259
260       /* arrange for metadata and get out of the way */
261       gst_aac_parse_set_src_caps (aacparse, caps);
262       gst_base_parse_set_passthrough (parse, TRUE);
263     } else
264       return FALSE;
265
266     /* caps info overrides */
267     gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
268     gst_structure_get_int (structure, "channels", &aacparse->channels);
269   } else {
270     gst_base_parse_set_passthrough (parse, FALSE);
271   }
272
273   return TRUE;
274 }
275
276
277 /**
278  * gst_aac_parse_adts_get_frame_len:
279  * @data: block of data containing an ADTS header.
280  *
281  * This function calculates ADTS frame length from the given header.
282  *
283  * Returns: size of the ADTS frame.
284  */
285 static inline guint
286 gst_aac_parse_adts_get_frame_len (const guint8 * data)
287 {
288   return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
289 }
290
291
292 /**
293  * gst_aac_parse_check_adts_frame:
294  * @aacparse: #GstAacParse.
295  * @data: Data to be checked.
296  * @avail: Amount of data passed.
297  * @framesize: If valid ADTS frame was found, this will be set to tell the
298  *             found frame size in bytes.
299  * @needed_data: If frame was not found, this may be set to tell how much
300  *               more data is needed in the next round to detect the frame
301  *               reliably. This may happen when a frame header candidate
302  *               is found but it cannot be guaranteed to be the header without
303  *               peeking the following data.
304  *
305  * Check if the given data contains contains ADTS frame. The algorithm
306  * will examine ADTS frame header and calculate the frame size. Also, another
307  * consecutive ADTS frame header need to be present after the found frame.
308  * Otherwise the data is not considered as a valid ADTS frame. However, this
309  * "extra check" is omitted when EOS has been received. In this case it is
310  * enough when data[0] contains a valid ADTS header.
311  *
312  * This function may set the #needed_data to indicate that a possible frame
313  * candidate has been found, but more data (#needed_data bytes) is needed to
314  * be absolutely sure. When this situation occurs, FALSE will be returned.
315  *
316  * When a valid frame is detected, this function will use
317  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
318  * to set the needed bytes for next frame.This way next data chunk is already
319  * of correct size.
320  *
321  * Returns: TRUE if the given data contains a valid ADTS header.
322  */
323 static gboolean
324 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
325     const guint8 * data, const guint avail, gboolean drain,
326     guint * framesize, guint * needed_data)
327 {
328   if (G_UNLIKELY (avail < 2))
329     return FALSE;
330
331   if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
332     *framesize = gst_aac_parse_adts_get_frame_len (data);
333
334     /* In EOS mode this is enough. No need to examine the data further.
335        We also relax the check when we have sync, on the assumption that
336        if we're not looking at random data, we have a much higher chance
337        to get the correct sync, and this avoids losing two frames when
338        a single bit corruption happens. */
339     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
340       return TRUE;
341     }
342
343     if (*framesize + ADTS_MAX_SIZE > avail) {
344       /* We have found a possible frame header candidate, but can't be
345          sure since we don't have enough data to check the next frame */
346       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
347           *framesize + ADTS_MAX_SIZE, avail);
348       *needed_data = *framesize + ADTS_MAX_SIZE;
349       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
350           *framesize + ADTS_MAX_SIZE);
351       return FALSE;
352     }
353
354     if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
355       guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
356
357       GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
358       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
359           nextlen + ADTS_MAX_SIZE);
360       return TRUE;
361     }
362   }
363   return FALSE;
364 }
365
366 /* caller ensure sufficient data */
367 static inline void
368 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
369     gint * rate, gint * channels, gint * object, gint * version)
370 {
371
372   if (rate) {
373     gint sr_idx = (data[2] & 0x3c) >> 2;
374
375     *rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
376   }
377   if (channels)
378     *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
379
380   if (version)
381     *version = (data[1] & 0x08) ? 2 : 4;
382   if (object)
383     *object = (data[2] & 0xc0) >> 6;
384 }
385
386 /**
387  * gst_aac_parse_detect_stream:
388  * @aacparse: #GstAacParse.
389  * @data: A block of data that needs to be examined for stream characteristics.
390  * @avail: Size of the given datablock.
391  * @framesize: If valid stream was found, this will be set to tell the
392  *             first frame size in bytes.
393  * @skipsize: If valid stream was found, this will be set to tell the first
394  *            audio frame position within the given data.
395  *
396  * Examines the given piece of data and try to detect the format of it. It
397  * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
398  * header. If the stream is detected, TRUE will be returned and #framesize
399  * is set to indicate the found frame size. Additionally, #skipsize might
400  * be set to indicate the number of bytes that need to be skipped, a.k.a. the
401  * position of the frame inside given data chunk.
402  *
403  * Returns: TRUE on success.
404  */
405 static gboolean
406 gst_aac_parse_detect_stream (GstAacParse * aacparse,
407     const guint8 * data, const guint avail, gboolean drain,
408     guint * framesize, gint * skipsize)
409 {
410   gboolean found = FALSE;
411   guint need_data = 0;
412   guint i = 0;
413
414   GST_DEBUG_OBJECT (aacparse, "Parsing header data");
415
416   /* FIXME: No need to check for ADIF if we are not in the beginning of the
417      stream */
418
419   /* Can we even parse the header? */
420   if (avail < ADTS_MAX_SIZE)
421     return FALSE;
422
423   for (i = 0; i < avail - 4; i++) {
424     if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
425         strncmp ((char *) data + i, "ADIF", 4) == 0) {
426       found = TRUE;
427
428       if (i) {
429         /* Trick: tell the parent class that we didn't find the frame yet,
430            but make it skip 'i' amount of bytes. Next time we arrive
431            here we have full frame in the beginning of the data. */
432         *skipsize = i;
433         return FALSE;
434       }
435       break;
436     }
437   }
438   if (!found) {
439     if (i)
440       *skipsize = i;
441     return FALSE;
442   }
443
444   if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
445           framesize, &need_data)) {
446     gint rate, channels;
447
448     GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
449
450     aacparse->header_type = DSPAAC_HEADER_ADTS;
451     gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
452         &aacparse->object_type, &aacparse->mpegversion);
453
454     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate, 1024, 2, 2);
455
456     GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
457         rate, channels, aacparse->object_type, aacparse->mpegversion);
458
459     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
460
461     return TRUE;
462   } else if (need_data) {
463     /* This tells the parent class not to skip any data */
464     *skipsize = 0;
465     return FALSE;
466   }
467
468   if (avail < ADIF_MAX_SIZE)
469     return FALSE;
470
471   if (memcmp (data + i, "ADIF", 4) == 0) {
472     const guint8 *adif;
473     int skip_size = 0;
474     int bitstream_type;
475     int sr_idx;
476     GstCaps *sinkcaps;
477
478     aacparse->header_type = DSPAAC_HEADER_ADIF;
479     aacparse->mpegversion = 4;
480
481     /* Skip the "ADIF" bytes */
482     adif = data + i + 4;
483
484     /* copyright string */
485     if (adif[0] & 0x80)
486       skip_size += 9;           /* skip 9 bytes */
487
488     bitstream_type = adif[0 + skip_size] & 0x10;
489     aacparse->bitrate =
490         ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
491         ((unsigned int) adif[1 + skip_size] << 11) |
492         ((unsigned int) adif[2 + skip_size] << 3) |
493         ((unsigned int) adif[3 + skip_size] & 0xe0);
494
495     /* CBR */
496     if (bitstream_type == 0) {
497 #if 0
498       /* Buffer fullness parsing. Currently not needed... */
499       guint num_elems = 0;
500       guint fullness = 0;
501
502       num_elems = (adif[3 + skip_size] & 0x1e);
503       GST_INFO ("ADIF num_config_elems: %d", num_elems);
504
505       fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
506           ((unsigned int) adif[4 + skip_size] << 11) |
507           ((unsigned int) adif[5 + skip_size] << 3) |
508           ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
509
510       GST_INFO ("ADIF buffer fullness: %d", fullness);
511 #endif
512       aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
513           ((adif[7 + skip_size] & 0x80) >> 7);
514       sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
515     }
516     /* VBR */
517     else {
518       aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
519       sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
520           ((adif[5 + skip_size] & 0x80) >> 7);
521     }
522
523     /* FIXME: This gives totally wrong results. Duration calculation cannot
524        be based on this */
525     aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
526
527     /* baseparse is not given any fps,
528      * so it will give up on timestamps, seeking, etc */
529
530     /* FIXME: Can we assume this? */
531     aacparse->channels = 2;
532
533     GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
534         aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
535
536     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
537
538     /* arrange for metadata and get out of the way */
539     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
540     gst_aac_parse_set_src_caps (aacparse, sinkcaps);
541     gst_caps_unref (sinkcaps);
542
543     /* not syncable, not easily seekable (unless we push data from start */
544     gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
545     gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
546     gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
547
548     *framesize = avail;
549     return TRUE;
550   }
551
552   /* This should never happen */
553   return FALSE;
554 }
555
556
557 /**
558  * gst_aac_parse_check_valid_frame:
559  * @parse: #GstBaseParse.
560  * @buffer: #GstBuffer.
561  * @framesize: If the buffer contains a valid frame, its size will be put here
562  * @skipsize: How much data parent class should skip in order to find the
563  *            frame header.
564  *
565  * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
566  *
567  * Returns: TRUE if buffer contains a valid frame.
568  */
569 gboolean
570 gst_aac_parse_check_valid_frame (GstBaseParse * parse,
571     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
572 {
573   guint8 *data;
574   gsize size;
575   GstAacParse *aacparse;
576   gboolean ret = FALSE;
577   gboolean lost_sync;
578   GstBuffer *buffer;
579
580   aacparse = GST_AAC_PARSE (parse);
581   buffer = frame->buffer;
582
583   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
584
585   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
586
587   if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
588       aacparse->header_type == DSPAAC_HEADER_NONE) {
589     /* There is nothing to parse */
590     *framesize = size;
591     ret = TRUE;
592
593   } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
594
595     ret = gst_aac_parse_detect_stream (aacparse, data, size,
596         GST_BASE_PARSE_DRAINING (parse), framesize, skipsize);
597
598   } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
599     guint needed_data = 1024;
600
601     ret = gst_aac_parse_check_adts_frame (aacparse, data, size,
602         GST_BASE_PARSE_DRAINING (parse), framesize, &needed_data);
603
604     if (!ret) {
605       GST_DEBUG ("buffer didn't contain valid frame");
606       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
607           needed_data);
608     }
609
610   } else {
611     GST_DEBUG ("buffer didn't contain valid frame");
612     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
613         ADTS_MAX_SIZE);
614   }
615   gst_buffer_unmap (buffer, data, size);
616
617   return ret;
618 }
619
620
621 /**
622  * gst_aac_parse_parse_frame:
623  * @parse: #GstBaseParse.
624  * @buffer: #GstBuffer.
625  *
626  * Implementation of "parse_frame" vmethod in #GstBaseParse class.
627  *
628  * Also determines frame overhead.
629  * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
630  * a per-frame header.
631  *
632  * We're making a couple of simplifying assumptions:
633  *
634  * 1. We count Program Configuration Elements rather than searching for them
635  *    in the streams to discount them - the overhead is negligible.
636  *
637  * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
638  *    bits, which should still not be significant enough to warrant the
639  *    additional parsing through the headers
640  *
641  * Returns: GST_FLOW_OK if frame was successfully parsed and can be pushed
642  *          forward. Otherwise appropriate error is returned.
643  */
644 GstFlowReturn
645 gst_aac_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
646 {
647   GstAacParse *aacparse;
648   GstBuffer *buffer;
649   GstFlowReturn ret = GST_FLOW_OK;
650   gint rate, channels;
651   guint8 *data;
652   gsize size;
653
654   aacparse = GST_AAC_PARSE (parse);
655   buffer = frame->buffer;
656
657   if (G_UNLIKELY (aacparse->header_type != DSPAAC_HEADER_ADTS))
658     return ret;
659
660   /* see above */
661   frame->overhead = 7;
662
663   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
664   gst_aac_parse_parse_adts_header (aacparse, data,
665       &rate, &channels, NULL, NULL);
666   gst_buffer_unmap (buffer, data, size);
667
668   GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
669
670   if (G_UNLIKELY (rate != aacparse->sample_rate
671           || channels != aacparse->channels)) {
672     GstCaps *sinkcaps;
673
674     aacparse->sample_rate = rate;
675     aacparse->channels = channels;
676
677     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE (aacparse)->sinkpad);
678     if (!gst_aac_parse_set_src_caps (aacparse, sinkcaps)) {
679       /* If linking fails, we need to return appropriate error */
680       gst_caps_unref (sinkcaps);
681       ret = GST_FLOW_NOT_LINKED;
682     }
683     gst_caps_unref (sinkcaps);
684
685     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
686         aacparse->sample_rate, 1024, 2, 2);
687   }
688
689   return ret;
690 }
691
692
693 /**
694  * gst_aac_parse_start:
695  * @parse: #GstBaseParse.
696  *
697  * Implementation of "start" vmethod in #GstBaseParse class.
698  *
699  * Returns: TRUE if startup succeeded.
700  */
701 gboolean
702 gst_aac_parse_start (GstBaseParse * parse)
703 {
704   GstAacParse *aacparse;
705
706   aacparse = GST_AAC_PARSE (parse);
707   GST_DEBUG ("start");
708   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
709   return TRUE;
710 }
711
712
713 /**
714  * gst_aac_parse_stop:
715  * @parse: #GstBaseParse.
716  *
717  * Implementation of "stop" vmethod in #GstBaseParse class.
718  *
719  * Returns: TRUE is stopping succeeded.
720  */
721 gboolean
722 gst_aac_parse_stop (GstBaseParse * parse)
723 {
724   GST_DEBUG ("stop");
725   return TRUE;
726 }