Modify the Ogg/Speex demuxer and the libspeex decoder so that they always treat
[FFMpeg-mirror/lagarith.git] / libavformat / dxa.c
blob5b6cd67b59a462250c5913211569103f790b9576
1 /*
2 * DXA demuxer
3 * Copyright (c) 2007 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "riff.h"
26 #define DXA_EXTRA_SIZE 9
28 typedef struct{
29 int frames;
30 int has_sound;
31 int bpc;
32 uint32_t bytes_left;
33 int64_t wavpos, vidpos;
34 int readvid;
35 }DXAContext;
37 static int dxa_probe(AVProbeData *p)
39 /* check file header */
40 if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
41 p->buf[2] == 'X' && p->buf[3] == 'A')
42 return AVPROBE_SCORE_MAX;
43 else
44 return 0;
47 static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
49 ByteIOContext *pb = s->pb;
50 DXAContext *c = s->priv_data;
51 AVStream *st, *ast;
52 uint32_t tag;
53 int32_t fps;
54 int w, h;
55 int num, den;
56 int flags;
58 tag = get_le32(pb);
59 if (tag != MKTAG('D', 'E', 'X', 'A'))
60 return -1;
61 flags = get_byte(pb);
62 c->frames = get_be16(pb);
63 if(!c->frames){
64 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
65 return -1;
68 fps = get_be32(pb);
69 if(fps > 0){
70 den = 1000;
71 num = fps;
72 }else if (fps < 0){
73 den = 100000;
74 num = -fps;
75 }else{
76 den = 10;
77 num = 1;
79 w = get_be16(pb);
80 h = get_be16(pb);
81 c->has_sound = 0;
83 st = av_new_stream(s, 0);
84 if (!st)
85 return -1;
87 // Parse WAV data header
88 if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){
89 uint32_t size, fsize;
90 c->has_sound = 1;
91 size = get_be32(pb);
92 c->vidpos = url_ftell(pb) + size;
93 url_fskip(pb, 16);
94 fsize = get_le32(pb);
96 ast = av_new_stream(s, 0);
97 if (!ast)
98 return -1;
99 ff_get_wav_header(pb, ast->codec, fsize);
100 // find 'data' chunk
101 while(url_ftell(pb) < c->vidpos && !url_feof(pb)){
102 tag = get_le32(pb);
103 fsize = get_le32(pb);
104 if(tag == MKTAG('d', 'a', 't', 'a')) break;
105 url_fskip(pb, fsize);
107 c->bpc = (fsize + c->frames - 1) / c->frames;
108 if(ast->codec->block_align)
109 c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
110 c->bytes_left = fsize;
111 c->wavpos = url_ftell(pb);
112 url_fseek(pb, c->vidpos, SEEK_SET);
115 /* now we are ready: build format streams */
116 st->codec->codec_type = CODEC_TYPE_VIDEO;
117 st->codec->codec_id = CODEC_ID_DXA;
118 st->codec->width = w;
119 st->codec->height = h;
120 av_reduce(&den, &num, den, num, (1UL<<31)-1);
121 av_set_pts_info(st, 33, num, den);
122 /* flags & 0x80 means that image is interlaced,
123 * flags & 0x40 means that image has double height
124 * either way set true height
126 if(flags & 0xC0){
127 st->codec->height >>= 1;
129 c->readvid = !c->has_sound;
130 c->vidpos = url_ftell(pb);
131 s->start_time = 0;
132 s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
133 av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
135 return 0;
138 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
140 DXAContext *c = s->priv_data;
141 int ret;
142 uint32_t size;
143 uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
144 int pal_size = 0;
146 if(!c->readvid && c->has_sound && c->bytes_left){
147 c->readvid = 1;
148 url_fseek(s->pb, c->wavpos, SEEK_SET);
149 size = FFMIN(c->bytes_left, c->bpc);
150 ret = av_get_packet(s->pb, pkt, size);
151 pkt->stream_index = 1;
152 if(ret != size)
153 return AVERROR(EIO);
154 c->bytes_left -= size;
155 c->wavpos = url_ftell(s->pb);
156 return 0;
158 url_fseek(s->pb, c->vidpos, SEEK_SET);
159 while(!url_feof(s->pb) && c->frames){
160 get_buffer(s->pb, buf, 4);
161 switch(AV_RL32(buf)){
162 case MKTAG('N', 'U', 'L', 'L'):
163 if(av_new_packet(pkt, 4 + pal_size) < 0)
164 return AVERROR(ENOMEM);
165 pkt->stream_index = 0;
166 if(pal_size) memcpy(pkt->data, pal, pal_size);
167 memcpy(pkt->data + pal_size, buf, 4);
168 c->frames--;
169 c->vidpos = url_ftell(s->pb);
170 c->readvid = 0;
171 return 0;
172 case MKTAG('C', 'M', 'A', 'P'):
173 pal_size = 768+4;
174 memcpy(pal, buf, 4);
175 get_buffer(s->pb, pal + 4, 768);
176 break;
177 case MKTAG('F', 'R', 'A', 'M'):
178 get_buffer(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
179 size = AV_RB32(buf + 5);
180 if(size > 0xFFFFFF){
181 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
182 return -1;
184 if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
185 return AVERROR(ENOMEM);
186 memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
187 ret = get_buffer(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
188 if(ret != size){
189 av_free_packet(pkt);
190 return AVERROR(EIO);
192 if(pal_size) memcpy(pkt->data, pal, pal_size);
193 pkt->stream_index = 0;
194 c->frames--;
195 c->vidpos = url_ftell(s->pb);
196 c->readvid = 0;
197 return 0;
198 default:
199 av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
200 return -1;
203 return AVERROR(EIO);
206 AVInputFormat dxa_demuxer = {
207 "dxa",
208 NULL_IF_CONFIG_SMALL("DXA"),
209 sizeof(DXAContext),
210 dxa_probe,
211 dxa_read_header,
212 dxa_read_packet,