libavfilter/vf_format.c
fcbed3c7
 /*
3fa77bde
  * Copyright (c) 2007 Bobby Bingham
fcbed3c7
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
dc264e77
  * format and noformat video filters
fcbed3c7
  */
 
1d9c2dc8
 #include <string.h>
 
 #include "libavutil/internal.h"
 #include "libavutil/mem.h"
718c7b18
 #include "libavutil/pixdesc.h"
e67a87ea
 #include "libavutil/opt.h"
 
fcbed3c7
 #include "avfilter.h"
b74a1da4
 #include "formats.h"
9d0bfc50
 #include "internal.h"
c04c533f
 #include "video.h"
fcbed3c7
 
58400ac1
 typedef struct FormatContext {
e67a87ea
     const AVClass *class;
     char *pix_fmts;
45fc73ed
 
fcbed3c7
     /**
45fc73ed
      * pix_fmts parsed into AVPixelFormats and terminated with
      * AV_PIX_FMT_NONE
fcbed3c7
      */
45fc73ed
     enum AVPixelFormat *formats;
fcbed3c7
 } FormatContext;
 
45fc73ed
 static av_cold void uninit(AVFilterContext *ctx)
 {
     FormatContext *s = ctx->priv;
     av_freep(&s->formats);
 }
fcbed3c7
 
d69a4177
 static av_cold int init(AVFilterContext *ctx)
fcbed3c7
 {
cbec213a
     FormatContext *s = ctx->priv;
45fc73ed
     char *cur, *sep;
     int nb_formats = 1;
     int i;
6e9dbee7
     int ret;
45fc73ed
 
d1afd3e1
     if (!s->pix_fmts) {
         av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
12652472
         return AVERROR(EINVAL);
d1afd3e1
     }
12652472
 
45fc73ed
     /* count the formats */
     cur = s->pix_fmts;
     while ((cur = strchr(cur, '|'))) {
         nb_formats++;
         if (*cur)
             cur++;
     }
 
     s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
     if (!s->formats)
         return AVERROR(ENOMEM);
fcbed3c7
 
     /* parse the list of formats */
45fc73ed
     cur = s->pix_fmts;
     for (i = 0; i < nb_formats; i++) {
         sep = strchr(cur, '|');
         if (sep)
             *sep++ = 0;
fcbed3c7
 
6e9dbee7
         if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0)
e26782a9
             return ret;
fcbed3c7
 
45fc73ed
         cur = sep;
     }
     s->formats[nb_formats] = AV_PIX_FMT_NONE;
 
     if (!strcmp(ctx->filter->name, "noformat")) {
         const AVPixFmtDescriptor *desc = NULL;
         enum AVPixelFormat *formats_allowed;
a37f2cc5
         int nb_formats_lavu = 0, nb_formats_allowed = 0;
fcbed3c7
 
45fc73ed
         /* count the formats known to lavu */
         while ((desc = av_pix_fmt_desc_next(desc)))
             nb_formats_lavu++;
 
         formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
         if (!formats_allowed)
             return AVERROR(ENOMEM);
 
         /* for each format known to lavu, check if it's in the list of
          * forbidden formats */
         while ((desc = av_pix_fmt_desc_next(desc))) {
             enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
fcbed3c7
 
45fc73ed
             for (i = 0; i < nb_formats; i++) {
                 if (s->formats[i] == pix_fmt)
                     break;
             }
             if (i < nb_formats)
                 continue;
 
             formats_allowed[nb_formats_allowed++] = pix_fmt;
         }
         formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
         av_freep(&s->formats);
         s->formats = formats_allowed;
fcbed3c7
     }
 
     return 0;
 }
 
45fc73ed
 static int query_formats(AVFilterContext *ctx)
fcbed3c7
 {
45fc73ed
     FormatContext *s = ctx->priv;
     AVFilterFormats *formats = ff_make_format_list(s->formats);
 
     if (!formats)
         return AVERROR(ENOMEM);
fcbed3c7
 
a0854c08
     return ff_set_common_formats(ctx, formats);
fcbed3c7
 }
 
45fc73ed
 
e67a87ea
 #define OFFSET(x) offsetof(FormatContext, x)
 static const AVOption options[] = {
7b7c338e
     { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
b211607b
     { NULL }
e67a87ea
 };
 
c0bc2fed
 #if CONFIG_FORMAT_FILTER
55fd1bbc
 
6008b5ab
 #define format_options options
 AVFILTER_DEFINE_CLASS(format);
e67a87ea
 
568c70e7
 static const AVFilterPad avfilter_vf_format_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
4cf1900b
         .get_video_buffer = ff_null_get_video_buffer,
568c70e7
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_format_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_format = {
b211607b
     .name          = "format",
     .description   = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
fcbed3c7
 
b211607b
     .init          = init,
6e9dbee7
     .uninit        = uninit,
fcbed3c7
 
45fc73ed
     .query_formats = query_formats,
fcbed3c7
 
b211607b
     .priv_size     = sizeof(FormatContext),
     .priv_class    = &format_class,
fcbed3c7
 
b211607b
     .inputs        = avfilter_vf_format_inputs,
     .outputs       = avfilter_vf_format_outputs,
fcbed3c7
 };
c0bc2fed
 #endif /* CONFIG_FORMAT_FILTER */
fcbed3c7
 
c0bc2fed
 #if CONFIG_NOFORMAT_FILTER
55fd1bbc
 
6008b5ab
 #define noformat_options options
 AVFILTER_DEFINE_CLASS(noformat);
e67a87ea
 
568c70e7
 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
4cf1900b
         .get_video_buffer = ff_null_get_video_buffer,
568c70e7
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_noformat = {
b211607b
     .name          = "noformat",
     .description   = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
fcbed3c7
 
b211607b
     .init          = init,
6e9dbee7
     .uninit        = uninit,
fcbed3c7
 
45fc73ed
     .query_formats = query_formats,
fcbed3c7
 
b211607b
     .priv_size     = sizeof(FormatContext),
     .priv_class    = &noformat_class,
fcbed3c7
 
b211607b
     .inputs        = avfilter_vf_noformat_inputs,
     .outputs       = avfilter_vf_noformat_outputs,
fcbed3c7
 };
c0bc2fed
 #endif /* CONFIG_NOFORMAT_FILTER */