dxa.c 7.18 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 23
#include <inttypes.h>

24
#include "libavutil/intreadwrite.h"
25
#include "avformat.h"
26
#include "internal.h"
27 28 29 30
#include "riff.h"

#define DXA_EXTRA_SIZE  9

31
typedef struct DXAContext {
32 33 34 35 36 37 38 39
    int frames;
    int has_sound;
    int bpc;
    uint32_t bytes_left;
    int64_t wavpos, vidpos;
    int readvid;
}DXAContext;

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

56
static int dxa_read_header(AVFormatContext *s)
57
{
58
    AVIOContext *pb = s->pb;
59 60 61 62 63 64 65
    DXAContext *c = s->priv_data;
    AVStream *st, *ast;
    uint32_t tag;
    int32_t fps;
    int w, h;
    int num, den;
    int flags;
66
    int ret;
67

68
    tag = avio_rl32(pb);
69
    if (tag != MKTAG('D', 'E', 'X', 'A'))
70
        return AVERROR_INVALIDDATA;
71 72
    flags = avio_r8(pb);
    c->frames = avio_rb16(pb);
73 74
    if(!c->frames){
        av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
75
        return AVERROR_INVALIDDATA;
76 77
    }

78
    fps = avio_rb32(pb);
79 80 81 82 83 84 85 86 87 88
    if(fps > 0){
        den = 1000;
        num = fps;
    }else if (fps < 0){
        den = 100000;
        num = -fps;
    }else{
        den = 10;
        num = 1;
    }
89 90
    w = avio_rb16(pb);
    h = avio_rb16(pb);
91 92
    c->has_sound = 0;

93
    st = avformat_new_stream(s, NULL);
94
    if (!st)
95
        return AVERROR(ENOMEM);
96 97

    // Parse WAV data header
98
    if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
99 100
        uint32_t size, fsize;
        c->has_sound = 1;
101
        size = avio_rb32(pb);
102
        c->vidpos = avio_tell(pb) + size;
103
        avio_skip(pb, 16);
104
        fsize = avio_rl32(pb);
105

106
        ast = avformat_new_stream(s, NULL);
107
        if (!ast)
108
            return AVERROR(ENOMEM);
109
        ret = ff_get_wav_header(s, pb, ast->codecpar, fsize, 0);
110 111
        if (ret < 0)
            return ret;
112 113
        if (ast->codecpar->sample_rate > 0)
            avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
114
        // find 'data' chunk
115
        while(avio_tell(pb) < c->vidpos && !avio_feof(pb)){
116 117
            tag = avio_rl32(pb);
            fsize = avio_rl32(pb);
118
            if(tag == MKTAG('d', 'a', 't', 'a')) break;
119
            avio_skip(pb, fsize);
120 121
        }
        c->bpc = (fsize + c->frames - 1) / c->frames;
122 123
        if(ast->codecpar->block_align)
            c->bpc = ((c->bpc + ast->codecpar->block_align - 1) / ast->codecpar->block_align) * ast->codecpar->block_align;
124
        c->bytes_left = fsize;
125
        c->wavpos = avio_tell(pb);
126
        avio_seek(pb, c->vidpos, SEEK_SET);
127 128 129
    }

    /* now we are ready: build format streams */
130 131 132 133
    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codecpar->codec_id   = AV_CODEC_ID_DXA;
    st->codecpar->width      = w;
    st->codecpar->height     = h;
134
    av_reduce(&den, &num, den, num, (1UL<<31)-1);
135
    avpriv_set_pts_info(st, 33, num, den);
136 137 138 139 140
    /* flags & 0x80 means that image is interlaced,
     * flags & 0x40 means that image has double height
     * either way set true height
     */
    if(flags & 0xC0){
141
        st->codecpar->height >>= 1;
142 143
    }
    c->readvid = !c->has_sound;
144
    c->vidpos  = avio_tell(pb);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    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;
162
        avio_seek(s->pb, c->wavpos, SEEK_SET);
163
        size = FFMIN(c->bytes_left, c->bpc);
164
        ret = av_get_packet(s->pb, pkt, size);
165 166
        pkt->stream_index = 1;
        if(ret != size)
167
            return AVERROR(EIO);
168
        c->bytes_left -= size;
169
        c->wavpos = avio_tell(s->pb);
170 171
        return 0;
    }
172
    avio_seek(s->pb, c->vidpos, SEEK_SET);
173
    while(!avio_feof(s->pb) && c->frames){
174
        uint32_t tag;
175 176 177 178
        if ((ret = avio_read(s->pb, buf, 4)) != 4) {
            av_log(s, AV_LOG_ERROR, "failed reading chunk type\n");
            return ret < 0 ? ret : AVERROR_INVALIDDATA;
        }
179 180
        tag = AV_RL32(buf);
        switch (tag) {
181
        case MKTAG('N', 'U', 'L', 'L'):
182 183
            if ((ret = av_new_packet(pkt, 4 + pal_size)) < 0)
                return ret;
184 185 186 187
            pkt->stream_index = 0;
            if(pal_size) memcpy(pkt->data, pal, pal_size);
            memcpy(pkt->data + pal_size, buf, 4);
            c->frames--;
188
            c->vidpos = avio_tell(s->pb);
189 190 191 192 193
            c->readvid = 0;
            return 0;
        case MKTAG('C', 'M', 'A', 'P'):
            pal_size = 768+4;
            memcpy(pal, buf, 4);
194
            avio_read(s->pb, pal + 4, 768);
195 196
            break;
        case MKTAG('F', 'R', 'A', 'M'):
197 198 199 200
            if ((ret = avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4)) != DXA_EXTRA_SIZE - 4) {
                av_log(s, AV_LOG_ERROR, "failed reading dxa_extra\n");
                return ret < 0 ? ret : AVERROR_INVALIDDATA;
            }
201 202
            size = AV_RB32(buf + 5);
            if(size > 0xFFFFFF){
203 204
                av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n",
                       size);
205
                return AVERROR_INVALIDDATA;
206
            }
207 208 209
            ret = av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size);
            if (ret < 0)
                return ret;
210
            memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
211
            ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
212
            if(ret != size){
213
                return AVERROR(EIO);
214 215 216 217
            }
            if(pal_size) memcpy(pkt->data, pal, pal_size);
            pkt->stream_index = 0;
            c->frames--;
218
            c->vidpos = avio_tell(s->pb);
219 220 221
            c->readvid = 0;
            return 0;
        default:
222
            av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag));
223
            return AVERROR_INVALIDDATA;
224 225
        }
    }
Paul B Mahol's avatar
Paul B Mahol committed
226
    return AVERROR_EOF;
227 228
}

229
AVInputFormat ff_dxa_demuxer = {
230 231 232 233 234 235
    .name           = "dxa",
    .long_name      = NULL_IF_CONFIG_SMALL("DXA"),
    .priv_data_size = sizeof(DXAContext),
    .read_probe     = dxa_probe,
    .read_header    = dxa_read_header,
    .read_packet    = dxa_read_packet,
236
};