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.

458 lines
14KB

  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 absrefs = FFABS(refs);
  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. if(yadif->temp_line_size < absrefs) {
  113. av_free(yadif->temp_line);
  114. yadif->temp_line = av_mallocz(2*64 + 5*absrefs);
  115. yadif->temp_line_size = absrefs;
  116. }
  117. for (y = 0; y < h; y++) {
  118. if ((y ^ parity) & 1) {
  119. uint8_t *prev = &yadif->prev->data[i][y*refs];
  120. uint8_t *cur = &yadif->cur ->data[i][y*refs];
  121. uint8_t *next = &yadif->next->data[i][y*refs];
  122. uint8_t *dst = &dstpic->data[i][y*dstpic->linesize[i]];
  123. int mode = y==1 || y+2==h ? 2 : yadif->mode;
  124. int prefs = y+1<h ? refs : -refs;
  125. int mrefs = y ?-refs : refs;
  126. if(y<=1 || y+2>=h) {
  127. int j;
  128. uint8_t *tmp = yadif->temp_line + 64 + 2*absrefs;
  129. if(mode<2)
  130. memcpy(tmp+2*mrefs, cur+2*mrefs, w*df);
  131. memcpy(tmp+mrefs, cur+mrefs, w*df);
  132. memcpy(tmp , cur , w*df);
  133. if(prefs != mrefs) {
  134. memcpy(tmp+prefs, cur+prefs, w*df);
  135. if(mode<2)
  136. memcpy(tmp+2*prefs, cur+2*prefs, w*df);
  137. }
  138. cur = tmp;
  139. }
  140. yadif->filter_line(dst, prev, cur, next, w, prefs, mrefs, parity ^ tff, mode);
  141. } else {
  142. memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
  143. &yadif->cur->data[i][y*refs], w*df);
  144. }
  145. }
  146. }
  147. emms_c();
  148. }
  149. static int return_frame(AVFilterContext *ctx, int is_second)
  150. {
  151. YADIFContext *yadif = ctx->priv;
  152. AVFilterLink *link= ctx->outputs[0];
  153. int tff, ret;
  154. if (yadif->parity == -1) {
  155. tff = yadif->cur->video->interlaced ?
  156. yadif->cur->video->top_field_first : 1;
  157. } else {
  158. tff = yadif->parity^1;
  159. }
  160. if (is_second) {
  161. yadif->out = ff_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
  162. AV_PERM_REUSE, link->w, link->h);
  163. if (!yadif->out)
  164. return AVERROR(ENOMEM);
  165. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  166. yadif->out->video->interlaced = 0;
  167. }
  168. if (!yadif->csp)
  169. yadif->csp = &av_pix_fmt_descriptors[link->format];
  170. if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
  171. yadif->filter_line = (void*)filter_line_c_16bit;
  172. filter(ctx, yadif->out, tff ^ !is_second, tff);
  173. if (is_second) {
  174. int64_t cur_pts = yadif->cur->pts;
  175. int64_t next_pts = yadif->next->pts;
  176. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  177. yadif->out->pts = cur_pts + next_pts;
  178. } else {
  179. yadif->out->pts = AV_NOPTS_VALUE;
  180. }
  181. ret = ff_start_frame(ctx->outputs[0], yadif->out);
  182. if (ret < 0)
  183. return ret;
  184. }
  185. if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
  186. (ret = ff_end_frame(ctx->outputs[0])) < 0)
  187. return ret;
  188. yadif->frame_pending = (yadif->mode&1) && !is_second;
  189. return 0;
  190. }
  191. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  192. {
  193. AVFilterContext *ctx = link->dst;
  194. YADIFContext *yadif = ctx->priv;
  195. av_assert0(picref);
  196. if (picref->video->h < 3 || picref->video->w < 3) {
  197. av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n");
  198. return AVERROR(EINVAL);
  199. }
  200. if (yadif->frame_pending)
  201. return_frame(ctx, 1);
  202. if (yadif->prev)
  203. avfilter_unref_buffer(yadif->prev);
  204. yadif->prev = yadif->cur;
  205. yadif->cur = yadif->next;
  206. yadif->next = picref;
  207. link->cur_buf = NULL;
  208. if (!yadif->cur)
  209. return 0;
  210. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  211. yadif->out = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
  212. if (!yadif->out)
  213. return AVERROR(ENOMEM);
  214. avfilter_unref_bufferp(&yadif->prev);
  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. if (!yadif->prev &&
  220. !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
  221. return AVERROR(ENOMEM);
  222. yadif->out = ff_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
  223. AV_PERM_REUSE, link->w, link->h);
  224. if (!yadif->out)
  225. return AVERROR(ENOMEM);
  226. avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
  227. yadif->out->video->interlaced = 0;
  228. if (yadif->out->pts != AV_NOPTS_VALUE)
  229. yadif->out->pts *= 2;
  230. return ff_start_frame(ctx->outputs[0], yadif->out);
  231. }
  232. static int end_frame(AVFilterLink *link)
  233. {
  234. AVFilterContext *ctx = link->dst;
  235. YADIFContext *yadif = ctx->priv;
  236. if (!yadif->out)
  237. return 0;
  238. if (yadif->auto_enable && !yadif->cur->video->interlaced) {
  239. int ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  240. if (ret >= 0)
  241. ret = ff_end_frame(ctx->outputs[0]);
  242. return ret;
  243. }
  244. return_frame(ctx, 0);
  245. return 0;
  246. }
  247. static int request_frame(AVFilterLink *link)
  248. {
  249. AVFilterContext *ctx = link->src;
  250. YADIFContext *yadif = ctx->priv;
  251. if (yadif->frame_pending) {
  252. return_frame(ctx, 1);
  253. return 0;
  254. }
  255. do {
  256. int ret;
  257. if (yadif->eof)
  258. return AVERROR_EOF;
  259. ret = ff_request_frame(link->src->inputs[0]);
  260. if (ret == AVERROR_EOF && yadif->cur) {
  261. AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
  262. if (!next)
  263. return AVERROR(ENOMEM);
  264. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  265. start_frame(link->src->inputs[0], next);
  266. end_frame(link->src->inputs[0]);
  267. yadif->eof = 1;
  268. } else if (ret < 0) {
  269. return ret;
  270. }
  271. } while (!yadif->cur);
  272. return 0;
  273. }
  274. static int poll_frame(AVFilterLink *link)
  275. {
  276. YADIFContext *yadif = link->src->priv;
  277. int ret, val;
  278. if (yadif->frame_pending)
  279. return 1;
  280. val = ff_poll_frame(link->src->inputs[0]);
  281. if (val <= 0)
  282. return val;
  283. if (val >= 1 && !yadif->next) { //FIXME change API to not requre this red tape
  284. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  285. return ret;
  286. val = ff_poll_frame(link->src->inputs[0]);
  287. if (val <= 0)
  288. return val;
  289. }
  290. assert(yadif->next || !val);
  291. if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
  292. return val;
  293. return val * ((yadif->mode&1)+1);
  294. }
  295. static av_cold void uninit(AVFilterContext *ctx)
  296. {
  297. YADIFContext *yadif = ctx->priv;
  298. if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
  299. if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
  300. if (yadif->next) avfilter_unref_bufferp(&yadif->next);
  301. av_freep(&yadif->temp_line); yadif->temp_line_size = 0;
  302. }
  303. static int query_formats(AVFilterContext *ctx)
  304. {
  305. static const enum PixelFormat pix_fmts[] = {
  306. PIX_FMT_YUV420P,
  307. PIX_FMT_YUV422P,
  308. PIX_FMT_YUV444P,
  309. PIX_FMT_YUV410P,
  310. PIX_FMT_YUV411P,
  311. PIX_FMT_GRAY8,
  312. PIX_FMT_YUVJ420P,
  313. PIX_FMT_YUVJ422P,
  314. PIX_FMT_YUVJ444P,
  315. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  316. PIX_FMT_YUV440P,
  317. PIX_FMT_YUVJ440P,
  318. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  319. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  320. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  321. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  322. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  323. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  324. PIX_FMT_YUVA420P,
  325. PIX_FMT_YUVA422P,
  326. PIX_FMT_YUVA444P,
  327. PIX_FMT_NONE
  328. };
  329. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  330. return 0;
  331. }
  332. static av_cold int init(AVFilterContext *ctx, const char *args)
  333. {
  334. YADIFContext *yadif = ctx->priv;
  335. yadif->mode = 0;
  336. yadif->parity = -1;
  337. yadif->auto_enable = 0;
  338. yadif->csp = NULL;
  339. if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
  340. yadif->filter_line = filter_line_c;
  341. if (HAVE_MMX)
  342. ff_yadif_init_x86(yadif);
  343. av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
  344. return 0;
  345. }
  346. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  347. {
  348. return 0;
  349. }
  350. static int config_props(AVFilterLink *link)
  351. {
  352. YADIFContext *yadif = link->src->priv;
  353. link->time_base.num = link->src->inputs[0]->time_base.num;
  354. link->time_base.den = link->src->inputs[0]->time_base.den * 2;
  355. link->w = link->src->inputs[0]->w;
  356. link->h = link->src->inputs[0]->h;
  357. if(yadif->mode&1)
  358. link->frame_rate = av_mul_q(link->src->inputs[0]->frame_rate, (AVRational){2,1});
  359. return 0;
  360. }
  361. AVFilter avfilter_vf_yadif = {
  362. .name = "yadif",
  363. .description = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
  364. .priv_size = sizeof(YADIFContext),
  365. .init = init,
  366. .uninit = uninit,
  367. .query_formats = query_formats,
  368. .inputs = (const AVFilterPad[]) {{ .name = "default",
  369. .type = AVMEDIA_TYPE_VIDEO,
  370. .start_frame = start_frame,
  371. .draw_slice = null_draw_slice,
  372. .end_frame = end_frame,
  373. .min_perms = AV_PERM_PRESERVE, },
  374. { .name = NULL}},
  375. .outputs = (const AVFilterPad[]) {{ .name = "default",
  376. .type = AVMEDIA_TYPE_VIDEO,
  377. .poll_frame = poll_frame,
  378. .request_frame = request_frame,
  379. .config_props = config_props, },
  380. { .name = NULL}},
  381. };