dxa.c 6.35 KB
Newer Older
1 2
/*
 * DXA demuxer
3
 * Copyright (c) 2007 Konstantin Shishkov
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/intreadwrite.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "avformat.h"
#include "riff.h"

#define DXA_EXTRA_SIZE  9

typedef struct{
    int frames;
    int has_sound;
    int bpc;
    uint32_t bytes_left;
    int64_t wavpos, vidpos;
    int readvid;
}DXAContext;

static int dxa_probe(AVProbeData *p)
{
39 40 41 42 43
    int w, h;
    if (p->buf_size < 15)
        return 0;
    w = AV_RB16(p->buf + 11);
    h = AV_RB16(p->buf + 13);
44 45
    /* check file header */
    if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
46 47
        p->buf[2] == 'X' && p->buf[3] == 'A' &&
        w && w <= 2048 && h && h <= 2048)
48 49 50 51 52 53 54
        return AVPROBE_SCORE_MAX;
    else
        return 0;
}

static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
55
    ByteIOContext *pb = s->pb;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    DXAContext *c = s->priv_data;
    AVStream *st, *ast;
    uint32_t tag;
    int32_t fps;
    int w, h;
    int num, den;
    int flags;

    tag = get_le32(pb);
    if (tag != MKTAG('D', 'E', 'X', 'A'))
        return -1;
    flags = get_byte(pb);
    c->frames = get_be16(pb);
    if(!c->frames){
        av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
        return -1;
    }

    fps = get_be32(pb);
    if(fps > 0){
        den = 1000;
        num = fps;
    }else if (fps < 0){
        den = 100000;
        num = -fps;
    }else{
        den = 10;
        num = 1;
    }
    w = get_be16(pb);
    h = get_be16(pb);
    c->has_sound = 0;

    st = av_new_stream(s, 0);
    if (!st)
        return -1;

    // Parse WAV data header
    if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){
        uint32_t size, fsize;
        c->has_sound = 1;
        size = get_be32(pb);
        c->vidpos = url_ftell(pb) + size;
        url_fskip(pb, 16);
        fsize = get_le32(pb);

        ast = av_new_stream(s, 0);
        if (!ast)
            return -1;
105
        ff_get_wav_header(pb, ast->codec, fsize);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        // find 'data' chunk
        while(url_ftell(pb) < c->vidpos && !url_feof(pb)){
            tag = get_le32(pb);
            fsize = get_le32(pb);
            if(tag == MKTAG('d', 'a', 't', 'a')) break;
            url_fskip(pb, fsize);
        }
        c->bpc = (fsize + c->frames - 1) / c->frames;
        if(ast->codec->block_align)
            c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
        c->bytes_left = fsize;
        c->wavpos = url_ftell(pb);
        url_fseek(pb, c->vidpos, SEEK_SET);
    }

    /* now we are ready: build format streams */
122
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    st->codec->codec_id   = CODEC_ID_DXA;
    st->codec->width      = w;
    st->codec->height     = h;
    av_reduce(&den, &num, den, num, (1UL<<31)-1);
    av_set_pts_info(st, 33, num, den);
    /* flags & 0x80 means that image is interlaced,
     * flags & 0x40 means that image has double height
     * either way set true height
     */
    if(flags & 0xC0){
        st->codec->height >>= 1;
    }
    c->readvid = !c->has_sound;
    c->vidpos  = url_ftell(pb);
    s->start_time = 0;
    s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
    av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);

    return 0;
}

static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    DXAContext *c = s->priv_data;
    int ret;
    uint32_t size;
    uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
    int pal_size = 0;

    if(!c->readvid && c->has_sound && c->bytes_left){
        c->readvid = 1;
154
        url_fseek(s->pb, c->wavpos, SEEK_SET);
155
        size = FFMIN(c->bytes_left, c->bpc);
156
        ret = av_get_packet(s->pb, pkt, size);
157 158
        pkt->stream_index = 1;
        if(ret != size)
159
            return AVERROR(EIO);
160
        c->bytes_left -= size;
161
        c->wavpos = url_ftell(s->pb);
162 163
        return 0;
    }
164 165 166
    url_fseek(s->pb, c->vidpos, SEEK_SET);
    while(!url_feof(s->pb) && c->frames){
        get_buffer(s->pb, buf, 4);
167 168 169
        switch(AV_RL32(buf)){
        case MKTAG('N', 'U', 'L', 'L'):
            if(av_new_packet(pkt, 4 + pal_size) < 0)
170
                return AVERROR(ENOMEM);
171 172 173 174
            pkt->stream_index = 0;
            if(pal_size) memcpy(pkt->data, pal, pal_size);
            memcpy(pkt->data + pal_size, buf, 4);
            c->frames--;
175
            c->vidpos = url_ftell(s->pb);
176 177 178 179 180
            c->readvid = 0;
            return 0;
        case MKTAG('C', 'M', 'A', 'P'):
            pal_size = 768+4;
            memcpy(pal, buf, 4);
181
            get_buffer(s->pb, pal + 4, 768);
182 183
            break;
        case MKTAG('F', 'R', 'A', 'M'):
184
            get_buffer(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
185 186 187 188 189 190
            size = AV_RB32(buf + 5);
            if(size > 0xFFFFFF){
                av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
                return -1;
            }
            if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
191
                return AVERROR(ENOMEM);
192
            memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
193
            ret = get_buffer(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
194 195
            if(ret != size){
                av_free_packet(pkt);
196
                return AVERROR(EIO);
197 198 199 200
            }
            if(pal_size) memcpy(pkt->data, pal, pal_size);
            pkt->stream_index = 0;
            c->frames--;
201
            c->vidpos = url_ftell(s->pb);
202 203 204 205 206 207 208 209 210 211 212 213
            c->readvid = 0;
            return 0;
        default:
            av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
            return -1;
        }
    }
    return AVERROR(EIO);
}

AVInputFormat dxa_demuxer = {
    "dxa",
214
    NULL_IF_CONFIG_SMALL("DXA"),
215 216 217 218 219
    sizeof(DXAContext),
    dxa_probe,
    dxa_read_header,
    dxa_read_packet,
};