vf_repeatfields.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2003 Tobias Diedrich
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "libavutil/imgutils.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. typedef struct RepeatFieldsContext {
  24. const AVClass *class;
  25. int state;
  26. int nb_planes;
  27. int linesize[4];
  28. int planeheight[4];
  29. AVFrame *frame;
  30. } RepeatFieldsContext;
  31. static av_cold void uninit(AVFilterContext *ctx)
  32. {
  33. RepeatFieldsContext *s = ctx->priv;
  34. av_frame_free(&s->frame);
  35. }
  36. static int query_formats(AVFilterContext *ctx)
  37. {
  38. static const enum AVPixelFormat pixel_fmts_eq[] = {
  39. AV_PIX_FMT_GRAY8,
  40. AV_PIX_FMT_YUV410P,
  41. AV_PIX_FMT_YUV411P,
  42. AV_PIX_FMT_YUV420P,
  43. AV_PIX_FMT_YUV422P,
  44. AV_PIX_FMT_YUV444P,
  45. AV_PIX_FMT_NONE
  46. };
  47. ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_eq));
  48. return 0;
  49. }
  50. static int config_input(AVFilterLink *inlink)
  51. {
  52. RepeatFieldsContext *s = inlink->dst->priv;
  53. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  54. int ret;
  55. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  56. return ret;
  57. s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  58. s->planeheight[0] = s->planeheight[3] = inlink->h;
  59. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  60. return 0;
  61. }
  62. static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
  63. {
  64. if (av_cmp_q(link->frame_rate, (AVRational){30000, 1001}) == 0 &&
  65. av_cmp_q(link->time_base, (AVRational){1001, 60000}) <= 0
  66. ) {
  67. f->pts = pts + av_rescale_q(fields, (AVRational){1001, 60000}, link->time_base);
  68. } else
  69. f->pts = AV_NOPTS_VALUE;
  70. }
  71. static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
  72. AVFilterContext *ctx = inlink->dst;
  73. AVFilterLink *outlink = inlink->dst->outputs[0];
  74. RepeatFieldsContext *s = ctx->priv;
  75. AVFrame *out;
  76. int ret, i;
  77. int state = s->state;
  78. if (!s->frame) {
  79. s->frame = av_frame_clone(in);
  80. if (!s->frame)
  81. return AVERROR(ENOMEM);
  82. s->frame->pts = AV_NOPTS_VALUE;
  83. }
  84. out = s->frame;
  85. if ((state == 0 && !in->top_field_first) ||
  86. (state == 1 && in->top_field_first)) {
  87. av_log(ctx, AV_LOG_WARNING, "Unexpected field flags: "
  88. "state=%d top_field_first=%d repeat_first_field=%d\n",
  89. state, in->top_field_first, in->repeat_pict);
  90. state ^= 1;
  91. }
  92. if (state == 0) {
  93. AVFrame *new;
  94. new = av_frame_clone(in);
  95. if (!new)
  96. return AVERROR(ENOMEM);
  97. ret = ff_filter_frame(outlink, new);
  98. if (in->repeat_pict) {
  99. av_frame_make_writable(out);
  100. update_pts(outlink, out, in->pts, 2);
  101. for (i = 0; i < s->nb_planes; i++) {
  102. av_image_copy_plane(out->data[i], out->linesize[i] * 2,
  103. in->data[i], in->linesize[i] * 2,
  104. s->linesize[i], s->planeheight[i] / 2);
  105. }
  106. state = 1;
  107. }
  108. } else {
  109. for (i = 0; i < s->nb_planes; i++) {
  110. av_frame_make_writable(out);
  111. av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
  112. in->data[i] + in->linesize[i], in->linesize[i] * 2,
  113. s->linesize[i], s->planeheight[i] / 2);
  114. }
  115. ret = ff_filter_frame(outlink, av_frame_clone(out));
  116. if (in->repeat_pict) {
  117. AVFrame *new;
  118. new = av_frame_clone(in);
  119. if (!new)
  120. return AVERROR(ENOMEM);
  121. ret = ff_filter_frame(outlink, new);
  122. state = 0;
  123. } else {
  124. av_frame_make_writable(out);
  125. update_pts(outlink, out, in->pts, 1);
  126. for (i = 0; i < s->nb_planes; i++) {
  127. av_image_copy_plane(out->data[i], out->linesize[i] * 2,
  128. in->data[i], in->linesize[i] * 2,
  129. s->linesize[i], s->planeheight[i] / 2);
  130. }
  131. }
  132. }
  133. s->state = state;
  134. av_frame_free(&in);
  135. return ret;
  136. }
  137. static const AVFilterPad repeatfields_inputs[] = {
  138. {
  139. .name = "default",
  140. .type = AVMEDIA_TYPE_VIDEO,
  141. .filter_frame = filter_frame,
  142. .config_props = config_input,
  143. },
  144. { NULL }
  145. };
  146. static const AVFilterPad repeatfields_outputs[] = {
  147. {
  148. .name = "default",
  149. .type = AVMEDIA_TYPE_VIDEO,
  150. },
  151. { NULL }
  152. };
  153. AVFilter ff_vf_repeatfields = {
  154. .name = "repeatfields",
  155. .description = NULL_IF_CONFIG_SMALL("Hard repeat fields based on MPEG repeat field flag."),
  156. .priv_size = sizeof(RepeatFieldsContext),
  157. .uninit = uninit,
  158. .inputs = repeatfields_inputs,
  159. .outputs = repeatfields_outputs,
  160. .query_formats = query_formats,
  161. };