vf_il.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2013 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * (de)interleave fields filter
  24. */
  25. #include "libavutil/opt.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. enum FilterMode {
  31. MODE_NONE,
  32. MODE_INTERLEAVE,
  33. MODE_DEINTERLEAVE
  34. };
  35. typedef struct IlContext {
  36. const AVClass *class;
  37. int luma_mode, chroma_mode, alpha_mode; ///<FilterMode
  38. int luma_swap, chroma_swap, alpha_swap;
  39. int nb_planes;
  40. int linesize[4], chroma_height;
  41. int has_alpha;
  42. } IlContext;
  43. #define OFFSET(x) offsetof(IlContext, x)
  44. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  45. static const AVOption il_options[] = {
  46. {"luma_mode", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
  47. {"l", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
  48. {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "luma_mode"},
  49. {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  50. {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  51. {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  52. {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  53. {"chroma_mode", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
  54. {"c", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
  55. {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "chroma_mode"},
  56. {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  57. {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  58. {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  59. {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  60. {"alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
  61. {"a", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
  62. {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "alpha_mode"},
  63. {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  64. {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  65. {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  66. {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  67. {"luma_swap", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  68. {"ls", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  69. {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  70. {"cs", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  71. {"alpha_swap", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  72. {"as", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  73. {NULL}
  74. };
  75. AVFILTER_DEFINE_CLASS(il);
  76. static int query_formats(AVFilterContext *ctx)
  77. {
  78. AVFilterFormats *formats = NULL;
  79. int fmt, ret;
  80. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  81. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  82. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
  83. !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
  84. (ret = ff_add_format(&formats, fmt)) < 0)
  85. return ret;
  86. }
  87. return ff_set_common_formats(ctx, formats);
  88. }
  89. static int config_input(AVFilterLink *inlink)
  90. {
  91. IlContext *s = inlink->dst->priv;
  92. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  93. int ret;
  94. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  95. s->has_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
  96. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  97. return ret;
  98. s->chroma_height = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  99. return 0;
  100. }
  101. static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
  102. int dst_linesize, int src_linesize,
  103. enum FilterMode mode, int swap)
  104. {
  105. const int a = swap;
  106. const int b = 1 - a;
  107. const int m = h >> 1;
  108. int y;
  109. switch (mode) {
  110. case MODE_DEINTERLEAVE:
  111. for (y = 0; y < m; y++) {
  112. memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
  113. memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
  114. }
  115. break;
  116. case MODE_NONE:
  117. for (y = 0; y < m; y++) {
  118. memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
  119. memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
  120. }
  121. break;
  122. case MODE_INTERLEAVE:
  123. for (y = 0; y < m; y++) {
  124. memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
  125. memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
  126. }
  127. break;
  128. }
  129. }
  130. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  131. {
  132. IlContext *s = inlink->dst->priv;
  133. AVFilterLink *outlink = inlink->dst->outputs[0];
  134. AVFrame *out;
  135. int comp;
  136. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  137. if (!out) {
  138. av_frame_free(&inpicref);
  139. return AVERROR(ENOMEM);
  140. }
  141. av_frame_copy_props(out, inpicref);
  142. interleave(out->data[0], inpicref->data[0],
  143. s->linesize[0], inlink->h,
  144. out->linesize[0], inpicref->linesize[0],
  145. s->luma_mode, s->luma_swap);
  146. for (comp = 1; comp < (s->nb_planes - s->has_alpha); comp++) {
  147. interleave(out->data[comp], inpicref->data[comp],
  148. s->linesize[comp], s->chroma_height,
  149. out->linesize[comp], inpicref->linesize[comp],
  150. s->chroma_mode, s->chroma_swap);
  151. }
  152. if (s->has_alpha) {
  153. comp = s->nb_planes - 1;
  154. interleave(out->data[comp], inpicref->data[comp],
  155. s->linesize[comp], inlink->h,
  156. out->linesize[comp], inpicref->linesize[comp],
  157. s->alpha_mode, s->alpha_swap);
  158. }
  159. av_frame_free(&inpicref);
  160. return ff_filter_frame(outlink, out);
  161. }
  162. static const AVFilterPad inputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .filter_frame = filter_frame,
  167. .config_props = config_input,
  168. },
  169. { NULL }
  170. };
  171. static const AVFilterPad outputs[] = {
  172. {
  173. .name = "default",
  174. .type = AVMEDIA_TYPE_VIDEO,
  175. },
  176. { NULL }
  177. };
  178. AVFilter ff_vf_il = {
  179. .name = "il",
  180. .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
  181. .priv_size = sizeof(IlContext),
  182. .query_formats = query_formats,
  183. .inputs = inputs,
  184. .outputs = outputs,
  185. .priv_class = &il_class,
  186. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  187. };