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
25 #define DXA_EXTRA_SIZE 9
32 int64_t wavpos, vidpos;
36 static int dxa_probe(AVProbeData *p)
38 /* check file header */
41 if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
42 p->buf[2] == 'X' && p->buf[3] == 'A')
43 return AVPROBE_SCORE_MAX;
48 static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
50 ByteIOContext *pb = &s->pb;
51 DXAContext *c = s->priv_data;
60 if (tag != MKTAG('D', 'E', 'X', 'A'))
63 c->frames = get_be16(pb);
65 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
84 st = av_new_stream(s, 0);
88 // Parse WAV data header
89 if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){
93 c->vidpos = url_ftell(pb) + size;
97 ast = av_new_stream(s, 0);
100 get_wav_header(pb, ast->codec, fsize);
102 while(url_ftell(pb) < c->vidpos && !url_feof(pb)){
104 fsize = get_le32(pb);
105 if(tag == MKTAG('d', 'a', 't', 'a')) break;
106 url_fskip(pb, fsize);
108 c->bpc = (fsize + c->frames - 1) / c->frames;
109 if(ast->codec->block_align)
110 c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
111 c->bytes_left = fsize;
112 c->wavpos = url_ftell(pb);
113 url_fseek(pb, c->vidpos, SEEK_SET);
116 /* now we are ready: build format streams */
117 st->codec->codec_type = CODEC_TYPE_VIDEO;
118 st->codec->codec_id = CODEC_ID_DXA;
119 st->codec->width = w;
120 st->codec->height = h;
121 av_reduce(&den, &num, den, num, (1UL<<31)-1);
122 av_set_pts_info(st, 33, num, den);
123 /* flags & 0x80 means that image is interlaced,
124 * flags & 0x40 means that image has double height
125 * either way set true height
128 st->codec->height >>= 1;
130 c->readvid = !c->has_sound;
131 c->vidpos = url_ftell(pb);
133 s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
134 av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
139 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
141 DXAContext *c = s->priv_data;
144 uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
147 if(!c->readvid && c->has_sound && c->bytes_left){
149 url_fseek(&s->pb, c->wavpos, SEEK_SET);
150 size = FFMIN(c->bytes_left, c->bpc);
151 ret = av_get_packet(&s->pb, pkt, size);
152 pkt->stream_index = 1;
155 c->bytes_left -= size;
156 c->wavpos = url_ftell(&s->pb);
159 url_fseek(&s->pb, c->vidpos, SEEK_SET);
160 while(!url_feof(&s->pb) && c->frames){
161 get_buffer(&s->pb, buf, 4);
162 switch(AV_RL32(buf)){
163 case MKTAG('N', 'U', 'L', 'L'):
164 if(av_new_packet(pkt, 4 + pal_size) < 0)
165 return AVERROR_NOMEM;
166 pkt->stream_index = 0;
167 if(pal_size) memcpy(pkt->data, pal, pal_size);
168 memcpy(pkt->data + pal_size, buf, 4);
170 c->vidpos = url_ftell(&s->pb);
173 case MKTAG('C', 'M', 'A', 'P'):
176 get_buffer(&s->pb, pal + 4, 768);
178 case MKTAG('F', 'R', 'A', 'M'):
179 get_buffer(&s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
180 size = AV_RB32(buf + 5);
182 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
185 if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
186 return AVERROR_NOMEM;
187 memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
188 ret = get_buffer(&s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
193 if(pal_size) memcpy(pkt->data, pal, pal_size);
194 pkt->stream_index = 0;
196 c->vidpos = url_ftell(&s->pb);
200 av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
207 AVInputFormat dxa_demuxer = {