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.

437 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. emms_c();
  127. }
  128. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  129. {
  130. AVFilterBufferRef *picref;
  131. int width = FFALIGN(w, 32);
  132. int height= FFALIGN(h+2, 32);
  133. int i;
  134. picref = ff_default_get_video_buffer(link, perms, width, height);
  135. picref->video->w = w;
  136. picref->video->h = h;
  137. for (i = 0; i < 3; i++)
  138. picref->data[i] += picref->linesize[i];
  139. return picref;
  140. }
  141. static int return_frame(AVFilterContext *ctx, int is_second)
  142. {
  143. YADIFContext *yadif = ctx->priv;
  144. AVFilterLink *link= ctx->outputs[0];
  145. int tff, ret;
  146. if (yadif->parity == -1) {
  147. tff = yadif->cur->video->interlaced ?
  148. yadif->cur->video->top_field_first : 1;
  149. } else {
  150. tff = yadif->parity^1;
  151. }
  152. if (is_second) {
  153. yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  154. AV_PERM_REUSE, link->w, link->h);
  155. if (!yadif->out)
  156. return AVERROR(ENOMEM);
  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. ret = ff_start_frame(ctx->outputs[0], yadif->out);
  174. if (ret < 0)
  175. return ret;
  176. }
  177. if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
  178. (ret = ff_end_frame(ctx->outputs[0])) < 0)
  179. return ret;
  180. yadif->frame_pending = (yadif->mode&1) && !is_second;
  181. return 0;
  182. }
  183. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  184. {
  185. AVFilterContext *ctx = link->dst;
  186. YADIFContext *yadif = ctx->priv;
  187. if (yadif->frame_pending)
  188. return_frame(ctx, 1);
  189. if (yadif->prev)
  190. avfilter_unref_buffer(yadif->prev);
  191. yadif->prev = yadif->cur;
  192. yadif->cur = yadif->next;
  193. yadif->next = picref;
  194. link->cur_buf = NULL;
  195. if (!yadif->cur)
  196. return 0;
  197. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  198. yadif->out = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
  199. if (!yadif->out)
  200. return AVERROR(ENOMEM);
  201. avfilter_unref_bufferp(&yadif->prev);
  202. if (yadif->out->pts != AV_NOPTS_VALUE)
  203. yadif->out->pts *= 2;
  204. return ff_start_frame(ctx->outputs[0], yadif->out);
  205. }
  206. if (!yadif->prev &&
  207. !(yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ)))
  208. return AVERROR(ENOMEM);
  209. yadif->out = ff_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  210. AV_PERM_REUSE, link->w, link->h);
  211. if (!yadif->out)
  212. return AVERROR(ENOMEM);
  213. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  214. yadif->out->video->interlaced = 0;
  215. if (yadif->out->pts != AV_NOPTS_VALUE)
  216. yadif->out->pts *= 2;
  217. return ff_start_frame(ctx->outputs[0], yadif->out);
  218. }
  219. static int end_frame(AVFilterLink *link)
  220. {
  221. AVFilterContext *ctx = link->dst;
  222. YADIFContext *yadif = ctx->priv;
  223. if (!yadif->out)
  224. return 0;
  225. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  226. int ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  227. if (ret >= 0)
  228. ret = ff_end_frame(ctx->outputs[0]);
  229. return ret;
  230. }
  231. return_frame(ctx, 0);
  232. return 0;
  233. }
  234. static int request_frame(AVFilterLink *link)
  235. {
  236. AVFilterContext *ctx = link->src;
  237. YADIFContext *yadif = ctx->priv;
  238. if (yadif->frame_pending) {
  239. return_frame(ctx, 1);
  240. return 0;
  241. }
  242. do {
  243. int ret;
  244. if (yadif->eof)
  245. return AVERROR_EOF;
  246. ret = ff_request_frame(link->src->inputs[0]);
  247. if (ret == AVERROR_EOF && yadif->next) {
  248. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, AV_PERM_READ);
  249. if (!next)
  250. return AVERROR(ENOMEM);
  251. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  252. start_frame(link->src->inputs[0], next);
  253. end_frame(link->src->inputs[0]);
  254. yadif->eof = 1;
  255. } else if (ret < 0) {
  256. return ret;
  257. }
  258. } while (!yadif->cur);
  259. return 0;
  260. }
  261. static int poll_frame(AVFilterLink *link)
  262. {
  263. YADIFContext *yadif = link->src->priv;
  264. int ret, val;
  265. if (yadif->frame_pending)
  266. return 1;
  267. val = ff_poll_frame(link->src->inputs[0]);
  268. if (val <= 0)
  269. return val;
  270. if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
  271. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  272. return ret;
  273. val = ff_poll_frame(link->src->inputs[0]);
  274. if (val <= 0)
  275. return val;
  276. }
  277. assert(yadif->next || !val);
  278. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  279. return val;
  280. return val * ((yadif->mode&1)+1);
  281. }
  282. static av_cold void uninit(AVFilterContext *ctx)
  283. {
  284. YADIFContext *yadif = ctx->priv;
  285. if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
  286. if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
  287. if (yadif->next) avfilter_unref_bufferp(&yadif->next);
  288. }
  289. static int query_formats(AVFilterContext *ctx)
  290. {
  291. static const enum PixelFormat pix_fmts[] = {
  292. PIX_FMT_YUV420P,
  293. PIX_FMT_YUV422P,
  294. PIX_FMT_YUV444P,
  295. PIX_FMT_YUV410P,
  296. PIX_FMT_YUV411P,
  297. PIX_FMT_GRAY8,
  298. PIX_FMT_YUVJ420P,
  299. PIX_FMT_YUVJ422P,
  300. PIX_FMT_YUVJ444P,
  301. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  302. PIX_FMT_YUV440P,
  303. PIX_FMT_YUVJ440P,
  304. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  305. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  306. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  307. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  308. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  309. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  310. PIX_FMT_YUVA420P,
  311. 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. yadif->mode = 0;
  320. yadif->parity = -1;
  321. yadif->auto_enable = 0;
  322. yadif->csp = NULL;
  323. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  324. yadif->filter_line = filter_line_c;
  325. if (HAVE_MMX)
  326. ff_yadif_init_x86(yadif);
  327. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  328. return 0;
  329. }
  330. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  331. {
  332. return 0;
  333. }
  334. static int config_props(AVFilterLink *link)
  335. {
  336. link->time_base.num = link->src->inputs[0]->time_base.num;
  337. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  338. link->w = link->src->inputs[0]->w;
  339. link->h = link->src->inputs[0]->h;
  340. return 0;
  341. }
  342. AVFilter avfilter_vf_yadif = {
  343. .name = "yadif",
  344. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
  345. .priv_size = sizeof(YADIFContext),
  346. .init = init,
  347. .uninit = uninit,
  348. .query_formats = query_formats,
  349. .inputs = (const AVFilterPad[]) {{ .name = "default",
  350. .type = AVMEDIA_TYPE_VIDEO,
  351. .start_frame = start_frame,
  352. .get_video_buffer = get_video_buffer,
  353. .draw_slice = null_draw_slice,
  354. .end_frame = end_frame, },
  355. { .name = NULL}},
  356. .outputs = (const AVFilterPad[]) {{ .name = "default",
  357. .type = AVMEDIA_TYPE_VIDEO,
  358. .poll_frame = poll_frame,
  359. .request_frame = request_frame,
  360. .config_props = config_props, },
  361. { .name = NULL}},
  362. };