vf_showpalette.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Display frame palette (AV_PIX_FMT_PAL8)
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct ShowPaletteContext {
  29. const AVClass *class;
  30. int size;
  31. } ShowPaletteContext;
  32. #define OFFSET(x) offsetof(ShowPaletteContext, x)
  33. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  34. static const AVOption showpalette_options[] = {
  35. { "s", "set pixel box size", OFFSET(size), AV_OPT_TYPE_INT, {.i64=30}, 1, 100, FLAGS },
  36. { NULL }
  37. };
  38. AVFILTER_DEFINE_CLASS(showpalette);
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. static const enum AVPixelFormat in_fmts[] = {AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE};
  42. static const enum AVPixelFormat out_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
  43. int ret;
  44. AVFilterFormats *in = ff_make_format_list(in_fmts);
  45. AVFilterFormats *out = ff_make_format_list(out_fmts);
  46. if (!in || !out) {
  47. ret = AVERROR(ENOMEM);
  48. goto fail;
  49. }
  50. if ((ret = ff_formats_ref(in , &ctx->inputs[0]->out_formats)) < 0 ||
  51. (ret = ff_formats_ref(out, &ctx->outputs[0]->in_formats)) < 0)
  52. goto fail;
  53. return 0;
  54. fail:
  55. if (in)
  56. av_freep(&in->formats);
  57. av_freep(&in);
  58. if (out)
  59. av_freep(&out->formats);
  60. av_freep(&out);
  61. return ret;
  62. }
  63. static int config_output(AVFilterLink *outlink)
  64. {
  65. AVFilterContext *ctx = outlink->src;
  66. const ShowPaletteContext *s = ctx->priv;
  67. outlink->w = outlink->h = 16 * s->size;
  68. return 0;
  69. }
  70. static int disp_palette(AVFrame *out, const AVFrame *in, int size)
  71. {
  72. int x, y, i, j;
  73. uint32_t *dst = (uint32_t *)out->data[0];
  74. const int dst_linesize = out->linesize[0] >> 2;
  75. const uint32_t *pal = (uint32_t *)in->data[1];
  76. for (y = 0; y < 16; y++)
  77. for (x = 0; x < 16; x++)
  78. for (j = 0; j < size; j++)
  79. for (i = 0; i < size; i++)
  80. dst[(y*dst_linesize + x) * size + j*dst_linesize + i] = pal[y*16 + x];
  81. return 0;
  82. }
  83. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  84. {
  85. int ret;
  86. AVFrame *out;
  87. AVFilterContext *ctx = inlink->dst;
  88. const ShowPaletteContext *s= ctx->priv;
  89. AVFilterLink *outlink = ctx->outputs[0];
  90. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  91. if (!out) {
  92. av_frame_free(&in);
  93. return AVERROR(ENOMEM);
  94. }
  95. av_frame_copy_props(out, in);
  96. ret = disp_palette(out, in, s->size);
  97. av_frame_free(&in);
  98. return ret < 0 ? ret : ff_filter_frame(outlink, out);
  99. }
  100. static const AVFilterPad showpalette_inputs[] = {
  101. {
  102. .name = "default",
  103. .type = AVMEDIA_TYPE_VIDEO,
  104. .filter_frame = filter_frame,
  105. },
  106. { NULL }
  107. };
  108. static const AVFilterPad showpalette_outputs[] = {
  109. {
  110. .name = "default",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .config_props = config_output,
  113. },
  114. { NULL }
  115. };
  116. AVFilter ff_vf_showpalette = {
  117. .name = "showpalette",
  118. .description = NULL_IF_CONFIG_SMALL("Display frame palette."),
  119. .priv_size = sizeof(ShowPaletteContext),
  120. .query_formats = query_formats,
  121. .inputs = showpalette_inputs,
  122. .outputs = showpalette_outputs,
  123. .priv_class = &showpalette_class,
  124. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  125. };