[FFmpeg-devel] [PATCH/RFC]Support muxing MP2/MP3 in caf

Carl Eugen Hoyos cehoyos at ag.or.at
Mon Jul 4 01:26:53 CEST 2011


Hi!

Attached patch allows muxing of MP2/MP3 in caf.

I suspect the realloc should be done differently, please advise, Carl Eugen
-------------- next part --------------
diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c
index 4156c49..027f190 100644
--- a/libavformat/cafenc.c
+++ b/libavformat/cafenc.c
@@ -28,6 +28,9 @@
 
 typedef struct {
     int64_t data;
+    uint8_t *pkt_sizes;
+    int size_buffer_size;
+    int packets;
 } CAFContext;
 
 static uint32_t codec_flags(enum CodecID codec_id) {
@@ -116,11 +119,6 @@ static int caf_write_header(AVFormatContext *s)
         return AVERROR_INVALIDDATA;
     }
 
-    if (!enc->block_align) {
-        av_log(s, AV_LOG_ERROR, "muxing with unknown or variable packet size not yet supported\n");
-        return AVERROR_PATCHWELCOME;
-    }
-
     ffio_wfourcc(pb, "caff"); //< mFileType
     avio_wb16(pb, 1);         //< mFileVersion
     avio_wb16(pb, 0);         //< mFileFlags
@@ -152,13 +150,36 @@ static int caf_write_header(AVFormatContext *s)
 
 static int caf_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
+    CAFContext *caf = s->priv_data;
+
     avio_write(s->pb, pkt->data, pkt->size);
+    if (!s->streams[0]->codec->block_align) {
+        int size = pkt->size, c = 0;
+        while (size) {
+            c <<= 8;
+            c |= 128 | size & 127;
+            size >>=7;
+        }
+        while (c) {
+            caf->pkt_sizes = av_realloc(caf->pkt_sizes, ++caf->size_buffer_size);
+            if (!caf->pkt_sizes)
+                return AVERROR(ENOMEM);
+            if (c & ~255) {
+                caf->pkt_sizes[caf->size_buffer_size - 1] = 128 | c & 255;
+            } else {
+                caf->pkt_sizes[caf->size_buffer_size - 1] = c & 127;
+            }
+            c >>= 8;
+        }
+        caf->packets++;
+    }
     return 0;
 }
 
 static int caf_write_trailer(AVFormatContext *s)
 {
     AVIOContext *pb = s->pb;
+    AVCodecContext *enc = s->streams[0]->codec;
 
     if (pb->seekable) {
         CAFContext *caf = s->priv_data;
@@ -167,6 +188,16 @@ static int caf_write_trailer(AVFormatContext *s)
         avio_seek(pb, caf->data, SEEK_SET);
         avio_wb64(pb, file_size - caf->data - 8);
         avio_seek(pb, file_size, SEEK_SET);
+        if (!enc->block_align) {
+            ffio_wfourcc(pb, "pakt");
+            avio_wb64(pb, caf->size_buffer_size + 24);
+            avio_wb64(pb, caf->packets); ///< mNumberPackets
+            avio_wb64(pb, caf->packets * samples_per_packet(enc->codec_id)); ///< mNumberValidFrames
+            avio_wb32(pb, 0); ///< mPrimingFrames
+            avio_wb32(pb, 0); ///< mRemainderFrames
+            avio_write(pb, caf->pkt_sizes, caf->size_buffer_size);
+            av_freep(&caf->pkt_sizes);
+        }
         avio_flush(pb);
     }
     return 0;


More information about the ffmpeg-devel mailing list