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 
38 {
40  AVCodecContext *avctx = s->streams[0]->codec;
41  int ret, size;
42 
43  if (avctx->channels <= 0) {
44  av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", avctx->channels);
45  return AVERROR_INVALIDDATA;
46  }
47 
48  size = BLOCK_SIZE * avctx->channels;
49 
50  pkt->pos = avio_tell(s->pb);
51  pkt->stream_index = 0;
52 
53  ret = av_get_packet(s->pb, pkt, size);
54  if (ret != size) {
55  av_free_packet(pkt);
56  return ret < 0 ? ret : AVERROR(EIO);
57  }
58  if (AV_RB16(pkt->data) & 0x8000) {
59  av_free_packet(pkt);
60  return AVERROR_EOF;
61  }
62  pkt->size = size;
63  pkt->duration = 1;
64  pkt->pts = (pkt->pos - c->header_size) / size;
65 
66  return 0;
67 }
68 
70 {
72  AVCodecContext *avctx;
73 
75  if (!st)
76  return AVERROR(ENOMEM);
77  avctx = s->streams[0]->codec;
78 
79  if (avio_rb16(s->pb) != 0x8000)
80  return AVERROR_INVALIDDATA;
81  c->header_size = avio_rb16(s->pb) + 4;
82  avio_seek(s->pb, -4, SEEK_CUR);
83 
84  if (ff_get_extradata(avctx, s->pb, c->header_size) < 0)
85  return AVERROR(ENOMEM);
86 
87  if (avctx->extradata_size < 12) {
88  av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
89  return AVERROR_INVALIDDATA;
90  }
91  avctx->channels = AV_RB8(avctx->extradata + 7);
92  avctx->sample_rate = AV_RB32(avctx->extradata + 8);
93 
94  if (avctx->channels <= 0) {
95  av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", avctx->channels);
96  return AVERROR_INVALIDDATA;
97  }
98 
100  st->codec->codec_id = s->iformat->raw_codec_id;
101 
103 
104  return 0;
105 }
106 
108  .name = "adx",
109  .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
110  .priv_data_size = sizeof(ADXDemuxerContext),
113  .extensions = "adx",
114  .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
116 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1187
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:4006
int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
Allocate extradata with additional FF_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:2885
int size
Definition: avcodec.h:1163
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:203
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:674
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:85
#define BLOCK_SAMPLES
Definition: adxdec.c:31
Format I/O context.
Definition: avformat.h:1272
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1355
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3672
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:85
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
uint8_t * data
Definition: avcodec.h:1162
#define AVERROR_EOF
End of file.
Definition: error.h:55
static int adx_read_header(AVFormatContext *s)
Definition: adxdec.c:69
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:241
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:365
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1180
#define av_log(a,...)
#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:175
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
ret
Definition: avfilter.c:974
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:620
Stream structure.
Definition: avformat.h:842
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:85
enum AVMediaType codec_type
Definition: avcodec.h:1249
enum AVCodecID codec_id
Definition: avcodec.h:1258
int sample_rate
samples per second
Definition: avcodec.h:1985
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
main external API structure.
Definition: avcodec.h:1241
int extradata_size
Definition: avcodec.h:1356
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:473
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:674
static int flags
Definition: cpu.c:47
Main libavformat public API header.
static double c[64]
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1284
int channels
number of audio channels
Definition: avcodec.h:1986
void * priv_data
Format private data.
Definition: avformat.h:1300
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int stream_index
Definition: avcodec.h:1164
AVInputFormat ff_adx_demuxer
Definition: adxdec.c:107
This structure stores compressed data.
Definition: avcodec.h:1139
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155