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.

310 lines
9.0KB

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