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.

160 lines
4.7KB

  1. /*
  2. * Copyright (c) 2009 Stefano Sabatini
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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_desc_get(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], outpicref->data[1], 256*4);
  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. 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. static const AVFilterPad avfilter_vf_pixdesctest_inputs[] = {
  108. {
  109. .name = "default",
  110. .type = AVMEDIA_TYPE_VIDEO,
  111. .start_frame = start_frame,
  112. .draw_slice = draw_slice,
  113. .config_props = config_props,
  114. .min_perms = AV_PERM_READ,
  115. },
  116. { NULL }
  117. };
  118. static const AVFilterPad avfilter_vf_pixdesctest_outputs[] = {
  119. {
  120. .name = "default",
  121. .type = AVMEDIA_TYPE_VIDEO,
  122. },
  123. { NULL }
  124. };
  125. AVFilter avfilter_vf_pixdesctest = {
  126. .name = "pixdesctest",
  127. .description = NULL_IF_CONFIG_SMALL("Test pixel format definitions."),
  128. .priv_size = sizeof(PixdescTestContext),
  129. .uninit = uninit,
  130. .inputs = avfilter_vf_pixdesctest_inputs,
  131. .outputs = avfilter_vf_pixdesctest_outputs,
  132. };