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