FFmpeg
adxdec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Justin Ruggles
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * CRI ADX demuxer
24  */
25 
26 #include "libavutil/intreadwrite.h"
27 #include "avformat.h"
28 #include "demux.h"
29 #include "internal.h"
30 
31 #define BLOCK_SIZE 18
32 #define BLOCK_SAMPLES 32
33 
34 typedef struct ADXDemuxerContext {
37 
38 static int adx_probe(const AVProbeData *p)
39 {
40  int offset;
41  if (AV_RB16(p->buf) != 0x8000)
42  return 0;
43  offset = AV_RB16(&p->buf[2]);
44  if ( offset < 8
45  || offset > p->buf_size - 4
46  || memcmp(p->buf + offset - 2, "(c)CRI", 6))
47  return 0;
48  return AVPROBE_SCORE_MAX * 3 / 4;
49 }
50 
52 {
53  ADXDemuxerContext *c = s->priv_data;
54  AVCodecParameters *par = s->streams[0]->codecpar;
55  int ret, size;
56 
57  if (avio_feof(s->pb))
58  return AVERROR_EOF;
59 
61 
62  pkt->pos = avio_tell(s->pb);
63  pkt->stream_index = 0;
64 
65  ret = av_get_packet(s->pb, pkt, size * 128);
66  if (ret < 0)
67  return ret;
68  if ((ret % size) && ret >= size) {
69  size = ret - (ret % size);
72  } else if (ret < size) {
73  return AVERROR(EIO);
74  } else {
75  size = ret;
76  }
77 
79  pkt->pts = (pkt->pos - c->header_size) / (BLOCK_SIZE * par->ch_layout.nb_channels);
80 
81  return 0;
82 }
83 
85 {
86  ADXDemuxerContext *c = s->priv_data;
87  AVCodecParameters *par;
88  int ret;
89  int channels;
90 
92  if (!st)
93  return AVERROR(ENOMEM);
94  par = s->streams[0]->codecpar;
95 
96  if (avio_rb16(s->pb) != 0x8000)
97  return AVERROR_INVALIDDATA;
98  c->header_size = avio_rb16(s->pb) + 4;
99  avio_seek(s->pb, -4, SEEK_CUR);
100 
101  if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
102  return ret;
103 
104  if (par->extradata_size < 12) {
105  av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
106  return AVERROR_INVALIDDATA;
107  }
108  channels = AV_RB8 (par->extradata + 7);
109  par->sample_rate = AV_RB32(par->extradata + 8);
110 
111  if (channels <= 0) {
112  av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", channels);
113  return AVERROR_INVALIDDATA;
114  }
115 
116  if (par->sample_rate <= 0) {
117  av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
118  return AVERROR_INVALIDDATA;
119  }
120 
125 
127 
128  return 0;
129 }
130 
132  .p.name = "adx",
133  .p.long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
134  .p.extensions = "adx",
135  .p.flags = AVFMT_GENERIC_INDEX,
136  .read_probe = adx_probe,
137  .priv_data_size = sizeof(ADXDemuxerContext),
140  .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
141 };
adx_read_packet
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: adxdec.c:51
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
ff_adx_demuxer
const FFInputFormat ff_adx_demuxer
Definition: adxdec.c:131
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
BLOCK_SAMPLES
#define BLOCK_SAMPLES
Definition: adxdec.c:32
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
ff_get_extradata
int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: demux_utils.c:335
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
ADXDemuxerContext::header_size
int header_size
Definition: adxdec.c:35
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:113
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:578
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
BLOCK_SIZE
#define BLOCK_SIZE
Definition: adxdec.c:31
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
adx_read_header
static int adx_read_header(AVFormatContext *s)
Definition: adxdec.c:84
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
channels
channels
Definition: aptx.h:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
AV_CODEC_ID_ADPCM_ADX
@ AV_CODEC_ID_ADPCM_ADX
Definition: codec_id.h:376
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
adx_probe
static int adx_probe(const AVProbeData *p)
Definition: adxdec.c:38
size
int size
Definition: twinvq_data.h:10344
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
ADXDemuxerContext
Definition: adxdec.c:34
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
demux.h
av_get_packet
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:103
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:745
avformat.h
AV_RB8
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL AV_RB8
Definition: bytestream.h:99
AVPacket::stream_index
int stream_index
Definition: packet.h:524
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:542
FFInputFormat
Definition: demux.h:37
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345