-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add frame syncing and bitstream conversion APIs.
Also add support for reading 14-bit DTS streams packed into 16-bit words. Fixes #23.
- Loading branch information
Showing
6 changed files
with
273 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
84b77a1
There was a problem hiding this comment.
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' follow84b77a1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed fbfe177 that should fix this (haven't actually tested).