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.

237 lines
9.1KB

  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/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. enum FilterMode {
  30. MODE_NONE,
  31. MODE_INTERLEAVE,
  32. MODE_DEINTERLEAVE
  33. };
  34. typedef struct {
  35. const AVClass *class;
  36. enum FilterMode luma_mode, chroma_mode, alpha_mode;
  37. int luma_swap, chroma_swap, alpha_swap;
  38. int nb_planes;
  39. int chroma_width, chroma_height;
  40. int width;
  41. } IlContext;
  42. #define OFFSET(x) offsetof(IlContext, x)
  43. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  44. static const AVOption il_options[] = {
  45. {"luma_mode", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
  46. {"l", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
  47. {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "luma_mode"},
  48. {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  49. {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  50. {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  51. {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
  52. {"chroma_mode", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
  53. {"c", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
  54. {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "chroma_mode"},
  55. {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  56. {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  57. {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  58. {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
  59. {"alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
  60. {"a", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
  61. {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "alpha_mode"},
  62. {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  63. {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  64. {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  65. {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
  66. {"luma_swap", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  67. {"ls", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  68. {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  69. {"cs", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  70. {"alpha_swap", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  71. {"as", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
  72. {NULL}
  73. };
  74. AVFILTER_DEFINE_CLASS(il);
  75. static av_cold int init(AVFilterContext *ctx, const char *args)
  76. {
  77. IlContext *il = ctx->priv;
  78. int ret;
  79. il->class = &il_class;
  80. av_opt_set_defaults(il);
  81. if ((ret = av_set_options_string(il, args, "=", ":")) < 0)
  82. return ret;
  83. return 0;
  84. }
  85. static int config_input(AVFilterLink *inlink)
  86. {
  87. IlContext *il = inlink->dst->priv;
  88. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  89. int bpp = av_get_padded_bits_per_pixel(desc) >> 3;
  90. int bytes = FFALIGN(desc->comp[0].depth_minus1 + 1, 8) / 8;
  91. int i;
  92. for (i = 0; i < desc->nb_components; i++)
  93. il->nb_planes = FFMAX(il->nb_planes, desc->comp[i].plane);
  94. il->nb_planes++;
  95. il->chroma_height = inlink->h >> desc->log2_chroma_h;
  96. il->chroma_width = (inlink->w >> desc->log2_chroma_w) * bytes;
  97. il->width = inlink->w * bytes;
  98. if (!(desc->flags & PIX_FMT_PLANAR))
  99. il->width *= bpp;
  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, AVFilterBufferRef *inpicref)
  132. {
  133. IlContext *il = inlink->dst->priv;
  134. AVFilterLink *outlink = inlink->dst->outputs[0];
  135. AVFilterBufferRef *out;
  136. int ret;
  137. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  138. if (!out) {
  139. avfilter_unref_bufferp(&inpicref);
  140. return AVERROR(ENOMEM);
  141. }
  142. avfilter_copy_buffer_ref_props(out, inpicref);
  143. interleave(out->data[0], inpicref->data[0],
  144. il->width, inlink->h,
  145. out->linesize[0], inpicref->linesize[0],
  146. il->luma_mode, il->luma_swap);
  147. if (il->nb_planes > 2) {
  148. interleave(out->data[1], inpicref->data[1],
  149. il->chroma_width, il->chroma_height,
  150. out->linesize[1], inpicref->linesize[1],
  151. il->chroma_mode, il->chroma_swap);
  152. interleave(out->data[2], inpicref->data[2],
  153. il->chroma_width, il->chroma_height,
  154. out->linesize[2], inpicref->linesize[2],
  155. il->chroma_mode, il->chroma_swap);
  156. }
  157. if (il->nb_planes == 2 && il->nb_planes == 4) {
  158. int comp = il->nb_planes - 1;
  159. interleave(out->data[comp], inpicref->data[comp],
  160. il->width, inlink->h,
  161. out->linesize[comp], inpicref->linesize[comp],
  162. il->alpha_mode, il->alpha_swap);
  163. }
  164. ret = ff_filter_frame(outlink, out);
  165. avfilter_unref_bufferp(&inpicref);
  166. return ret;
  167. }
  168. static int query_formats(AVFilterContext *ctx)
  169. {
  170. AVFilterFormats *formats = NULL;
  171. int fmt;
  172. for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
  173. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  174. if (!(desc->flags & PIX_FMT_PAL ||
  175. fmt == AV_PIX_FMT_NV21 ||
  176. fmt == AV_PIX_FMT_NV12))
  177. ff_add_format(&formats, fmt);
  178. }
  179. ff_set_common_formats(ctx, formats);
  180. return 0;
  181. }
  182. static const AVFilterPad inputs[] = {
  183. {
  184. .name = "default",
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. .get_video_buffer = ff_null_get_video_buffer,
  187. .filter_frame = filter_frame,
  188. .config_props = config_input,
  189. },
  190. { NULL }
  191. };
  192. static const AVFilterPad outputs[] = {
  193. {
  194. .name = "default",
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. },
  197. { NULL }
  198. };
  199. AVFilter avfilter_vf_il = {
  200. .name = "il",
  201. .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
  202. .priv_size = sizeof(IlContext),
  203. .init = init,
  204. .query_formats = query_formats,
  205. .inputs = inputs,
  206. .outputs = outputs,
  207. .priv_class = &il_class,
  208. };