* ok - let's start with avifile->ffmpeg morphing

for the begining it's major rewrite of asf parsing code
  (tested only inside avifile at this moment)
  it handles descrambling (though without WMA it's probably
  useless inside ffmpeg...
* extended AVStream structure to return information about
  stream time length
* extended AVStream to export  extra data found after standard
  headers - not really usefull for ffmpeg - but Windows codecs
  need them.
* asf parsing is not yet finished but works nicely already
  (at 100% better them before :))

Originally committed as revision 846 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
Zdenek Kabelac 2002-08-08 16:43:47 +00:00
parent 47ceabc959
commit 2a10020bf3
2 changed files with 354 additions and 174 deletions

View File

@ -32,17 +32,59 @@ typedef struct {
/* use for reading */
AVPacket pkt;
int frag_offset;
int timestamp;
INT64 duration;
int ds_span; /* descrambling */
int ds_packet_size;
int ds_chunk_size;
int ds_data_size;
int ds_silence_data;
} ASFStream;
typedef struct {
UINT32 v1;
UINT16 v2;
UINT16 v3;
UINT8 v4[8];
} GUID;
typedef struct __attribute__((packed)) {
GUID guid; // generated by client computer
uint64_t file_size; // in bytes
// invalid if broadcasting
uint64_t create_time; // time of creation, in 100-nanosecond units since 1.1.1601
// invalid if broadcasting
uint64_t packets_count; // how many packets are there in the file
// invalid if broadcasting
uint64_t play_time; // play time, in 100-nanosecond units
// invalid if broadcasting
uint64_t send_time; // time to send file, in 100-nanosecond units
// invalid if broadcasting (could be ignored)
uint32_t preroll; // timestamp of the first packet, in milliseconds
// if nonzero - substract from time
uint32_t ignore; // preroll is 64bit - but let's just ignore it
uint32_t flags; // 0x01 - broadcast
// 0x02 - seekable
// rest is reserved should be 0
uint32_t min_pktsize; // size of a data packet
// invalid if broadcasting
uint32_t max_pktsize; // shall be the same as for min_pktsize
// invalid if broadcasting
uint32_t max_bitrate; // bandwith of stream in bps
// should be the sum of bitrates of the
// individual media streams
} ASFMainHeader;
typedef struct {
int seqno;
int packet_size;
int is_streamed;
ASFStream streams[2];
int asfid2avid[128];
ASFStream streams[128]; // it's max number
/* non streamed additonnal info */
int data_offset;
INT64 nb_packets;
INT64 duration; /* in 100ns units */
/* packet filling */
@ -53,15 +95,29 @@ typedef struct {
UINT8 packet_buf[PACKET_SIZE];
ByteIOContext pb;
/* only for reading */
int packet_padsize;
} ASFContext;
uint64_t data_offset; /* begining of the first data packet */
typedef struct {
UINT32 v1;
UINT16 v2;
UINT16 v3;
UINT8 v4[8];
} GUID;
ASFMainHeader hdr;
int packet_flags;
int packet_property;
int packet_timestamp;
int packet_segsizetype;
int packet_segments;
int packet_seq;
int packet_replic_size;
int packet_key_frame;
int packet_padsize;
int packet_frag_offset;
int packet_frag_size;
int packet_frag_timestamp;
int packet_obj_size;
int packet_time_delta;
int packet_time_start;
int stream_index;
ASFStream* asf_st; /* currently decoded stream */
} ASFContext;
static const GUID asf_header = {
0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
@ -260,7 +316,7 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu
}
put_guid(pb, &asf_header);
put_le64(pb, 0); /* header length, will be patched after */
put_le64(pb, -1); /* header length, will be patched after */
put_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
put_byte(pb, 1); /* ??? */
put_byte(pb, 2); /* ??? */
@ -747,18 +803,19 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
if (gsize < 24)
goto fail;
if (!memcmp(&g, &file_header, sizeof(GUID))) {
get_guid(pb, &g);
get_le64(pb); /* file size */
get_le64(pb); /* file time */
get_le64(pb); /* nb_packets */
get_le64(pb); /* length 0 in us */
get_le64(pb); /* length 1 in us */
get_le32(pb);
get_le32(pb);
get_le32(pb);
asf->packet_size = get_le32(pb);
get_le32(pb);
get_le32(pb);
get_guid(pb, &asf->hdr.guid);
asf->hdr.file_size = get_le64(pb);
asf->hdr.create_time = get_le64(pb);
asf->hdr.packets_count = get_le64(pb);
asf->hdr.play_time = get_le64(pb);
asf->hdr.send_time = get_le64(pb);
asf->hdr.preroll = get_le32(pb);
asf->hdr.ignore = get_le32(pb);
asf->hdr.flags = get_le32(pb);
asf->hdr.min_pktsize = get_le32(pb);
asf->hdr.max_pktsize = get_le32(pb);
asf->hdr.max_bitrate = get_le32(pb);
asf->packet_size = asf->hdr.max_pktsize;
} else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
int type, id, total_size;
unsigned int tag1;
@ -769,12 +826,12 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
st = av_mallocz(sizeof(AVStream));
if (!st)
goto fail;
s->streams[s->nb_streams++] = st;
s->streams[s->nb_streams] = st;
asf_st = av_mallocz(sizeof(ASFStream));
if (!asf_st)
goto fail;
st->priv_data = asf_st;
st->time_length = (asf->hdr.send_time - asf->hdr.preroll) / 10000;
get_guid(pb, &g);
if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
type = CODEC_TYPE_AUDIO;
@ -787,7 +844,10 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
total_size = get_le64(pb);
get_le32(pb);
get_le32(pb);
st->id = get_le16(pb); /* stream id */
st->id = get_le16(pb) & 0x7f; /* stream id */
// mapping of asf ID to AV stream ID;
asf->asfid2avid[st->id] = s->nb_streams++;
get_le32(pb);
st->codec.codec_type = type;
if (type == CODEC_TYPE_AUDIO) {
@ -796,12 +856,32 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
st->codec.channels = get_le16(pb);
st->codec.sample_rate = get_le32(pb);
st->codec.bit_rate = get_le32(pb) * 8;
get_le16(pb); /* block align */
st->codec.block_align = get_le16(pb); /* block align */
bps = get_le16(pb); /* bits per sample */
st->codec.codec_id = wav_codec_get_id(id, bps);
size = get_le16(pb);
url_fskip(pb, size);
st->extra_data = av_mallocz(size);
get_buffer(pb, st->extra_data, size);
st->extra_data_size = size;
/* We have to init the frame size at some point .... */
pos2 = url_ftell(pb);
if (gsize > (pos2 + 8 - pos1 + 24))
{
asf_st->ds_span = get_byte(pb);
asf_st->ds_packet_size = get_le16(pb);
asf_st->ds_chunk_size = get_le16(pb);
asf_st->ds_data_size = get_le16(pb);
asf_st->ds_silence_data = get_byte(pb);
}
//printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
// asf_st->ds_packet_size, asf_st->ds_chunk_size,
// asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
if (asf_st->ds_span > 1)
{
if (!asf_st->ds_chunk_size
|| (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1))
asf_st->ds_span = 0; // disable descrambling
}
switch (st->codec.codec_id) {
case CODEC_ID_MP3LAME:
st->codec.frame_size = MPA_FRAME_SIZE;
@ -829,6 +909,7 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
get_le32(pb); /* size */
st->codec.width = get_le32(pb);
st->codec.height = get_le32(pb);
/* not available for asf */
st->codec.frame_rate = 25 * FRAME_RATE_BASE; /* XXX: find it */
get_le16(pb); /* panes */
get_le16(pb); /* depth */
@ -910,129 +991,218 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
return -1;
}
#define DO_2BITS(bits, var, defval) \
switch (bits & 3) \
{ \
case 3: var = get_le32(pb); rsize += 4; break; \
case 2: var = get_le16(pb); rsize += 2; break; \
case 1: var = get_byte(pb); rsize++; break; \
default: var = defval; break; \
}
static int asf_get_packet(AVFormatContext *s)
{
ASFContext *asf = s->priv_data;
ByteIOContext *pb = &s->pb;
int c, flags, timestamp, hdr_size;
hdr_size = 12;
c = get_byte(pb);
uint32_t packet_length, padsize;
int rsize = 11;
int c = get_byte(pb);
if (c != 0x82)
{
printf("BAD HRD %x at:%Ld\n", c, url_ftell(pb));
return -EIO;
get_le16(pb);
flags = get_byte(pb);
get_byte(pb);
asf->packet_padsize = 0;
if (flags & 0x10) {
asf->packet_padsize = get_le16(pb);
hdr_size += 2;
} else if (flags & 0x08) {
asf->packet_padsize = get_byte(pb);
hdr_size++;
}
timestamp = get_le32(pb);
if ((c & 0x0f) == 2) { // always true for now
if (get_le16(pb) != 0)
{
printf("BAD NO ZERO\n");
return -EIO;
}
}
asf->packet_flags = get_byte(pb);
asf->packet_property = get_byte(pb);
DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size);
DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
asf->packet_timestamp = get_le32(pb);
get_le16(pb); /* duration */
get_byte(pb); /* nb_frames */
// rsize has 11 bytes static bytes which have to be present
if (asf->packet_flags & 0x01) {
asf->packet_segsizetype = get_byte(pb); rsize++;
asf->packet_segments = asf->packet_segsizetype & 0x3f;
} else {
asf->packet_segments = 1;
asf->packet_segsizetype = 0x80;
}
asf->packet_size_left = packet_length - padsize - rsize;
asf->packet_padsize = padsize;
#ifdef DEBUG
printf("packet: size=%d padsize=%d\n", asf->packet_size, asf->packet_padsize);
printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left);
#endif
asf->packet_size_left = asf->packet_size - hdr_size;
return 0;
}
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
{
ASFContext *asf = s->priv_data;
AVStream *st;
ASFStream *asf_st;
ASFStream *asf_st = 0;
ByteIOContext *pb = &s->pb;
int ret, num, seq, frag_offset, payload_size, frag_len;
int key_frame;
int timestamp, i;
for(;;) {
if (asf->packet_size_left < FRAME_HEADER_SIZE ||
asf->packet_size_left <= asf->packet_padsize) {
static int pc = 0;
for (;;) {
int rsize = 0;
if (asf->packet_size_left < FRAME_HEADER_SIZE) {
//asf->packet_size_left <= asf->packet_padsize) {
int ret;
//printf("PACKETLEFTSIZE %d\n", asf->packet_size_left);
/* fail safe */
if (asf->packet_size_left)
url_fskip(pb, asf->packet_size_left);
if (asf->packet_padsize)
url_fskip(pb, asf->packet_padsize);
ret = asf_get_packet(s);
//printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
if (ret < 0)
return -EIO;
continue;
}
if (1) { // FIXME - handle replicated multi packets
/* read frame header */
num = get_byte(pb);
if (num & 0x80)
key_frame = 1;
else
key_frame = 0;
num &= 0x7f;
seq = get_byte(pb);
frag_offset = get_le32(pb);
get_byte(pb); /* flags */
payload_size = get_le32(pb);
timestamp = get_le32(pb);
frag_len = get_le16(pb);
#ifdef DEBUG
printf("num=%d seq=%d totsize=%d frag_off=%d frag_size=%d\n",
num, seq, payload_size, frag_offset, frag_len);
#endif
st = NULL;
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
if (st->id == num)
break;
}
asf->packet_size_left -= FRAME_HEADER_SIZE + frag_len;
if (i == s->nb_streams) {
int num = get_byte(pb);
rsize++;
asf->packet_key_frame = (num & 0x80) >> 7;
asf->stream_index = asf->asfid2avid[num & 0x7f];
if (asf->stream_index < 0)
{
/* unhandled packet (should not happen) */
url_fskip(pb, frag_len);
} else {
asf_st = st->priv_data;
if (asf_st->frag_offset == 0) {
/* new packet */
av_new_packet(&asf_st->pkt, payload_size);
asf_st->seq = seq;
if (key_frame)
asf_st->pkt.flags |= PKT_FLAG_KEY;
url_fskip(pb, asf->packet_frag_size);
asf->packet_size_left -= asf->packet_frag_size;
//printf("#####xxxxxx###### skip %d\n", asf->packet_frag_size);
continue;
// FIXME
}
asf->asf_st = s->streams[asf->stream_index]->priv_data;
//printf("ASFST %p %d <> %d\n", asf->asf_st, asf->stream_index, num & 0x7f);
// sequence should be ignored!
DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
asf_st->pkt.pts = timestamp;
if (asf->packet_replic_size > 1) {
// it should be always at least 8 bytes - FIXME validate
asf->packet_obj_size = get_le32(pb);
asf->packet_frag_timestamp = get_le32(pb); // timestamp
rsize += asf->packet_replic_size; // FIXME - check validity
} else {
if (seq == asf_st->seq &&
frag_offset == asf_st->frag_offset) {
/* continuing packet */
// multipacket - frag_offset is beginig timestamp
asf->packet_time_start = asf->packet_frag_offset;
asf->packet_frag_offset = 0;
asf->packet_frag_timestamp = asf->packet_timestamp;
if (asf->packet_replic_size == 1) {
asf->packet_time_delta = get_byte(pb);
rsize++;
}
}
if (asf->packet_flags & 0x01) {
DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
//printf("Fragsize %d\n", asf->packet_frag_size);
} else {
asf->packet_frag_size = asf->packet_size_left - rsize;
//printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
}
#undef DO_2BITS
asf->packet_size_left -= rsize;
//printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
}
asf_st = asf->asf_st;
if ((asf->packet_frag_offset != asf_st->frag_offset
|| (asf->packet_frag_offset
&& asf->packet_seq != asf_st->seq)) // seq should be ignored
) {
/* cannot continue current packet: free it */
// FIXME better check if packet was already allocated
printf("asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
asf_st->pkt.size,
asf->packet_obj_size,
asf->packet_frag_offset, asf_st->frag_offset,
asf->packet_seq, asf_st->seq, asf->packet_frag_size);
if (asf_st->pkt.size)
av_free_packet(&asf_st->pkt);
asf_st->frag_offset = 0;
if (frag_offset != 0) {
if (asf->packet_frag_offset != 0) {
/* cannot create new packet */
url_fskip(pb, frag_len);
goto next_frame;
} else {
/* create new packet */
av_new_packet(&asf_st->pkt, payload_size);
asf_st->seq = seq;
url_fskip(pb, asf->packet_frag_size);
printf("asf parser skiping %db\n", asf->packet_frag_size);
asf->packet_size_left -= asf->packet_frag_size;
continue;
}
}
}
/* read data */
get_buffer(pb, asf_st->pkt.data + frag_offset, frag_len);
asf_st->frag_offset += frag_len;
/* test if whole packet read */
if (asf_st->frag_offset == asf_st->pkt.size) {
/* return packet */
asf_st->pkt.stream_index = i;
asf_st->frag_offset = 0;
memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
break;
}
}
next_frame:;
if (asf->packet_replic_size == 1)
{
// frag_size is now begining timestamp
asf->packet_frag_timestamp = asf->packet_time_start;
asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
asf->packet_size_left--;
//printf("COMPRESS size %d %d\n", asf->packet_obj_size, asf->packet_frag_timestamp);
}
//printf("PACKET OFFSET %d\n", asf_st->frag_offset);
if (asf_st->frag_offset == 0) {
/* new packet */
av_new_packet(&asf_st->pkt, asf->packet_obj_size);
asf_st->seq = asf->packet_seq;
asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
asf_st->pkt.stream_index = asf->stream_index;
if (asf->packet_key_frame)
asf_st->pkt.flags |= PKT_FLAG_KEY;
}
/* read data */
//printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
// asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
// asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
asf->packet_frag_size);
asf_st->frag_offset += asf->packet_frag_size;
asf->packet_size_left -= asf->packet_frag_size;
/* test if whole packet is read */
if (asf_st->frag_offset == asf_st->pkt.size) {
/* return packet */
if (asf_st->ds_span > 1) {
// descramble packet
char* newdata = av_malloc(asf_st->pkt.size);
if (newdata) {
int offset = 0;
while (offset < asf_st->pkt.size) {
int off = offset / asf_st->ds_chunk_size;
int row = off / asf_st->ds_span;
int col = off % asf_st->ds_span;
int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
//printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
memcpy(newdata + offset,
asf_st->pkt.data + idx * asf_st->ds_chunk_size,
asf_st->ds_chunk_size);
offset += asf_st->ds_chunk_size;
}
av_free(asf_st->pkt.data);
asf_st->pkt.data = newdata;
}
}
asf_st->frag_offset = 0;
memcpy(pkt, &asf_st->pkt, sizeof(AVPacket));
//printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
asf_st->pkt.size = 0;
asf_st->pkt.data = 0;
// FIXME descrambling
break; // packet completed
}
}
return 0;
}
@ -1049,6 +1219,12 @@ static int asf_read_close(AVFormatContext *s)
return 0;
}
static int asf_read_seek(AVFormatContext *s, int64_t pts)
{
printf("SEEK TO %Ld", pts);
return -1;
}
AVInputFormat asf_iformat = {
"asf",
"asf format",
@ -1057,6 +1233,7 @@ AVInputFormat asf_iformat = {
asf_read_header,
asf_read_packet,
asf_read_close,
asf_read_seek,
};
AVOutputFormat asf_oformat = {

View File

@ -3,7 +3,7 @@
#define LIBAVFORMAT_VERSION_INT 0x000406
#define LIBAVFORMAT_VERSION "0.4.6"
#define LIBAVFORMAT_BUILD 4601
#define LIBAVFORMAT_BUILD 4602
#include "avcodec.h"
@ -121,6 +121,9 @@ typedef struct AVStream {
int id; /* format specific stream id */
AVCodecContext codec; /* codec context */
int r_frame_rate; /* real frame rate of the stream */
uint64_t time_length; /* real length of the stream in miliseconds */
void* extra_data; /* some extra data - i.e. longer WAVEFORMATEX */
int extra_data_size; /* size of extra data chunk */
void *priv_data;
/* internal data used in av_find_stream_info() */
int codec_info_state;