vf_il.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 {
  36. const AVClass *class;
  37. enum FilterMode luma_mode, chroma_mode, alpha_mode;
  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_INT, {.i64=0}, 0, 1, FLAGS},
  68. {"ls", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  69. {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  70. {"cs", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  71. {"alpha_swap", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  72. {"as", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_INT, {.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;
  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) && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  83. ff_add_format(&formats, fmt);
  84. }
  85. ff_set_common_formats(ctx, formats);
  86. return 0;
  87. }
  88. static int config_input(AVFilterLink *inlink)
  89. {
  90. IlContext *il = inlink->dst->priv;
  91. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  92. int ret;
  93. il->nb_planes = av_pix_fmt_count_planes(inlink->format);
  94. il->has_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
  95. if ((ret = av_image_fill_linesizes(il->linesize, inlink->format, inlink->w)) < 0)
  96. return ret;
  97. il->chroma_height = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  98. return 0;
  99. }
  100. static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
  101. int dst_linesize, int src_linesize,
  102. enum FilterMode mode, int swap)
  103. {
  104. const int a = swap;
  105. const int b = 1 - a;
  106. const int m = h >> 1;
  107. int y;
  108. switch (mode) {
  109. case MODE_DEINTERLEAVE:
  110. for (y = 0; y < m; y++) {
  111. memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
  112. memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
  113. }
  114. break;
  115. case MODE_NONE:
  116. for (y = 0; y < m; y++) {
  117. memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
  118. memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
  119. }
  120. break;
  121. case MODE_INTERLEAVE:
  122. for (y = 0; y < m; y++) {
  123. memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
  124. memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
  125. }
  126. break;
  127. }
  128. }
  129. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  130. {
  131. IlContext *il = inlink->dst->priv;
  132. AVFilterLink *outlink = inlink->dst->outputs[0];
  133. AVFrame *out;
  134. int comp;
  135. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  136. if (!out) {
  137. av_frame_free(&inpicref);
  138. return AVERROR(ENOMEM);
  139. }
  140. av_frame_copy_props(out, inpicref);
  141. interleave(out->data[0], inpicref->data[0],
  142. il->linesize[0], inlink->h,
  143. out->linesize[0], inpicref->linesize[0],
  144. il->luma_mode, il->luma_swap);
  145. for (comp = 1; comp < (il->nb_planes - il->has_alpha); comp++) {
  146. interleave(out->data[comp], inpicref->data[comp],
  147. il->linesize[comp], il->chroma_height,
  148. out->linesize[comp], inpicref->linesize[comp],
  149. il->chroma_mode, il->chroma_swap);
  150. }
  151. if (il->has_alpha) {
  152. comp = il->nb_planes - 1;
  153. interleave(out->data[comp], inpicref->data[comp],
  154. il->linesize[comp], inlink->h,
  155. out->linesize[comp], inpicref->linesize[comp],
  156. il->alpha_mode, il->alpha_swap);
  157. }
  158. av_frame_free(&inpicref);
  159. return ff_filter_frame(outlink, out);
  160. }
  161. static const AVFilterPad inputs[] = {
  162. {
  163. .name = "default",
  164. .type = AVMEDIA_TYPE_VIDEO,
  165. .filter_frame = filter_frame,
  166. .config_props = config_input,
  167. },
  168. { NULL }
  169. };
  170. static const AVFilterPad outputs[] = {
  171. {
  172. .name = "default",
  173. .type = AVMEDIA_TYPE_VIDEO,
  174. },
  175. { NULL }
  176. };
  177. AVFilter ff_vf_il = {
  178. .name = "il",
  179. .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
  180. .priv_size = sizeof(IlContext),
  181. .query_formats = query_formats,
  182. .inputs = inputs,
  183. .outputs = outputs,
  184. .priv_class = &il_class,
  185. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  186. };