History

fftools/ffmpeg_filter: simplify choose_pix_fmts

The only meaningful difference between choose_pix_fmts and the default code was the inclusion of an extra branch for `keep_pix_fmt` being true. However, in this case, we either: 1. Force the specific `ofp->format` that we inherited from ofilter_bind_ost, or if no format was set: 2. Print an empty format list Both of these goals can be accomplished by simply moving the decision logic to ofilter_bind_ost, to avoid setting any format list when keep_pix_fmt is enabled. This is arguably cleaner as it moves format selection logic to a single function. In the case of branch 1, nothing else needs to be done as we already force the format provided in ofp->format, if any is set. Add an assertion to verify this assumption just in case. (Side note: The "choose_*" family of functions are arguably misnomers, as they should really be called "print_*" - their current behavior is to print the relevant format lists to the `vf/af_format` filter arguments)

Niklas Haas committed 3 years ago

Commit 7d6cd131 parent 7b0eca11 file · +11 −38Browse files

Unified · Split

fftools/ffmpeg_filter.c+11−38
@@ -364,35 +364,6 @@ static void sub2video_update(InputFilterPriv *ifp, int64_t heartbeat_pts, · 363 unmodified lines
364364 ifp->sub2video.initialize = 0;
365365 }
366366
367−/* *dst may return be set to NULL (no pixel format found), a static string or a
368− * string backed by the bprint. Nothing has been written to the AVBPrint in case
369− * NULL is returned. The AVBPrint provided should be clean. */
370−static int choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint,
371− const char **dst)
372−{
373− OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
374−
375− *dst = NULL;
376−
377− if (ofp->flags & OFILTER_FLAG_DISABLE_CONVERT || ofp->format != AV_PIX_FMT_NONE) {
378− *dst = ofp->format == AV_PIX_FMT_NONE ? NULL :
379− av_get_pix_fmt_name(ofp->format);
380− } else if (ofp->formats) {
381− const enum AVPixelFormat *p = ofp->formats;
382−
383− for (; *p != AV_PIX_FMT_NONE; p++) {
384− const char *name = av_get_pix_fmt_name(*p);
385− av_bprintf(bprint, "%s%c", name, p[1] == AV_PIX_FMT_NONE ? '\0' : '|');
386− }
387− if (!av_bprint_is_complete(bprint))
388− return AVERROR(ENOMEM);
389−
390− *dst = bprint->str;
391− }
392−
393− return 0;
394−}
395−
396367 /* Define a function for appending a list of allowed formats
397368 * to an AVBPrint. If nonempty, the list will have a header. */
398369 #define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
@@ -415,8 +386,8 @@ static void choose_ ## name (OutputFilterPriv *ofp, AVBPrint *bprint) \ · 16 unmodified lines
415386 av_bprint_chars(bprint, ':', 1); \
416387 }
417388
418−//DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
419−// av_get_pix_fmt_name)
389+DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats,
390+ AV_PIX_FMT_NONE, "%s", av_get_pix_fmt_name)
420391
421392 DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
422393 AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
@@ -850,7 +821,8 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost, · 427 unmodified lines
850821 ofp->format = opts->format;
851822 } else if (opts->pix_fmts)
852823 ofp->formats = opts->pix_fmts;
853− else if (opts->enc)
824+ else if (opts->enc &&
825+ !(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT))
854826 ofp->formats = opts->enc->pix_fmts;
855827
856828 fgp->disable_conversions |= !!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT);
@@ -1481,7 +1453,6 @@ static int configure_output_video_filter(FilterGraph *fg, AVFilterGraph *graph, · 624 unmodified lines
14811453 AVBPrint bprint;
14821454 int pad_idx = out->pad_idx;
14831455 int ret;
1484− const char *pix_fmts;
14851456 char name[255];
14861457
14871458 snprintf(name, sizeof(name), "out_%s", ofp->name);
@@ -1515,17 +1486,19 @@ static int configure_output_video_filter(FilterGraph *fg, AVFilterGraph *graph, · 27 unmodified lines
15151486 pad_idx = 0;
15161487 }
15171488
1489+ av_assert0(!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT) ||
1490+ ofp->format != AV_PIX_FMT_NONE || !ofp->formats);
15181491 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1519− ret = choose_pix_fmts(ofilter, &bprint, &pix_fmts);
1520− if (ret < 0)
1521− return ret;
1492+ choose_pix_fmts(ofp, &bprint);
1493+ if (!av_bprint_is_complete(&bprint))
1494+ return AVERROR(ENOMEM);
15221495
1523− if (pix_fmts) {
1496+ if (bprint.len) {
15241497 AVFilterContext *filter;
15251498
15261499 ret = avfilter_graph_create_filter(&filter,
15271500 avfilter_get_by_name("format"),
1528− "format", pix_fmts, NULL, graph);
1501+ "format", bprint.str, NULL, graph);
15291502 av_bprint_finalize(&bprint, NULL);
15301503 if (ret < 0)
15311504 return ret;

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