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.

284 lines
9.0KB

  1. /*
  2. * Copyright (c) 2012 Rudolf Polzer
  3. * Copyright (c) 2013 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file telecine filter, heavily based from mpv-player:TOOLS/vf_dlopen/telecine.c by
  23. * Rudolf Polzer.
  24. */
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. typedef struct {
  34. const AVClass *class;
  35. int first_field;
  36. char *pattern;
  37. unsigned int pattern_pos;
  38. AVRational pts;
  39. double ts_unit;
  40. int out_cnt;
  41. int occupied;
  42. int64_t frame_count;
  43. int nb_planes;
  44. int planeheight[4];
  45. int stride[4];
  46. AVFrame *frame[5];
  47. AVFrame *temp;
  48. } TelecineContext;
  49. #define OFFSET(x) offsetof(TelecineContext, x)
  50. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  51. static const AVOption telecine_options[] = {
  52. {"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
  53. {"top", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  54. {"t", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  55. {"bottom", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  56. {"b", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  57. {"pattern", "pattern that describe for how many fields a frame is to be displayed", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str="23"}, 0, 0, FLAGS},
  58. {NULL}
  59. };
  60. AVFILTER_DEFINE_CLASS(telecine);
  61. static av_cold int init(AVFilterContext *ctx, const char *args)
  62. {
  63. TelecineContext *tc = ctx->priv;
  64. const char *p;
  65. int max = 0;
  66. if (!strlen(tc->pattern)) {
  67. av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. for (p = tc->pattern; *p; p++) {
  71. if (!av_isdigit(*p)) {
  72. av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
  73. return AVERROR_INVALIDDATA;
  74. }
  75. max = FFMAX(*p - '0', max);
  76. tc->pts.num += 2;
  77. tc->pts.den += *p - '0';
  78. }
  79. tc->out_cnt = (max + 1) / 2;
  80. av_log(ctx, AV_LOG_INFO, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
  81. tc->pattern, tc->out_cnt, tc->pts.num, tc->pts.den);
  82. return 0;
  83. }
  84. static int query_formats(AVFilterContext *ctx)
  85. {
  86. AVFilterFormats *pix_fmts = NULL;
  87. int fmt;
  88. for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
  89. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  90. if (!(desc->flags & PIX_FMT_HWACCEL))
  91. ff_add_format(&pix_fmts, fmt);
  92. }
  93. ff_set_common_formats(ctx, pix_fmts);
  94. return 0;
  95. }
  96. static int config_input(AVFilterLink *inlink)
  97. {
  98. TelecineContext *tc = inlink->dst->priv;
  99. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  100. int i, ret;
  101. tc->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
  102. if (!tc->temp)
  103. return AVERROR(ENOMEM);
  104. for (i = 0; i < tc->out_cnt; i++) {
  105. tc->frame[i] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
  106. if (!tc->frame[i])
  107. return AVERROR(ENOMEM);
  108. }
  109. if ((ret = av_image_fill_linesizes(tc->stride, inlink->format, inlink->w)) < 0)
  110. return ret;
  111. tc->planeheight[1] = tc->planeheight[2] = inlink->h >> desc->log2_chroma_h;
  112. tc->planeheight[0] = tc->planeheight[3] = inlink->h;
  113. tc->nb_planes = av_pix_fmt_count_planes(inlink->format);
  114. return 0;
  115. }
  116. static int config_output(AVFilterLink *outlink)
  117. {
  118. AVFilterContext *ctx = outlink->src;
  119. TelecineContext *tc = ctx->priv;
  120. const AVFilterLink *inlink = ctx->inputs[0];
  121. AVRational fps = inlink->frame_rate;
  122. if (!fps.num || !fps.den) {
  123. av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
  124. "current rate of %d/%d is invalid\n", fps.num, fps.den);
  125. return AVERROR(EINVAL);
  126. }
  127. fps = av_mul_q(fps, av_inv_q(tc->pts));
  128. av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
  129. inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
  130. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  131. outlink->frame_rate = fps;
  132. outlink->time_base = av_mul_q(inlink->time_base, tc->pts);
  133. tc->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
  134. return 0;
  135. }
  136. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  137. {
  138. AVFilterContext *ctx = inlink->dst;
  139. AVFilterLink *outlink = ctx->outputs[0];
  140. TelecineContext *tc = ctx->priv;
  141. int i, len, ret = 0, nout = 0;
  142. len = tc->pattern[tc->pattern_pos] - '0';
  143. tc->pattern_pos++;
  144. if (!tc->pattern[tc->pattern_pos])
  145. tc->pattern_pos = 0;
  146. if (!len) { // do not output any field from this frame
  147. av_frame_free(&inpicref);
  148. return 0;
  149. }
  150. if (tc->occupied) {
  151. for (i = 0; i < tc->nb_planes; i++) {
  152. // fill in the EARLIER field from the buffered pic
  153. av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * tc->first_field,
  154. tc->frame[nout]->linesize[i] * 2,
  155. tc->temp->data[i] + tc->temp->linesize[i] * tc->first_field,
  156. tc->temp->linesize[i] * 2,
  157. tc->stride[i],
  158. (tc->planeheight[i] - tc->first_field + 1) / 2);
  159. // fill in the LATER field from the new pic
  160. av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * !tc->first_field,
  161. tc->frame[nout]->linesize[i] * 2,
  162. inpicref->data[i] + inpicref->linesize[i] * !tc->first_field,
  163. inpicref->linesize[i] * 2,
  164. tc->stride[i],
  165. (tc->planeheight[i] - !tc->first_field + 1) / 2);
  166. }
  167. nout++;
  168. len--;
  169. tc->occupied = 0;
  170. }
  171. while (len >= 2) {
  172. // output THIS image as-is
  173. for (i = 0; i < tc->nb_planes; i++)
  174. av_image_copy_plane(tc->frame[nout]->data[i], tc->frame[nout]->linesize[i],
  175. inpicref->data[i], inpicref->linesize[i],
  176. tc->stride[i],
  177. tc->planeheight[i]);
  178. nout++;
  179. len -= 2;
  180. }
  181. if (len >= 1) {
  182. // copy THIS image to the buffer, we need it later
  183. for (i = 0; i < tc->nb_planes; i++)
  184. av_image_copy_plane(tc->temp->data[i], tc->temp->linesize[i],
  185. inpicref->data[i], inpicref->linesize[i],
  186. tc->stride[i],
  187. tc->planeheight[i]);
  188. tc->occupied = 1;
  189. }
  190. for (i = 0; i < nout; i++) {
  191. AVFrame *frame = av_frame_clone(tc->frame[i]);
  192. if (!frame) {
  193. av_frame_free(&inpicref);
  194. return AVERROR(ENOMEM);
  195. }
  196. av_frame_copy_props(frame, inpicref);
  197. frame->pts = tc->frame_count++ * tc->ts_unit;
  198. ret = ff_filter_frame(outlink, frame);
  199. }
  200. av_frame_free(&inpicref);
  201. return ret;
  202. }
  203. static av_cold void uninit(AVFilterContext *ctx)
  204. {
  205. TelecineContext *tc = ctx->priv;
  206. int i;
  207. av_frame_free(&tc->temp);
  208. for (i = 0; i < tc->out_cnt; i++)
  209. av_frame_free(&tc->frame[i]);
  210. }
  211. static const AVFilterPad telecine_inputs[] = {
  212. {
  213. .name = "default",
  214. .type = AVMEDIA_TYPE_VIDEO,
  215. .filter_frame = filter_frame,
  216. .config_props = config_input,
  217. },
  218. { NULL }
  219. };
  220. static const AVFilterPad telecine_outputs[] = {
  221. {
  222. .name = "default",
  223. .type = AVMEDIA_TYPE_VIDEO,
  224. .config_props = config_output,
  225. },
  226. { NULL }
  227. };
  228. AVFilter avfilter_vf_telecine = {
  229. .name = "telecine",
  230. .description = NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
  231. .priv_size = sizeof(TelecineContext),
  232. .priv_class = &telecine_class,
  233. .init = init,
  234. .uninit = uninit,
  235. .query_formats = query_formats,
  236. .inputs = telecine_inputs,
  237. .outputs = telecine_outputs,
  238. };