• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer DCA parser
2  * Copyright (C) 2010 Tim-Philipp Müller <tim centricular net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION:element-dcaparse
22  * @title: dcaparse
23  * @short_description: DCA (DTS Coherent Acoustics) parser
24  * @see_also: #GstAmrParse, #GstAACParse, #GstAc3Parse
25  *
26  * This is a DCA (DTS Coherent Acoustics) parser.
27  *
28  * ## Example launch line
29  * |[
30  * gst-launch-1.0 filesrc location=abc.dts ! dcaparse ! dtsdec ! audioresample ! audioconvert ! autoaudiosink
31  * ]|
32  *
33  */
34 
35 /* TODO:
36  *  - should accept framed and unframed input (needs decodebin fixes first)
37  *  - seeking in raw .dts files doesn't seem to work, but duration estimate ok
38  *
39  *  - if frames have 'odd' durations, the frame durations (plus timestamps)
40  *    aren't adjusted up occasionally to make up for rounding error gaps.
41  *    (e.g. if 512 samples per frame @ 48kHz = 10.666666667 ms/frame)
42  */
43 
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 
48 #include <string.h>
49 
50 #include "gstaudioparserselements.h"
51 #include "gstdcaparse.h"
52 #include <gst/base/base.h>
53 #include <gst/pbutils/pbutils.h>
54 
55 GST_DEBUG_CATEGORY_STATIC (dca_parse_debug);
56 #define GST_CAT_DEFAULT dca_parse_debug
57 
58 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("audio/x-dts,"
62         " framed = (boolean) true,"
63         " channels = (int) [ 1, 8 ],"
64         " rate = (int) [ 8000, 192000 ],"
65         " depth = (int) { 14, 16 },"
66         " endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
67         " block-size = (int) [ 1, MAX], " " frame-size = (int) [ 1, MAX]"));
68 
69 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS ("audio/x-dts; " "audio/x-private1-dts"));
73 
74 static void gst_dca_parse_finalize (GObject * object);
75 
76 static gboolean gst_dca_parse_start (GstBaseParse * parse);
77 static gboolean gst_dca_parse_stop (GstBaseParse * parse);
78 static GstFlowReturn gst_dca_parse_handle_frame (GstBaseParse * parse,
79     GstBaseParseFrame * frame, gint * skipsize);
80 static GstFlowReturn gst_dca_parse_pre_push_frame (GstBaseParse * parse,
81     GstBaseParseFrame * frame);
82 static GstCaps *gst_dca_parse_get_sink_caps (GstBaseParse * parse,
83     GstCaps * filter);
84 static gboolean gst_dca_parse_set_sink_caps (GstBaseParse * parse,
85     GstCaps * caps);
86 
87 #define gst_dca_parse_parent_class parent_class
88 G_DEFINE_TYPE (GstDcaParse, gst_dca_parse, GST_TYPE_BASE_PARSE);
89 GST_ELEMENT_REGISTER_DEFINE (dcaparse, "dcaparse",
90     GST_RANK_PRIMARY + 1, GST_TYPE_DCA_PARSE);
91 
92 static void
gst_dca_parse_class_init(GstDcaParseClass * klass)93 gst_dca_parse_class_init (GstDcaParseClass * klass)
94 {
95   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
96   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97   GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 
99   GST_DEBUG_CATEGORY_INIT (dca_parse_debug, "dcaparse", 0,
100       "DCA audio stream parser");
101 
102   object_class->finalize = gst_dca_parse_finalize;
103 
104   parse_class->start = GST_DEBUG_FUNCPTR (gst_dca_parse_start);
105   parse_class->stop = GST_DEBUG_FUNCPTR (gst_dca_parse_stop);
106   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_dca_parse_handle_frame);
107   parse_class->pre_push_frame =
108       GST_DEBUG_FUNCPTR (gst_dca_parse_pre_push_frame);
109   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_dca_parse_get_sink_caps);
110   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_dca_parse_set_sink_caps);
111 
112   gst_element_class_add_static_pad_template (element_class, &sink_template);
113   gst_element_class_add_static_pad_template (element_class, &src_template);
114 
115   gst_element_class_set_static_metadata (element_class,
116       "DTS Coherent Acoustics audio stream parser", "Codec/Parser/Audio",
117       "DCA parser", "Tim-Philipp Müller <tim centricular net>");
118 }
119 
120 static void
gst_dca_parse_reset(GstDcaParse * dcaparse)121 gst_dca_parse_reset (GstDcaParse * dcaparse)
122 {
123   dcaparse->channels = -1;
124   dcaparse->rate = -1;
125   dcaparse->depth = -1;
126   dcaparse->endianness = -1;
127   dcaparse->block_size = -1;
128   dcaparse->frame_size = -1;
129   dcaparse->last_sync = 0;
130   dcaparse->sent_codec_tag = FALSE;
131 }
132 
133 static void
gst_dca_parse_init(GstDcaParse * dcaparse)134 gst_dca_parse_init (GstDcaParse * dcaparse)
135 {
136   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (dcaparse),
137       DCA_MIN_FRAMESIZE);
138   gst_dca_parse_reset (dcaparse);
139   dcaparse->baseparse_chainfunc =
140       GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE (dcaparse))->chainfunc;
141 
142   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (dcaparse));
143   GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (dcaparse));
144 }
145 
146 static void
gst_dca_parse_finalize(GObject * object)147 gst_dca_parse_finalize (GObject * object)
148 {
149   G_OBJECT_CLASS (parent_class)->finalize (object);
150 }
151 
152 static gboolean
gst_dca_parse_start(GstBaseParse * parse)153 gst_dca_parse_start (GstBaseParse * parse)
154 {
155   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
156 
157   GST_DEBUG_OBJECT (parse, "starting");
158 
159   gst_dca_parse_reset (dcaparse);
160 
161   return TRUE;
162 }
163 
164 static gboolean
gst_dca_parse_stop(GstBaseParse * parse)165 gst_dca_parse_stop (GstBaseParse * parse)
166 {
167   GST_DEBUG_OBJECT (parse, "stopping");
168 
169   return TRUE;
170 }
171 
172 static gboolean
gst_dca_parse_parse_header(GstDcaParse * dcaparse,const GstByteReader * reader,guint * frame_size,guint * sample_rate,guint * channels,guint * depth,gint * endianness,guint * num_blocks,guint * samples_per_block,gboolean * terminator)173 gst_dca_parse_parse_header (GstDcaParse * dcaparse,
174     const GstByteReader * reader, guint * frame_size,
175     guint * sample_rate, guint * channels, guint * depth,
176     gint * endianness, guint * num_blocks, guint * samples_per_block,
177     gboolean * terminator)
178 {
179   static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
180     22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
181   };
182   static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
183     6, 6, 6, 7, 8, 8
184   };
185   GstByteReader r = *reader;
186   guint16 hdr[8];
187   guint32 marker;
188   guint chans, lfe, i;
189 
190   if (gst_byte_reader_get_remaining (&r) < (4 + sizeof (hdr)))
191     return FALSE;
192 
193   marker = gst_byte_reader_peek_uint32_be_unchecked (&r);
194 
195   /* raw big endian or 14-bit big endian */
196   if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
197     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
198       hdr[i] = gst_byte_reader_get_uint16_be_unchecked (&r);
199   } else
200     /* raw little endian or 14-bit little endian */
201   if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
202     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
203       hdr[i] = gst_byte_reader_get_uint16_le_unchecked (&r);
204   } else {
205     return FALSE;
206   }
207 
208   GST_LOG_OBJECT (dcaparse, "dts sync marker 0x%08x at offset %u", marker,
209       gst_byte_reader_get_pos (reader));
210 
211   /* 14-bit mode */
212   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
213     if ((hdr[2] & 0xFFF0) != 0x07F0)
214       return FALSE;
215     /* discard top 2 bits (2 void), shift in 2 */
216     hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
217     /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
218     hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
219     hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
220     hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
221     hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
222     hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
223     hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
224     g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
225   }
226 
227   GST_LOG_OBJECT (dcaparse, "frame header: %04x%04x%04x%04x",
228       hdr[2], hdr[3], hdr[4], hdr[5]);
229 
230   *terminator = (hdr[2] & 0x80) ? FALSE : TRUE;
231   *samples_per_block = ((hdr[2] >> 10) & 0x1f) + 1;
232   *num_blocks = ((hdr[2] >> 2) & 0x7F) + 1;
233   *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
234   chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
235   *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
236   lfe = (hdr[5] >> 9) & 0x03;
237 
238   GST_TRACE_OBJECT (dcaparse, "frame size %u, num_blocks %u, rate %u, "
239       "samples per block %u", *frame_size, *num_blocks, *sample_rate,
240       *samples_per_block);
241 
242   if (*num_blocks < 6 || *frame_size < 96 || *sample_rate == 0)
243     return FALSE;
244 
245   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
246     *frame_size = (*frame_size * 16) / 14;      /* FIXME: round up? */
247 
248   if (chans < G_N_ELEMENTS (channels_table))
249     *channels = channels_table[chans] + ((lfe) ? 1 : 0);
250   else
251     return FALSE;
252 
253   if (depth)
254     *depth = (marker == 0x1FFFE800 || marker == 0xFF1F00E8) ? 14 : 16;
255   if (endianness)
256     *endianness = (marker == 0xFE7F0180 || marker == 0xFF1F00E8) ?
257         G_LITTLE_ENDIAN : G_BIG_ENDIAN;
258 
259   GST_TRACE_OBJECT (dcaparse, "frame size %u, channels %u, rate %u, "
260       "num_blocks %u, samples_per_block %u", *frame_size, *channels,
261       *sample_rate, *num_blocks, *samples_per_block);
262 
263   return TRUE;
264 }
265 
266 static gint
gst_dca_parse_find_sync(GstDcaParse * dcaparse,GstByteReader * reader,gsize bufsize,guint32 * sync)267 gst_dca_parse_find_sync (GstDcaParse * dcaparse, GstByteReader * reader,
268     gsize bufsize, guint32 * sync)
269 {
270   guint32 best_sync = 0;
271   guint best_offset = G_MAXUINT;
272   gint off;
273 
274   /* FIXME: verify syncs via _parse_header() here already */
275 
276   /* Raw little endian */
277   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xfe7f0180,
278       0, bufsize);
279   if (off >= 0 && off < best_offset) {
280     best_offset = off;
281     best_sync = 0xfe7f0180;
282   }
283 
284   /* Raw big endian */
285   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x7ffe8001,
286       0, bufsize);
287   if (off >= 0 && off < best_offset) {
288     best_offset = off;
289     best_sync = 0x7ffe8001;
290   }
291 
292   /* FIXME: check next 2 bytes as well for 14-bit formats (but then don't
293    * forget to adjust the *skipsize= in _check_valid_frame() */
294 
295   /* 14-bit little endian  */
296   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xff1f00e8,
297       0, bufsize);
298   if (off >= 0 && off < best_offset) {
299     best_offset = off;
300     best_sync = 0xff1f00e8;
301   }
302 
303   /* 14-bit big endian  */
304   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x1fffe800,
305       0, bufsize);
306   if (off >= 0 && off < best_offset) {
307     best_offset = off;
308     best_sync = 0x1fffe800;
309   }
310 
311   if (best_offset == G_MAXUINT)
312     return -1;
313 
314   *sync = best_sync;
315   return best_offset;
316 }
317 
318 static GstFlowReturn
gst_dca_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)319 gst_dca_parse_handle_frame (GstBaseParse * parse,
320     GstBaseParseFrame * frame, gint * skipsize)
321 {
322   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
323   GstBuffer *buf = frame->buffer;
324   GstByteReader r;
325   gboolean parser_in_sync;
326   gboolean terminator;
327   guint32 sync = 0;
328   guint size = 0, rate, chans, num_blocks, samples_per_block, depth;
329   gint block_size;
330   gint endianness;
331   gint off = -1;
332   GstMapInfo map;
333   GstFlowReturn ret = GST_FLOW_EOS;
334   gsize extra_size = 0;
335 
336   gst_buffer_map (buf, &map, GST_MAP_READ);
337 
338   if (G_UNLIKELY (map.size < 16)) {
339     *skipsize = 1;
340     goto cleanup;
341   }
342 
343   parser_in_sync = !GST_BASE_PARSE_LOST_SYNC (parse);
344 
345   gst_byte_reader_init (&r, map.data, map.size);
346 
347   if (G_LIKELY (parser_in_sync && dcaparse->last_sync != 0)) {
348     off = gst_byte_reader_masked_scan_uint32 (&r, 0xffffffff,
349         dcaparse->last_sync, 0, map.size);
350   }
351 
352   if (G_UNLIKELY (off < 0)) {
353     off = gst_dca_parse_find_sync (dcaparse, &r, map.size, &sync);
354   }
355 
356   /* didn't find anything that looks like a sync word, skip */
357   if (off < 0) {
358     *skipsize = map.size - 3;
359     GST_DEBUG_OBJECT (dcaparse, "no sync, skipping %d bytes", *skipsize);
360     goto cleanup;
361   }
362 
363   GST_LOG_OBJECT (parse, "possible sync %08x at buffer offset %d", sync, off);
364 
365   /* possible frame header, but not at offset 0? skip bytes before sync */
366   if (off > 0) {
367     *skipsize = off;
368     goto cleanup;
369   }
370 
371   /* make sure the values in the frame header look sane */
372   if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, &depth,
373           &endianness, &num_blocks, &samples_per_block, &terminator)) {
374     *skipsize = 4;
375     goto cleanup;
376   }
377 
378   GST_LOG_OBJECT (parse, "got frame, sync %08x, size %u, rate %d, channels %d",
379       sync, size, rate, chans);
380 
381   dcaparse->last_sync = sync;
382 
383   /* FIXME: Don't look for a second syncword, there are streams out there
384    * that consistently contain garbage between every frame so we never ever
385    * find a second consecutive syncword.
386    * See https://bugzilla.gnome.org/show_bug.cgi?id=738237
387    */
388 #if 0
389   parser_draining = GST_BASE_PARSE_DRAINING (parse);
390 
391   if (!parser_in_sync && !parser_draining) {
392     /* check for second frame to be sure */
393     GST_DEBUG_OBJECT (dcaparse, "resyncing; checking next frame syncword");
394     if (map.size >= (size + 16)) {
395       guint s2, r2, c2, n2, s3;
396       gboolean t;
397 
398       GST_MEMDUMP ("buf", map.data, size + 16);
399       gst_byte_reader_init (&r, map.data, map.size);
400       gst_byte_reader_skip_unchecked (&r, size);
401 
402       if (!gst_dca_parse_parse_header (dcaparse, &r, &s2, &r2, &c2, NULL, NULL,
403               &n2, &s3, &t)) {
404         GST_DEBUG_OBJECT (dcaparse, "didn't find second syncword");
405         *skipsize = 4;
406         goto cleanup;
407       }
408 
409       /* ok, got sync now, let's assume constant frame size */
410       gst_base_parse_set_min_frame_size (parse, size);
411     } else {
412       /* wait for some more data */
413       GST_LOG_OBJECT (dcaparse,
414           "next sync out of reach (%" G_GSIZE_FORMAT " < %u)", map.size,
415           size + 16);
416       goto cleanup;
417     }
418   }
419 #endif
420 
421   /* found frame */
422   ret = GST_FLOW_OK;
423 
424   /* metadata handling */
425   block_size = num_blocks * samples_per_block;
426 
427   if (G_UNLIKELY (dcaparse->rate != rate || dcaparse->channels != chans
428           || dcaparse->depth != depth || dcaparse->endianness != endianness
429           || (!terminator && dcaparse->block_size != block_size)
430           || (size != dcaparse->frame_size))) {
431     GstCaps *caps;
432 
433     caps = gst_caps_new_simple ("audio/x-dts",
434         "framed", G_TYPE_BOOLEAN, TRUE,
435         "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
436         "endianness", G_TYPE_INT, endianness, "depth", G_TYPE_INT, depth,
437         "block-size", G_TYPE_INT, block_size, "frame-size", G_TYPE_INT, size,
438         NULL);
439     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
440     gst_caps_unref (caps);
441 
442     dcaparse->rate = rate;
443     dcaparse->channels = chans;
444     dcaparse->depth = depth;
445     dcaparse->endianness = endianness;
446     dcaparse->block_size = block_size;
447     dcaparse->frame_size = size;
448 
449     gst_base_parse_set_frame_rate (parse, rate, block_size, 0, 0);
450   }
451 
452 cleanup:
453   /* it is possible that DTS HD substream after DTS core */
454   if (parse->flags & GST_BASE_PARSE_FLAG_DRAINING || map.size >= size + 9) {
455     extra_size = 0;
456     if (map.size >= size + 9) {
457       const guint8 *next = map.data + size;
458       /* Check for DTS_SYNCWORD_SUBSTREAM */
459       if (next[0] == 0x64 && next[1] == 0x58 && next[2] == 0x20
460           && next[3] == 0x25) {
461         /* 7.4.1 Extension Substream Header */
462         GstBitReader reader;
463         gst_bit_reader_init (&reader, next + 4, 5);
464         gst_bit_reader_skip (&reader, 8 + 2);   /* skip UserDefinedBits and nExtSSIndex) */
465         if (gst_bit_reader_get_bits_uint8_unchecked (&reader, 1) == 0) {
466           gst_bit_reader_skip (&reader, 8);
467           extra_size =
468               gst_bit_reader_get_bits_uint32_unchecked (&reader, 16) + 1;
469         } else {
470           gst_bit_reader_skip (&reader, 12);
471           extra_size =
472               gst_bit_reader_get_bits_uint32_unchecked (&reader, 20) + 1;
473         }
474       }
475     }
476     gst_buffer_unmap (buf, &map);
477     if (ret == GST_FLOW_OK && size + extra_size <= map.size) {
478       ret = gst_base_parse_finish_frame (parse, frame, size + extra_size);
479     } else {
480       ret = GST_FLOW_OK;
481     }
482   } else {
483     gst_buffer_unmap (buf, &map);
484   }
485 
486   return ret;
487 }
488 
489 /*
490  * MPEG-PS private1 streams add a 2 bytes "Audio Substream Headers" for each
491  * buffer (not each frame) with the offset of the next frame's start.
492  * These 2 bytes can be dropped safely as they do not include any timing
493  * information, only the offset to the start of the next frame.
494  * See gstac3parse.c for a more detailed description.
495  * */
496 
497 static GstFlowReturn
gst_dca_parse_chain_priv(GstPad * pad,GstObject * parent,GstBuffer * buffer)498 gst_dca_parse_chain_priv (GstPad * pad, GstObject * parent, GstBuffer * buffer)
499 {
500   GstDcaParse *dcaparse = GST_DCA_PARSE (parent);
501   GstFlowReturn ret;
502   GstBuffer *newbuf;
503   gsize size;
504 
505   size = gst_buffer_get_size (buffer);
506   if (size >= 2) {
507     newbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 2, size - 2);
508     gst_buffer_unref (buffer);
509     ret = dcaparse->baseparse_chainfunc (pad, parent, newbuf);
510   } else {
511     gst_buffer_unref (buffer);
512     ret = GST_FLOW_OK;
513   }
514 
515   return ret;
516 }
517 
518 static void
remove_fields(GstCaps * caps)519 remove_fields (GstCaps * caps)
520 {
521   guint i, n;
522 
523   n = gst_caps_get_size (caps);
524   for (i = 0; i < n; i++) {
525     GstStructure *s = gst_caps_get_structure (caps, i);
526 
527     gst_structure_remove_field (s, "framed");
528   }
529 }
530 
531 static GstCaps *
gst_dca_parse_get_sink_caps(GstBaseParse * parse,GstCaps * filter)532 gst_dca_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
533 {
534   GstCaps *peercaps, *templ;
535   GstCaps *res;
536 
537   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
538   if (filter) {
539     GstCaps *fcopy = gst_caps_copy (filter);
540     /* Remove the fields we convert */
541     remove_fields (fcopy);
542     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
543     gst_caps_unref (fcopy);
544   } else
545     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
546 
547   if (peercaps) {
548     /* Remove the framed field */
549     peercaps = gst_caps_make_writable (peercaps);
550     remove_fields (peercaps);
551 
552     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
553     gst_caps_unref (peercaps);
554     gst_caps_unref (templ);
555   } else {
556     res = templ;
557   }
558 
559   if (filter) {
560     GstCaps *intersection;
561 
562     intersection =
563         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
564     gst_caps_unref (res);
565     res = intersection;
566   }
567 
568   return res;
569 }
570 
571 static gboolean
gst_dca_parse_set_sink_caps(GstBaseParse * parse,GstCaps * caps)572 gst_dca_parse_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
573 {
574   GstStructure *s;
575   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
576 
577   s = gst_caps_get_structure (caps, 0);
578   if (gst_structure_has_name (s, "audio/x-private1-dts")) {
579     gst_pad_set_chain_function (parse->sinkpad, gst_dca_parse_chain_priv);
580   } else {
581     gst_pad_set_chain_function (parse->sinkpad, dcaparse->baseparse_chainfunc);
582   }
583   return TRUE;
584 }
585 
586 static GstFlowReturn
gst_dca_parse_pre_push_frame(GstBaseParse * parse,GstBaseParseFrame * frame)587 gst_dca_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
588 {
589   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
590 
591   if (!dcaparse->sent_codec_tag) {
592     GstTagList *taglist;
593     GstCaps *caps;
594 
595     /* codec tag */
596     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
597     if (G_UNLIKELY (caps == NULL)) {
598       if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
599         GST_INFO_OBJECT (parse, "Src pad is flushing");
600         return GST_FLOW_FLUSHING;
601       } else {
602         GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
603         return GST_FLOW_NOT_NEGOTIATED;
604       }
605     }
606 
607     taglist = gst_tag_list_new_empty ();
608     gst_pb_utils_add_codec_description_to_tag_list (taglist,
609         GST_TAG_AUDIO_CODEC, caps);
610     gst_caps_unref (caps);
611 
612     gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
613     gst_tag_list_unref (taglist);
614 
615     /* also signals the end of first-frame processing */
616     dcaparse->sent_codec_tag = TRUE;
617   }
618 
619   frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
620 
621   return GST_FLOW_OK;
622 }
623