xref: /third_party/ffmpeg/libavformat/dxa.c (revision cabdff1a)
1/*
2 * DXA demuxer
3 * Copyright (c) 2007 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <inttypes.h>
23
24#include "libavutil/intreadwrite.h"
25#include "avformat.h"
26#include "internal.h"
27#include "riff.h"
28
29#define DXA_EXTRA_SIZE  9
30
31typedef struct DXAContext {
32    int frames;
33    int has_sound;
34    int bpc;
35    uint32_t bytes_left;
36    int64_t wavpos, vidpos;
37    int readvid;
38}DXAContext;
39
40static int dxa_probe(const AVProbeData *p)
41{
42    int w, h;
43    if (p->buf_size < 15)
44        return 0;
45    w = AV_RB16(p->buf + 11);
46    h = AV_RB16(p->buf + 13);
47    /* check file header */
48    if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
49        p->buf[2] == 'X' && p->buf[3] == 'A' &&
50        w && w <= 2048 && h && h <= 2048)
51        return AVPROBE_SCORE_MAX;
52    else
53        return 0;
54}
55
56static int dxa_read_header(AVFormatContext *s)
57{
58    AVIOContext *pb = s->pb;
59    DXAContext *c = s->priv_data;
60    AVStream *st, *ast;
61    uint32_t tag;
62    int32_t fps;
63    int w, h;
64    int num, den;
65    int flags;
66    int ret;
67
68    tag = avio_rl32(pb);
69    if (tag != MKTAG('D', 'E', 'X', 'A'))
70        return AVERROR_INVALIDDATA;
71    flags = avio_r8(pb);
72    c->frames = avio_rb16(pb);
73    if(!c->frames){
74        av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
75        return AVERROR_INVALIDDATA;
76    }
77
78    fps = avio_rb32(pb);
79    if(fps > 0){
80        den = 1000;
81        num = fps;
82    }else if (fps < 0 && fps > INT_MIN){
83        den = 100000;
84        num = -fps;
85    }else{
86        den = 10;
87        num = 1;
88    }
89    w = avio_rb16(pb);
90    h = avio_rb16(pb);
91    c->has_sound = 0;
92
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        uint32_t size, fsize;
100        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        if (ret < 0)
111            return ret;
112        if (ast->codecpar->sample_rate > 0)
113            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            tag = avio_rl32(pb);
117            fsize = avio_rl32(pb);
118            if(tag == MKTAG('d', 'a', 't', 'a')) break;
119            avio_skip(pb, fsize);
120        }
121        c->bpc = (fsize + (int64_t)c->frames - 1) / c->frames;
122        if(ast->codecpar->block_align) {
123            if (c->bpc > INT_MAX - ast->codecpar->block_align + 1)
124                return AVERROR_INVALIDDATA;
125            c->bpc = ((c->bpc + ast->codecpar->block_align - 1) / ast->codecpar->block_align) * ast->codecpar->block_align;
126        }
127        c->bytes_left = fsize;
128        c->wavpos = avio_tell(pb);
129        avio_seek(pb, c->vidpos, SEEK_SET);
130    }
131
132    /* now we are ready: build format streams */
133    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
134    st->codecpar->codec_id   = AV_CODEC_ID_DXA;
135    st->codecpar->width      = w;
136    st->codecpar->height     = h;
137    av_reduce(&den, &num, den, num, (1UL<<31)-1);
138    avpriv_set_pts_info(st, 33, num, den);
139    /* flags & 0x80 means that image is interlaced,
140     * flags & 0x40 means that image has double height
141     * either way set true height
142     */
143    if(flags & 0xC0){
144        st->codecpar->height >>= 1;
145    }
146    c->readvid = !c->has_sound;
147    c->vidpos  = avio_tell(pb);
148    s->start_time = 0;
149    s->duration = av_rescale(c->frames, AV_TIME_BASE * (int64_t)num, den);
150    av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
151
152    return 0;
153}
154
155static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
156{
157    DXAContext *c = s->priv_data;
158    int ret;
159    uint32_t size;
160    uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
161    int pal_size = 0;
162
163    if(!c->readvid && c->has_sound && c->bytes_left){
164        c->readvid = 1;
165        avio_seek(s->pb, c->wavpos, SEEK_SET);
166        size = FFMIN(c->bytes_left, c->bpc);
167        ret = av_get_packet(s->pb, pkt, size);
168        pkt->stream_index = 1;
169        if(ret != size)
170            return AVERROR(EIO);
171        c->bytes_left -= size;
172        c->wavpos = avio_tell(s->pb);
173        return 0;
174    }
175    avio_seek(s->pb, c->vidpos, SEEK_SET);
176    while(!avio_feof(s->pb) && c->frames){
177        uint32_t tag;
178        if ((ret = avio_read(s->pb, buf, 4)) != 4) {
179            av_log(s, AV_LOG_ERROR, "failed reading chunk type\n");
180            return ret < 0 ? ret : AVERROR_INVALIDDATA;
181        }
182        tag = AV_RL32(buf);
183        switch (tag) {
184        case MKTAG('N', 'U', 'L', 'L'):
185            if ((ret = av_new_packet(pkt, 4 + pal_size)) < 0)
186                return ret;
187            pkt->stream_index = 0;
188            if(pal_size) memcpy(pkt->data, pal, pal_size);
189            memcpy(pkt->data + pal_size, buf, 4);
190            c->frames--;
191            c->vidpos = avio_tell(s->pb);
192            c->readvid = 0;
193            return 0;
194        case MKTAG('C', 'M', 'A', 'P'):
195            pal_size = 768+4;
196            memcpy(pal, buf, 4);
197            avio_read(s->pb, pal + 4, 768);
198            break;
199        case MKTAG('F', 'R', 'A', 'M'):
200            if ((ret = avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4)) != DXA_EXTRA_SIZE - 4) {
201                av_log(s, AV_LOG_ERROR, "failed reading dxa_extra\n");
202                return ret < 0 ? ret : AVERROR_INVALIDDATA;
203            }
204            size = AV_RB32(buf + 5);
205            if(size > 0xFFFFFF){
206                av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n",
207                       size);
208                return AVERROR_INVALIDDATA;
209            }
210            ret = av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size);
211            if (ret < 0)
212                return ret;
213            memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
214            ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
215            if(ret != size){
216                return AVERROR(EIO);
217            }
218            if(pal_size) memcpy(pkt->data, pal, pal_size);
219            pkt->stream_index = 0;
220            c->frames--;
221            c->vidpos = avio_tell(s->pb);
222            c->readvid = 0;
223            return 0;
224        default:
225            av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag));
226            return AVERROR_INVALIDDATA;
227        }
228    }
229    return AVERROR_EOF;
230}
231
232const AVInputFormat ff_dxa_demuxer = {
233    .name           = "dxa",
234    .long_name      = NULL_IF_CONFIG_SMALL("DXA"),
235    .priv_data_size = sizeof(DXAContext),
236    .read_probe     = dxa_probe,
237    .read_header    = dxa_read_header,
238    .read_packet    = dxa_read_packet,
239};
240