vf_detelecine.c 13 KB

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