You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.4KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "avcodec.h"
  23. #include "bsf.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/opt.h"
  27. typedef struct NoiseContext {
  28. const AVClass *class;
  29. int amount;
  30. unsigned int state;
  31. } NoiseContext;
  32. static int noise(AVBSFContext *ctx, AVPacket *out)
  33. {
  34. NoiseContext *s = ctx->priv_data;
  35. AVPacket *in;
  36. int amount = s->amount > 0 ? s->amount : (s->state % 10001 + 1);
  37. int i, ret = 0;
  38. if (amount <= 0)
  39. return AVERROR(EINVAL);
  40. ret = ff_bsf_get_packet(ctx, &in);
  41. if (ret < 0)
  42. return ret;
  43. ret = av_new_packet(out, in->size);
  44. if (ret < 0)
  45. goto fail;
  46. ret = av_packet_copy_props(out, in);
  47. if (ret < 0)
  48. goto fail;
  49. memcpy(out->data, in->data, in->size);
  50. for (i = 0; i < out->size; i++) {
  51. s->state += out->data[i] + 1;
  52. if (s->state % amount == 0)
  53. out->data[i] = s->state;
  54. }
  55. fail:
  56. if (ret < 0)
  57. av_packet_unref(out);
  58. av_packet_free(&in);
  59. return ret;
  60. }
  61. #define OFFSET(x) offsetof(NoiseContext, x)
  62. static const AVOption options[] = {
  63. { "amount", NULL, OFFSET(amount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX },
  64. { NULL },
  65. };
  66. static const AVClass noise_class = {
  67. .class_name = "noise",
  68. .item_name = av_default_item_name,
  69. .option = options,
  70. .version = LIBAVUTIL_VERSION_INT,
  71. };
  72. const AVBitStreamFilter ff_noise_bsf = {
  73. .name = "noise",
  74. .priv_data_size = sizeof(int),
  75. .priv_class = &noise_class,
  76. .filter = noise,
  77. };