]> git.sesse.net Git - ffmpeg/blob - libavformat/adxdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / adxdec.c
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
21 /**
22  * @file
23  * CRI ADX demuxer
24  */
25
26 #include "libavutil/intreadwrite.h"
27 #include "libavcodec/adx.h"
28 #include "avformat.h"
29
30 #define BLOCK_SIZE    18
31 #define BLOCK_SAMPLES 32
32
33 typedef struct ADXDemuxerContext {
34     int header_size;
35 } ADXDemuxerContext;
36
37 static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
38 {
39     ADXDemuxerContext *c = s->priv_data;
40     AVCodecContext *avctx = s->streams[0]->codec;
41     int ret, size;
42
43     size = BLOCK_SIZE * avctx->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_free_packet(pkt);
51         return ret < 0 ? ret : AVERROR(EIO);
52     }
53     if (AV_RB16(pkt->data) & 0x8000) {
54         av_free_packet(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
64 static int adx_read_header(AVFormatContext *s, AVFormatParameters *ap)
65 {
66     ADXDemuxerContext *c = s->priv_data;
67     AVCodecContext *avctx;
68     int ret;
69
70     AVStream *st = avformat_new_stream(s, NULL);
71     if (!st)
72         return AVERROR(ENOMEM);
73     avctx = s->streams[0]->codec;
74
75     if (avio_rb16(s->pb) != 0x8000)
76         return AVERROR_INVALIDDATA;
77     c->header_size = avio_rb16(s->pb) + 4;
78     avio_seek(s->pb, -4, SEEK_CUR);
79
80     avctx->extradata = av_mallocz(c->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
81     if (!avctx->extradata)
82         return AVERROR(ENOMEM);
83     if (avio_read(s->pb, avctx->extradata, c->header_size) < c->header_size) {
84         av_freep(&avctx->extradata);
85         return AVERROR(EIO);
86     }
87     avctx->extradata_size = c->header_size;
88
89     ret = avpriv_adx_decode_header(avctx, avctx->extradata,
90                                    avctx->extradata_size, &c->header_size,
91                                    NULL);
92     if (ret)
93         return ret;
94
95     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
96     st->codec->codec_id    = s->iformat->value;
97
98     av_set_pts_info(st, 64, BLOCK_SAMPLES, avctx->sample_rate);
99
100     return 0;
101 }
102
103 AVInputFormat ff_adx_demuxer = {
104     .name           = "adx",
105     .long_name      = NULL_IF_CONFIG_SMALL("CRI ADX"),
106     .priv_data_size = sizeof(ADXDemuxerContext),
107     .read_header    = adx_read_header,
108     .read_packet    = adx_read_packet,
109     .extensions     = "adx",
110     .value          = CODEC_ID_ADPCM_ADX,
111 };