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.

281 lines
8.3KB

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