]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/noise_bsf.c
ratecontrol: Drop commented out cruft
[ffmpeg] / libavcodec / noise_bsf.c
index 491fbccc1d1b1dde3bf716f46b243ff3b6b1ef8b..3b41dbffc85ab2cc2a965510439675534904eb70 100644 (file)
@@ -1,46 +1,88 @@
 /*
  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  *
- * 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 <stdlib.h>
+#include <string.h>
+
 #include "avcodec.h"
+#include "bsf.h"
+
+#include "libavutil/log.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+
+typedef struct NoiseContext {
+    const AVClass *class;
+    int amount;
+    unsigned int state;
+} NoiseContext;
+
+static int noise(AVBSFContext *ctx, AVPacket *out)
+{
+    NoiseContext *s = ctx->priv_data;
+    AVPacket *in;
+    int amount = s->amount > 0 ? s->amount : (s->state % 10001 + 1);
+    int i, ret = 0;
+
+    ret = ff_bsf_get_packet(ctx, &in);
+    if (ret < 0)
+        return ret;
 
+    ret = av_new_packet(out, in->size);
+    if (ret < 0)
+        goto fail;
 
-static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
-                     uint8_t **poutbuf, int *poutbuf_size,
-                     const uint8_t *buf, int buf_size, int keyframe){
-    unsigned int *state= bsfc->priv_data;
-    int amount= args ? atoi(args) : (*state % 10001+1);
-    int i;
+    ret = av_packet_copy_props(out, in);
+    if (ret < 0)
+        goto fail;
 
-    *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
+    memcpy(out->data, in->data, in->size);
 
-    memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    for(i=0; i<buf_size; i++){
-        (*state) += (*poutbuf)[i] + 1;
-        if(*state % amount == 0)
-            (*poutbuf)[i] = *state;
+    for (i = 0; i < out->size; i++) {
+        s->state += out->data[i] + 1;
+        if (s->state % amount == 0)
+            out->data[i] = s->state;
     }
-    return 1;
+fail:
+    if (ret < 0)
+        av_packet_unref(out);
+    av_packet_free(&in);
+    return ret;
 }
 
-AVBitStreamFilter ff_noise_bsf={
-    "noise",
-    sizeof(int),
-    noise,
+#define OFFSET(x) offsetof(NoiseContext, x)
+static const AVOption options[] = {
+    { "amount", NULL, OFFSET(amount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX },
+    { NULL },
+};
+
+static const AVClass noise_class = {
+    .class_name = "noise",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+const AVBitStreamFilter ff_noise_bsf = {
+    .name           = "noise",
+    .priv_data_size = sizeof(int),
+    .priv_class     = &noise_class,
+    .filter         = noise,
 };