vf_extractplanes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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;
  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_le[] = {
  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_YUVJ411P,
  68. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
  69. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
  70. AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
  71. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
  72. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
  73. AV_PIX_FMT_YA16LE, AV_PIX_FMT_GRAY16LE,
  74. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  75. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  76. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  77. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  78. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  79. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
  80. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
  81. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  82. AV_PIX_FMT_GBRP16LE, AV_PIX_FMT_GBRAP16LE,
  83. AV_PIX_FMT_NONE,
  84. };
  85. static const enum AVPixelFormat in_pixfmts_be[] = {
  86. AV_PIX_FMT_YUV410P,
  87. AV_PIX_FMT_YUV411P,
  88. AV_PIX_FMT_YUV440P,
  89. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
  90. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
  91. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  92. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  93. AV_PIX_FMT_YUVJ411P,
  94. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
  95. AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
  96. AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
  97. AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
  98. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
  99. AV_PIX_FMT_YA16BE, AV_PIX_FMT_GRAY16BE,
  100. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  101. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  102. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  103. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  104. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  105. AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
  106. AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
  107. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  108. AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRAP16BE,
  109. AV_PIX_FMT_NONE,
  110. };
  111. static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  112. static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
  113. static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
  114. const enum AVPixelFormat *out_pixfmts, *in_pixfmts;
  115. const AVPixFmtDescriptor *desc;
  116. AVFilterFormats *avff;
  117. int i, ret, depth = 0, be = 0;
  118. if (!ctx->inputs[0]->in_formats ||
  119. !ctx->inputs[0]->in_formats->nb_formats) {
  120. return AVERROR(EAGAIN);
  121. }
  122. avff = ctx->inputs[0]->in_formats;
  123. desc = av_pix_fmt_desc_get(avff->formats[0]);
  124. depth = desc->comp[0].depth;
  125. be = desc->flags & AV_PIX_FMT_FLAG_BE;
  126. if (be) {
  127. in_pixfmts = in_pixfmts_be;
  128. } else {
  129. in_pixfmts = in_pixfmts_le;
  130. }
  131. if (!ctx->inputs[0]->out_formats)
  132. if ((ret = ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats)) < 0)
  133. return ret;
  134. for (i = 1; i < avff->nb_formats; i++) {
  135. desc = av_pix_fmt_desc_get(avff->formats[i]);
  136. if (depth != desc->comp[0].depth ||
  137. be != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
  138. return AVERROR(EAGAIN);
  139. }
  140. }
  141. if (depth == 8)
  142. out_pixfmts = out8_pixfmts;
  143. else if (be)
  144. out_pixfmts = out16be_pixfmts;
  145. else
  146. out_pixfmts = out16le_pixfmts;
  147. for (i = 0; i < ctx->nb_outputs; i++)
  148. if ((ret = ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats)) < 0)
  149. return ret;
  150. return 0;
  151. }
  152. static int config_input(AVFilterLink *inlink)
  153. {
  154. AVFilterContext *ctx = inlink->dst;
  155. ExtractPlanesContext *s = ctx->priv;
  156. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  157. int plane_avail, ret, i;
  158. uint8_t rgba_map[4];
  159. plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
  160. PLANE_Y |
  161. ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
  162. ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
  163. if (s->requested_planes & ~plane_avail) {
  164. av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
  165. return AVERROR(EINVAL);
  166. }
  167. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  168. return ret;
  169. s->depth = desc->comp[0].depth >> 3;
  170. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  171. s->is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  172. (desc->nb_components > 1);
  173. if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
  174. ff_fill_rgba_map(rgba_map, inlink->format);
  175. for (i = 0; i < 4; i++)
  176. s->map[i] = rgba_map[s->map[i]];
  177. }
  178. return 0;
  179. }
  180. static int config_output(AVFilterLink *outlink)
  181. {
  182. AVFilterContext *ctx = outlink->src;
  183. AVFilterLink *inlink = ctx->inputs[0];
  184. ExtractPlanesContext *s = ctx->priv;
  185. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  186. const int output = outlink->srcpad - ctx->output_pads;
  187. if (s->map[output] == 1 || s->map[output] == 2) {
  188. outlink->h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  189. outlink->w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  190. }
  191. return 0;
  192. }
  193. static void extract_from_packed(uint8_t *dst, int dst_linesize,
  194. const uint8_t *src, int src_linesize,
  195. int width, int height,
  196. int depth, int step, int comp)
  197. {
  198. int x, y;
  199. for (y = 0; y < height; y++) {
  200. switch (depth) {
  201. case 1:
  202. for (x = 0; x < width; x++)
  203. dst[x] = src[x * step + comp];
  204. break;
  205. case 2:
  206. for (x = 0; x < width; x++) {
  207. dst[x * 2 ] = src[x * step + comp * 2 ];
  208. dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
  209. }
  210. break;
  211. }
  212. dst += dst_linesize;
  213. src += src_linesize;
  214. }
  215. }
  216. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  217. {
  218. AVFilterContext *ctx = inlink->dst;
  219. ExtractPlanesContext *s = ctx->priv;
  220. int i, eof = 0, ret = 0;
  221. for (i = 0; i < ctx->nb_outputs; i++) {
  222. AVFilterLink *outlink = ctx->outputs[i];
  223. const int idx = s->map[i];
  224. AVFrame *out;
  225. if (outlink->status)
  226. continue;
  227. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  228. if (!out) {
  229. ret = AVERROR(ENOMEM);
  230. break;
  231. }
  232. av_frame_copy_props(out, frame);
  233. if (s->is_packed) {
  234. extract_from_packed(out->data[0], out->linesize[0],
  235. frame->data[0], frame->linesize[0],
  236. outlink->w, outlink->h,
  237. s->depth,
  238. s->step, idx);
  239. } else {
  240. av_image_copy_plane(out->data[0], out->linesize[0],
  241. frame->data[idx], frame->linesize[idx],
  242. s->linesize[idx], outlink->h);
  243. }
  244. ret = ff_filter_frame(outlink, out);
  245. if (ret == AVERROR_EOF)
  246. eof++;
  247. else if (ret < 0)
  248. break;
  249. }
  250. av_frame_free(&frame);
  251. if (eof == ctx->nb_outputs)
  252. ret = AVERROR_EOF;
  253. else if (ret == AVERROR_EOF)
  254. ret = 0;
  255. return ret;
  256. }
  257. static av_cold int init(AVFilterContext *ctx)
  258. {
  259. ExtractPlanesContext *s = ctx->priv;
  260. int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
  261. int i;
  262. for (i = 0; i < 4; i++) {
  263. char *name;
  264. AVFilterPad pad = { 0 };
  265. if (!(planes & (1 << i)))
  266. continue;
  267. name = av_asprintf("out%d", ctx->nb_outputs);
  268. if (!name)
  269. return AVERROR(ENOMEM);
  270. s->map[ctx->nb_outputs] = i;
  271. pad.name = name;
  272. pad.type = AVMEDIA_TYPE_VIDEO;
  273. pad.config_props = config_output;
  274. ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
  275. }
  276. return 0;
  277. }
  278. static av_cold void uninit(AVFilterContext *ctx)
  279. {
  280. int i;
  281. for (i = 0; i < ctx->nb_outputs; i++)
  282. av_freep(&ctx->output_pads[i].name);
  283. }
  284. static const AVFilterPad extractplanes_inputs[] = {
  285. {
  286. .name = "default",
  287. .type = AVMEDIA_TYPE_VIDEO,
  288. .filter_frame = filter_frame,
  289. .config_props = config_input,
  290. },
  291. { NULL }
  292. };
  293. AVFilter ff_vf_extractplanes = {
  294. .name = "extractplanes",
  295. .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
  296. .priv_size = sizeof(ExtractPlanesContext),
  297. .priv_class = &extractplanes_class,
  298. .init = init,
  299. .uninit = uninit,
  300. .query_formats = query_formats,
  301. .inputs = extractplanes_inputs,
  302. .outputs = NULL,
  303. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  304. };
  305. #if CONFIG_ALPHAEXTRACT_FILTER
  306. static av_cold int init_alphaextract(AVFilterContext *ctx)
  307. {
  308. ExtractPlanesContext *s = ctx->priv;
  309. s->requested_planes = PLANE_A;
  310. return init(ctx);
  311. }
  312. AVFilter ff_vf_alphaextract = {
  313. .name = "alphaextract",
  314. .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  315. "grayscale image component."),
  316. .priv_size = sizeof(ExtractPlanesContext),
  317. .init = init_alphaextract,
  318. .uninit = uninit,
  319. .query_formats = query_formats,
  320. .inputs = extractplanes_inputs,
  321. .outputs = NULL,
  322. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  323. };
  324. #endif /* CONFIG_ALPHAEXTRACT_FILTER */