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
364 ifp->sub2video.initialize = 0;364 ifp->sub2video.initialize = 0;
365}365}
366 366
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. */
370static 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
396/* Define a function for appending a list of allowed formats367/* Define a function for appending a list of allowed formats
397 * to an AVBPrint. If nonempty, the list will have a header. */368 * to an AVBPrint. If nonempty, the list will have a header. */
398#define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \369#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
415 av_bprint_chars(bprint, ':', 1); \386 av_bprint_chars(bprint, ':', 1); \
416}387}
417 388
418//DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,389DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats,
419// av_get_pix_fmt_name)390 AV_PIX_FMT_NONE, "%s", av_get_pix_fmt_name)
420 391
421DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,392DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
422 AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)393 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
850 ofp->format = opts->format;821 ofp->format = opts->format;
851 } else if (opts->pix_fmts)822 } else if (opts->pix_fmts)
852 ofp->formats = opts->pix_fmts;823 ofp->formats = opts->pix_fmts;
853 else if (opts->enc)824 else if (opts->enc &&
825 !(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT))
854 ofp->formats = opts->enc->pix_fmts;826 ofp->formats = opts->enc->pix_fmts;
855 827
856 fgp->disable_conversions |= !!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT);828 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
1481 AVBPrint bprint;1453 AVBPrint bprint;
1482 int pad_idx = out->pad_idx;1454 int pad_idx = out->pad_idx;
1483 int ret;1455 int ret;
1484 const char *pix_fmts;
1485 char name[255];1456 char name[255];
1486 1457
1487 snprintf(name, sizeof(name), "out_%s", ofp->name);1458 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
1515 pad_idx = 0;1486 pad_idx = 0;
1516 }1487 }
1517 1488
1489 av_assert0(!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT) ||
1490 ofp->format != AV_PIX_FMT_NONE || !ofp->formats);
1518 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);1491 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1519 ret = choose_pix_fmts(ofilter, &bprint, &pix_fmts);1492 choose_pix_fmts(ofp, &bprint);
1520 if (ret < 0)1493 if (!av_bprint_is_complete(&bprint))
1521 return ret;1494 return AVERROR(ENOMEM);
1522 1495
1523 if (pix_fmts) {1496 if (bprint.len) {
1524 AVFilterContext *filter;1497 AVFilterContext *filter;
1525 1498
1526 ret = avfilter_graph_create_filter(&filter,1499 ret = avfilter_graph_create_filter(&filter,
1527 avfilter_get_by_name("format"),1500 avfilter_get_by_name("format"),
1528 "format", pix_fmts, NULL, graph);1501 "format", bprint.str, NULL, graph);
1529 av_bprint_finalize(&bprint, NULL);1502 av_bprint_finalize(&bprint, NULL);
1530 if (ret < 0)1503 if (ret < 0)
1531 return ret;1504 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