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