vf_fieldhint.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2016 Paul B Mahol
  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 "libavutil/avassert.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/internal.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct FieldHintContext {
  29. const AVClass *class;
  30. char *hint_file_str;
  31. FILE *hint;
  32. int mode;
  33. AVFrame *frame[3];
  34. int64_t line;
  35. int nb_planes;
  36. int eof;
  37. int planewidth[4];
  38. int planeheight[4];
  39. } FieldHintContext;
  40. #define OFFSET(x) offsetof(FieldHintContext, x)
  41. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  42. static const AVOption fieldhint_options[] = {
  43. { "hint", "set hint file", OFFSET(hint_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  44. { "mode", "set hint mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
  45. { "absolute", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  46. { "relative", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  47. { NULL }
  48. };
  49. AVFILTER_DEFINE_CLASS(fieldhint);
  50. static av_cold int init(AVFilterContext *ctx)
  51. {
  52. FieldHintContext *s = ctx->priv;
  53. int ret;
  54. if (!s->hint_file_str) {
  55. av_log(ctx, AV_LOG_ERROR, "Hint file must be set.\n");
  56. return AVERROR(EINVAL);
  57. }
  58. s->hint = fopen(s->hint_file_str, "r");
  59. if (!s->hint) {
  60. ret = AVERROR(errno);
  61. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", s->hint_file_str, av_err2str(ret));
  62. return ret;
  63. }
  64. return 0;
  65. }
  66. static int query_formats(AVFilterContext *ctx)
  67. {
  68. AVFilterFormats *pix_fmts = NULL;
  69. int fmt, ret;
  70. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  71. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  72. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  73. desc->flags & AV_PIX_FMT_FLAG_PAL ||
  74. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
  75. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  76. return ret;
  77. }
  78. return ff_set_common_formats(ctx, pix_fmts);
  79. }
  80. static int config_input(AVFilterLink *inlink)
  81. {
  82. FieldHintContext *s = inlink->dst->priv;
  83. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  84. int ret;
  85. if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
  86. return ret;
  87. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  88. s->planeheight[0] = s->planeheight[3] = inlink->h;
  89. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  90. return 0;
  91. }
  92. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  93. {
  94. AVFilterContext *ctx = inlink->dst;
  95. AVFilterLink *outlink = ctx->outputs[0];
  96. FieldHintContext *s = ctx->priv;
  97. AVFrame *out, *top, *bottom;
  98. char buf[1024] = { 0 };
  99. int64_t tf, bf;
  100. char hint = '=';
  101. int p;
  102. av_frame_free(&s->frame[0]);
  103. s->frame[0] = s->frame[1];
  104. s->frame[1] = s->frame[2];
  105. s->frame[2] = in;
  106. if (!s->frame[1])
  107. return 0;
  108. else if (!s->frame[0]) {
  109. s->frame[0] = av_frame_clone(s->frame[1]);
  110. if (!s->frame[0])
  111. return AVERROR(ENOMEM);
  112. }
  113. while (1) {
  114. if (fgets(buf, sizeof(buf)-1, s->hint)) {
  115. s->line++;
  116. if (buf[0] == '#' || buf[0] == ';') {
  117. continue;
  118. } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) {
  119. ;
  120. } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) {
  121. ;
  122. } else {
  123. av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line);
  124. return AVERROR_INVALIDDATA;
  125. }
  126. switch (s->mode) {
  127. case 0:
  128. if (tf > outlink->frame_count + 1 || tf < FFMAX(0, outlink->frame_count - 1) ||
  129. bf > outlink->frame_count + 1 || bf < FFMAX(0, outlink->frame_count - 1)) {
  130. av_log(ctx, AV_LOG_ERROR, "Out of range frames %"PRId64" and/or %"PRId64" on line %"PRId64" for %"PRId64". input frame.\n", tf, bf, s->line, inlink->frame_count);
  131. return AVERROR_INVALIDDATA;
  132. }
  133. break;
  134. case 1:
  135. if (tf > 1 || tf < -1 ||
  136. bf > 1 || bf < -1) {
  137. av_log(ctx, AV_LOG_ERROR, "Out of range %"PRId64" and/or %"PRId64" on line %"PRId64" for %"PRId64". input frame.\n", tf, bf, s->line, inlink->frame_count);
  138. return AVERROR_INVALIDDATA;
  139. }
  140. };
  141. break;
  142. } else {
  143. av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inlink->frame_count);
  144. return AVERROR_INVALIDDATA;
  145. }
  146. }
  147. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  148. if (!out)
  149. return AVERROR(ENOMEM);
  150. av_frame_copy_props(out, s->frame[1]);
  151. switch (s->mode) {
  152. case 0:
  153. top = s->frame[tf - outlink->frame_count + 1];
  154. bottom = s->frame[bf - outlink->frame_count + 1];
  155. break;
  156. case 1:
  157. top = s->frame[1 + tf];
  158. bottom = s->frame[1 + bf];
  159. break;
  160. default:
  161. av_assert0(0);
  162. }
  163. switch (hint) {
  164. case '+':
  165. out->interlaced_frame = 1;
  166. break;
  167. case '-':
  168. out->interlaced_frame = 0;
  169. break;
  170. case '=':
  171. break;
  172. default:
  173. av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint);
  174. return AVERROR(EINVAL);
  175. }
  176. for (p = 0; p < s->nb_planes; p++) {
  177. av_image_copy_plane(out->data[p],
  178. out->linesize[p] * 2,
  179. top->data[p],
  180. top->linesize[p] * 2,
  181. s->planewidth[p],
  182. (s->planeheight[p] + 1) / 2);
  183. av_image_copy_plane(out->data[p] + out->linesize[p],
  184. out->linesize[p] * 2,
  185. bottom->data[p] + bottom->linesize[p],
  186. bottom->linesize[p] * 2,
  187. s->planewidth[p],
  188. (s->planeheight[p] + 1) / 2);
  189. }
  190. return ff_filter_frame(outlink, out);
  191. }
  192. static int request_frame(AVFilterLink *outlink)
  193. {
  194. AVFilterContext *ctx = outlink->src;
  195. FieldHintContext *s = ctx->priv;
  196. int ret;
  197. if (s->eof)
  198. return AVERROR_EOF;
  199. ret = ff_request_frame(ctx->inputs[0]);
  200. if (ret == AVERROR_EOF && s->frame[2]) {
  201. AVFrame *next = av_frame_clone(s->frame[2]);
  202. if (!next)
  203. return AVERROR(ENOMEM);
  204. ret = filter_frame(ctx->inputs[0], next);
  205. s->eof = 1;
  206. }
  207. return ret;
  208. }
  209. static av_cold void uninit(AVFilterContext *ctx)
  210. {
  211. FieldHintContext *s = ctx->priv;
  212. if (s->hint)
  213. fclose(s->hint);
  214. s->hint = NULL;
  215. av_frame_free(&s->frame[0]);
  216. av_frame_free(&s->frame[1]);
  217. av_frame_free(&s->frame[2]);
  218. }
  219. static const AVFilterPad inputs[] = {
  220. {
  221. .name = "default",
  222. .type = AVMEDIA_TYPE_VIDEO,
  223. .config_props = config_input,
  224. .filter_frame = filter_frame,
  225. },
  226. { NULL }
  227. };
  228. static const AVFilterPad outputs[] = {
  229. {
  230. .name = "default",
  231. .type = AVMEDIA_TYPE_VIDEO,
  232. .request_frame = request_frame,
  233. },
  234. { NULL }
  235. };
  236. AVFilter ff_vf_fieldhint = {
  237. .name = "fieldhint",
  238. .description = NULL_IF_CONFIG_SMALL("Field matching using hints."),
  239. .priv_size = sizeof(FieldHintContext),
  240. .priv_class = &fieldhint_class,
  241. .init = init,
  242. .uninit = uninit,
  243. .query_formats = query_formats,
  244. .inputs = inputs,
  245. .outputs = outputs,
  246. };