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