3 * Copyright (c) 2016 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
26 static int aix_probe(AVProbeData *p)
28 if (AV_RL32(p->buf) != MKTAG('A','I','X','F') ||
29 AV_RB32(p->buf + 8) != 0x01000014 ||
30 AV_RB32(p->buf + 12) != 0x00000800)
33 return AVPROBE_SCORE_MAX;
36 static int aix_read_header(AVFormatContext *s)
38 unsigned nb_streams, first_offset, nb_segments;
39 unsigned stream_list_offset;
40 unsigned segment_list_offset = 0x20;
41 unsigned segment_list_entry_size = 0x10;
46 first_offset = avio_rb32(s->pb) + 8;
48 nb_segments = avio_rb16(s->pb);
50 return AVERROR_INVALIDDATA;
51 stream_list_offset = segment_list_offset + segment_list_entry_size * nb_segments + 0x10;
52 if (stream_list_offset >= first_offset)
53 return AVERROR_INVALIDDATA;
54 avio_seek(s->pb, stream_list_offset, SEEK_SET);
55 nb_streams = avio_r8(s->pb);
57 return AVERROR_INVALIDDATA;
59 for (i = 0; i < nb_streams; i++) {
60 AVStream *st = avformat_new_stream(s, NULL);
63 return AVERROR(ENOMEM);
64 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
65 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ADX;
66 st->codecpar->sample_rate = avio_rb32(s->pb);
67 st->codecpar->channels = avio_r8(s->pb);
68 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
72 avio_seek(s->pb, first_offset, SEEK_SET);
73 for (i = 0; i < nb_streams; i++) {
74 if (avio_rl32(s->pb) != MKTAG('A','I','X','P'))
75 return AVERROR_INVALIDDATA;
76 size = avio_rb32(s->pb);
78 return AVERROR_INVALIDDATA;
80 ff_get_extradata(s, s->streams[i]->codecpar, s->pb, size - 8);
86 static int aix_read_packet(AVFormatContext *s, AVPacket *pkt)
88 unsigned size, index, duration, chunk;
92 pos = avio_tell(s->pb);
95 chunk = avio_rl32(s->pb);
96 size = avio_rb32(s->pb);
97 if (chunk == MKTAG('A','I','X','E')) {
98 avio_skip(s->pb, size);
99 for (i = 0; i < s->nb_streams; i++) {
100 if (avio_feof(s->pb))
102 chunk = avio_rl32(s->pb);
103 size = avio_rb32(s->pb);
104 avio_skip(s->pb, size);
106 pos = avio_tell(s->pb);
107 chunk = avio_rl32(s->pb);
108 size = avio_rb32(s->pb);
111 if (chunk != MKTAG('A','I','X','P'))
112 return AVERROR_INVALIDDATA;
114 return AVERROR_INVALIDDATA;
115 index = avio_r8(s->pb);
116 if (avio_r8(s->pb) != s->nb_streams || index >= s->nb_streams)
117 return AVERROR_INVALIDDATA;
118 duration = avio_rb16(s->pb);
119 sequence = avio_rb32(s->pb);
121 avio_skip(s->pb, size - 8);
125 ret = av_get_packet(s->pb, pkt, size - 8);
126 pkt->stream_index = index;
127 pkt->duration = duration;
132 AVInputFormat ff_aix_demuxer = {
134 .long_name = NULL_IF_CONFIG_SMALL("CRI AIX"),
135 .read_probe = aix_probe,
136 .read_header = aix_read_header,
137 .read_packet = aix_read_packet,
139 .flags = AVFMT_GENERIC_INDEX,