Commit bd603494 authored by Ramiro Polla's avatar Ramiro Polla Committed by Michael Niedermayer

asfenc: properly write index information

The index must take into account the pre-roll time and must seek backwards,
not forwards.
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent ae315a6a
...@@ -208,11 +208,13 @@ typedef struct { ...@@ -208,11 +208,13 @@ typedef struct {
/* only for reading */ /* only for reading */
uint64_t data_offset; ///< beginning of the first data packet uint64_t data_offset; ///< beginning of the first data packet
int64_t last_indexed_pts;
ASFIndex* index_ptr; ASFIndex* index_ptr;
uint32_t nb_index_count;
uint32_t nb_index_memory_alloc; uint32_t nb_index_memory_alloc;
uint16_t maximum_packet; uint16_t maximum_packet;
uint32_t next_packet_number;
uint16_t next_packet_count;
int next_start_sec;
int end_sec;
} ASFContext; } ASFContext;
static const AVCodecTag codec_asf_bmp_tags[] = { static const AVCodecTag codec_asf_bmp_tags[] = {
...@@ -557,10 +559,8 @@ static int asf_write_header(AVFormatContext *s) ...@@ -557,10 +559,8 @@ static int asf_write_header(AVFormatContext *s)
s->packet_size = PACKET_SIZE; s->packet_size = PACKET_SIZE;
asf->nb_packets = 0; asf->nb_packets = 0;
asf->last_indexed_pts = 0;
asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK ); asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
asf->nb_index_memory_alloc = ASF_INDEX_BLOCK; asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
asf->nb_index_count = 0;
asf->maximum_packet = 0; asf->maximum_packet = 0;
/* the data-chunk-size has to be 50, which is data_size - asf->data_offset /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
...@@ -782,6 +782,34 @@ static void put_frame( ...@@ -782,6 +782,34 @@ static void put_frame(
stream->seq++; 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) static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
{ {
ASFContext *asf = s->priv_data; ASFContext *asf = s->priv_data;
...@@ -789,7 +817,7 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -789,7 +817,7 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
AVCodecContext *codec; AVCodecContext *codec;
uint32_t packet_number; uint32_t packet_number;
int64_t pts; int64_t pts;
int start_sec,i; int start_sec;
int flags= pkt->flags; int flags= pkt->flags;
codec = s->streams[pkt->stream_index]->codec; codec = s->streams[pkt->stream_index]->codec;
...@@ -806,25 +834,16 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -806,25 +834,16 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
packet_number = asf->nb_packets; packet_number = asf->nb_packets;
put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags); 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 */ /* check index */
if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) { if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
uint16_t packet_count = asf->nb_packets - packet_number; uint16_t packet_count = asf->nb_packets - packet_number;
start_sec = (int)(pts / INT64_C(10000000)); update_index(s, start_sec, packet_number, packet_count);
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;
}
} }
asf->end_sec = start_sec;
return 0; return 0;
} }
...@@ -859,8 +878,9 @@ static int asf_write_trailer(AVFormatContext *s) ...@@ -859,8 +878,9 @@ static int asf_write_trailer(AVFormatContext *s)
/* write index */ /* write index */
data_size = avio_tell(s->pb); data_size = avio_tell(s->pb);
if ((!asf->is_streamed) && (asf->nb_index_count != 0)) { if (!asf->is_streamed && asf->next_start_sec) {
asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count); 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); avio_flush(s->pb);
......
e2ee0e05f020adb0aa84202a60211790 *./tests/data/lavf/lavf.asf 0c314dcdf9a4f9afda244f0ffdaaeffb *./tests/data/lavf/lavf.asf
333489 ./tests/data/lavf/lavf.asf 333581 ./tests/data/lavf/lavf.asf
./tests/data/lavf/lavf.asf CRC=0x9f5ab3e6 ./tests/data/lavf/lavf.asf CRC=0x9f5ab3e6
...@@ -2,9 +2,9 @@ ret: 0 st: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208 ...@@ -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: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 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 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: 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: 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:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st: 1 flags:0 ts: 2.577000 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 ...@@ -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: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 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 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: 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: 1 flags:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st: 0 flags:1 ts: 2.413000 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 ...@@ -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: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 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 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: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 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 ts: 1.989173
ret: 0 st: 1 flags:1 dts: 0.930000 pts: 0.930000 pos: 301489 size: 209 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: 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: 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:1 dts:-0.011000 pts:-0.011000 pos: 689 size: 208
ret: 0 st: 1 flags:0 ts: 2.672000 ret: 0 st: 1 flags:0 ts: 2.672000
......
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