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.

407 lines
15KB

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