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 "internal.h"
00025 #include "riff.h"
00026
00027 #define DXA_EXTRA_SIZE 9
00028
00029 typedef struct{
00030 int frames;
00031 int has_sound;
00032 int bpc;
00033 uint32_t bytes_left;
00034 int64_t wavpos, vidpos;
00035 int readvid;
00036 }DXAContext;
00037
00038 static int dxa_probe(AVProbeData *p)
00039 {
00040 int w, h;
00041 if (p->buf_size < 15)
00042 return 0;
00043 w = AV_RB16(p->buf + 11);
00044 h = AV_RB16(p->buf + 13);
00045
00046 if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
00047 p->buf[2] == 'X' && p->buf[3] == 'A' &&
00048 w && w <= 2048 && h && h <= 2048)
00049 return AVPROBE_SCORE_MAX;
00050 else
00051 return 0;
00052 }
00053
00054 static int dxa_read_header(AVFormatContext *s)
00055 {
00056 AVIOContext *pb = s->pb;
00057 DXAContext *c = s->priv_data;
00058 AVStream *st, *ast;
00059 uint32_t tag;
00060 int32_t fps;
00061 int w, h;
00062 int num, den;
00063 int flags;
00064 int ret;
00065
00066 tag = avio_rl32(pb);
00067 if (tag != MKTAG('D', 'E', 'X', 'A'))
00068 return -1;
00069 flags = avio_r8(pb);
00070 c->frames = avio_rb16(pb);
00071 if(!c->frames){
00072 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
00073 return -1;
00074 }
00075
00076 fps = avio_rb32(pb);
00077 if(fps > 0){
00078 den = 1000;
00079 num = fps;
00080 }else if (fps < 0){
00081 den = 100000;
00082 num = -fps;
00083 }else{
00084 den = 10;
00085 num = 1;
00086 }
00087 w = avio_rb16(pb);
00088 h = avio_rb16(pb);
00089 c->has_sound = 0;
00090
00091 st = avformat_new_stream(s, NULL);
00092 if (!st)
00093 return -1;
00094
00095
00096 if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
00097 uint32_t size, fsize;
00098 c->has_sound = 1;
00099 size = avio_rb32(pb);
00100 c->vidpos = avio_tell(pb) + size;
00101 avio_skip(pb, 16);
00102 fsize = avio_rl32(pb);
00103
00104 ast = avformat_new_stream(s, NULL);
00105 if (!ast)
00106 return -1;
00107 ret = ff_get_wav_header(pb, ast->codec, fsize);
00108 if (ret < 0)
00109 return ret;
00110 if (ast->codec->sample_rate > 0)
00111 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00112
00113 while(avio_tell(pb) < c->vidpos && !url_feof(pb)){
00114 tag = avio_rl32(pb);
00115 fsize = avio_rl32(pb);
00116 if(tag == MKTAG('d', 'a', 't', 'a')) break;
00117 avio_skip(pb, fsize);
00118 }
00119 c->bpc = (fsize + c->frames - 1) / c->frames;
00120 if(ast->codec->block_align)
00121 c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
00122 c->bytes_left = fsize;
00123 c->wavpos = avio_tell(pb);
00124 avio_seek(pb, c->vidpos, SEEK_SET);
00125 }
00126
00127
00128 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00129 st->codec->codec_id = AV_CODEC_ID_DXA;
00130 st->codec->width = w;
00131 st->codec->height = h;
00132 av_reduce(&den, &num, den, num, (1UL<<31)-1);
00133 avpriv_set_pts_info(st, 33, num, den);
00134
00135
00136
00137
00138 if(flags & 0xC0){
00139 st->codec->height >>= 1;
00140 }
00141 c->readvid = !c->has_sound;
00142 c->vidpos = avio_tell(pb);
00143 s->start_time = 0;
00144 s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
00145 av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
00146
00147 return 0;
00148 }
00149
00150 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
00151 {
00152 DXAContext *c = s->priv_data;
00153 int ret;
00154 uint32_t size;
00155 uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
00156 int pal_size = 0;
00157
00158 if(!c->readvid && c->has_sound && c->bytes_left){
00159 c->readvid = 1;
00160 avio_seek(s->pb, c->wavpos, SEEK_SET);
00161 size = FFMIN(c->bytes_left, c->bpc);
00162 ret = av_get_packet(s->pb, pkt, size);
00163 pkt->stream_index = 1;
00164 if(ret != size)
00165 return AVERROR(EIO);
00166 c->bytes_left -= size;
00167 c->wavpos = avio_tell(s->pb);
00168 return 0;
00169 }
00170 avio_seek(s->pb, c->vidpos, SEEK_SET);
00171 while(!url_feof(s->pb) && c->frames){
00172 avio_read(s->pb, buf, 4);
00173 switch(AV_RL32(buf)){
00174 case MKTAG('N', 'U', 'L', 'L'):
00175 if(av_new_packet(pkt, 4 + pal_size) < 0)
00176 return AVERROR(ENOMEM);
00177 pkt->stream_index = 0;
00178 if(pal_size) memcpy(pkt->data, pal, pal_size);
00179 memcpy(pkt->data + pal_size, buf, 4);
00180 c->frames--;
00181 c->vidpos = avio_tell(s->pb);
00182 c->readvid = 0;
00183 return 0;
00184 case MKTAG('C', 'M', 'A', 'P'):
00185 pal_size = 768+4;
00186 memcpy(pal, buf, 4);
00187 avio_read(s->pb, pal + 4, 768);
00188 break;
00189 case MKTAG('F', 'R', 'A', 'M'):
00190 avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
00191 size = AV_RB32(buf + 5);
00192 if(size > 0xFFFFFF){
00193 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
00194 return -1;
00195 }
00196 if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
00197 return AVERROR(ENOMEM);
00198 memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
00199 ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
00200 if(ret != size){
00201 av_free_packet(pkt);
00202 return AVERROR(EIO);
00203 }
00204 if(pal_size) memcpy(pkt->data, pal, pal_size);
00205 pkt->stream_index = 0;
00206 c->frames--;
00207 c->vidpos = avio_tell(s->pb);
00208 c->readvid = 0;
00209 return 0;
00210 default:
00211 av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
00212 return -1;
00213 }
00214 }
00215 return AVERROR(EIO);
00216 }
00217
00218 AVInputFormat ff_dxa_demuxer = {
00219 .name = "dxa",
00220 .long_name = NULL_IF_CONFIG_SMALL("DXA"),
00221 .priv_data_size = sizeof(DXAContext),
00222 .read_probe = dxa_probe,
00223 .read_header = dxa_read_header,
00224 .read_packet = dxa_read_packet,
00225 };