Libav 0.7.1
libavformat/dxa.c
Go to the documentation of this file.
00001 /*
00002  * DXA demuxer
00003  * Copyright (c) 2007 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     int w, h;
00040     if (p->buf_size < 15)
00041         return 0;
00042     w = AV_RB16(p->buf + 11);
00043     h = AV_RB16(p->buf + 13);
00044     /* check file header */
00045     if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
00046         p->buf[2] == 'X' && p->buf[3] == 'A' &&
00047         w && w <= 2048 && h && h <= 2048)
00048         return AVPROBE_SCORE_MAX;
00049     else
00050         return 0;
00051 }
00052 
00053 static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
00054 {
00055     AVIOContext *pb = s->pb;
00056     DXAContext *c = s->priv_data;
00057     AVStream *st, *ast;
00058     uint32_t tag;
00059     int32_t fps;
00060     int w, h;
00061     int num, den;
00062     int flags;
00063     int ret;
00064 
00065     tag = avio_rl32(pb);
00066     if (tag != MKTAG('D', 'E', 'X', 'A'))
00067         return -1;
00068     flags = avio_r8(pb);
00069     c->frames = avio_rb16(pb);
00070     if(!c->frames){
00071         av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
00072         return -1;
00073     }
00074 
00075     fps = avio_rb32(pb);
00076     if(fps > 0){
00077         den = 1000;
00078         num = fps;
00079     }else if (fps < 0){
00080         den = 100000;
00081         num = -fps;
00082     }else{
00083         den = 10;
00084         num = 1;
00085     }
00086     w = avio_rb16(pb);
00087     h = avio_rb16(pb);
00088     c->has_sound = 0;
00089 
00090     st = av_new_stream(s, 0);
00091     if (!st)
00092         return -1;
00093 
00094     // Parse WAV data header
00095     if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
00096         uint32_t size, fsize;
00097         c->has_sound = 1;
00098         size = avio_rb32(pb);
00099         c->vidpos = avio_tell(pb) + size;
00100         avio_skip(pb, 16);
00101         fsize = avio_rl32(pb);
00102 
00103         ast = av_new_stream(s, 0);
00104         if (!ast)
00105             return -1;
00106         ret = ff_get_wav_header(pb, ast->codec, fsize);
00107         if (ret < 0)
00108             return ret;
00109         // find 'data' chunk
00110         while(avio_tell(pb) < c->vidpos && !pb->eof_reached){
00111             tag = avio_rl32(pb);
00112             fsize = avio_rl32(pb);
00113             if(tag == MKTAG('d', 'a', 't', 'a')) break;
00114             avio_skip(pb, fsize);
00115         }
00116         c->bpc = (fsize + c->frames - 1) / c->frames;
00117         if(ast->codec->block_align)
00118             c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
00119         c->bytes_left = fsize;
00120         c->wavpos = avio_tell(pb);
00121         avio_seek(pb, c->vidpos, SEEK_SET);
00122     }
00123 
00124     /* now we are ready: build format streams */
00125     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00126     st->codec->codec_id   = CODEC_ID_DXA;
00127     st->codec->width      = w;
00128     st->codec->height     = h;
00129     av_reduce(&den, &num, den, num, (1UL<<31)-1);
00130     av_set_pts_info(st, 33, num, den);
00131     /* flags & 0x80 means that image is interlaced,
00132      * flags & 0x40 means that image has double height
00133      * either way set true height
00134      */
00135     if(flags & 0xC0){
00136         st->codec->height >>= 1;
00137     }
00138     c->readvid = !c->has_sound;
00139     c->vidpos  = avio_tell(pb);
00140     s->start_time = 0;
00141     s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
00142     av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
00143 
00144     return 0;
00145 }
00146 
00147 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
00148 {
00149     DXAContext *c = s->priv_data;
00150     int ret;
00151     uint32_t size;
00152     uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
00153     int pal_size = 0;
00154 
00155     if(!c->readvid && c->has_sound && c->bytes_left){
00156         c->readvid = 1;
00157         avio_seek(s->pb, c->wavpos, SEEK_SET);
00158         size = FFMIN(c->bytes_left, c->bpc);
00159         ret = av_get_packet(s->pb, pkt, size);
00160         pkt->stream_index = 1;
00161         if(ret != size)
00162             return AVERROR(EIO);
00163         c->bytes_left -= size;
00164         c->wavpos = avio_tell(s->pb);
00165         return 0;
00166     }
00167     avio_seek(s->pb, c->vidpos, SEEK_SET);
00168     while(!s->pb->eof_reached && c->frames){
00169         avio_read(s->pb, buf, 4);
00170         switch(AV_RL32(buf)){
00171         case MKTAG('N', 'U', 'L', 'L'):
00172             if(av_new_packet(pkt, 4 + pal_size) < 0)
00173                 return AVERROR(ENOMEM);
00174             pkt->stream_index = 0;
00175             if(pal_size) memcpy(pkt->data, pal, pal_size);
00176             memcpy(pkt->data + pal_size, buf, 4);
00177             c->frames--;
00178             c->vidpos = avio_tell(s->pb);
00179             c->readvid = 0;
00180             return 0;
00181         case MKTAG('C', 'M', 'A', 'P'):
00182             pal_size = 768+4;
00183             memcpy(pal, buf, 4);
00184             avio_read(s->pb, pal + 4, 768);
00185             break;
00186         case MKTAG('F', 'R', 'A', 'M'):
00187             avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
00188             size = AV_RB32(buf + 5);
00189             if(size > 0xFFFFFF){
00190                 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
00191                 return -1;
00192             }
00193             if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
00194                 return AVERROR(ENOMEM);
00195             memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
00196             ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
00197             if(ret != size){
00198                 av_free_packet(pkt);
00199                 return AVERROR(EIO);
00200             }
00201             if(pal_size) memcpy(pkt->data, pal, pal_size);
00202             pkt->stream_index = 0;
00203             c->frames--;
00204             c->vidpos = avio_tell(s->pb);
00205             c->readvid = 0;
00206             return 0;
00207         default:
00208             av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
00209             return -1;
00210         }
00211     }
00212     return AVERROR(EIO);
00213 }
00214 
00215 AVInputFormat ff_dxa_demuxer = {
00216     "dxa",
00217     NULL_IF_CONFIG_SMALL("DXA"),
00218     sizeof(DXAContext),
00219     dxa_probe,
00220     dxa_read_header,
00221     dxa_read_packet,
00222 };