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