33a0dd37 |
/*
* DXA demuxer |
406792e7 |
* Copyright (c) 2007 Konstantin Shishkov |
33a0dd37 |
*
* 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
*/
|
d92024f1 |
#include <inttypes.h>
|
6a5d31ac |
#include "libavutil/intreadwrite.h" |
33a0dd37 |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
33a0dd37 |
#include "riff.h"
#define DXA_EXTRA_SIZE 9
|
daf8cf35 |
typedef struct DXAContext { |
33a0dd37 |
int frames;
int has_sound;
int bpc;
uint32_t bytes_left;
int64_t wavpos, vidpos;
int readvid;
}DXAContext;
static int dxa_probe(AVProbeData *p)
{ |
3214db98 |
int w, h;
if (p->buf_size < 15)
return 0;
w = AV_RB16(p->buf + 11);
h = AV_RB16(p->buf + 13); |
33a0dd37 |
/* check file header */
if (p->buf[0] == 'D' && p->buf[1] == 'E' && |
3214db98 |
p->buf[2] == 'X' && p->buf[3] == 'A' &&
w && w <= 2048 && h && h <= 2048) |
33a0dd37 |
return AVPROBE_SCORE_MAX;
else
return 0;
}
|
6e9651d1 |
static int dxa_read_header(AVFormatContext *s) |
33a0dd37 |
{ |
471fe57e |
AVIOContext *pb = s->pb; |
33a0dd37 |
DXAContext *c = s->priv_data;
AVStream *st, *ast;
uint32_t tag;
int32_t fps;
int w, h;
int num, den;
int flags; |
ca402f32 |
int ret; |
33a0dd37 |
|
e63a3628 |
tag = avio_rl32(pb); |
33a0dd37 |
if (tag != MKTAG('D', 'E', 'X', 'A')) |
0aabd35b |
return AVERROR_INVALIDDATA; |
e63a3628 |
flags = avio_r8(pb);
c->frames = avio_rb16(pb); |
33a0dd37 |
if(!c->frames){
av_log(s, AV_LOG_ERROR, "File contains no frames ???\n"); |
0aabd35b |
return AVERROR_INVALIDDATA; |
33a0dd37 |
}
|
e63a3628 |
fps = avio_rb32(pb); |
33a0dd37 |
if(fps > 0){
den = 1000;
num = fps;
}else if (fps < 0){
den = 100000;
num = -fps;
}else{
den = 10;
num = 1;
} |
e63a3628 |
w = avio_rb16(pb);
h = avio_rb16(pb); |
33a0dd37 |
c->has_sound = 0;
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
33a0dd37 |
if (!st) |
0aabd35b |
return AVERROR(ENOMEM); |
33a0dd37 |
// Parse WAV data header |
e63a3628 |
if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){ |
33a0dd37 |
uint32_t size, fsize;
c->has_sound = 1; |
e63a3628 |
size = avio_rb32(pb); |
384c9c2f |
c->vidpos = avio_tell(pb) + size; |
45a8a02a |
avio_skip(pb, 16); |
e63a3628 |
fsize = avio_rl32(pb); |
33a0dd37 |
|
3b3bbdd3 |
ast = avformat_new_stream(s, NULL); |
33a0dd37 |
if (!ast) |
0aabd35b |
return AVERROR(ENOMEM); |
6f69f7a8 |
ret = ff_get_wav_header(s, pb, ast->codecpar, fsize, 0); |
ca402f32 |
if (ret < 0)
return ret; |
9200514a |
if (ast->codecpar->sample_rate > 0)
avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate); |
33a0dd37 |
// find 'data' chunk |
d34ec64a |
while(avio_tell(pb) < c->vidpos && !avio_feof(pb)){ |
e63a3628 |
tag = avio_rl32(pb);
fsize = avio_rl32(pb); |
33a0dd37 |
if(tag == MKTAG('d', 'a', 't', 'a')) break; |
45a8a02a |
avio_skip(pb, fsize); |
33a0dd37 |
}
c->bpc = (fsize + c->frames - 1) / c->frames; |
9200514a |
if(ast->codecpar->block_align)
c->bpc = ((c->bpc + ast->codecpar->block_align - 1) / ast->codecpar->block_align) * ast->codecpar->block_align; |
33a0dd37 |
c->bytes_left = fsize; |
384c9c2f |
c->wavpos = avio_tell(pb); |
f59d8ff8 |
avio_seek(pb, c->vidpos, SEEK_SET); |
33a0dd37 |
}
/* now we are ready: build format streams */ |
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = AV_CODEC_ID_DXA;
st->codecpar->width = w;
st->codecpar->height = h; |
33a0dd37 |
av_reduce(&den, &num, den, num, (1UL<<31)-1); |
c3f9ebf7 |
avpriv_set_pts_info(st, 33, num, den); |
33a0dd37 |
/* flags & 0x80 means that image is interlaced,
* flags & 0x40 means that image has double height
* either way set true height
*/
if(flags & 0xC0){ |
9200514a |
st->codecpar->height >>= 1; |
33a0dd37 |
}
c->readvid = !c->has_sound; |
384c9c2f |
c->vidpos = avio_tell(pb); |
33a0dd37 |
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; |
f59d8ff8 |
avio_seek(s->pb, c->wavpos, SEEK_SET); |
33a0dd37 |
size = FFMIN(c->bytes_left, c->bpc); |
899681cd |
ret = av_get_packet(s->pb, pkt, size); |
33a0dd37 |
pkt->stream_index = 1;
if(ret != size) |
6f3e0b21 |
return AVERROR(EIO); |
33a0dd37 |
c->bytes_left -= size; |
384c9c2f |
c->wavpos = avio_tell(s->pb); |
33a0dd37 |
return 0;
} |
f59d8ff8 |
avio_seek(s->pb, c->vidpos, SEEK_SET); |
d34ec64a |
while(!avio_feof(s->pb) && c->frames){ |
bec96a72 |
uint32_t tag; |
ae09db10 |
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;
} |
bec96a72 |
tag = AV_RL32(buf);
switch (tag) { |
33a0dd37 |
case MKTAG('N', 'U', 'L', 'L'):
if(av_new_packet(pkt, 4 + pal_size) < 0) |
769e10f0 |
return AVERROR(ENOMEM); |
33a0dd37 |
pkt->stream_index = 0;
if(pal_size) memcpy(pkt->data, pal, pal_size);
memcpy(pkt->data + pal_size, buf, 4);
c->frames--; |
384c9c2f |
c->vidpos = avio_tell(s->pb); |
33a0dd37 |
c->readvid = 0;
return 0;
case MKTAG('C', 'M', 'A', 'P'):
pal_size = 768+4;
memcpy(pal, buf, 4); |
e63a3628 |
avio_read(s->pb, pal + 4, 768); |
33a0dd37 |
break;
case MKTAG('F', 'R', 'A', 'M'): |
ae09db10 |
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;
} |
33a0dd37 |
size = AV_RB32(buf + 5);
if(size > 0xFFFFFF){ |
d92024f1 |
av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n",
size); |
0aabd35b |
return AVERROR_INVALIDDATA; |
33a0dd37 |
}
if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0) |
769e10f0 |
return AVERROR(ENOMEM); |
33a0dd37 |
memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE); |
e63a3628 |
ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size); |
33a0dd37 |
if(ret != size){ |
ce70f28a |
av_packet_unref(pkt); |
6f3e0b21 |
return AVERROR(EIO); |
33a0dd37 |
}
if(pal_size) memcpy(pkt->data, pal, pal_size);
pkt->stream_index = 0;
c->frames--; |
384c9c2f |
c->vidpos = avio_tell(s->pb); |
33a0dd37 |
c->readvid = 0;
return 0;
default: |
bec96a72 |
av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag)); |
0aabd35b |
return AVERROR_INVALIDDATA; |
33a0dd37 |
}
} |
c5008135 |
return AVERROR_EOF; |
33a0dd37 |
}
|
66355be3 |
AVInputFormat ff_dxa_demuxer = { |
dfc2c4d9 |
.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, |
33a0dd37 |
}; |