avformat/mxfdec: Check edit unit for overflow in mxf_set_current_edit_unit()
[FFMpeg-mirror.git] / libavformat / adxdec.c
blob0909884f197f68dd85ac52cca7ccbcf217fbe7eb
1 /*
2 * Copyright (c) 2011 Justin Ruggles
4 * This file is part of FFmpeg.
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.
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.
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
21 /**
22 * @file
23 * CRI ADX demuxer
26 #include "libavutil/intreadwrite.h"
27 #include "avformat.h"
28 #include "demux.h"
29 #include "internal.h"
31 #define BLOCK_SIZE 18
32 #define BLOCK_SAMPLES 32
34 typedef struct ADXDemuxerContext {
35 int header_size;
36 } ADXDemuxerContext;
38 static int adx_probe(const AVProbeData *p)
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;
51 static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
53 ADXDemuxerContext *c = s->priv_data;
54 AVCodecParameters *par = s->streams[0]->codecpar;
55 int ret, size;
57 if (avio_feof(s->pb))
58 return AVERROR_EOF;
60 size = BLOCK_SIZE * par->ch_layout.nb_channels;
62 pkt->pos = avio_tell(s->pb);
63 pkt->stream_index = 0;
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);
70 av_shrink_packet(pkt, size);
71 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
72 } else if (ret < size) {
73 return AVERROR(EIO);
74 } else {
75 size = ret;
78 pkt->duration = size / (BLOCK_SIZE * par->ch_layout.nb_channels);
79 pkt->pts = (pkt->pos - c->header_size) / (BLOCK_SIZE * par->ch_layout.nb_channels);
81 return 0;
84 static int adx_read_header(AVFormatContext *s)
86 ADXDemuxerContext *c = s->priv_data;
87 AVCodecParameters *par;
88 int ret;
89 int channels;
91 AVStream *st = avformat_new_stream(s, NULL);
92 if (!st)
93 return AVERROR(ENOMEM);
94 par = s->streams[0]->codecpar;
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);
101 if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
102 return ret;
104 if (par->extradata_size < 12) {
105 av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
106 return AVERROR_INVALIDDATA;
108 channels = AV_RB8 (par->extradata + 7);
109 par->sample_rate = AV_RB32(par->extradata + 8);
111 if (channels <= 0) {
112 av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", channels);
113 return AVERROR_INVALIDDATA;
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;
121 par->ch_layout.nb_channels = channels;
122 par->codec_type = AVMEDIA_TYPE_AUDIO;
123 par->codec_id = AV_CODEC_ID_ADPCM_ADX;
124 par->bit_rate = (int64_t)par->sample_rate * par->ch_layout.nb_channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
126 avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
128 return 0;
131 const FFInputFormat ff_adx_demuxer = {
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),
138 .read_header = adx_read_header,
139 .read_packet = adx_read_packet,
140 .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,