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.

283 lines
8.9KB

  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. int nb_planes;
  43. int planeheight[4];
  44. int stride[4];
  45. AVFrame *frame[5];
  46. AVFrame *temp;
  47. } TelecineContext;
  48. #define OFFSET(x) offsetof(TelecineContext, x)
  49. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  50. static const AVOption telecine_options[] = {
  51. {"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
  52. {"top", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  53. {"t", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
  54. {"bottom", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  55. {"b", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
  56. {"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},
  57. {NULL}
  58. };
  59. AVFILTER_DEFINE_CLASS(telecine);
  60. static av_cold int init(AVFilterContext *ctx)
  61. {
  62. TelecineContext *tc = ctx->priv;
  63. const char *p;
  64. int max = 0;
  65. if (!strlen(tc->pattern)) {
  66. av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
  67. return AVERROR_INVALIDDATA;
  68. }
  69. for (p = tc->pattern; *p; p++) {
  70. if (!av_isdigit(*p)) {
  71. av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
  72. return AVERROR_INVALIDDATA;
  73. }
  74. max = FFMAX(*p - '0', max);
  75. tc->pts.num += 2;
  76. tc->pts.den += *p - '0';
  77. }
  78. tc->out_cnt = (max + 1) / 2;
  79. av_log(ctx, AV_LOG_INFO, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
  80. tc->pattern, tc->out_cnt, tc->pts.num, tc->pts.den);
  81. return 0;
  82. }
  83. static int query_formats(AVFilterContext *ctx)
  84. {
  85. AVFilterFormats *pix_fmts = NULL;
  86. int fmt;
  87. for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
  88. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  89. if (!(desc->flags & PIX_FMT_HWACCEL))
  90. ff_add_format(&pix_fmts, fmt);
  91. }
  92. ff_set_common_formats(ctx, pix_fmts);
  93. return 0;
  94. }
  95. static int config_input(AVFilterLink *inlink)
  96. {
  97. TelecineContext *tc = inlink->dst->priv;
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  99. int i, ret;
  100. tc->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
  101. if (!tc->temp)
  102. return AVERROR(ENOMEM);
  103. for (i = 0; i < tc->out_cnt; i++) {
  104. tc->frame[i] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
  105. if (!tc->frame[i])
  106. return AVERROR(ENOMEM);
  107. }
  108. if ((ret = av_image_fill_linesizes(tc->stride, inlink->format, inlink->w)) < 0)
  109. return ret;
  110. tc->planeheight[1] = tc->planeheight[2] = inlink->h >> desc->log2_chroma_h;
  111. tc->planeheight[0] = tc->planeheight[3] = inlink->h;
  112. tc->nb_planes = av_pix_fmt_count_planes(inlink->format);
  113. return 0;
  114. }
  115. static int config_output(AVFilterLink *outlink)
  116. {
  117. AVFilterContext *ctx = outlink->src;
  118. TelecineContext *tc = ctx->priv;
  119. const AVFilterLink *inlink = ctx->inputs[0];
  120. AVRational fps = inlink->frame_rate;
  121. if (!fps.num || !fps.den) {
  122. av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
  123. "current rate of %d/%d is invalid\n", fps.num, fps.den);
  124. return AVERROR(EINVAL);
  125. }
  126. fps = av_mul_q(fps, av_inv_q(tc->pts));
  127. av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
  128. inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
  129. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  130. outlink->frame_rate = fps;
  131. outlink->time_base = av_mul_q(inlink->time_base, tc->pts);
  132. tc->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
  133. return 0;
  134. }
  135. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  136. {
  137. AVFilterContext *ctx = inlink->dst;
  138. AVFilterLink *outlink = ctx->outputs[0];
  139. TelecineContext *tc = ctx->priv;
  140. int i, len, ret = 0, nout = 0;
  141. len = tc->pattern[tc->pattern_pos] - '0';
  142. tc->pattern_pos++;
  143. if (!tc->pattern[tc->pattern_pos])
  144. tc->pattern_pos = 0;
  145. if (!len) { // do not output any field from this frame
  146. av_frame_free(&inpicref);
  147. return 0;
  148. }
  149. if (tc->occupied) {
  150. for (i = 0; i < tc->nb_planes; i++) {
  151. // fill in the EARLIER field from the buffered pic
  152. av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * tc->first_field,
  153. tc->frame[nout]->linesize[i] * 2,
  154. tc->temp->data[i] + tc->temp->linesize[i] * tc->first_field,
  155. tc->temp->linesize[i] * 2,
  156. tc->stride[i],
  157. (tc->planeheight[i] - tc->first_field + 1) / 2);
  158. // fill in the LATER field from the new pic
  159. av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * !tc->first_field,
  160. tc->frame[nout]->linesize[i] * 2,
  161. inpicref->data[i] + inpicref->linesize[i] * !tc->first_field,
  162. inpicref->linesize[i] * 2,
  163. tc->stride[i],
  164. (tc->planeheight[i] - !tc->first_field + 1) / 2);
  165. }
  166. nout++;
  167. len--;
  168. tc->occupied = 0;
  169. }
  170. while (len >= 2) {
  171. // output THIS image as-is
  172. for (i = 0; i < tc->nb_planes; i++)
  173. av_image_copy_plane(tc->frame[nout]->data[i], tc->frame[nout]->linesize[i],
  174. inpicref->data[i], inpicref->linesize[i],
  175. tc->stride[i],
  176. tc->planeheight[i]);
  177. nout++;
  178. len -= 2;
  179. }
  180. if (len >= 1) {
  181. // copy THIS image to the buffer, we need it later
  182. for (i = 0; i < tc->nb_planes; i++)
  183. av_image_copy_plane(tc->temp->data[i], tc->temp->linesize[i],
  184. inpicref->data[i], inpicref->linesize[i],
  185. tc->stride[i],
  186. tc->planeheight[i]);
  187. tc->occupied = 1;
  188. }
  189. for (i = 0; i < nout; i++) {
  190. AVFrame *frame = av_frame_clone(tc->frame[i]);
  191. if (!frame) {
  192. av_frame_free(&inpicref);
  193. return AVERROR(ENOMEM);
  194. }
  195. av_frame_copy_props(frame, inpicref);
  196. frame->pts = outlink->frame_count * tc->ts_unit;
  197. ret = ff_filter_frame(outlink, frame);
  198. }
  199. av_frame_free(&inpicref);
  200. return ret;
  201. }
  202. static av_cold void uninit(AVFilterContext *ctx)
  203. {
  204. TelecineContext *tc = ctx->priv;
  205. int i;
  206. av_frame_free(&tc->temp);
  207. for (i = 0; i < tc->out_cnt; i++)
  208. av_frame_free(&tc->frame[i]);
  209. }
  210. static const AVFilterPad telecine_inputs[] = {
  211. {
  212. .name = "default",
  213. .type = AVMEDIA_TYPE_VIDEO,
  214. .filter_frame = filter_frame,
  215. .config_props = config_input,
  216. },
  217. { NULL }
  218. };
  219. static const AVFilterPad telecine_outputs[] = {
  220. {
  221. .name = "default",
  222. .type = AVMEDIA_TYPE_VIDEO,
  223. .config_props = config_output,
  224. },
  225. { NULL }
  226. };
  227. AVFilter avfilter_vf_telecine = {
  228. .name = "telecine",
  229. .description = NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
  230. .priv_size = sizeof(TelecineContext),
  231. .priv_class = &telecine_class,
  232. .init = init,
  233. .uninit = uninit,
  234. .query_formats = query_formats,
  235. .inputs = telecine_inputs,
  236. .outputs = telecine_outputs,
  237. };