---
 libavcodec/allcodecs.c         |  1 -
 libavcodec/bitstream_filters.c |  4 +++
 libavcodec/noise_bsf.c         | 77 +++++++++++++++++++++++++++++++-----------
 3 files changed, 61 insertions(+), 21 deletions(-)

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 8a545db..12786b7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -538,6 +538,5 @@ void avcodec_register_all(void)
     REGISTER_PARSER(VP8,                vp8);
 
     /* bitstream filters */
-    REGISTER_BSF(NOISE,                 noise);
     REGISTER_BSF(REMOVE_EXTRADATA,      remove_extradata);
 }
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 0b2e1a4..7f9ddf6 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -34,6 +34,7 @@ extern const AVBitStreamFilter ff_mjpeg2jpeg_bsf;
 extern const AVBitStreamFilter ff_mjpega_dump_header_bsf;
 extern const AVBitStreamFilter ff_mov2textsub_bsf;
 extern const AVBitStreamFilter ff_text2movsub_bsf;
+extern const AVBitStreamFilter ff_noise_bsf;
 
 static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_AAC_ADTSTOASC_BSF
@@ -66,6 +67,9 @@ static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_TEXT2MOVSUB_BSF
     &ff_text2movsub_bsf,
 #endif
+#if CONFIG_NOISE_BSF
+    &ff_noise_bsf,
+#endif
     NULL,
 };
 
diff --git a/libavcodec/noise_bsf.c b/libavcodec/noise_bsf.c
index 81840bd..3b41dbf 100644
--- a/libavcodec/noise_bsf.c
+++ b/libavcodec/noise_bsf.c
@@ -22,30 +22,67 @@
 #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;
 
+    ret = av_packet_copy_props(out, in);
+    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;
-
-    *poutbuf= av_malloc(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
-    if (!*poutbuf)
-        return AVERROR(ENOMEM);
-    memcpy(*poutbuf, buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
-    for(i=0; i<buf_size; i++){
-        (*state) += (*poutbuf)[i] + 1;
-        if(*state % amount == 0)
-            (*poutbuf)[i] = *state;
+    memcpy(out->data, in->data, in->size);
+
+    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,
 };
-- 
2.0.0

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to