Libav
pafaudio.c
Go to the documentation of this file.
1 /*
2  * Packed Animation File audio decoder
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 
24 #include "avcodec.h"
25 #include "internal.h"
26 #include "mathops.h"
27 
28 #define PAF_SOUND_SAMPLES 2205
29 #define PAF_SOUND_FRAME_SIZE ((256 + PAF_SOUND_SAMPLES) * 2)
30 
32 {
33  if (avctx->channels != 2) {
34  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
35  return AVERROR_INVALIDDATA;
36  }
37 
40 
41  return 0;
42 }
43 
44 static int paf_audio_decode(AVCodecContext *avctx, void *data,
45  int *got_frame, AVPacket *pkt)
46 {
47  AVFrame *frame = data;
48  int16_t *output_samples;
49  const uint8_t *src = pkt->data;
50  int frames, ret, i, j;
51  int16_t cb[256];
52 
53  frames = pkt->size / PAF_SOUND_FRAME_SIZE;
54  if (frames < 1)
55  return AVERROR_INVALIDDATA;
56 
57  frame->nb_samples = PAF_SOUND_SAMPLES * frames;
58  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
59  return ret;
60 
61  output_samples = (int16_t *)frame->data[0];
62  // codebook of 256 16-bit samples and 8-bit indices to it
63  for (j = 0; j < frames; j++) {
64  for (i = 0; i < 256; i++)
65  cb[i] = sign_extend(AV_RL16(src + i * 2), 16);
66  src += 256 * 2;
67  // always 2 channels
68  for (i = 0; i < PAF_SOUND_SAMPLES * 2; i++)
69  *output_samples++ = cb[*src++];
70  }
71  *got_frame = 1;
72 
73  return pkt->size;
74 }
75 
77  .name = "paf_audio",
78  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
79  .type = AVMEDIA_TYPE_AUDIO,
81  .init = paf_audio_init,
82  .decode = paf_audio_decode,
83  .capabilities = CODEC_CAP_DR1,
84 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
#define AV_RL16(x)
Definition: intreadwrite.h:220
int size
Definition: avcodec.h:974
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:2796
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
uint8_t
static int paf_audio_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
Definition: pafaudio.c:44
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define PAF_SOUND_FRAME_SIZE
Definition: pafaudio.c:29
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
#define av_cold
Definition: attributes.h:66
main external API structure.
Definition: avcodec.h:1050
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
uint8_t * data
Definition: avcodec.h:973
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:127
AVCodec ff_paf_audio_decoder
Definition: pafaudio.c:76
static av_cold int paf_audio_init(AVCodecContext *avctx)
Definition: pafaudio.c:31
common internal api header.
signed 16 bits
Definition: samplefmt.h:64
int channels
number of audio channels
Definition: avcodec.h:1792
#define PAF_SOUND_SAMPLES
Definition: pafaudio.c:28
This structure stores compressed data.
Definition: avcodec.h:950
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
for(j=16;j >0;--j)