Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * CDXL demuxer | ||
3 | * Copyright (c) 2011-2012 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/channel_layout.h" | ||
23 | #include "libavutil/intreadwrite.h" | ||
24 | #include "libavutil/parseutils.h" | ||
25 | #include "libavutil/opt.h" | ||
26 | #include "avformat.h" | ||
27 | #include "demux.h" | ||
28 | #include "internal.h" | ||
29 | |||
30 | #define CDXL_HEADER_SIZE 32 | ||
31 | |||
32 | typedef struct CDXLDemuxContext { | ||
33 | AVClass *class; | ||
34 | int read_chunk; | ||
35 | AVRational frate; | ||
36 | int srate; | ||
37 | AVRational frame_rate; | ||
38 | int sample_rate; | ||
39 | uint8_t header[CDXL_HEADER_SIZE]; | ||
40 | int video_stream_index; | ||
41 | int audio_stream_index; | ||
42 | int64_t filesize; | ||
43 | int64_t pos; | ||
44 | } CDXLDemuxContext; | ||
45 | |||
46 | 7186 | static int cdxl_read_probe(const AVProbeData *p) | |
47 | { | ||
48 | 7186 | int score = AVPROBE_SCORE_EXTENSION + 10; | |
49 | 7186 | const uint8_t *buf = p->buf; | |
50 | |||
51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7186 times.
|
7186 | if (p->buf_size < CDXL_HEADER_SIZE) |
52 | ✗ | return 0; | |
53 | |||
54 | /* check type */ | ||
55 |
2/2✓ Branch 0 taken 5729 times.
✓ Branch 1 taken 1457 times.
|
7186 | if (buf[0] > 1) |
56 | 5729 | return 0; | |
57 | |||
58 | /* reserved bytes should always be set to 0 */ | ||
59 |
2/2✓ Branch 0 taken 1210 times.
✓ Branch 1 taken 247 times.
|
1457 | if (AV_RL24(&buf[29])) |
60 | 1210 | return 0; | |
61 | |||
62 | /* check palette size */ | ||
63 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 166 times.
|
247 | if (!AV_RN16(&buf[20])) |
64 | 81 | return 0; | |
65 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
|
166 | if (buf[0] == 1 && AV_RB16(&buf[20]) > 512) |
66 | ✗ | return 0; | |
67 |
4/4✓ Branch 0 taken 152 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 131 times.
|
166 | if (buf[0] == 0 && AV_RB16(&buf[20]) > 768) |
68 | 21 | return 0; | |
69 | |||
70 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 143 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
145 | if (!AV_RN16(&buf[22]) && AV_RN16(&buf[24])) |
71 | ✗ | return 0; | |
72 | |||
73 |
4/6✓ Branch 0 taken 131 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 131 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 131 times.
|
145 | if (buf[0] == 0 && (!buf[26] || !AV_RB16(&buf[24]))) |
74 | ✗ | return 0; | |
75 | |||
76 | /* check number of planes */ | ||
77 |
5/6✓ Branch 0 taken 143 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 140 times.
✗ Branch 5 not taken.
|
145 | if (buf[19] != 6 && buf[19] != 8 && buf[19] != 24) |
78 | 140 | return 0; | |
79 | |||
80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (buf[18]) |
81 | ✗ | return 0; | |
82 | |||
83 | /* check widh and height */ | ||
84 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if (AV_RB16(&buf[14]) > 640 || AV_RB16(&buf[16]) > 480 || |
85 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | AV_RB16(&buf[14]) == 0 || AV_RB16(&buf[16]) == 0) |
86 | ✗ | return 0; | |
87 | |||
88 | /* chunk size */ | ||
89 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (AV_RB32(&buf[2]) <= AV_RB16(&buf[20]) + AV_RB16(&buf[22]) * (1 + !!(buf[1] & 0x10)) + CDXL_HEADER_SIZE) |
90 | ✗ | return 0; | |
91 | |||
92 | /* previous chunk size */ | ||
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (AV_RN32(&buf[6])) |
94 | ✗ | score /= 2; | |
95 | |||
96 | /* current frame number, usually starts from 1 */ | ||
97 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (AV_RB32(&buf[10]) != 1) |
98 | 1 | score /= 2; | |
99 | |||
100 | 5 | return score; | |
101 | } | ||
102 | |||
103 | 6 | static int cdxl_read_header(AVFormatContext *s) | |
104 | { | ||
105 | 6 | CDXLDemuxContext *cdxl = s->priv_data; | |
106 | |||
107 | 6 | cdxl->read_chunk = 0; | |
108 | 6 | cdxl->video_stream_index = -1; | |
109 | 6 | cdxl->audio_stream_index = -1; | |
110 | |||
111 | 6 | cdxl->filesize = avio_size(s->pb); | |
112 | |||
113 | 6 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
114 | |||
115 | 6 | return 0; | |
116 | } | ||
117 | |||
118 | 198 | static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt) | |
119 | { | ||
120 | 198 | CDXLDemuxContext *cdxl = s->priv_data; | |
121 | 198 | AVIOContext *pb = s->pb; | |
122 | uint32_t current_size, video_size, image_size; | ||
123 | uint16_t audio_size, palette_size, width, height; | ||
124 | int channels, type, format, ret; | ||
125 | |||
126 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 187 times.
|
198 | if (avio_feof(pb)) |
127 | 11 | return AVERROR_EOF; | |
128 | |||
129 |
2/2✓ Branch 0 taken 107 times.
✓ Branch 1 taken 80 times.
|
187 | if (!cdxl->read_chunk) { |
130 | 107 | cdxl->pos = avio_tell(pb); | |
131 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 106 times.
|
107 | if (avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE) |
132 | 1 | return AVERROR_EOF; | |
133 | } | ||
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | if (cdxl->header[0] > 1) { |
135 | ✗ | av_log(s, AV_LOG_ERROR, "unsupported cdxl file\n"); | |
136 | ✗ | return AVERROR_INVALIDDATA; | |
137 | } | ||
138 | |||
139 | 186 | type = cdxl->header[0]; | |
140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | channels = 1 + !!(cdxl->header[1] & 0x10); |
141 | 186 | format = cdxl->header[1] & 0xE0; | |
142 | 186 | current_size = AV_RB32(&cdxl->header[2]); | |
143 | 186 | width = AV_RB16(&cdxl->header[14]); | |
144 | 186 | height = AV_RB16(&cdxl->header[16]); | |
145 | 186 | palette_size = AV_RB16(&cdxl->header[20]); | |
146 | 186 | audio_size = AV_RB16(&cdxl->header[22]) * channels; | |
147 | 186 | cdxl->srate = AV_RB16(&cdxl->header[24]); | |
148 |
3/4✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 22 times.
|
186 | if (!cdxl->srate && audio_size) |
149 | 164 | cdxl->srate = cdxl->sample_rate; | |
150 | 186 | cdxl->frate.num = cdxl->header[26]; | |
151 | 186 | cdxl->frate.den = 1; | |
152 |
1/2✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
|
186 | if (cdxl->header[19] == 0 || |
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX) |
154 | ✗ | return AVERROR_INVALIDDATA; | |
155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | if (format == 0x20) |
156 | ✗ | image_size = width * height * cdxl->header[19] / 8; | |
157 | else | ||
158 | 186 | image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8; | |
159 | 186 | video_size = palette_size + image_size; | |
160 | |||
161 |
3/6✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 186 times.
|
186 | if ((type == 1 && palette_size > 512) || |
162 | ✗ | (type == 0 && palette_size > 768)) | |
163 | ✗ | return AVERROR_INVALIDDATA; | |
164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE) |
165 | ✗ | return AVERROR_INVALIDDATA; | |
166 | |||
167 |
4/6✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 164 times.
✗ Branch 5 not taken.
|
186 | if (!cdxl->frate.num && audio_size && cdxl->srate > 0) { |
168 | 164 | cdxl->frate = (AVRational){ cdxl->srate, audio_size }; | |
169 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | } else if (!cdxl->frate.num) { |
170 | 22 | cdxl->frate = cdxl->frame_rate; | |
171 | } | ||
172 | |||
173 |
3/4✓ Branch 0 taken 80 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
186 | if (cdxl->read_chunk && audio_size) { |
174 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 76 times.
|
80 | if (cdxl->audio_stream_index == -1) { |
175 | 4 | AVStream *st = avformat_new_stream(s, NULL); | |
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!st) |
177 | ✗ | return AVERROR(ENOMEM); | |
178 | |||
179 | 4 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
180 | 4 | st->codecpar->codec_tag = 0; | |
181 | 4 | st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR; | |
182 | 4 | av_channel_layout_default(&st->codecpar->ch_layout, channels); | |
183 | 4 | st->codecpar->sample_rate= cdxl->srate; | |
184 | 4 | st->start_time = 0; | |
185 | 4 | cdxl->audio_stream_index = st->index; | |
186 | 4 | avpriv_set_pts_info(st, 64, 1, cdxl->srate); | |
187 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | if (current_size && cdxl->filesize > 0 && audio_size > 0) |
188 | 4 | st->duration = (cdxl->filesize / current_size) * audio_size / channels; | |
189 | } | ||
190 | |||
191 | 80 | ret = av_get_packet(pb, pkt, audio_size); | |
192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (ret < 0) |
193 | ✗ | return ret; | |
194 | 80 | pkt->stream_index = cdxl->audio_stream_index; | |
195 | 80 | pkt->pos = cdxl->pos; | |
196 | 80 | pkt->duration = audio_size / channels; | |
197 | 80 | cdxl->read_chunk = 0; | |
198 | } else { | ||
199 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 100 times.
|
106 | if (cdxl->video_stream_index == -1) { |
200 | 6 | AVStream *st = avformat_new_stream(s, NULL); | |
201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!st) |
202 | ✗ | return AVERROR(ENOMEM); | |
203 | |||
204 | 6 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
205 | 6 | st->codecpar->codec_tag = 0; | |
206 | 6 | st->codecpar->codec_id = AV_CODEC_ID_CDXL; | |
207 | 6 | st->codecpar->width = width; | |
208 | 6 | st->codecpar->height = height; | |
209 | |||
210 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | if (current_size && cdxl->filesize > 0) |
211 | 6 | st->nb_frames = cdxl->filesize / current_size; | |
212 | 6 | st->start_time = 0; | |
213 | 6 | cdxl->video_stream_index = st->index; | |
214 | 6 | avpriv_set_pts_info(st, 64, cdxl->frate.den, cdxl->frate.num); | |
215 | } | ||
216 | |||
217 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 106 times.
|
106 | if ((ret = av_new_packet(pkt, video_size + CDXL_HEADER_SIZE)) < 0) |
218 | ✗ | return ret; | |
219 | 106 | memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE); | |
220 | 106 | ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size); | |
221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
|
106 | if (ret < 0) { |
222 | ✗ | return ret; | |
223 | } | ||
224 | 106 | av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret); | |
225 | 106 | pkt->stream_index = cdxl->video_stream_index; | |
226 | 106 | pkt->flags |= AV_PKT_FLAG_KEY; | |
227 | 106 | pkt->pos = cdxl->pos; | |
228 | 106 | pkt->duration = 1; | |
229 | 106 | cdxl->read_chunk = audio_size; | |
230 | } | ||
231 | |||
232 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 84 times.
|
186 | if (!cdxl->read_chunk) |
233 | 102 | avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE); | |
234 | 186 | return ret; | |
235 | } | ||
236 | |||
237 | ✗ | static int read_seek(AVFormatContext *s, int stream_index, | |
238 | int64_t timestamp, int flags) | ||
239 | { | ||
240 | ✗ | CDXLDemuxContext *cdxl = s->priv_data; | |
241 | |||
242 | ✗ | cdxl->read_chunk = 0; | |
243 | |||
244 | ✗ | return -1; | |
245 | } | ||
246 | |||
247 | #define OFFSET(x) offsetof(CDXLDemuxContext, x) | ||
248 | static const AVOption cdxl_options[] = { | ||
249 | { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64=11025 }, 8000, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, | ||
250 | { "frame_rate", "", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, { .str="15" }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, | ||
251 | { NULL }, | ||
252 | }; | ||
253 | |||
254 | static const AVClass cdxl_demuxer_class = { | ||
255 | .class_name = "CDXL demuxer", | ||
256 | .item_name = av_default_item_name, | ||
257 | .option = cdxl_options, | ||
258 | .version = LIBAVUTIL_VERSION_INT, | ||
259 | }; | ||
260 | |||
261 | const FFInputFormat ff_cdxl_demuxer = { | ||
262 | .p.name = "cdxl", | ||
263 | .p.long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"), | ||
264 | .p.priv_class = &cdxl_demuxer_class, | ||
265 | .p.extensions = "cdxl,xl", | ||
266 | .p.flags = AVFMT_GENERIC_INDEX, | ||
267 | .priv_data_size = sizeof(CDXLDemuxContext), | ||
268 | .read_probe = cdxl_read_probe, | ||
269 | .read_header = cdxl_read_header, | ||
270 | .read_packet = cdxl_read_packet, | ||
271 | .read_seek = read_seek, | ||
272 | }; | ||
273 |