vf_extractplanes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2013 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avfilter.h"
  25. #include "drawutils.h"
  26. #include "internal.h"
  27. #define PLANE_R 0x01
  28. #define PLANE_G 0x02
  29. #define PLANE_B 0x04
  30. #define PLANE_A 0x08
  31. #define PLANE_Y 0x10
  32. #define PLANE_U 0x20
  33. #define PLANE_V 0x40
  34. typedef struct {
  35. const AVClass *class;
  36. int requested_planes;
  37. int map[4];
  38. int linesize[4];
  39. int is_packed_rgb;
  40. int depth;
  41. int step;
  42. } ExtractPlanesContext;
  43. #define OFFSET(x) offsetof(ExtractPlanesContext, x)
  44. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  45. static const AVOption extractplanes_options[] = {
  46. { "planes", "set planes", OFFSET(requested_planes), AV_OPT_TYPE_FLAGS, {.i64=1}, 1, 0xff, FLAGS, "flags"},
  47. { "y", "set luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, "flags"},
  48. { "u", "set u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, "flags"},
  49. { "v", "set v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, "flags"},
  50. { "r", "set red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, "flags"},
  51. { "g", "set green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, "flags"},
  52. { "b", "set blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, "flags"},
  53. { "a", "set alpha plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_A}, 0, 0, FLAGS, "flags"},
  54. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(extractplanes);
  57. static int query_formats(AVFilterContext *ctx)
  58. {
  59. static const enum AVPixelFormat in_pixfmts[] = {
  60. AV_PIX_FMT_YUV410P,
  61. AV_PIX_FMT_YUV411P,
  62. AV_PIX_FMT_YUV440P,
  63. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
  64. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
  65. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  66. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  67. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
  68. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
  69. AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
  70. AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
  71. AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
  72. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
  73. AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
  74. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
  75. AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE,
  76. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  77. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  78. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  79. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
  80. AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
  81. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
  82. AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
  83. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  84. AV_PIX_FMT_GBRP16LE, AV_PIX_FMT_GBRP16BE,
  85. AV_PIX_FMT_GBRAP16LE, AV_PIX_FMT_GBRAP16BE,
  86. AV_PIX_FMT_NONE,
  87. };
  88. static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  89. static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
  90. static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
  91. const enum AVPixelFormat *out_pixfmts;
  92. const AVPixFmtDescriptor *desc;
  93. AVFilterFormats *avff;
  94. int i, depth = 0, be = 0;
  95. if (!ctx->inputs[0]->in_formats ||
  96. !ctx->inputs[0]->in_formats->nb_formats) {
  97. return AVERROR(EAGAIN);
  98. }
  99. if (!ctx->inputs[0]->out_formats)
  100. ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats);
  101. avff = ctx->inputs[0]->in_formats;
  102. desc = av_pix_fmt_desc_get(avff->formats[0]);
  103. depth = desc->comp[0].depth_minus1;
  104. be = desc->flags & AV_PIX_FMT_FLAG_BE;
  105. for (i = 1; i < avff->nb_formats; i++) {
  106. desc = av_pix_fmt_desc_get(avff->formats[i]);
  107. if (depth != desc->comp[0].depth_minus1 ||
  108. be != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
  109. return AVERROR(EAGAIN);
  110. }
  111. }
  112. if (depth == 7)
  113. out_pixfmts = out8_pixfmts;
  114. else if (be)
  115. out_pixfmts = out16be_pixfmts;
  116. else
  117. out_pixfmts = out16le_pixfmts;
  118. for (i = 0; i < ctx->nb_outputs; i++)
  119. ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats);
  120. return 0;
  121. }
  122. static int config_input(AVFilterLink *inlink)
  123. {
  124. AVFilterContext *ctx = inlink->dst;
  125. ExtractPlanesContext *s = ctx->priv;
  126. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  127. int plane_avail, ret, i;
  128. uint8_t rgba_map[4];
  129. plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
  130. PLANE_Y |
  131. ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
  132. ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
  133. if (s->requested_planes & ~plane_avail) {
  134. av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
  135. return AVERROR(EINVAL);
  136. }
  137. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  138. return ret;
  139. s->depth = (desc->comp[0].depth_minus1 + 1) >> 3;
  140. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  141. s->is_packed_rgb = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  142. if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
  143. ff_fill_rgba_map(rgba_map, inlink->format);
  144. for (i = 0; i < 4; i++)
  145. s->map[i] = rgba_map[s->map[i]];
  146. }
  147. return 0;
  148. }
  149. static int config_output(AVFilterLink *outlink)
  150. {
  151. AVFilterContext *ctx = outlink->src;
  152. AVFilterLink *inlink = ctx->inputs[0];
  153. ExtractPlanesContext *s = ctx->priv;
  154. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  155. const int output = outlink->srcpad - ctx->output_pads;
  156. if (s->map[output] == 1 || s->map[output] == 2) {
  157. outlink->h = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  158. outlink->w = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  159. }
  160. return 0;
  161. }
  162. static void extract_from_packed(uint8_t *dst, int dst_linesize,
  163. const uint8_t *src, int src_linesize,
  164. int width, int height,
  165. int depth, int step, int comp)
  166. {
  167. int x, y;
  168. for (y = 0; y < height; y++) {
  169. switch (depth) {
  170. case 1:
  171. for (x = 0; x < width; x++)
  172. dst[x] = src[x * step + comp];
  173. break;
  174. case 2:
  175. for (x = 0; x < width; x++) {
  176. dst[x * 2 ] = src[x * step + comp * 2 ];
  177. dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
  178. }
  179. break;
  180. }
  181. dst += dst_linesize;
  182. src += src_linesize;
  183. }
  184. }
  185. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  186. {
  187. AVFilterContext *ctx = inlink->dst;
  188. ExtractPlanesContext *s = ctx->priv;
  189. int i, eof = 0, ret = 0;
  190. for (i = 0; i < ctx->nb_outputs; i++) {
  191. AVFilterLink *outlink = ctx->outputs[i];
  192. const int idx = s->map[i];
  193. AVFrame *out;
  194. if (outlink->closed)
  195. continue;
  196. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  197. if (!out) {
  198. ret = AVERROR(ENOMEM);
  199. break;
  200. }
  201. av_frame_copy_props(out, frame);
  202. if (s->is_packed_rgb) {
  203. extract_from_packed(out->data[0], out->linesize[0],
  204. frame->data[0], frame->linesize[0],
  205. outlink->w, outlink->h,
  206. s->depth,
  207. s->step, idx);
  208. } else {
  209. av_image_copy_plane(out->data[0], out->linesize[0],
  210. frame->data[idx], frame->linesize[idx],
  211. s->linesize[idx], outlink->h);
  212. }
  213. ret = ff_filter_frame(outlink, out);
  214. if (ret == AVERROR_EOF)
  215. eof++;
  216. else if (ret < 0)
  217. break;
  218. }
  219. av_frame_free(&frame);
  220. if (eof == ctx->nb_outputs)
  221. ret = AVERROR_EOF;
  222. else if (ret == AVERROR_EOF)
  223. ret = 0;
  224. return ret;
  225. }
  226. static av_cold int init(AVFilterContext *ctx)
  227. {
  228. ExtractPlanesContext *s = ctx->priv;
  229. int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
  230. int i;
  231. for (i = 0; i < 4; i++) {
  232. char *name;
  233. AVFilterPad pad = { 0 };
  234. if (!(planes & (1 << i)))
  235. continue;
  236. name = av_asprintf("out%d", ctx->nb_outputs);
  237. if (!name)
  238. return AVERROR(ENOMEM);
  239. s->map[ctx->nb_outputs] = i;
  240. pad.name = name;
  241. pad.type = AVMEDIA_TYPE_VIDEO;
  242. pad.config_props = config_output;
  243. ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
  244. }
  245. return 0;
  246. }
  247. static av_cold void uninit(AVFilterContext *ctx)
  248. {
  249. int i;
  250. for (i = 0; i < ctx->nb_outputs; i++)
  251. av_freep(&ctx->output_pads[i].name);
  252. }
  253. static const AVFilterPad extractplanes_inputs[] = {
  254. {
  255. .name = "default",
  256. .type = AVMEDIA_TYPE_VIDEO,
  257. .filter_frame = filter_frame,
  258. .config_props = config_input,
  259. },
  260. { NULL }
  261. };
  262. AVFilter ff_vf_extractplanes = {
  263. .name = "extractplanes",
  264. .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
  265. .priv_size = sizeof(ExtractPlanesContext),
  266. .priv_class = &extractplanes_class,
  267. .init = init,
  268. .uninit = uninit,
  269. .query_formats = query_formats,
  270. .inputs = extractplanes_inputs,
  271. .outputs = NULL,
  272. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  273. };
  274. #if CONFIG_ALPHAEXTRACT_FILTER
  275. static av_cold int init_alphaextract(AVFilterContext *ctx)
  276. {
  277. ExtractPlanesContext *s = ctx->priv;
  278. s->requested_planes = PLANE_A;
  279. return init(ctx);
  280. }
  281. AVFilter ff_vf_alphaextract = {
  282. .name = "alphaextract",
  283. .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  284. "grayscale image component."),
  285. .priv_size = sizeof(ExtractPlanesContext),
  286. .init = init_alphaextract,
  287. .uninit = uninit,
  288. .query_formats = query_formats,
  289. .inputs = extractplanes_inputs,
  290. .outputs = NULL,
  291. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  292. };
  293. #endif /* CONFIG_ALPHAEXTRACT_FILTER */