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.

339 lines
11KB

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