vf_elbg.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 ELBGContext {
  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. int pal8;
  45. } ELBGContext;
  46. #define OFFSET(x) offsetof(ELBGContext, x)
  47. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  48. static const AVOption elbg_options[] = {
  49. { "codebook_length", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
  50. { "l", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
  51. { "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 },
  52. { "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 },
  53. { "seed", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
  54. { "s", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT32_MAX, FLAGS },
  55. { "pal8", "set the pal8 output", OFFSET(pal8), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  56. { NULL }
  57. };
  58. AVFILTER_DEFINE_CLASS(elbg);
  59. static av_cold int init(AVFilterContext *ctx)
  60. {
  61. ELBGContext *elbg = ctx->priv;
  62. if (elbg->pal8 && elbg->codebook_length > 256) {
  63. av_log(ctx, AV_LOG_ERROR, "pal8 output allows max 256 codebook length.\n");
  64. return AVERROR(EINVAL);
  65. }
  66. if (elbg->lfg_seed == -1)
  67. elbg->lfg_seed = av_get_random_seed();
  68. av_lfg_init(&elbg->lfg, elbg->lfg_seed);
  69. return 0;
  70. }
  71. static int query_formats(AVFilterContext *ctx)
  72. {
  73. ELBGContext *elbg = ctx->priv;
  74. int ret;
  75. static const enum AVPixelFormat pix_fmts[] = {
  76. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  77. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  78. AV_PIX_FMT_NONE
  79. };
  80. if (!elbg->pal8) {
  81. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  82. if (!fmts_list)
  83. return AVERROR(ENOMEM);
  84. return ff_set_common_formats(ctx, fmts_list);
  85. } else {
  86. static const enum AVPixelFormat pal8_fmt[] = {
  87. AV_PIX_FMT_PAL8,
  88. AV_PIX_FMT_NONE
  89. };
  90. if ((ret = ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->inputs[0]->out_formats)) < 0 ||
  91. (ret = ff_formats_ref(ff_make_format_list(pal8_fmt), &ctx->outputs[0]->in_formats)) < 0)
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. #define NB_COMPONENTS 3
  97. static int config_input(AVFilterLink *inlink)
  98. {
  99. AVFilterContext *ctx = inlink->dst;
  100. ELBGContext *elbg = ctx->priv;
  101. elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
  102. elbg->codeword_length = inlink->w * inlink->h;
  103. elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
  104. NB_COMPONENTS * sizeof(*elbg->codeword));
  105. if (!elbg->codeword)
  106. return AVERROR(ENOMEM);
  107. elbg->codeword_closest_codebook_idxs =
  108. av_realloc_f(elbg->codeword_closest_codebook_idxs, elbg->codeword_length,
  109. sizeof(*elbg->codeword_closest_codebook_idxs));
  110. if (!elbg->codeword_closest_codebook_idxs)
  111. return AVERROR(ENOMEM);
  112. elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
  113. NB_COMPONENTS * sizeof(*elbg->codebook));
  114. if (!elbg->codebook)
  115. return AVERROR(ENOMEM);
  116. ff_fill_rgba_map(elbg->rgba_map, inlink->format);
  117. return 0;
  118. }
  119. #define R 0
  120. #define G 1
  121. #define B 2
  122. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  123. {
  124. ELBGContext *elbg = inlink->dst->priv;
  125. int i, j, k;
  126. uint8_t *p, *p0;
  127. const uint8_t r_idx = elbg->rgba_map[R];
  128. const uint8_t g_idx = elbg->rgba_map[G];
  129. const uint8_t b_idx = elbg->rgba_map[B];
  130. /* build the codeword */
  131. p0 = frame->data[0];
  132. k = 0;
  133. for (i = 0; i < inlink->h; i++) {
  134. p = p0;
  135. for (j = 0; j < inlink->w; j++) {
  136. elbg->codeword[k++] = p[r_idx];
  137. elbg->codeword[k++] = p[g_idx];
  138. elbg->codeword[k++] = p[b_idx];
  139. p += elbg->pix_desc->nb_components;
  140. }
  141. p0 += frame->linesize[0];
  142. }
  143. /* compute the codebook */
  144. avpriv_init_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
  145. elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
  146. elbg->codeword_closest_codebook_idxs, &elbg->lfg);
  147. avpriv_do_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
  148. elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
  149. elbg->codeword_closest_codebook_idxs, &elbg->lfg);
  150. if (elbg->pal8) {
  151. AVFilterLink *outlink = inlink->dst->outputs[0];
  152. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  153. uint32_t *pal;
  154. if (!out)
  155. return AVERROR(ENOMEM);
  156. out->pts = frame->pts;
  157. av_frame_free(&frame);
  158. pal = (uint32_t *)out->data[1];
  159. p0 = (uint8_t *)out->data[0];
  160. for (i = 0; i < elbg->codebook_length; i++) {
  161. pal[i] = 0xFFU << 24 |
  162. (elbg->codebook[i*3 ] << 16) |
  163. (elbg->codebook[i*3+1] << 8) |
  164. elbg->codebook[i*3+2];
  165. }
  166. k = 0;
  167. for (i = 0; i < inlink->h; i++) {
  168. p = p0;
  169. for (j = 0; j < inlink->w; j++, p++) {
  170. p[0] = elbg->codeword_closest_codebook_idxs[k++];
  171. }
  172. p0 += out->linesize[0];
  173. }
  174. return ff_filter_frame(outlink, out);
  175. }
  176. /* fill the output with the codebook values */
  177. p0 = frame->data[0];
  178. k = 0;
  179. for (i = 0; i < inlink->h; i++) {
  180. p = p0;
  181. for (j = 0; j < inlink->w; j++) {
  182. int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
  183. p[r_idx] = elbg->codebook[cb_idx];
  184. p[g_idx] = elbg->codebook[cb_idx+1];
  185. p[b_idx] = elbg->codebook[cb_idx+2];
  186. p += elbg->pix_desc->nb_components;
  187. }
  188. p0 += frame->linesize[0];
  189. }
  190. return ff_filter_frame(inlink->dst->outputs[0], frame);
  191. }
  192. static av_cold void uninit(AVFilterContext *ctx)
  193. {
  194. ELBGContext *elbg = ctx->priv;
  195. av_freep(&elbg->codebook);
  196. av_freep(&elbg->codeword);
  197. av_freep(&elbg->codeword_closest_codebook_idxs);
  198. }
  199. static const AVFilterPad elbg_inputs[] = {
  200. {
  201. .name = "default",
  202. .type = AVMEDIA_TYPE_VIDEO,
  203. .config_props = config_input,
  204. .filter_frame = filter_frame,
  205. .needs_writable = 1,
  206. },
  207. { NULL }
  208. };
  209. static const AVFilterPad elbg_outputs[] = {
  210. {
  211. .name = "default",
  212. .type = AVMEDIA_TYPE_VIDEO,
  213. },
  214. { NULL }
  215. };
  216. AVFilter ff_vf_elbg = {
  217. .name = "elbg",
  218. .description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
  219. .priv_size = sizeof(ELBGContext),
  220. .priv_class = &elbg_class,
  221. .query_formats = query_formats,
  222. .init = init,
  223. .uninit = uninit,
  224. .inputs = elbg_inputs,
  225. .outputs = elbg_outputs,
  226. };