History

lavfi: add ff_parse_pixel_format() internal function, and use it

Reduce code duplication.

Stefano Sabatini committed 15 years ago

Commit e26782a1 parent 2c6348e4 files · +33 −15Browse files

Unified · Split

4 files
  1. libavfilter/formats.c +15 −0
  2. libavfilter/internal.h +10 −0
  3. libavfilter/vf_format.c +4 −6
  4. libavfilter/vsrc_buffer.c +4 −9
libavfilter/formats.c+15−0
@@ -236,6 +236,21 @@ void avfilter_formats_changeref(AVFilterFormats **oldref, · 235 unmodified lines
236236
237237 /* internal functions for parsing audio format arguments */
238238
239+int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
240+{
241+ char *tail;
242+ int pix_fmt = av_get_pix_fmt(arg);
243+ if (pix_fmt == PIX_FMT_NONE) {
244+ pix_fmt = strtol(arg, &tail, 0);
245+ if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
246+ av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
247+ return AVERROR(EINVAL);
248+ }
249+ }
250+ *ret = pix_fmt;
251+ return 0;
252+}
253+
239254 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
240255 {
241256 char *tail;
libavfilter/internal.h+10−0
@@ -63,6 +63,16 @@ int ff_fmt_is_in(int fmt, const int *fmts); · 62 unmodified lines
6363
6464 /* Functions to parse audio format arguments */
6565
66+/**
67+ * Parse a pixel format.
68+ *
69+ * @param ret pixel format pointer to where the value should be written
70+ * @param arg string to parse
71+ * @param log_ctx log context
72+ * @return 0 in case of success, a negative AVERROR code on error
73+ */
74+int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx);
75+
6676 /**
6777 * Parse a sample rate.
6878 *
libavfilter/vf_format.c+4−6
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525
2626 #include "libavutil/pixdesc.h"
2727 #include "avfilter.h"
28+#include "internal.h"
2829
2930 typedef struct {
3031 /**
@@ -41,7 +42,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 10 unmodified lines
4142 FormatContext *format = ctx->priv;
4243 const char *cur, *sep;
4344 char pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
44− int pix_fmt_name_len;
45+ int pix_fmt_name_len, ret;
4546 enum PixelFormat pix_fmt;
4647
4748 /* parse the list of formats */
@@ -57,12 +58,9 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 9 unmodified lines
5758
5859 memcpy(pix_fmt_name, cur, pix_fmt_name_len);
5960 pix_fmt_name[pix_fmt_name_len] = 0;
60− pix_fmt = av_get_pix_fmt(pix_fmt_name);
6161
62− if (pix_fmt == PIX_FMT_NONE) {
63− av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
64− return -1;
65− }
62+ if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
63+ return ret;
6664
6765 format->listed_pix_fmt_flags[pix_fmt] = 1;
6866 }
libavfilter/vsrc_buffer.c+4−9
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424 */
2525
2626 #include "avfilter.h"
27+#include "internal.h"
2728 #include "avcodec.h"
2829 #include "vsrc_buffer.h"
2930 #include "libavutil/imgutils.h"
@@ -134,7 +135,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 104 unmodified lines
134135 {
135136 BufferSourceContext *c = ctx->priv;
136137 char pix_fmt_str[128];
137− int n = 0;
138+ int ret, n = 0;
138139 *c->sws_param = 0;
139140
140141 if (!args ||
@@ -145,14 +146,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) · 4 unmodified lines
145146 return AVERROR(EINVAL);
146147 }
147148
148− if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
149− char *tail;
150− c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
151− if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
152− av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
153− return AVERROR(EINVAL);
154− }
155− }
149+ if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
150+ return ret;
156151
157152 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
158153 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t