vf_qp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. int evaluate_per_mb;
  35. } QPContext;
  36. #define OFFSET(x) offsetof(QPContext, x)
  37. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  38. static const AVOption qp_options[] = {
  39. { "qp", "set qp expression", OFFSET(qp_expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  40. { NULL }
  41. };
  42. AVFILTER_DEFINE_CLASS(qp);
  43. static int config_input(AVFilterLink *inlink)
  44. {
  45. AVFilterContext *ctx = inlink->dst;
  46. QPContext *s = ctx->priv;
  47. int i;
  48. int ret;
  49. AVExpr *e = NULL;
  50. static const char *var_names[] = { "known", "qp", "x", "y", "w", "h", NULL };
  51. if (!s->qp_expr_str)
  52. return 0;
  53. ret = av_expr_parse(&e, s->qp_expr_str, var_names, NULL, NULL, NULL, NULL, 0, ctx);
  54. if (ret < 0)
  55. return ret;
  56. s->h = (inlink->h + 15) >> 4;
  57. s->qstride = (inlink->w + 15) >> 4;
  58. for (i = -129; i < 128; i++) {
  59. double var_values[] = { i != -129, i, NAN, NAN, s->qstride, s->h, 0};
  60. double temp_val = av_expr_eval(e, var_values, NULL);
  61. if (isnan(temp_val)) {
  62. if(strchr(s->qp_expr_str, 'x') || strchr(s->qp_expr_str, 'y'))
  63. s->evaluate_per_mb = 1;
  64. else {
  65. av_expr_free(e);
  66. return AVERROR(EINVAL);
  67. }
  68. }
  69. s->lut[i + 129] = lrintf(temp_val);
  70. }
  71. av_expr_free(e);
  72. return 0;
  73. }
  74. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  75. {
  76. AVFilterContext *ctx = inlink->dst;
  77. AVFilterLink *outlink = ctx->outputs[0];
  78. QPContext *s = ctx->priv;
  79. AVBufferRef *out_qp_table_buf;
  80. AVFrame *out = NULL;
  81. const int8_t *in_qp_table;
  82. int type, stride, ret;
  83. if (!s->qp_expr_str || ctx->is_disabled)
  84. return ff_filter_frame(outlink, in);
  85. out_qp_table_buf = av_buffer_alloc(s->h * s->qstride);
  86. if (!out_qp_table_buf) {
  87. ret = AVERROR(ENOMEM);
  88. goto fail;
  89. }
  90. out = av_frame_clone(in);
  91. if (!out) {
  92. av_buffer_unref(&out_qp_table_buf);
  93. ret = AVERROR(ENOMEM);
  94. goto fail;
  95. }
  96. in_qp_table = av_frame_get_qp_table(in, &stride, &type);
  97. av_frame_set_qp_table(out, out_qp_table_buf, s->qstride, type);
  98. if (s->evaluate_per_mb) {
  99. int y, x;
  100. for (y = 0; y < s->h; y++)
  101. for (x = 0; x < s->qstride; x++) {
  102. int qp = in_qp_table ? in_qp_table[x + stride * y] : NAN;
  103. double var_values[] = { !!in_qp_table, qp, x, y, s->qstride, s->h, 0};
  104. static const char *var_names[] = { "known", "qp", "x", "y", "w", "h", NULL };
  105. double temp_val;
  106. ret = av_expr_parse_and_eval(&temp_val, s->qp_expr_str,
  107. var_names, var_values,
  108. NULL, NULL, NULL, NULL, 0, 0, ctx);
  109. if (ret < 0)
  110. goto fail;
  111. out_qp_table_buf->data[x + s->qstride * y] = lrintf(temp_val);
  112. }
  113. } else if (in_qp_table) {
  114. int y, x;
  115. for (y = 0; y < s->h; y++)
  116. for (x = 0; x < s->qstride; x++)
  117. out_qp_table_buf->data[x + s->qstride * y] = s->lut[129 +
  118. ((int8_t)in_qp_table[x + stride * y])];
  119. } else {
  120. int y, x, qp = s->lut[0];
  121. for (y = 0; y < s->h; y++)
  122. for (x = 0; x < s->qstride; x++)
  123. out_qp_table_buf->data[x + s->qstride * y] = qp;
  124. }
  125. ret = ff_filter_frame(outlink, out);
  126. out = NULL;
  127. fail:
  128. av_frame_free(&in);
  129. av_frame_free(&out);
  130. return ret;
  131. }
  132. static const AVFilterPad qp_inputs[] = {
  133. {
  134. .name = "default",
  135. .type = AVMEDIA_TYPE_VIDEO,
  136. .filter_frame = filter_frame,
  137. .config_props = config_input,
  138. },
  139. { NULL }
  140. };
  141. static const AVFilterPad qp_outputs[] = {
  142. {
  143. .name = "default",
  144. .type = AVMEDIA_TYPE_VIDEO,
  145. },
  146. { NULL }
  147. };
  148. AVFilter ff_vf_qp = {
  149. .name = "qp",
  150. .description = NULL_IF_CONFIG_SMALL("Change video quantization parameters."),
  151. .priv_size = sizeof(QPContext),
  152. .inputs = qp_inputs,
  153. .outputs = qp_outputs,
  154. .priv_class = &qp_class,
  155. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  156. };