vf_pixdesctest.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 {
  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_descriptors[inlink->format];
  42. if (!(priv->line = av_malloc(sizeof(*priv->line) * inlink->w)))
  43. return AVERROR(ENOMEM);
  44. return 0;
  45. }
  46. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  47. {
  48. PixdescTestContext *priv = inlink->dst->priv;
  49. AVFilterLink *outlink = inlink->dst->outputs[0];
  50. AVFilterBufferRef *outpicref, *for_next_filter;
  51. int i, ret = 0;
  52. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE,
  53. outlink->w, outlink->h);
  54. if (!outpicref)
  55. return AVERROR(ENOMEM);
  56. avfilter_copy_buffer_ref_props(outpicref, picref);
  57. for (i = 0; i < 4; i++) {
  58. int h = outlink->h;
  59. h = i == 1 || i == 2 ? h>>priv->pix_desc->log2_chroma_h : h;
  60. if (outpicref->data[i]) {
  61. uint8_t *data = outpicref->data[i] +
  62. (outpicref->linesize[i] > 0 ? 0 : outpicref->linesize[i] * (h-1));
  63. memset(data, 0, FFABS(outpicref->linesize[i]) * h);
  64. }
  65. }
  66. /* copy palette */
  67. if (priv->pix_desc->flags & PIX_FMT_PAL ||
  68. priv->pix_desc->flags & PIX_FMT_PSEUDOPAL)
  69. memcpy(outpicref->data[1], picref->data[1], AVPALETTE_SIZE);
  70. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  71. if (for_next_filter)
  72. ret = ff_start_frame(outlink, for_next_filter);
  73. else
  74. ret = AVERROR(ENOMEM);
  75. if (ret < 0) {
  76. avfilter_unref_bufferp(&outpicref);
  77. return ret;
  78. }
  79. outlink->out_buf = outpicref;
  80. return 0;
  81. }
  82. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  83. {
  84. PixdescTestContext *priv = inlink->dst->priv;
  85. AVFilterBufferRef *inpic = inlink->cur_buf;
  86. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  87. int i, c, w = inlink->w;
  88. for (c = 0; c < priv->pix_desc->nb_components; c++) {
  89. int w1 = c == 1 || c == 2 ? w>>priv->pix_desc->log2_chroma_w : w;
  90. int h1 = c == 1 || c == 2 ? h>>priv->pix_desc->log2_chroma_h : h;
  91. int y1 = c == 1 || c == 2 ? y>>priv->pix_desc->log2_chroma_h : y;
  92. for (i = y1; i < y1 + h1; i++) {
  93. av_read_image_line(priv->line,
  94. (void*)inpic->data,
  95. inpic->linesize,
  96. priv->pix_desc,
  97. 0, i, c, w1, 0);
  98. av_write_image_line(priv->line,
  99. outpic->data,
  100. outpic->linesize,
  101. priv->pix_desc,
  102. 0, i, c, w1);
  103. }
  104. }
  105. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  106. }
  107. AVFilter avfilter_vf_pixdesctest = {
  108. .name = "pixdesctest",
  109. .description = NULL_IF_CONFIG_SMALL("Test pixel format definitions."),
  110. .priv_size = sizeof(PixdescTestContext),
  111. .uninit = uninit,
  112. .inputs = (const AVFilterPad[]) {{ .name = "default",
  113. .type = AVMEDIA_TYPE_VIDEO,
  114. .start_frame = start_frame,
  115. .draw_slice = draw_slice,
  116. .config_props = config_props,
  117. .min_perms = AV_PERM_READ, },
  118. { .name = NULL}},
  119. .outputs = (const AVFilterPad[]) {{ .name = "default",
  120. .type = AVMEDIA_TYPE_VIDEO, },
  121. { .name = NULL}},
  122. };