/src/ffmpeg/libavcodec/dca.c
Line | Count | Source |
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 | 15.5M | { |
52 | 15.5M | uint32_t mrk; |
53 | 15.5M | int i, tmp; |
54 | 15.5M | PutBitContext pb; |
55 | | |
56 | 15.5M | if ((unsigned) src_size > (unsigned) max_size) |
57 | 0 | src_size = max_size; |
58 | | |
59 | 15.5M | mrk = AV_RB32(src); |
60 | 15.5M | switch (mrk) { |
61 | 268k | case DCA_SYNCWORD_CORE_BE: |
62 | 349k | case DCA_SYNCWORD_SUBSTREAM: |
63 | 349k | memcpy(dst, src, src_size); |
64 | 349k | return src_size; |
65 | 78.0k | case DCA_SYNCWORD_CORE_LE: |
66 | 12.7M | for (i = 0; i < (src_size + 1) >> 1; i++) { |
67 | 12.7M | AV_WB16(dst, AV_RL16(src)); |
68 | 12.7M | src += 2; |
69 | 12.7M | dst += 2; |
70 | 12.7M | } |
71 | 78.0k | return src_size; |
72 | 70.7k | case DCA_SYNCWORD_CORE_14B_BE: |
73 | 155k | case DCA_SYNCWORD_CORE_14B_LE: |
74 | 155k | init_put_bits(&pb, dst, max_size); |
75 | 5.09M | for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) { |
76 | 4.93M | tmp = ((mrk == DCA_SYNCWORD_CORE_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF; |
77 | 4.93M | put_bits(&pb, 14, tmp); |
78 | 4.93M | } |
79 | 155k | flush_put_bits(&pb); |
80 | 155k | return put_bytes_output(&pb); |
81 | 15.0M | default: |
82 | 15.0M | return AVERROR_INVALIDDATA; |
83 | 15.5M | } |
84 | 15.5M | } |
85 | | |
86 | | int ff_dca_parse_core_frame_header(DCACoreFrameHeader *h, GetBitContext *gb) |
87 | 559k | { |
88 | 559k | if (get_bits_long(gb, 32) != DCA_SYNCWORD_CORE_BE) |
89 | 902 | return DCA_PARSE_ERROR_SYNC_WORD; |
90 | | |
91 | 559k | h->normal_frame = get_bits1(gb); |
92 | 559k | h->deficit_samples = get_bits(gb, 5) + 1; |
93 | 559k | if (h->deficit_samples != DCA_PCMBLOCK_SAMPLES) |
94 | 1.71k | return DCA_PARSE_ERROR_DEFICIT_SAMPLES; |
95 | | |
96 | 557k | h->crc_present = get_bits1(gb); |
97 | 557k | h->npcmblocks = get_bits(gb, 7) + 1; |
98 | 557k | if (h->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) |
99 | 157k | return DCA_PARSE_ERROR_PCM_BLOCKS; |
100 | | |
101 | 399k | h->frame_size = get_bits(gb, 14) + 1; |
102 | 399k | if (h->frame_size < 96) |
103 | 30.9k | return DCA_PARSE_ERROR_FRAME_SIZE; |
104 | | |
105 | 368k | h->audio_mode = get_bits(gb, 6); |
106 | 368k | if (h->audio_mode >= DCA_AMODE_COUNT) |
107 | 21.5k | return DCA_PARSE_ERROR_AMODE; |
108 | | |
109 | 346k | h->sr_code = get_bits(gb, 4); |
110 | 346k | if (!ff_dca_sample_rates[h->sr_code]) |
111 | 15.9k | return DCA_PARSE_ERROR_SAMPLE_RATE; |
112 | | |
113 | 331k | h->br_code = get_bits(gb, 5); |
114 | 331k | if (get_bits1(gb)) |
115 | 14.7k | return DCA_PARSE_ERROR_RESERVED_BIT; |
116 | | |
117 | 316k | h->drc_present = get_bits1(gb); |
118 | 316k | h->ts_present = get_bits1(gb); |
119 | 316k | h->aux_present = get_bits1(gb); |
120 | 316k | h->hdcd_master = get_bits1(gb); |
121 | 316k | h->ext_audio_type = get_bits(gb, 3); |
122 | 316k | h->ext_audio_present = get_bits1(gb); |
123 | 316k | h->sync_ssf = get_bits1(gb); |
124 | 316k | h->lfe_present = get_bits(gb, 2); |
125 | 316k | if (h->lfe_present == DCA_LFE_FLAG_INVALID) |
126 | 21.2k | return DCA_PARSE_ERROR_LFE_FLAG; |
127 | | |
128 | 295k | h->predictor_history = get_bits1(gb); |
129 | 295k | if (h->crc_present) |
130 | 96.5k | skip_bits(gb, 16); |
131 | 295k | h->filter_perfect = get_bits1(gb); |
132 | 295k | h->encoder_rev = get_bits(gb, 4); |
133 | 295k | h->copy_hist = get_bits(gb, 2); |
134 | 295k | h->pcmr_code = get_bits(gb, 3); |
135 | 295k | if (!ff_dca_bits_per_sample[h->pcmr_code]) |
136 | 14.2k | return DCA_PARSE_ERROR_PCM_RES; |
137 | | |
138 | 280k | h->sumdiff_front = get_bits1(gb); |
139 | 280k | h->sumdiff_surround = get_bits1(gb); |
140 | 280k | h->dn_code = get_bits(gb, 4); |
141 | 280k | return 0; |
142 | 295k | } |
143 | | |
144 | | int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, const uint8_t *buf, int size) |
145 | 402k | { |
146 | 402k | GetBitContext gb; |
147 | 402k | int ret; |
148 | | |
149 | 402k | ret = init_get_bits8(&gb, buf, size); |
150 | 402k | if (ret < 0) |
151 | 0 | return ret; |
152 | | |
153 | 402k | if (ff_dca_parse_core_frame_header(h, &gb) < 0) |
154 | 255k | return AVERROR_INVALIDDATA; |
155 | | |
156 | 147k | return 0; |
157 | 402k | } |