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