Skip to content

Commit

Permalink
Add frame syncing and bitstream conversion APIs.
Browse files Browse the repository at this point in the history
Also add support for reading 14-bit DTS streams packed into 16-bit words.
Fixes #23.
  • Loading branch information
foo86 committed Apr 16, 2015
1 parent 77ee882 commit 84b77a1
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 83 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ INC_LIB = \
libdcadec/dca_context.h

ifndef CONFIG_SMALL
SRC_LIB += libdcadec/dca_frame.c
SRC_LIB += libdcadec/dca_stream.c
SRC_LIB += libdcadec/dca_waveout.c
INC_LIB += libdcadec/dca_frame.h
INC_LIB += libdcadec/dca_stream.h
INC_LIB += libdcadec/dca_waveout.h
endif
Expand Down
2 changes: 1 addition & 1 deletion dcadec.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ int main(int argc, char **argv)
}

if (ret == 0) {
fprintf(stderr, "This doesn't look like a 16-bit DTS bit stream\n");
fprintf(stderr, "This doesn't look like a valid DTS bit stream\n");
dcadec_stream_close(stream);
return 1;
}
Expand Down
2 changes: 2 additions & 0 deletions libdcadec/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ enum Speaker {
enum SyncWord {
SYNC_WORD_CORE = 0x7ffe8001,
SYNC_WORD_CORE_LE = 0xfe7f0180,
SYNC_WORD_CORE_LE14 = 0xff1f00e8,
SYNC_WORD_CORE_BE14 = 0x1fffe800,
SYNC_WORD_REV1AUX = 0x9a1105a0,
SYNC_WORD_REV2AUX = 0x7004c070,
SYNC_WORD_XCH = 0x5a5a5a5a,
Expand Down
144 changes: 144 additions & 0 deletions libdcadec/dca_frame.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* This file is part of libdcadec.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "common.h"
#include "bitstream.h"
#include "dca_frame.h"

#define SRC_OP(E) \
uint16_t src_0 = DCA_16##E(_src[0]); \
uint16_t src_1 = DCA_16##E(_src[1]); \
uint16_t src_2 = DCA_16##E(_src[2]); \
uint16_t src_3 = DCA_16##E(_src[3]); \
uint16_t src_4 = DCA_16##E(_src[4]); \
uint16_t src_5 = DCA_16##E(_src[5]); \
uint16_t src_6 = DCA_16##E(_src[6]); \
uint16_t src_7 = DCA_16##E(_src[7]);

#define DST_OP \
_dst[0] = DCA_16BE((src_0 << 2) | ((src_1 & 0x3fff) >> 12)); \
_dst[1] = DCA_16BE((src_1 << 4) | ((src_2 & 0x3fff) >> 10)); \
_dst[2] = DCA_16BE((src_2 << 6) | ((src_3 & 0x3fff) >> 8)); \
_dst[3] = DCA_16BE((src_3 << 8) | ((src_4 & 0x3fff) >> 6)); \
_dst[4] = DCA_16BE((src_4 << 10) | ((src_5 & 0x3fff) >> 4)); \
_dst[5] = DCA_16BE((src_5 << 12) | ((src_6 & 0x3fff) >> 2)); \
_dst[6] = DCA_16BE((src_6 << 14) | ((src_7 & 0x3fff) >> 0));

DCADEC_API int dcadec_frame_convert_bitstream(uint8_t *dst, size_t *dst_size,
const uint8_t *src, size_t src_size)
{
const uint16_t *_src = (const uint16_t *)src;
uint16_t *_dst = (uint16_t *)dst;
size_t count;

switch (DCA_MEM32NE(src)) {
case DCA_32BE(SYNC_WORD_CORE):
case DCA_32BE(SYNC_WORD_EXSS):
if (src != dst)
memcpy(dst, src, src_size);
*dst_size = src_size;
return DCADEC_BITSTREAM_BE16;

case DCA_32BE(SYNC_WORD_CORE_LE):
case DCA_32BE(SYNC_WORD_EXSS_LE):
count = (src_size + 1) / 2;
while (count--)
*_dst++ = dca_bswap16(*_src++);
*dst_size = src_size;
return DCADEC_BITSTREAM_LE16;

case DCA_32BE(SYNC_WORD_CORE_BE14):
count = (src_size + 15) / 16;
while (count--) {
SRC_OP(BE)
DST_OP
_src += 8;
_dst += 7;
}
*dst_size = src_size - src_size / 8;
return DCADEC_BITSTREAM_BE14;

case DCA_32BE(SYNC_WORD_CORE_LE14):
count = (src_size + 15) / 16;
while (count--) {
SRC_OP(LE)
DST_OP
_src += 8;
_dst += 7;
}
*dst_size = src_size - src_size / 8;
return DCADEC_BITSTREAM_LE14;

default:
return -DCADEC_ENOSYNC;
}
}

#undef SRC_OP
#undef DST_OP

DCADEC_API int dcadec_frame_parse_header(const uint8_t *data, size_t *size)
{
struct bitstream bits;
uint8_t header[DCADEC_FRAME_HEADER_SIZE];
size_t header_size, frame_size;

int ret;
if ((ret = dcadec_frame_convert_bitstream(header, &header_size,
data, DCADEC_FRAME_HEADER_SIZE)) < 0)
return ret;

bits_init(&bits, header, header_size);

switch (bits_get(&bits, 32)) {
case SYNC_WORD_CORE: {
bool normal_frame = bits_get1(&bits);
int deficit_samples = bits_get(&bits, 5) + 1;
if (normal_frame && deficit_samples != 32)
return -DCADEC_ENOSYNC;
bits_skip1(&bits);
int npcmblocks = bits_get(&bits, 7) + 1;
if (npcmblocks < 6)
return -DCADEC_ENOSYNC;
frame_size = bits_get(&bits, 14) + 1;
if (frame_size < 96)
return -DCADEC_ENOSYNC;
if (ret & DCADEC_BITSTREAM_BE14)
*size = frame_size + frame_size / 7;
else
*size = frame_size;
return DCADEC_FRAME_TYPE_CORE;
}

case SYNC_WORD_EXSS: {
bits_skip(&bits, 10);
bool wide_hdr = bits_get1(&bits);
bits_skip(&bits, 8 + 4 * wide_hdr);
frame_size = bits_get(&bits, 16 + 4 * wide_hdr) + 1;
if (frame_size < DCADEC_FRAME_HEADER_SIZE)
return -DCADEC_ENOSYNC;
if (frame_size & 3)
return -DCADEC_ENOSYNC;
*size = frame_size;
return DCADEC_FRAME_TYPE_EXSS;
}

default:
return -DCADEC_ENOSYNC;
}
}
88 changes: 88 additions & 0 deletions libdcadec/dca_frame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* This file is part of libdcadec.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef DCA_FRAME_H
#define DCA_FRAME_H

#include "dca_context.h"

/**
* Minimum size alignment, in bytes, of source and destination buffers that must
* be passed to dcadec_frame_convert_bitstream().
*/
#define DCADEC_FRAME_BUFFER_ALIGN 16

/**
* Minimum size of data buffer, in bytes, that must be passed to
* dcadec_frame_parse_header().
*/
#define DCADEC_FRAME_HEADER_SIZE 16

/**@{*/
#define DCADEC_BITSTREAM_BE16 0
#define DCADEC_BITSTREAM_LE16 1
#define DCADEC_BITSTREAM_BE14 2
#define DCADEC_BITSTREAM_LE14 3
/**@}*/

/**@{*/
#define DCADEC_FRAME_TYPE_CORE 0 /**< Backward compatible DTS core */
#define DCADEC_FRAME_TYPE_EXSS 1 /**< Extension sub-stream (EXSS) */
/**@}*/

/**
* Convert the raw input frame into native 16-bit big-endian format understood
* by dcadec_context_parse(). Can operate in-place when destination buffer
* is the same as source buffer.
*
* @param dst Pointer to destination buffer. Destination buffer size
* must be no less than source buffer size, including
* alignment.
*
* @param dst_size Filled with resulting frame size after conversion, in bytes.
*
* @param src Pointer to source buffer that must start with a valid sync
* word. Source buffer size must be no less than src_size bytes
* plus alignment to DCADEC_FRAME_BUFFER_ALIGN boundary.
*
* @param src_size Size of raw frame data prior to conversion, in bytes. Size
* should not include aligment.
*
* @return Detected bitstream format on success, negative error code
* on failure.
*/
DCADEC_API int dcadec_frame_convert_bitstream(uint8_t *dst, size_t *dst_size,
const uint8_t *src, size_t src_size);

/**
* Check that the passed data buffer starts with a valid sync word and satisfies
* the basic requirements to be a valid DTS core or EXSS frame header.
*
* @param data Pointer to data buffer that possibly contains the frame
* header. The first DCADEC_FRAME_HEADER_SIZE bytes are read
* from the buffer.
*
* @param size Filled with raw frame size, in bytes. Frame size includes
* sync word and header bytes.
*
* @return Detected frame type on success, negative error code
* on failure.
*/
DCADEC_API int dcadec_frame_parse_header(const uint8_t *data, size_t *size);

#endif

2 comments on commit 84b77a1

@michaelni
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks build with gcc 4.6
libdcadec/dcadec/libdcadec/dca_frame.c:80: undefined reference to __builtin_bswap16' libdcadec/dcadec/libdcadec/dca_frame.c:80: undefined reference to__builtin_bswap16'
libdcadec/dcadec/libdcadec/dca_frame.c:80: undefined reference to __builtin_bswap16' libdcadec/dcadec/libdcadec/dca_frame.c:80: undefined reference to__builtin_bswap16'
libdcadec/dcadec/libdcadec/dca_frame.c:80: undefined reference to __builtin_bswap16' libdcadec/libdcadec.a(dca_frame.o):libdcadec/dcadec/libdcadec/dca_frame.c:80: more undefined references to__builtin_bswap16' follow

@foo86
Copy link
Owner Author

@foo86 foo86 commented on 84b77a1 Apr 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks build with gcc 4.6

Pushed fbfe177 that should fix this (haven't actually tested).

Please sign in to comment.