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.

262 lines
8.2KB

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