3 * Copyright (c) 2005 Jeff Muizelaar
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file libavcodec/shorten.c
25 * @author Jeff Muizelaar
35 #define MAX_CHANNELS 8
36 #define MAX_BLOCKSIZE 65535
38 #define OUT_BUFFER_SIZE 16384
42 #define WAVE_FORMAT_PCM 0x0001
44 #define DEFAULT_BLOCK_SIZE 256
50 #define BITSHIFTSIZE 2
59 #define V2LPCQOFFSET (1 << LPCQUANT)
67 #define FN_BLOCKSIZE 5
73 #define VERBATIM_CKSIZE_SIZE 5
74 #define VERBATIM_BYTE_SIZE 8
75 #define CANONICAL_HEADER_SIZE 44
77 typedef struct ShortenContext
{
78 AVCodecContext
*avctx
;
81 int min_framesize
, max_framesize
;
84 int32_t *decoded
[MAX_CHANNELS
];
85 int32_t *offset
[MAX_CHANNELS
];
89 unsigned int allocated_bitstream_size
;
91 uint8_t header
[OUT_BUFFER_SIZE
];
103 static av_cold
int shorten_decode_init(AVCodecContext
* avctx
)
105 ShortenContext
*s
= avctx
->priv_data
;
107 avctx
->sample_fmt
= SAMPLE_FMT_S16
;
112 static int allocate_buffers(ShortenContext
*s
)
115 for (chan
=0; chan
<s
->channels
; chan
++) {
116 if(FFMAX(1, s
->nmean
) >= UINT_MAX
/sizeof(int32_t)){
117 av_log(s
->avctx
, AV_LOG_ERROR
, "nmean too large\n");
120 if(s
->blocksize
+ s
->nwrap
>= UINT_MAX
/sizeof(int32_t) || s
->blocksize
+ s
->nwrap
<= (unsigned)s
->nwrap
){
121 av_log(s
->avctx
, AV_LOG_ERROR
, "s->blocksize + s->nwrap too large\n");
125 s
->offset
[chan
] = av_realloc(s
->offset
[chan
], sizeof(int32_t)*FFMAX(1, s
->nmean
));
127 s
->decoded
[chan
] = av_realloc(s
->decoded
[chan
], sizeof(int32_t)*(s
->blocksize
+ s
->nwrap
));
128 for (i
=0; i
<s
->nwrap
; i
++)
129 s
->decoded
[chan
][i
] = 0;
130 s
->decoded
[chan
] += s
->nwrap
;
136 static inline unsigned int get_uint(ShortenContext
*s
, int k
)
139 k
= get_ur_golomb_shorten(&s
->gb
, ULONGSIZE
);
140 return get_ur_golomb_shorten(&s
->gb
, k
);
144 static void fix_bitshift(ShortenContext
*s
, int32_t *buffer
)
148 if (s
->bitshift
!= 0)
149 for (i
= 0; i
< s
->blocksize
; i
++)
150 buffer
[s
->nwrap
+ i
] <<= s
->bitshift
;
154 static void init_offset(ShortenContext
*s
)
158 int nblock
= FFMAX(1, s
->nmean
);
159 /* initialise offset */
160 switch (s
->internal_ftype
)
167 av_log(s
->avctx
, AV_LOG_ERROR
, "unknown audio type");
171 for (chan
= 0; chan
< s
->channels
; chan
++)
172 for (i
= 0; i
< nblock
; i
++)
173 s
->offset
[chan
][i
] = mean
;
176 static inline int get_le32(GetBitContext
*gb
)
178 return bswap_32(get_bits_long(gb
, 32));
181 static inline short get_le16(GetBitContext
*gb
)
183 return bswap_16(get_bits_long(gb
, 16));
186 static int decode_wave_header(AVCodecContext
*avctx
, uint8_t *header
, int header_size
)
193 init_get_bits(&hb
, header
, header_size
*8);
194 if (get_le32(&hb
) != MKTAG('R','I','F','F')) {
195 av_log(avctx
, AV_LOG_ERROR
, "missing RIFF tag\n");
199 chunk_size
= get_le32(&hb
);
201 if (get_le32(&hb
) != MKTAG('W','A','V','E')) {
202 av_log(avctx
, AV_LOG_ERROR
, "missing WAVE tag\n");
206 while (get_le32(&hb
) != MKTAG('f','m','t',' ')) {
208 skip_bits(&hb
, 8*len
);
213 av_log(avctx
, AV_LOG_ERROR
, "fmt chunk was too short\n");
217 wave_format
= get_le16(&hb
);
219 switch (wave_format
) {
220 case WAVE_FORMAT_PCM
:
223 av_log(avctx
, AV_LOG_ERROR
, "unsupported wave format\n");
227 avctx
->channels
= get_le16(&hb
);
228 avctx
->sample_rate
= get_le32(&hb
);
229 avctx
->bit_rate
= get_le32(&hb
) * 8;
230 avctx
->block_align
= get_le16(&hb
);
231 avctx
->bits_per_coded_sample
= get_le16(&hb
);
233 if (avctx
->bits_per_coded_sample
!= 16) {
234 av_log(avctx
, AV_LOG_ERROR
, "unsupported number of bits per sample\n");
240 av_log(avctx
, AV_LOG_INFO
, "%d header bytes unparsed\n", len
);
245 static int16_t * interleave_buffer(int16_t *samples
, int nchan
, int blocksize
, int32_t **buffer
) {
247 for (i
=0; i
<blocksize
; i
++)
248 for (chan
=0; chan
< nchan
; chan
++)
249 *samples
++ = FFMIN(buffer
[chan
][i
], 32768);
253 static void decode_subframe_lpc(ShortenContext
*s
, int channel
, int residual_size
, int pred_order
)
256 int coeffs
[pred_order
];
258 for (i
=0; i
<pred_order
; i
++)
259 coeffs
[i
] = get_sr_golomb_shorten(&s
->gb
, LPCQUANT
);
261 for (i
=0; i
< s
->blocksize
; i
++) {
263 for (j
=0; j
<pred_order
; j
++)
264 sum
+= coeffs
[j
] * s
->decoded
[channel
][i
-j
-1];
265 s
->decoded
[channel
][i
] = get_sr_golomb_shorten(&s
->gb
, residual_size
) + (sum
>> LPCQUANT
);
270 static int shorten_decode_frame(AVCodecContext
*avctx
,
271 void *data
, int *data_size
,
274 const uint8_t *buf
= avpkt
->data
;
275 int buf_size
= avpkt
->size
;
276 ShortenContext
*s
= avctx
->priv_data
;
277 int i
, input_buf_size
= 0;
278 int16_t *samples
= data
;
279 if(s
->max_framesize
== 0){
280 s
->max_framesize
= 1024; // should hopefully be enough for the first header
281 s
->bitstream
= av_fast_realloc(s
->bitstream
, &s
->allocated_bitstream_size
, s
->max_framesize
);
284 if(1 && s
->max_framesize
){//FIXME truncated
285 buf_size
= FFMIN(buf_size
, s
->max_framesize
- s
->bitstream_size
);
286 input_buf_size
= buf_size
;
288 if(s
->bitstream_index
+ s
->bitstream_size
+ buf_size
> s
->allocated_bitstream_size
){
289 // printf("memmove\n");
290 memmove(s
->bitstream
, &s
->bitstream
[s
->bitstream_index
], s
->bitstream_size
);
291 s
->bitstream_index
=0;
293 memcpy(&s
->bitstream
[s
->bitstream_index
+ s
->bitstream_size
], buf
, buf_size
);
294 buf
= &s
->bitstream
[s
->bitstream_index
];
295 buf_size
+= s
->bitstream_size
;
296 s
->bitstream_size
= buf_size
;
298 if(buf_size
< s
->max_framesize
){
299 //dprintf(avctx, "wanna more data ... %d\n", buf_size);
301 return input_buf_size
;
304 init_get_bits(&s
->gb
, buf
, buf_size
*8);
305 skip_bits(&s
->gb
, s
->bitindex
);
309 /* shorten signature */
310 if (get_bits_long(&s
->gb
, 32) != AV_RB32("ajkg")) {
311 av_log(s
->avctx
, AV_LOG_ERROR
, "missing shorten magic 'ajkg'\n");
316 s
->blocksize
= DEFAULT_BLOCK_SIZE
;
319 s
->version
= get_bits(&s
->gb
, 8);
320 s
->internal_ftype
= get_uint(s
, TYPESIZE
);
322 s
->channels
= get_uint(s
, CHANSIZE
);
323 if (s
->channels
> MAX_CHANNELS
) {
324 av_log(s
->avctx
, AV_LOG_ERROR
, "too many channels: %d\n", s
->channels
);
328 /* get blocksize if version > 0 */
329 if (s
->version
> 0) {
331 s
->blocksize
= get_uint(s
, av_log2(DEFAULT_BLOCK_SIZE
));
332 maxnlpc
= get_uint(s
, LPCQSIZE
);
333 s
->nmean
= get_uint(s
, 0);
335 skip_bytes
= get_uint(s
, NSKIPSIZE
);
336 for (i
=0; i
<skip_bytes
; i
++) {
337 skip_bits(&s
->gb
, 8);
340 s
->nwrap
= FFMAX(NWRAP
, maxnlpc
);
342 if (allocate_buffers(s
))
348 s
->lpcqoffset
= V2LPCQOFFSET
;
350 if (get_ur_golomb_shorten(&s
->gb
, FNSIZE
) != FN_VERBATIM
) {
351 av_log(s
->avctx
, AV_LOG_ERROR
, "missing verbatim section at beginning of stream\n");
355 s
->header_size
= get_ur_golomb_shorten(&s
->gb
, VERBATIM_CKSIZE_SIZE
);
356 if (s
->header_size
>= OUT_BUFFER_SIZE
|| s
->header_size
< CANONICAL_HEADER_SIZE
) {
357 av_log(s
->avctx
, AV_LOG_ERROR
, "header is wrong size: %d\n", s
->header_size
);
361 for (i
=0; i
<s
->header_size
; i
++)
362 s
->header
[i
] = (char)get_ur_golomb_shorten(&s
->gb
, VERBATIM_BYTE_SIZE
);
364 if (decode_wave_header(avctx
, s
->header
, s
->header_size
) < 0)
374 cmd
= get_ur_golomb_shorten(&s
->gb
, FNSIZE
);
383 int residual_size
= 0;
384 int channel
= s
->cur_chan
;
386 if (cmd
!= FN_ZERO
) {
387 residual_size
= get_ur_golomb_shorten(&s
->gb
, ENERGYSIZE
);
388 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
394 coffset
= s
->offset
[channel
][0];
396 int32_t sum
= (s
->version
< 2) ? 0 : s
->nmean
/ 2;
397 for (i
=0; i
<s
->nmean
; i
++)
398 sum
+= s
->offset
[channel
][i
];
399 coffset
= sum
/ s
->nmean
;
401 coffset
>>= FFMIN(1, s
->bitshift
);
405 for (i
=0; i
<s
->blocksize
; i
++)
406 s
->decoded
[channel
][i
] = 0;
409 for (i
=0; i
<s
->blocksize
; i
++)
410 s
->decoded
[channel
][i
] = get_sr_golomb_shorten(&s
->gb
, residual_size
) + coffset
;
413 for (i
=0; i
<s
->blocksize
; i
++)
414 s
->decoded
[channel
][i
] = get_sr_golomb_shorten(&s
->gb
, residual_size
) + s
->decoded
[channel
][i
- 1];
417 for (i
=0; i
<s
->blocksize
; i
++)
418 s
->decoded
[channel
][i
] = get_sr_golomb_shorten(&s
->gb
, residual_size
) + 2*s
->decoded
[channel
][i
-1]
419 - s
->decoded
[channel
][i
-2];
422 for (i
=0; i
<s
->blocksize
; i
++)
423 s
->decoded
[channel
][i
] = get_sr_golomb_shorten(&s
->gb
, residual_size
) + 3*s
->decoded
[channel
][i
-1]
424 - 3*s
->decoded
[channel
][i
-2]
425 + s
->decoded
[channel
][i
-3];
429 int pred_order
= get_ur_golomb_shorten(&s
->gb
, LPCQSIZE
);
430 for (i
=0; i
<pred_order
; i
++)
431 s
->decoded
[channel
][i
- pred_order
] -= coffset
;
432 decode_subframe_lpc(s
, channel
, residual_size
, pred_order
);
434 for (i
=0; i
< s
->blocksize
; i
++)
435 s
->decoded
[channel
][i
] += coffset
;
439 int32_t sum
= (s
->version
< 2) ? 0 : s
->blocksize
/ 2;
440 for (i
=0; i
<s
->blocksize
; i
++)
441 sum
+= s
->decoded
[channel
][i
];
443 for (i
=1; i
<s
->nmean
; i
++)
444 s
->offset
[channel
][i
-1] = s
->offset
[channel
][i
];
447 s
->offset
[channel
][s
->nmean
- 1] = sum
/ s
->blocksize
;
449 s
->offset
[channel
][s
->nmean
- 1] = (sum
/ s
->blocksize
) << s
->bitshift
;
451 for (i
=-s
->nwrap
; i
<0; i
++)
452 s
->decoded
[channel
][i
] = s
->decoded
[channel
][i
+ s
->blocksize
];
454 fix_bitshift(s
, s
->decoded
[channel
]);
457 if (s
->cur_chan
== s
->channels
) {
458 samples
= interleave_buffer(samples
, s
->channels
, s
->blocksize
, s
->decoded
);
466 len
= get_ur_golomb_shorten(&s
->gb
, VERBATIM_CKSIZE_SIZE
);
468 get_ur_golomb_shorten(&s
->gb
, VERBATIM_BYTE_SIZE
);
472 s
->bitshift
= get_ur_golomb_shorten(&s
->gb
, BITSHIFTSIZE
);
475 s
->blocksize
= get_uint(s
, av_log2(s
->blocksize
));
482 av_log(avctx
, AV_LOG_ERROR
, "unknown shorten function %d\n", cmd
);
488 *data_size
= (int8_t *)samples
- (int8_t *)data
;
490 // s->last_blocksize = s->blocksize;
491 s
->bitindex
= get_bits_count(&s
->gb
) - 8*((get_bits_count(&s
->gb
))/8);
492 i
= (get_bits_count(&s
->gb
))/8;
494 av_log(s
->avctx
, AV_LOG_ERROR
, "overread: %d\n", i
- buf_size
);
496 s
->bitstream_index
=0;
499 if (s
->bitstream_size
) {
500 s
->bitstream_index
+= i
;
501 s
->bitstream_size
-= i
;
502 return input_buf_size
;
507 static av_cold
int shorten_decode_close(AVCodecContext
*avctx
)
509 ShortenContext
*s
= avctx
->priv_data
;
512 for (i
= 0; i
< s
->channels
; i
++) {
513 s
->decoded
[i
] -= s
->nwrap
;
514 av_freep(&s
->decoded
[i
]);
515 av_freep(&s
->offset
[i
]);
517 av_freep(&s
->bitstream
);
521 static void shorten_flush(AVCodecContext
*avctx
){
522 ShortenContext
*s
= avctx
->priv_data
;
525 s
->bitstream_index
= 0;
528 AVCodec shorten_decoder
= {
532 sizeof(ShortenContext
),
535 shorten_decode_close
,
536 shorten_decode_frame
,
537 .flush
= shorten_flush
,
538 .long_name
= NULL_IF_CONFIG_SMALL("Shorten"),