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.

422 lines
13KB

  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 CHECK(j)\
  31. { int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
  32. + FFABS(cur[mrefs +(j)] - cur[prefs -(j)])\
  33. + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
  34. if (score < spatial_score) {\
  35. spatial_score= score;\
  36. spatial_pred= (cur[mrefs +(j)] + cur[prefs -(j)])>>1;\
  37. #define FILTER \
  38. for (x = 0; x < w; x++) { \
  39. int c = cur[mrefs]; \
  40. int d = (prev2[0] + next2[0])>>1; \
  41. int e = cur[prefs]; \
  42. int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
  43. int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
  44. int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
  45. int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
  46. int spatial_pred = (c+e)>>1; \
  47. int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
  48. + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
  49. \
  50. CHECK(-1) CHECK(-2) }} }} \
  51. CHECK( 1) CHECK( 2) }} }} \
  52. \
  53. if (mode < 2) { \
  54. int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
  55. int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
  56. int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
  57. int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
  58. \
  59. diff = FFMAX3(diff, min, -max); \
  60. } \
  61. \
  62. if (spatial_pred > d + diff) \
  63. spatial_pred = d + diff; \
  64. else if (spatial_pred < d - diff) \
  65. spatial_pred = d - diff; \
  66. \
  67. dst[0] = spatial_pred; \
  68. \
  69. dst++; \
  70. cur++; \
  71. prev++; \
  72. next++; \
  73. prev2++; \
  74. next2++; \
  75. }
  76. static void filter_line_c(uint8_t *dst,
  77. uint8_t *prev, uint8_t *cur, uint8_t *next,
  78. int w, int prefs, int mrefs, int parity, int mode)
  79. {
  80. int x;
  81. uint8_t *prev2 = parity ? prev : cur ;
  82. uint8_t *next2 = parity ? cur : next;
  83. FILTER
  84. }
  85. static void filter_line_c_16bit(uint16_t *dst,
  86. uint16_t *prev, uint16_t *cur, uint16_t *next,
  87. int w, int prefs, int mrefs, int parity, int mode)
  88. {
  89. int x;
  90. uint16_t *prev2 = parity ? prev : cur ;
  91. uint16_t *next2 = parity ? cur : next;
  92. mrefs /= 2;
  93. prefs /= 2;
  94. FILTER
  95. }
  96. static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
  97. int parity, int tff)
  98. {
  99. YADIFContext *yadif = ctx->priv;
  100. int y, i;
  101. for (i = 0; i < yadif->csp->nb_components; i++) {
  102. int w = dstpic->video->w;
  103. int h = dstpic->video->h;
  104. int refs = yadif->cur->linesize[i];
  105. int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
  106. if (i == 1 || i == 2) {
  107. /* Why is this not part of the per-plane description thing? */
  108. w >>= yadif->csp->log2_chroma_w;
  109. h >>= yadif->csp->log2_chroma_h;
  110. }
  111. for (y = 0; y < h; y++) {
  112. if ((y ^ parity) & 1) {
  113. uint8_t *prev = &yadif->prev->data[i][y*refs];
  114. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  115. uint8_t *next = &yadif->next->data[i][y*refs];
  116. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  117. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  118. yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
  119. } else {
  120. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  121. &yadif->cur->data[i][y*refs], w*df);
  122. }
  123. }
  124. }
  125. #if HAVE_MMX
  126. __asm__ volatile("emms \n\t" : : : "memory");
  127. #endif
  128. }
  129. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  130. {
  131. AVFilterBufferRef *picref;
  132. int width = FFALIGN(w, 32);
  133. int height= FFALIGN(h+2, 32);
  134. int i;
  135. picref = ff_default_get_video_buffer(link, perms, width, height);
  136. picref->video->w = w;
  137. picref->video->h = h;
  138. for (i = 0; i < 3; i++)
  139. picref->data[i] += picref->linesize[i];
  140. return picref;
  141. }
  142. static void return_frame(AVFilterContext *ctx, int is_second)
  143. {
  144. YADIFContext *yadif = ctx->priv;
  145. AVFilterLink *link= ctx->outputs[0];
  146. int tff;
  147. if (yadif->parity == -1) {
  148. tff = yadif->cur->video->interlaced ?
  149. yadif->cur->video->top_field_first : 1;
  150. } else {
  151. tff = yadif->parity^1;
  152. }
  153. if (is_second) {
  154. yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  155. AV_PERM_REUSE, link->w, link->h);
  156. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  157. yadif->out->video->interlaced = 0;
  158. }
  159. if (!yadif->csp)
  160. yadif->csp = &av_pix_fmt_descriptors[link->format];
  161. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  162. yadif->filter_line = (void*)filter_line_c_16bit;
  163. filter(ctx, yadif->out, tff ^ !is_second, tff);
  164. if (is_second) {
  165. int64_t cur_pts = yadif->cur->pts;
  166. int64_t next_pts = yadif->next->pts;
  167. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  168. yadif->out->pts = cur_pts + next_pts;
  169. } else {
  170. yadif->out->pts = AV_NOPTS_VALUE;
  171. }
  172. ff_start_frame(ctx->outputs[0], yadif->out);
  173. }
  174. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  175. ff_end_frame(ctx->outputs[0]);
  176. yadif->frame_pending = (yadif->mode&1) && !is_second;
  177. }
  178. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  179. {
  180. AVFilterContext *ctx = link->dst;
  181. YADIFContext *yadif = ctx->priv;
  182. av_assert0(picref);
  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->cur) {
  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_YUVA422P,
  300. PIX_FMT_YUVA444P,
  301. PIX_FMT_NONE
  302. };
  303. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  304. return 0;
  305. }
  306. static av_cold int init(AVFilterContext *ctx, const char *args)
  307. {
  308. YADIFContext *yadif = ctx->priv;
  309. yadif->mode = 0;
  310. yadif->parity = -1;
  311. yadif->auto_enable = 0;
  312. yadif->csp = NULL;
  313. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  314. yadif->filter_line = filter_line_c;
  315. if (HAVE_MMX)
  316. ff_yadif_init_x86(yadif);
  317. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  318. return 0;
  319. }
  320. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  321. static int config_props(AVFilterLink *link)
  322. {
  323. link->time_base.num = link->src->inputs[0]->time_base.num;
  324. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  325. link->w = link->src->inputs[0]->w;
  326. link->h = link->src->inputs[0]->h;
  327. return 0;
  328. }
  329. AVFilter avfilter_vf_yadif = {
  330. .name = "yadif",
  331. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  332. .priv_size = sizeof(YADIFContext),
  333. .init = init,
  334. .uninit = uninit,
  335. .query_formats = query_formats,
  336. .inputs = (const AVFilterPad[]) {{ .name = "default",
  337. .type = AVMEDIA_TYPE_VIDEO,
  338. .start_frame = start_frame,
  339. .get_video_buffer = get_video_buffer,
  340. .draw_slice = null_draw_slice,
  341. .end_frame = end_frame,
  342. .rej_perms = AV_PERM_REUSE2, },
  343. { .name = NULL}},
  344. .outputs = (const AVFilterPad[]) {{ .name = "default",
  345. .type = AVMEDIA_TYPE_VIDEO,
  346. .poll_frame = poll_frame,
  347. .request_frame = request_frame,
  348. .config_props = config_props, },
  349. { .name = NULL}},
  350. };