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