vf_yadif.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #include "yadif.h"
  28. #undef NDEBUG
  29. #include <assert.h>
  30. #define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
  31. #define CHECK(j)\
  32. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  33. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  34. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  35. if (score < spatial_score) {\
  36. spatial_score= score;\
  37. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  38. #define FILTER \
  39. for (x = 0; x < w; x++) { \
  40. int c = cur[mrefs]; \
  41. int d = (prev2[0] + next2[0])>>1; \
  42. int e = cur[prefs]; \
  43. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  44. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  45. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  46. int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
  47. int spatial_pred = (c+e) >> 1; \
  48. int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
  49. + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
  50. \
  51. CHECK(-1) CHECK(-2) }} }} \
  52. CHECK( 1) CHECK( 2) }} }} \
  53. \
  54. if (mode < 2) { \
  55. int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
  56. int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
  57. int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
  58. int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
  59. \
  60. diff = FFMAX3(diff, min, -max); \
  61. } \
  62. \
  63. if (spatial_pred > d + diff) \
  64. spatial_pred = d + diff; \
  65. else if (spatial_pred < d - diff) \
  66. spatial_pred = d - diff; \
  67. \
  68. dst[0] = spatial_pred; \
  69. \
  70. dst++; \
  71. cur++; \
  72. prev++; \
  73. next++; \
  74. prev2++; \
  75. next2++; \
  76. }
  77. static void filter_line_c(uint8_t *dst,
  78. uint8_t *prev, uint8_t *cur, uint8_t *next,
  79. int w, int prefs, int mrefs, int parity, int mode)
  80. {
  81. int x;
  82. uint8_t *prev2 = parity ? prev : cur ;
  83. uint8_t *next2 = parity ? cur : next;
  84. FILTER
  85. }
  86. static void filter_line_c_16bit(uint16_t *dst,
  87. uint16_t *prev, uint16_t *cur, uint16_t *next,
  88. int w, int prefs, int mrefs, int parity,
  89. int mode)
  90. {
  91. int x;
  92. uint16_t *prev2 = parity ? prev : cur ;
  93. uint16_t *next2 = parity ? cur : next;
  94. mrefs /= 2;
  95. prefs /= 2;
  96. FILTER
  97. }
  98. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  99. int parity, int tff)
  100. {
  101. YADIFContext *yadif = ctx->priv;
  102. int y, i;
  103. for (i = 0; i < yadif->csp->nb_components; i++) {
  104. int w = dstpic->video->w;
  105. int h = dstpic->video->h;
  106. int refs = yadif->cur->linesize[i];
  107. int absrefs = FFABS(refs);
  108. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  109. if (i == 1 || i == 2) {
  110. /* Why is this not part of the per-plane description thing? */
  111. w >>= yadif->csp->log2_chroma_w;
  112. h >>= yadif->csp->log2_chroma_h;
  113. }
  114. if(yadif->temp_line_size < absrefs) {
  115. av_free(yadif->temp_line);
  116. yadif->temp_line = av_mallocz(2*64 + 5*absrefs);
  117. yadif->temp_line_size = absrefs;
  118. }
  119. for (y = 0; y < h; y++) {
  120. if ((y ^ parity) & 1) {
  121. uint8_t *prev = &yadif->prev->data[i][y * refs];
  122. uint8_t *cur = &yadif->cur ->data[i][y * refs];
  123. uint8_t *next = &yadif->next->data[i][y * refs];
  124. uint8_t *dst = &dstpic->data[i][y * dstpic->linesize[i]];
  125. int mode = y == 1 || y + 2 == h ? 2 : yadif->mode;
  126. int prefs = y+1<h ? refs : -refs;
  127. int mrefs = y ?-refs : refs;
  128. if(y<=1 || y+2>=h) {
  129. uint8_t *tmp = yadif->temp_line + 64 + 2*absrefs;
  130. if(mode<2)
  131. memcpy(tmp+2*mrefs, cur+2*mrefs, w*df);
  132. memcpy(tmp+mrefs, cur+mrefs, w*df);
  133. memcpy(tmp , cur , w*df);
  134. if(prefs != mrefs) {
  135. memcpy(tmp+prefs, cur+prefs, w*df);
  136. if(mode<2)
  137. memcpy(tmp+2*prefs, cur+2*prefs, w*df);
  138. }
  139. cur = tmp;
  140. }
  141. yadif->filter_line(dst, prev, cur, next, w,
  142. prefs, mrefs,
  143. parity ^ tff, mode);
  144. } else {
  145. memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
  146. &yadif->cur->data[i][y * refs], w * df);
  147. }
  148. }
  149. }
  150. emms_c();
  151. }
  152. static int return_frame(AVFilterContext *ctx, int is_second)
  153. {
  154. YADIFContext *yadif = ctx->priv;
  155. AVFilterLink *link = ctx->outputs[0];
  156. int tff, ret;
  157. if (yadif->parity == -1) {
  158. tff = yadif->cur->video->interlaced ?
  159. yadif->cur->video->top_field_first : 1;
  160. } else {
  161. tff = yadif->parity ^ 1;
  162. }
  163. if (is_second) {
  164. yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
  165. if (!yadif->out)
  166. return AVERROR(ENOMEM);
  167. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  168. yadif->out->video->interlaced = 0;
  169. }
  170. if (!yadif->csp)
  171. yadif->csp = &av_pix_fmt_descriptors[link->format];
  172. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  173. yadif->filter_line = (void*)filter_line_c_16bit;
  174. filter(ctx, yadif->out, tff ^ !is_second, tff);
  175. if (is_second) {
  176. int64_t cur_pts = yadif->cur->pts;
  177. int64_t next_pts = yadif->next->pts;
  178. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  179. yadif->out->pts = cur_pts + next_pts;
  180. } else {
  181. yadif->out->pts = AV_NOPTS_VALUE;
  182. }
  183. ret = ff_start_frame(ctx->outputs[0], yadif->out);
  184. if (ret < 0)
  185. return ret;
  186. }
  187. if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
  188. (ret = ff_end_frame(ctx->outputs[0])) < 0)
  189. return ret;
  190. yadif->frame_pending = (yadif->mode&1) && !is_second;
  191. return 0;
  192. }
  193. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  194. {
  195. AVFilterContext *ctx = link->dst;
  196. YADIFContext *yadif = ctx->priv;
  197. av_assert0(picref);
  198. if (picref->video->h < 3 || picref->video->w < 3) {
  199. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  200. return AVERROR(EINVAL);
  201. }
  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. link->cur_buf = NULL;
  210. if (!yadif->cur)
  211. return 0;
  212. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  213. yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
  214. if (!yadif->out)
  215. return AVERROR(ENOMEM);
  216. avfilter_unref_bufferp(&yadif->prev);
  217. if (yadif->out->pts != AV_NOPTS_VALUE)
  218. yadif->out->pts *= 2;
  219. return ff_start_frame(ctx->outputs[0], yadif->out);
  220. }
  221. if (!yadif->prev &&
  222. !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
  223. return AVERROR(ENOMEM);
  224. yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
  225. link->w, link->h);
  226. if (!yadif->out)
  227. return AVERROR(ENOMEM);
  228. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  229. yadif->out->video->interlaced = 0;
  230. if (yadif->out->pts != AV_NOPTS_VALUE)
  231. yadif->out->pts *= 2;
  232. return ff_start_frame(ctx->outputs[0], yadif->out);
  233. }
  234. static int end_frame(AVFilterLink *link)
  235. {
  236. AVFilterContext *ctx = link->dst;
  237. YADIFContext *yadif = ctx->priv;
  238. if (!yadif->out)
  239. return 0;
  240. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  241. int ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  242. if (ret >= 0)
  243. ret = ff_end_frame(ctx->outputs[0]);
  244. return ret;
  245. }
  246. return_frame(ctx, 0);
  247. return 0;
  248. }
  249. static int request_frame(AVFilterLink *link)
  250. {
  251. AVFilterContext *ctx = link->src;
  252. YADIFContext *yadif = ctx->priv;
  253. if (yadif->frame_pending) {
  254. return_frame(ctx, 1);
  255. return 0;
  256. }
  257. do {
  258. int ret;
  259. if (yadif->eof)
  260. return AVERROR_EOF;
  261. ret = ff_request_frame(link->src->inputs[0]);
  262. if (ret == AVERROR_EOF && yadif->cur) {
  263. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
  264. if (!next)
  265. return AVERROR(ENOMEM);
  266. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  267. start_frame(link->src->inputs[0], next);
  268. end_frame(link->src->inputs[0]);
  269. yadif->eof = 1;
  270. } else if (ret < 0) {
  271. return ret;
  272. }
  273. } while (!yadif->cur);
  274. return 0;
  275. }
  276. static int poll_frame(AVFilterLink *link)
  277. {
  278. YADIFContext *yadif = link->src->priv;
  279. int ret, val;
  280. if (yadif->frame_pending)
  281. return 1;
  282. val = ff_poll_frame(link->src->inputs[0]);
  283. if (val <= 0)
  284. return val;
  285. //FIXME change API to not requre this red tape
  286. if (val >= 1 && !yadif->next) {
  287. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  288. return ret;
  289. val = ff_poll_frame(link->src->inputs[0]);
  290. if (val <= 0)
  291. return val;
  292. }
  293. assert(yadif->next || !val);
  294. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  295. return val;
  296. return val * ((yadif->mode&1)+1);
  297. }
  298. static av_cold void uninit(AVFilterContext *ctx)
  299. {
  300. YADIFContext *yadif = ctx->priv;
  301. if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
  302. if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
  303. if (yadif->next) avfilter_unref_bufferp(&yadif->next);
  304. av_freep(&yadif->temp_line); yadif->temp_line_size = 0;
  305. }
  306. static int query_formats(AVFilterContext *ctx)
  307. {
  308. static const enum PixelFormat pix_fmts[] = {
  309. PIX_FMT_YUV420P,
  310. PIX_FMT_YUV422P,
  311. PIX_FMT_YUV444P,
  312. PIX_FMT_YUV410P,
  313. PIX_FMT_YUV411P,
  314. PIX_FMT_GRAY8,
  315. PIX_FMT_YUVJ420P,
  316. PIX_FMT_YUVJ422P,
  317. PIX_FMT_YUVJ444P,
  318. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  319. PIX_FMT_YUV440P,
  320. PIX_FMT_YUVJ440P,
  321. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  322. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  323. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  324. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  325. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  326. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  327. PIX_FMT_YUVA420P,
  328. PIX_FMT_YUVA422P,
  329. PIX_FMT_YUVA444P,
  330. PIX_FMT_NONE
  331. };
  332. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  333. return 0;
  334. }
  335. static av_cold int init(AVFilterContext *ctx, const char *args)
  336. {
  337. YADIFContext *yadif = ctx->priv;
  338. yadif->mode = 0;
  339. yadif->parity = -1;
  340. yadif->auto_enable = 0;
  341. yadif->csp = NULL;
  342. if (args)
  343. sscanf(args, "%d:%d:%d",
  344. &yadif->mode, &yadif->parity, &yadif->auto_enable);
  345. yadif->filter_line = filter_line_c;
  346. if (HAVE_MMX)
  347. ff_yadif_init_x86(yadif);
  348. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
  349. yadif->mode, yadif->parity, yadif->auto_enable);
  350. return 0;
  351. }
  352. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  353. {
  354. return 0;
  355. }
  356. static int config_props(AVFilterLink *link)
  357. {
  358. YADIFContext *yadif = link->src->priv;
  359. link->time_base.num = link->src->inputs[0]->time_base.num;
  360. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  361. link->w = link->src->inputs[0]->w;
  362. link->h = link->src->inputs[0]->h;
  363. if(yadif->mode&1)
  364. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  365. return 0;
  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 = (const AVFilterPad[]) {{ .name = "default",
  375. .type = AVMEDIA_TYPE_VIDEO,
  376. .start_frame = start_frame,
  377. .draw_slice = null_draw_slice,
  378. .end_frame = end_frame,
  379. .min_perms = AV_PERM_PRESERVE, },
  380. { .name = NULL}},
  381. .outputs = (const AVFilterPad[]) {{ .name = "default",
  382. .type = AVMEDIA_TYPE_VIDEO,
  383. .poll_frame = poll_frame,
  384. .request_frame = request_frame,
  385. .config_props = config_props, },
  386. { .name = NULL}},
  387. };