vf_pixdesctest.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2009 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. * pixdesc test filter
  23. */
  24. #include "libavutil/common.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct PixdescTestContext {
  30. const AVPixFmtDescriptor *pix_desc;
  31. uint16_t *line;
  32. } PixdescTestContext;
  33. static av_cold void uninit(AVFilterContext *ctx)
  34. {
  35. PixdescTestContext *priv = ctx->priv;
  36. av_freep(&priv->line);
  37. }
  38. static int config_props(AVFilterLink *inlink)
  39. {
  40. PixdescTestContext *priv = inlink->dst->priv;
  41. priv->pix_desc = av_pix_fmt_desc_get(inlink->format);
  42. av_freep(&priv->line);
  43. if (!(priv->line = av_malloc_array(sizeof(*priv->line), inlink->w)))
  44. return AVERROR(ENOMEM);
  45. return 0;
  46. }
  47. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  48. {
  49. PixdescTestContext *priv = inlink->dst->priv;
  50. AVFilterLink *outlink = inlink->dst->outputs[0];
  51. AVFrame *out;
  52. int i, c, w = inlink->w, h = inlink->h;
  53. const int cw = FF_CEIL_RSHIFT(w, priv->pix_desc->log2_chroma_w);
  54. const int ch = FF_CEIL_RSHIFT(h, priv->pix_desc->log2_chroma_h);
  55. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  56. if (!out) {
  57. av_frame_free(&in);
  58. return AVERROR(ENOMEM);
  59. }
  60. av_frame_copy_props(out, in);
  61. for (i = 0; i < 4; i++) {
  62. const int h1 = i == 1 || i == 2 ? ch : h;
  63. if (out->data[i]) {
  64. uint8_t *data = out->data[i] +
  65. (out->linesize[i] > 0 ? 0 : out->linesize[i] * (h1-1));
  66. memset(data, 0, FFABS(out->linesize[i]) * h1);
  67. }
  68. }
  69. /* copy palette */
  70. if (priv->pix_desc->flags & AV_PIX_FMT_FLAG_PAL ||
  71. priv->pix_desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  72. memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
  73. for (c = 0; c < priv->pix_desc->nb_components; c++) {
  74. const int w1 = c == 1 || c == 2 ? cw : w;
  75. const int h1 = c == 1 || c == 2 ? ch : h;
  76. for (i = 0; i < h1; i++) {
  77. av_read_image_line(priv->line,
  78. (void*)in->data,
  79. in->linesize,
  80. priv->pix_desc,
  81. 0, i, c, w1, 0);
  82. av_write_image_line(priv->line,
  83. out->data,
  84. out->linesize,
  85. priv->pix_desc,
  86. 0, i, c, w1);
  87. }
  88. }
  89. av_frame_free(&in);
  90. return ff_filter_frame(outlink, out);
  91. }
  92. static const AVFilterPad avfilter_vf_pixdesctest_inputs[] = {
  93. {
  94. .name = "default",
  95. .type = AVMEDIA_TYPE_VIDEO,
  96. .filter_frame = filter_frame,
  97. .config_props = config_props,
  98. },
  99. { NULL }
  100. };
  101. static const AVFilterPad avfilter_vf_pixdesctest_outputs[] = {
  102. {
  103. .name = "default",
  104. .type = AVMEDIA_TYPE_VIDEO,
  105. },
  106. { NULL }
  107. };
  108. AVFilter ff_vf_pixdesctest = {
  109. .name = "pixdesctest",
  110. .description = NULL_IF_CONFIG_SMALL("Test pixel format definitions."),
  111. .priv_size = sizeof(PixdescTestContext),
  112. .uninit = uninit,
  113. .inputs = avfilter_vf_pixdesctest_inputs,
  114. .outputs = avfilter_vf_pixdesctest_outputs,
  115. };