[FFmpeg-devel] [PATCH 1/1] Extended ASF functionality

Vadim Belov vadim.belov at gmail.com
Wed Jul 1 14:49:11 CEST 2015


---
 libavformat/Makefile             |   2 +-
 libavformat/asf.c                |   4 +
 libavformat/asf.h                |  12 ++-
 libavformat/asf_ex.h             |  58 ++++++++++
 libavformat/asf_trim.c           | 228 +++++++++++++++++++++++++++++++++++++++
 libavformat/asf_trim.h           |  95 ++++++++++++++++
 libavformat/asfdec.c             |  17 ++-
 libavformat/asfenc.c             |  64 ++++++++++-
 8 files changed, 954 insertions(+), 485 deletions(-)
 create mode 100644 libavformat/asf_ex.h
 create mode 100644 libavformat/asf_trim.c
 create mode 100644 libavformat/asf_trim.h

 1.9.5.msysgit.1

 Signed-off-by: Vadim Belov <vadim.belov at gmail.com>

diff --git a/libavformat/Makefile b/libavformat/Makefile
index bca9d5b..2743467 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -84,7 +84,7 @@ OBJS-$(CONFIG_APNG_MUXER)                += apngenc.o
 OBJS-$(CONFIG_AQTITLE_DEMUXER)           += aqtitledec.o subtitles.o
 OBJS-$(CONFIG_ASF_DEMUXER)               += asfdec.o asf.o asfcrypt.o \
                                             avlanguage.o
-OBJS-$(CONFIG_ASF_MUXER)                 += asfenc.o asf.o
+OBJS-$(CONFIG_ASF_MUXER)                 += asfenc.o asf.o asf_trim.o
 OBJS-$(CONFIG_ASS_DEMUXER)               += assdec.o subtitles.o
 OBJS-$(CONFIG_ASS_MUXER)                 += assenc.o
 OBJS-$(CONFIG_AST_DEMUXER)               += ast.o astdec.o
diff --git a/libavformat/asf.c b/libavformat/asf.c
index 80d24db..d2a0a8f 100644
--- a/libavformat/asf.c
+++ b/libavformat/asf.c
@@ -143,6 +143,10 @@ const ff_asf_guid ff_asf_digital_signature = {
     0xfc, 0xb3, 0x11, 0x22, 0x23, 0xbd, 0xd2, 0x11, 0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e
 };
 
+const ff_asf_guid ff_asf_index_header = {
+    0xd3, 0x29, 0xe2, 0xd6, 0xda, 0x35, 0xd1, 0x11, 0x90, 0x34, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xbe
+};
+
 /* List of official tags at http://msdn.microsoft.com/en-us/library/dd743066(VS.85).aspx */
 const AVMetadataConv ff_asf_metadata_conv[] = {
     { "WM/AlbumArtist",          "album_artist"     },
diff --git a/libavformat/asf.h b/libavformat/asf.h
index 0c9598a..0ef4ea6 100644
--- a/libavformat/asf.h
+++ b/libavformat/asf.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2000, 2001 Fabrice Bellard
+ * Copyright (c) 2015 Vadim Belov
  *
  * This file is part of FFmpeg.
  *
@@ -28,6 +29,11 @@
 
 #define PACKET_SIZE 3200
 
+/** 
+ * "asf_ex.h" is IMPORTANT to be placed After PACKET_SIZE definition
+ */
+#include "asf_ex.h"
+
 typedef struct ASFPayload {
     uint8_t type;
     uint16_t size;
@@ -57,7 +63,9 @@ typedef struct ASFStream {
     uint32_t palette[256];
 
     int payload_ext_ct;
-    ASFPayload payload[8];
+    ASFPayload payload[8];	
+
+    ASFIndexData idx_data;
 } ASFStream;
 
 typedef struct ASFMainHeader {
@@ -123,7 +131,7 @@ extern const ff_asf_guid ff_asf_language_guid;
 extern const ff_asf_guid ff_asf_content_encryption;
 extern const ff_asf_guid ff_asf_ext_content_encryption;
 extern const ff_asf_guid ff_asf_digital_signature;
-
+extern const ff_asf_guid ff_asf_index_header;
 extern const AVMetadataConv ff_asf_metadata_conv[];
 
 #define ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT 0x80 //1000 0000
diff --git a/libavformat/asf_ex.h b/libavformat/asf_ex.h
new file mode 100644
index 0000000..480105a
--- /dev/null
+++ b/libavformat/asf_ex.h
@@ -0,0 +1,58 @@
+/**
+ * @file asf_ex.h
+ * Extensions to ASF handling
+ * @author Vadim Belov
+ *
+ * Copyright (c) 2015 Vadim Belov
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFORMAT_ASF_EX_H
+#define AVFORMAT_ASF_EX_H
+
+#include <stdint.h>
+
+/**
+ * Packet size according to the size that Custom ASF File Creator writes 
+ * to its output packets:
+ * ASF_PACKET_SIZE is 8192, but is decremented later.
+ * Final result: in the ASF core file the value is 8032 
+ */
+#ifdef PACKET_SIZE
+#undef PACKET_SIZE
+#endif
+#define PACKET_SIZE 8032 
+
+#define STREAM_DIRECTION_STR "streamDirection"
+#define DIRECTION_DICT_KEY "direction"
+#define NO_STREAM_DIRECTION -1
+#define ASF_MAX_STREAMS_NUM 128
+
+typedef struct ASFStreamIndex { // Index Entry value 
+    uint64_t offset;
+} ASFStreamIndex;
+
+typedef struct ASFIndexData { 
+    ASFStreamIndex* indices;	// array of ASFStreamIndex
+    uint32_t indices_max_count; // allocated size
+    uint32_t next_duration_mark; // for next index
+    uint32_t indices_count;		 // current index 
+    int64_t  duration_overall;
+} ASFIndexData;
+
+#endif /* AVFORMAT_ASF_EX_H */
diff --git a/libavformat/asf_trim.c b/libavformat/asf_trim.c
new file mode 100644
index 0000000..ce75170
--- /dev/null
+++ b/libavformat/asf_trim.c
@@ -0,0 +1,228 @@
+/*
+ * @file asf_trim.c
+ * Extensions to ASF handling
+ * @author Vadim Belov
+ *
+ * Copyright (c) 2015 Vadim Belov
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "asf_trim.h"
+
+#define ASF_INDEX_ENTRY_TIME_INTERVAL 10000
+#define SPECIFIER_INDEX_TYPE 3
+
+#define BLOCK_PART      0xFFFFFFFF00000000
+#define OFFSET_PART     0x00000000FFFFFFFF
+#define INVALID_OFFSET  0xFFFFFFFF
+
+int asf_get_streams_direction(AVFormatContext *s)
+{
+    int ret = 0;
+    int n;
+    for (n = 0; n < s->nb_streams; n++) {
+        AVDictionaryEntry *t = av_dict_get(
+            s->streams[n]->metadata, DIRECTION_DICT_KEY, NULL, 0);
+        
+        av_log(s, AV_LOG_DEBUG, "direction metadata ptr is %p\n", t);
+
+        if (t) ret++;
+    }
+    return ret;
+}
+
+void set_stream_direction(
+    AVFormatContext *s,
+    int direction[ASF_MAX_STREAMS_NUM],
+    AVStream *st,
+    int i)
+{
+    if(direction[i] != NO_STREAM_DIRECTION) {
+        char buffer[32];                                
+        sprintf(buffer, "%d %d", i, direction[i]);              
+        av_dict_set(&st->metadata, DIRECTION_DICT_KEY, buffer, 0);
+    }           
+}
+
+int asf_alloc_streams_index(
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    int num_streams,
+    int block_size)
+{
+    int i;
+    for (i = 0; i < num_streams; ++i){
+        ASFIndexData *idx = &streams[i].idx_data;
+
+        idx->indices = av_malloc(sizeof(ASFStreamIndex)* block_size);
+        if (!idx->indices) return AVERROR(ENOMEM);
+
+        idx->indices_max_count = block_size;
+        idx->duration_overall = 0;
+        idx->next_duration_mark = 0;
+        idx->indices_count = 0;
+    }
+
+    return 0;
+}
+
+void asf_free_streams_index(
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    int num_streams)
+{
+    int i;  
+    for (i = 0; i < num_streams; ++i) {
+        av_freep(&streams[i].idx_data.indices);
+    }
+}
+
+int upadte_indices(
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    uint64_t pkt_time,
+    uint64_t packet_offset,
+    int stream_index,
+    int64_t pkt_duration,
+    uint32_t asf_index_block,
+    uint64_t asf_data_start)
+{
+    ASFIndexData *idx = &streams[stream_index].idx_data;
+
+    // reallocate if size too small
+    if (idx->indices_count > idx->indices_max_count) {
+        int err;
+        idx->indices_max_count = (pkt_time + asf_index_block) & ~(asf_index_block - 1);
+        if ((err = av_reallocp_array(&idx,
+            idx->indices_max_count,
+            sizeof(*idx->indices))) < 0) {
+            idx->indices_max_count = 0;
+            return err;
+        }
+    }
+
+    // update offset if reached the duration mark
+    idx->duration_overall += pkt_duration;
+    if (idx->duration_overall > idx->next_duration_mark) {
+        idx->indices[idx->indices_count].offset = packet_offset - asf_data_start;
+        idx->indices_count++;
+        idx->next_duration_mark += ASF_INDEX_ENTRY_TIME_INTERVAL;
+    }
+
+    return 0;
+}
+
+static int get_num_entries(
+    ASFStream streams[ASF_MAX_STREAMS_NUM], 
+    int num_streams)
+{
+    int i;
+    int ret = 0;
+    for (i = 0; i < num_streams; ++i){
+        ASFIndexData *idx = &(streams[i].idx_data);
+        ret = FFMAX(ret, idx->indices_count);
+    }
+    return ret;
+}
+
+static int should_iterate_block(int i, ASFIndexData *idx, uint64_t curr_offset)
+{
+    if (i + 1 < idx->indices_count) {
+        uint64_t next_offset = idx->indices[i + 1].offset;
+        uint64_t curr_block = curr_offset & BLOCK_PART;
+        uint64_t next_block = next_offset & BLOCK_PART;
+        return curr_block != next_block;
+    }
+
+    return 0;
+}
+
+int asf_write_indices(
+    AVFormatContext *s,
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    int num_streams)
+{
+    AVIOContext *pb = s->pb;
+    
+    int i, j;
+    int num_blocks = 0;
+    int num_entries = get_num_entries(streams, num_streams);
+
+    int64_t blocks_pos, end_of_indices;
+
+    char write_index = 1;
+    char iterate_block = 1;
+
+    av_log(s, AV_LOG_DEBUG, "Write Index object for %d streams with %d entries.\n",
+        num_streams, num_entries);
+
+    ff_put_guid(pb, &ff_asf_index_header);
+    avio_wl64(pb, 34 + 4 * num_streams + 4 + 8 * num_streams + 4 * num_entries*num_streams);
+    avio_wl32(pb, ASF_INDEX_ENTRY_TIME_INTERVAL); // interval
+    avio_wl16(pb, num_streams); // specifiers count
+
+    blocks_pos = avio_tell(pb);
+    avio_wl32(pb, 0); // rewrite later
+    
+    // write specifiers
+    for (i = 0; i < num_streams; i++) {
+        avio_wl16(pb, i + 1);
+        avio_wl16(pb, SPECIFIER_INDEX_TYPE);
+    }
+
+    avio_wl32(pb, num_entries); // Index Entry Count
+
+    for (i = 0; write_index && i < num_entries; ++i) {
+
+        // block header
+        if (iterate_block) {
+            ++num_blocks;
+
+            for (j = 0; j < num_streams; ++j) {
+                avio_wl64(pb, 0);
+            }
+            iterate_block = 0;
+        }
+
+        // block content
+        for (j = 0; j < num_streams; ++j) {
+            ASFIndexData *idx = &(streams[j].idx_data);
+            uint32_t offset = INVALID_OFFSET;
+            if (i < idx->indices_count) {
+                uint64_t full_offset = idx->indices[i].offset;
+                offset = (uint32_t)full_offset & OFFSET_PART;
+                iterate_block = should_iterate_block(i, idx, full_offset);
+            }
+            else {
+                write_index = 0;
+                offset = INVALID_OFFSET;
+                av_log(s, AV_LOG_WARNING, "Index: invalid offset (%d,%d)\n", i, j);
+            }
+            avio_wl32(pb, offset);
+        }
+    }
+
+    end_of_indices = avio_tell(pb);
+    
+    // rewrite blocks count and return file descriptor to end
+    avio_seek(pb, blocks_pos, SEEK_SET);
+    avio_wl32(pb, num_blocks);
+    avio_seek(pb, end_of_indices, SEEK_SET);
+
+    av_log(s, AV_LOG_DEBUG, "Write Index object for %d streams with %d entries done.\n",
+        num_streams, num_entries);
+
+    return 0;
+}
diff --git a/libavformat/asf_trim.h b/libavformat/asf_trim.h
new file mode 100644
index 0000000..ddef614
--- /dev/null
+++ b/libavformat/asf_trim.h
@@ -0,0 +1,95 @@
+/*
+ * @file asf_trim.c
+ * Extensions to ASF handling
+ * @author Vadim Belov
+ *
+ * Copyright (c) 2015 Vadim Belov
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef ASF_TRIM_H
+#define ASF_TRIM_H
+
+#include "asf.h"
+#include "avformat.h"
+
+/**
+* Get stream direction attributes values from input ASF file
+*
+* @return  number of stream direction attributes in all streams
+*/
+int asf_get_streams_direction(AVFormatContext *s);
+
+
+/**
+* Set stream direction attributes in output ASF file
+*/
+void set_stream_direction(
+    AVFormatContext *s,
+    int direction[ASF_MAX_STREAMS_NUM],
+    AVStream *st,
+    int i);
+
+
+/**
+* Allocate index object for a stream
+*
+* @return error code: 0 iff success
+*/
+int asf_alloc_streams_index(
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    int num_streams,
+    int block_size);
+
+
+/**
+* Free index object for a stream
+*/
+void asf_free_streams_index(
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    int num_streams);
+
+
+/**
+* Update index object of the packet's stream if necessary:
+* when packet time has reached the defined interval between 
+* the indices
+*
+* @return error code: 0 iff success
+*/
+int upadte_indices(
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    uint64_t pkt_time,
+    uint64_t packet_offset,
+    int stream_index,
+    int64_t pkt_duration,
+    uint32_t asf_index_block,
+    uint64_t asf_data_start);
+
+
+/**
+* Allocate index object for a stream
+*
+* @return error code: 0 iff success
+*/
+int asf_write_indices(
+    AVFormatContext *s,
+    ASFStream streams[ASF_MAX_STREAMS_NUM],
+    int num_streams);
+
+#endif /* ASF_TRIM_H */
diff --git a/libavformat/asfdec.c b/libavformat/asfdec.c
index 359ee8b..28b429a 100644
--- a/libavformat/asfdec.c
+++ b/libavformat/asfdec.c
@@ -1,6 +1,7 @@
 /*
  * ASF compatible demuxer
  * Copyright (c) 2000, 2001 Fabrice Bellard
+ * Copyright (c) 2015 Vadim Belov
  *
  * This file is part of FFmpeg.
  *
@@ -38,6 +39,7 @@
 #include "riff.h"
 #include "asf.h"
 #include "asfcrypt.h"
+#include "asf_trim.h"
 
 typedef struct ASFContext {
     const AVClass *class;
@@ -81,6 +83,7 @@ typedef struct ASFContext {
 
     int no_resync_search;
     int export_xmp;
+    int direction[ASF_MAX_STREAMS_NUM];
 } ASFContext;
 
 static const AVOption options[] = {
@@ -672,6 +675,8 @@ static int asf_read_metadata(AVFormatContext *s, int64_t size)
         av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
                 i, stream_num, name_len, value_type, value_len, name);
 
+        asf->direction[stream_num] = NO_STREAM_DIRECTION;
+        
         if (!strcmp(name, "AspectRatioX")){
             int aspect_x = get_value(s->pb, value_type, 16);
             if(stream_num < 128)
@@ -680,7 +685,15 @@ static int asf_read_metadata(AVFormatContext *s, int64_t size)
             int aspect_y = get_value(s->pb, value_type, 16);
             if(stream_num < 128)
                 asf->dar[stream_num].den = aspect_y;
-        } else {
+        } else if(!strcmp(name, STREAM_DIRECTION_STR) &&
+                    0 < stream_num && stream_num < 128){
+            int direction = get_value(s->pb, value_type, value_len);
+            
+            av_log(s, AV_LOG_DEBUG, "stream %d Direction is %d\n",
+                stream_num, direction);
+            
+            asf->direction[stream_num] = direction;
+        } else {            
             get_tag(s, name, value_type, value_len, 16);
         }
     }
@@ -855,6 +868,8 @@ static int asf_read_header(AVFormatContext *s)
                           &st->sample_aspect_ratio.den,
                           asf->dar[0].num, asf->dar[0].den, INT_MAX);
 
+            set_stream_direction(s, asf->direction, st, i);
+            
             av_log(s, AV_LOG_TRACE, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
                     i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
                     st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c
index 015c731..782c5cc 100644
--- a/libavformat/asfenc.c
+++ b/libavformat/asfenc.c
@@ -1,6 +1,7 @@
 /*
  * ASF muxer
  * Copyright (c) 2000, 2001 Fabrice Bellard
+ * Copyright (c) 2015 Vadim Belov
  *
  * This file is part of FFmpeg.
  *
@@ -27,6 +28,7 @@
 #include "internal.h"
 #include "riff.h"
 #include "asf.h"
+#include "asf_trim.h"
 
 #define ASF_INDEXED_INTERVAL    10000000
 #define ASF_INDEX_BLOCK         (1<<9)
@@ -218,7 +220,12 @@ static const AVCodecTag codec_asf_bmp_tags[] = {
     { AV_CODEC_ID_NONE,      0 },
 };
 
-#define PREROLL_TIME 3100
+/**
+ * Preroll must be 0 in order to be consistent with player
+ * instead of:
+ * #define PREROLL_TIME 3100
+ */
+#define PREROLL_TIME 0
 
 static void put_str16(AVIOContext *s, const char *tag)
 {
@@ -350,6 +357,7 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
     AVDictionaryEntry *tags[5];
     int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
     int has_title, has_aspect_ratio = 0;
+    int has_direction = 0; 
     int metadata_count;
     AVCodecContext *enc;
     int64_t header_offset, cur_pos, hpos;
@@ -381,6 +389,8 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
             has_aspect_ratio++;
     }
 
+    has_direction = asf_get_streams_direction(s);
+
     if (asf->is_streamed) {
         put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
     }
@@ -412,7 +422,36 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size,
     hpos = put_header(pb, &ff_asf_head1_guid);
     ff_put_guid(pb, &ff_asf_head2_guid);
     avio_wl16(pb, 6);
-    if (has_aspect_ratio) {
+    
+    if (has_direction) {
+        int64_t hpos2;
+        avio_wl32(pb, 26 + has_direction * 46);
+        hpos2 = put_header(pb, &ff_asf_metadata_header);        
+        avio_wl16(pb, has_direction);
+        for (n = 0; n < s->nb_streams; n++) {
+            int streamId;
+            int direction;
+
+            AVDictionaryEntry *t = av_dict_get(
+                s->streams[n]->metadata, DIRECTION_DICT_KEY, NULL, 0);
+
+            sscanf(t->value, "%d %d", &streamId, &direction);
+
+            av_log(s, AV_LOG_DEBUG, "Writing stream[%d] direction of stream %d, (%d)\n",
+                n + 1, streamId, direction);
+
+            avio_wl16(pb, 0);       // Reserved
+            avio_wl16(pb, streamId);// Stream Number
+            avio_wl16(pb, 32);      // Name Length
+            avio_wl16(pb, 5);       // Data Type 
+            avio_wl32(pb, 2);       // Data Length
+            avio_put_str16le(pb, STREAM_DIRECTION_STR);  // 32
+            avio_wl16(pb, direction);
+        }
+        end_header(pb, hpos2);
+
+    } else if (has_aspect_ratio) {
+    
         int64_t hpos2;
         avio_wl32(pb, 26 + has_aspect_ratio * 84);
         hpos2 = put_header(pb, &ff_asf_metadata_header);
@@ -657,12 +696,16 @@ static int asf_write_header(AVFormatContext *s)
     asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
     asf->maximum_packet        = 0;
 
+    if (asf_alloc_streams_index(asf->streams, s->nb_streams, ASF_INDEX_BLOCK) 
+        != 0) return -1;
+    
     /* the data-chunk-size has to be 50 (DATA_HEADER_SIZE), which is
      * data_size - asf->data_offset at the moment this function is done.
      * It is needed to use asf as a streamable format. */
     if (asf_write_header1(s, 0, DATA_HEADER_SIZE) < 0) {
         //av_free(asf);
         av_freep(&asf->index_ptr);
+        asf_free_streams_index(asf->streams, s->nb_streams);
         return -1;
     }
 
@@ -945,7 +988,17 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
             return ret;
     }
     asf->end_sec = start_sec;
-
+    
+    ret = upadte_indices( 
+        asf->streams,
+        pts,
+        offset, 
+        pkt->stream_index, 
+        pkt->duration,
+        ASF_INDEX_BLOCK,
+        asf->data_offset + DATA_HEADER_SIZE);
+    if (ret < 0) return ret;
+    
     return 0;
 }
 
@@ -988,6 +1041,10 @@ static int asf_write_trailer(AVFormatContext *s)
     }
     avio_flush(s->pb);
 
+    /* write indices with duration */
+    asf_write_indices(s, asf->streams, s->nb_streams);
+    avio_flush(s->pb);
+
     if (asf->is_streamed || !s->pb->seekable) {
         put_chunk(s, 0x4524, 0, 0); /* end of stream */
     } else {
@@ -997,6 +1054,7 @@ static int asf_write_trailer(AVFormatContext *s)
         asf_write_header1(s, file_size, data_size - asf->data_offset);
     }
 
+    asf_free_streams_index(asf->streams, s->nb_streams);
     av_freep(&asf->index_ptr);
     return 0;
 }


More information about the ffmpeg-devel mailing list