]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/roqaudioenc.c
suggest to use av_get_bytes_per_sample() in av_get_bits_per_sample_format() doxy
[ffmpeg] / libavcodec / roqaudioenc.c
index f76079e1d1e242b1337da1cfc1dba56a938ae269..46aefb4bef8839893972f52847fab528bd954f22 100644 (file)
@@ -4,23 +4,24 @@
  * Copyright (c) 2005 Eric Lasota
  *    Based on RoQ specs (c)2001 Tim Ferguson
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav 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 FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/intmath.h"
 #include "avcodec.h"
 #include "bytestream.h"
 
 
 
 #define MAX_DPCM (127*127)
-static unsigned char dpcmValues[MAX_DPCM];
 
 
 typedef struct
 {
     short lastSample[2];
-} ROQDPCMContext_t;
+} ROQDPCMContext;
 
-static void roq_dpcm_table_init(void)
+static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
 {
-    int i;
-
-    /* Create a table of quick DPCM values */
-    for (i=0; i<MAX_DPCM; i++) {
-        int s= ff_sqrt(i);
-        int mid= s*s + s;
-        dpcmValues[i]= s + (i>mid);
-    }
-}
-
-static int roq_dpcm_encode_init(AVCodecContext *avctx)
-{
-    ROQDPCMContext_t *context = avctx->priv_data;
+    ROQDPCMContext *context = avctx->priv_data;
 
     if (avctx->channels > 2) {
         av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
@@ -61,13 +49,11 @@ static int roq_dpcm_encode_init(AVCodecContext *avctx)
         av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
         return -1;
     }
-    if (avctx->sample_fmt != SAMPLE_FMT_S16) {
+    if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
         av_log(avctx, AV_LOG_ERROR, "Audio must be signed 16-bit\n");
         return -1;
     }
 
-    roq_dpcm_table_init();
-
     avctx->frame_size = ROQ_FIRST_FRAME_SIZE;
 
     context->lastSample[0] = context->lastSample[1] = 0;
@@ -92,8 +78,10 @@ static unsigned char dpcm_predict(short *previous, short current)
 
     if (diff >= MAX_DPCM)
         result = 127;
-    else
-        result = dpcmValues[diff];
+    else {
+        result = ff_sqrt(diff);
+        result += diff > result*result+result;
+    }
 
     /* See if this overflows */
  retry:
@@ -120,10 +108,10 @@ static int roq_dpcm_encode_frame(AVCodecContext *avctx,
                 unsigned char *frame, int buf_size, void *data)
 {
     int i, samples, stereo, ch;
-    short *in;
+    const short *in;
     unsigned char *out;
 
-    ROQDPCMContext_t *context = avctx->priv_data;
+    ROQDPCMContext *context = avctx->priv_data;
 
     stereo = (avctx->channels == 2);
 
@@ -158,20 +146,22 @@ static int roq_dpcm_encode_frame(AVCodecContext *avctx,
     return out - frame;
 }
 
-static int roq_dpcm_encode_close(AVCodecContext *avctx)
+static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
 {
     av_freep(&avctx->coded_frame);
 
     return 0;
 }
 
-AVCodec roq_dpcm_encoder = {
+AVCodec ff_roq_dpcm_encoder = {
     "roq_dpcm",
-    CODEC_TYPE_AUDIO,
+    AVMEDIA_TYPE_AUDIO,
     CODEC_ID_ROQ_DPCM,
-    sizeof(ROQDPCMContext_t),
+    sizeof(ROQDPCMContext),
     roq_dpcm_encode_init,
     roq_dpcm_encode_frame,
     roq_dpcm_encode_close,
     NULL,
+    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
+    .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
 };