FFmpeg
dca.c
Go to the documentation of this file.
1 /*
2  * DCA compatible decoder data
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include <stdint.h>
26 #include <string.h>
27 
28 #include "libavutil/error.h"
29 
30 #include "dca.h"
31 #include "dca_core.h"
32 #include "dca_syncwords.h"
33 #include "get_bits.h"
34 #include "put_bits.h"
35 
36 const uint32_t ff_dca_sampling_freqs[16] = {
37  8000, 16000, 32000, 64000, 128000, 22050, 44100, 88200,
38  176400, 352800, 12000, 24000, 48000, 96000, 192000, 384000,
39 };
40 
41 const uint8_t ff_dca_freq_ranges[16] = {
42  0, 1, 2, 3, 4, 1, 2, 3, 4, 4, 0, 1, 2, 3, 4, 4
43 };
44 
45 const uint8_t ff_dca_bits_per_sample[8] = {
46  16, 16, 20, 20, 0, 24, 24, 0
47 };
48 
49 int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
50  int max_size)
51 {
52  uint32_t mrk;
53  int i, tmp;
54  PutBitContext pb;
55 
56  if ((unsigned) src_size > (unsigned) max_size)
57  src_size = max_size;
58 
59  mrk = AV_RB32(src);
60  switch (mrk) {
63  memcpy(dst, src, src_size);
64  return src_size;
66  for (i = 0; i < (src_size + 1) >> 1; i++) {
67  AV_WB16(dst, AV_RL16(src));
68  src += 2;
69  dst += 2;
70  }
71  return src_size;
74  init_put_bits(&pb, dst, max_size);
75  for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
76  tmp = ((mrk == DCA_SYNCWORD_CORE_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
77  put_bits(&pb, 14, tmp);
78  }
79  flush_put_bits(&pb);
80  return put_bytes_output(&pb);
81  default:
82  return AVERROR_INVALIDDATA;
83  }
84 }
85 
87 {
88  if (get_bits_long(gb, 32) != DCA_SYNCWORD_CORE_BE)
90 
91  h->normal_frame = get_bits1(gb);
92  h->deficit_samples = get_bits(gb, 5) + 1;
93  if (h->deficit_samples != DCA_PCMBLOCK_SAMPLES)
95 
96  h->crc_present = get_bits1(gb);
97  h->npcmblocks = get_bits(gb, 7) + 1;
98  if (h->npcmblocks & (DCA_SUBBAND_SAMPLES - 1))
100 
101  h->frame_size = get_bits(gb, 14) + 1;
102  if (h->frame_size < 96)
104 
105  h->audio_mode = get_bits(gb, 6);
106  if (h->audio_mode >= DCA_AMODE_COUNT)
107  return DCA_PARSE_ERROR_AMODE;
108 
109  h->sr_code = get_bits(gb, 4);
110  if (!ff_dca_sample_rates[h->sr_code])
112 
113  h->br_code = get_bits(gb, 5);
114  if (get_bits1(gb))
116 
117  h->drc_present = get_bits1(gb);
118  h->ts_present = get_bits1(gb);
119  h->aux_present = get_bits1(gb);
120  h->hdcd_master = get_bits1(gb);
121  h->ext_audio_type = get_bits(gb, 3);
122  h->ext_audio_present = get_bits1(gb);
123  h->sync_ssf = get_bits1(gb);
124  h->lfe_present = get_bits(gb, 2);
125  if (h->lfe_present == DCA_LFE_FLAG_INVALID)
127 
128  h->predictor_history = get_bits1(gb);
129  if (h->crc_present)
130  skip_bits(gb, 16);
131  h->filter_perfect = get_bits1(gb);
132  h->encoder_rev = get_bits(gb, 4);
133  h->copy_hist = get_bits(gb, 2);
134  h->pcmr_code = get_bits(gb, 3);
135  if (!ff_dca_bits_per_sample[h->pcmr_code])
137 
138  h->sumdiff_front = get_bits1(gb);
139  h->sumdiff_surround = get_bits1(gb);
140  h->dn_code = get_bits(gb, 4);
141  return 0;
142 }
143 
145 {
146  GetBitContext gb;
147  int ret;
148 
149  ret = init_get_bits8(&gb, buf, size);
150  if (ret < 0)
151  return ret;
152 
153  if (ff_dca_parse_core_frame_header(h, &gb) < 0)
154  return AVERROR_INVALIDDATA;
155 
156  return 0;
157 }
DCA_LFE_FLAG_INVALID
@ DCA_LFE_FLAG_INVALID
Definition: dca_core.h:82
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:89
dca.h
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
DCA_SYNCWORD_CORE_14B_BE
#define DCA_SYNCWORD_CORE_14B_BE
Definition: dca_syncwords.h:24
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:222
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
DCA_SUBBAND_SAMPLES
#define DCA_SUBBAND_SAMPLES
Definition: dca_core.h:43
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
avpriv_dca_convert_bitstream
int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
Convert bitstream to one representation based on sync marker.
Definition: dca.c:49
DCA_PARSE_ERROR_FRAME_SIZE
@ DCA_PARSE_ERROR_FRAME_SIZE
Definition: dca.h:42
GetBitContext
Definition: get_bits.h:108
dca_core.h
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
DCA_PARSE_ERROR_PCM_RES
@ DCA_PARSE_ERROR_PCM_RES
Definition: dca.h:47
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
ff_dca_sample_rates
const uint32_t ff_dca_sample_rates[16]
Definition: dca_sample_rate_tab.h:29
PutBitContext
Definition: put_bits.h:50
dca_syncwords.h
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:403
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
error.h
DCA_SYNCWORD_CORE_BE
#define DCA_SYNCWORD_CORE_BE
Definition: dca_syncwords.h:22
DCACoreFrameHeader
Definition: dca.h:50
DCA_PARSE_ERROR_SYNC_WORD
@ DCA_PARSE_ERROR_SYNC_WORD
Definition: dca.h:39
size
int size
Definition: twinvq_data.h:10344
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
DCA_SYNCWORD_CORE_14B_LE
#define DCA_SYNCWORD_CORE_14B_LE
Definition: dca_syncwords.h:25
DCA_PARSE_ERROR_DEFICIT_SAMPLES
@ DCA_PARSE_ERROR_DEFICIT_SAMPLES
Definition: dca.h:40
ff_dca_parse_core_frame_header
int ff_dca_parse_core_frame_header(DCACoreFrameHeader *h, GetBitContext *gb)
Parse and validate core frame header.
Definition: dca.c:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
DCA_SYNCWORD_SUBSTREAM
#define DCA_SYNCWORD_SUBSTREAM
Definition: dca_syncwords.h:32
DCA_PCMBLOCK_SAMPLES
#define DCA_PCMBLOCK_SAMPLES
Definition: dca_core.h:44
ret
ret
Definition: filter_design.txt:187
ff_dca_bits_per_sample
const uint8_t ff_dca_bits_per_sample[8]
Definition: dca.c:45
DCA_AMODE_COUNT
@ DCA_AMODE_COUNT
Definition: dca_core.h:69
DCA_PARSE_ERROR_PCM_BLOCKS
@ DCA_PARSE_ERROR_PCM_BLOCKS
Definition: dca.h:41
ff_dca_sampling_freqs
const uint32_t ff_dca_sampling_freqs[16]
Definition: dca.c:36
DCA_PARSE_ERROR_LFE_FLAG
@ DCA_PARSE_ERROR_LFE_FLAG
Definition: dca.h:46
DCA_PARSE_ERROR_SAMPLE_RATE
@ DCA_PARSE_ERROR_SAMPLE_RATE
Definition: dca.h:44
DCA_SYNCWORD_CORE_LE
#define DCA_SYNCWORD_CORE_LE
Definition: dca_syncwords.h:23
avpriv_dca_parse_core_frame_header
int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, const uint8_t *buf, int size)
Parse and validate core frame header.
Definition: dca.c:144
DCA_PARSE_ERROR_RESERVED_BIT
@ DCA_PARSE_ERROR_RESERVED_BIT
Definition: dca.h:45
ff_dca_freq_ranges
const uint8_t ff_dca_freq_ranges[16]
Definition: dca.c:41
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
put_bits.h
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
DCA_PARSE_ERROR_AMODE
@ DCA_PARSE_ERROR_AMODE
Definition: dca.h:43