/src/ffmpeg/libavformat/cdxl.c
Line | Count | 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 | 3.93M | #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 | | static int cdxl_read_probe(const AVProbeData *p) |
47 | 942k | { |
48 | 942k | int score = AVPROBE_SCORE_EXTENSION + 10; |
49 | 942k | const uint8_t *buf = p->buf; |
50 | | |
51 | 942k | if (p->buf_size < CDXL_HEADER_SIZE) |
52 | 207k | return 0; |
53 | | |
54 | | /* check type */ |
55 | 735k | if (buf[0] > 1) |
56 | 571k | return 0; |
57 | | |
58 | | /* reserved bytes should always be set to 0 */ |
59 | 164k | if (AV_RL24(&buf[29])) |
60 | 113k | return 0; |
61 | | |
62 | | /* check palette size */ |
63 | 50.7k | if (!AV_RN16(&buf[20])) |
64 | 31.3k | return 0; |
65 | 19.4k | if (buf[0] == 1 && AV_RB16(&buf[20]) > 512) |
66 | 2.85k | return 0; |
67 | 16.6k | if (buf[0] == 0 && AV_RB16(&buf[20]) > 768) |
68 | 7.72k | return 0; |
69 | | |
70 | 8.87k | if (!AV_RN16(&buf[22]) && AV_RN16(&buf[24])) |
71 | 1.17k | return 0; |
72 | | |
73 | 7.70k | if (buf[0] == 0 && (!buf[26] || !AV_RB16(&buf[24]))) |
74 | 2.38k | return 0; |
75 | | |
76 | | /* check number of planes */ |
77 | 5.32k | if (buf[19] != 6 && buf[19] != 8 && buf[19] != 24) |
78 | 2.51k | return 0; |
79 | | |
80 | 2.80k | if (buf[18]) |
81 | 203 | return 0; |
82 | | |
83 | | /* check widh and height */ |
84 | 2.60k | if (AV_RB16(&buf[14]) > 640 || AV_RB16(&buf[16]) > 480 || |
85 | 2.22k | AV_RB16(&buf[14]) == 0 || AV_RB16(&buf[16]) == 0) |
86 | 859 | return 0; |
87 | | |
88 | | /* chunk size */ |
89 | 1.74k | if (AV_RB32(&buf[2]) <= AV_RB16(&buf[20]) + AV_RB16(&buf[22]) * (1 + !!(buf[1] & 0x10)) + CDXL_HEADER_SIZE) |
90 | 249 | return 0; |
91 | | |
92 | | /* previous chunk size */ |
93 | 1.49k | if (AV_RN32(&buf[6])) |
94 | 1.24k | score /= 2; |
95 | | |
96 | | /* current frame number, usually starts from 1 */ |
97 | 1.49k | if (AV_RB32(&buf[10]) != 1) |
98 | 541 | score /= 2; |
99 | | |
100 | 1.49k | return score; |
101 | 1.74k | } |
102 | | |
103 | | static int cdxl_read_header(AVFormatContext *s) |
104 | 2.01k | { |
105 | 2.01k | CDXLDemuxContext *cdxl = s->priv_data; |
106 | | |
107 | 2.01k | cdxl->read_chunk = 0; |
108 | 2.01k | cdxl->video_stream_index = -1; |
109 | 2.01k | cdxl->audio_stream_index = -1; |
110 | | |
111 | 2.01k | cdxl->filesize = avio_size(s->pb); |
112 | | |
113 | 2.01k | s->ctx_flags |= AVFMTCTX_NOHEADER; |
114 | | |
115 | 2.01k | return 0; |
116 | 2.01k | } |
117 | | |
118 | | static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt) |
119 | 635k | { |
120 | 635k | CDXLDemuxContext *cdxl = s->priv_data; |
121 | 635k | AVIOContext *pb = s->pb; |
122 | 635k | uint32_t current_size, video_size, image_size; |
123 | 635k | uint16_t audio_size, palette_size, width, height; |
124 | 635k | int channels, type, format, ret; |
125 | | |
126 | 635k | if (avio_feof(pb)) |
127 | 2.15k | return AVERROR_EOF; |
128 | | |
129 | 633k | if (!cdxl->read_chunk) { |
130 | 337k | cdxl->pos = avio_tell(pb); |
131 | 337k | if (avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE) |
132 | 774 | return AVERROR_EOF; |
133 | 337k | } |
134 | 632k | if (cdxl->header[0] > 1) { |
135 | 117 | av_log(s, AV_LOG_ERROR, "unsupported cdxl file\n"); |
136 | 117 | return AVERROR_INVALIDDATA; |
137 | 117 | } |
138 | | |
139 | 632k | type = cdxl->header[0]; |
140 | 632k | channels = 1 + !!(cdxl->header[1] & 0x10); |
141 | 632k | format = cdxl->header[1] & 0xE0; |
142 | 632k | current_size = AV_RB32(&cdxl->header[2]); |
143 | 632k | width = AV_RB16(&cdxl->header[14]); |
144 | 632k | height = AV_RB16(&cdxl->header[16]); |
145 | 632k | palette_size = AV_RB16(&cdxl->header[20]); |
146 | 632k | audio_size = AV_RB16(&cdxl->header[22]) * channels; |
147 | 632k | cdxl->srate = AV_RB16(&cdxl->header[24]); |
148 | 632k | if (!cdxl->srate && audio_size) |
149 | 3.64k | cdxl->srate = cdxl->sample_rate; |
150 | 632k | cdxl->frate.num = cdxl->header[26]; |
151 | 632k | cdxl->frate.den = 1; |
152 | 632k | if (cdxl->header[19] == 0 || |
153 | 632k | FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX) |
154 | 129 | return AVERROR_INVALIDDATA; |
155 | 632k | if (format == 0x20) |
156 | 722 | image_size = width * height * cdxl->header[19] / 8; |
157 | 631k | else |
158 | 631k | image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8; |
159 | 632k | video_size = palette_size + image_size; |
160 | | |
161 | 632k | if ((type == 1 && palette_size > 512) || |
162 | 632k | (type == 0 && palette_size > 768)) |
163 | 60 | return AVERROR_INVALIDDATA; |
164 | 632k | if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE) |
165 | 88 | return AVERROR_INVALIDDATA; |
166 | | |
167 | 632k | if (!cdxl->frate.num && audio_size && cdxl->srate > 0) { |
168 | 295k | cdxl->frate = (AVRational){ cdxl->srate, audio_size }; |
169 | 337k | } else if (!cdxl->frate.num) { |
170 | 31.7k | cdxl->frate = cdxl->frame_rate; |
171 | 31.7k | } |
172 | | |
173 | 632k | if (cdxl->read_chunk && audio_size) { |
174 | 296k | if (cdxl->audio_stream_index == -1) { |
175 | 1.38k | AVStream *st = avformat_new_stream(s, NULL); |
176 | 1.38k | if (!st) |
177 | 0 | return AVERROR(ENOMEM); |
178 | | |
179 | 1.38k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
180 | 1.38k | st->codecpar->codec_tag = 0; |
181 | 1.38k | st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR; |
182 | 1.38k | av_channel_layout_default(&st->codecpar->ch_layout, channels); |
183 | 1.38k | st->codecpar->sample_rate= cdxl->srate; |
184 | 1.38k | st->start_time = 0; |
185 | 1.38k | cdxl->audio_stream_index = st->index; |
186 | 1.38k | avpriv_set_pts_info(st, 64, 1, cdxl->srate); |
187 | 1.38k | if (current_size && cdxl->filesize > 0 && audio_size > 0) |
188 | 620 | st->duration = (cdxl->filesize / current_size) * audio_size / channels; |
189 | 1.38k | } |
190 | | |
191 | 296k | ret = av_get_packet(pb, pkt, audio_size); |
192 | 296k | if (ret < 0) |
193 | 420 | return ret; |
194 | 295k | pkt->stream_index = cdxl->audio_stream_index; |
195 | 295k | pkt->pos = cdxl->pos; |
196 | 295k | pkt->duration = audio_size / channels; |
197 | 295k | cdxl->read_chunk = 0; |
198 | 336k | } else { |
199 | 336k | if (cdxl->video_stream_index == -1) { |
200 | 1.74k | AVStream *st = avformat_new_stream(s, NULL); |
201 | 1.74k | if (!st) |
202 | 0 | return AVERROR(ENOMEM); |
203 | | |
204 | 1.74k | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
205 | 1.74k | st->codecpar->codec_tag = 0; |
206 | 1.74k | st->codecpar->codec_id = AV_CODEC_ID_CDXL; |
207 | 1.74k | st->codecpar->width = width; |
208 | 1.74k | st->codecpar->height = height; |
209 | | |
210 | 1.74k | if (current_size && cdxl->filesize > 0) |
211 | 691 | st->nb_frames = cdxl->filesize / current_size; |
212 | 1.74k | st->start_time = 0; |
213 | 1.74k | cdxl->video_stream_index = st->index; |
214 | 1.74k | avpriv_set_pts_info(st, 64, cdxl->frate.den, cdxl->frate.num); |
215 | 1.74k | } |
216 | | |
217 | 336k | if ((ret = av_new_packet(pkt, video_size + CDXL_HEADER_SIZE)) < 0) |
218 | 0 | return ret; |
219 | 336k | memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE); |
220 | 336k | ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size); |
221 | 336k | if (ret < 0) { |
222 | 96 | return ret; |
223 | 96 | } |
224 | 336k | av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret); |
225 | 336k | pkt->stream_index = cdxl->video_stream_index; |
226 | 336k | pkt->flags |= AV_PKT_FLAG_KEY; |
227 | 336k | pkt->pos = cdxl->pos; |
228 | 336k | pkt->duration = 1; |
229 | 336k | cdxl->read_chunk = audio_size; |
230 | 336k | } |
231 | | |
232 | 631k | if (!cdxl->read_chunk) |
233 | 335k | avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE); |
234 | 631k | return ret; |
235 | 632k | } |
236 | | |
237 | | static int read_seek(AVFormatContext *s, int stream_index, |
238 | | int64_t timestamp, int flags) |
239 | 0 | { |
240 | 0 | CDXLDemuxContext *cdxl = s->priv_data; |
241 | |
|
242 | 0 | cdxl->read_chunk = 0; |
243 | |
|
244 | 0 | return -1; |
245 | 0 | } |
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 | | }; |