vf_vidstabdetect.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2013 Georg Martius <georg dot martius at web dot de>
  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. #define DEFAULT_RESULT_NAME "transforms.trf"
  21. #include <vid.stab/libvidstab.h>
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/imgutils.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "vidstabutils.h"
  28. typedef struct {
  29. const AVClass* class;
  30. VSMotionDetect md;
  31. VSMotionDetectConfig conf;
  32. char* result;
  33. FILE* f;
  34. } StabData;
  35. #define OFFSET(x) offsetof(StabData, x)
  36. #define OFFSETC(x) (offsetof(StabData, conf)+offsetof(VSMotionDetectConfig, x))
  37. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  38. static const AVOption vidstabdetect_options[]= {
  39. {"result", "path to the file used to write the transforms (def:transforms.trf)", OFFSET(result), AV_OPT_TYPE_STRING, {.str = DEFAULT_RESULT_NAME}},
  40. {"shakiness", "how shaky is the video and how quick is the camera?"
  41. " 1: little (fast) 10: very strong/quick (slow) (def: 5)", OFFSETC(shakiness), AV_OPT_TYPE_INT, {.i64 = 5}, 1, 10, FLAGS},
  42. {"accuracy", "(>=shakiness) 1: low 15: high (slow) (def: 9)", OFFSETC(accuracy), AV_OPT_TYPE_INT, {.i64 = 9 }, 1, 15, FLAGS},
  43. {"stepsize", "region around minimum is scanned with 1 pixel resolution (def: 6)", OFFSETC(stepSize), AV_OPT_TYPE_INT, {.i64 = 6}, 1, 32, FLAGS},
  44. {"mincontrast", "below this contrast a field is discarded (0-1) (def: 0.3)", OFFSETC(contrastThreshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.25}, 0.0, 1.0, FLAGS},
  45. {"show", "0: draw nothing (def); 1,2: show fields and transforms", OFFSETC(show), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, FLAGS},
  46. {"tripod", "virtual tripod mode (if >0): motion is compared to a reference"
  47. " reference frame (frame # is the value) (def: 0)", OFFSETC(virtualTripod), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
  48. {NULL},
  49. };
  50. AVFILTER_DEFINE_CLASS(vidstabdetect);
  51. static av_cold int init(AVFilterContext *ctx)
  52. {
  53. StabData* sd = ctx->priv;
  54. vs_set_mem_and_log_functions();
  55. sd->class = &vidstabdetect_class;
  56. av_log(ctx, AV_LOG_VERBOSE, "vidstabdetect filter: init %s\n", LIBVIDSTAB_VERSION);
  57. return 0;
  58. }
  59. static av_cold void uninit(AVFilterContext *ctx)
  60. {
  61. StabData *sd = ctx->priv;
  62. VSMotionDetect* md = &(sd->md);
  63. if (sd->f) {
  64. fclose(sd->f);
  65. sd->f = NULL;
  66. }
  67. vsMotionDetectionCleanup(md);
  68. }
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. // If you add something here also add it in vidstabutils.c
  72. static const enum AVPixelFormat pix_fmts[] = {
  73. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  74. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
  75. AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
  76. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
  77. AV_PIX_FMT_NONE
  78. };
  79. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  80. return 0;
  81. }
  82. static int config_input(AVFilterLink *inlink)
  83. {
  84. AVFilterContext *ctx = inlink->dst;
  85. StabData *sd = ctx->priv;
  86. VSMotionDetect* md = &(sd->md);
  87. VSFrameInfo fi;
  88. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  89. vsFrameInfoInit(&fi,inlink->w, inlink->h, av_2_vs_pixel_format(ctx, inlink->format));
  90. if(fi.bytesPerPixel != av_get_bits_per_pixel(desc)/8){
  91. av_log(ctx, AV_LOG_ERROR, "pixel-format error: wrong bits/per/pixel, please report a BUG");
  92. return AVERROR(EINVAL);
  93. }
  94. if(fi.log2ChromaW != desc->log2_chroma_w){
  95. av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_w, please report a BUG");
  96. return AVERROR(EINVAL);
  97. }
  98. if(fi.log2ChromaH != desc->log2_chroma_h){
  99. av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_h, please report a BUG");
  100. return AVERROR(EINVAL);
  101. }
  102. // set values that are not initializes by the options
  103. sd->conf.algo = 1;
  104. sd->conf.modName = "vidstabdetect";
  105. if(vsMotionDetectInit(md, &sd->conf, &fi) != VS_OK){
  106. av_log(ctx, AV_LOG_ERROR, "initialization of Motion Detection failed, please report a BUG");
  107. return AVERROR(EINVAL);
  108. }
  109. vsMotionDetectGetConfig(&sd->conf, md);
  110. av_log(ctx, AV_LOG_INFO, "Video stabilization settings (pass 1/2):\n");
  111. av_log(ctx, AV_LOG_INFO, " shakiness = %d\n", sd->conf.shakiness);
  112. av_log(ctx, AV_LOG_INFO, " accuracy = %d\n", sd->conf.accuracy);
  113. av_log(ctx, AV_LOG_INFO, " stepsize = %d\n", sd->conf.stepSize);
  114. av_log(ctx, AV_LOG_INFO, " mincontrast = %f\n", sd->conf.contrastThreshold);
  115. av_log(ctx, AV_LOG_INFO, " show = %d\n", sd->conf.show);
  116. av_log(ctx, AV_LOG_INFO, " result = %s\n", sd->result);
  117. sd->f = fopen(sd->result, "w");
  118. if (sd->f == NULL) {
  119. av_log(ctx, AV_LOG_ERROR, "cannot open transform file %s\n", sd->result);
  120. return AVERROR(EINVAL);
  121. }else{
  122. if(vsPrepareFile(md, sd->f) != VS_OK){
  123. av_log(ctx, AV_LOG_ERROR, "cannot write to transform file %s\n", sd->result);
  124. return AVERROR(EINVAL);
  125. }
  126. }
  127. return 0;
  128. }
  129. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  130. {
  131. AVFilterContext *ctx = inlink->dst;
  132. StabData *sd = ctx->priv;
  133. VSMotionDetect* md = &(sd->md);
  134. LocalMotions localmotions;
  135. AVFilterLink *outlink = inlink->dst->outputs[0];
  136. int direct = 0;
  137. AVFrame *out;
  138. VSFrame frame;
  139. int plane;
  140. if (av_frame_is_writable(in)) {
  141. direct = 1;
  142. out = in;
  143. } else {
  144. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  145. if (!out) {
  146. av_frame_free(&in);
  147. return AVERROR(ENOMEM);
  148. }
  149. av_frame_copy_props(out, in);
  150. }
  151. for(plane=0; plane < md->fi.planes; plane++){
  152. frame.data[plane] = in->data[plane];
  153. frame.linesize[plane] = in->linesize[plane];
  154. }
  155. if(vsMotionDetection(md, &localmotions, &frame) != VS_OK){
  156. av_log(ctx, AV_LOG_ERROR, "motion detection failed");
  157. return AVERROR(AVERROR_EXTERNAL);
  158. } else {
  159. if(vsWriteToFile(md, sd->f, &localmotions) != VS_OK){
  160. av_log(ctx, AV_LOG_ERROR, "cannot write to transform file");
  161. return AVERROR(errno);
  162. }
  163. vs_vector_del(&localmotions);
  164. }
  165. if(sd->conf.show>0 && !direct){
  166. av_image_copy(out->data, out->linesize,
  167. (void*)in->data, in->linesize,
  168. in->format, in->width, in->height);
  169. }
  170. if (!direct)
  171. av_frame_free(&in);
  172. return ff_filter_frame(outlink, out);
  173. }
  174. static const AVFilterPad avfilter_vf_vidstabdetect_inputs[] = {
  175. {
  176. .name = "default",
  177. .type = AVMEDIA_TYPE_VIDEO,
  178. .filter_frame = filter_frame,
  179. .config_props = config_input,
  180. },
  181. { NULL }
  182. };
  183. static const AVFilterPad avfilter_vf_vidstabdetect_outputs[] = {
  184. {
  185. .name = "default",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. },
  188. { NULL }
  189. };
  190. AVFilter avfilter_vf_vidstabdetect = {
  191. .name = "vidstabdetect",
  192. .description = NULL_IF_CONFIG_SMALL("pass 1 of 2 for stabilization"
  193. "extracts relative transformations"
  194. "(pass 2 see vidstabtransform)"),
  195. .priv_size = sizeof(StabData),
  196. .init = init,
  197. .uninit = uninit,
  198. .query_formats = query_formats,
  199. .inputs = avfilter_vf_vidstabdetect_inputs,
  200. .outputs = avfilter_vf_vidstabdetect_outputs,
  201. .priv_class = &vidstabdetect_class,
  202. };