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.

231 lines
8.8KB

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