]> git.sesse.net Git - ffmpeg/blob - libavformat/cdxl.c
lavfi: rename vsrc_buffer.c to buffersrc.c
[ffmpeg] / libavformat / cdxl.c
1 /*
2  * CDXL demuxer
3  * Copyright (c) 2011-2012 Paul B Mahol
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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_header(AVFormatContext *s)
42 {
43     CDXLDemuxContext *cdxl = s->priv_data;
44     int ret;
45
46     if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
47         av_log(s, AV_LOG_ERROR,
48                "Could not parse framerate: %s.\n", cdxl->framerate);
49         return ret;
50     }
51
52     cdxl->read_chunk         =  0;
53     cdxl->video_stream_index = -1;
54     cdxl->audio_stream_index = -1;
55
56     s->ctx_flags |= AVFMTCTX_NOHEADER;
57
58     return 0;
59 }
60
61 static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
62 {
63     CDXLDemuxContext *cdxl = s->priv_data;
64     AVIOContext *pb = s->pb;
65     uint32_t current_size, video_size, image_size;
66     uint16_t audio_size, palette_size, width, height;
67     int64_t  pos;
68     int      ret;
69
70     if (pb->eof_reached)
71         return AVERROR_EOF;
72
73     pos = avio_tell(pb);
74     if (!cdxl->read_chunk &&
75         avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
76         return AVERROR_EOF;
77     if (cdxl->header[0] != 1) {
78         av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
79         return AVERROR_INVALIDDATA;
80     }
81
82     current_size = AV_RB32(&cdxl->header[2]);
83     width        = AV_RB16(&cdxl->header[14]);
84     height       = AV_RB16(&cdxl->header[16]);
85     palette_size = AV_RB16(&cdxl->header[20]);
86     audio_size   = AV_RB16(&cdxl->header[22]);
87     image_size   = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
88     video_size   = palette_size + image_size;
89
90     if (palette_size > 512)
91         return AVERROR_INVALIDDATA;
92     if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
93         return AVERROR_INVALIDDATA;
94
95     if (cdxl->read_chunk && audio_size) {
96         if (cdxl->audio_stream_index == -1) {
97             AVStream *st = avformat_new_stream(s, NULL);
98             if (!st)
99                 return AVERROR(ENOMEM);
100
101             st->codec->codec_type    = AVMEDIA_TYPE_AUDIO;
102             st->codec->codec_tag     = 0;
103             st->codec->codec_id      = CODEC_ID_PCM_S8;
104             st->codec->channels      = cdxl->header[1] & 0x10 ? 2 : 1;
105             st->codec->sample_rate   = cdxl->sample_rate;
106             st->start_time           = 0;
107             cdxl->audio_stream_index = st->index;
108             avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
109         }
110
111         ret = av_get_packet(pb, pkt, audio_size);
112         if (ret < 0)
113             return ret;
114         pkt->stream_index = cdxl->audio_stream_index;
115         pkt->pos          = pos;
116         pkt->duration     = audio_size;
117         cdxl->read_chunk  = 0;
118     } else {
119         if (cdxl->video_stream_index == -1) {
120             AVStream *st = avformat_new_stream(s, NULL);
121             if (!st)
122                 return AVERROR(ENOMEM);
123
124             st->codec->codec_type    = AVMEDIA_TYPE_VIDEO;
125             st->codec->codec_tag     = 0;
126             st->codec->codec_id      = CODEC_ID_CDXL;
127             st->codec->width         = width;
128             st->codec->height        = height;
129             st->start_time           = 0;
130             cdxl->video_stream_index = st->index;
131             if (cdxl->framerate)
132                 avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
133             else
134                 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
135         }
136
137         if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
138             return AVERROR(ENOMEM);
139         memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
140         ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
141         if (ret < 0) {
142             av_free_packet(pkt);
143             return ret;
144         }
145         av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
146         pkt->stream_index  = cdxl->video_stream_index;
147         pkt->flags        |= AV_PKT_FLAG_KEY;
148         pkt->pos           = pos;
149         pkt->duration      = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
150         cdxl->read_chunk   = audio_size;
151     }
152
153     if (!cdxl->read_chunk)
154         avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
155     return ret;
156 }
157
158 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
159 static const AVOption cdxl_options[] = {
160     { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT,    { .dbl = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
161     { "framerate",   "", OFFSET(framerate),   AV_OPT_TYPE_STRING, { .str = NULL },  0, 0,       AV_OPT_FLAG_DECODING_PARAM },
162     { NULL },
163 };
164
165 static const AVClass cdxl_demuxer_class = {
166     .class_name = "CDXL demuxer",
167     .item_name  = av_default_item_name,
168     .option     = cdxl_options,
169     .version    = LIBAVUTIL_VERSION_INT,
170 };
171
172 AVInputFormat ff_cdxl_demuxer = {
173     .name           = "cdxl",
174     .long_name      = NULL_IF_CONFIG_SMALL("Commodore CDXL video format"),
175     .priv_data_size = sizeof(CDXLDemuxContext),
176     .read_header    = cdxl_read_header,
177     .read_packet    = cdxl_read_packet,
178     .extensions     = "cdxl,xl",
179     .flags          = AVFMT_GENERIC_INDEX,
180     .priv_class     = &cdxl_demuxer_class,
181 };