]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/alac.c
Flip (M)JPEG frames encoded by Intel JPEG library.
[ffmpeg] / libavcodec / alac.c
index 3ea7952ba075c3e2f27752fd8ac1579fd1e9e166..5c48a4b28f4a105a8a34dcc602e8bddf788ef90a 100644 (file)
@@ -20,7 +20,7 @@
  */
 
 /**
- * @file alac.c
+ * @file libavcodec/alac.c
  * ALAC (Apple Lossless Audio Codec) decoder
  * @author 2005 David Hammerton
  *
 
 
 #include "avcodec.h"
-#include "bitstream.h"
+#include "get_bits.h"
 #include "bytestream.h"
 #include "unary.h"
+#include "mathops.h"
 
 #define ALAC_EXTRADATA_SIZE 36
 #define MAX_CHANNELS 2
@@ -229,11 +230,6 @@ static void bastardized_rice_decompress(ALACContext *alac,
     }
 }
 
-static inline int32_t extend_sign32(int32_t val, int bits)
-{
-    return (val << (32 - bits)) >> (32 - bits);
-}
-
 static inline int sign_only(int v)
 {
     return v ? FFSIGN(v) : 0;
@@ -273,7 +269,7 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer,
             prev_value = buffer_out[i];
             error_value = error_buffer[i+1];
             buffer_out[i+1] =
-                extend_sign32((prev_value + error_value), readsamplesize);
+                sign_extend((prev_value + error_value), readsamplesize);
         }
         return;
     }
@@ -284,7 +280,7 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer,
             int32_t val;
 
             val = buffer_out[i] + error_buffer[i+1];
-            val = extend_sign32(val, readsamplesize);
+            val = sign_extend(val, readsamplesize);
             buffer_out[i+1] = val;
         }
 
@@ -319,7 +315,7 @@ static void predictor_decompress_fir_adapt(int32_t *error_buffer,
             outval = (1 << (predictor_quantitization-1)) + sum;
             outval = outval >> predictor_quantitization;
             outval = outval + buffer_out[0] + error_val;
-            outval = extend_sign32(outval, readsamplesize);
+            outval = sign_extend(outval, readsamplesize);
 
             buffer_out[predictor_coef_num+1] = outval;
 
@@ -404,14 +400,16 @@ static void reconstruct_stereo_16(int32_t *buffer[MAX_CHANNELS],
 
 static int alac_decode_frame(AVCodecContext *avctx,
                              void *outbuffer, int *outputsize,
-                             const uint8_t *inbuffer, int input_buffer_size)
+                             AVPacket *avpkt)
 {
+    const uint8_t *inbuffer = avpkt->data;
+    int input_buffer_size = avpkt->size;
     ALACContext *alac = avctx->priv_data;
 
     int channels;
     unsigned int outputsamples;
     int hassize;
-    int readsamplesize;
+    unsigned int readsamplesize;
     int wasted_bytes;
     int isnotcompressed;
     uint8_t interlacing_shift;
@@ -476,6 +474,10 @@ static int alac_decode_frame(AVCodecContext *avctx,
 
     *outputsize = outputsamples * alac->bytespersample;
     readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + channels - 1;
+    if (readsamplesize > MIN_CACHE_BITS) {
+        av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
+        return -1;
+    }
 
     if (!isnotcompressed) {
         /* so it is compressed */
@@ -535,16 +537,15 @@ static int alac_decode_frame(AVCodecContext *avctx,
         }
     } else {
         /* not compressed, easy case */
-            int i, chan;
-            for (chan = 0; chan < channels; chan++)
-                for (i = 0; i < outputsamples; i++) {
-                    int32_t audiobits;
+        int i, chan;
+        for (i = 0; i < outputsamples; i++)
+            for (chan = 0; chan < channels; chan++) {
+                int32_t audiobits;
 
-                    audiobits = get_bits_long(&alac->gb, alac->setinfo_sample_size);
-                    audiobits = extend_sign32(audiobits, alac->setinfo_sample_size);
+                audiobits = get_sbits_long(&alac->gb, alac->setinfo_sample_size);
 
-                    alac->outputsamples_buffer[chan][i] = audiobits;
-                }
+                alac->outputsamples_buffer[chan][i] = audiobits;
+            }
         /* wasted_bytes = 0; */
         interlacing_shift = 0;
         interlacing_leftweight = 0;
@@ -593,7 +594,8 @@ static av_cold int alac_decode_init(AVCodecContext * avctx)
     alac->context_initialized = 0;
 
     alac->numchannels = alac->avctx->channels;
-    alac->bytespersample = (avctx->bits_per_sample / 8) * alac->numchannels;
+    alac->bytespersample = 2 * alac->numchannels;
+    avctx->sample_fmt = SAMPLE_FMT_S16;
 
     return 0;
 }
@@ -620,5 +622,5 @@ AVCodec alac_decoder = {
     NULL,
     alac_decode_close,
     alac_decode_frame,
-    .long_name = "ALAC (Apple Lossless Audio Codec)",
+    .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
 };