Libav
adxdec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Justin Ruggles
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include "libavutil/intreadwrite.h"
27 #include "avformat.h"
28 #include "internal.h"
29 
30 #define BLOCK_SIZE 18
31 #define BLOCK_SAMPLES 32
32 
33 typedef struct ADXDemuxerContext {
36 
38 {
40  AVCodecParameters *par = s->streams[0]->codecpar;
41  int ret, size;
42 
43  size = BLOCK_SIZE * par->channels;
44 
45  pkt->pos = avio_tell(s->pb);
46  pkt->stream_index = 0;
47 
48  ret = av_get_packet(s->pb, pkt, size);
49  if (ret != size) {
50  av_packet_unref(pkt);
51  return ret < 0 ? ret : AVERROR(EIO);
52  }
53  if (AV_RB16(pkt->data) & 0x8000) {
54  av_packet_unref(pkt);
55  return AVERROR_EOF;
56  }
57  pkt->size = size;
58  pkt->duration = 1;
59  pkt->pts = (pkt->pos - c->header_size) / size;
60 
61  return 0;
62 }
63 
65 {
67  AVCodecParameters *par;
68 
70  if (!st)
71  return AVERROR(ENOMEM);
72  par = s->streams[0]->codecpar;
73 
74  if (avio_rb16(s->pb) != 0x8000)
75  return AVERROR_INVALIDDATA;
76  c->header_size = avio_rb16(s->pb) + 4;
77  avio_seek(s->pb, -4, SEEK_CUR);
78 
80  if (!par->extradata)
81  return AVERROR(ENOMEM);
82  if (avio_read(s->pb, par->extradata, c->header_size) < c->header_size) {
83  av_freep(&par->extradata);
84  return AVERROR(EIO);
85  }
86  par->extradata_size = c->header_size;
87 
88  if (par->extradata_size < 12) {
89  av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
90  return AVERROR_INVALIDDATA;
91  }
92  par->channels = AV_RB8 (par->extradata + 7);
93  par->sample_rate = AV_RB32(par->extradata + 8);
94 
95  if (par->channels <= 0) {
96  av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
97  return AVERROR_INVALIDDATA;
98  }
99 
101  par->codec_id = s->iformat->raw_codec_id;
102 
104 
105  return 0;
106 }
107 
109  .name = "adx",
110  .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
111  .priv_data_size = sizeof(ADXDemuxerContext),
114  .extensions = "adx",
115  .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
117 };
#define AV_RB8(x)
Definition: intreadwrite.h:368
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: adxdec.c:37
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:681
#define BLOCK_SAMPLES
Definition: adxdec.c:31
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
#define AV_RB32
Definition: intreadwrite.h:130
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
#define AVERROR_EOF
End of file.
Definition: error.h:51
static int adx_read_header(AVFormatContext *s)
Definition: adxdec.c:64
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define BLOCK_SIZE
Definition: adxdec.c:30
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:421
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:575
int sample_rate
Audio only.
Definition: avcodec.h:3564
Main libavformat public API header.
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:952
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
void * priv_data
Format private data.
Definition: avformat.h:968
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int channels
Audio only.
Definition: avcodec.h:3560
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
AVInputFormat ff_adx_demuxer
Definition: adxdec.c:108
This structure stores compressed data.
Definition: avcodec.h:1323
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339