noise_bsf.c 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
 *
 * 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.1 of the License, or (at your option) any later version.
 *
 * 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 FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21 22 23
#include <stdlib.h>
#include <string.h>

24
#include "avcodec.h"
25 26 27
#include "bsf.h"

#include "libavutil/log.h"
28
#include "libavutil/mem.h"
29
#include "libavutil/opt.h"
30

31 32 33
typedef struct NoiseContext {
    const AVClass *class;
    int amount;
34
    int dropamount;
35 36
    unsigned int state;
} NoiseContext;
37

38 39 40 41 42 43
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;
44

45
    if (amount <= 0)
46 47
        return AVERROR(EINVAL);

48 49 50 51
    ret = ff_bsf_get_packet(ctx, &in);
    if (ret < 0)
        return ret;

52 53 54 55 56 57
    if (s->dropamount > 0 && s->state % s->dropamount == 0) {
        s->state++;
        av_packet_free(&in);
        return AVERROR(EAGAIN);
    }

58 59 60
    ret = av_new_packet(out, in->size);
    if (ret < 0)
        goto fail;
61

62 63 64
    ret = av_packet_copy_props(out, in);
    if (ret < 0)
        goto fail;
65

66
    memcpy(out->data, in->data, in->size);
67

68 69 70 71
    for (i = 0; i < out->size; i++) {
        s->state += out->data[i] + 1;
        if (s->state % amount == 0)
            out->data[i] = s->state;
72
    }
73 74 75 76 77
fail:
    if (ret < 0)
        av_packet_unref(out);
    av_packet_free(&in);
    return ret;
78 79
}

80 81 82
#define OFFSET(x) offsetof(NoiseContext, x)
static const AVOption options[] = {
    { "amount", NULL, OFFSET(amount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX },
83
    { "dropamount", NULL, OFFSET(dropamount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX },
84 85 86 87 88 89 90 91 92 93 94
    { 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 = {
95
    .name           = "noise",
96
    .priv_data_size = sizeof(NoiseContext),
97
    .priv_class     = &noise_class,
98
    .filter         = noise,
99
};