[SCM] libav/experimental: fix obnoxious ogg_packet passing from encoder to muxer

siretart at users.alioth.debian.org siretart at users.alioth.debian.org
Sun Jun 30 15:39:46 UTC 2013


The following commit has been merged in the experimental branch:
commit 6d8f985ecfb1cab04dc114307091282db6f16933
Author: Michael Niedermayer <michaelni at gmx.at>
Date:   Sun Apr 4 14:39:20 2004 +0000

    fix obnoxious ogg_packet passing from encoder to muxer
    
    Originally committed as revision 2955 to svn://svn.ffmpeg.org/ffmpeg/trunk

diff --git a/libavcodec/oggvorbis.c b/libavcodec/oggvorbis.c
index 16516e1..104895a 100644
--- a/libavcodec/oggvorbis.c
+++ b/libavcodec/oggvorbis.c
@@ -9,13 +9,17 @@
 #include "avcodec.h"
 #include "oggvorbis.h"
 
-#define OGGVORBIS_FRAME_SIZE 1024
+//#define OGGVORBIS_FRAME_SIZE 1024
+#define OGGVORBIS_FRAME_SIZE 64
 
+#define BUFFER_SIZE (1024*64)
 
 typedef struct OggVorbisContext {
     vorbis_info vi ;
     vorbis_dsp_state vd ;
     vorbis_block vb ;
+    uint8_t buffer[BUFFER_SIZE];
+    int buffer_index;
 
     /* decoder */
     vorbis_comment vc ;
@@ -85,20 +89,33 @@ static int oggvorbis_encode_frame(AVCodecContext *avccontext,
     
     vorbis_analysis_wrote(&context->vd, samples) ; 
 
-    l = 0 ;
-
     while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
 	vorbis_analysis(&context->vb, NULL);
 	vorbis_bitrate_addblock(&context->vb) ;
 
 	while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
-	    memcpy(packets + l, &op, sizeof(ogg_packet)) ;
-	    memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
-	    l += sizeof(ogg_packet) + op.bytes ;
+            memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
+            context->buffer_index += sizeof(ogg_packet);
+            memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
+            context->buffer_index += op.bytes;
+//            av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
 	}
     }
 
-    return l ;
+    if(context->buffer_index){
+        ogg_packet *op2= context->buffer;
+        op2->packet = context->buffer + sizeof(ogg_packet);
+        l=  op2->bytes;
+        
+        memcpy(packets, op2->packet, l);
+        context->buffer_index -= l + sizeof(ogg_packet);
+        memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
+        
+//        av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
+        return l;
+    }
+
+    return 0;
 }
 
 
diff --git a/libavformat/ogg.c b/libavformat/ogg.c
index ed5bc3f..3c8b1e2 100644
--- a/libavformat/ogg.c
+++ b/libavformat/ogg.c
@@ -14,6 +14,9 @@
 #include "avformat.h"
 #include "oggvorbis.h"
 
+#undef NDEBUG
+#include <assert.h>
+
 #define DECODER_BUFFER_SIZE 4096
 
 
@@ -21,8 +24,7 @@ typedef struct OggContext {
     /* output */
     ogg_stream_state os ;
     int header_handled ;
-    ogg_int64_t base_packet_no ;
-    ogg_int64_t base_granule_pos ;
+    ogg_packet op;
 
     /* input */
     ogg_sync_state oy ;
@@ -40,7 +42,9 @@ static int ogg_write_header(AVFormatContext *avfcontext)
     vorbis_block vb ;
     ogg_packet header, header_comm, header_code ; 
     int n ;
-    
+
+    av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE);
+
     ogg_stream_init(&context->os, 31415);
     
     for(n = 0 ; n < avfcontext->nb_streams ; n++) {
@@ -79,7 +83,6 @@ static int ogg_write_header(AVFormatContext *avfcontext)
 	/* end of vorbis specific code */
 
 	context->header_handled = 0 ;
-	context->base_packet_no = 0 ;
     }
     
     return 0 ;
@@ -88,13 +91,21 @@ static int ogg_write_header(AVFormatContext *avfcontext)
 
 static int ogg_write_packet(AVFormatContext *avfcontext,
 			    int stream_index,
-			    const uint8_t *buf, int size, int64_t force_pts)
+			    const uint8_t *buf, int size, int64_t pts)
 {
     OggContext *context = avfcontext->priv_data ;
-    ogg_packet *op ;
+    AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec;
+    ogg_packet *op= &context->op;
     ogg_page og ;
-    int l = 0 ;
-    
+
+    pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE);
+
+    if(!size){
+//        av_log(avfcontext, AV_LOG_DEBUG, "zero packet\n");
+        return 0;
+    }
+//    av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
+
     /* flush header packets so audio starts on a new page */
 
     if(!context->header_handled) {
@@ -106,29 +117,21 @@ static int ogg_write_packet(AVFormatContext *avfcontext,
 	context->header_handled = 1 ;
     }
 
-    while(l < size) {
-	op = (ogg_packet*)(buf + l) ;
-	op->packet = (uint8_t*) buf + l + sizeof( ogg_packet) ; /* fix data pointer */
-
-	if(!context->base_packet_no) { /* this is the first packet */
-	    context->base_packet_no = op->packetno ; 
-	    context->base_granule_pos = op->granulepos ;
-	}
-
-	/* correct the fields in the packet -- essential for streaming */
-
-	op->packetno -= context->base_packet_no ;
-	op->granulepos -= context->base_granule_pos ;
-
-	ogg_stream_packetin(&context->os, op) ;
-	l += sizeof(ogg_packet) + op->bytes ;
-
-	while(ogg_stream_pageout(&context->os, &og)) {
-	    put_buffer(&avfcontext->pb, og.header, og.header_len) ;
-	    put_buffer(&avfcontext->pb, og.body, og.body_len) ;
-	    put_flush_packet(&avfcontext->pb);
-	}
-    }
+    op->packet = (uint8_t*) buf;
+    op->bytes  = size;
+    op->b_o_s  = op->packetno == 0;
+    op->granulepos= pts;
+
+    /* correct the fields in the packet -- essential for streaming */
+                                                        
+    ogg_stream_packetin(&context->os, op);              
+                                                        
+    while(ogg_stream_pageout(&context->os, &og)) {
+        put_buffer(&avfcontext->pb, og.header, og.header_len);
+	put_buffer(&avfcontext->pb, og.body, og.body_len);     
+	put_flush_packet(&avfcontext->pb);
+    }                                                   
+    op->packetno++;
 
     return 0;
 }
diff --git a/libavformat/utils.c b/libavformat/utils.c
index c170c03..14f965d 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -1707,7 +1707,11 @@ int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
     switch (st->codec.codec_type) {
     case CODEC_TYPE_AUDIO:
         frame_size = get_audio_frame_size(&st->codec, size);
-        if (frame_size >= 0) {
+
+        /* note, we skip the initial 0-size packets as they are most likely equal to the encoder delay,
+           but it would be better if we had the real timestamps from the encoder */
+//        av_log(s, AV_LOG_DEBUG, "%d %lld %lld\n", size, st->pts.num, st->pts.val);
+        if (frame_size >= 0 && (size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
             av_frac_add(&st->pts, 
                         (int64_t)s->pts_den * frame_size);
         }
@@ -1900,6 +1904,8 @@ int64_t parse_date(const char *datestr, int duration)
     const char *q;
     int is_utc, len;
     char lastch;
+
+#undef time
     time_t now = time(0);
 
     len = strlen(datestr);

-- 
Libav/FFmpeg packaging



More information about the pkg-multimedia-commits mailing list