vf_elbg.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2013 Stefano Sabatini
  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. /**
  21. * @file
  22. * video quantizer filter based on ELBG
  23. */
  24. #include "libavcodec/elbg.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/random_seed.h"
  28. #include "avfilter.h"
  29. #include "drawutils.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. typedef struct ColorContext {
  33. const AVClass *class;
  34. AVLFG lfg;
  35. unsigned int lfg_seed;
  36. int max_steps_nb;
  37. int *codeword;
  38. int codeword_length;
  39. int *codeword_closest_codebook_idxs;
  40. int *codebook;
  41. int codebook_length;
  42. const AVPixFmtDescriptor *pix_desc;
  43. uint8_t rgba_map[4];
  44. } ELBGContext;
  45. #define OFFSET(x) offsetof(ELBGContext, x)
  46. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  47. static const AVOption elbg_options[] = {
  48. { "codebook_length", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
  49. { "l", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
  50. { "nb_steps", "set max number of steps used to compute the mapping", OFFSET(max_steps_nb), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
  51. { "n", "set max number of steps used to compute the mapping", OFFSET(max_steps_nb), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
  52. { "seed", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
  53. { "s", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT32_MAX, FLAGS },
  54. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(elbg);
  57. static av_cold int init(AVFilterContext *ctx)
  58. {
  59. ELBGContext *elbg = ctx->priv;
  60. if (elbg->lfg_seed == -1)
  61. elbg->lfg_seed = av_get_random_seed();
  62. av_lfg_init(&elbg->lfg, elbg->lfg_seed);
  63. return 0;
  64. }
  65. static int query_formats(AVFilterContext *ctx)
  66. {
  67. static const enum PixelFormat pix_fmts[] = {
  68. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  69. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  70. AV_PIX_FMT_NONE
  71. };
  72. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  73. return 0;
  74. }
  75. #define NB_COMPONENTS 3
  76. static int config_input(AVFilterLink *inlink)
  77. {
  78. AVFilterContext *ctx = inlink->dst;
  79. ELBGContext *elbg = ctx->priv;
  80. elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
  81. elbg->codeword_length = inlink->w * inlink->h;
  82. elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
  83. NB_COMPONENTS * sizeof(*elbg->codeword));
  84. if (!elbg->codeword)
  85. return AVERROR(ENOMEM);
  86. elbg->codeword_closest_codebook_idxs =
  87. av_realloc_f(elbg->codeword_closest_codebook_idxs, elbg->codeword_length,
  88. sizeof(*elbg->codeword_closest_codebook_idxs));
  89. if (!elbg->codeword_closest_codebook_idxs)
  90. return AVERROR(ENOMEM);
  91. elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
  92. NB_COMPONENTS * sizeof(*elbg->codebook));
  93. if (!elbg->codebook)
  94. return AVERROR(ENOMEM);
  95. ff_fill_rgba_map(elbg->rgba_map, inlink->format);
  96. return 0;
  97. }
  98. #define R 0
  99. #define G 1
  100. #define B 2
  101. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  102. {
  103. ELBGContext *elbg = inlink->dst->priv;
  104. int i, j, k;
  105. uint8_t *p, *p0;
  106. const uint8_t r_idx = elbg->rgba_map[R];
  107. const uint8_t g_idx = elbg->rgba_map[G];
  108. const uint8_t b_idx = elbg->rgba_map[B];
  109. /* build the codeword */
  110. p0 = frame->data[0];
  111. k = 0;
  112. for (i = 0; i < inlink->h; i++) {
  113. p = p0;
  114. for (j = 0; j < inlink->w; j++) {
  115. elbg->codeword[k++] = p[r_idx];
  116. elbg->codeword[k++] = p[g_idx];
  117. elbg->codeword[k++] = p[b_idx];
  118. p += elbg->pix_desc->nb_components;
  119. }
  120. p0 += frame->linesize[0];
  121. }
  122. /* compute the codebook */
  123. avpriv_init_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
  124. elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
  125. elbg->codeword_closest_codebook_idxs, &elbg->lfg);
  126. avpriv_do_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
  127. elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
  128. elbg->codeword_closest_codebook_idxs, &elbg->lfg);
  129. /* fill the output with the codebook values */
  130. p0 = frame->data[0];
  131. k = 0;
  132. for (i = 0; i < inlink->h; i++) {
  133. p = p0;
  134. for (j = 0; j < inlink->w; j++) {
  135. int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
  136. p[r_idx] = elbg->codebook[cb_idx];
  137. p[g_idx] = elbg->codebook[cb_idx+1];
  138. p[b_idx] = elbg->codebook[cb_idx+2];
  139. p += elbg->pix_desc->nb_components;
  140. }
  141. p0 += frame->linesize[0];
  142. }
  143. return ff_filter_frame(inlink->dst->outputs[0], frame);
  144. }
  145. static av_cold void uninit(AVFilterContext *ctx)
  146. {
  147. ELBGContext *elbg = ctx->priv;
  148. av_freep(&elbg->codebook);
  149. av_freep(&elbg->codeword);
  150. av_freep(&elbg->codeword_closest_codebook_idxs);
  151. }
  152. static const AVFilterPad elbg_inputs[] = {
  153. {
  154. .name = "default",
  155. .type = AVMEDIA_TYPE_VIDEO,
  156. .config_props = config_input,
  157. .filter_frame = filter_frame,
  158. .needs_writable = 1,
  159. },
  160. { NULL }
  161. };
  162. static const AVFilterPad elbg_outputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. },
  167. { NULL }
  168. };
  169. AVFilter ff_vf_elbg = {
  170. .name = "elbg",
  171. .description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
  172. .priv_size = sizeof(ELBGContext),
  173. .priv_class = &elbg_class,
  174. .query_formats = query_formats,
  175. .init = init,
  176. .uninit = uninit,
  177. .inputs = elbg_inputs,
  178. .outputs = elbg_outputs,
  179. };