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.

457 lines
15KB

  1. /*
  2. * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. #include "yadif.h"
  29. #undef NDEBUG
  30. #include <assert.h>
  31. #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
  32. #define CHECK(j)\
  33. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  34. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  35. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  36. if (score < spatial_score) {\
  37. spatial_score= score;\
  38. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  39. #define FILTER \
  40. for (x = 0; x < w; x++) { \
  41. int c = cur[mrefs]; \
  42. int d = (prev2[0] + next2[0])>>1; \
  43. int e = cur[prefs]; \
  44. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  45. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  46. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  47. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  48. int spatial_pred = (c+e) >> 1; \
  49. int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
  50. + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
  51. \
  52. CHECK(-1) CHECK(-2) }} }} \
  53. CHECK( 1) CHECK( 2) }} }} \
  54. \
  55. if (mode < 2) { \
  56. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  57. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  58. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  59. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  60. \
  61. diff = FFMAX3(diff, min, -max); \
  62. } \
  63. \
  64. if (spatial_pred > d + diff) \
  65. spatial_pred = d + diff; \
  66. else if (spatial_pred < d - diff) \
  67. spatial_pred = d - diff; \
  68. \
  69. dst[0] = spatial_pred; \
  70. \
  71. dst++; \
  72. cur++; \
  73. prev++; \
  74. next++; \
  75. prev2++; \
  76. next2++; \
  77. }
  78. static void filter_line_c(void *dst1,
  79. void *prev1, void *cur1, void *next1,
  80. int w, int prefs, int mrefs, int parity, int mode)
  81. {
  82. uint8_t *dst = dst1;
  83. uint8_t *prev = prev1;
  84. uint8_t *cur = cur1;
  85. uint8_t *next = next1;
  86. int x;
  87. uint8_t *prev2 = parity ? prev : cur ;
  88. uint8_t *next2 = parity ? cur : next;
  89. FILTER
  90. }
  91. static void filter_line_c_16bit(void *dst1,
  92. void *prev1, void *cur1, void *next1,
  93. int w, int prefs, int mrefs, int parity,
  94. int mode)
  95. {
  96. uint16_t *dst = dst1;
  97. uint16_t *prev = prev1;
  98. uint16_t *cur = cur1;
  99. uint16_t *next = next1;
  100. int x;
  101. uint16_t *prev2 = parity ? prev : cur ;
  102. uint16_t *next2 = parity ? cur : next;
  103. mrefs /= 2;
  104. prefs /= 2;
  105. FILTER
  106. }
  107. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  108. int parity, int tff)
  109. {
  110. YADIFContext *yadif = ctx->priv;
  111. int y, i;
  112. for (i = 0; i < yadif->csp->nb_components; i++) {
  113. int w = dstpic->video->w;
  114. int h = dstpic->video->h;
  115. int refs = yadif->cur->linesize[i];
  116. int absrefs = FFABS(refs);
  117. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  118. if (i == 1 || i == 2) {
  119. /* Why is this not part of the per-plane description thing? */
  120. w >>= yadif->csp->log2_chroma_w;
  121. h >>= yadif->csp->log2_chroma_h;
  122. }
  123. if(yadif->temp_line_size < absrefs) {
  124. av_free(yadif->temp_line);
  125. yadif->temp_line = av_mallocz(2*64 + 5*absrefs);
  126. yadif->temp_line_size = absrefs;
  127. }
  128. for (y = 0; y < h; y++) {
  129. if ((y ^ parity) & 1) {
  130. uint8_t *prev = &yadif->prev->data[i][y * refs];
  131. uint8_t *cur = &yadif->cur ->data[i][y * refs];
  132. uint8_t *next = &yadif->next->data[i][y * refs];
  133. uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
  134. int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
  135. int prefs = y+1<h ? refs : -refs;
  136. int mrefs = y ?-refs : refs;
  137. if(y<=1 || y+2>=h) {
  138. uint8_t *tmp = yadif->temp_line + 64 + 2*absrefs;
  139. if(mode<2)
  140. memcpy(tmp+2*mrefs, cur+2*mrefs, w*df);
  141. memcpy(tmp+mrefs, cur+mrefs, w*df);
  142. memcpy(tmp , cur , w*df);
  143. if(prefs != mrefs) {
  144. memcpy(tmp+prefs, cur+prefs, w*df);
  145. if(mode<2)
  146. memcpy(tmp+2*prefs, cur+2*prefs, w*df);
  147. }
  148. cur = tmp;
  149. }
  150. yadif->filter_line(dst, prev, cur, next, w,
  151. prefs, mrefs,
  152. parity ^ tff, mode);
  153. } else {
  154. memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
  155. &yadif->cur->data[i][y * refs], w * df);
  156. }
  157. }
  158. }
  159. emms_c();
  160. }
  161. static int return_frame(AVFilterContext *ctx, int is_second)
  162. {
  163. YADIFContext *yadif = ctx->priv;
  164. AVFilterLink *link = ctx->outputs[0];
  165. int tff, ret;
  166. if (yadif->parity == -1) {
  167. tff = yadif->cur->video->interlaced ?
  168. yadif->cur->video->top_field_first : 1;
  169. } else {
  170. tff = yadif->parity ^ 1;
  171. }
  172. if (is_second) {
  173. yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
  174. if (!yadif->out)
  175. return AVERROR(ENOMEM);
  176. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  177. yadif->out->video->interlaced = 0;
  178. }
  179. if (!yadif->csp)
  180. yadif->csp = av_pix_fmt_desc_get(link->format);
  181. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  182. yadif->filter_line = (void*)filter_line_c_16bit;
  183. filter(ctx, yadif->out, tff ^ !is_second, tff);
  184. if (is_second) {
  185. int64_t cur_pts = yadif->cur->pts;
  186. int64_t next_pts = yadif->next->pts;
  187. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  188. yadif->out->pts = cur_pts + next_pts;
  189. } else {
  190. yadif->out->pts = AV_NOPTS_VALUE;
  191. }
  192. }
  193. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  194. yadif->frame_pending = (yadif->mode&1) && !is_second;
  195. return ret;
  196. }
  197. static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  198. {
  199. AVFilterContext *ctx = link->dst;
  200. YADIFContext *yadif = ctx->priv;
  201. av_assert0(picref);
  202. if (yadif->frame_pending)
  203. return_frame(ctx, 1);
  204. if (yadif->prev)
  205. avfilter_unref_buffer(yadif->prev);
  206. yadif->prev = yadif->cur;
  207. yadif->cur = yadif->next;
  208. yadif->next = picref;
  209. if (!yadif->cur)
  210. return 0;
  211. if (yadif->deint && !yadif->cur->video->interlaced) {
  212. yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
  213. if (!yadif->out)
  214. return AVERROR(ENOMEM);
  215. avfilter_unref_bufferp(&yadif->prev);
  216. if (yadif->out->pts != AV_NOPTS_VALUE)
  217. yadif->out->pts *= 2;
  218. return ff_filter_frame(ctx->outputs[0], yadif->out);
  219. }
  220. if (!yadif->prev &&
  221. !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
  222. return AVERROR(ENOMEM);
  223. yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
  224. link->w, link->h);
  225. if (!yadif->out)
  226. return AVERROR(ENOMEM);
  227. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  228. yadif->out->video->interlaced = 0;
  229. if (yadif->out->pts != AV_NOPTS_VALUE)
  230. yadif->out->pts *= 2;
  231. return return_frame(ctx, 0);
  232. }
  233. static int request_frame(AVFilterLink *link)
  234. {
  235. AVFilterContext *ctx = link->src;
  236. YADIFContext *yadif = ctx->priv;
  237. if (yadif->frame_pending) {
  238. return_frame(ctx, 1);
  239. return 0;
  240. }
  241. do {
  242. int ret;
  243. if (yadif->eof)
  244. return AVERROR_EOF;
  245. ret = ff_request_frame(link->src->inputs[0]);
  246. if (ret == AVERROR_EOF && yadif->cur) {
  247. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
  248. if (!next)
  249. return AVERROR(ENOMEM);
  250. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  251. filter_frame(link->src->inputs[0], next);
  252. yadif->eof = 1;
  253. } else if (ret < 0) {
  254. return ret;
  255. }
  256. } while (!yadif->cur);
  257. return 0;
  258. }
  259. #define OFFSET(x) offsetof(YADIFContext, x)
  260. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  261. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  262. static const AVOption yadif_options[] = {
  263. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
  264. CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
  265. CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
  266. CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
  267. CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
  268. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
  269. CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
  270. CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
  271. CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
  272. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
  273. CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
  274. CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
  275. {NULL},
  276. };
  277. AVFILTER_DEFINE_CLASS(yadif);
  278. static av_cold void uninit(AVFilterContext *ctx)
  279. {
  280. YADIFContext *yadif = ctx->priv;
  281. avfilter_unref_bufferp(&yadif->prev);
  282. avfilter_unref_bufferp(&yadif->cur );
  283. avfilter_unref_bufferp(&yadif->next);
  284. av_freep(&yadif->temp_line); yadif->temp_line_size = 0;
  285. av_opt_free(yadif);
  286. }
  287. static int query_formats(AVFilterContext *ctx)
  288. {
  289. static const enum AVPixelFormat pix_fmts[] = {
  290. AV_PIX_FMT_YUV420P,
  291. AV_PIX_FMT_YUV422P,
  292. AV_PIX_FMT_YUV444P,
  293. AV_PIX_FMT_YUV410P,
  294. AV_PIX_FMT_YUV411P,
  295. AV_PIX_FMT_GRAY8,
  296. AV_PIX_FMT_YUVJ420P,
  297. AV_PIX_FMT_YUVJ422P,
  298. AV_PIX_FMT_YUVJ444P,
  299. AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
  300. AV_PIX_FMT_YUV440P,
  301. AV_PIX_FMT_YUVJ440P,
  302. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  303. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  304. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  305. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  306. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  307. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  308. AV_PIX_FMT_YUVA420P,
  309. AV_PIX_FMT_YUVA422P,
  310. AV_PIX_FMT_YUVA444P,
  311. AV_PIX_FMT_NONE
  312. };
  313. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  314. return 0;
  315. }
  316. static av_cold int init(AVFilterContext *ctx, const char *args)
  317. {
  318. YADIFContext *yadif = ctx->priv;
  319. static const char *shorthand[] = { "mode", "parity", "deint", NULL };
  320. int ret;
  321. yadif->csp = NULL;
  322. yadif->class = &yadif_class;
  323. av_opt_set_defaults(yadif);
  324. if ((ret = av_opt_set_from_string(yadif, args, shorthand, "=", ":")) < 0)
  325. return ret;
  326. yadif->filter_line = filter_line_c;
  327. if (ARCH_X86)
  328. ff_yadif_init_x86(yadif);
  329. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d deint:%d\n",
  330. yadif->mode, yadif->parity, yadif->deint);
  331. return 0;
  332. }
  333. static int config_props(AVFilterLink *link)
  334. {
  335. AVFilterContext *ctx = link->src;
  336. YADIFContext *yadif = ctx->priv;
  337. link->time_base.num = link->src->inputs[0]->time_base.num;
  338. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  339. link->w = link->src->inputs[0]->w;
  340. link->h = link->src->inputs[0]->h;
  341. if(yadif->mode&1)
  342. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  343. if (link->w < 3 || link->h < 3) {
  344. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  345. return AVERROR(EINVAL);
  346. }
  347. return 0;
  348. }
  349. static const AVFilterPad avfilter_vf_yadif_inputs[] = {
  350. {
  351. .name = "default",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. .filter_frame = filter_frame,
  354. .min_perms = AV_PERM_PRESERVE,
  355. },
  356. { NULL }
  357. };
  358. static const AVFilterPad avfilter_vf_yadif_outputs[] = {
  359. {
  360. .name = "default",
  361. .type = AVMEDIA_TYPE_VIDEO,
  362. .request_frame = request_frame,
  363. .config_props = config_props,
  364. },
  365. { NULL }
  366. };
  367. AVFilter avfilter_vf_yadif = {
  368. .name = "yadif",
  369. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  370. .priv_size = sizeof(YADIFContext),
  371. .init = init,
  372. .uninit = uninit,
  373. .query_formats = query_formats,
  374. .inputs = avfilter_vf_yadif_inputs,
  375. .outputs = avfilter_vf_yadif_outputs,
  376. .priv_class = &yadif_class,
  377. };