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.

213 lines
6.6KB

  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 AVPixelFormat 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. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  73. if (!fmts_list)
  74. return AVERROR(ENOMEM);
  75. return ff_set_common_formats(ctx, fmts_list);
  76. }
  77. #define NB_COMPONENTS 3
  78. static int config_input(AVFilterLink *inlink)
  79. {
  80. AVFilterContext *ctx = inlink->dst;
  81. ELBGContext *elbg = ctx->priv;
  82. elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
  83. elbg->codeword_length = inlink->w * inlink->h;
  84. elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
  85. NB_COMPONENTS * sizeof(*elbg->codeword));
  86. if (!elbg->codeword)
  87. return AVERROR(ENOMEM);
  88. elbg->codeword_closest_codebook_idxs =
  89. av_realloc_f(elbg->codeword_closest_codebook_idxs, elbg->codeword_length,
  90. sizeof(*elbg->codeword_closest_codebook_idxs));
  91. if (!elbg->codeword_closest_codebook_idxs)
  92. return AVERROR(ENOMEM);
  93. elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
  94. NB_COMPONENTS * sizeof(*elbg->codebook));
  95. if (!elbg->codebook)
  96. return AVERROR(ENOMEM);
  97. ff_fill_rgba_map(elbg->rgba_map, inlink->format);
  98. return 0;
  99. }
  100. #define R 0
  101. #define G 1
  102. #define B 2
  103. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  104. {
  105. ELBGContext *elbg = inlink->dst->priv;
  106. int i, j, k;
  107. uint8_t *p, *p0;
  108. const uint8_t r_idx = elbg->rgba_map[R];
  109. const uint8_t g_idx = elbg->rgba_map[G];
  110. const uint8_t b_idx = elbg->rgba_map[B];
  111. /* build the codeword */
  112. p0 = frame->data[0];
  113. k = 0;
  114. for (i = 0; i < inlink->h; i++) {
  115. p = p0;
  116. for (j = 0; j < inlink->w; j++) {
  117. elbg->codeword[k++] = p[r_idx];
  118. elbg->codeword[k++] = p[g_idx];
  119. elbg->codeword[k++] = p[b_idx];
  120. p += elbg->pix_desc->nb_components;
  121. }
  122. p0 += frame->linesize[0];
  123. }
  124. /* compute the codebook */
  125. avpriv_init_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
  126. elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
  127. elbg->codeword_closest_codebook_idxs, &elbg->lfg);
  128. avpriv_do_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
  129. elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
  130. elbg->codeword_closest_codebook_idxs, &elbg->lfg);
  131. /* fill the output with the codebook values */
  132. p0 = frame->data[0];
  133. k = 0;
  134. for (i = 0; i < inlink->h; i++) {
  135. p = p0;
  136. for (j = 0; j < inlink->w; j++) {
  137. int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
  138. p[r_idx] = elbg->codebook[cb_idx];
  139. p[g_idx] = elbg->codebook[cb_idx+1];
  140. p[b_idx] = elbg->codebook[cb_idx+2];
  141. p += elbg->pix_desc->nb_components;
  142. }
  143. p0 += frame->linesize[0];
  144. }
  145. return ff_filter_frame(inlink->dst->outputs[0], frame);
  146. }
  147. static av_cold void uninit(AVFilterContext *ctx)
  148. {
  149. ELBGContext *elbg = ctx->priv;
  150. av_freep(&elbg->codebook);
  151. av_freep(&elbg->codeword);
  152. av_freep(&elbg->codeword_closest_codebook_idxs);
  153. }
  154. static const AVFilterPad elbg_inputs[] = {
  155. {
  156. .name = "default",
  157. .type = AVMEDIA_TYPE_VIDEO,
  158. .config_props = config_input,
  159. .filter_frame = filter_frame,
  160. .needs_writable = 1,
  161. },
  162. { NULL }
  163. };
  164. static const AVFilterPad elbg_outputs[] = {
  165. {
  166. .name = "default",
  167. .type = AVMEDIA_TYPE_VIDEO,
  168. },
  169. { NULL }
  170. };
  171. AVFilter ff_vf_elbg = {
  172. .name = "elbg",
  173. .description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
  174. .priv_size = sizeof(ELBGContext),
  175. .priv_class = &elbg_class,
  176. .query_formats = query_formats,
  177. .init = init,
  178. .uninit = uninit,
  179. .inputs = elbg_inputs,
  180. .outputs = elbg_outputs,
  181. };