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.

216 lines
7.2KB

  1. /*
  2. * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
  3. * 2010 James Darnley <james.darnley@gmail.com>
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/imgutils.h"
  22. #include "internal.h"
  23. #include "yadif.h"
  24. static int return_frame(AVFilterContext *ctx, int is_second)
  25. {
  26. YADIFContext *yadif = ctx->priv;
  27. AVFilterLink *link = ctx->outputs[0];
  28. int tff, ret;
  29. if (yadif->parity == -1) {
  30. tff = yadif->cur->interlaced_frame ?
  31. yadif->cur->top_field_first : 1;
  32. } else {
  33. tff = yadif->parity ^ 1;
  34. }
  35. if (is_second) {
  36. yadif->out = ff_get_video_buffer(link, link->w, link->h);
  37. if (!yadif->out)
  38. return AVERROR(ENOMEM);
  39. av_frame_copy_props(yadif->out, yadif->cur);
  40. yadif->out->interlaced_frame = 0;
  41. if (yadif->current_field == YADIF_FIELD_BACK_END)
  42. yadif->current_field = YADIF_FIELD_END;
  43. }
  44. yadif->filter(ctx, yadif->out, tff ^ !is_second, tff);
  45. if (is_second) {
  46. int64_t cur_pts = yadif->cur->pts;
  47. int64_t next_pts = yadif->next->pts;
  48. if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
  49. yadif->out->pts = cur_pts + next_pts;
  50. } else {
  51. yadif->out->pts = AV_NOPTS_VALUE;
  52. }
  53. }
  54. ret = ff_filter_frame(ctx->outputs[0], yadif->out);
  55. yadif->frame_pending = (yadif->mode&1) && !is_second;
  56. return ret;
  57. }
  58. static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
  59. {
  60. int i;
  61. for (i = 0; i < yadif->csp->nb_components; i++)
  62. if (a->linesize[i] != b->linesize[i])
  63. return 1;
  64. return 0;
  65. }
  66. static void fixstride(AVFilterLink *link, AVFrame *f)
  67. {
  68. AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
  69. if(!dst)
  70. return;
  71. av_frame_copy_props(dst, f);
  72. av_image_copy(dst->data, dst->linesize,
  73. (const uint8_t **)f->data, f->linesize,
  74. dst->format, dst->width, dst->height);
  75. av_frame_unref(f);
  76. av_frame_move_ref(f, dst);
  77. av_frame_free(&dst);
  78. }
  79. int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
  80. {
  81. AVFilterContext *ctx = link->dst;
  82. YADIFContext *yadif = ctx->priv;
  83. av_assert0(frame);
  84. if (yadif->frame_pending)
  85. return_frame(ctx, 1);
  86. if (yadif->prev)
  87. av_frame_free(&yadif->prev);
  88. yadif->prev = yadif->cur;
  89. yadif->cur = yadif->next;
  90. yadif->next = frame;
  91. if (!yadif->cur) {
  92. yadif->cur = av_frame_clone(yadif->next);
  93. if (!yadif->cur)
  94. return AVERROR(ENOMEM);
  95. yadif->current_field = YADIF_FIELD_END;
  96. }
  97. if (checkstride(yadif, yadif->next, yadif->cur)) {
  98. av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
  99. fixstride(link, yadif->next);
  100. }
  101. if (checkstride(yadif, yadif->next, yadif->cur))
  102. fixstride(link, yadif->cur);
  103. if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
  104. fixstride(link, yadif->prev);
  105. if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
  106. av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
  107. return -1;
  108. }
  109. if (!yadif->prev)
  110. return 0;
  111. if ((yadif->deint && !yadif->cur->interlaced_frame) ||
  112. ctx->is_disabled ||
  113. (yadif->deint && !yadif->prev->interlaced_frame && yadif->prev->repeat_pict) ||
  114. (yadif->deint && !yadif->next->interlaced_frame && yadif->next->repeat_pict)
  115. ) {
  116. yadif->out = av_frame_clone(yadif->cur);
  117. if (!yadif->out)
  118. return AVERROR(ENOMEM);
  119. av_frame_free(&yadif->prev);
  120. if (yadif->out->pts != AV_NOPTS_VALUE)
  121. yadif->out->pts *= 2;
  122. return ff_filter_frame(ctx->outputs[0], yadif->out);
  123. }
  124. yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
  125. if (!yadif->out)
  126. return AVERROR(ENOMEM);
  127. av_frame_copy_props(yadif->out, yadif->cur);
  128. yadif->out->interlaced_frame = 0;
  129. if (yadif->out->pts != AV_NOPTS_VALUE)
  130. yadif->out->pts *= 2;
  131. return return_frame(ctx, 0);
  132. }
  133. int ff_yadif_request_frame(AVFilterLink *link)
  134. {
  135. AVFilterContext *ctx = link->src;
  136. YADIFContext *yadif = ctx->priv;
  137. int ret;
  138. if (yadif->frame_pending) {
  139. return_frame(ctx, 1);
  140. return 0;
  141. }
  142. if (yadif->eof)
  143. return AVERROR_EOF;
  144. ret = ff_request_frame(ctx->inputs[0]);
  145. if (ret == AVERROR_EOF && yadif->cur) {
  146. AVFrame *next = av_frame_clone(yadif->next);
  147. if (!next)
  148. return AVERROR(ENOMEM);
  149. yadif->current_field = YADIF_FIELD_BACK_END;
  150. next->pts = yadif->next->pts * 2 - yadif->cur->pts;
  151. ff_yadif_filter_frame(ctx->inputs[0], next);
  152. yadif->eof = 1;
  153. } else if (ret < 0) {
  154. return ret;
  155. }
  156. return 0;
  157. }
  158. #define OFFSET(x) offsetof(YADIFContext, x)
  159. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  160. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
  161. const AVOption ff_yadif_options[] = {
  162. { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
  163. CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
  164. CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
  165. CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
  166. CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
  167. { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
  168. CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
  169. CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
  170. CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
  171. { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
  172. CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
  173. CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
  174. { NULL }
  175. };