Commit 4dd43382 authored by kabi's avatar kabi

* 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 :))


git-svn-id: file:///var/local/repositories/ffmpeg/trunk@846 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
parent b11547df
...@@ -32,17 +32,59 @@ typedef struct { ...@@ -32,17 +32,59 @@ typedef struct {
/* use for reading */ /* use for reading */
AVPacket pkt; AVPacket pkt;
int frag_offset; int frag_offset;
int timestamp;
INT64 duration; INT64 duration;
int ds_span; /* descrambling */
int ds_packet_size;
int ds_chunk_size;
int ds_data_size;
int ds_silence_data;
} ASFStream; } 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 { typedef struct {
int seqno; int seqno;
int packet_size; int packet_size;
int is_streamed; int is_streamed;
int asfid2avid[128];
ASFStream streams[2]; ASFStream streams[128]; // it's max number
/* non streamed additonnal info */ /* non streamed additonnal info */
int data_offset;
INT64 nb_packets; INT64 nb_packets;
INT64 duration; /* in 100ns units */ INT64 duration; /* in 100ns units */
/* packet filling */ /* packet filling */
...@@ -53,16 +95,30 @@ typedef struct { ...@@ -53,16 +95,30 @@ typedef struct {
UINT8 packet_buf[PACKET_SIZE]; UINT8 packet_buf[PACKET_SIZE];
ByteIOContext pb; ByteIOContext pb;
/* only for reading */ /* only for reading */
uint64_t data_offset; /* begining of the first data packet */
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_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; } ASFContext;
typedef struct {
UINT32 v1;
UINT16 v2;
UINT16 v3;
UINT8 v4[8];
} GUID;
static const GUID asf_header = { static const GUID asf_header = {
0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
}; };
...@@ -120,7 +176,7 @@ static const GUID head1_guid = { ...@@ -120,7 +176,7 @@ static const GUID head1_guid = {
static const GUID head2_guid = { static const GUID head2_guid = {
0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 }, 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 },
}; };
/* I am not a number !!! This GUID is the one found on the PC used to /* I am not a number !!! This GUID is the one found on the PC used to
generate the stream */ generate the stream */
static const GUID my_guid = { static const GUID my_guid = {
...@@ -142,11 +198,11 @@ CodecTag codec_asf_bmp_tags[] = { ...@@ -142,11 +198,11 @@ CodecTag codec_asf_bmp_tags[] = {
{ CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') }, { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
{ CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') }, { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') },
{ CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */ { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
{ CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') }, /* default signature when using MSMPEG4 */ { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') }, /* default signature when using MSMPEG4 */
{ CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') }, { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') },
{ CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') }, { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
{ CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') }, { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
{ CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') }, { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
{ 0, 0 }, { 0, 0 },
}; };
...@@ -171,7 +227,7 @@ static void put_str16(ByteIOContext *s, const char *tag) ...@@ -171,7 +227,7 @@ static void put_str16(ByteIOContext *s, const char *tag)
for(;;) { for(;;) {
c = (UINT8)*tag++; c = (UINT8)*tag++;
put_le16(s, c); put_le16(s, c);
if (c == '\0') if (c == '\0')
break; break;
} }
} }
...@@ -183,7 +239,7 @@ static void put_str16_nolen(ByteIOContext *s, const char *tag) ...@@ -183,7 +239,7 @@ static void put_str16_nolen(ByteIOContext *s, const char *tag)
for(;;) { for(;;) {
c = (UINT8)*tag++; c = (UINT8)*tag++;
put_le16(s, c); put_le16(s, c);
if (c == '\0') if (c == '\0')
break; break;
} }
} }
...@@ -217,8 +273,8 @@ static void put_chunk(AVFormatContext *s, int type, int payload_length, int flag ...@@ -217,8 +273,8 @@ static void put_chunk(AVFormatContext *s, int type, int payload_length, int flag
int length; int length;
length = payload_length + 8; length = payload_length + 8;
put_le16(pb, type); put_le16(pb, type);
put_le16(pb, length); put_le16(pb, length);
put_le32(pb, asf->seqno); put_le32(pb, asf->seqno);
put_le16(pb, flags); /* unknown bytes */ put_le16(pb, flags); /* unknown bytes */
put_le16(pb, length); put_le16(pb, length);
...@@ -229,7 +285,7 @@ static void put_chunk(AVFormatContext *s, int type, int payload_length, int flag ...@@ -229,7 +285,7 @@ static void put_chunk(AVFormatContext *s, int type, int payload_length, int flag
static INT64 unix_to_file_time(int ti) static INT64 unix_to_file_time(int ti)
{ {
INT64 t; INT64 t;
t = ti * INT64_C(10000000); t = ti * INT64_C(10000000);
t += INT64_C(116444736000000000); t += INT64_C(116444736000000000);
return t; return t;
...@@ -260,11 +316,11 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu ...@@ -260,11 +316,11 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu
} }
put_guid(pb, &asf_header); 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_le32(pb, 3 + has_title + s->nb_streams); /* number of chunks in header */
put_byte(pb, 1); /* ??? */ put_byte(pb, 1); /* ??? */
put_byte(pb, 2); /* ??? */ put_byte(pb, 2); /* ??? */
/* file header */ /* file header */
header_offset = url_ftell(pb); header_offset = url_ftell(pb);
hpos = put_header(pb, &file_header); hpos = put_header(pb, &file_header);
...@@ -275,9 +331,9 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu ...@@ -275,9 +331,9 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu
put_le64(pb, asf->nb_packets); /* number of packets */ put_le64(pb, asf->nb_packets); /* number of packets */
put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */ put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */
put_le64(pb, asf->duration); /* duration (in 100ns units) */ put_le64(pb, asf->duration); /* duration (in 100ns units) */
put_le32(pb, 0); /* start time stamp */ put_le32(pb, 0); /* start time stamp */
put_le32(pb, 0); /* ??? */ put_le32(pb, 0); /* ??? */
put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */ put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */
put_le32(pb, asf->packet_size); /* packet size */ put_le32(pb, asf->packet_size); /* packet size */
put_le32(pb, asf->packet_size); /* packet size */ put_le32(pb, asf->packet_size); /* packet size */
put_le32(pb, bit_rate); /* Nominal data rate in bps */ put_le32(pb, bit_rate); /* Nominal data rate in bps */
...@@ -313,7 +369,7 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu ...@@ -313,7 +369,7 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu
enc = &s->streams[n]->codec; enc = &s->streams[n]->codec;
asf->streams[n].num = n + 1; asf->streams[n].num = n + 1;
asf->streams[n].seq = 0; asf->streams[n].seq = 0;
switch(enc->codec_type) { switch(enc->codec_type) {
case CODEC_TYPE_AUDIO: case CODEC_TYPE_AUDIO:
wav_extra_size = 0; wav_extra_size = 0;
...@@ -350,7 +406,7 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu ...@@ -350,7 +406,7 @@ static int asf_write_header1(AVFormatContext *s, INT64 file_size, INT64 data_chu
put_le32(pb, extra_size2); /* additional data len */ put_le32(pb, extra_size2); /* additional data len */
put_le16(pb, n + 1); /* stream number */ put_le16(pb, n + 1); /* stream number */
put_le32(pb, 0); /* ??? */ put_le32(pb, 0); /* ??? */
if (enc->codec_type == CODEC_TYPE_AUDIO) { if (enc->codec_type == CODEC_TYPE_AUDIO) {
/* WAVEFORMATEX header */ /* WAVEFORMATEX header */
int wavsize = put_wav_header(pb, enc); int wavsize = put_wav_header(pb, enc);
...@@ -464,8 +520,8 @@ static int asf_write_stream_header(AVFormatContext *s) ...@@ -464,8 +520,8 @@ static int asf_write_stream_header(AVFormatContext *s)
} }
/* write a fixed size packet */ /* write a fixed size packet */
static int put_packet(AVFormatContext *s, static int put_packet(AVFormatContext *s,
unsigned int timestamp, unsigned int duration, unsigned int timestamp, unsigned int duration,
int nb_frames, int padsize) int nb_frames, int padsize)
{ {
ASFContext *asf = s->priv_data; ASFContext *asf = s->priv_data;
...@@ -478,7 +534,7 @@ static int put_packet(AVFormatContext *s, ...@@ -478,7 +534,7 @@ static int put_packet(AVFormatContext *s,
put_byte(pb, 0x82); put_byte(pb, 0x82);
put_le16(pb, 0); put_le16(pb, 0);
flags = 0x01; /* nb segments present */ flags = 0x01; /* nb segments present */
if (padsize > 0) { if (padsize > 0) {
if (padsize < 256) if (padsize < 256)
...@@ -503,17 +559,17 @@ static void flush_packet(AVFormatContext *s) ...@@ -503,17 +559,17 @@ static void flush_packet(AVFormatContext *s)
{ {
ASFContext *asf = s->priv_data; ASFContext *asf = s->priv_data;
int hdr_size, ptr; int hdr_size, ptr;
hdr_size = put_packet(s, asf->packet_timestamp_start, hdr_size = put_packet(s, asf->packet_timestamp_start,
asf->packet_timestamp_end - asf->packet_timestamp_start, asf->packet_timestamp_end - asf->packet_timestamp_start,
asf->packet_nb_frames, asf->packet_size_left); asf->packet_nb_frames, asf->packet_size_left);
/* Clear out the padding bytes */ /* Clear out the padding bytes */
ptr = asf->packet_size - hdr_size - asf->packet_size_left; ptr = asf->packet_size - hdr_size - asf->packet_size_left;
memset(asf->packet_buf + ptr, 0, asf->packet_size_left); memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size); put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
put_flush_packet(&s->pb); put_flush_packet(&s->pb);
asf->nb_packets++; asf->nb_packets++;
asf->packet_nb_frames = 0; asf->packet_nb_frames = 0;
...@@ -547,14 +603,14 @@ static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestam ...@@ -547,14 +603,14 @@ static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestam
/* Output a frame. We suppose that payload_size <= PACKET_SIZE. /* Output a frame. We suppose that payload_size <= PACKET_SIZE.
It is there that you understand that the ASF format is really It is there that you understand that the ASF format is really
crap. They have misread the MPEG Systems spec ! crap. They have misread the MPEG Systems spec !
*/ */
static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp, static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
UINT8 *buf, int payload_size) UINT8 *buf, int payload_size)
{ {
ASFContext *asf = s->priv_data; ASFContext *asf = s->priv_data;
int frag_pos, frag_len, frag_len1; int frag_pos, frag_len, frag_len1;
frag_pos = 0; frag_pos = 0;
while (frag_pos < payload_size) { while (frag_pos < payload_size) {
frag_len = payload_size - frag_pos; frag_len = payload_size - frag_pos;
...@@ -594,23 +650,23 @@ static int asf_write_packet(AVFormatContext *s, int stream_index, ...@@ -594,23 +650,23 @@ static int asf_write_packet(AVFormatContext *s, int stream_index,
stream = &asf->streams[stream_index]; stream = &asf->streams[stream_index];
codec = &s->streams[stream_index]->codec; codec = &s->streams[stream_index]->codec;
stream = &asf->streams[stream_index]; stream = &asf->streams[stream_index];
if (codec->codec_type == CODEC_TYPE_AUDIO) { if (codec->codec_type == CODEC_TYPE_AUDIO) {
timestamp = (int)ticker_abs(&stream->pts_ticker, codec->frame_number); timestamp = (int)ticker_abs(&stream->pts_ticker, codec->frame_number);
duration = (codec->frame_number * codec->frame_size * INT64_C(10000000)) / duration = (codec->frame_number * codec->frame_size * INT64_C(10000000)) /
codec->sample_rate; codec->sample_rate;
} else { } else {
timestamp = (int)ticker_abs(&stream->pts_ticker, codec->frame_number); timestamp = (int)ticker_abs(&stream->pts_ticker, codec->frame_number);
duration = codec->frame_number * duration = codec->frame_number *
((INT64_C(10000000) * FRAME_RATE_BASE) / codec->frame_rate); ((INT64_C(10000000) * FRAME_RATE_BASE) / codec->frame_rate);
} }
if (duration > asf->duration) if (duration > asf->duration)
asf->duration = duration; asf->duration = duration;
put_frame(s, stream, timestamp, buf, size); put_frame(s, stream, timestamp, buf, size);
return 0; return 0;
} }
static int asf_write_trailer(AVFormatContext *s) static int asf_write_trailer(AVFormatContext *s)
{ {
ASFContext *asf = s->priv_data; ASFContext *asf = s->priv_data;
...@@ -643,7 +699,7 @@ static void print_guid(const GUID *g) ...@@ -643,7 +699,7 @@ static void print_guid(const GUID *g)
{ {
int i; int i;
printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3); printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3);
for(i=0;i<8;i++) for(i=0;i<8;i++)
printf(" 0x%02x,", g->v4[i]); printf(" 0x%02x,", g->v4[i]);
printf("}\n"); printf("}\n");
} }
...@@ -747,34 +803,35 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -747,34 +803,35 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
if (gsize < 24) if (gsize < 24)
goto fail; goto fail;
if (!memcmp(&g, &file_header, sizeof(GUID))) { if (!memcmp(&g, &file_header, sizeof(GUID))) {
get_guid(pb, &g); get_guid(pb, &asf->hdr.guid);
get_le64(pb); /* file size */ asf->hdr.file_size = get_le64(pb);
get_le64(pb); /* file time */ asf->hdr.create_time = get_le64(pb);
get_le64(pb); /* nb_packets */ asf->hdr.packets_count = get_le64(pb);
get_le64(pb); /* length 0 in us */ asf->hdr.play_time = get_le64(pb);
get_le64(pb); /* length 1 in us */ asf->hdr.send_time = get_le64(pb);
get_le32(pb); asf->hdr.preroll = get_le32(pb);
get_le32(pb); asf->hdr.ignore = get_le32(pb);
get_le32(pb); asf->hdr.flags = get_le32(pb);
asf->packet_size = get_le32(pb); asf->hdr.min_pktsize = get_le32(pb);
get_le32(pb); asf->hdr.max_pktsize = get_le32(pb);
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))) { } else if (!memcmp(&g, &stream_header, sizeof(GUID))) {
int type, id, total_size; int type, id, total_size;
unsigned int tag1; unsigned int tag1;
INT64 pos1, pos2; INT64 pos1, pos2;
pos1 = url_ftell(pb); pos1 = url_ftell(pb);
st = av_mallocz(sizeof(AVStream)); st = av_mallocz(sizeof(AVStream));
if (!st) if (!st)
goto fail; goto fail;
s->streams[s->nb_streams++] = st; s->streams[s->nb_streams] = st;
asf_st = av_mallocz(sizeof(ASFStream)); asf_st = av_mallocz(sizeof(ASFStream));
if (!asf_st) if (!asf_st)
goto fail; goto fail;
st->priv_data = asf_st; st->priv_data = asf_st;
st->time_length = (asf->hdr.send_time - asf->hdr.preroll) / 10000;
get_guid(pb, &g); get_guid(pb, &g);
if (!memcmp(&g, &audio_stream, sizeof(GUID))) { if (!memcmp(&g, &audio_stream, sizeof(GUID))) {
type = CODEC_TYPE_AUDIO; type = CODEC_TYPE_AUDIO;
...@@ -787,21 +844,44 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -787,21 +844,44 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
total_size = get_le64(pb); total_size = get_le64(pb);
get_le32(pb); get_le32(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); get_le32(pb);
st->codec.codec_type = type; st->codec.codec_type = type;
if (type == CODEC_TYPE_AUDIO) { if (type == CODEC_TYPE_AUDIO) {
id = get_le16(pb); id = get_le16(pb);
st->codec.codec_tag = id; st->codec.codec_tag = id;
st->codec.channels = get_le16(pb); st->codec.channels = get_le16(pb);
st->codec.sample_rate = get_le32(pb); st->codec.sample_rate = get_le32(pb);
st->codec.bit_rate = get_le32(pb) * 8; 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 */ bps = get_le16(pb); /* bits per sample */
st->codec.codec_id = wav_codec_get_id(id, bps); st->codec.codec_id = wav_codec_get_id(id, bps);
size = get_le16(pb); size = get_le16(pb);
url_fskip(pb, size); st->extra_data = av_mallocz(size);
/* We have to init the frame size at some point .... */ 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) { switch (st->codec.codec_id) {
case CODEC_ID_MP3LAME: case CODEC_ID_MP3LAME:
st->codec.frame_size = MPA_FRAME_SIZE; st->codec.frame_size = MPA_FRAME_SIZE;
...@@ -828,14 +908,15 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -828,14 +908,15 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
size = get_le16(pb); /* size */ size = get_le16(pb); /* size */
get_le32(pb); /* size */ get_le32(pb); /* size */
st->codec.width = get_le32(pb); st->codec.width = get_le32(pb);
st->codec.height = get_le32(pb); st->codec.height = get_le32(pb);
st->codec.frame_rate = 25 * FRAME_RATE_BASE; /* XXX: find it */ /* not available for asf */
st->codec.frame_rate = 25 * FRAME_RATE_BASE; /* XXX: find it */
get_le16(pb); /* panes */ get_le16(pb); /* panes */
get_le16(pb); /* depth */ get_le16(pb); /* depth */
tag1 = get_le32(pb); tag1 = get_le32(pb);
st->codec.codec_tag = tag1; st->codec.codec_tag = tag1;
st->codec.codec_id = codec_get_id(codec_asf_bmp_tags, tag1); st->codec.codec_id = codec_get_id(codec_asf_bmp_tags, tag1);
url_fskip(pb, size - 5 * 4); url_fskip(pb, size - 5 * 4);
} }
pos2 = url_ftell(pb); pos2 = url_ftell(pb);
url_fskip(pb, gsize - (pos2 - pos1 + 24)); url_fskip(pb, gsize - (pos2 - pos1 + 24));
...@@ -867,7 +948,7 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -867,7 +948,7 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
get_guid(pb, &g); get_guid(pb, &g);
print_guid(&g); print_guid(&g);
n = get_le32(pb); n = get_le32(pb);
for(i=0;i<n;i++) { for(i=0;i<n;i++) {
num = get_le16(pb); /* stream number */ num = get_le16(pb); /* stream number */
...@@ -910,129 +991,218 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -910,129 +991,218 @@ static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
return -1; 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) static int asf_get_packet(AVFormatContext *s)
{ {
ASFContext *asf = s->priv_data; ASFContext *asf = s->priv_data;
ByteIOContext *pb = &s->pb; ByteIOContext *pb = &s->pb;
int c, flags, timestamp, hdr_size; uint32_t packet_length, padsize;
int rsize = 11;
hdr_size = 12; int c = get_byte(pb);
c = get_byte(pb);
if (c != 0x82) if (c != 0x82)
return -EIO; {
get_le16(pb); printf("BAD HRD %x at:%Ld\n", c, url_ftell(pb));
flags = get_byte(pb); return -EIO;
get_byte(pb); }
asf->packet_padsize = 0; if ((c & 0x0f) == 2) { // always true for now
if (flags & 0x10) { if (get_le16(pb) != 0)
asf->packet_padsize = get_le16(pb); {
hdr_size += 2; printf("BAD NO ZERO\n");
} else if (flags & 0x08) { return -EIO;
asf->packet_padsize = get_byte(pb); }
hdr_size++;
} }
timestamp = get_le32(pb);
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_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 #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 #endif
asf->packet_size_left = asf->packet_size - hdr_size;
return 0; return 0;
} }
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt) static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
{ {
ASFContext *asf = s->priv_data; ASFContext *asf = s->priv_data;
AVStream *st; ASFStream *asf_st = 0;
ASFStream *asf_st;
ByteIOContext *pb = &s->pb; ByteIOContext *pb = &s->pb;
int ret, num, seq, frag_offset, payload_size, frag_len; static int pc = 0;
int key_frame; for (;;) {
int timestamp, i; int rsize = 0;
if (asf->packet_size_left < FRAME_HEADER_SIZE) {
for(;;) { //asf->packet_size_left <= asf->packet_padsize) {
if (asf->packet_size_left < FRAME_HEADER_SIZE || int ret;
asf->packet_size_left <= asf->packet_padsize) { //printf("PACKETLEFTSIZE %d\n", asf->packet_size_left);
/* fail safe */ /* fail safe */
if (asf->packet_size_left) if (asf->packet_size_left)
url_fskip(pb, asf->packet_size_left); url_fskip(pb, asf->packet_size_left);
ret = asf_get_packet(s); if (asf->packet_padsize)
if (ret < 0) url_fskip(pb, asf->packet_padsize);
return -EIO; ret = asf_get_packet(s);
} //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++);
/* read frame header */ if (ret < 0)
num = get_byte(pb); return -EIO;
if (num & 0x80) continue;
key_frame = 1; }
else if (1) { // FIXME - handle replicated multi packets
key_frame = 0; /* read frame header */
int num = get_byte(pb);
num &= 0x7f; rsize++;
seq = get_byte(pb); asf->packet_key_frame = (num & 0x80) >> 7;
frag_offset = get_le32(pb); asf->stream_index = asf->asfid2avid[num & 0x7f];
get_byte(pb); /* flags */ if (asf->stream_index < 0)
payload_size = get_le32(pb); {
timestamp = get_le32(pb); /* unhandled packet (should not happen) */
frag_len = get_le16(pb); url_fskip(pb, asf->packet_frag_size);
#ifdef DEBUG asf->packet_size_left -= asf->packet_frag_size;
printf("num=%d seq=%d totsize=%d frag_off=%d frag_size=%d\n", //printf("#####xxxxxx###### skip %d\n", asf->packet_frag_size);
num, seq, payload_size, frag_offset, frag_len); continue;
#endif // FIXME
st = NULL; }
for(i=0;i<s->nb_streams;i++) { asf->asf_st = s->streams[asf->stream_index]->priv_data;
st = s->streams[i]; //printf("ASFST %p %d <> %d\n", asf->asf_st, asf->stream_index, num & 0x7f);
if (st->id == num) // sequence should be ignored!
break; DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
} DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
asf->packet_size_left -= FRAME_HEADER_SIZE + frag_len; DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
if (i == s->nb_streams) {
/* unhandled packet (should not happen) */ if (asf->packet_replic_size > 1) {
url_fskip(pb, frag_len); // it should be always at least 8 bytes - FIXME validate
} else { asf->packet_obj_size = get_le32(pb);
asf_st = st->priv_data; asf->packet_frag_timestamp = get_le32(pb); // timestamp
if (asf_st->frag_offset == 0) { rsize += asf->packet_replic_size; // FIXME - check validity
/* new packet */ } else {
av_new_packet(&asf_st->pkt, payload_size); // multipacket - frag_offset is beginig timestamp
asf_st->seq = seq; asf->packet_time_start = asf->packet_frag_offset;
if (key_frame) asf->packet_frag_offset = 0;
asf_st->pkt.flags |= PKT_FLAG_KEY; asf->packet_frag_timestamp = asf->packet_timestamp;
if (asf->packet_replic_size == 1) {
asf_st->pkt.pts = timestamp; asf->packet_time_delta = get_byte(pb);
} else { rsize++;
if (seq == asf_st->seq && }
frag_offset == asf_st->frag_offset) { }
/* continuing packet */
} else { if (asf->packet_flags & 0x01) {
/* cannot continue current packet: free it */ DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
av_free_packet(&asf_st->pkt); //printf("Fragsize %d\n", asf->packet_frag_size);
asf_st->frag_offset = 0; } else {
if (frag_offset != 0) { asf->packet_frag_size = asf->packet_size_left - rsize;
/* cannot create new packet */ //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
url_fskip(pb, frag_len); }
goto next_frame; #undef DO_2BITS
} else { asf->packet_size_left -= rsize;
/* create new packet */ //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
av_new_packet(&asf_st->pkt, payload_size); }
asf_st->seq = seq; asf_st = asf->asf_st;
}
} if ((asf->packet_frag_offset != asf_st->frag_offset
} || (asf->packet_frag_offset
/* read data */ && asf->packet_seq != asf_st->seq)) // seq should be ignored
get_buffer(pb, asf_st->pkt.data + frag_offset, frag_len); ) {
asf_st->frag_offset += frag_len; /* cannot continue current packet: free it */
/* test if whole packet read */ // FIXME better check if packet was already allocated
if (asf_st->frag_offset == asf_st->pkt.size) { printf("asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n",
/* return packet */ asf_st->pkt.size,
asf_st->pkt.stream_index = i; asf->packet_obj_size,
asf_st->frag_offset = 0; asf->packet_frag_offset, asf_st->frag_offset,
memcpy(pkt, &asf_st->pkt, sizeof(AVPacket)); asf->packet_seq, asf_st->seq, asf->packet_frag_size);
break; if (asf_st->pkt.size)
} av_free_packet(&asf_st->pkt);
} asf_st->frag_offset = 0;
next_frame:; if (asf->packet_frag_offset != 0) {
/* cannot create new packet */
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;
}
}
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; return 0;
} }
...@@ -1049,6 +1219,12 @@ static int asf_read_close(AVFormatContext *s) ...@@ -1049,6 +1219,12 @@ static int asf_read_close(AVFormatContext *s)
return 0; return 0;
} }
static int asf_read_seek(AVFormatContext *s, int64_t pts)
{
printf("SEEK TO %Ld", pts);
return -1;
}
AVInputFormat asf_iformat = { AVInputFormat asf_iformat = {
"asf", "asf",
"asf format", "asf format",
...@@ -1057,6 +1233,7 @@ AVInputFormat asf_iformat = { ...@@ -1057,6 +1233,7 @@ AVInputFormat asf_iformat = {
asf_read_header, asf_read_header,
asf_read_packet, asf_read_packet,
asf_read_close, asf_read_close,
asf_read_seek,
}; };
AVOutputFormat asf_oformat = { AVOutputFormat asf_oformat = {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#define LIBAVFORMAT_VERSION_INT 0x000406 #define LIBAVFORMAT_VERSION_INT 0x000406
#define LIBAVFORMAT_VERSION "0.4.6" #define LIBAVFORMAT_VERSION "0.4.6"
#define LIBAVFORMAT_BUILD 4601 #define LIBAVFORMAT_BUILD 4602
#include "avcodec.h" #include "avcodec.h"
...@@ -121,6 +121,9 @@ typedef struct AVStream { ...@@ -121,6 +121,9 @@ typedef struct AVStream {
int id; /* format specific stream id */ int id; /* format specific stream id */
AVCodecContext codec; /* codec context */ AVCodecContext codec; /* codec context */
int r_frame_rate; /* real frame rate of the stream */ 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; void *priv_data;
/* internal data used in av_find_stream_info() */ /* internal data used in av_find_stream_info() */
int codec_info_state; int codec_info_state;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment