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