Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/adxdec.c |
Date: | 2022-07-05 19:52:29 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 52 | 86 | 60.5% |
Branches: | 19 | 48 | 39.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * ADX ADPCM codecs | ||
3 | * Copyright (c) 2001,2003 BERO | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include "libavutil/intreadwrite.h" | ||
23 | #include "avcodec.h" | ||
24 | #include "adx.h" | ||
25 | #include "codec_internal.h" | ||
26 | #include "get_bits.h" | ||
27 | #include "internal.h" | ||
28 | |||
29 | /** | ||
30 | * @file | ||
31 | * SEGA CRI adx codecs. | ||
32 | * | ||
33 | * Reference documents: | ||
34 | * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html | ||
35 | * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/ | ||
36 | */ | ||
37 | |||
38 | 2 | static av_cold int adx_decode_init(AVCodecContext *avctx) | |
39 | { | ||
40 | 2 | ADXContext *c = avctx->priv_data; | |
41 | int ret, header_size; | ||
42 | |||
43 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (avctx->extradata_size >= 24) { |
44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if ((ret = ff_adx_decode_header(avctx, avctx->extradata, |
45 | avctx->extradata_size, &header_size, | ||
46 | 2 | c->coeff)) < 0) { | |
47 | ✗ | av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n"); | |
48 | ✗ | return AVERROR_INVALIDDATA; | |
49 | } | ||
50 | 2 | c->channels = avctx->ch_layout.nb_channels; | |
51 | 2 | c->header_parsed = 1; | |
52 | } | ||
53 | |||
54 | 2 | avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | |
55 | |||
56 | 2 | return 0; | |
57 | } | ||
58 | |||
59 | /** | ||
60 | * Decode 32 samples from 18 bytes. | ||
61 | * | ||
62 | * A 16-bit scalar value is applied to 32 residuals, which then have a | ||
63 | * 2nd-order LPC filter applied to it to form the output signal for a single | ||
64 | * channel. | ||
65 | */ | ||
66 | 16794 | static int adx_decode(ADXContext *c, int16_t *out, int offset, | |
67 | const uint8_t *in, int ch) | ||
68 | { | ||
69 | 16794 | ADXChannelState *prev = &c->prev[ch]; | |
70 | GetBitContext gb; | ||
71 | 16794 | int scale = AV_RB16(in); | |
72 | int i; | ||
73 | int s0, s1, s2, d; | ||
74 | |||
75 | /* check if this is an EOF packet */ | ||
76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16794 times.
|
16794 | if (scale & 0x8000) |
77 | ✗ | return -1; | |
78 | |||
79 | 16794 | init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8); | |
80 | 16794 | out += offset; | |
81 | 16794 | s1 = prev->s1; | |
82 | 16794 | s2 = prev->s2; | |
83 |
2/2✓ Branch 0 taken 537408 times.
✓ Branch 1 taken 16794 times.
|
554202 | for (i = 0; i < BLOCK_SAMPLES; i++) { |
84 | 537408 | d = get_sbits(&gb, 4); | |
85 | 537408 | s0 = d * scale + ((c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS); | |
86 | 537408 | s2 = s1; | |
87 | 537408 | s1 = av_clip_int16(s0); | |
88 | 537408 | *out++ = s1; | |
89 | } | ||
90 | 16794 | prev->s1 = s1; | |
91 | 16794 | prev->s2 = s2; | |
92 | |||
93 | 16794 | return 0; | |
94 | } | ||
95 | |||
96 | 66 | static int adx_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
97 | int *got_frame_ptr, AVPacket *avpkt) | ||
98 | { | ||
99 | 66 | int buf_size = avpkt->size; | |
100 | 66 | ADXContext *c = avctx->priv_data; | |
101 | int16_t **samples; | ||
102 | int samples_offset; | ||
103 | 66 | const uint8_t *buf = avpkt->data; | |
104 | 66 | const uint8_t *buf_end = buf + avpkt->size; | |
105 | int num_blocks, ch, ret; | ||
106 | size_t new_extradata_size; | ||
107 | uint8_t *new_extradata; | ||
108 | |||
109 | 66 | new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, | |
110 | &new_extradata_size); | ||
111 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
66 | if (new_extradata && new_extradata_size > 0) { |
112 | int header_size; | ||
113 | ✗ | if ((ret = ff_adx_decode_header(avctx, new_extradata, | |
114 | new_extradata_size, &header_size, | ||
115 | ✗ | c->coeff)) < 0) { | |
116 | ✗ | av_log(avctx, AV_LOG_ERROR, "error parsing new ADX extradata\n"); | |
117 | ✗ | return AVERROR_INVALIDDATA; | |
118 | } | ||
119 | |||
120 | ✗ | c->eof = 0; | |
121 | } | ||
122 | |||
123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (c->eof) { |
124 | ✗ | *got_frame_ptr = 0; | |
125 | ✗ | return buf_size; | |
126 | } | ||
127 | |||
128 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
66 | if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) { |
129 | int header_size; | ||
130 | ✗ | if ((ret = ff_adx_decode_header(avctx, buf, buf_size, &header_size, | |
131 | ✗ | c->coeff)) < 0) { | |
132 | ✗ | av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n"); | |
133 | ✗ | return AVERROR_INVALIDDATA; | |
134 | } | ||
135 | ✗ | c->channels = avctx->ch_layout.nb_channels; | |
136 | ✗ | c->header_parsed = 1; | |
137 | ✗ | if (buf_size < header_size) | |
138 | ✗ | return AVERROR_INVALIDDATA; | |
139 | ✗ | buf += header_size; | |
140 | ✗ | buf_size -= header_size; | |
141 | } | ||
142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (!c->header_parsed) |
143 | ✗ | return AVERROR_INVALIDDATA; | |
144 | |||
145 | /* calculate number of blocks in the packet */ | ||
146 | 66 | num_blocks = buf_size / (BLOCK_SIZE * c->channels); | |
147 | |||
148 | /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF | ||
149 | packet */ | ||
150 |
2/4✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
|
66 | if (!num_blocks || buf_size % (BLOCK_SIZE * c->channels)) { |
151 | ✗ | if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) { | |
152 | ✗ | c->eof = 1; | |
153 | ✗ | *got_frame_ptr = 0; | |
154 | ✗ | return avpkt->size; | |
155 | } | ||
156 | ✗ | return AVERROR_INVALIDDATA; | |
157 | } | ||
158 | |||
159 | /* get output buffer */ | ||
160 | 66 | frame->nb_samples = num_blocks * BLOCK_SAMPLES; | |
161 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
|
66 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
162 | ✗ | return ret; | |
163 | 66 | samples = (int16_t **)frame->extended_data; | |
164 | 66 | samples_offset = 0; | |
165 | |||
166 |
2/2✓ Branch 0 taken 8397 times.
✓ Branch 1 taken 66 times.
|
8463 | while (num_blocks--) { |
167 |
2/2✓ Branch 0 taken 16794 times.
✓ Branch 1 taken 8397 times.
|
25191 | for (ch = 0; ch < c->channels; ch++) { |
168 |
2/4✓ Branch 0 taken 16794 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 16794 times.
|
16794 | if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) { |
169 | ✗ | c->eof = 1; | |
170 | ✗ | buf = avpkt->data + avpkt->size; | |
171 | ✗ | break; | |
172 | } | ||
173 | 16794 | buf_size -= BLOCK_SIZE; | |
174 | 16794 | buf += BLOCK_SIZE; | |
175 | } | ||
176 |
1/2✓ Branch 0 taken 8397 times.
✗ Branch 1 not taken.
|
8397 | if (!c->eof) |
177 | 8397 | samples_offset += BLOCK_SAMPLES; | |
178 | } | ||
179 | |||
180 | 66 | frame->nb_samples = samples_offset; | |
181 | 66 | *got_frame_ptr = 1; | |
182 | |||
183 | 66 | return buf - avpkt->data; | |
184 | } | ||
185 | |||
186 | ✗ | static void adx_decode_flush(AVCodecContext *avctx) | |
187 | { | ||
188 | ✗ | ADXContext *c = avctx->priv_data; | |
189 | ✗ | memset(c->prev, 0, sizeof(c->prev)); | |
190 | ✗ | c->eof = 0; | |
191 | } | ||
192 | |||
193 | const FFCodec ff_adpcm_adx_decoder = { | ||
194 | .p.name = "adpcm_adx", | ||
195 | .p.long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"), | ||
196 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
197 | .p.id = AV_CODEC_ID_ADPCM_ADX, | ||
198 | .priv_data_size = sizeof(ADXContext), | ||
199 | .init = adx_decode_init, | ||
200 | FF_CODEC_DECODE_CB(adx_decode_frame), | ||
201 | .flush = adx_decode_flush, | ||
202 | .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | | ||
203 | AV_CODEC_CAP_DR1, | ||
204 | .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, | ||
205 | AV_SAMPLE_FMT_NONE }, | ||
206 | .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, | ||
207 | }; | ||
208 |