]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ac3dec.c
fixing ac prediction encoding with adaptive quantization
[ffmpeg] / libavcodec / ac3dec.c
index bdf5b31586bd0798a6f14e176d083bcb23b98176..6352f55c7a5516a2f69ee89cbb9225709adeb5ef 100644 (file)
@@ -1,20 +1,20 @@
 /*
  * AC3 decoder
- * Copyright (c) 2001 Gerard Lantau.
+ * Copyright (c) 2001 Fabrice Bellard.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU 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 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 program is distributed in the hope that it will be useful,
+ * 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 General Public License for more details.
+ * 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 General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * 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
  */
 #include "avcodec.h"
 #include "libac3/ac3.h"
@@ -26,6 +26,7 @@ typedef struct AC3DecodeState {
     UINT8 *inbuf_ptr;
     int frame_size;
     int flags;
+    int channels;
     ac3_state_t state;
 } AC3DecodeState;
 
@@ -52,24 +53,16 @@ static inline int blah (int32_t i)
        return i - 0x43c00000;
 }
 
-static inline void float_to_int (float * _f, INT16 * s16
+static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
 {
-    int i;
+    int i, j, c;
     int32_t * f = (int32_t *) _f;      // XXX assumes IEEE float format
 
+    j = 0;
+    nchannels *= 256;
     for (i = 0; i < 256; i++) {
-       s16[2*i] = blah (f[i]);
-       s16[2*i+1] = blah (f[i+256]);
-    }
-}
-
-static inline void float_to_int_mono (float * _f, INT16 * s16) 
-{
-    int i;
-    int32_t * f = (int32_t *) _f;      // XXX assumes IEEE float format
-
-    for (i = 0; i < 256; i++) {
-       s16[i] = blah (f[i]);
+       for (c = 0; c < nchannels; c += 256)
+           s16[j++] = blah (f[i + c]);
     }
 }
 
@@ -87,6 +80,9 @@ static int ac3_decode_frame(AVCodecContext *avctx,
     int sample_rate, bit_rate;
     short *out_samples = data;
     float level;
+    static const int ac3_channels[8] = {
+       2, 1, 2, 3, 3, 4, 4, 5
+    };
 
     *data_size = 0;
     buf_ptr = buf;
@@ -108,32 +104,39 @@ static int ac3_decode_frame(AVCodecContext *avctx,
                     memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
                     s->inbuf_ptr--;
                 } else {
-                    s->frame_size = len;
+                   s->frame_size = len;
                     /* update codec info */
                     avctx->sample_rate = sample_rate;
-                    if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
-                        avctx->channels = 1;
-                    else
-                        avctx->channels = 2;
-                    avctx->bit_rate = bit_rate;
+                    s->channels = ac3_channels[s->flags & 7];
+                    if (s->flags & AC3_LFE)
+                       s->channels++;
+                   if (avctx->channels == 0)
+                       /* No specific number of channel requested */
+                       avctx->channels = s->channels;
+                   else if (s->channels < avctx->channels) {
+                       fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
+                       avctx->channels = s->channels;
+                   }
+                   avctx->bit_rate = bit_rate;
                 }
             }
         } else if (len < s->frame_size) {
             len = s->frame_size - len;
             if (len > buf_size)
                 len = buf_size;
-            
+
             memcpy(s->inbuf_ptr, buf_ptr, len);
             buf_ptr += len;
             s->inbuf_ptr += len;
             buf_size -= len;
         } else {
+            flags = s->flags;
             if (avctx->channels == 1)
                 flags = AC3_MONO;
-            else
+            else if (avctx->channels == 2)
                 flags = AC3_STEREO;
-
-            flags |= AC3_ADJUST_LEVEL;
+            else
+                flags |= AC3_ADJUST_LEVEL;
             level = 1;
             if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
             fail:
@@ -144,10 +147,7 @@ static int ac3_decode_frame(AVCodecContext *avctx,
             for (i = 0; i < 6; i++) {
                 if (ac3_block (&s->state))
                     goto fail;
-                if (avctx->channels == 1)
-                    float_to_int_mono (*samples, out_samples + i * 256);
-                else
-                    float_to_int (*samples, out_samples + i * 512);
+                float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
             }
             s->inbuf_ptr = s->inbuf;
             s->frame_size = 0;