| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Discworld II BMV audio decoder | ||
| 3 | * Copyright (c) 2011 Konstantin Shishkov | ||
| 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/channel_layout.h" | ||
| 23 | #include "libavutil/common.h" | ||
| 24 | |||
| 25 | #include "avcodec.h" | ||
| 26 | #include "codec_internal.h" | ||
| 27 | #include "decode.h" | ||
| 28 | |||
| 29 | static const int bmv_aud_mults[16] = { | ||
| 30 | 16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32 | ||
| 31 | }; | ||
| 32 | |||
| 33 | 3 | static av_cold int bmv_aud_decode_init(AVCodecContext *avctx) | |
| 34 | { | ||
| 35 | 3 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 36 | 3 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
| 37 | 3 | avctx->sample_fmt = AV_SAMPLE_FMT_S16; | |
| 38 | |||
| 39 | 3 | return 0; | |
| 40 | } | ||
| 41 | |||
| 42 | 23 | static int bmv_aud_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 43 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 44 | { | ||
| 45 | 23 | const uint8_t *buf = avpkt->data; | |
| 46 | 23 | int buf_size = avpkt->size; | |
| 47 | 23 | int blocks = 0, total_blocks, i; | |
| 48 | int ret; | ||
| 49 | int16_t *output_samples; | ||
| 50 | int scale[2]; | ||
| 51 | |||
| 52 | 23 | total_blocks = *buf++; | |
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (buf_size < total_blocks * 65 + 1) { |
| 54 | ✗ | av_log(avctx, AV_LOG_ERROR, "expected %d bytes, got %d\n", | |
| 55 | ✗ | total_blocks * 65 + 1, buf_size); | |
| 56 | ✗ | return AVERROR_INVALIDDATA; | |
| 57 | } | ||
| 58 | |||
| 59 | /* get output buffer */ | ||
| 60 | 23 | frame->nb_samples = total_blocks * 32; | |
| 61 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 62 | ✗ | return ret; | |
| 63 | 23 | output_samples = (int16_t *)frame->data[0]; | |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 1322 times.
✓ Branch 1 taken 23 times.
|
1345 | for (blocks = 0; blocks < total_blocks; blocks++) { |
| 66 | 1322 | uint8_t code = *buf++; | |
| 67 | 1322 | code = (code >> 1) | (code << 7); | |
| 68 | 1322 | scale[0] = bmv_aud_mults[code & 0xF]; | |
| 69 | 1322 | scale[1] = bmv_aud_mults[code >> 4]; | |
| 70 |
2/2✓ Branch 0 taken 42304 times.
✓ Branch 1 taken 1322 times.
|
43626 | for (i = 0; i < 32; i++) { |
| 71 | 42304 | *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5); | |
| 72 | 42304 | *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5); | |
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 23 | *got_frame_ptr = 1; | |
| 77 | |||
| 78 | 23 | return buf_size; | |
| 79 | } | ||
| 80 | |||
| 81 | const FFCodec ff_bmv_audio_decoder = { | ||
| 82 | .p.name = "bmv_audio", | ||
| 83 | CODEC_LONG_NAME("Discworld II BMV audio"), | ||
| 84 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 85 | .p.id = AV_CODEC_ID_BMV_AUDIO, | ||
| 86 | .init = bmv_aud_decode_init, | ||
| 87 | FF_CODEC_DECODE_CB(bmv_aud_decode_frame), | ||
| 88 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, | ||
| 89 | }; | ||
| 90 |