1 /*
2 * AIX demuxer
3 * Copyright (c) 2016 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25
aix_probe(const AVProbeData * p)26 static int aix_probe(const AVProbeData *p)
27 {
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)
31 return 0;
32
33 return AVPROBE_SCORE_MAX;
34 }
35
aix_read_header(AVFormatContext * s)36 static int aix_read_header(AVFormatContext *s)
37 {
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;
42 unsigned size;
43 int i;
44
45 avio_skip(s->pb, 4);
46 first_offset = avio_rb32(s->pb) + 8;
47 avio_skip(s->pb, 16);
48 nb_segments = avio_rb16(s->pb);
49 if (nb_segments == 0)
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);
56 if (nb_streams == 0)
57 return AVERROR_INVALIDDATA;
58 avio_skip(s->pb, 7);
59 for (i = 0; i < nb_streams; i++) {
60 AVStream *st = avformat_new_stream(s, NULL);
61
62 if (!st)
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);
69 avio_skip(s->pb, 3);
70 }
71
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);
77 if (size <= 8)
78 return AVERROR_INVALIDDATA;
79 avio_skip(s->pb, 8);
80 ff_get_extradata(s, s->streams[i]->codecpar, s->pb, size - 8);
81 }
82
83 return 0;
84 }
85
aix_read_packet(AVFormatContext * s,AVPacket * pkt)86 static int aix_read_packet(AVFormatContext *s, AVPacket *pkt)
87 {
88 unsigned size, index, duration, chunk;
89 int64_t pos;
90 int sequence, ret, i;
91
92 pos = avio_tell(s->pb);
93 if (avio_feof(s->pb))
94 return AVERROR_EOF;
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))
101 return AVERROR_EOF;
102 chunk = avio_rl32(s->pb);
103 size = avio_rb32(s->pb);
104 avio_skip(s->pb, size);
105 }
106 pos = avio_tell(s->pb);
107 chunk = avio_rl32(s->pb);
108 size = avio_rb32(s->pb);
109 }
110
111 if (chunk != MKTAG('A','I','X','P'))
112 return AVERROR_INVALIDDATA;
113 if (size <= 8)
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);
120 if (sequence < 0) {
121 avio_skip(s->pb, size - 8);
122 return 0;
123 }
124
125 ret = av_get_packet(s->pb, pkt, size - 8);
126 pkt->stream_index = index;
127 pkt->duration = duration;
128 pkt->pos = pos;
129 return ret;
130 }
131
132 AVInputFormat ff_aix_demuxer = {
133 .name = "aix",
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,
138 .extensions = "aix",
139 .flags = AVFMT_GENERIC_INDEX,
140 };
141