Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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.  
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "drawutils.h"
  27. #include "internal.h"
  28.  
  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.  
  37. typedef struct {
  38.     const AVClass *class;
  39.     int requested_planes;
  40.     int map[4];
  41.     int linesize[4];
  42.     int is_packed;
  43.     int depth;
  44.     int step;
  45. } ExtractPlanesContext;
  46.  
  47. #define OFFSET(x) offsetof(ExtractPlanesContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. static const AVOption extractplanes_options[] = {
  50.     { "planes", "set planes",  OFFSET(requested_planes), AV_OPT_TYPE_FLAGS, {.i64=1}, 1, 0xff, FLAGS, "flags"},
  51.     {      "y", "set luma plane",  0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, "flags"},
  52.     {      "u", "set u plane",     0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, "flags"},
  53.     {      "v", "set v plane",     0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, "flags"},
  54.     {      "r", "set red plane",   0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, "flags"},
  55.     {      "g", "set green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, "flags"},
  56.     {      "b", "set blue plane",  0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, "flags"},
  57.     {      "a", "set alpha plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_A}, 0, 0, FLAGS, "flags"},
  58.     { NULL }
  59. };
  60.  
  61. AVFILTER_DEFINE_CLASS(extractplanes);
  62.  
  63. static int query_formats(AVFilterContext *ctx)
  64. {
  65.     static const enum AVPixelFormat in_pixfmts[] = {
  66.         AV_PIX_FMT_YUV410P,
  67.         AV_PIX_FMT_YUV411P,
  68.         AV_PIX_FMT_YUV440P,
  69.         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
  70.         AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
  71.         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  72.         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  73.         AV_PIX_FMT_YUVJ411P,
  74.         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
  75.         AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
  76.         AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
  77.         AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
  78.         AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
  79.         AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
  80.         AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
  81.         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
  82.         AV_PIX_FMT_YA16LE, AV_PIX_FMT_YA16BE,
  83.         AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE,
  84.         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  85.         AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  86.         AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  87.         AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
  88.         AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
  89.         AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
  90.         AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
  91.         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  92.         AV_PIX_FMT_GBRP16LE, AV_PIX_FMT_GBRP16BE,
  93.         AV_PIX_FMT_GBRAP16LE, AV_PIX_FMT_GBRAP16BE,
  94.         AV_PIX_FMT_NONE,
  95.     };
  96.     static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  97.     static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
  98.     static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
  99.     const enum AVPixelFormat *out_pixfmts;
  100.     const AVPixFmtDescriptor *desc;
  101.     AVFilterFormats *avff;
  102.     int i, depth = 0, be = 0;
  103.  
  104.     if (!ctx->inputs[0]->in_formats ||
  105.         !ctx->inputs[0]->in_formats->nb_formats) {
  106.         return AVERROR(EAGAIN);
  107.     }
  108.  
  109.     if (!ctx->inputs[0]->out_formats)
  110.         ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats);
  111.  
  112.     avff = ctx->inputs[0]->in_formats;
  113.     desc = av_pix_fmt_desc_get(avff->formats[0]);
  114.     depth = desc->comp[0].depth_minus1;
  115.     be = desc->flags & AV_PIX_FMT_FLAG_BE;
  116.     for (i = 1; i < avff->nb_formats; i++) {
  117.         desc = av_pix_fmt_desc_get(avff->formats[i]);
  118.         if (depth != desc->comp[0].depth_minus1 ||
  119.             be    != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
  120.             return AVERROR(EAGAIN);
  121.         }
  122.     }
  123.  
  124.     if (depth == 7)
  125.         out_pixfmts = out8_pixfmts;
  126.     else if (be)
  127.         out_pixfmts = out16be_pixfmts;
  128.     else
  129.         out_pixfmts = out16le_pixfmts;
  130.  
  131.     for (i = 0; i < ctx->nb_outputs; i++)
  132.         ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats);
  133.     return 0;
  134. }
  135.  
  136. static int config_input(AVFilterLink *inlink)
  137. {
  138.     AVFilterContext *ctx = inlink->dst;
  139.     ExtractPlanesContext *s = ctx->priv;
  140.     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  141.     int plane_avail, ret, i;
  142.     uint8_t rgba_map[4];
  143.  
  144.     plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
  145.                                                  PLANE_Y |
  146.                                 ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
  147.                   ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
  148.     if (s->requested_planes & ~plane_avail) {
  149.         av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
  150.         return AVERROR(EINVAL);
  151.     }
  152.     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  153.         return ret;
  154.  
  155.     s->depth = (desc->comp[0].depth_minus1 + 1) >> 3;
  156.     s->step = av_get_padded_bits_per_pixel(desc) >> 3;
  157.     s->is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  158.                     (desc->nb_components > 1);
  159.     if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
  160.         ff_fill_rgba_map(rgba_map, inlink->format);
  161.         for (i = 0; i < 4; i++)
  162.             s->map[i] = rgba_map[s->map[i]];
  163.     }
  164.  
  165.     return 0;
  166. }
  167.  
  168. static int config_output(AVFilterLink *outlink)
  169. {
  170.     AVFilterContext *ctx = outlink->src;
  171.     AVFilterLink *inlink = ctx->inputs[0];
  172.     ExtractPlanesContext *s = ctx->priv;
  173.     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  174.     const int output = outlink->srcpad - ctx->output_pads;
  175.  
  176.     if (s->map[output] == 1 || s->map[output] == 2) {
  177.         outlink->h = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  178.         outlink->w = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  179.     }
  180.  
  181.     return 0;
  182. }
  183.  
  184. static void extract_from_packed(uint8_t *dst, int dst_linesize,
  185.                                 const uint8_t *src, int src_linesize,
  186.                                 int width, int height,
  187.                                 int depth, int step, int comp)
  188. {
  189.     int x, y;
  190.  
  191.     for (y = 0; y < height; y++) {
  192.         switch (depth) {
  193.         case 1:
  194.             for (x = 0; x < width; x++)
  195.                 dst[x] = src[x * step + comp];
  196.             break;
  197.         case 2:
  198.             for (x = 0; x < width; x++) {
  199.                 dst[x * 2    ] = src[x * step + comp * 2    ];
  200.                 dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
  201.             }
  202.             break;
  203.         }
  204.         dst += dst_linesize;
  205.         src += src_linesize;
  206.     }
  207. }
  208.  
  209. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  210. {
  211.     AVFilterContext *ctx = inlink->dst;
  212.     ExtractPlanesContext *s = ctx->priv;
  213.     int i, eof = 0, ret = 0;
  214.  
  215.     for (i = 0; i < ctx->nb_outputs; i++) {
  216.         AVFilterLink *outlink = ctx->outputs[i];
  217.         const int idx = s->map[i];
  218.         AVFrame *out;
  219.  
  220.         if (outlink->closed)
  221.             continue;
  222.  
  223.         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  224.         if (!out) {
  225.             ret = AVERROR(ENOMEM);
  226.             break;
  227.         }
  228.         av_frame_copy_props(out, frame);
  229.  
  230.         if (s->is_packed) {
  231.             extract_from_packed(out->data[0], out->linesize[0],
  232.                                 frame->data[0], frame->linesize[0],
  233.                                 outlink->w, outlink->h,
  234.                                 s->depth,
  235.                                 s->step, idx);
  236.         } else {
  237.             av_image_copy_plane(out->data[0], out->linesize[0],
  238.                                 frame->data[idx], frame->linesize[idx],
  239.                                 s->linesize[idx], outlink->h);
  240.         }
  241.  
  242.         ret = ff_filter_frame(outlink, out);
  243.         if (ret == AVERROR_EOF)
  244.             eof++;
  245.         else if (ret < 0)
  246.             break;
  247.     }
  248.     av_frame_free(&frame);
  249.  
  250.     if (eof == ctx->nb_outputs)
  251.         ret = AVERROR_EOF;
  252.     else if (ret == AVERROR_EOF)
  253.         ret = 0;
  254.     return ret;
  255. }
  256.  
  257. static av_cold int init(AVFilterContext *ctx)
  258. {
  259.     ExtractPlanesContext *s = ctx->priv;
  260.     int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
  261.     int i;
  262.  
  263.     for (i = 0; i < 4; i++) {
  264.         char *name;
  265.         AVFilterPad pad = { 0 };
  266.  
  267.         if (!(planes & (1 << i)))
  268.             continue;
  269.  
  270.         name = av_asprintf("out%d", ctx->nb_outputs);
  271.         if (!name)
  272.             return AVERROR(ENOMEM);
  273.         s->map[ctx->nb_outputs] = i;
  274.         pad.name = name;
  275.         pad.type = AVMEDIA_TYPE_VIDEO;
  276.         pad.config_props = config_output;
  277.  
  278.         ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
  279.     }
  280.  
  281.     return 0;
  282. }
  283.  
  284. static av_cold void uninit(AVFilterContext *ctx)
  285. {
  286.     int i;
  287.  
  288.     for (i = 0; i < ctx->nb_outputs; i++)
  289.         av_freep(&ctx->output_pads[i].name);
  290. }
  291.  
  292. static const AVFilterPad extractplanes_inputs[] = {
  293.     {
  294.         .name         = "default",
  295.         .type         = AVMEDIA_TYPE_VIDEO,
  296.         .filter_frame = filter_frame,
  297.         .config_props = config_input,
  298.     },
  299.     { NULL }
  300. };
  301.  
  302. AVFilter ff_vf_extractplanes = {
  303.     .name          = "extractplanes",
  304.     .description   = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
  305.     .priv_size     = sizeof(ExtractPlanesContext),
  306.     .priv_class    = &extractplanes_class,
  307.     .init          = init,
  308.     .uninit        = uninit,
  309.     .query_formats = query_formats,
  310.     .inputs        = extractplanes_inputs,
  311.     .outputs       = NULL,
  312.     .flags         = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  313. };
  314.  
  315. #if CONFIG_ALPHAEXTRACT_FILTER
  316.  
  317. static av_cold int init_alphaextract(AVFilterContext *ctx)
  318. {
  319.     ExtractPlanesContext *s = ctx->priv;
  320.  
  321.     s->requested_planes = PLANE_A;
  322.  
  323.     return init(ctx);
  324. }
  325.  
  326. AVFilter ff_vf_alphaextract = {
  327.     .name           = "alphaextract",
  328.     .description    = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
  329.                       "grayscale image component."),
  330.     .priv_size      = sizeof(ExtractPlanesContext),
  331.     .init           = init_alphaextract,
  332.     .uninit         = uninit,
  333.     .query_formats  = query_formats,
  334.     .inputs         = extractplanes_inputs,
  335.     .outputs        = NULL,
  336.     .flags          = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  337. };
  338. #endif  /* CONFIG_ALPHAEXTRACT_FILTER */
  339.