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.

446 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. emms_c();
  126. }
  127. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  128. {
  129. AVFilterBufferRef *picref;
  130. int width = FFALIGN(w, 32);
  131. int height= FFALIGN(h+2, 32);
  132. int i;
  133. picref = ff_default_get_video_buffer(link, perms, width, height);
  134. picref->video->w = w;
  135. picref->video->h = h;
  136. for (i = 0; i < 3; i++)
  137. picref->data[i] += picref->linesize[i];
  138. return picref;
  139. }
  140. static int return_frame(AVFilterContext *ctx, int is_second)
  141. {
  142. YADIFContext *yadif = ctx->priv;
  143. AVFilterLink *link= ctx->outputs[0];
  144. int tff, ret;
  145. if (yadif->parity == -1) {
  146. tff = yadif->cur->video->interlaced ?
  147. yadif->cur->video->top_field_first : 1;
  148. } else {
  149. tff = yadif->parity^1;
  150. }
  151. if (is_second) {
  152. yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  153. AV_PERM_REUSE, link->w, link->h);
  154. if (!yadif->out)
  155. return AVERROR(ENOMEM);
  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. ret = ff_start_frame(ctx->outputs[0], yadif->out);
  173. if (ret < 0)
  174. return ret;
  175. }
  176. if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
  177. (ret = ff_end_frame(ctx->outputs[0])) < 0)
  178. return ret;
  179. yadif->frame_pending = (yadif->mode&1) && !is_second;
  180. return 0;
  181. }
  182. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  183. {
  184. AVFilterContext *ctx = link->dst;
  185. YADIFContext *yadif = ctx->priv;
  186. av_assert0(picref);
  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->cur) {
  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_YUVA422P,
  312. PIX_FMT_YUVA444P,
  313. PIX_FMT_NONE
  314. };
  315. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  316. return 0;
  317. }
  318. static av_cold int init(AVFilterContext *ctx, const char *args)
  319. {
  320. YADIFContext *yadif = ctx->priv;
  321. yadif->mode = 0;
  322. yadif->parity = -1;
  323. yadif->auto_enable = 0;
  324. yadif->csp = NULL;
  325. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  326. yadif->filter_line = filter_line_c;
  327. if (HAVE_MMX)
  328. ff_yadif_init_x86(yadif);
  329. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  330. return 0;
  331. }
  332. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  333. {
  334. return 0;
  335. }
  336. static int config_props(AVFilterLink *link)
  337. {
  338. YADIFContext *yadif = link->src->priv;
  339. link->time_base.num = link->src->inputs[0]->time_base.num;
  340. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  341. link->w = link->src->inputs[0]->w;
  342. link->h = link->src->inputs[0]->h;
  343. if(yadif->mode&1)
  344. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  345. return 0;
  346. }
  347. AVFilter avfilter_vf_yadif = {
  348. .name = "yadif",
  349. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  350. .priv_size = sizeof(YADIFContext),
  351. .init = init,
  352. .uninit = uninit,
  353. .query_formats = query_formats,
  354. .inputs = (const AVFilterPad[]) {{ .name = "default",
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. .start_frame = start_frame,
  357. .get_video_buffer = get_video_buffer,
  358. .draw_slice = null_draw_slice,
  359. .end_frame = end_frame,
  360. .rej_perms = AV_PERM_REUSE2, },
  361. { .name = NULL}},
  362. .outputs = (const AVFilterPad[]) {{ .name = "default",
  363. .type = AVMEDIA_TYPE_VIDEO,
  364. .poll_frame = poll_frame,
  365. .request_frame = request_frame,
  366. .config_props = config_props, },
  367. { .name = NULL}},
  368. };