vf_qp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  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. #include <math.h>
  21. #include "libavutil/eval.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/opt.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct QPContext {
  30. const AVClass *class;
  31. char *qp_expr_str;
  32. int8_t lut[257];
  33. int h, qstride;
  34. } QPContext;
  35. #define OFFSET(x) offsetof(QPContext, x)
  36. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  37. static const AVOption qp_options[] = {
  38. { "qp", "set qp expression", OFFSET(qp_expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  39. { NULL }
  40. };
  41. AVFILTER_DEFINE_CLASS(qp);
  42. static int config_input(AVFilterLink *inlink)
  43. {
  44. AVFilterContext *ctx = inlink->dst;
  45. QPContext *s = ctx->priv;
  46. int i;
  47. if (!s->qp_expr_str)
  48. return 0;
  49. s->h = (inlink->h + 15) >> 4;
  50. s->qstride = (inlink->w + 15) >> 4;
  51. for (i = -129; i < 128; i++) {
  52. double var_values[] = { i != -129, i, 0 };
  53. static const char *var_names[] = { "known", "qp", NULL };
  54. double temp_val;
  55. int ret;
  56. ret = av_expr_parse_and_eval(&temp_val, s->qp_expr_str,
  57. var_names, var_values,
  58. NULL, NULL, NULL, NULL, 0, 0, ctx);
  59. if (ret < 0)
  60. return ret;
  61. s->lut[i + 129] = lrintf(temp_val);
  62. }
  63. return 0;
  64. }
  65. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  66. {
  67. AVFilterContext *ctx = inlink->dst;
  68. AVFilterLink *outlink = ctx->outputs[0];
  69. QPContext *s = ctx->priv;
  70. AVBufferRef *out_qp_table_buf;
  71. AVFrame *out;
  72. const int8_t *in_qp_table;
  73. int type, stride, ret;
  74. if (!s->qp_expr_str || ctx->is_disabled)
  75. return ff_filter_frame(outlink, in);
  76. out_qp_table_buf = av_buffer_alloc(s->h * s->qstride);
  77. if (!out_qp_table_buf) {
  78. ret = AVERROR(ENOMEM);
  79. goto fail;
  80. }
  81. out = av_frame_clone(in);
  82. if (!out) {
  83. av_buffer_unref(&out_qp_table_buf);
  84. ret = AVERROR(ENOMEM);
  85. goto fail;
  86. }
  87. in_qp_table = av_frame_get_qp_table(in, &stride, &type);
  88. av_frame_set_qp_table(out, out_qp_table_buf, s->qstride, type);
  89. if (in_qp_table) {
  90. int y, x;
  91. for (y = 0; y < s->h; y++)
  92. for (x = 0; x < s->qstride; x++)
  93. out_qp_table_buf->data[x + s->qstride * y] = s->lut[129 +
  94. ((int8_t)in_qp_table[x + stride * y])];
  95. } else {
  96. int y, x, qp = s->lut[0];
  97. for (y = 0; y < s->h; y++)
  98. for (x = 0; x < s->qstride; x++)
  99. out_qp_table_buf->data[x + s->qstride * y] = qp;
  100. }
  101. ret = ff_filter_frame(outlink, out);
  102. fail:
  103. av_frame_free(&in);
  104. return ret;
  105. }
  106. static const AVFilterPad qp_inputs[] = {
  107. {
  108. .name = "default",
  109. .type = AVMEDIA_TYPE_VIDEO,
  110. .filter_frame = filter_frame,
  111. .config_props = config_input,
  112. },
  113. { NULL }
  114. };
  115. static const AVFilterPad qp_outputs[] = {
  116. {
  117. .name = "default",
  118. .type = AVMEDIA_TYPE_VIDEO,
  119. },
  120. { NULL }
  121. };
  122. AVFilter ff_vf_qp = {
  123. .name = "qp",
  124. .description = NULL_IF_CONFIG_SMALL("Change video quantization parameters."),
  125. .priv_size = sizeof(QPContext),
  126. .inputs = qp_inputs,
  127. .outputs = qp_outputs,
  128. .priv_class = &qp_class,
  129. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  130. };