[FFmpeg-devel] [PATCH] nutenc: mux chapters.

Anton Khirnov wyskas
Sun Feb 28 22:11:55 CET 2010


---
 libavformat/nut.h    |    5 ++++
 libavformat/nutenc.c |   62 ++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/libavformat/nut.h b/libavformat/nut.h
index 35593e9..1d85f2f 100644
--- a/libavformat/nut.h
+++ b/libavformat/nut.h
@@ -82,6 +82,10 @@ typedef struct {
 } StreamContext;
 
 typedef struct {
+    AVRational *time_base;
+} ChapterContext;
+
+typedef struct {
     AVFormatContext *avf;
 //    int written_packet_size;
 //    int64_t packet_start;
@@ -90,6 +94,7 @@ typedef struct {
     const uint8_t *header[128];
     uint64_t next_startcode;     ///< stores the next startcode if it has already been parsed but the stream is not seekable
     StreamContext *stream;
+    ChapterContext *chapter;
     unsigned int max_distance;
     unsigned int time_base_count;
     int64_t last_syncpoint_pos;
diff --git a/libavformat/nutenc.c b/libavformat/nutenc.c
index 03d3712..bf21dca 100644
--- a/libavformat/nutenc.c
+++ b/libavformat/nutenc.c
@@ -261,9 +261,9 @@ static void put_v(ByteIOContext *bc, uint64_t val){
     put_byte(bc, val&127);
 }
 
-static void put_tt(NUTContext *nut, StreamContext *nus, ByteIOContext *bc, uint64_t val){
+static void put_tt(NUTContext *nut, AVRational *time_base, ByteIOContext *bc, uint64_t val){
     val *= nut->time_base_count;
-    val += nus->time_base - nut->time_base;
+    val += time_base - nut->time_base;
     put_v(bc, val);
 }
 
@@ -503,6 +503,34 @@ static int write_streaminfo(NUTContext *nut, ByteIOContext *bc, int stream_id){
     return count;
 }
 
+static int write_chapter(NUTContext *nut, ByteIOContext *bc, int id)
+{
+    ByteIOContext *dyn_bc;
+    uint8_t *dyn_buf = NULL;
+    AVMetadataTag *t = NULL;
+    AVChapter *ch    = nut->avf->chapters[id];
+    int ret, dyn_size, count = 0;
+
+    ret = url_open_dyn_buf(&dyn_bc);
+    if (ret < 0)
+        return ret;
+
+    put_v(bc, 0);                                           // stream_id_plus1
+    put_s(bc, id + 1);                                      // chapter_id
+    put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
+    put_v(bc, ch->end - ch->start);                         // chapter_len
+
+    while ((t = av_metadata_get(ch->metadata, "", t, AV_METADATA_IGNORE_SUFFIX)))
+        count += add_info(dyn_bc, t->key, t->value);
+
+    put_v(bc, count);
+
+    dyn_size = url_close_dyn_buf(dyn_bc, &dyn_buf);
+    put_buffer(bc, dyn_buf, dyn_size);
+    av_freep(&dyn_buf);
+    return 0;
+}
+
 static int write_headers(NUTContext *nut, ByteIOContext *bc){
     ByteIOContext *dyn_bc;
     int i, ret;
@@ -543,6 +571,16 @@ static int write_headers(NUTContext *nut, ByteIOContext *bc){
         }
     }
 
+    for (i = 0; i < nut->avf->nb_chapters; i++) {
+        ret = url_open_dyn_buf(&dyn_bc);
+        if (ret < 0)
+            return ret;
+        ret = write_chapter(nut, dyn_bc, i);
+        if (ret < 0)
+            return ret;
+        put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
+    }
+
     nut->last_syncpoint_pos= INT_MIN;
     nut->header_count++;
     return 0;
@@ -556,7 +594,9 @@ static int write_header(AVFormatContext *s){
     nut->avf= s;
 
     nut->stream   = av_mallocz(sizeof(StreamContext)*s->nb_streams);
-    nut->time_base= av_mallocz(sizeof(AVRational   )*s->nb_streams);
+    nut->chapter  = av_mallocz(sizeof(ChapterContext)*s->nb_chapters);
+    nut->time_base= av_mallocz(sizeof(AVRational   )*(s->nb_streams +
+                                                      s->nb_chapters));
 
     for(i=0; i<s->nb_streams; i++){
         AVStream *st= s->streams[i];
@@ -583,6 +623,20 @@ static int write_header(AVFormatContext *s){
         nut->stream[i].max_pts_distance= FFMAX(1/av_q2d(time_base), 1);
     }
 
+    for (i = 0; i < s->nb_chapters; i++) {
+        AVChapter *ch = s->chapters[i];
+
+        for (j = 0; j < nut->time_base_count; j++) {
+            if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
+                break;
+        }
+
+        nut->time_base[j] = ch->time_base;
+        nut->chapter[i].time_base = &nut->time_base[j];
+        if(j == nut->time_base_count)
+            nut->time_base_count++;
+    }
+
     nut->max_distance = MAX_DISTANCE;
     build_elision_headers(s);
     build_frame_code(s);
@@ -683,7 +737,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt){
         ret = url_open_dyn_buf(&dyn_bc);
         if(ret < 0)
             return ret;
-        put_tt(nut, nus, dyn_bc, pkt->dts);
+        put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
         put_v(dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos)>>4 : 0);
         put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
 
-- 
1.6.6.1




More information about the ffmpeg-devel mailing list