]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/sonic.c
simd add_hfyu_left_prediction
[ffmpeg] / libavcodec / sonic.c
index 00a81dd166119e16337bd771c85199287d58d7cc..178ae01528859141492de159649c7e6c65ec25ba 100644 (file)
@@ -2,26 +2,28 @@
  * Simple free lossless/lossy audio codec
  * Copyright (c) 2004 Alex Beregszaszi
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg 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
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avcodec.h"
-#include "bitstream.h"
+#include "get_bits.h"
 #include "golomb.h"
 
 /**
- * @file sonic.c
+ * @file libavcodec/sonic.c
  * Simple free lossless/lossy audio codec
  * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk)
  * Written and designed by Alex Beregszaszi
@@ -406,6 +408,7 @@ static int predictor_calc_error(int *k, int *state, int order, int error)
     return x;
 }
 
+#if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
 // Heavily modified Levinson-Durbin algorithm which
 // copes better with quantization, and calculates the
 // actual whitened result as it goes.
@@ -477,11 +480,6 @@ static void modified_levinson_durbin(int *window, int window_entries,
     av_free(state);
 }
 
-static int samplerate_table[] =
-    { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
-
-#ifdef CONFIG_ENCODERS
-
 static inline int code_samplerate(int samplerate)
 {
     switch (samplerate)
@@ -499,7 +497,7 @@ static inline int code_samplerate(int samplerate)
     return -1;
 }
 
-static int sonic_encode_init(AVCodecContext *avctx)
+static av_cold int sonic_encode_init(AVCodecContext *avctx)
 {
     SonicContext *s = avctx->priv_data;
     PutBitContext pb;
@@ -597,14 +595,14 @@ static int sonic_encode_init(AVCodecContext *avctx)
 
     avctx->coded_frame = avcodec_alloc_frame();
     if (!avctx->coded_frame)
-        return -ENOMEM;
+        return AVERROR(ENOMEM);
     avctx->coded_frame->key_frame = 1;
     avctx->frame_size = s->block_align*s->downsampling;
 
     return 0;
 }
 
-static int sonic_encode_close(AVCodecContext *avctx)
+static av_cold int sonic_encode_close(AVCodecContext *avctx)
 {
     SonicContext *s = avctx->priv_data;
     int i;
@@ -744,9 +742,13 @@ static int sonic_encode_frame(AVCodecContext *avctx,
     flush_put_bits(&pb);
     return (put_bits_count(&pb)+7)/8;
 }
-#endif //CONFIG_ENCODERS
+#endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
 
-static int sonic_decode_init(AVCodecContext *avctx)
+#if CONFIG_SONIC_DECODER
+static const int samplerate_table[] =
+    { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
+
+static av_cold int sonic_decode_init(AVCodecContext *avctx)
 {
     SonicContext *s = avctx->priv_data;
     GetBitContext gb;
@@ -823,10 +825,11 @@ static int sonic_decode_init(AVCodecContext *avctx)
     }
     s->int_samples = av_mallocz(4* s->frame_size);
 
+    avctx->sample_fmt = SAMPLE_FMT_S16;
     return 0;
 }
 
-static int sonic_decode_close(AVCodecContext *avctx)
+static av_cold int sonic_decode_close(AVCodecContext *avctx)
 {
     SonicContext *s = avctx->priv_data;
     int i;
@@ -846,8 +849,10 @@ static int sonic_decode_close(AVCodecContext *avctx)
 
 static int sonic_decode_frame(AVCodecContext *avctx,
                             void *data, int *data_size,
-                            uint8_t *buf, int buf_size)
+                            AVPacket *avpkt)
 {
+    const uint8_t *buf = avpkt->data;
+    int buf_size = avpkt->size;
     SonicContext *s = avctx->priv_data;
     GetBitContext gb;
     int i, quant, ch, j;
@@ -921,14 +926,7 @@ static int sonic_decode_frame(AVCodecContext *avctx,
 
     // internal -> short
     for (i = 0; i < s->frame_size; i++)
-    {
-        if (s->int_samples[i] > 32767)
-            samples[i] = 32767;
-        else if (s->int_samples[i] < -32768)
-            samples[i] = -32768;
-        else
-            samples[i] = s->int_samples[i];
-    }
+        samples[i] = av_clip_int16(s->int_samples[i]);
 
     align_get_bits(&gb);
 
@@ -937,7 +935,20 @@ static int sonic_decode_frame(AVCodecContext *avctx,
     return (get_bits_count(&gb)+7)/8;
 }
 
-#ifdef CONFIG_ENCODERS
+AVCodec sonic_decoder = {
+    "sonic",
+    CODEC_TYPE_AUDIO,
+    CODEC_ID_SONIC,
+    sizeof(SonicContext),
+    sonic_decode_init,
+    NULL,
+    sonic_decode_close,
+    sonic_decode_frame,
+    .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
+};
+#endif /* CONFIG_SONIC_DECODER */
+
+#if CONFIG_SONIC_ENCODER
 AVCodec sonic_encoder = {
     "sonic",
     CODEC_TYPE_AUDIO,
@@ -947,8 +958,11 @@ AVCodec sonic_encoder = {
     sonic_encode_frame,
     sonic_encode_close,
     NULL,
+    .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
 };
+#endif
 
+#if CONFIG_SONIC_LS_ENCODER
 AVCodec sonic_ls_encoder = {
     "sonicls",
     CODEC_TYPE_AUDIO,
@@ -958,18 +972,6 @@ AVCodec sonic_ls_encoder = {
     sonic_encode_frame,
     sonic_encode_close,
     NULL,
-};
-#endif
-
-#ifdef CONFIG_DECODERS
-AVCodec sonic_decoder = {
-    "sonic",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_SONIC,
-    sizeof(SonicContext),
-    sonic_decode_init,
-    NULL,
-    sonic_decode_close,
-    sonic_decode_frame,
+    .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"),
 };
 #endif