vf_extractplanes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "filters.h"
  27. #include "internal.h"
  28. #define PLANE_R 0x01
  29. #define PLANE_G 0x02
  30. #define PLANE_B 0x04
  31. #define PLANE_A 0x08
  32. #define PLANE_Y 0x10
  33. #define PLANE_U 0x20
  34. #define PLANE_V 0x40
  35. typedef struct ExtractPlanesContext {
  36. const AVClass *class;
  37. int requested_planes;
  38. int map[4];
  39. int linesize[4];
  40. int is_packed;
  41. int depth;
  42. int step;
  43. } ExtractPlanesContext;
  44. #define OFFSET(x) offsetof(ExtractPlanesContext, x)
  45. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  46. static const AVOption extractplanes_options[] = {
  47. { "planes", "set planes", OFFSET(requested_planes), AV_OPT_TYPE_FLAGS, {.i64=1}, 1, 0xff, FLAGS, "flags"},
  48. { "y", "set luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, "flags"},
  49. { "u", "set u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, "flags"},
  50. { "v", "set v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, "flags"},
  51. { "r", "set red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, "flags"},
  52. { "g", "set green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, "flags"},
  53. { "b", "set blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, "flags"},
  54. { "a", "set alpha plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_A}, 0, 0, FLAGS, "flags"},
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(extractplanes);
  58. #define EIGHTBIT_FORMATS \
  59. AV_PIX_FMT_YUV410P, \
  60. AV_PIX_FMT_YUV411P, \
  61. AV_PIX_FMT_YUV440P, \
  62. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, \
  63. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P, \
  64. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, \
  65. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, \
  66. AV_PIX_FMT_YUVJ411P, \
  67. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, \
  68. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, \
  69. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, \
  70. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, \
  71. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, \
  72. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0, \
  73. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, \
  74. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP
  75. #define HIGHDEPTH_FORMATS(suf) \
  76. AV_PIX_FMT_YA16##suf, AV_PIX_FMT_GRAY16##suf, \
  77. AV_PIX_FMT_YUV420P16##suf, AV_PIX_FMT_YUVA420P16##suf, \
  78. AV_PIX_FMT_YUV422P16##suf, AV_PIX_FMT_YUVA422P16##suf, \
  79. AV_PIX_FMT_YUV444P16##suf, AV_PIX_FMT_YUVA444P16##suf, \
  80. AV_PIX_FMT_RGB48##suf, AV_PIX_FMT_BGR48##suf, \
  81. AV_PIX_FMT_RGBA64##suf, AV_PIX_FMT_BGRA64##suf, \
  82. AV_PIX_FMT_GBRP16##suf, AV_PIX_FMT_GBRAP16##suf, \
  83. AV_PIX_FMT_YUV420P10##suf, \
  84. AV_PIX_FMT_YUV422P10##suf, \
  85. AV_PIX_FMT_YUV444P10##suf, \
  86. AV_PIX_FMT_YUV440P10##suf, \
  87. AV_PIX_FMT_YUVA420P10##suf, \
  88. AV_PIX_FMT_YUVA422P10##suf, \
  89. AV_PIX_FMT_YUVA444P10##suf, \
  90. AV_PIX_FMT_YUV420P12##suf, \
  91. AV_PIX_FMT_YUV422P12##suf, \
  92. AV_PIX_FMT_YUV444P12##suf, \
  93. AV_PIX_FMT_YUV440P12##suf, \
  94. AV_PIX_FMT_YUVA422P12##suf, \
  95. AV_PIX_FMT_YUVA444P12##suf, \
  96. AV_PIX_FMT_GBRP10##suf, AV_PIX_FMT_GBRAP10##suf, \
  97. AV_PIX_FMT_GBRP12##suf, AV_PIX_FMT_GBRAP12##suf, \
  98. AV_PIX_FMT_YUV420P9##suf, \
  99. AV_PIX_FMT_YUV422P9##suf, \
  100. AV_PIX_FMT_YUV444P9##suf, \
  101. AV_PIX_FMT_YUVA420P9##suf, \
  102. AV_PIX_FMT_YUVA422P9##suf, \
  103. AV_PIX_FMT_YUVA444P9##suf, \
  104. AV_PIX_FMT_GBRP9##suf, \
  105. AV_PIX_FMT_GBRP14##suf, \
  106. AV_PIX_FMT_YUV420P14##suf, \
  107. AV_PIX_FMT_YUV422P14##suf, \
  108. AV_PIX_FMT_YUV444P14##suf
  109. static int query_formats(AVFilterContext *ctx)
  110. {
  111. static const enum AVPixelFormat in_pixfmts_le[] = {
  112. EIGHTBIT_FORMATS,
  113. HIGHDEPTH_FORMATS(LE),
  114. AV_PIX_FMT_NONE,
  115. };
  116. static const enum AVPixelFormat in_pixfmts_be[] = {
  117. EIGHTBIT_FORMATS,
  118. HIGHDEPTH_FORMATS(BE),
  119. AV_PIX_FMT_NONE,
  120. };
  121. static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  122. static const enum AVPixelFormat out9le_pixfmts[] = { AV_PIX_FMT_GRAY9LE, AV_PIX_FMT_NONE };
  123. static const enum AVPixelFormat out9be_pixfmts[] = { AV_PIX_FMT_GRAY9BE, AV_PIX_FMT_NONE };
  124. static const enum AVPixelFormat out10le_pixfmts[] = { AV_PIX_FMT_GRAY10LE, AV_PIX_FMT_NONE };
  125. static const enum AVPixelFormat out10be_pixfmts[] = { AV_PIX_FMT_GRAY10BE, AV_PIX_FMT_NONE };
  126. static const enum AVPixelFormat out12le_pixfmts[] = { AV_PIX_FMT_GRAY12LE, AV_PIX_FMT_NONE };
  127. static const enum AVPixelFormat out12be_pixfmts[] = { AV_PIX_FMT_GRAY12BE, AV_PIX_FMT_NONE };
  128. static const enum AVPixelFormat out14le_pixfmts[] = { AV_PIX_FMT_GRAY14LE, AV_PIX_FMT_NONE };
  129. static const enum AVPixelFormat out14be_pixfmts[] = { AV_PIX_FMT_GRAY14BE, AV_PIX_FMT_NONE };
  130. static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
  131. static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
  132. const enum AVPixelFormat *out_pixfmts, *in_pixfmts;
  133. const AVPixFmtDescriptor *desc;
  134. AVFilterFormats *avff;
  135. int i, ret, depth = 0, be = 0;
  136. if (!ctx->inputs[0]->in_formats ||
  137. !ctx->inputs[0]->in_formats->nb_formats) {
  138. return AVERROR(EAGAIN);
  139. }
  140. avff = ctx->inputs[0]->in_formats;
  141. desc = av_pix_fmt_desc_get(avff->formats[0]);
  142. depth = desc->comp[0].depth;
  143. be = desc->flags & AV_PIX_FMT_FLAG_BE;
  144. if (be) {
  145. in_pixfmts = in_pixfmts_be;
  146. } else {
  147. in_pixfmts = in_pixfmts_le;
  148. }
  149. if (!ctx->inputs[0]->out_formats)
  150. if ((ret = ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats)) < 0)
  151. return ret;
  152. for (i = 1; i < avff->nb_formats; i++) {
  153. desc = av_pix_fmt_desc_get(avff->formats[i]);
  154. if (depth != desc->comp[0].depth ||
  155. be != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
  156. return AVERROR(EAGAIN);
  157. }
  158. }
  159. if (depth == 8)
  160. out_pixfmts = out8_pixfmts;
  161. else if (!be && depth == 9)
  162. out_pixfmts = out9le_pixfmts;
  163. else if (be && depth == 9)
  164. out_pixfmts = out9be_pixfmts;
  165. else if (!be && depth == 10)
  166. out_pixfmts = out10le_pixfmts;
  167. else if (be && depth == 10)
  168. out_pixfmts = out10be_pixfmts;
  169. else if (!be && depth == 12)
  170. out_pixfmts = out12le_pixfmts;
  171. else if (be && depth == 12)
  172. out_pixfmts = out12be_pixfmts;
  173. else if (!be && depth == 14)
  174. out_pixfmts = out14le_pixfmts;
  175. else if (be && depth == 14)
  176. out_pixfmts = out14be_pixfmts;
  177. else if (be)
  178. out_pixfmts = out16be_pixfmts;
  179. else
  180. out_pixfmts = out16le_pixfmts;
  181. for (i = 0; i < ctx->nb_outputs; i++)
  182. if ((ret = ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats)) < 0)
  183. return ret;
  184. return 0;
  185. }
  186. static int config_input(AVFilterLink *inlink)
  187. {
  188. AVFilterContext *ctx = inlink->dst;
  189. ExtractPlanesContext *s = ctx->priv;
  190. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  191. int plane_avail, ret, i;
  192. uint8_t rgba_map[4];
  193. plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
  194. PLANE_Y |
  195. ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
  196. ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
  197. if (s->requested_planes & ~plane_avail) {
  198. av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
  199. return AVERROR(EINVAL);
  200. }
  201. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  202. return ret;
  203. s->depth = desc->comp[0].depth >> 3;
  204. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  205. s->is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  206. (desc->nb_components > 1);
  207. if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
  208. ff_fill_rgba_map(rgba_map, inlink->format);
  209. for (i = 0; i < 4; i++)
  210. s->map[i] = rgba_map[s->map[i]];
  211. }
  212. return 0;
  213. }
  214. static int config_output(AVFilterLink *outlink)
  215. {
  216. AVFilterContext *ctx = outlink->src;
  217. AVFilterLink *inlink = ctx->inputs[0];
  218. ExtractPlanesContext *s = ctx->priv;
  219. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  220. const int output = outlink->srcpad - ctx->output_pads;
  221. if (s->map[output] == 1 || s->map[output] == 2) {
  222. outlink->h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  223. outlink->w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  224. }
  225. return 0;
  226. }
  227. static void extract_from_packed(uint8_t *dst, int dst_linesize,
  228. const uint8_t *src, int src_linesize,
  229. int width, int height,
  230. int depth, int step, int comp)
  231. {
  232. int x, y;
  233. for (y = 0; y < height; y++) {
  234. switch (depth) {
  235. case 1:
  236. for (x = 0; x < width; x++)
  237. dst[x] = src[x * step + comp];
  238. break;
  239. case 2:
  240. for (x = 0; x < width; x++) {
  241. dst[x * 2 ] = src[x * step + comp * 2 ];
  242. dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
  243. }
  244. break;
  245. }
  246. dst += dst_linesize;
  247. src += src_linesize;
  248. }
  249. }
  250. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  251. {
  252. AVFilterContext *ctx = inlink->dst;
  253. ExtractPlanesContext *s = ctx->priv;
  254. int i, eof = 0, ret = 0;
  255. for (i = 0; i < ctx->nb_outputs; i++) {
  256. AVFilterLink *outlink = ctx->outputs[i];
  257. const int idx = s->map[i];
  258. AVFrame *out;
  259. if (ff_outlink_get_status(outlink))
  260. continue;
  261. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  262. if (!out) {
  263. ret = AVERROR(ENOMEM);
  264. break;
  265. }
  266. av_frame_copy_props(out, frame);
  267. if (s->is_packed) {
  268. extract_from_packed(out->data[0], out->linesize[0],
  269. frame->data[0], frame->linesize[0],
  270. outlink->w, outlink->h,
  271. s->depth,
  272. s->step, idx);
  273. } else {
  274. av_image_copy_plane(out->data[0], out->linesize[0],
  275. frame->data[idx], frame->linesize[idx],
  276. s->linesize[idx], outlink->h);
  277. }
  278. ret = ff_filter_frame(outlink, out);
  279. if (ret == AVERROR_EOF)
  280. eof++;
  281. else if (ret < 0)
  282. break;
  283. }
  284. av_frame_free(&frame);
  285. if (eof == ctx->nb_outputs)
  286. ret = AVERROR_EOF;
  287. else if (ret == AVERROR_EOF)
  288. ret = 0;
  289. return ret;
  290. }
  291. static av_cold int init(AVFilterContext *ctx)
  292. {
  293. ExtractPlanesContext *s = ctx->priv;
  294. int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
  295. int i, ret;
  296. for (i = 0; i < 4; i++) {
  297. char *name;
  298. AVFilterPad pad = { 0 };
  299. if (!(planes & (1 << i)))
  300. continue;
  301. name = av_asprintf("out%d", ctx->nb_outputs);
  302. if (!name)
  303. return AVERROR(ENOMEM);
  304. s->map[ctx->nb_outputs] = i;
  305. pad.name = name;
  306. pad.type = AVMEDIA_TYPE_VIDEO;
  307. pad.config_props = config_output;
  308. if ((ret = ff_insert_outpad(ctx, ctx->nb_outputs, &pad)) < 0) {
  309. av_freep(&pad.name);
  310. return ret;
  311. }
  312. }
  313. return 0;
  314. }
  315. static av_cold void uninit(AVFilterContext *ctx)
  316. {
  317. int i;
  318. for (i = 0; i < ctx->nb_outputs; i++)
  319. av_freep(&ctx->output_pads[i].name);
  320. }
  321. static const AVFilterPad extractplanes_inputs[] = {
  322. {
  323. .name = "default",
  324. .type = AVMEDIA_TYPE_VIDEO,
  325. .filter_frame = filter_frame,
  326. .config_props = config_input,
  327. },
  328. { NULL }
  329. };
  330. AVFilter ff_vf_extractplanes = {
  331. .name = "extractplanes",
  332. .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
  333. .priv_size = sizeof(ExtractPlanesContext),
  334. .priv_class = &extractplanes_class,
  335. .init = init,
  336. .uninit = uninit,
  337. .query_formats = query_formats,
  338. .inputs = extractplanes_inputs,
  339. .outputs = NULL,
  340. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  341. };
  342. #if CONFIG_ALPHAEXTRACT_FILTER
  343. static av_cold int init_alphaextract(AVFilterContext *ctx)
  344. {
  345. ExtractPlanesContext *s = ctx->priv;
  346. s->requested_planes = PLANE_A;
  347. return init(ctx);
  348. }
  349. AVFilter ff_vf_alphaextract = {
  350. .name = "alphaextract",
  351. .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  352. "grayscale image component."),
  353. .priv_size = sizeof(ExtractPlanesContext),
  354. .init = init_alphaextract,
  355. .uninit = uninit,
  356. .query_formats = query_formats,
  357. .inputs = extractplanes_inputs,
  358. .outputs = NULL,
  359. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  360. };
  361. #endif /* CONFIG_ALPHAEXTRACT_FILTER */