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.

418 lines
13KB

  1. /*
  2. * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with Libav; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/common.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 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, int mode)
  89. {
  90. int x;
  91. uint16_t *prev2 = parity ? prev : cur ;
  92. uint16_t *next2 = parity ? cur : next;
  93. mrefs /= 2;
  94. prefs /= 2;
  95. FILTER
  96. }
  97. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  98. int parity, int tff)
  99. {
  100. YADIFContext *yadif = ctx->priv;
  101. int y, i;
  102. for (i = 0; i < yadif->csp->nb_components; i++) {
  103. int w = dstpic->video->w;
  104. int h = dstpic->video->h;
  105. int refs = yadif->cur->linesize[i];
  106. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  107. if (i == 1 || i == 2) {
  108. /* Why is this not part of the per-plane description thing? */
  109. w >>= yadif->csp->log2_chroma_w;
  110. h >>= yadif->csp->log2_chroma_h;
  111. }
  112. for (y = 0; y < h; y++) {
  113. if ((y ^ parity) & 1) {
  114. uint8_t *prev = &yadif->prev->data[i][y*refs];
  115. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  116. uint8_t *next = &yadif->next->data[i][y*refs];
  117. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  118. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  119. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  120. } else {
  121. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  122. &yadif->cur->data[i][y*refs], w*df);
  123. }
  124. }
  125. }
  126. #if HAVE_MMX
  127. __asm__ volatile("emms \n\t" : : : "memory");
  128. #endif
  129. }
  130. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  131. {
  132. AVFilterBufferRef *picref;
  133. int width = FFALIGN(w, 32);
  134. int height= FFALIGN(h+2, 32);
  135. int i;
  136. picref = ff_default_get_video_buffer(link, perms, width, height);
  137. picref->video->w = w;
  138. picref->video->h = h;
  139. for (i = 0; i < 3; i++)
  140. picref->data[i] += picref->linesize[i];
  141. return picref;
  142. }
  143. static void return_frame(AVFilterContext *ctx, int is_second)
  144. {
  145. YADIFContext *yadif = ctx->priv;
  146. AVFilterLink *link= ctx->outputs[0];
  147. int tff;
  148. if (yadif->parity == -1) {
  149. tff = yadif->cur->video->interlaced ?
  150. yadif->cur->video->top_field_first : 1;
  151. } else {
  152. tff = yadif->parity^1;
  153. }
  154. if (is_second) {
  155. yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  156. AV_PERM_REUSE, link->w, link->h);
  157. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  158. yadif->out->video->interlaced = 0;
  159. }
  160. if (!yadif->csp)
  161. yadif->csp = &av_pix_fmt_descriptors[link->format];
  162. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  163. yadif->filter_line = filter_line_c_16bit;
  164. filter(ctx, yadif->out, tff ^ !is_second, tff);
  165. if (is_second) {
  166. int64_t cur_pts = yadif->cur->pts;
  167. int64_t next_pts = yadif->next->pts;
  168. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  169. yadif->out->pts = cur_pts + next_pts;
  170. } else {
  171. yadif->out->pts = AV_NOPTS_VALUE;
  172. }
  173. ff_start_frame(ctx->outputs[0], yadif->out);
  174. }
  175. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  176. ff_end_frame(ctx->outputs[0]);
  177. yadif->frame_pending = (yadif->mode&1) && !is_second;
  178. }
  179. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  180. {
  181. AVFilterContext *ctx = link->dst;
  182. YADIFContext *yadif = ctx->priv;
  183. if (yadif->frame_pending)
  184. return_frame(ctx, 1);
  185. if (yadif->prev)
  186. avfilter_unref_buffer(yadif->prev);
  187. yadif->prev = yadif->cur;
  188. yadif->cur = yadif->next;
  189. yadif->next = picref;
  190. if (!yadif->cur)
  191. return;
  192. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  193. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  194. avfilter_unref_buffer(yadif->prev);
  195. yadif->prev = NULL;
  196. if (yadif->out->pts != AV_NOPTS_VALUE)
  197. yadif->out->pts *= 2;
  198. ff_start_frame(ctx->outputs[0], yadif->out);
  199. return;
  200. }
  201. if (!yadif->prev)
  202. yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  203. yadif->out = ff_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  204. AV_PERM_REUSE, link->w, link->h);
  205. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  206. yadif->out->video->interlaced = 0;
  207. if (yadif->out->pts != AV_NOPTS_VALUE)
  208. yadif->out->pts *= 2;
  209. ff_start_frame(ctx->outputs[0], yadif->out);
  210. }
  211. static void end_frame(AVFilterLink *link)
  212. {
  213. AVFilterContext *ctx = link->dst;
  214. YADIFContext *yadif = ctx->priv;
  215. if (!yadif->out)
  216. return;
  217. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  218. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  219. ff_end_frame(ctx->outputs[0]);
  220. return;
  221. }
  222. return_frame(ctx, 0);
  223. }
  224. static int request_frame(AVFilterLink *link)
  225. {
  226. AVFilterContext *ctx = link->src;
  227. YADIFContext *yadif = ctx->priv;
  228. if (yadif->frame_pending) {
  229. return_frame(ctx, 1);
  230. return 0;
  231. }
  232. do {
  233. int ret;
  234. if (yadif->eof)
  235. return AVERROR_EOF;
  236. ret = ff_request_frame(link->src->inputs[0]);
  237. if (ret == AVERROR_EOF && yadif->next) {
  238. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, AV_PERM_READ);
  239. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  240. start_frame(link->src->inputs[0], next);
  241. end_frame(link->src->inputs[0]);
  242. yadif->eof = 1;
  243. } else if (ret < 0) {
  244. return ret;
  245. }
  246. } while (!yadif->cur);
  247. return 0;
  248. }
  249. static int poll_frame(AVFilterLink *link)
  250. {
  251. YADIFContext *yadif = link->src->priv;
  252. int ret, val;
  253. if (yadif->frame_pending)
  254. return 1;
  255. val = ff_poll_frame(link->src->inputs[0]);
  256. if (val <= 0)
  257. return val;
  258. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  259. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  260. return ret;
  261. val = ff_poll_frame(link->src->inputs[0]);
  262. if (val <= 0)
  263. return val;
  264. }
  265. assert(yadif->next || !val);
  266. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  267. return val;
  268. return val * ((yadif->mode&1)+1);
  269. }
  270. static av_cold void uninit(AVFilterContext *ctx)
  271. {
  272. YADIFContext *yadif = ctx->priv;
  273. if (yadif->prev) avfilter_unref_buffer(yadif->prev);
  274. if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
  275. if (yadif->next) avfilter_unref_buffer(yadif->next);
  276. }
  277. static int query_formats(AVFilterContext *ctx)
  278. {
  279. static const enum PixelFormat pix_fmts[] = {
  280. PIX_FMT_YUV420P,
  281. PIX_FMT_YUV422P,
  282. PIX_FMT_YUV444P,
  283. PIX_FMT_YUV410P,
  284. PIX_FMT_YUV411P,
  285. PIX_FMT_GRAY8,
  286. PIX_FMT_YUVJ420P,
  287. PIX_FMT_YUVJ422P,
  288. PIX_FMT_YUVJ444P,
  289. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  290. PIX_FMT_YUV440P,
  291. PIX_FMT_YUVJ440P,
  292. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  293. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  294. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  295. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  296. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  297. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  298. PIX_FMT_YUVA420P,
  299. PIX_FMT_NONE
  300. };
  301. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  302. return 0;
  303. }
  304. static av_cold int init(AVFilterContext *ctx, const char *args)
  305. {
  306. YADIFContext *yadif = ctx->priv;
  307. yadif->mode = 0;
  308. yadif->parity = -1;
  309. yadif->auto_enable = 0;
  310. yadif->csp = NULL;
  311. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  312. yadif->filter_line = filter_line_c;
  313. if (HAVE_MMX)
  314. ff_yadif_init_x86(yadif);
  315. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  316. return 0;
  317. }
  318. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  319. static int config_props(AVFilterLink *link)
  320. {
  321. link->time_base.num = link->src->inputs[0]->time_base.num;
  322. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  323. link->w = link->src->inputs[0]->w;
  324. link->h = link->src->inputs[0]->h;
  325. return 0;
  326. }
  327. AVFilter avfilter_vf_yadif = {
  328. .name = "yadif",
  329. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  330. .priv_size = sizeof(YADIFContext),
  331. .init = init,
  332. .uninit = uninit,
  333. .query_formats = query_formats,
  334. .inputs = (AVFilterPad[]) {{ .name = "default",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .start_frame = start_frame,
  337. .get_video_buffer = get_video_buffer,
  338. .draw_slice = null_draw_slice,
  339. .end_frame = end_frame, },
  340. { .name = NULL}},
  341. .outputs = (AVFilterPad[]) {{ .name = "default",
  342. .type = AVMEDIA_TYPE_VIDEO,
  343. .poll_frame = poll_frame,
  344. .request_frame = request_frame,
  345. .config_props = config_props, },
  346. { .name = NULL}},
  347. };