• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     ByteIOContext *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 
00064     tag = get_le32(pb);
00065     if (tag != MKTAG('D', 'E', 'X', 'A'))
00066         return -1;
00067     flags = get_byte(pb);
00068     c->frames = get_be16(pb);
00069     if(!c->frames){
00070         av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
00071         return -1;
00072     }
00073 
00074     fps = get_be32(pb);
00075     if(fps > 0){
00076         den = 1000;
00077         num = fps;
00078     }else if (fps < 0){
00079         den = 100000;
00080         num = -fps;
00081     }else{
00082         den = 10;
00083         num = 1;
00084     }
00085     w = get_be16(pb);
00086     h = get_be16(pb);
00087     c->has_sound = 0;
00088 
00089     st = av_new_stream(s, 0);
00090     if (!st)
00091         return -1;
00092 
00093     // Parse WAV data header
00094     if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){
00095         uint32_t size, fsize;
00096         c->has_sound = 1;
00097         size = get_be32(pb);
00098         c->vidpos = url_ftell(pb) + size;
00099         url_fskip(pb, 16);
00100         fsize = get_le32(pb);
00101 
00102         ast = av_new_stream(s, 0);
00103         if (!ast)
00104             return -1;
00105         ff_get_wav_header(pb, ast->codec, fsize);
00106         // find 'data' chunk
00107         while(url_ftell(pb) < c->vidpos && !url_feof(pb)){
00108             tag = get_le32(pb);
00109             fsize = get_le32(pb);
00110             if(tag == MKTAG('d', 'a', 't', 'a')) break;
00111             url_fskip(pb, fsize);
00112         }
00113         c->bpc = (fsize + c->frames - 1) / c->frames;
00114         if(ast->codec->block_align)
00115             c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
00116         c->bytes_left = fsize;
00117         c->wavpos = url_ftell(pb);
00118         url_fseek(pb, c->vidpos, SEEK_SET);
00119     }
00120 
00121     /* now we are ready: build format streams */
00122     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00123     st->codec->codec_id   = CODEC_ID_DXA;
00124     st->codec->width      = w;
00125     st->codec->height     = h;
00126     av_reduce(&den, &num, den, num, (1UL<<31)-1);
00127     av_set_pts_info(st, 33, num, den);
00128     /* flags & 0x80 means that image is interlaced,
00129      * flags & 0x40 means that image has double height
00130      * either way set true height
00131      */
00132     if(flags & 0xC0){
00133         st->codec->height >>= 1;
00134     }
00135     c->readvid = !c->has_sound;
00136     c->vidpos  = url_ftell(pb);
00137     s->start_time = 0;
00138     s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
00139     av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
00140 
00141     return 0;
00142 }
00143 
00144 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
00145 {
00146     DXAContext *c = s->priv_data;
00147     int ret;
00148     uint32_t size;
00149     uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
00150     int pal_size = 0;
00151 
00152     if(!c->readvid && c->has_sound && c->bytes_left){
00153         c->readvid = 1;
00154         url_fseek(s->pb, c->wavpos, SEEK_SET);
00155         size = FFMIN(c->bytes_left, c->bpc);
00156         ret = av_get_packet(s->pb, pkt, size);
00157         pkt->stream_index = 1;
00158         if(ret != size)
00159             return AVERROR(EIO);
00160         c->bytes_left -= size;
00161         c->wavpos = url_ftell(s->pb);
00162         return 0;
00163     }
00164     url_fseek(s->pb, c->vidpos, SEEK_SET);
00165     while(!url_feof(s->pb) && c->frames){
00166         get_buffer(s->pb, buf, 4);
00167         switch(AV_RL32(buf)){
00168         case MKTAG('N', 'U', 'L', 'L'):
00169             if(av_new_packet(pkt, 4 + pal_size) < 0)
00170                 return AVERROR(ENOMEM);
00171             pkt->stream_index = 0;
00172             if(pal_size) memcpy(pkt->data, pal, pal_size);
00173             memcpy(pkt->data + pal_size, buf, 4);
00174             c->frames--;
00175             c->vidpos = url_ftell(s->pb);
00176             c->readvid = 0;
00177             return 0;
00178         case MKTAG('C', 'M', 'A', 'P'):
00179             pal_size = 768+4;
00180             memcpy(pal, buf, 4);
00181             get_buffer(s->pb, pal + 4, 768);
00182             break;
00183         case MKTAG('F', 'R', 'A', 'M'):
00184             get_buffer(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
00185             size = AV_RB32(buf + 5);
00186             if(size > 0xFFFFFF){
00187                 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
00188                 return -1;
00189             }
00190             if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
00191                 return AVERROR(ENOMEM);
00192             memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
00193             ret = get_buffer(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
00194             if(ret != size){
00195                 av_free_packet(pkt);
00196                 return AVERROR(EIO);
00197             }
00198             if(pal_size) memcpy(pkt->data, pal, pal_size);
00199             pkt->stream_index = 0;
00200             c->frames--;
00201             c->vidpos = url_ftell(s->pb);
00202             c->readvid = 0;
00203             return 0;
00204         default:
00205             av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
00206             return -1;
00207         }
00208     }
00209     return AVERROR(EIO);
00210 }
00211 
00212 AVInputFormat dxa_demuxer = {
00213     "dxa",
00214     NULL_IF_CONFIG_SMALL("DXA"),
00215     sizeof(DXAContext),
00216     dxa_probe,
00217     dxa_read_header,
00218     dxa_read_packet,
00219 };

Generated on Fri Sep 16 2011 17:17:35 for FFmpeg by  doxygen 1.7.1