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.

313 lines
9.1KB

  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 = av_fopen_utf8(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. int tfactor = 0, bfactor = 1;
  101. char hint = '=', field = '=';
  102. int p;
  103. av_frame_free(&s->frame[0]);
  104. s->frame[0] = s->frame[1];
  105. s->frame[1] = s->frame[2];
  106. s->frame[2] = in;
  107. if (!s->frame[1])
  108. return 0;
  109. else if (!s->frame[0]) {
  110. s->frame[0] = av_frame_clone(s->frame[1]);
  111. if (!s->frame[0])
  112. return AVERROR(ENOMEM);
  113. }
  114. while (1) {
  115. if (fgets(buf, sizeof(buf)-1, s->hint)) {
  116. s->line++;
  117. if (buf[0] == '#' || buf[0] == ';') {
  118. continue;
  119. } else if (sscanf(buf, "%"PRId64",%"PRId64" %c %c", &tf, &bf, &hint, &field) == 4) {
  120. ;
  121. } else if (sscanf(buf, "%"PRId64",%"PRId64" %c", &tf, &bf, &hint) == 3) {
  122. ;
  123. } else if (sscanf(buf, "%"PRId64",%"PRId64"", &tf, &bf) == 2) {
  124. ;
  125. } else {
  126. av_log(ctx, AV_LOG_ERROR, "Invalid entry at line %"PRId64".\n", s->line);
  127. return AVERROR_INVALIDDATA;
  128. }
  129. switch (s->mode) {
  130. case 0:
  131. if (tf > outlink->frame_count_in + 1 || tf < FFMAX(0, outlink->frame_count_in - 1) ||
  132. bf > outlink->frame_count_in + 1 || bf < FFMAX(0, outlink->frame_count_in - 1)) {
  133. 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_out);
  134. return AVERROR_INVALIDDATA;
  135. }
  136. break;
  137. case 1:
  138. if (tf > 1 || tf < -1 ||
  139. bf > 1 || bf < -1) {
  140. 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_out);
  141. return AVERROR_INVALIDDATA;
  142. }
  143. };
  144. break;
  145. } else {
  146. av_log(ctx, AV_LOG_ERROR, "Missing entry for %"PRId64". input frame.\n", inlink->frame_count_out);
  147. return AVERROR_INVALIDDATA;
  148. }
  149. }
  150. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  151. if (!out)
  152. return AVERROR(ENOMEM);
  153. av_frame_copy_props(out, s->frame[1]);
  154. switch (s->mode) {
  155. case 0:
  156. top = s->frame[tf - outlink->frame_count_in + 1];
  157. bottom = s->frame[bf - outlink->frame_count_in + 1];
  158. break;
  159. case 1:
  160. top = s->frame[1 + tf];
  161. bottom = s->frame[1 + bf];
  162. break;
  163. default:
  164. av_assert0(0);
  165. }
  166. switch (field) {
  167. case 'b':
  168. tfactor = 1;
  169. top = bottom;
  170. break;
  171. case 't':
  172. bfactor = 0;
  173. bottom = top;
  174. break;
  175. case '=':
  176. break;
  177. default:
  178. av_log(ctx, AV_LOG_ERROR, "Invalid field: %c.\n", field);
  179. av_frame_free(&out);
  180. return AVERROR(EINVAL);
  181. }
  182. switch (hint) {
  183. case '+':
  184. out->interlaced_frame = 1;
  185. break;
  186. case '-':
  187. out->interlaced_frame = 0;
  188. break;
  189. case '=':
  190. break;
  191. case 'b':
  192. tfactor = 1;
  193. top = bottom;
  194. break;
  195. case 't':
  196. bfactor = 0;
  197. bottom = top;
  198. break;
  199. default:
  200. av_log(ctx, AV_LOG_ERROR, "Invalid hint: %c.\n", hint);
  201. av_frame_free(&out);
  202. return AVERROR(EINVAL);
  203. }
  204. for (p = 0; p < s->nb_planes; p++) {
  205. av_image_copy_plane(out->data[p],
  206. out->linesize[p] * 2,
  207. top->data[p] + tfactor * top->linesize[p],
  208. top->linesize[p] * 2,
  209. s->planewidth[p],
  210. (s->planeheight[p] + 1) / 2);
  211. av_image_copy_plane(out->data[p] + out->linesize[p],
  212. out->linesize[p] * 2,
  213. bottom->data[p] + bfactor * bottom->linesize[p],
  214. bottom->linesize[p] * 2,
  215. s->planewidth[p],
  216. (s->planeheight[p] + 1) / 2);
  217. }
  218. return ff_filter_frame(outlink, out);
  219. }
  220. static int request_frame(AVFilterLink *outlink)
  221. {
  222. AVFilterContext *ctx = outlink->src;
  223. FieldHintContext *s = ctx->priv;
  224. int ret;
  225. if (s->eof)
  226. return AVERROR_EOF;
  227. ret = ff_request_frame(ctx->inputs[0]);
  228. if (ret == AVERROR_EOF && s->frame[2]) {
  229. AVFrame *next = av_frame_clone(s->frame[2]);
  230. if (!next)
  231. return AVERROR(ENOMEM);
  232. ret = filter_frame(ctx->inputs[0], next);
  233. s->eof = 1;
  234. }
  235. return ret;
  236. }
  237. static av_cold void uninit(AVFilterContext *ctx)
  238. {
  239. FieldHintContext *s = ctx->priv;
  240. if (s->hint)
  241. fclose(s->hint);
  242. s->hint = NULL;
  243. av_frame_free(&s->frame[0]);
  244. av_frame_free(&s->frame[1]);
  245. av_frame_free(&s->frame[2]);
  246. }
  247. static const AVFilterPad inputs[] = {
  248. {
  249. .name = "default",
  250. .type = AVMEDIA_TYPE_VIDEO,
  251. .config_props = config_input,
  252. .filter_frame = filter_frame,
  253. },
  254. { NULL }
  255. };
  256. static const AVFilterPad outputs[] = {
  257. {
  258. .name = "default",
  259. .type = AVMEDIA_TYPE_VIDEO,
  260. .request_frame = request_frame,
  261. },
  262. { NULL }
  263. };
  264. AVFilter ff_vf_fieldhint = {
  265. .name = "fieldhint",
  266. .description = NULL_IF_CONFIG_SMALL("Field matching using hints."),
  267. .priv_size = sizeof(FieldHintContext),
  268. .priv_class = &fieldhint_class,
  269. .init = init,
  270. .uninit = uninit,
  271. .query_formats = query_formats,
  272. .inputs = inputs,
  273. .outputs = outputs,
  274. };