vf_detelecine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2015 Himangi Saraogi <himangi774@gmail.com>
  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. /**
  21. * @file detelecine filter.
  22. */
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct {
  32. const AVClass *class;
  33. int first_field;
  34. char *pattern;
  35. int start_frame;
  36. unsigned int pattern_pos;
  37. unsigned int nskip_fields;
  38. int64_t start_time;
  39. AVRational pts;
  40. AVRational ts_unit;
  41. int occupied;
  42. int nb_planes;
  43. int planeheight[4];
  44. int stride[4];
  45. AVFrame *frame;
  46. AVFrame *temp;
  47. } DetelecineContext;
  48. #define OFFSET(x) offsetof(DetelecineContext, x)
  49. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  50. static const AVOption detelecine_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. {"start_frame", "position of first frame with respect to the pattern if stream is cut", OFFSET(start_frame), AV_OPT_TYPE_INT, {.i64=0}, 0, 13, FLAGS},
  58. {NULL}
  59. };
  60. AVFILTER_DEFINE_CLASS(detelecine);
  61. static av_cold int init(AVFilterContext *ctx)
  62. {
  63. DetelecineContext *s = ctx->priv;
  64. const char *p;
  65. int max = 0;
  66. if (!strlen(s->pattern)) {
  67. av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. for (p = s->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. s->pts.num += *p - '0';
  77. s->pts.den += 2;
  78. }
  79. s->nskip_fields = 0;
  80. s->pattern_pos = 0;
  81. s->start_time = AV_NOPTS_VALUE;
  82. if (s->start_frame != 0) {
  83. int nfields = 0;
  84. for (p = s->pattern; *p; p++) {
  85. nfields += *p - '0';
  86. s->pattern_pos++;
  87. if (nfields >= 2*s->start_frame) {
  88. s->nskip_fields = nfields - 2*s->start_frame;
  89. break;
  90. }
  91. }
  92. }
  93. av_log(ctx, AV_LOG_INFO, "Detelecine pattern %s removes up to %d frames per frame, pts advance factor: %d/%d\n",
  94. s->pattern, (max + 1) / 2, s->pts.num, s->pts.den);
  95. return 0;
  96. }
  97. static int query_formats(AVFilterContext *ctx)
  98. {
  99. AVFilterFormats *pix_fmts = NULL;
  100. int fmt, ret;
  101. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  102. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  103. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  104. desc->flags & AV_PIX_FMT_FLAG_PAL ||
  105. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
  106. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  107. return ret;
  108. }
  109. return ff_set_common_formats(ctx, pix_fmts);
  110. }
  111. static int config_input(AVFilterLink *inlink)
  112. {
  113. DetelecineContext *s = inlink->dst->priv;
  114. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  115. int ret;
  116. s->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
  117. if (!s->temp)
  118. return AVERROR(ENOMEM);
  119. s->frame = ff_get_video_buffer(inlink, inlink->w, inlink->h);
  120. if (!s->frame)
  121. return AVERROR(ENOMEM);
  122. if ((ret = av_image_fill_linesizes(s->stride, inlink->format, inlink->w)) < 0)
  123. return ret;
  124. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  125. s->planeheight[0] = s->planeheight[3] = inlink->h;
  126. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  127. return 0;
  128. }
  129. static int config_output(AVFilterLink *outlink)
  130. {
  131. AVFilterContext *ctx = outlink->src;
  132. DetelecineContext *s = ctx->priv;
  133. const AVFilterLink *inlink = ctx->inputs[0];
  134. AVRational fps = inlink->frame_rate;
  135. if (!fps.num || !fps.den) {
  136. av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
  137. "current rate of %d/%d is invalid\n", fps.num, fps.den);
  138. return AVERROR(EINVAL);
  139. }
  140. fps = av_mul_q(fps, av_inv_q(s->pts));
  141. av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
  142. inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
  143. outlink->frame_rate = fps;
  144. outlink->time_base = av_mul_q(inlink->time_base, s->pts);
  145. av_log(ctx, AV_LOG_VERBOSE, "TB: %d/%d -> %d/%d\n",
  146. inlink->time_base.num, inlink->time_base.den, outlink->time_base.num, outlink->time_base.den);
  147. s->ts_unit = av_inv_q(av_mul_q(fps, outlink->time_base));
  148. return 0;
  149. }
  150. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  151. {
  152. AVFilterContext *ctx = inlink->dst;
  153. AVFilterLink *outlink = ctx->outputs[0];
  154. DetelecineContext *s = ctx->priv;
  155. int i, len = 0, ret = 0, out = 0;
  156. if (s->start_time == AV_NOPTS_VALUE)
  157. s->start_time = inpicref->pts;
  158. if (s->nskip_fields >= 2) {
  159. s->nskip_fields -= 2;
  160. return 0;
  161. } else if (s->nskip_fields >= 1) {
  162. if (s->occupied) {
  163. s->occupied = 0;
  164. s->nskip_fields--;
  165. }
  166. else {
  167. for (i = 0; i < s->nb_planes; i++) {
  168. av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
  169. inpicref->data[i], inpicref->linesize[i],
  170. s->stride[i],
  171. s->planeheight[i]);
  172. }
  173. s->occupied = 1;
  174. s->nskip_fields--;
  175. return 0;
  176. }
  177. }
  178. if (s->nskip_fields == 0) {
  179. while(!len && s->pattern[s->pattern_pos]) {
  180. len = s->pattern[s->pattern_pos] - '0';
  181. s->pattern_pos++;
  182. }
  183. if (!s->pattern[s->pattern_pos])
  184. s->pattern_pos = 0;
  185. if(!len) { // do not output any field as the entire pattern is zero
  186. av_frame_free(&inpicref);
  187. return 0;
  188. }
  189. if (s->occupied) {
  190. for (i = 0; i < s->nb_planes; i++) {
  191. // fill in the EARLIER field from the new pic
  192. av_image_copy_plane(s->frame->data[i] + s->frame->linesize[i] * s->first_field,
  193. s->frame->linesize[i] * 2,
  194. inpicref->data[i] + inpicref->linesize[i] * s->first_field,
  195. inpicref->linesize[i] * 2,
  196. s->stride[i],
  197. (s->planeheight[i] - s->first_field + 1) / 2);
  198. // fill in the LATER field from the buffered pic
  199. av_image_copy_plane(s->frame->data[i] + s->frame->linesize[i] * !s->first_field,
  200. s->frame->linesize[i] * 2,
  201. s->temp->data[i] + s->temp->linesize[i] * !s->first_field,
  202. s->temp->linesize[i] * 2,
  203. s->stride[i],
  204. (s->planeheight[i] - !s->first_field + 1) / 2);
  205. }
  206. len -= 2;
  207. for (i = 0; i < s->nb_planes; i++) {
  208. av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
  209. inpicref->data[i], inpicref->linesize[i],
  210. s->stride[i],
  211. s->planeheight[i]);
  212. }
  213. s->occupied = 1;
  214. out = 1;
  215. } else {
  216. if (len >= 2) {
  217. // output THIS image as-is
  218. for (i = 0; i < s->nb_planes; i++)
  219. av_image_copy_plane(s->frame->data[i], s->frame->linesize[i],
  220. inpicref->data[i], inpicref->linesize[i],
  221. s->stride[i],
  222. s->planeheight[i]);
  223. len -= 2;
  224. out = 1;
  225. } else if (len == 1) {
  226. // fill in the EARLIER field from the new pic
  227. for (i = 0; i < s->nb_planes; i++) {
  228. av_image_copy_plane(s->frame->data[i] +
  229. s->frame->linesize[i] * s->first_field,
  230. s->frame->linesize[i] * 2,
  231. inpicref->data[i] +
  232. inpicref->linesize[i] * s->first_field,
  233. inpicref->linesize[i] * 2, s->stride[i],
  234. (s->planeheight[i] - s->first_field + 1) / 2);
  235. }
  236. // TODO: not sure about the other field
  237. len--;
  238. out = 1;
  239. }
  240. }
  241. if (len == 1 && s->occupied)
  242. {
  243. len--;
  244. s->occupied = 0;
  245. }
  246. }
  247. s->nskip_fields = len;
  248. if (out) {
  249. AVFrame *frame = av_frame_clone(s->frame);
  250. if (!frame) {
  251. av_frame_free(&inpicref);
  252. return AVERROR(ENOMEM);
  253. }
  254. av_frame_copy_props(frame, inpicref);
  255. frame->pts = ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time) +
  256. av_rescale(outlink->frame_count, s->ts_unit.num,
  257. s->ts_unit.den);
  258. ret = ff_filter_frame(outlink, frame);
  259. }
  260. av_frame_free(&inpicref);
  261. return ret;
  262. }
  263. static av_cold void uninit(AVFilterContext *ctx)
  264. {
  265. DetelecineContext *s = ctx->priv;
  266. av_frame_free(&s->temp);
  267. av_frame_free(&s->frame);
  268. }
  269. static const AVFilterPad detelecine_inputs[] = {
  270. {
  271. .name = "default",
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .filter_frame = filter_frame,
  274. .config_props = config_input,
  275. },
  276. { NULL }
  277. };
  278. static const AVFilterPad detelecine_outputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_VIDEO,
  282. .config_props = config_output,
  283. },
  284. { NULL }
  285. };
  286. AVFilter ff_vf_detelecine = {
  287. .name = "detelecine",
  288. .description = NULL_IF_CONFIG_SMALL("Apply an inverse telecine pattern."),
  289. .priv_size = sizeof(DetelecineContext),
  290. .priv_class = &detelecine_class,
  291. .init = init,
  292. .uninit = uninit,
  293. .query_formats = query_formats,
  294. .inputs = detelecine_inputs,
  295. .outputs = detelecine_outputs,
  296. };