vf_repeatfields.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_eq);
  48. if (!fmts_list)
  49. return AVERROR(ENOMEM);
  50. return ff_set_common_formats(ctx, fmts_list);
  51. }
  52. static int config_input(AVFilterLink *inlink)
  53. {
  54. RepeatFieldsContext *s = inlink->dst->priv;
  55. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  56. int ret;
  57. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  58. return ret;
  59. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  60. s->planeheight[0] = s->planeheight[3] = inlink->h;
  61. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  62. return 0;
  63. }
  64. static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
  65. {
  66. if (av_cmp_q(link->frame_rate, (AVRational){30000, 1001}) == 0 &&
  67. av_cmp_q(link->time_base, (AVRational){1001, 60000}) <= 0
  68. ) {
  69. f->pts = pts + av_rescale_q(fields, (AVRational){1001, 60000}, link->time_base);
  70. } else
  71. f->pts = AV_NOPTS_VALUE;
  72. }
  73. static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
  74. AVFilterContext *ctx = inlink->dst;
  75. AVFilterLink *outlink = inlink->dst->outputs[0];
  76. RepeatFieldsContext *s = ctx->priv;
  77. AVFrame *out;
  78. int ret, i;
  79. int state = s->state;
  80. if (!s->frame) {
  81. s->frame = av_frame_clone(in);
  82. if (!s->frame)
  83. return AVERROR(ENOMEM);
  84. s->frame->pts = AV_NOPTS_VALUE;
  85. }
  86. out = s->frame;
  87. if ((state == 0 && !in->top_field_first) ||
  88. (state == 1 && in->top_field_first)) {
  89. av_log(ctx, AV_LOG_WARNING, "Unexpected field flags: "
  90. "state=%d top_field_first=%d repeat_first_field=%d\n",
  91. state, in->top_field_first, in->repeat_pict);
  92. state ^= 1;
  93. }
  94. if (state == 0) {
  95. AVFrame *new;
  96. new = av_frame_clone(in);
  97. if (!new)
  98. return AVERROR(ENOMEM);
  99. ret = ff_filter_frame(outlink, new);
  100. if (in->repeat_pict) {
  101. av_frame_make_writable(out);
  102. update_pts(outlink, out, in->pts, 2);
  103. for (i = 0; i < s->nb_planes; i++) {
  104. av_image_copy_plane(out->data[i], out->linesize[i] * 2,
  105. in->data[i], in->linesize[i] * 2,
  106. s->linesize[i], s->planeheight[i] / 2);
  107. }
  108. state = 1;
  109. }
  110. } else {
  111. for (i = 0; i < s->nb_planes; i++) {
  112. av_frame_make_writable(out);
  113. av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
  114. in->data[i] + in->linesize[i], in->linesize[i] * 2,
  115. s->linesize[i], s->planeheight[i] / 2);
  116. }
  117. ret = ff_filter_frame(outlink, av_frame_clone(out));
  118. if (in->repeat_pict) {
  119. AVFrame *new;
  120. new = av_frame_clone(in);
  121. if (!new)
  122. return AVERROR(ENOMEM);
  123. ret = ff_filter_frame(outlink, new);
  124. state = 0;
  125. } else {
  126. av_frame_make_writable(out);
  127. update_pts(outlink, out, in->pts, 1);
  128. for (i = 0; i < s->nb_planes; i++) {
  129. av_image_copy_plane(out->data[i], out->linesize[i] * 2,
  130. in->data[i], in->linesize[i] * 2,
  131. s->linesize[i], s->planeheight[i] / 2);
  132. }
  133. }
  134. }
  135. s->state = state;
  136. av_frame_free(&in);
  137. return ret;
  138. }
  139. static const AVFilterPad repeatfields_inputs[] = {
  140. {
  141. .name = "default",
  142. .type = AVMEDIA_TYPE_VIDEO,
  143. .filter_frame = filter_frame,
  144. .config_props = config_input,
  145. },
  146. { NULL }
  147. };
  148. static const AVFilterPad repeatfields_outputs[] = {
  149. {
  150. .name = "default",
  151. .type = AVMEDIA_TYPE_VIDEO,
  152. },
  153. { NULL }
  154. };
  155. AVFilter ff_vf_repeatfields = {
  156. .name = "repeatfields",
  157. .description = NULL_IF_CONFIG_SMALL("Hard repeat fields based on MPEG repeat field flag."),
  158. .priv_size = sizeof(RepeatFieldsContext),
  159. .uninit = uninit,
  160. .inputs = repeatfields_inputs,
  161. .outputs = repeatfields_outputs,
  162. .query_formats = query_formats,
  163. };