FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bmvaudio.c
Go to the documentation of this file.
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 
23 #include "libavutil/common.h"
24 
25 #include "avcodec.h"
26 #include "internal.h"
27 
28 static const int bmv_aud_mults[16] = {
29  16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
30 };
31 
33 {
34  avctx->channels = 2;
37 
38  return 0;
39 }
40 
41 static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data,
42  int *got_frame_ptr, AVPacket *avpkt)
43 {
44  AVFrame *frame = data;
45  const uint8_t *buf = avpkt->data;
46  int buf_size = avpkt->size;
47  int blocks = 0, total_blocks, i;
48  int ret;
49  int16_t *output_samples;
50  int scale[2];
51 
52  total_blocks = *buf++;
53  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  frame->nb_samples = total_blocks * 32;
61  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
62  return ret;
63  output_samples = (int16_t *)frame->data[0];
64 
65  for (blocks = 0; blocks < total_blocks; blocks++) {
66  uint8_t code = *buf++;
67  code = (code >> 1) | (code << 7);
68  scale[0] = bmv_aud_mults[code & 0xF];
69  scale[1] = bmv_aud_mults[code >> 4];
70  for (i = 0; i < 32; i++) {
71  *output_samples++ = av_clip_int16((scale[0] * (int8_t)*buf++) >> 5);
72  *output_samples++ = av_clip_int16((scale[1] * (int8_t)*buf++) >> 5);
73  }
74  }
75 
76  *got_frame_ptr = 1;
77 
78  return buf_size;
79 }
80 
82  .name = "bmv_audio",
83  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV audio"),
84  .type = AVMEDIA_TYPE_AUDIO,
86  .init = bmv_aud_decode_init,
87  .decode = bmv_aud_decode_frame,
88  .capabilities = AV_CODEC_CAP_DR1,
89 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: avcodec.h:1581
AVCodec ff_bmv_audio_decoder
Definition: bmvaudio.c:81
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:3542
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2418
uint8_t
#define av_cold
Definition: attributes.h:82
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1580
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * name
Name of the codec implementation.
Definition: avcodec.h:3549
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2461
audio channel layout utility functions
static const int bmv_aud_mults[16]
Definition: bmvaudio.c:28
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1649
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
void * buf
Definition: avisynth_c.h:553
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
common internal api header.
common internal and external API header
signed 16 bits
Definition: samplefmt.h:61
static int bmv_aud_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: bmvaudio.c:41
int channels
number of audio channels
Definition: avcodec.h:2411
static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
Definition: bmvaudio.c:32
This structure stores compressed data.
Definition: avcodec.h:1557
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
for(j=16;j >0;--j)