AMR-NB audio support patch by (<joca at rixmail dot se>)
authorjoca@rixmail.se <joca@rixmail.se>
Wed, 14 May 2003 11:40:16 +0000 (11:40 +0000)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 14 May 2003 11:40:16 +0000 (11:40 +0000)
Originally committed as revision 1876 to svn://svn.ffmpeg.org/ffmpeg/trunk

Makefile
configure
libavcodec/Makefile
libavcodec/allcodecs.c
libavcodec/amr.c [new file with mode: 0644]
libavcodec/avcodec.h
libavformat/mov.c

index 4ded0e4..dc50977 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -43,6 +43,12 @@ EXTRALIBS+=-lmp3lame
 endif
 endif
 
+ifeq ($(AMR_NB),yes)
+EXTRALIBS+= libavcodec/amr/spc.a libavcodec/amr/fipop.a
+AMRLIBS=amrlibs
+CLEANAMR=cleanamr
+endif
+
 ifeq ($(CONFIG_VORBIS),yes)
 EXTRALIBS+=-logg -lvorbis -lvorbisenc
 endif
@@ -73,10 +79,14 @@ FFLIBS = -L./libavformat -lavformat -L./libavcodec -lavcodec
 
 all: lib $(PROG) $(VHOOK)
 
-lib:
+lib: $(AMRLIBS)
        $(MAKE) -C libavcodec all
        $(MAKE) -C libavformat all
 
+#make sure you have added -DMMS_IO to CFLAGS in libavcodec/amr/makefile!!
+amrlibs:
+       $(MAKE) -C libavcodec/amr spclib fipoplib
+
 ffmpeg_g$(EXE): ffmpeg.o .libs
        $(CC) $(LDFLAGS) -o $@ ffmpeg.o $(FFLIBS) $(EXTRALIBS)
 
@@ -128,7 +138,7 @@ endif
        @test -f .libs || touch .libs
        @for i in $(DEP_LIBS) ; do if $(TEST) $$i -nt .libs ; then touch .libs; fi ; done
 
-clean: $(CLEANVHOOK)
+clean: $(CLEANVHOOK) $(CLEANAMR)
        $(MAKE) -C libavcodec clean
        $(MAKE) -C libavformat clean
        $(MAKE) -C tests clean
@@ -141,6 +151,9 @@ distclean: clean
        $(MAKE) -C libavcodec distclean
        rm -f config.mak config.h
 
+cleanamr:
+       $(MAKE) -C libavcodec/amr clean
+
 TAGS:
        etags *.[ch] libavformat/*.[ch] libavcodec/*.[ch]
 
index 3ff80a5..c02d43e 100755 (executable)
--- a/configure
+++ b/configure
@@ -91,6 +91,7 @@ SLIBPREF="lib"
 SLIBSUF=".so"
 risky="yes"
 small="no"
+amr_nb="no"
 
 # OS specific
 targetos=`uname -s`
@@ -319,6 +320,8 @@ for opt do
   ;;
   --enable-small) small="yes"
   ;;
+  --enable-amr_nb) amr_nb="yes"
+  ;;
   esac
 done
 
@@ -607,6 +610,7 @@ echo "  --enable-a52bin          open liba52.so.0 at runtime [default=no]"
 echo "  --disable-pp             disable GPL'ed post processing support [default=no]"
 echo "  --enable-shared-pp       use libpostproc.so [default=no]"
 echo "  --enable-shared          build shared libraries [default=no]"
+echo "  --enable-amr_nb          enable amr_nb audio codec"
 echo ""
 echo "Advanced options (experts only):"
 echo "  --source-path=PATH       path of source code [$source_path]"
@@ -672,6 +676,7 @@ if test "$vhook" = "yes" ; then
 echo "Imlib2 support   $imlib2"
 echo "freetype support $freetype2"
 fi
+echo "AMR-NB support"  $amr_nb
 
 echo "Creating config.mak and config.h"
 
@@ -943,6 +948,17 @@ if test "$source_path_used" = "yes" ; then
 fi
 echo "SRC_PATH=$source_path" >> config.mak
 
+if test "$amr_nb" = "yes" ; then
+  echo "#define AMR_NB 1" >> $TMPH
+  echo "AMR_NB=yes" >> config.mak
+  echo
+  echo "AMR NB NOTICE! Make sure you have downloaded TS26.073 from "
+  echo "http://www.3gpp.org/ftp/Specs/latest/Rel-5/26_series/26073-510.zip"
+  echo "and extracted src to libavcodec/amr"
+  echo "You must also add -DMMS_IO to CFLAGS in libavcodec/amr/makefile."
+  echo
+fi
+
 diff $TMPH config.h >/dev/null 2>&1
 if test $? -ne 0 ; then
        mv -f $TMPH config.h
index 0ef7e17..ae76d00 100644 (file)
@@ -18,6 +18,11 @@ OBJS= common.o utils.o mem.o allcodecs.o \
       ratecontrol.o adpcm.o eval.o dv.o error_resilience.o \
       fft.o mdct.o mace.o huffyuv.o cyuv.o opts.o raw.o h264.o golomb.o \
       vp3.o
+
+ifeq ($(AMR_NB),yes)
+OBJS+= amr.o
+endif
+
 ASM_OBJS=
 
 # codecs which are patented in some non free countries like the us
index 52c1ac7..34fa848 100644 (file)
@@ -108,6 +108,9 @@ void avcodec_register_all(void)
 #endif
 #endif /* CONFIG_DECODERS */
 
+#ifdef AMR_NB
+    register_avcodec(&amr_nb_decoder);
+#endif /* AMR_NB */
     /* pcm codecs */
 
 #define PCM_CODEC(id, name) \
diff --git a/libavcodec/amr.c b/libavcodec/amr.c
new file mode 100644 (file)
index 0000000..ca3df7f
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * AMR Audio decoder stub
+ * Copyright (c) 2003 the ffmpeg project
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+//#define DEBUG
+#define MMS_IO
+#include "avcodec.h"
+
+#include "amr/sp_dec.h"
+#include "amr/d_homing.h"
+#include "amr/typedef.h"
+
+/* frame size in serial bitstream file (frame type + serial stream + flags) */
+#define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
+
+typedef struct AMRDecodeContext {
+    int frameCount;
+    Speech_Decode_FrameState *speech_decoder_state;
+    enum RXFrameType rx_type;
+    enum Mode mode;
+    Word16 reset_flag;
+    Word16 reset_flag_old;
+
+} AMRDecodeContext;
+
+static int amr_nb_decode_init(AVCodecContext * avctx)
+{
+    AMRDecodeContext *s = avctx->priv_data;
+    s->frameCount=0;
+    s->speech_decoder_state=NULL;
+    s->rx_type = (enum RXFrameType)0;
+    s->mode= (enum Mode)0;
+    s->reset_flag=0;
+    s->reset_flag_old=1;
+    
+    if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
+    {
+        printf("error\r\n");
+        return -1;
+    }
+    return 0;
+}
+
+static int amr_decode_close(AVCodecContext * avctx)
+{
+    AMRDecodeContext *s = avctx->priv_data;
+    Speech_Decode_Frame_exit(&s->speech_decoder_state);
+}
+
+static int amr_nb_decode_frame(AVCodecContext * avctx,
+            void *data, int *data_size,
+            uint8_t * buf, int buf_size)
+{
+    AMRDecodeContext *s = avctx->priv_data;
+
+    uint8_t*amrData=buf;
+    int offset=0;
+
+    UWord8 toc, q, ft;
+    
+    Word16 serial[SERIAL_FRAMESIZE];   /* coded bits */
+    Word16 *synth;
+    UWord8 *packed_bits;
+
+    static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
+    int i;
+
+    //printf("amr_decode_frame data=0x%X data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",data,&data_size,buf,buf_size,s->frameCount);
+
+    synth=data;
+
+    while(offset<buf_size)
+    {
+        toc=amrData[offset++];
+        /* read rest of the frame based on ToC byte */
+        q  = (toc >> 2) & 0x01;
+        ft = (toc >> 3) & 0x0F;
+
+        packed_bits=amrData+offset;
+
+        offset+=packed_size[ft];
+
+               //Unsort and unpack bits
+        s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
+
+               //We have a new frame
+        s->frameCount++;
+
+        if (s->rx_type == RX_NO_DATA) 
+        {
+            s->mode = s->speech_decoder_state->prev_mode;
+        }
+        else {
+            s->speech_decoder_state->prev_mode = s->mode;
+        }
+        
+        /* if homed: check if this frame is another homing frame */
+        if (s->reset_flag_old == 1)
+        {
+            /* only check until end of first subframe */
+            s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
+        }
+        /* produce encoder homing frame if homed & input=decoder homing frame */
+        if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
+        {
+            for (i = 0; i < L_FRAME; i++)
+            {
+                synth[i] = EHF_MASK;
+            }
+        }
+        else
+        {     
+            /* decode frame */
+            Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
+        }
+
+               //Each AMR-frame results in 160 16-bit samples
+        *data_size+=160*2;
+        synth+=160;
+        
+        /* if not homed: check whether current frame is a homing frame */
+        if (s->reset_flag_old == 0)
+        {
+            /* check whole frame */
+            s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
+        }
+        /* reset decoder if current frame is a homing frame */
+        if (s->reset_flag != 0)
+        {
+            Speech_Decode_Frame_reset(s->speech_decoder_state);
+        }
+        s->reset_flag_old = s->reset_flag;
+        
+    }
+    return buf_size;
+}
+
+AVCodec amr_nb_decoder =
+{
+    "amr_nb",
+    CODEC_TYPE_AUDIO,
+    CODEC_ID_AMR_NB,
+    sizeof(AMRDecodeContext),
+    amr_nb_decode_init,
+    NULL,
+    amr_decode_close,
+    amr_nb_decode_frame,
+};
+
index 6f0f7df..6351142 100644 (file)
@@ -70,6 +70,9 @@ enum CodecID {
     CODEC_ID_ADPCM_IMA_QT,
     CODEC_ID_ADPCM_IMA_WAV,
     CODEC_ID_ADPCM_MS,
+
+       /* AMR */
+    CODEC_ID_AMR_NB,
 };
 
 enum CodecType {
@@ -1224,6 +1227,7 @@ extern AVCodec cyuv_decoder;
 extern AVCodec h264_decoder;
 extern AVCodec indeo3_decoder;
 extern AVCodec vp3_decoder;
+extern AVCodec amr_nb_decoder;
 extern AVCodec aac_decoder;
 extern AVCodec mpeg4aac_decoder;
 
index 2c274bb..3d36225 100644 (file)
@@ -116,6 +116,7 @@ static const CodecTag mov_audio_tags[] = {
 /* MP4 tags */
     { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG 4 AAC or audio ? */
     /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
+    { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
     { CODEC_ID_NONE, 0 },
 };
 
@@ -710,7 +711,7 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
     entries = get_be32(pb);
 
-    while(entries--) {
+    while(entries--) { //Parsing Sample description table
         enum CodecID id;
        int size = get_be32(pb); /* size */
         format = get_le32(pb); /* data format */
@@ -808,7 +809,7 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
                             get_be32(pb); /* streamType + buffer size */
                            get_be32(pb); /* max bit rate */
                             get_be32(pb); /* avg bit rate */
-                            len = mp4_read_descr(pb, &tag);
+                            len = mov_mp4_read_descr(pb, &tag);
                             if (tag != 0x05)
                                 goto fail;
                             /* MP4DecSpecificDescrTag */
@@ -844,41 +845,95 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
            mov_read_default(c, pb, a);
 #endif
        } else {
-            get_be16(pb); /* version */
-            get_be16(pb); /* revision level */
-            get_be32(pb); /* vendor */
+            st->codec.codec_id = codec_get_id(mov_audio_tags, format);
+           if(st->codec.codec_id==CODEC_ID_AMR_NB) //from TS26.244
+           {
+#ifdef DEBUG
+               printf("AMR-NB audio identified!!\n");
+#endif
+               get_be32(pb);get_be32(pb); //Reserved_8
+               get_be16(pb);//Reserved_2
+               get_be16(pb);//Reserved_2
+               get_be32(pb);//Reserved_4
+               get_be16(pb);//TimeScale
+               get_be16(pb);//Reserved_2
+
+                //AMRSpecificBox.(10 bytes)
+                
+#ifdef DEBUG
+               int damr_size=
+#endif
+               get_be32(pb); //size
+#ifdef DEBUG
+               int damr_type=
+#endif
+               get_be32(pb); //type=='damr'
+#ifdef DEBUG
+               int damr_vendor=
+#endif
+               get_be32(pb); //vendor
+               get_byte(pb); //decoder version
+               get_be16(pb); //mode_set
+               get_byte(pb); //mode_change_period
+               get_byte(pb); //frames_per_sample
 
-            st->codec.channels = get_be16(pb);         /* channel count */
-           st->codec.bits_per_sample = get_be16(pb);   /* sample size */
-
-           st->codec.codec_id = codec_get_id(mov_audio_tags, format);
-            /* handle specific s8 codec */
-            get_be16(pb); /* compression id = 0*/
-            get_be16(pb); /* packet size = 0 */
-
-            st->codec.sample_rate = ((get_be32(pb) >> 16));
-           //printf("CODECID %d  %d  %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
-
-           switch (st->codec.codec_id) {
-           case CODEC_ID_PCM_S16BE:
-               if (st->codec.bits_per_sample == 8)
-                   st->codec.codec_id = CODEC_ID_PCM_S8;
-                /* fall */
-           case CODEC_ID_PCM_U8:
-               st->codec.bit_rate = st->codec.sample_rate * 8;
-               break;
-           default:
-                ;
+#ifdef DEBUG
+               printf("Audio: damr_type=%c%c%c%c damr_size=%Ld damr_vendor=%c%c%c%c\n",
+                       (damr_type >> 24) & 0xff,
+                       (damr_type >> 16) & 0xff,
+                       (damr_type >> 8) & 0xff,
+                       (damr_type >> 0) & 0xff,
+                      damr_size, 
+                       (damr_vendor >> 24) & 0xff,
+                       (damr_vendor >> 16) & 0xff,
+                       (damr_vendor >> 8) & 0xff,
+                       (damr_vendor >> 0) & 0xff
+                      );
+#endif
+                
+               st->time_length=0;//Not possible to get from this info, must count number of AMR frames
+               st->codec.sample_rate=8000;
+               st->codec.channels=1;
+               st->codec.bits_per_sample=16;
+               st->codec.bit_rate=0; /*It is not possible to tell this before we have 
+                                       an audio frame and even then every frame can be different*/
            }
-           get_be32(pb); /* samples per packet */
-           get_be32(pb); /* bytes per packet */
-            get_be32(pb); /* bytes per frame */
-            get_be32(pb); /* bytes per sample */
-
+           else
            {
-               MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
-               mov_read_default(c, pb, a);
-           }
+                get_be16(pb); /* version */
+                get_be16(pb); /* revision level */
+                get_be32(pb); /* vendor */
+
+                st->codec.channels = get_be16(pb);             /* channel count */
+               st->codec.bits_per_sample = get_be16(pb);       /* sample size */
+
+                /* handle specific s8 codec */
+                get_be16(pb); /* compression id = 0*/
+                get_be16(pb); /* packet size = 0 */
+
+                st->codec.sample_rate = ((get_be32(pb) >> 16));
+               //printf("CODECID %d  %d  %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
+
+               switch (st->codec.codec_id) {
+               case CODEC_ID_PCM_S16BE:
+                   if (st->codec.bits_per_sample == 8)
+                       st->codec.codec_id = CODEC_ID_PCM_S8;
+                    /* fall */
+               case CODEC_ID_PCM_U8:
+                   st->codec.bit_rate = st->codec.sample_rate * 8;
+                   break;
+               default:
+                    ;
+               }
+               get_be32(pb); /* samples per packet */
+               get_be32(pb); /* bytes per packet */
+                get_be32(pb); /* bytes per frame */
+                get_be32(pb); /* bytes per sample */
+               {
+                   MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
+                   mov_read_default(c, pb, a);
+               }
+            }
         }
     }
 
@@ -951,20 +1006,32 @@ static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
     AVStream *st = c->fc->streams[c->fc->nb_streams-1];
     //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
     int entries, i;
+    int duration=0;
+    int total_sample_count=0;
 
     print_atom("stts", atom);
 
     get_byte(pb); /* version */
     get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
     entries = get_be32(pb);
+
+
 #ifdef DEBUG
 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
 #endif
     for(i=0; i<entries; i++) {
         int sample_duration;
+        int sample_count;
 
-        get_be32(pb);
+        sample_count=get_be32(pb);
         sample_duration = get_be32(pb);
+#ifdef DEBUG
+        printf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
+#endif
+        duration+=sample_duration*sample_count;
+        total_sample_count+=sample_count;
+
+#if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
 
         if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
             st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
@@ -973,6 +1040,18 @@ printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
             printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
 #endif
         }
+#endif
+    }
+
+    if(st->codec.codec_type==CODEC_TYPE_VIDEO)
+    {
+        //Only support constant frame rate. But lets calculate the average. Needed by .mp4-files created with nec e606 3g phone.
+        //Hmm, lets set base to 1
+        st->codec.frame_rate_base=1;
+        st->codec.frame_rate = st->codec.frame_rate_base * c->streams[c->total_streams]->time_scale * total_sample_count / duration;
+#ifdef DEBUG
+        printf("VIDEO FRAME RATE average= %i (tot sample count= %i ,tot dur= %i)\n", st->codec.frame_rate,total_sample_count,duration);
+#endif
     }
     return 0;
 }