• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 "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     /* check file header */
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, AVFormatParameters *ap)
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     // Parse WAV data header
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         // find 'data' chunk
00111         while(avio_tell(pb) < c->vidpos && !pb->eof_reached){
00112             tag = avio_rl32(pb);
00113             fsize = avio_rl32(pb);
00114             if(tag == MKTAG('d', 'a', 't', 'a')) break;
00115             avio_skip(pb, fsize);
00116         }
00117         c->bpc = (fsize + c->frames - 1) / c->frames;
00118         if(ast->codec->block_align)
00119             c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
00120         c->bytes_left = fsize;
00121         c->wavpos = avio_tell(pb);
00122         avio_seek(pb, c->vidpos, SEEK_SET);
00123     }
00124 
00125     /* now we are ready: build format streams */
00126     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00127     st->codec->codec_id   = CODEC_ID_DXA;
00128     st->codec->width      = w;
00129     st->codec->height     = h;
00130     av_reduce(&den, &num, den, num, (1UL<<31)-1);
00131     avpriv_set_pts_info(st, 33, num, den);
00132     /* flags & 0x80 means that image is interlaced,
00133      * flags & 0x40 means that image has double height
00134      * either way set true height
00135      */
00136     if(flags & 0xC0){
00137         st->codec->height >>= 1;
00138     }
00139     c->readvid = !c->has_sound;
00140     c->vidpos  = avio_tell(pb);
00141     s->start_time = 0;
00142     s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
00143     av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
00144 
00145     return 0;
00146 }
00147 
00148 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
00149 {
00150     DXAContext *c = s->priv_data;
00151     int ret;
00152     uint32_t size;
00153     uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
00154     int pal_size = 0;
00155 
00156     if(!c->readvid && c->has_sound && c->bytes_left){
00157         c->readvid = 1;
00158         avio_seek(s->pb, c->wavpos, SEEK_SET);
00159         size = FFMIN(c->bytes_left, c->bpc);
00160         ret = av_get_packet(s->pb, pkt, size);
00161         pkt->stream_index = 1;
00162         if(ret != size)
00163             return AVERROR(EIO);
00164         c->bytes_left -= size;
00165         c->wavpos = avio_tell(s->pb);
00166         return 0;
00167     }
00168     avio_seek(s->pb, c->vidpos, SEEK_SET);
00169     while(!s->pb->eof_reached && c->frames){
00170         avio_read(s->pb, buf, 4);
00171         switch(AV_RL32(buf)){
00172         case MKTAG('N', 'U', 'L', 'L'):
00173             if(av_new_packet(pkt, 4 + pal_size) < 0)
00174                 return AVERROR(ENOMEM);
00175             pkt->stream_index = 0;
00176             if(pal_size) memcpy(pkt->data, pal, pal_size);
00177             memcpy(pkt->data + pal_size, buf, 4);
00178             c->frames--;
00179             c->vidpos = avio_tell(s->pb);
00180             c->readvid = 0;
00181             return 0;
00182         case MKTAG('C', 'M', 'A', 'P'):
00183             pal_size = 768+4;
00184             memcpy(pal, buf, 4);
00185             avio_read(s->pb, pal + 4, 768);
00186             break;
00187         case MKTAG('F', 'R', 'A', 'M'):
00188             avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
00189             size = AV_RB32(buf + 5);
00190             if(size > 0xFFFFFF){
00191                 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
00192                 return -1;
00193             }
00194             if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
00195                 return AVERROR(ENOMEM);
00196             memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
00197             ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
00198             if(ret != size){
00199                 av_free_packet(pkt);
00200                 return AVERROR(EIO);
00201             }
00202             if(pal_size) memcpy(pkt->data, pal, pal_size);
00203             pkt->stream_index = 0;
00204             c->frames--;
00205             c->vidpos = avio_tell(s->pb);
00206             c->readvid = 0;
00207             return 0;
00208         default:
00209             av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
00210             return -1;
00211         }
00212     }
00213     return AVERROR(EIO);
00214 }
00215 
00216 AVInputFormat ff_dxa_demuxer = {
00217     .name           = "dxa",
00218     .long_name      = NULL_IF_CONFIG_SMALL("DXA"),
00219     .priv_data_size = sizeof(DXAContext),
00220     .read_probe     = dxa_probe,
00221     .read_header    = dxa_read_header,
00222     .read_packet    = dxa_read_packet,
00223 };
Generated on Thu Jan 24 2013 17:08:51 for Libav by doxygen 1.7.1