]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/pcm.c
split intra / inter dequantization
[ffmpeg] / libavcodec / pcm.c
index bff8aaf4c47eb7e2e405278dee71389f403a6323..a6c0d343b066dbf6ff2a3e44309d81126f41b52b 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+/**
+ * @file pcm.c
+ * PCM codecs
+ */
 #include "avcodec.h"
 
 /* from g711.c by SUN microsystems (unrestricted use) */
@@ -39,19 +45,11 @@ static int alaw2linear(unsigned char        a_val)
 
        a_val ^= 0x55;
 
-       t = (a_val & QUANT_MASK) << 4;
+       t = a_val & QUANT_MASK;
        seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
-       switch (seg) {
-       case 0:
-               t += 8;
-               break;
-       case 1:
-               t += 0x108;
-               break;
-       default:
-               t += 0x108;
-               t <<= seg - 1;
-       }
+       if(seg) t= (t + t + 1 + 32) << (seg + 2);
+       else    t= (t + t + 1     ) << 3;
+
        return ((a_val & SIGN_BIT) ? t : -t);
 }
 
@@ -73,13 +71,13 @@ static int ulaw2linear(unsigned char        u_val)
 }
 
 /* 16384 entries per table */
-static UINT8 *linear_to_alaw = NULL;
+static uint8_t *linear_to_alaw = NULL;
 static int linear_to_alaw_ref = 0;
 
-static UINT8 *linear_to_ulaw = NULL;
+static uint8_t *linear_to_ulaw = NULL;
 static int linear_to_ulaw_ref = 0;
 
-static void build_xlaw_table(UINT8 *linear_to_xlaw, 
+static void build_xlaw_table(uint8_t *linear_to_xlaw, 
                              int (*xlaw2linear)(unsigned char),
                              int mask) 
 {
@@ -103,7 +101,7 @@ static void build_xlaw_table(UINT8 *linear_to_xlaw,
     linear_to_xlaw[0] = linear_to_xlaw[1];
 }
 
-static int encode_init(AVCodecContext *avctx)
+static int pcm_encode_init(AVCodecContext *avctx)
 {
     avctx->frame_size = 1;
     switch(avctx->codec->id) {
@@ -128,11 +126,17 @@ static int encode_init(AVCodecContext *avctx)
     default:
         break;
     }
+    
+    avctx->coded_frame= avcodec_alloc_frame();
+    avctx->coded_frame->key_frame= 1;
+    
     return 0;
 }
 
-static int encode_close(AVCodecContext *avctx)
+static int pcm_encode_close(AVCodecContext *avctx)
 {
+    av_freep(&avctx->coded_frame);
+
     switch(avctx->codec->id) {
     case CODEC_ID_PCM_ALAW:
         if (--linear_to_alaw_ref == 0)
@@ -149,8 +153,8 @@ static int encode_close(AVCodecContext *avctx)
     return 0;
 }
 
-static int encode_frame(AVCodecContext *avctx,
-                        unsigned char *frame, int buf_size, void *data)
+static int pcm_encode_frame(AVCodecContext *avctx,
+                           unsigned char *frame, int buf_size, void *data)
 {
     int n, sample_size, v;
     short *samples;
@@ -209,14 +213,14 @@ static int encode_frame(AVCodecContext *avctx,
     case CODEC_ID_PCM_S8:
         for(;n>0;n--) {
             v = *samples++;
-            dst[0] = (v + 128) >> 8;
+            dst[0] = v >> 8;
             dst++;
         }
         break;
     case CODEC_ID_PCM_U8:
         for(;n>0;n--) {
             v = *samples++;
-            dst[0] = ((v + 128) >> 8) + 128;
+            dst[0] = (v >> 8) + 128;
             dst++;
         }
         break;
@@ -237,7 +241,6 @@ static int encode_frame(AVCodecContext *avctx,
     default:
         return -1;
     }
-    avctx->key_frame = 1;
     //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
 
     return dst - frame;
@@ -247,7 +250,7 @@ typedef struct PCMDecode {
     short table[256];
 } PCMDecode;
 
-static int decode_init(AVCodecContext * avctx)
+static int pcm_decode_init(AVCodecContext * avctx)
 {
     PCMDecode *s = avctx->priv_data;
     int i;
@@ -267,14 +270,14 @@ static int decode_init(AVCodecContext * avctx)
     return 0;
 }
 
-static int decode_frame(AVCodecContext *avctx, 
-                        void *data, int *data_size,
-                        UINT8 *buf, int buf_size)
+static int pcm_decode_frame(AVCodecContext *avctx,
+                           void *data, int *data_size,
+                           uint8_t *buf, int buf_size)
 {
     PCMDecode *s = avctx->priv_data;
     int n;
     short *samples;
-    UINT8 *src;
+    uint8_t *src;
 
     samples = data;
     src = buf;
@@ -334,7 +337,7 @@ static int decode_frame(AVCodecContext *avctx,
         *data_size = 0;
         return -1;
     }
-    *data_size = (UINT8 *)samples - (UINT8 *)data;
+    *data_size = (uint8_t *)samples - (uint8_t *)data;
     return src - buf;
 }
 
@@ -344,9 +347,9 @@ AVCodec name ## _encoder = {                    \
     CODEC_TYPE_AUDIO,                           \
     id,                                         \
     0,                                          \
-    encode_init,                                \
-    encode_frame,                               \
-    encode_close,                               \
+    pcm_encode_init,                           \
+    pcm_encode_frame,                          \
+    pcm_encode_close,                          \
     NULL,                                       \
 };                                              \
 AVCodec name ## _decoder = {                    \
@@ -354,11 +357,11 @@ AVCodec name ## _decoder = {                    \
     CODEC_TYPE_AUDIO,                           \
     id,                                         \
     sizeof(PCMDecode),                          \
-    decode_init,                                \
+    pcm_decode_init,                           \
     NULL,                                       \
     NULL,                                       \
-    decode_frame,                               \
-};
+    pcm_decode_frame,                           \
+}
 
 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
@@ -368,3 +371,5 @@ PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
+
+#undef PCM_CODEC