105#define SPDIF_FLAG_BIGENDIAN 0x01
114{
"spdif_flags",
"IEC 61937 encapsulation flags", offsetof(
IEC61937Context, spdif_flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM, .unit =
"spdif_flags" },
116{
"dtshd_rate",
"mux complete DTS frames in HD mode at the specified IEC958 rate (in Hz, default 0=disabled)", offsetof(
IEC61937Context, dtshd_rate),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 768000,
AV_OPT_FLAG_ENCODING_PARAM },
117{
"dtshd_fallback_time",
"min secs to strip HD for after an overflow (-1: till the end, default 60)", offsetof(
IEC61937Context, dtshd_fallback),
AV_OPT_TYPE_INT, {.i64 = 60}, -1, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM },
122 .class_name =
"spdif",
131 int bitstream_mode =
pkt->data[5] & 0x7;
141 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
145 int bsid =
pkt->data[5] >> 3;
146 if (bsid > 10 && (
pkt->data[4] & 0xc0) != 0xc0)
147 repeat = eac3_repeat[(
pkt->data[4] & 0x30) >> 4];
154 memcpy(&
ctx->hd_buf[0][
ctx->hd_buf_filled],
pkt->data,
pkt->size);
156 ctx->hd_buf_filled +=
pkt->size;
157 if (++
ctx->hd_buf_count < repeat){
162 ctx->pkt_offset = 24576;
163 ctx->out_buf =
ctx->hd_buf[0];
164 ctx->out_bytes =
ctx->hd_buf_filled;
165 ctx->length_code =
ctx->hd_buf_filled;
167 ctx->hd_buf_count = 0;
168 ctx->hd_buf_filled = 0;
182 case 512:
return 0x0;
183 case 1024:
return 0x1;
184 case 2048:
return 0x2;
185 case 4096:
return 0x3;
186 case 8192:
return 0x4;
187 case 16384:
return 0x5;
193 int sample_rate,
int blocks)
196 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
197 int pkt_size =
pkt->size;
211 period =
ctx->dtshd_rate * (blocks << 5) / sample_rate;
216 "impossible repetition period of %d for the current DTS stream"
217 " (blocks = %d, sample rate = %d)\n",
ctx->dtshd_rate, period,
218 blocks << 5, sample_rate);
224 ctx->pkt_offset = period * 4;
232 if (
sizeof(dtshd_start_code) + 2 + pkt_size
234 if (!
ctx->dtshd_skip)
236 "temporarily sending core only\n");
237 if (
ctx->dtshd_fallback > 0)
238 ctx->dtshd_skip = sample_rate *
ctx->dtshd_fallback / (blocks << 5);
244 if (
ctx->dtshd_skip && core_size && core_size <= pkt->
size) {
245 pkt_size = core_size;
246 if (
ctx->dtshd_fallback >= 0)
250 ctx->out_bytes =
sizeof(dtshd_start_code) + 2 + pkt_size;
254 ctx->length_code =
FFALIGN(
ctx->out_bytes + 0x8, 0x10) - 0x8;
260 ctx->out_buf =
ctx->hd_buf[0];
262 memcpy(
ctx->hd_buf[0], dtshd_start_code,
sizeof(dtshd_start_code));
263 AV_WB16(
ctx->hd_buf[0] +
sizeof(dtshd_start_code), pkt_size);
264 memcpy(
ctx->hd_buf[0] +
sizeof(dtshd_start_code) + 2,
pkt->data, pkt_size);
280 switch (syncword_dts) {
282 blocks = (
AV_RB16(
pkt->data + 4) >> 2) & 0x7f;
283 core_size = ((
AV_RB24(
pkt->data + 5) >> 4) & 0x3fff) + 1;
287 blocks = (
AV_RL16(
pkt->data + 4) >> 2) & 0x7f;
288 ctx->extra_bswap = 1;
292 (((
pkt->data[5] & 0x07) << 4) | ((
pkt->data[6] & 0x3f) >> 2));
296 (((
pkt->data[4] & 0x07) << 4) | ((
pkt->data[7] & 0x3f) >> 2));
297 ctx->extra_bswap = 1;
326 if (core_size && core_size < pkt->
size) {
327 ctx->out_bytes = core_size;
328 ctx->length_code = core_size << 3;
331 ctx->pkt_offset = blocks << 7;
333 if (
ctx->out_bytes ==
ctx->pkt_offset) {
337 ctx->use_preamble = 0;
356 int layer = 3 - ((
pkt->data[1] >> 1) & 3);
357 int extension =
pkt->data[2] & 1;
359 if (layer == 3 ||
version == 1) {
364 if (
version == 2 && extension) {
366 ctx->pkt_offset = 4608;
388 ctx->pkt_offset = samples << 2;
401 "%"PRIu32
" samples in AAC frame not supported\n", samples);
413#define MAT_PKT_OFFSET 61440
414#define MAT_FRAME_SIZE 61424
415#define TRUEHD_NOMINAL_AU_SPACING 2560
418 0x07, 0x9E, 0x00, 0x03, 0x84, 0x01, 0x01, 0x01, 0x80, 0x00, 0x56, 0xA5, 0x3B, 0xF4, 0x81, 0x83,
419 0x49, 0x80, 0x77, 0xE0,
422 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0,
425 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11,
428#define MAT_CODE(position, data) { .pos = position, .code = data, .len = sizeof(data) }
460 ctx->hd_buf_ready_count++;
476 int major_sync_size = 28;
485 memset(au, 0,
sizeof(*au));
500 ratebits =
data[4] >> 4;
501 else if (
data[3] == 0xbb)
502 ratebits =
data[5] >> 4;
510 if (
data[3] == 0xba &&
data[25] & 1) {
511 int ext_size =
data[26] >> 4;
512 major_sync_size += 2 + ext_size * 2;
515 if (major_sync_size >
size)
522 num_substreams =
data[16] >> 4;
523 if (num_substreams <= 0)
526 for (
i = 0;
i < num_substreams;
i++) {
554 if (sync_word != (0x31ea >> 1))
567 uint8_t *hd_buf =
ctx->hd_buf[
ctx->hd_buf_idx];
569 int padding_remaining = 0;
571 int output_discontinuity = 0;
572 uint16_t input_timing;
573 int total_frame_size =
pkt->size;
574 const uint8_t *dataptr =
pkt->data;
575 int data_remaining =
pkt->size;
590 ctx->truehd_samples_per_frame);
593 if (!
ctx->truehd_samples_per_frame)
598 ctx->truehd_output_timing +=
ctx->truehd_samples_per_frame;
600 if (
ctx->truehd_output_timing_valid &&
616 ctx->truehd_samples_per_frame;
618 ctx->truehd_samples_per_frame;
619 int previous_oi_delta =
ctx->truehd_oi_delta;
620 int current_oi_delta = (int16_t)(output_timing_minus_spf -
622 int prev_padding = 0;
623 int discontinuity_padding = 0;
625 output_discontinuity = 1;
626 discontinuity_padding = (previous_oi_delta - current_oi_delta) *
629 if (
ctx->truehd_prev_size)
632 padding_remaining = prev_padding + discontinuity_padding;
634 if (padding_remaining < 0 || padding_remaining > max_padding_per_packet) {
636 "Unusual TrueHD output_timing discontinuity: "
637 "expected %"PRIu16
" got %"PRIu16
", "
638 "timing delta %d => %d, prev padding %d, "
639 "discontinuity padding %d, "
640 "total padding %d ignored",
642 previous_oi_delta, current_oi_delta, prev_padding,
643 discontinuity_padding, padding_remaining);
644 padding_remaining = 0;
647 "TrueHD output_timing discontinuity: "
648 "expected %"PRIu16
" got %"PRIu16
", "
649 "timing delta %d => %d, prev padding %d, "
650 "discontinuity padding %d, "
651 "total padding %d\n",
653 previous_oi_delta, current_oi_delta, prev_padding,
654 discontinuity_padding, padding_remaining);
659 ctx->truehd_output_timing_valid = 1;
662 if (
ctx->truehd_prev_size && !output_discontinuity) {
663 uint16_t delta_samples = input_timing -
ctx->truehd_prev_time;
674 ctx->truehd_samples_per_frame;
677 padding_remaining = delta_bytes -
ctx->truehd_prev_size;
680 delta_samples, delta_bytes);
683 if (padding_remaining < 0 || padding_remaining > max_padding_per_packet) {
685 ctx->truehd_prev_time, input_timing,
ctx->truehd_samples_per_frame);
686 padding_remaining = 0;
690 if (
ctx->truehd_output_timing_valid) {
691 uint16_t output_timing_minus_spf =
ctx->truehd_output_timing -
692 ctx->truehd_samples_per_frame;
693 ctx->truehd_oi_delta = (int16_t)(output_timing_minus_spf - input_timing);
703 while (padding_remaining || data_remaining ||
708 int code_len =
mat_codes[next_code_idx].len;
709 int code_len_remaining = code_len;
712 ctx->hd_buf_filled += code_len;
724 hd_buf =
ctx->hd_buf[
ctx->hd_buf_idx];
725 ctx->hd_buf_filled = 0;
731 if (padding_remaining) {
733 int counted_as_padding =
FFMIN(padding_remaining,
735 padding_remaining -= counted_as_padding;
736 code_len_remaining -= counted_as_padding;
739 if (code_len_remaining)
740 total_frame_size += code_len_remaining;
743 if (padding_remaining) {
747 memset(hd_buf +
ctx->hd_buf_filled, 0, padding_to_insert);
748 ctx->hd_buf_filled += padding_to_insert;
749 padding_remaining -= padding_to_insert;
751 if (padding_remaining)
755 if (data_remaining) {
759 memcpy(hd_buf +
ctx->hd_buf_filled, dataptr, data_to_insert);
760 ctx->hd_buf_filled += data_to_insert;
761 dataptr += data_to_insert;
762 data_remaining -= data_to_insert;
766 ctx->truehd_prev_size = total_frame_size;
767 ctx->truehd_prev_time = input_timing;
770 total_frame_size,
ctx->hd_buf_filled);
784 switch (
s->streams[0]->codecpar->codec_id) {
813 s->streams[0]->codecpar->codec_id);
842 ctx->out_bytes =
pkt->size;
844 ctx->use_preamble = 1;
845 ctx->extra_bswap = 0;
847 ret =
ctx->header_info(
s,
pkt);
857 if (!
ctx->hd_buf_ready_count)
860 ready_idx =
ctx->hd_buf_next_ready_idx;
862 ctx->hd_buf_ready_count--;
864 ctx->out_buf =
ctx->hd_buf[ready_idx];
871 if (!
ctx->pkt_offset)
880 if (
ctx->use_preamble) {
898 if (
ctx->out_bytes & 1)
904 ctx->data_type,
ctx->out_bytes,
ctx->pkt_offset);
912 .p.extensions =
"spdif",
int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames)
Extract the number of samples and frames from AAC data.
static double val(void *priv, double ch)
void avio_wl16(AVIOContext *s, unsigned int val)
void avio_wb16(AVIOContext *s, unsigned int val)
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void ffio_fill(AVIOContext *s, int b, int64_t count)
#define i(width, name, range_min, range_max)
const uint32_t ff_dca_sample_rates[16]
#define DCA_SYNCWORD_SUBSTREAM
#define DCA_SYNCWORD_CORE_14B_BE
#define DCA_SYNCWORD_CORE_LE
#define DCA_SYNCWORD_CORE_BE
#define DCA_SYNCWORD_CORE_14B_LE
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static unsigned int get_bits1(GetBitContext *s)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static void skip_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
#define LIBAVUTIL_VERSION_INT
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
Memory handling functions.
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
#define FF_ARRAY_ELEMS(a)
void ff_spdif_bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
@ IEC61937_MPEG1_LAYER1
MPEG-1 layer 1.
@ IEC61937_MPEG2_AAC_LSF_2048
MPEG-2 AAC ADTS half-rate low sampling frequency.
@ IEC61937_TRUEHD
TrueHD data.
@ IEC61937_DTS3
DTS type III (2048 samples)
@ IEC61937_MPEG1_LAYER23
MPEG-1 layer 2 or 3 data or MPEG-2 without extension.
@ IEC61937_MPEG2_EXT
MPEG-2 data with extension.
@ IEC61937_DTS2
DTS type II (1024 samples)
@ IEC61937_MPEG2_AAC_LSF_4096
MPEG-2 AAC ADTS quarter-rate low sampling frequency.
@ IEC61937_MPEG2_AAC
MPEG-2 AAC ADTS.
@ IEC61937_DTSHD
DTS HD data.
@ IEC61937_MPEG2_LAYER2_LSF
MPEG-2, layer-2 low sampling frequency.
@ IEC61937_EAC3
E-AC-3 data.
@ IEC61937_MPEG2_LAYER1_LSF
MPEG-2, layer-1 low sampling frequency.
@ IEC61937_DTS1
DTS type I (512 samples)
@ IEC61937_MPEG2_LAYER3_LSF
MPEG-2, layer-3 low sampling frequency.
static const uint16_t spdif_mpeg_pkt_offset[2][3]
#define BURST_HEADER_SIZE
static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
static const uint8_t mat_start_code[20]
static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
static int spdif_dts4_subtype(int period)
static enum IEC61937DataType mpeg_data_type[2][3]
static int truehd_enqueue_mat(AVFormatContext *s)
Mark the current MAT buffer ready and advance to the next free buffer.
static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size, int sample_rate, int blocks)
static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
#define MAT_CODE(position, data)
static int truehd_parse_access_unit(const uint8_t *au_data, int au_size, TrueHDAccessUnitInfo *au)
static const AVClass spdif_class
#define TRUEHD_NOMINAL_AU_SPACING
static const struct @332306274226367364027312103037300030072170240123 mat_codes[]
static int spdif_write_header(AVFormatContext *s)
static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
#define SPDIF_FLAG_BIGENDIAN
static const uint8_t mat_end_code[16]
static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
static const uint8_t mat_middle_code[12]
static void spdif_deinit(AVFormatContext *s)
static int truehd_next_mat_buffer(int idx)
Get the next index in the MAT buffer ring.
static av_always_inline void spdif_put_16(IEC61937Context *ctx, AVIOContext *pb, unsigned int val)
Describe the class of an AVClass context structure.
This structure stores compressed data.
int length_code
length code in bits or bytes, depending on data type
int hd_buf_next_ready_idx
oldest completed truehd MAT buffer ready to write (truehd)
int(* header_info)(AVFormatContext *s, AVPacket *pkt)
function, which generates codec dependent header information.
uint16_t truehd_output_timing
expected output_timing for truehd restart headers
int hd_buf_count
number of frames in the hd audio buffer (eac3)
const uint8_t * out_buf
pointer to the outgoing data before byte-swapping
int truehd_output_timing_valid
restart header output_timing has been read
int dtshd_skip
counter used for skipping DTS-HD frames
int truehd_prev_size
previous frame size in bytes, including any MAT codes
int hd_buf_filled
amount of bytes in the hd audio buffer (eac3, truehd)
uint8_t * buffer
allocated buffer, used for swap bytes
int extra_bswap
extra bswap for payload (for LE DTS => standard BE DTS)
int hd_buf_size
size of the hd audio buffer (eac3, dts4)
int use_preamble
preamble enabled (disabled for exactly pre-padded DTS)
int hd_buf_ready_count
number of completed TrueHD MAT buffers ready to write (truehd)
enum IEC61937DataType data_type
burst info - reference to type of payload of the data-burst
uint16_t truehd_prev_time
input_timing from the last frame
int truehd_oi_delta
signed (output_timing-samples_per_frame)-input_timing
uint8_t * hd_buf[HD_BUF_COUNT]
allocated buffers to concatenate hd audio frames
int pkt_offset
data burst repetition period in bytes
int truehd_samples_per_frame
samples per frame for padding calculation
int out_bytes
amount of outgoing bytes
int buffer_size
size of allocated buffer
int hd_buf_idx
active hd buffer index (truehd)
#define avpriv_request_sample(...)
static AVFormatContext * ctx