Apply 'cold' attribute to init/uninit functions in libavcodec
[ffmpeg.git] / libavcodec / pcm.c
index 2551d12..661dfc6 100644 (file)
@@ -28,6 +28,8 @@
 #include "bitstream.h" // for ff_reverse
 #include "bytestream.h"
 
+#define MAX_CHANNELS 64
+
 /* from g711.c by SUN microsystems (unrestricted use) */
 
 #define         SIGN_BIT        (0x80)      /* Sign bit for a A-law byte. */
@@ -42,7 +44,7 @@
  * alaw2linear() - Convert an A-law value to 16-bit linear PCM
  *
  */
-static int alaw2linear(unsigned char a_val)
+static av_cold int alaw2linear(unsigned char a_val)
 {
         int t;
         int seg;
@@ -57,7 +59,7 @@ static int alaw2linear(unsigned char a_val)
         return ((a_val & SIGN_BIT) ? t : -t);
 }
 
-static int ulaw2linear(unsigned char u_val)
+static av_cold int ulaw2linear(unsigned char u_val)
 {
         int t;
 
@@ -78,7 +80,7 @@ static int ulaw2linear(unsigned char u_val)
 static uint8_t linear_to_alaw[16384];
 static uint8_t linear_to_ulaw[16384];
 
-static void build_xlaw_table(uint8_t *linear_to_xlaw,
+static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
                              int (*xlaw2linear)(unsigned char),
                              int mask)
 {
@@ -102,7 +104,7 @@ static void build_xlaw_table(uint8_t *linear_to_xlaw,
     linear_to_xlaw[0] = linear_to_xlaw[1];
 }
 
-static int pcm_encode_init(AVCodecContext *avctx)
+static av_cold int pcm_encode_init(AVCodecContext *avctx)
 {
     avctx->frame_size = 1;
     switch(avctx->codec->id) {
@@ -152,7 +154,7 @@ static int pcm_encode_init(AVCodecContext *avctx)
     return 0;
 }
 
-static int pcm_encode_close(AVCodecContext *avctx)
+static av_cold int pcm_encode_close(AVCodecContext *avctx)
 {
     av_freep(&avctx->coded_frame);
 
@@ -291,6 +293,14 @@ static int pcm_encode_frame(AVCodecContext *avctx,
             *dst++ = (v >> 8) + 128;
         }
         break;
+    case CODEC_ID_PCM_ZORK:
+        for(;n>0;n--) {
+            v= *samples++ >> 8;
+            if(v<0)   v = -v;
+            else      v+= 128;
+            *dst++ = v;
+        }
+        break;
     case CODEC_ID_PCM_ALAW:
         for(;n>0;n--) {
             v = *samples++;
@@ -315,7 +325,7 @@ typedef struct PCMDecode {
     short table[256];
 } PCMDecode;
 
-static int pcm_decode_init(AVCodecContext * avctx)
+static av_cold int pcm_decode_init(AVCodecContext * avctx)
 {
     PCMDecode *s = avctx->priv_data;
     int i;
@@ -345,7 +355,7 @@ static int pcm_decode_init(AVCodecContext * avctx)
  * \param src_len number of bytes in src
  */
 static inline void decode_to16(int bps, int le, int us,
-                               uint8_t **src, short **samples, int src_len)
+                               const uint8_t **src, short **samples, int src_len)
 {
     int usum = us ? -0x8000 : 0;
     register int n = src_len / bps;
@@ -363,12 +373,12 @@ static inline void decode_to16(int bps, int le, int us,
 
 static int pcm_decode_frame(AVCodecContext *avctx,
                             void *data, int *data_size,
-                            uint8_t *buf, int buf_size)
+                            const uint8_t *buf, int buf_size)
 {
     PCMDecode *s = avctx->priv_data;
-    int n;
+    int c, n;
     short *samples;
-    uint8_t *src;
+    const uint8_t *src, *src2[MAX_CHANNELS];
 
     samples = data;
     src = buf;
@@ -378,10 +388,18 @@ static int pcm_decode_frame(AVCodecContext *avctx,
         av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
         return -1;
     }
+    if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
+        av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
+        return -1;
+    }
 
     buf_size= FFMIN(buf_size, *data_size/2);
     *data_size=0;
 
+    n = buf_size/avctx->channels;
+    for(c=0;c<avctx->channels;c++)
+        src2[c] = &src[c*n];
+
     switch(avctx->codec->id) {
     case CODEC_ID_PCM_S32LE:
         decode_to16(4, 1, 0, &src, &samples, buf_size);
@@ -422,6 +440,12 @@ static int pcm_decode_frame(AVCodecContext *avctx,
             *samples++ = bytestream_get_le16(&src);
         }
         break;
+    case CODEC_ID_PCM_S16LE_PLANAR:
+        for(n>>=1;n>0;n--)
+            for(c=0;c<avctx->channels;c++)
+                *samples++ = bytestream_get_le16(&src2[c]);
+        src = src2[avctx->channels-1];
+        break;
     case CODEC_ID_PCM_S16BE:
         n = buf_size >> 1;
         for(;n>0;n--) {
@@ -452,6 +476,15 @@ static int pcm_decode_frame(AVCodecContext *avctx,
             *samples++ = ((int)*src++ - 128) << 8;
         }
         break;
+    case CODEC_ID_PCM_ZORK:
+        n = buf_size;
+        for(;n>0;n--) {
+            int x= *src++;
+            if(x&128) x-= 128;
+            else      x = -x;
+            *samples++ = x << 8;
+        }
+        break;
     case CODEC_ID_PCM_ALAW:
     case CODEC_ID_PCM_MULAW:
         n = buf_size;
@@ -466,7 +499,8 @@ static int pcm_decode_frame(AVCodecContext *avctx,
     return src - buf;
 }
 
-#define PCM_CODEC(id, name)                     \
+#ifdef CONFIG_ENCODERS
+#define PCM_ENCODER(id,name)                    \
 AVCodec name ## _encoder = {                    \
     #name,                                      \
     CODEC_TYPE_AUDIO,                           \
@@ -476,7 +510,13 @@ AVCodec name ## _encoder = {                    \
     pcm_encode_frame,                           \
     pcm_encode_close,                           \
     NULL,                                       \
-};                                              \
+};
+#else
+#define PCM_ENCODER(id,name)
+#endif
+
+#ifdef CONFIG_DECODERS
+#define PCM_DECODER(id,name)                    \
 AVCodec name ## _decoder = {                    \
     #name,                                      \
     CODEC_TYPE_AUDIO,                           \
@@ -486,24 +526,30 @@ AVCodec name ## _decoder = {                    \
     NULL,                                       \
     NULL,                                       \
     pcm_decode_frame,                           \
-}
+};
+#else
+#define PCM_DECODER(id,name)
+#endif
 
-PCM_CODEC(CODEC_ID_PCM_S32LE, pcm_s32le);
-PCM_CODEC(CODEC_ID_PCM_S32BE, pcm_s32be);
-PCM_CODEC(CODEC_ID_PCM_U32LE, pcm_u32le);
-PCM_CODEC(CODEC_ID_PCM_U32BE, pcm_u32be);
-PCM_CODEC(CODEC_ID_PCM_S24LE, pcm_s24le);
-PCM_CODEC(CODEC_ID_PCM_S24BE, pcm_s24be);
-PCM_CODEC(CODEC_ID_PCM_U24LE, pcm_u24le);
-PCM_CODEC(CODEC_ID_PCM_U24BE, pcm_u24be);
-PCM_CODEC(CODEC_ID_PCM_S24DAUD, pcm_s24daud);
-PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
-PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
-PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
-PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
-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
+#define PCM_CODEC(id, name)                     \
+PCM_ENCODER(id,name) PCM_DECODER(id,name)
+
+PCM_CODEC  (CODEC_ID_PCM_S32LE, pcm_s32le);
+PCM_CODEC  (CODEC_ID_PCM_S32BE, pcm_s32be);
+PCM_CODEC  (CODEC_ID_PCM_U32LE, pcm_u32le);
+PCM_CODEC  (CODEC_ID_PCM_U32BE, pcm_u32be);
+PCM_CODEC  (CODEC_ID_PCM_S24LE, pcm_s24le);
+PCM_CODEC  (CODEC_ID_PCM_S24BE, pcm_s24be);
+PCM_CODEC  (CODEC_ID_PCM_U24LE, pcm_u24le);
+PCM_CODEC  (CODEC_ID_PCM_U24BE, pcm_u24be);
+PCM_CODEC  (CODEC_ID_PCM_S24DAUD, pcm_s24daud);
+PCM_CODEC  (CODEC_ID_PCM_S16LE, pcm_s16le);
+PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, pcm_s16le_planar);
+PCM_CODEC  (CODEC_ID_PCM_S16BE, pcm_s16be);
+PCM_CODEC  (CODEC_ID_PCM_U16LE, pcm_u16le);
+PCM_CODEC  (CODEC_ID_PCM_U16BE, pcm_u16be);
+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);
+PCM_CODEC  (CODEC_ID_PCM_ZORK, pcm_zork);