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.

215 lines
8.4KB

  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; fmt < AV_PIX_FMT_NB; fmt++) {
  81. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  82. if (!(desc->flags & PIX_FMT_PAL) && !(desc->flags & PIX_FMT_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 i, ret;
  93. for (i = 0; i < desc->nb_components; i++)
  94. il->nb_planes = FFMAX(il->nb_planes, desc->comp[i].plane);
  95. il->nb_planes++;
  96. il->has_alpha = !!(desc->flags & PIX_FMT_ALPHA);
  97. if ((ret = av_image_fill_linesizes(il->linesize, inlink->format, inlink->w)) < 0)
  98. return ret;
  99. il->chroma_height = inlink->h >> desc->log2_chroma_h;
  100. return 0;
  101. }
  102. static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
  103. int dst_linesize, int src_linesize,
  104. enum FilterMode mode, int swap)
  105. {
  106. const int a = swap;
  107. const int b = 1 - a;
  108. const int m = h >> 1;
  109. int y;
  110. switch (mode) {
  111. case MODE_DEINTERLEAVE:
  112. for (y = 0; y < m; y++) {
  113. memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
  114. memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
  115. }
  116. break;
  117. case MODE_NONE:
  118. for (y = 0; y < m; y++) {
  119. memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
  120. memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
  121. }
  122. break;
  123. case MODE_INTERLEAVE:
  124. for (y = 0; y < m; y++) {
  125. memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
  126. memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
  127. }
  128. break;
  129. }
  130. }
  131. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  132. {
  133. IlContext *il = inlink->dst->priv;
  134. AVFilterLink *outlink = inlink->dst->outputs[0];
  135. AVFrame *out;
  136. int comp;
  137. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  138. if (!out) {
  139. av_frame_free(&inpicref);
  140. return AVERROR(ENOMEM);
  141. }
  142. av_frame_copy_props(out, inpicref);
  143. interleave(out->data[0], inpicref->data[0],
  144. il->linesize[0], inlink->h,
  145. out->linesize[0], inpicref->linesize[0],
  146. il->luma_mode, il->luma_swap);
  147. for (comp = 1; comp < (il->nb_planes - il->has_alpha); comp++) {
  148. interleave(out->data[comp], inpicref->data[comp],
  149. il->linesize[comp], il->chroma_height,
  150. out->linesize[comp], inpicref->linesize[comp],
  151. il->chroma_mode, il->chroma_swap);
  152. }
  153. if (il->has_alpha) {
  154. comp = il->nb_planes - 1;
  155. interleave(out->data[comp], inpicref->data[comp],
  156. il->linesize[comp], inlink->h,
  157. out->linesize[comp], inpicref->linesize[comp],
  158. il->alpha_mode, il->alpha_swap);
  159. }
  160. av_frame_free(&inpicref);
  161. return ff_filter_frame(outlink, out);
  162. }
  163. static const AVFilterPad inputs[] = {
  164. {
  165. .name = "default",
  166. .type = AVMEDIA_TYPE_VIDEO,
  167. .get_video_buffer = ff_null_get_video_buffer,
  168. .filter_frame = filter_frame,
  169. .config_props = config_input,
  170. },
  171. { NULL }
  172. };
  173. static const AVFilterPad outputs[] = {
  174. {
  175. .name = "default",
  176. .type = AVMEDIA_TYPE_VIDEO,
  177. },
  178. { NULL }
  179. };
  180. AVFilter avfilter_vf_il = {
  181. .name = "il",
  182. .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
  183. .priv_size = sizeof(IlContext),
  184. .query_formats = query_formats,
  185. .inputs = inputs,
  186. .outputs = outputs,
  187. .priv_class = &il_class,
  188. };