vf_detelecine.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 DetelecineContext {
  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. av_frame_free(&inpicref);
  172. return 0;
  173. } else if (s->nskip_fields >= 1) {
  174. for (i = 0; i < s->nb_planes; i++) {
  175. av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
  176. inpicref->data[i], inpicref->linesize[i],
  177. s->stride[i],
  178. s->planeheight[i]);
  179. }
  180. s->occupied = 1;
  181. s->nskip_fields--;
  182. av_frame_free(&inpicref);
  183. return 0;
  184. }
  185. if (s->nskip_fields == 0) {
  186. len = s->init_len;
  187. s->init_len = 0;
  188. while(!len && s->pattern[s->pattern_pos]) {
  189. len = s->pattern[s->pattern_pos] - '0';
  190. s->pattern_pos++;
  191. }
  192. if (!s->pattern[s->pattern_pos])
  193. s->pattern_pos = 0;
  194. if(!len) { // do not output any field as the entire pattern is zero
  195. av_frame_free(&inpicref);
  196. return 0;
  197. }
  198. if (len == 1 && s->occupied) {
  199. s->occupied = 0;
  200. // output THIS image as-is
  201. for (i = 0; i < s->nb_planes; i++)
  202. av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
  203. s->temp->data[i], s->temp->linesize[i],
  204. s->stride[i],
  205. s->planeheight[i]);
  206. len = 0;
  207. while(!len && s->pattern[s->pattern_pos]) {
  208. len = s->pattern[s->pattern_pos] - '0';
  209. s->pattern_pos++;
  210. }
  211. if (!s->pattern[s->pattern_pos])
  212. s->pattern_pos = 0;
  213. s->occupied = 0;
  214. ++out;
  215. }
  216. if (s->occupied) {
  217. for (i = 0; i < s->nb_planes; i++) {
  218. // fill in the EARLIER field from the new pic
  219. av_image_copy_plane(s->frame[out]->data[i] + s->frame[out]->linesize[i] * s->first_field,
  220. s->frame[out]->linesize[i] * 2,
  221. inpicref->data[i] + inpicref->linesize[i] * s->first_field,
  222. inpicref->linesize[i] * 2,
  223. s->stride[i],
  224. (s->planeheight[i] - s->first_field + 1) / 2);
  225. // fill in the LATER field from the buffered pic
  226. av_image_copy_plane(s->frame[out]->data[i] + s->frame[out]->linesize[i] * !s->first_field,
  227. s->frame[out]->linesize[i] * 2,
  228. s->temp->data[i] + s->temp->linesize[i] * !s->first_field,
  229. s->temp->linesize[i] * 2,
  230. s->stride[i],
  231. (s->planeheight[i] - !s->first_field + 1) / 2);
  232. }
  233. s->occupied = 0;
  234. if (len <= 2) {
  235. for (i = 0; i < s->nb_planes; i++) {
  236. av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
  237. inpicref->data[i], inpicref->linesize[i],
  238. s->stride[i],
  239. s->planeheight[i]);
  240. }
  241. s->occupied = 1;
  242. }
  243. ++out;
  244. len = (len >= 3) ? len - 3 : 0;
  245. } else {
  246. if (len >= 2) {
  247. // output THIS image as-is
  248. for (i = 0; i < s->nb_planes; i++)
  249. av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
  250. inpicref->data[i], inpicref->linesize[i],
  251. s->stride[i],
  252. s->planeheight[i]);
  253. len -= 2;
  254. ++out;
  255. } else if (len == 1) {
  256. // output THIS image as-is
  257. for (i = 0; i < s->nb_planes; i++)
  258. av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
  259. inpicref->data[i], inpicref->linesize[i],
  260. s->stride[i],
  261. s->planeheight[i]);
  262. for (i = 0; i < s->nb_planes; i++) {
  263. av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
  264. inpicref->data[i], inpicref->linesize[i],
  265. s->stride[i],
  266. s->planeheight[i]);
  267. }
  268. s->occupied = 1;
  269. len--;
  270. ++out;
  271. }
  272. }
  273. if (len == 1 && s->occupied)
  274. {
  275. len--;
  276. s->occupied = 0;
  277. }
  278. }
  279. s->nskip_fields = len;
  280. for (i = 0; i < out; ++i) {
  281. AVFrame *frame = av_frame_clone(s->frame[i]);
  282. if (!frame) {
  283. av_frame_free(&inpicref);
  284. return AVERROR(ENOMEM);
  285. }
  286. av_frame_copy_props(frame, inpicref);
  287. frame->pts = ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time) +
  288. av_rescale(outlink->frame_count_in, s->ts_unit.num,
  289. s->ts_unit.den);
  290. ret = ff_filter_frame(outlink, frame);
  291. }
  292. av_frame_free(&inpicref);
  293. return ret;
  294. }
  295. static av_cold void uninit(AVFilterContext *ctx)
  296. {
  297. DetelecineContext *s = ctx->priv;
  298. av_frame_free(&s->temp);
  299. av_frame_free(&s->frame[0]);
  300. av_frame_free(&s->frame[1]);
  301. }
  302. static const AVFilterPad detelecine_inputs[] = {
  303. {
  304. .name = "default",
  305. .type = AVMEDIA_TYPE_VIDEO,
  306. .filter_frame = filter_frame,
  307. .config_props = config_input,
  308. },
  309. { NULL }
  310. };
  311. static const AVFilterPad detelecine_outputs[] = {
  312. {
  313. .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .config_props = config_output,
  316. },
  317. { NULL }
  318. };
  319. AVFilter ff_vf_detelecine = {
  320. .name = "detelecine",
  321. .description = NULL_IF_CONFIG_SMALL("Apply an inverse telecine pattern."),
  322. .priv_size = sizeof(DetelecineContext),
  323. .priv_class = &detelecine_class,
  324. .init = init,
  325. .uninit = uninit,
  326. .query_formats = query_formats,
  327. .inputs = detelecine_inputs,
  328. .outputs = detelecine_outputs,
  329. };