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.

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