FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
29 
30 #define BLOCK_SIZE 18
31 #define BLOCK_SAMPLES 32
32 
33 typedef struct ADXDemuxerContext {
36 
37 static int adx_probe(AVProbeData *p)
38 {
39  int offset;
40  if (AV_RB16(p->buf) != 0x8000)
41  return 0;
42  offset = AV_RB16(&p->buf[2]);
43  if ( offset < 8
44  || offset > p->buf_size - 4
45  || memcmp(p->buf + offset - 2, "(c)CRI", 6))
46  return 0;
47  return AVPROBE_SCORE_MAX * 3 / 4;
48 }
49 
51 {
53  AVCodecParameters *par = s->streams[0]->codecpar;
54  int ret, size;
55 
56  if (par->channels <= 0) {
57  av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
58  return AVERROR_INVALIDDATA;
59  }
60 
61  size = BLOCK_SIZE * par->channels;
62 
63  pkt->pos = avio_tell(s->pb);
64  pkt->stream_index = 0;
65 
66  ret = av_get_packet(s->pb, pkt, size);
67  if (ret != size) {
68  av_packet_unref(pkt);
69  return ret < 0 ? ret : AVERROR(EIO);
70  }
71  if (AV_RB16(pkt->data) & 0x8000) {
72  av_packet_unref(pkt);
73  return AVERROR_EOF;
74  }
75  pkt->size = size;
76  pkt->duration = 1;
77  pkt->pts = (pkt->pos - c->header_size) / size;
78 
79  return 0;
80 }
81 
83 {
85  AVCodecParameters *par;
86 
88  if (!st)
89  return AVERROR(ENOMEM);
90  par = s->streams[0]->codecpar;
91 
92  if (avio_rb16(s->pb) != 0x8000)
93  return AVERROR_INVALIDDATA;
94  c->header_size = avio_rb16(s->pb) + 4;
95  avio_seek(s->pb, -4, SEEK_CUR);
96 
97  if (ff_get_extradata(s, par, s->pb, c->header_size) < 0)
98  return AVERROR(ENOMEM);
99 
100  if (par->extradata_size < 12) {
101  av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
102  return AVERROR_INVALIDDATA;
103  }
104  par->channels = AV_RB8 (par->extradata + 7);
105  par->sample_rate = AV_RB32(par->extradata + 8);
106 
107  if (par->channels <= 0) {
108  av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
109  return AVERROR_INVALIDDATA;
110  }
111 
112  if (par->sample_rate <= 0) {
113  av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
114  return AVERROR_INVALIDDATA;
115  }
116 
118  par->codec_id = s->iformat->raw_codec_id;
119  par->bit_rate = (int64_t)par->sample_rate * par->channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
120 
122 
123  return 0;
124 }
125 
127  .name = "adx",
128  .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
129  .read_probe = adx_probe,
130  .priv_data_size = sizeof(ADXDemuxerContext),
133  .extensions = "adx",
134  .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
136 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1677
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: adxdec.c:50
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:4601
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int size
Definition: avcodec.h:1658
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:754
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:87
#define BLOCK_SAMPLES
Definition: adxdec.c:31
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4058
Format I/O context.
Definition: avformat.h:1349
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1675
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4231
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:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
static int adx_read_header(AVFormatContext *s)
Definition: adxdec.c:82
static int adx_probe(AVProbeData *p)
Definition: adxdec.c:37
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:289
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:525
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4095
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define BLOCK_SIZE
Definition: adxdec.c:30
#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:179
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4062
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4084
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:510
Stream structure.
Definition: avformat.h:889
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
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:87
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:589
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:487
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:710
int sample_rate
Audio only.
Definition: avcodec.h:4176
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
Main libavformat public API header.
static double c[64]
int ff_get_extradata(AVFormatContext *s, 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: utils.c:3234
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1361
void * priv_data
Format private data.
Definition: avformat.h:1377
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4080
int channels
Audio only.
Definition: avcodec.h:4172
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int stream_index
Definition: avcodec.h:1659
AVInputFormat ff_adx_demuxer
Definition: adxdec.c:126
This structure stores compressed data.
Definition: avcodec.h:1634
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1650