changeset 1322:95f56c7b24eb libavformat

* Moving FifoBuffer out of libavformat/avformat.h and libavformat/utils.c into libavutil
author romansh
date Thu, 21 Sep 2006 07:31:53 +0000
parents 9eeb01383e30
children e9bf6926aecf
files avformat.h dvenc.c gxfenc.c mpeg.c utils.c
diffstat 5 files changed, 38 insertions(+), 204 deletions(-) [+]
line wrap: on
line diff
--- a/avformat.h	Wed Sep 20 21:23:32 2006 +0000
+++ b/avformat.h	Thu Sep 21 07:31:53 2006 +0000
@@ -441,29 +441,6 @@
 
 void av_register_all(void);
 
-typedef struct FifoBuffer {
-    uint8_t *buffer;
-    uint8_t *rptr, *wptr, *end;
-} FifoBuffer;
-
-int fifo_init(FifoBuffer *f, int size);
-void fifo_free(FifoBuffer *f);
-int fifo_size(FifoBuffer *f, uint8_t *rptr);
-int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr);
-void fifo_write(FifoBuffer *f, const uint8_t *buf, int size, uint8_t **wptr_ptr);
-int put_fifo(ByteIOContext *pb, FifoBuffer *f, int buf_size, uint8_t **rptr_ptr);
-void fifo_realloc(FifoBuffer *f, unsigned int size);
-static inline uint8_t fifo_peek(FifoBuffer *f, int offs)
-{
-    return f->buffer[(f->rptr - f->buffer + offs) % (f->end - f->buffer)];
-}
-static inline void fifo_drain(FifoBuffer *f, int size)
-{
-    f->rptr += size;
-    if (f->rptr >= f->end)
-        f->rptr = f->buffer + (f->rptr - f->end);
-}
-
 /* media file input */
 AVInputFormat *av_find_input_format(const char *short_name);
 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
--- a/dvenc.c	Wed Sep 20 21:23:32 2006 +0000
+++ b/dvenc.c	Thu Sep 21 07:31:53 2006 +0000
@@ -29,12 +29,13 @@
 #include "avformat.h"
 #include "dvdata.h"
 #include "dv.h"
+#include "fifo.h"
 
 struct DVMuxContext {
     const DVprofile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
     int         n_ast;        /* Number of stereo audio streams (up to 2) */
     AVStream   *ast[2];       /* Stereo audio streams */
-    FifoBuffer  audio_data[2]; /* Fifo for storing excessive amounts of PCM */
+    AVFifoBuffer  audio_data[2]; /* Fifo for storing excessive amounts of PCM */
     int         frames;       /* Number of a current frame */
     time_t      start_time;   /* Start time of recording */
     int         has_audio;    /* frame under contruction has audio */
@@ -185,8 +186,8 @@
              if (of*2 >= size)
                  continue;
 
-             frame_ptr[d] = fifo_peek(&c->audio_data[channel], of*2+1); // FIXME: may be we have to admit
-             frame_ptr[d+1] = fifo_peek(&c->audio_data[channel], of*2); //        that DV is a big endian PCM
+             frame_ptr[d] = av_fifo_peek(&c->audio_data[channel], of*2+1); // FIXME: may be we have to admit
+             frame_ptr[d+1] = av_fifo_peek(&c->audio_data[channel], of*2); //        that DV is a big endian PCM
           }
           frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
        }
@@ -247,12 +248,12 @@
           for (i = 0; i < c->n_ast && st != c->ast[i]; i++);
 
           /* FIXME: we have to have more sensible approach than this one */
-          if (fifo_size(&c->audio_data[i], c->audio_data[i].rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
+          if (av_fifo_size(&c->audio_data[i]) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
               av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
-          fifo_write(&c->audio_data[i], data, data_size, &c->audio_data[i].wptr);
+          av_fifo_write(&c->audio_data[i], data, data_size);
 
           /* Lets see if we've got enough audio for one DV frame */
-          c->has_audio |= ((reqasize <= fifo_size(&c->audio_data[i], c->audio_data[i].rptr)) << i);
+          c->has_audio |= ((reqasize <= av_fifo_size(&c->audio_data[i])) << i);
 
           break;
     default:
@@ -264,7 +265,7 @@
         dv_inject_metadata(c, *frame);
         for (i=0; i<c->n_ast; i++) {
              dv_inject_audio(c, i, *frame);
-             fifo_drain(&c->audio_data[i], reqasize);
+             av_fifo_drain(&c->audio_data[i], reqasize);
         }
 
         c->has_video = 0;
@@ -333,10 +334,10 @@
     c->start_time = (time_t)s->timestamp;
 
     for (i=0; i<c->n_ast; i++) {
-        if (c->ast[i] && fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
+        if (c->ast[i] && av_fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
             while (i>0) {
                 i--;
-                fifo_free(&c->audio_data[i]);
+                av_fifo_free(&c->audio_data[i]);
             }
             goto bail_out;
         }
@@ -353,7 +354,7 @@
 {
     int i;
     for (i=0; i < c->n_ast; i++)
-        fifo_free(&c->audio_data[i]);
+        av_fifo_free(&c->audio_data[i]);
 }
 
 #ifdef CONFIG_MUXERS
--- a/gxfenc.c	Wed Sep 20 21:23:32 2006 +0000
+++ b/gxfenc.c	Thu Sep 21 07:31:53 2006 +0000
@@ -20,12 +20,13 @@
 #include "avformat.h"
 #include "gxf.h"
 #include "riff.h"
+#include "fifo.h"
 
 #define GXF_AUDIO_PACKET_SIZE 65536
 
 typedef struct GXFStreamContext {
     AVCodecContext *codec;
-    FifoBuffer audio_buffer;
+    AVFifoBuffer audio_buffer;
     uint32_t track_type;
     uint32_t sample_size;
     uint32_t sample_rate;
@@ -604,7 +605,7 @@
             sc->fields = -2;
             gxf->audio_tracks++;
             gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
-            fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
+            av_fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
         } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) {
             /* FIXME check from time_base ? */
             if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */
@@ -670,7 +671,7 @@
 
     for (i = 0; i < s->nb_streams; ++i) {
         if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
-            fifo_free(&gxf->streams[i].audio_buffer);
+            av_fifo_free(&gxf->streams[i].audio_buffer);
         }
         if (s->streams[i]->codec->frame_number > gxf->nb_frames)
             gxf->nb_frames = 2 * s->streams[i]->codec->frame_number;
@@ -762,12 +763,12 @@
 
 static int gxf_new_audio_packet(GXFContext *gxf, GXFStreamContext *sc, AVPacket *pkt, int flush)
 {
-    int size = flush ? fifo_size(&sc->audio_buffer, NULL) : GXF_AUDIO_PACKET_SIZE;
+    int size = flush ? av_fifo_size(&sc->audio_buffer) : GXF_AUDIO_PACKET_SIZE;
 
     if (!size)
         return 0;
     av_new_packet(pkt, size);
-    fifo_read(&sc->audio_buffer, pkt->data, size, NULL);
+    av_fifo_read(&sc->audio_buffer, pkt->data, size);
     pkt->stream_index = sc->index;
     pkt->dts = sc->current_dts;
     sc->current_dts += size / 2; /* we only support 16 bit pcm mono for now */
@@ -784,10 +785,10 @@
         if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
             GXFStreamContext *sc = &gxf->streams[i];
             if (pkt && pkt->stream_index == i) {
-                fifo_write(&sc->audio_buffer, pkt->data, pkt->size, NULL);
+                av_fifo_write(&sc->audio_buffer, pkt->data, pkt->size);
                 pkt = NULL;
             }
-            if (flush || fifo_size(&sc->audio_buffer, NULL) >= GXF_AUDIO_PACKET_SIZE) {
+            if (flush || av_fifo_size(&sc->audio_buffer) >= GXF_AUDIO_PACKET_SIZE) {
                 if (gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) {
                     pkt = &new_pkt;
                     break; /* add pkt right now into list */
--- a/mpeg.c	Wed Sep 20 21:23:32 2006 +0000
+++ b/mpeg.c	Thu Sep 21 07:31:53 2006 +0000
@@ -18,6 +18,7 @@
  */
 #include "avformat.h"
 #include "bitstream.h"
+#include "fifo.h"
 
 #define MAX_PAYLOAD_SIZE 4096
 //#define DEBUG_SEEK
@@ -35,7 +36,7 @@
 } PacketDesc;
 
 typedef struct {
-    FifoBuffer fifo;
+    AVFifoBuffer fifo;
     uint8_t id;
     int max_buffer_size; /* in bytes */
     int buffer_index;
@@ -45,7 +46,7 @@
     int packet_number;
     uint8_t lpcm_header[3];
     int lpcm_align;
-    uint8_t *fifo_iframe_ptr;
+    int bytes_to_iframe;
     int align_iframe;
     int64_t vobu_start_pts;
 } StreamInfo;
@@ -412,7 +413,7 @@
         default:
             return -1;
         }
-        fifo_init(&stream->fifo, 16);
+        av_fifo_init(&stream->fifo, 16);
     }
     bitrate = 0;
     audio_bitrate = 0;
@@ -708,14 +709,7 @@
             }
         } else if (s->is_dvd) {
             if (stream->align_iframe || s->packet_number == 0){
-                int bytes_to_iframe;
-                int PES_bytes_to_fill;
-                if (stream->fifo_iframe_ptr >= stream->fifo.rptr) {
-                    bytes_to_iframe = stream->fifo_iframe_ptr - stream->fifo.rptr;
-                } else {
-                    bytes_to_iframe = (stream->fifo.end - stream->fifo.rptr) + (stream->fifo_iframe_ptr - stream->fifo.buffer);
-                }
-                PES_bytes_to_fill = s->packet_size - size - 10;
+                int PES_bytes_to_fill = s->packet_size - size - 10;
 
                 if (pts != AV_NOPTS_VALUE) {
                     if (dts != pts)
@@ -724,7 +718,7 @@
                         PES_bytes_to_fill -= 5;
                 }
 
-                if (bytes_to_iframe == 0 || s->packet_number == 0) {
+                if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
                     size = put_system_header(ctx, buf_ptr, 0);
                     buf_ptr += size;
                     size = buf_ptr - buffer;
@@ -751,8 +745,8 @@
                     s->last_scr= scr;
                     buf_ptr += size;
                     /* GOP Start */
-                } else if (bytes_to_iframe < PES_bytes_to_fill) {
-                    pad_packet_bytes = PES_bytes_to_fill - bytes_to_iframe;
+                } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
+                    pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
                 }
             }
         } else {
@@ -824,7 +818,7 @@
             startcode = 0x100 + id;
         }
 
-        stuffing_size = payload_size - fifo_size(&stream->fifo, stream->fifo.rptr);
+        stuffing_size = payload_size - av_fifo_size(&stream->fifo);
 
         // first byte doesnt fit -> reset pts/dts + stuffing
         if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
@@ -951,8 +945,9 @@
         }
 
         /* output data */
-        if(put_fifo(&ctx->pb, &stream->fifo, payload_size - stuffing_size, &stream->fifo.rptr) < 0)
+        if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0)
             return -1;
+        stream->bytes_to_iframe -= payload_size - stuffing_size;
     }else{
         payload_size=
         stuffing_size= 0;
@@ -1065,7 +1060,7 @@
     for(i=0; i<ctx->nb_streams; i++){
         AVStream *st = ctx->streams[i];
         StreamInfo *stream = st->priv_data;
-        const int avail_data=  fifo_size(&stream->fifo, stream->fifo.rptr);
+        const int avail_data=  av_fifo_size(&stream->fifo);
         const int space= stream->max_buffer_size - stream->buffer_index;
         int rel_space= 1024*space / stream->max_buffer_size;
         PacketDesc *next_pkt= stream->premux_packet;
@@ -1125,7 +1120,7 @@
     st = ctx->streams[best_i];
     stream = st->priv_data;
 
-    assert(fifo_size(&stream->fifo, stream->fifo.rptr) > 0);
+    assert(av_fifo_size(&stream->fifo) > 0);
 
     assert(avail_space >= s->packet_size || ignore_constraints);
 
@@ -1141,7 +1136,7 @@
 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i);
         es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
     }else{
-        assert(fifo_size(&stream->fifo, stream->fifo.rptr) == trailer_size);
+        assert(av_fifo_size(&stream->fifo) == trailer_size);
         es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
     }
 
@@ -1204,11 +1199,11 @@
         stream->predecode_packet= pkt_desc;
     stream->next_packet= &pkt_desc->next;
 
-    fifo_realloc(&stream->fifo, fifo_size(&stream->fifo, NULL) + size + 1);
+    av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
 
     if (s->is_dvd){
         if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
-            stream->fifo_iframe_ptr = stream->fifo.wptr;
+            stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
             stream->align_iframe = 1;
             stream->vobu_start_pts = pts;
         } else {
@@ -1216,7 +1211,7 @@
         }
     }
 
-    fifo_write(&stream->fifo, buf, size, &stream->fifo.wptr);
+    av_fifo_write(&stream->fifo, buf, size);
 
     for(;;){
         int ret= output_packet(ctx, 0);
@@ -1248,8 +1243,8 @@
     for(i=0;i<ctx->nb_streams;i++) {
         stream = ctx->streams[i]->priv_data;
 
-        assert(fifo_size(&stream->fifo, stream->fifo.rptr) == 0);
-        fifo_free(&stream->fifo);
+        assert(av_fifo_size(&stream->fifo) == 0);
+        av_fifo_free(&stream->fifo);
     }
     return 0;
 }
--- a/utils.c	Wed Sep 20 21:23:32 2006 +0000
+++ b/utils.c	Thu Sep 21 07:31:53 2006 +0000
@@ -263,146 +263,6 @@
     return 0;
 }
 
-/* fifo handling */
-
-int fifo_init(FifoBuffer *f, int size)
-{
-    f->buffer = av_malloc(size);
-    if (!f->buffer)
-        return -1;
-    f->end = f->buffer + size;
-    f->wptr = f->rptr = f->buffer;
-    return 0;
-}
-
-void fifo_free(FifoBuffer *f)
-{
-    av_free(f->buffer);
-}
-
-int fifo_size(FifoBuffer *f, uint8_t *rptr)
-{
-    int size;
-
-    if(!rptr)
-        rptr= f->rptr;
-
-    if (f->wptr >= rptr) {
-        size = f->wptr - rptr;
-    } else {
-        size = (f->end - rptr) + (f->wptr - f->buffer);
-    }
-    return size;
-}
-
-/**
- * Get data from the fifo (returns -1 if not enough data).
- */
-int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr)
-{
-    uint8_t *rptr;
-    int size, len;
-
-    if(!rptr_ptr)
-        rptr_ptr= &f->rptr;
-    rptr = *rptr_ptr;
-
-    if (f->wptr >= rptr) {
-        size = f->wptr - rptr;
-    } else {
-        size = (f->end - rptr) + (f->wptr - f->buffer);
-    }
-
-    if (size < buf_size)
-        return -1;
-    while (buf_size > 0) {
-        len = f->end - rptr;
-        if (len > buf_size)
-            len = buf_size;
-        memcpy(buf, rptr, len);
-        buf += len;
-        rptr += len;
-        if (rptr >= f->end)
-            rptr = f->buffer;
-        buf_size -= len;
-    }
-    *rptr_ptr = rptr;
-    return 0;
-}
-
-/**
- * Resizes a FIFO.
- */
-void fifo_realloc(FifoBuffer *f, unsigned int new_size){
-    unsigned int old_size= f->end - f->buffer;
-
-    if(old_size < new_size){
-        uint8_t *old= f->buffer;
-
-        f->buffer= av_realloc(f->buffer, new_size);
-
-        f->rptr += f->buffer - old;
-        f->wptr += f->buffer - old;
-
-        if(f->wptr < f->rptr){
-            memmove(f->rptr + new_size - old_size, f->rptr, f->buffer + old_size - f->rptr);
-            f->rptr += new_size - old_size;
-        }
-        f->end= f->buffer + new_size;
-    }
-}
-
-void fifo_write(FifoBuffer *f, const uint8_t *buf, int size, uint8_t **wptr_ptr)
-{
-    int len;
-    uint8_t *wptr;
-
-    if(!wptr_ptr)
-        wptr_ptr= &f->wptr;
-    wptr = *wptr_ptr;
-
-    while (size > 0) {
-        len = f->end - wptr;
-        if (len > size)
-            len = size;
-        memcpy(wptr, buf, len);
-        wptr += len;
-        if (wptr >= f->end)
-            wptr = f->buffer;
-        buf += len;
-        size -= len;
-    }
-    *wptr_ptr = wptr;
-}
-
-/* get data from the fifo (return -1 if not enough data) */
-int put_fifo(ByteIOContext *pb, FifoBuffer *f, int buf_size, uint8_t **rptr_ptr)
-{
-    uint8_t *rptr = *rptr_ptr;
-    int size, len;
-
-    if (f->wptr >= rptr) {
-        size = f->wptr - rptr;
-    } else {
-        size = (f->end - rptr) + (f->wptr - f->buffer);
-    }
-
-    if (size < buf_size)
-        return -1;
-    while (buf_size > 0) {
-        len = f->end - rptr;
-        if (len > buf_size)
-            len = buf_size;
-        put_buffer(pb, rptr, len);
-        rptr += len;
-        if (rptr >= f->end)
-            rptr = f->buffer;
-        buf_size -= len;
-    }
-    *rptr_ptr = rptr;
-    return 0;
-}
-
 /**
  * Allocate the payload of a packet and intialized its fields to default values.
  *