asfenc: properly write index information

The index must take into account the pre-roll time and must seek backwards,
not forwards.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
Ramiro Polla 2012-04-04 02:52:27 -03:00 committed by Michael Niedermayer
parent ae315a6a38
commit bd603494f9
3 changed files with 49 additions and 29 deletions

View File

@ -208,11 +208,13 @@ typedef struct {
/* only for reading */
uint64_t data_offset; ///< beginning of the first data packet
int64_t last_indexed_pts;
ASFIndex* index_ptr;
uint32_t nb_index_count;
uint32_t nb_index_memory_alloc;
uint16_t maximum_packet;
uint32_t next_packet_number;
uint16_t next_packet_count;
int next_start_sec;
int end_sec;
} ASFContext;
static const AVCodecTag codec_asf_bmp_tags[] = {
@ -557,10 +559,8 @@ static int asf_write_header(AVFormatContext *s)
s->packet_size = PACKET_SIZE;
asf->nb_packets = 0;
asf->last_indexed_pts = 0;
asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
asf->nb_index_count = 0;
asf->maximum_packet = 0;
/* the data-chunk-size has to be 50, which is data_size - asf->data_offset
@ -782,6 +782,34 @@ static void put_frame(
stream->seq++;
}
static void update_index(AVFormatContext *s, int start_sec,
uint32_t packet_number, uint16_t packet_count)
{
ASFContext *asf = s->priv_data;
if (start_sec > asf->next_start_sec) {
int i;
if (!asf->next_start_sec) {
asf->next_packet_number = packet_number;
asf->next_packet_count = packet_count;
}
if (start_sec > asf->nb_index_memory_alloc) {
asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
asf->index_ptr = av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
}
for (i = asf->next_start_sec; i < start_sec; i++) {
asf->index_ptr[i].packet_number = asf->next_packet_number;
asf->index_ptr[i].packet_count = asf->next_packet_count;
}
}
asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
asf->next_packet_number = packet_number;
asf->next_packet_count = packet_count;
asf->next_start_sec = start_sec;
}
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
{
ASFContext *asf = s->priv_data;
@ -789,7 +817,7 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
AVCodecContext *codec;
uint32_t packet_number;
int64_t pts;
int start_sec,i;
int start_sec;
int flags= pkt->flags;
codec = s->streams[pkt->stream_index]->codec;
@ -806,25 +834,16 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
packet_number = asf->nb_packets;
put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
/ ASF_INDEXED_INTERVAL);
/* check index */
if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
uint16_t packet_count = asf->nb_packets - packet_number;
start_sec = (int)(pts / INT64_C(10000000));
if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
if (start_sec > asf->nb_index_memory_alloc) {
asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
asf->index_ptr = av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
}
for(i=asf->nb_index_count;i<start_sec;i++) {
// store
asf->index_ptr[i].packet_number = packet_number;
asf->index_ptr[i].packet_count = packet_count;
asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
}
asf->nb_index_count = start_sec;
asf->last_indexed_pts = pts;
}
update_index(s, start_sec, packet_number, packet_count);
}
asf->end_sec = start_sec;
return 0;
}
@ -859,8 +878,9 @@ static int asf_write_trailer(AVFormatContext *s)
/* write index */
data_size = avio_tell(s->pb);
if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
if (!asf->is_streamed && asf->next_start_sec) {
update_index(s, asf->end_sec + 1, 0, 0);
asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->next_start_sec);
}
avio_flush(s->pb);

View File

@ -1,3 +1,3 @@
e2ee0e05f020adb0aa84202a60211790 *./tests/data/lavf/lavf.asf
333489 ./tests/data/lavf/lavf.asf
0c314dcdf9a4f9afda244f0ffdaaeffb *./tests/data/lavf/lavf.asf
333581 ./tests/data/lavf/lavf.asf
./tests/data/lavf/lavf.asf CRC=0x9f5ab3e6

View File

@ -2,9 +2,9 @@ ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st:-1 flags:0 ts:-1.000000
ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st:-1 flags:1 ts: 1.894167
ret: 0 st: 1 flags:1 dts: 0.930000 pts: 0.930000 pos: 301489 size: 209
ret: 0 st: 1 flags:1 dts: 0.459000 pts: 0.459000 pos: 147889 size: 209
ret: 0 st: 0 flags:0 ts: 0.788000
ret: 0 st: 1 flags:1 dts: 0.930000 pts: 0.930000 pos: 301489 size: 209
ret: 0 st: 1 flags:1 dts: 0.459000 pts: 0.459000 pos: 147889 size: 209
ret: 0 st: 0 flags:1 ts:-0.317000
ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st: 1 flags:0 ts: 2.577000
@ -26,7 +26,7 @@ ret: 0 st: 1 flags:1 dts: 0.982000 pts: 0.982000 pos: 330289 size: 209
ret: 0 st:-1 flags:0 ts: 1.730004
ret: 0 st: 1 flags:1 dts: 0.930000 pts: 0.930000 pos: 301489 size: 209
ret: 0 st:-1 flags:1 ts: 0.624171
ret: 0 st: 1 flags:1 dts: 0.459000 pts: 0.459000 pos: 147889 size: 209
ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st: 0 flags:0 ts:-0.482000
ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st: 0 flags:1 ts: 2.413000
@ -34,13 +34,13 @@ ret: 0 st: 1 flags:1 dts: 0.930000 pts: 0.930000 pos: 301489 size: 209
ret: 0 st: 1 flags:0 ts: 1.307000
ret: 0 st: 1 flags:1 dts: 0.982000 pts: 0.982000 pos: 330289 size: 209
ret: 0 st: 1 flags:1 ts: 0.201000
ret: 0 st: 1 flags:1 dts: 0.198000 pts: 0.198000 pos: 74289 size: 209
ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st:-1 flags:0 ts:-0.904994
ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st:-1 flags:1 ts: 1.989173
ret: 0 st: 1 flags:1 dts: 0.930000 pts: 0.930000 pos: 301489 size: 209
ret: 0 st: 0 flags:0 ts: 0.883000
ret: 0 st: 1 flags:1 dts: 0.930000 pts: 0.930000 pos: 301489 size: 209
ret: 0 st: 1 flags:1 dts: 0.459000 pts: 0.459000 pos: 147889 size: 209
ret: 0 st: 0 flags:1 ts:-0.222000
ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st: 1 flags:0 ts: 2.672000