[FFmpeg-devel] [PATCH] Handle AAC in RM similar to other audio codecs

Kostya kostya.shishkov
Tue Feb 3 08:57:02 CET 2009


With this patch RM demuxer handles AAC in the same way as other audio codecs -
it loads it into temporary buffer and passes chunks from it as requested.
(Demuxer still should be cleaned, refactored and maybe rewritten from scratch)

This patch also fixes -an behaviour with AAC sound.
-------------- next part --------------
Index: libavformat/rmdec.c
===================================================================
--- libavformat/rmdec.c	(revision 16915)
+++ libavformat/rmdec.c	(working copy)
@@ -37,6 +37,7 @@
     int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
     int audio_framesize; /// Audio frame size from container
     int sub_packet_lengths[16]; /// Length of each subpacket
+    int audio_offset;
 };
 
 typedef struct {
@@ -629,16 +630,21 @@
                 *flags = 2; // Mark first packet as keyframe
             }
         } else if (st->codec->codec_id == CODEC_ID_AAC) {
-            int x;
+            int x, asize = 0;
             rm->audio_stream_num = st->index;
             ast->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
             if (ast->sub_packet_cnt) {
-                for (x = 0; x < ast->sub_packet_cnt; x++)
+                for (x = 0; x < ast->sub_packet_cnt; x++) {
                     ast->sub_packet_lengths[x] = get_be16(pb);
+                    asize += ast->sub_packet_lengths[x];
+                }
+                av_new_packet(&ast->pkt, asize - ast->sub_packet_lengths[0]);
                 // Release first audio packet
                 rm->audio_pkt_cnt = ast->sub_packet_cnt - 1;
                 av_get_packet(pb, pkt, ast->sub_packet_lengths[0]);
                 *flags = 2; // Mark first packet as keyframe
+                get_buffer(pb, ast->pkt.data, asize - ast->sub_packet_lengths[0]);
+                ast->audio_offset = 0;
             }
         } else {
             av_get_packet(pb, pkt, len);
@@ -683,9 +689,13 @@
 
     assert (rm->audio_pkt_cnt > 0);
 
-    if (st->codec->codec_id == CODEC_ID_AAC)
-        av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
-    else {
+    if (st->codec->codec_id == CODEC_ID_AAC) {
+        av_new_packet(pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
+        memcpy(pkt->data, ast->pkt.data + ast->audio_offset, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
+        ast->audio_offset += ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt];
+        if (rm->audio_pkt_cnt == 1) //last subpacket is used, free temporary packet
+            av_free_packet(&ast->pkt);
+    } else {
         av_new_packet(pkt, st->codec->block_align);
         memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
                (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),



More information about the ffmpeg-devel mailing list