2007-03-14 14:49:49 +00:00
|
|
|
/*
|
|
|
|
* DXA demuxer
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2007 Konstantin Shishkov
|
2007-03-14 14:49:49 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2007-03-14 14:49:49 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-03-14 14:49:49 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-03-14 14:49:49 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-03-14 14:49:49 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2014-03-10 14:35:59 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2009-01-11 22:19:48 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2007-03-14 14:49:49 +00:00
|
|
|
#include "avformat.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2007-03-14 14:49:49 +00:00
|
|
|
#include "riff.h"
|
|
|
|
|
|
|
|
#define DXA_EXTRA_SIZE 9
|
|
|
|
|
2014-09-22 07:19:33 +00:00
|
|
|
typedef struct DXAContext {
|
2007-03-14 14:49:49 +00:00
|
|
|
int frames;
|
|
|
|
int has_sound;
|
|
|
|
int bpc;
|
|
|
|
uint32_t bytes_left;
|
|
|
|
int64_t wavpos, vidpos;
|
|
|
|
int readvid;
|
|
|
|
}DXAContext;
|
|
|
|
|
|
|
|
static int dxa_probe(AVProbeData *p)
|
|
|
|
{
|
2009-09-17 18:08:07 +00:00
|
|
|
int w, h;
|
|
|
|
if (p->buf_size < 15)
|
|
|
|
return 0;
|
|
|
|
w = AV_RB16(p->buf + 11);
|
|
|
|
h = AV_RB16(p->buf + 13);
|
2007-03-14 14:49:49 +00:00
|
|
|
/* check file header */
|
|
|
|
if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
|
2009-09-17 18:08:07 +00:00
|
|
|
p->buf[2] == 'X' && p->buf[3] == 'A' &&
|
|
|
|
w && w <= 2048 && h && h <= 2048)
|
2007-03-14 14:49:49 +00:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int dxa_read_header(AVFormatContext *s)
|
2007-03-14 14:49:49 +00:00
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2007-03-14 14:49:49 +00:00
|
|
|
DXAContext *c = s->priv_data;
|
|
|
|
AVStream *st, *ast;
|
|
|
|
uint32_t tag;
|
|
|
|
int32_t fps;
|
|
|
|
int w, h;
|
|
|
|
int num, den;
|
|
|
|
int flags;
|
2011-04-12 15:44:20 +00:00
|
|
|
int ret;
|
2007-03-14 14:49:49 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
tag = avio_rl32(pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
if (tag != MKTAG('D', 'E', 'X', 'A'))
|
|
|
|
return -1;
|
2011-02-21 15:43:01 +00:00
|
|
|
flags = avio_r8(pb);
|
|
|
|
c->frames = avio_rb16(pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
if(!c->frames){
|
|
|
|
av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
fps = avio_rb32(pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
if(fps > 0){
|
|
|
|
den = 1000;
|
|
|
|
num = fps;
|
|
|
|
}else if (fps < 0){
|
|
|
|
den = 100000;
|
|
|
|
num = -fps;
|
|
|
|
}else{
|
|
|
|
den = 10;
|
|
|
|
num = 1;
|
|
|
|
}
|
2011-02-21 15:43:01 +00:00
|
|
|
w = avio_rb16(pb);
|
|
|
|
h = avio_rb16(pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
c->has_sound = 0;
|
|
|
|
|
2011-06-18 09:43:24 +00:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2007-03-14 14:49:49 +00:00
|
|
|
if (!st)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Parse WAV data header
|
2011-02-21 15:43:01 +00:00
|
|
|
if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
|
2007-03-14 14:49:49 +00:00
|
|
|
uint32_t size, fsize;
|
|
|
|
c->has_sound = 1;
|
2011-02-21 15:43:01 +00:00
|
|
|
size = avio_rb32(pb);
|
2011-03-03 19:11:45 +00:00
|
|
|
c->vidpos = avio_tell(pb) + size;
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 16);
|
2011-02-21 15:43:01 +00:00
|
|
|
fsize = avio_rl32(pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
|
2011-06-18 09:43:24 +00:00
|
|
|
ast = avformat_new_stream(s, NULL);
|
2007-03-14 14:49:49 +00:00
|
|
|
if (!ast)
|
|
|
|
return -1;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
ret = ff_get_wav_header(s, pb, ast->codecpar, fsize);
|
2011-04-12 15:44:20 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
if (ast->codecpar->sample_rate > 0)
|
|
|
|
avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
|
2007-03-14 14:49:49 +00:00
|
|
|
// find 'data' chunk
|
2011-03-07 20:50:25 +00:00
|
|
|
while(avio_tell(pb) < c->vidpos && !pb->eof_reached){
|
2011-02-21 15:43:01 +00:00
|
|
|
tag = avio_rl32(pb);
|
|
|
|
fsize = avio_rl32(pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
if(tag == MKTAG('d', 'a', 't', 'a')) break;
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, fsize);
|
2007-03-14 14:49:49 +00:00
|
|
|
}
|
|
|
|
c->bpc = (fsize + c->frames - 1) / c->frames;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
if(ast->codecpar->block_align)
|
|
|
|
c->bpc = ((c->bpc + ast->codecpar->block_align - 1) / ast->codecpar->block_align) * ast->codecpar->block_align;
|
2007-03-14 14:49:49 +00:00
|
|
|
c->bytes_left = fsize;
|
2011-03-03 19:11:45 +00:00
|
|
|
c->wavpos = avio_tell(pb);
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, c->vidpos, SEEK_SET);
|
2007-03-14 14:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now we are ready: build format streams */
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_DXA;
|
|
|
|
st->codecpar->width = w;
|
|
|
|
st->codecpar->height = h;
|
2007-03-14 14:49:49 +00:00
|
|
|
av_reduce(&den, &num, den, num, (1UL<<31)-1);
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 33, num, den);
|
2007-03-14 14:49:49 +00:00
|
|
|
/* flags & 0x80 means that image is interlaced,
|
|
|
|
* flags & 0x40 means that image has double height
|
|
|
|
* either way set true height
|
|
|
|
*/
|
|
|
|
if(flags & 0xC0){
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->height >>= 1;
|
2007-03-14 14:49:49 +00:00
|
|
|
}
|
|
|
|
c->readvid = !c->has_sound;
|
2011-03-03 19:11:45 +00:00
|
|
|
c->vidpos = avio_tell(pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
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;
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(s->pb, c->wavpos, SEEK_SET);
|
2007-03-14 14:49:49 +00:00
|
|
|
size = FFMIN(c->bytes_left, c->bpc);
|
2007-11-21 07:41:00 +00:00
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
2007-03-14 14:49:49 +00:00
|
|
|
pkt->stream_index = 1;
|
|
|
|
if(ret != size)
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2007-03-14 14:49:49 +00:00
|
|
|
c->bytes_left -= size;
|
2011-03-03 19:11:45 +00:00
|
|
|
c->wavpos = avio_tell(s->pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(s->pb, c->vidpos, SEEK_SET);
|
2011-03-07 20:50:25 +00:00
|
|
|
while(!s->pb->eof_reached && c->frames){
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(s->pb, buf, 4);
|
2007-03-14 14:49:49 +00:00
|
|
|
switch(AV_RL32(buf)){
|
|
|
|
case MKTAG('N', 'U', 'L', 'L'):
|
|
|
|
if(av_new_packet(pkt, 4 + pal_size) < 0)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2007-03-14 14:49:49 +00:00
|
|
|
pkt->stream_index = 0;
|
|
|
|
if(pal_size) memcpy(pkt->data, pal, pal_size);
|
|
|
|
memcpy(pkt->data + pal_size, buf, 4);
|
|
|
|
c->frames--;
|
2011-03-03 19:11:45 +00:00
|
|
|
c->vidpos = avio_tell(s->pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
c->readvid = 0;
|
|
|
|
return 0;
|
|
|
|
case MKTAG('C', 'M', 'A', 'P'):
|
|
|
|
pal_size = 768+4;
|
|
|
|
memcpy(pal, buf, 4);
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(s->pb, pal + 4, 768);
|
2007-03-14 14:49:49 +00:00
|
|
|
break;
|
|
|
|
case MKTAG('F', 'R', 'A', 'M'):
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
|
2007-03-14 14:49:49 +00:00
|
|
|
size = AV_RB32(buf + 5);
|
|
|
|
if(size > 0xFFFFFF){
|
2014-03-10 14:35:59 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n",
|
|
|
|
size);
|
2007-03-14 14:49:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2007-03-14 14:49:49 +00:00
|
|
|
memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
|
2011-02-21 15:43:01 +00:00
|
|
|
ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
|
2007-03-14 14:49:49 +00:00
|
|
|
if(ret != size){
|
2015-10-23 09:11:31 +00:00
|
|
|
av_packet_unref(pkt);
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2007-03-14 14:49:49 +00:00
|
|
|
}
|
|
|
|
if(pal_size) memcpy(pkt->data, pal, pal_size);
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
c->frames--;
|
2011-03-03 19:11:45 +00:00
|
|
|
c->vidpos = avio_tell(s->pb);
|
2007-03-14 14:49:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_dxa_demuxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.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,
|
2007-03-14 14:49:49 +00:00
|
|
|
};
|