00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "riff.h"
00025
00026 #define DXA_EXTRA_SIZE 9
00027
00028 typedef struct{
00029 int frames;
00030 int has_sound;
00031 int bpc;
00032 uint32_t bytes_left;
00033 int64_t wavpos, vidpos;
00034 int readvid;
00035 }DXAContext;
00036
00037 static int dxa_probe(AVProbeData *p)
00038 {
00039
00040 if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
00041 p->buf[2] == 'X' && p->buf[3] == 'A')
00042 return AVPROBE_SCORE_MAX;
00043 else
00044 return 0;
00045 }
00046
00047 static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
00048 {
00049 ByteIOContext *pb = s->pb;
00050 DXAContext *c = s->priv_data;
00051 AVStream *st, *ast;
00052 uint32_t tag;
00053 int32_t fps;
00054 int w, h;
00055 int num, den;
00056 int flags;
00057
00058 tag = get_le32(pb);
00059 if (tag != MKTAG('D', 'E', 'X', 'A'))
00060 return -1;
00061 flags = get_byte(pb);
00062 c->frames = get_be16(pb);
00063 if(!c->frames){
00064 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
00065 return -1;
00066 }
00067
00068 fps = get_be32(pb);
00069 if(fps > 0){
00070 den = 1000;
00071 num = fps;
00072 }else if (fps < 0){
00073 den = 100000;
00074 num = -fps;
00075 }else{
00076 den = 10;
00077 num = 1;
00078 }
00079 w = get_be16(pb);
00080 h = get_be16(pb);
00081 c->has_sound = 0;
00082
00083 st = av_new_stream(s, 0);
00084 if (!st)
00085 return -1;
00086
00087
00088 if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){
00089 uint32_t size, fsize;
00090 c->has_sound = 1;
00091 size = get_be32(pb);
00092 c->vidpos = url_ftell(pb) + size;
00093 url_fskip(pb, 16);
00094 fsize = get_le32(pb);
00095
00096 ast = av_new_stream(s, 0);
00097 if (!ast)
00098 return -1;
00099 get_wav_header(pb, ast->codec, fsize);
00100
00101 while(url_ftell(pb) < c->vidpos && !url_feof(pb)){
00102 tag = get_le32(pb);
00103 fsize = get_le32(pb);
00104 if(tag == MKTAG('d', 'a', 't', 'a')) break;
00105 url_fskip(pb, fsize);
00106 }
00107 c->bpc = (fsize + c->frames - 1) / c->frames;
00108 if(ast->codec->block_align)
00109 c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
00110 c->bytes_left = fsize;
00111 c->wavpos = url_ftell(pb);
00112 url_fseek(pb, c->vidpos, SEEK_SET);
00113 }
00114
00115
00116 st->codec->codec_type = CODEC_TYPE_VIDEO;
00117 st->codec->codec_id = CODEC_ID_DXA;
00118 st->codec->width = w;
00119 st->codec->height = h;
00120 av_reduce(&den, &num, den, num, (1UL<<31)-1);
00121 av_set_pts_info(st, 33, num, den);
00122
00123
00124
00125
00126 if(flags & 0xC0){
00127 st->codec->height >>= 1;
00128 }
00129 c->readvid = !c->has_sound;
00130 c->vidpos = url_ftell(pb);
00131 s->start_time = 0;
00132 s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
00133 av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
00134
00135 return 0;
00136 }
00137
00138 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
00139 {
00140 DXAContext *c = s->priv_data;
00141 int ret;
00142 uint32_t size;
00143 uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
00144 int pal_size = 0;
00145
00146 if(!c->readvid && c->has_sound && c->bytes_left){
00147 c->readvid = 1;
00148 url_fseek(s->pb, c->wavpos, SEEK_SET);
00149 size = FFMIN(c->bytes_left, c->bpc);
00150 ret = av_get_packet(s->pb, pkt, size);
00151 pkt->stream_index = 1;
00152 if(ret != size)
00153 return AVERROR(EIO);
00154 c->bytes_left -= size;
00155 c->wavpos = url_ftell(s->pb);
00156 return 0;
00157 }
00158 url_fseek(s->pb, c->vidpos, SEEK_SET);
00159 while(!url_feof(s->pb) && c->frames){
00160 get_buffer(s->pb, buf, 4);
00161 switch(AV_RL32(buf)){
00162 case MKTAG('N', 'U', 'L', 'L'):
00163 if(av_new_packet(pkt, 4 + pal_size) < 0)
00164 return AVERROR(ENOMEM);
00165 pkt->stream_index = 0;
00166 if(pal_size) memcpy(pkt->data, pal, pal_size);
00167 memcpy(pkt->data + pal_size, buf, 4);
00168 c->frames--;
00169 c->vidpos = url_ftell(s->pb);
00170 c->readvid = 0;
00171 return 0;
00172 case MKTAG('C', 'M', 'A', 'P'):
00173 pal_size = 768+4;
00174 memcpy(pal, buf, 4);
00175 get_buffer(s->pb, pal + 4, 768);
00176 break;
00177 case MKTAG('F', 'R', 'A', 'M'):
00178 get_buffer(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
00179 size = AV_RB32(buf + 5);
00180 if(size > 0xFFFFFF){
00181 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
00182 return -1;
00183 }
00184 if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
00185 return AVERROR(ENOMEM);
00186 memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
00187 ret = get_buffer(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
00188 if(ret != size){
00189 av_free_packet(pkt);
00190 return AVERROR(EIO);
00191 }
00192 if(pal_size) memcpy(pkt->data, pal, pal_size);
00193 pkt->stream_index = 0;
00194 c->frames--;
00195 c->vidpos = url_ftell(s->pb);
00196 c->readvid = 0;
00197 return 0;
00198 default:
00199 av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
00200 return -1;
00201 }
00202 }
00203 return AVERROR(EIO);
00204 }
00205
00206 AVInputFormat dxa_demuxer = {
00207 "dxa",
00208 NULL_IF_CONFIG_SMALL("DXA"),
00209 sizeof(DXAContext),
00210 dxa_probe,
00211 dxa_read_header,
00212 dxa_read_packet,
00213 };