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.

405 lines
14KB

  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_GBRP10##suf, AV_PIX_FMT_GBRAP10##suf, \
  95. AV_PIX_FMT_GBRP12##suf, AV_PIX_FMT_GBRAP12##suf, \
  96. AV_PIX_FMT_YUV420P9##suf, \
  97. AV_PIX_FMT_YUV422P9##suf, \
  98. AV_PIX_FMT_YUV444P9##suf, \
  99. AV_PIX_FMT_YUVA420P9##suf, \
  100. AV_PIX_FMT_YUVA422P9##suf, \
  101. AV_PIX_FMT_YUVA444P9##suf, \
  102. AV_PIX_FMT_GBRP9##suf, \
  103. AV_PIX_FMT_GBRP14##suf, \
  104. AV_PIX_FMT_YUV420P14##suf, \
  105. AV_PIX_FMT_YUV422P14##suf, \
  106. AV_PIX_FMT_YUV444P14##suf
  107. static int query_formats(AVFilterContext *ctx)
  108. {
  109. static const enum AVPixelFormat in_pixfmts_le[] = {
  110. EIGHTBIT_FORMATS,
  111. HIGHDEPTH_FORMATS(LE),
  112. AV_PIX_FMT_NONE,
  113. };
  114. static const enum AVPixelFormat in_pixfmts_be[] = {
  115. EIGHTBIT_FORMATS,
  116. HIGHDEPTH_FORMATS(BE),
  117. AV_PIX_FMT_NONE,
  118. };
  119. static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  120. static const enum AVPixelFormat out9le_pixfmts[] = { AV_PIX_FMT_GRAY9LE, AV_PIX_FMT_NONE };
  121. static const enum AVPixelFormat out9be_pixfmts[] = { AV_PIX_FMT_GRAY9BE, AV_PIX_FMT_NONE };
  122. static const enum AVPixelFormat out10le_pixfmts[] = { AV_PIX_FMT_GRAY10LE, AV_PIX_FMT_NONE };
  123. static const enum AVPixelFormat out10be_pixfmts[] = { AV_PIX_FMT_GRAY10BE, AV_PIX_FMT_NONE };
  124. static const enum AVPixelFormat out12le_pixfmts[] = { AV_PIX_FMT_GRAY12LE, AV_PIX_FMT_NONE };
  125. static const enum AVPixelFormat out12be_pixfmts[] = { AV_PIX_FMT_GRAY12BE, AV_PIX_FMT_NONE };
  126. static const enum AVPixelFormat out14le_pixfmts[] = { AV_PIX_FMT_GRAY14LE, AV_PIX_FMT_NONE };
  127. static const enum AVPixelFormat out14be_pixfmts[] = { AV_PIX_FMT_GRAY14BE, AV_PIX_FMT_NONE };
  128. static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
  129. static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
  130. const enum AVPixelFormat *out_pixfmts, *in_pixfmts;
  131. const AVPixFmtDescriptor *desc;
  132. AVFilterFormats *avff;
  133. int i, ret, depth = 0, be = 0;
  134. if (!ctx->inputs[0]->in_formats ||
  135. !ctx->inputs[0]->in_formats->nb_formats) {
  136. return AVERROR(EAGAIN);
  137. }
  138. avff = ctx->inputs[0]->in_formats;
  139. desc = av_pix_fmt_desc_get(avff->formats[0]);
  140. depth = desc->comp[0].depth;
  141. be = desc->flags & AV_PIX_FMT_FLAG_BE;
  142. if (be) {
  143. in_pixfmts = in_pixfmts_be;
  144. } else {
  145. in_pixfmts = in_pixfmts_le;
  146. }
  147. if (!ctx->inputs[0]->out_formats)
  148. if ((ret = ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats)) < 0)
  149. return ret;
  150. for (i = 1; i < avff->nb_formats; i++) {
  151. desc = av_pix_fmt_desc_get(avff->formats[i]);
  152. if (depth != desc->comp[0].depth ||
  153. be != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
  154. return AVERROR(EAGAIN);
  155. }
  156. }
  157. if (depth == 8)
  158. out_pixfmts = out8_pixfmts;
  159. else if (!be && depth == 9)
  160. out_pixfmts = out9le_pixfmts;
  161. else if (be && depth == 9)
  162. out_pixfmts = out9be_pixfmts;
  163. else if (!be && depth == 10)
  164. out_pixfmts = out10le_pixfmts;
  165. else if (be && depth == 10)
  166. out_pixfmts = out10be_pixfmts;
  167. else if (!be && depth == 12)
  168. out_pixfmts = out12le_pixfmts;
  169. else if (be && depth == 12)
  170. out_pixfmts = out12be_pixfmts;
  171. else if (!be && depth == 14)
  172. out_pixfmts = out14le_pixfmts;
  173. else if (be && depth == 14)
  174. out_pixfmts = out14be_pixfmts;
  175. else if (be)
  176. out_pixfmts = out16be_pixfmts;
  177. else
  178. out_pixfmts = out16le_pixfmts;
  179. for (i = 0; i < ctx->nb_outputs; i++)
  180. if ((ret = ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats)) < 0)
  181. return ret;
  182. return 0;
  183. }
  184. static int config_input(AVFilterLink *inlink)
  185. {
  186. AVFilterContext *ctx = inlink->dst;
  187. ExtractPlanesContext *s = ctx->priv;
  188. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  189. int plane_avail, ret, i;
  190. uint8_t rgba_map[4];
  191. plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
  192. PLANE_Y |
  193. ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
  194. ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
  195. if (s->requested_planes & ~plane_avail) {
  196. av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
  197. return AVERROR(EINVAL);
  198. }
  199. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  200. return ret;
  201. s->depth = desc->comp[0].depth >> 3;
  202. s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  203. s->is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  204. (desc->nb_components > 1);
  205. if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
  206. ff_fill_rgba_map(rgba_map, inlink->format);
  207. for (i = 0; i < 4; i++)
  208. s->map[i] = rgba_map[s->map[i]];
  209. }
  210. return 0;
  211. }
  212. static int config_output(AVFilterLink *outlink)
  213. {
  214. AVFilterContext *ctx = outlink->src;
  215. AVFilterLink *inlink = ctx->inputs[0];
  216. ExtractPlanesContext *s = ctx->priv;
  217. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  218. const int output = outlink->srcpad - ctx->output_pads;
  219. if (s->map[output] == 1 || s->map[output] == 2) {
  220. outlink->h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  221. outlink->w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  222. }
  223. return 0;
  224. }
  225. static void extract_from_packed(uint8_t *dst, int dst_linesize,
  226. const uint8_t *src, int src_linesize,
  227. int width, int height,
  228. int depth, int step, int comp)
  229. {
  230. int x, y;
  231. for (y = 0; y < height; y++) {
  232. switch (depth) {
  233. case 1:
  234. for (x = 0; x < width; x++)
  235. dst[x] = src[x * step + comp];
  236. break;
  237. case 2:
  238. for (x = 0; x < width; x++) {
  239. dst[x * 2 ] = src[x * step + comp * 2 ];
  240. dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
  241. }
  242. break;
  243. }
  244. dst += dst_linesize;
  245. src += src_linesize;
  246. }
  247. }
  248. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  249. {
  250. AVFilterContext *ctx = inlink->dst;
  251. ExtractPlanesContext *s = ctx->priv;
  252. int i, eof = 0, ret = 0;
  253. for (i = 0; i < ctx->nb_outputs; i++) {
  254. AVFilterLink *outlink = ctx->outputs[i];
  255. const int idx = s->map[i];
  256. AVFrame *out;
  257. if (ff_outlink_get_status(outlink))
  258. continue;
  259. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  260. if (!out) {
  261. ret = AVERROR(ENOMEM);
  262. break;
  263. }
  264. av_frame_copy_props(out, frame);
  265. if (s->is_packed) {
  266. extract_from_packed(out->data[0], out->linesize[0],
  267. frame->data[0], frame->linesize[0],
  268. outlink->w, outlink->h,
  269. s->depth,
  270. s->step, idx);
  271. } else {
  272. av_image_copy_plane(out->data[0], out->linesize[0],
  273. frame->data[idx], frame->linesize[idx],
  274. s->linesize[idx], outlink->h);
  275. }
  276. ret = ff_filter_frame(outlink, out);
  277. if (ret == AVERROR_EOF)
  278. eof++;
  279. else if (ret < 0)
  280. break;
  281. }
  282. av_frame_free(&frame);
  283. if (eof == ctx->nb_outputs)
  284. ret = AVERROR_EOF;
  285. else if (ret == AVERROR_EOF)
  286. ret = 0;
  287. return ret;
  288. }
  289. static av_cold int init(AVFilterContext *ctx)
  290. {
  291. ExtractPlanesContext *s = ctx->priv;
  292. int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
  293. int i, ret;
  294. for (i = 0; i < 4; i++) {
  295. char *name;
  296. AVFilterPad pad = { 0 };
  297. if (!(planes & (1 << i)))
  298. continue;
  299. name = av_asprintf("out%d", ctx->nb_outputs);
  300. if (!name)
  301. return AVERROR(ENOMEM);
  302. s->map[ctx->nb_outputs] = i;
  303. pad.name = name;
  304. pad.type = AVMEDIA_TYPE_VIDEO;
  305. pad.config_props = config_output;
  306. if ((ret = ff_insert_outpad(ctx, ctx->nb_outputs, &pad)) < 0) {
  307. av_freep(&pad.name);
  308. return ret;
  309. }
  310. }
  311. return 0;
  312. }
  313. static av_cold void uninit(AVFilterContext *ctx)
  314. {
  315. int i;
  316. for (i = 0; i < ctx->nb_outputs; i++)
  317. av_freep(&ctx->output_pads[i].name);
  318. }
  319. static const AVFilterPad extractplanes_inputs[] = {
  320. {
  321. .name = "default",
  322. .type = AVMEDIA_TYPE_VIDEO,
  323. .filter_frame = filter_frame,
  324. .config_props = config_input,
  325. },
  326. { NULL }
  327. };
  328. AVFilter ff_vf_extractplanes = {
  329. .name = "extractplanes",
  330. .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
  331. .priv_size = sizeof(ExtractPlanesContext),
  332. .priv_class = &extractplanes_class,
  333. .init = init,
  334. .uninit = uninit,
  335. .query_formats = query_formats,
  336. .inputs = extractplanes_inputs,
  337. .outputs = NULL,
  338. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  339. };
  340. #if CONFIG_ALPHAEXTRACT_FILTER
  341. static av_cold int init_alphaextract(AVFilterContext *ctx)
  342. {
  343. ExtractPlanesContext *s = ctx->priv;
  344. s->requested_planes = PLANE_A;
  345. return init(ctx);
  346. }
  347. AVFilter ff_vf_alphaextract = {
  348. .name = "alphaextract",
  349. .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  350. "grayscale image component."),
  351. .priv_size = sizeof(ExtractPlanesContext),
  352. .init = init_alphaextract,
  353. .uninit = uninit,
  354. .query_formats = query_formats,
  355. .inputs = extractplanes_inputs,
  356. .outputs = NULL,
  357. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  358. };
  359. #endif /* CONFIG_ALPHAEXTRACT_FILTER */