Something went wrong. Try again.
🎥 Command line media player
Something went wrong. Try again.
C
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024/* * This file is part of mpv. * * mpv 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. * * mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>. */
/// \file/// \ingroup Options
#include "config.h"
#include <stdlib.h>#include <string.h>#include <limits.h>#include <math.h>#include <stdio.h>#include <stdarg.h>#include <limits.h>#include <inttypes.h>#include <assert.h>
#include <libavutil/common.h>
#include "libmpv/client.h"#include "player/client.h"
#include "mpv_talloc.h"#include "common/common.h"#include "common/msg.h"#include "common/msg_control.h"#include "misc/json.h"#include "misc/node.h"#include "m_option.h"#include "m_config_frontend.h"
#if HAVE_DOS_PATHS#define OPTION_PATH_SEPARATOR ';'#else#define OPTION_PATH_SEPARATOR ':'#endif
const char m_option_path_separator = OPTION_PATH_SEPARATOR;
// For integer types: since min/max are floats and may not be able to represent// the real min/max, and since opt.min/.max may use +/-INFINITY, some care has// to be taken. (Also tricky rounding.)#define OPT_INT_MIN(opt, T, Tm) ((opt)->min < (opt)->max \ ? ((opt)->min <= (double)(Tm) ? (Tm) : (T)((opt)->min)) : (Tm))#define OPT_INT_MAX(opt, T, Tm) ((opt)->min < (opt)->max \ ? ((opt)->max >= (double)(Tm) ? (Tm) : (T)((opt)->max)) : (Tm))
int m_option_parse(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ int r = M_OPT_INVALID; if (bstr_equals0(param, "help") && opt->help) { r = opt->help(log, opt, name); if (r < 0) return r; }
r = opt->type->parse(log, opt, name, param, dst); if (r < 0) return r;
if (opt->validate) { r = opt->validate(log, opt, name, dst); if (r < 0) { if (opt->type->free) opt->type->free(dst); return r; } } return 1;}
char *m_option_strerror(int code){ switch (code) { case M_OPT_UNKNOWN: return "option not found"; case M_OPT_MISSING_PARAM: return "option requires parameter"; case M_OPT_INVALID: return "option parameter could not be parsed"; case M_OPT_OUT_OF_RANGE: return "parameter is outside values allowed for option"; case M_OPT_DISALLOW_PARAM: return "option doesn't take a parameter"; default: return "parser error"; }}
int m_option_required_params(const m_option_t *opt){ if (opt->type->flags & M_OPT_TYPE_OPTIONAL_PARAM) return 0; if (opt->flags & M_OPT_OPTIONAL_PARAM) return 0; if (opt->type == &m_option_type_choice) { const struct m_opt_choice_alternatives *alt; for (alt = opt->priv; alt->name; alt++) { if (strcmp(alt->name, "yes") == 0) return 0; } } return 1;}
int m_option_set_node_or_string(struct mp_log *log, const m_option_t *opt, const char *name, void *dst, struct mpv_node *src){ if (src->format == MPV_FORMAT_STRING) { // The af and vf option unfortunately require this, because the // option name includes the "action". bstr optname = bstr0(name), a, b; if (bstr_split_tok(optname, "/", &a, &b)) optname = b; return m_option_parse(log, opt, optname, bstr0(src->u.string), dst); } else { return m_option_set_node(opt, dst, src); }}
// Default function that just does a memcpy
static void copy_opt(const m_option_t *opt, void *dst, const void *src){ if (dst && src) memcpy(dst, src, opt->type->size);}
// Bool
#define VAL(x) (*(bool *)(x))
static int parse_bool(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (bstr_equals0(param, "yes") || !param.len) { if (dst) VAL(dst) = 1; return 1; } if (bstr_equals0(param, "no")) { if (dst) VAL(dst) = 0; return 1; } bool is_help = bstr_equals0(param, "help"); if (is_help) { mp_info(log, "Valid values for %.*s flag are:\n", BSTR_P(name)); } else { mp_fatal(log, "Invalid parameter for %.*s flag: %.*s\n", BSTR_P(name), BSTR_P(param)); mp_info(log, "Valid values are:\n"); } mp_info(log, " yes\n"); mp_info(log, " no\n"); mp_info(log, " (passing nothing)\n"); return is_help ? M_OPT_EXIT : M_OPT_INVALID;}
static char *print_bool(const m_option_t *opt, const void *val){ return talloc_strdup(NULL, VAL(val) ? "yes" : "no");}
static void add_bool(const m_option_t *opt, void *val, double add, bool wrap){ if (fabs(add) < 0.5) return; bool state = !!VAL(val); state = wrap ? !state : add > 0; VAL(val) = state ? 1 : 0;}
static int bool_set(const m_option_t *opt, void *dst, struct mpv_node *src){ if (src->format != MPV_FORMAT_FLAG) return M_OPT_UNKNOWN; VAL(dst) = !!src->u.flag; return 1;}
static int bool_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ dst->format = MPV_FORMAT_FLAG; dst->u.flag = !!VAL(src); return 1;}
static bool bool_equal(const m_option_t *opt, void *a, void *b){ return VAL(a) == VAL(b);}
const m_option_type_t m_option_type_bool = { .name = "Flag", // same as m_option_type_flag; transparent to user .size = sizeof(bool), .flags = M_OPT_TYPE_OPTIONAL_PARAM | M_OPT_TYPE_CHOICE, .parse = parse_bool, .print = print_bool, .copy = copy_opt, .add = add_bool, .set = bool_set, .get = bool_get, .equal = bool_equal,};
#undef VAL
// Flag
#define VAL(x) (*(int *)(x))
static int parse_flag(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ bool bdst = false; int r = parse_bool(log, opt, name, param, &bdst); if (dst) VAL(dst) = bdst; return r;}
static char *print_flag(const m_option_t *opt, const void *val){ return print_bool(opt, &(bool){VAL(val)});}
static void add_flag(const m_option_t *opt, void *val, double add, bool wrap){ bool bval = VAL(val); add_bool(opt, &bval, add, wrap); VAL(val) = bval;}
static int flag_set(const m_option_t *opt, void *dst, struct mpv_node *src){ bool bdst = false; int r = bool_set(opt, &bdst, src); if (r >= 0) VAL(dst) = bdst; return r;}
static int flag_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ return bool_get(opt, ta_parent, dst, &(bool){VAL(src)});}
static bool flag_equal(const m_option_t *opt, void *a, void *b){ return VAL(a) == VAL(b);}
// Only exists for libmpv interopability and should not be used anywhere.const m_option_type_t m_option_type_flag = { // need yes or no in config files .name = "Flag", .size = sizeof(int), .flags = M_OPT_TYPE_OPTIONAL_PARAM | M_OPT_TYPE_CHOICE, .parse = parse_flag, .print = print_flag, .copy = copy_opt, .add = add_flag, .set = flag_set, .get = flag_get, .equal = flag_equal,};
// Integer
#undef VAL
static int clamp_longlong(const m_option_t *opt, long long i_min, long long i_max, void *val){ long long v = *(long long *)val; int r = 0; long long min = OPT_INT_MIN(opt, long long, i_min); long long max = OPT_INT_MAX(opt, long long, i_max); if (v > max) { v = max; r = M_OPT_OUT_OF_RANGE; } if (v < min) { v = min; r = M_OPT_OUT_OF_RANGE; } *(long long *)val = v; return r;}
static int parse_longlong(struct mp_log *log, const m_option_t *opt, long long i_min, long long i_max, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
struct bstr rest; long long tmp_int = bstrtoll(param, &rest, 10); if (rest.len) tmp_int = bstrtoll(param, &rest, 0); if (rest.len) { mp_err(log, "The %.*s option must be an integer: %.*s\n", BSTR_P(name), BSTR_P(param)); return M_OPT_INVALID; }
long long min = OPT_INT_MIN(opt, long long, i_min); if (tmp_int < min) { mp_err(log, "The %.*s option must be >= %lld: %.*s\n", BSTR_P(name), min, BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
long long max = OPT_INT_MAX(opt, long long, i_max); if (tmp_int > max) { mp_err(log, "The %.*s option must be <= %lld: %.*s\n", BSTR_P(name), max, BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
if (dst) *(long long *)dst = tmp_int;
return 1;}
static int clamp_int64(const m_option_t *opt, void *val){ long long tmp = *(int64_t *)val; int r = clamp_longlong(opt, INT64_MIN, INT64_MAX, &tmp); *(int64_t *)val = tmp; return r;}
static int parse_int(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ long long tmp; int r = parse_longlong(log, opt, INT_MIN, INT_MAX, name, param, &tmp); if (r >= 0 && dst) *(int *)dst = tmp; return r;}
static int parse_int64(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ long long tmp; int r = parse_longlong(log, opt, INT64_MIN, INT64_MAX, name, param, &tmp); if (r >= 0 && dst) *(int64_t *)dst = tmp; return r;}
static char *print_int(const m_option_t *opt, const void *val){ if (opt->type->size == sizeof(int64_t)) return talloc_asprintf(NULL, "%"PRId64, *(const int64_t *)val); return talloc_asprintf(NULL, "%d", *(const int *)val);}
static void add_int64(const m_option_t *opt, void *val, double add, bool wrap){ int64_t v = *(int64_t *)val;
clamp_int64(opt, &v);
v = v + add;
bool is64 = opt->type->size == sizeof(int64_t); int64_t nmin = is64 ? INT64_MIN : INT_MIN; int64_t nmax = is64 ? INT64_MAX : INT_MAX;
int64_t min = OPT_INT_MIN(opt, int64_t, nmin); int64_t max = OPT_INT_MAX(opt, int64_t, nmax);
if (v < min) v = wrap ? max : min; if (v > max) v = wrap ? min : max;
*(int64_t *)val = v;}
static void add_int(const m_option_t *opt, void *val, double add, bool wrap){ int64_t tmp = *(int *)val; add_int64(opt, &tmp, add, wrap); *(int *)val = tmp;}
static void multiply_int64(const m_option_t *opt, void *val, double f){ double v = *(int64_t *)val * f; int64_t iv = v; if (v < INT64_MIN) iv = INT64_MIN; if (v >= (double)INT64_MAX) iv = INT64_MAX; *(int64_t *)val = iv; clamp_int64(opt, val);}
static void multiply_int(const m_option_t *opt, void *val, double f){ int64_t tmp = *(int *)val; multiply_int64(opt, &tmp, f); *(int *)val = MPCLAMP(tmp, INT_MIN, INT_MAX);}
static int int64_set(const m_option_t *opt, void *dst, struct mpv_node *src){ if (src->format != MPV_FORMAT_INT64) return M_OPT_UNKNOWN; int64_t val = src->u.int64; if (val < OPT_INT_MIN(opt, int64_t, INT64_MIN)) return M_OPT_OUT_OF_RANGE; if (val > OPT_INT_MAX(opt, int64_t, INT64_MAX)) return M_OPT_OUT_OF_RANGE; *(int64_t *)dst = val; return 1;}
static int int_set(const m_option_t *opt, void *dst, struct mpv_node *src){ int64_t val; int r = int64_set(opt, &val, src); if (r >= 0) { if (val < INT_MIN || val > INT_MAX) return M_OPT_OUT_OF_RANGE; *(int *)dst = val; } return r;}
static int int64_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ dst->format = MPV_FORMAT_INT64; dst->u.int64 = *(int64_t *)src; return 1;}
static int int_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ dst->format = MPV_FORMAT_INT64; dst->u.int64 = *(int *)src; return 1;}
static bool int_equal(const m_option_t *opt, void *a, void *b){ return *(int *)a == *(int *)b;}
static bool int64_equal(const m_option_t *opt, void *a, void *b){ return *(int64_t *)a == *(int64_t *)b;}
const m_option_type_t m_option_type_int = { .name = "Integer", .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(int), .parse = parse_int, .print = print_int, .copy = copy_opt, .add = add_int, .multiply = multiply_int, .set = int_set, .get = int_get, .equal = int_equal,};
const m_option_type_t m_option_type_int64 = { .name = "Integer64", .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(int64_t), .parse = parse_int64, .print = print_int, .copy = copy_opt, .add = add_int64, .multiply = multiply_int64, .set = int64_set, .get = int64_get, .equal = int64_equal,};
static int parse_byte_size(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
struct bstr r; long long tmp_int = bstrtoll(param, &r, 0); int64_t unit = 1; if (r.len) { if (bstrcasecmp0(r, "b") == 0) { unit = 1; } else if (bstrcasecmp0(r, "kib") == 0 || bstrcasecmp0(r, "k") == 0) { unit = 1024; } else if (bstrcasecmp0(r, "mib") == 0 || bstrcasecmp0(r, "m") == 0) { unit = 1024 * 1024; } else if (bstrcasecmp0(r, "gib") == 0 || bstrcasecmp0(r, "g") == 0) { unit = 1024 * 1024 * 1024; } else if (bstrcasecmp0(r, "tib") == 0 || bstrcasecmp0(r, "t") == 0) { unit = 1024 * 1024 * 1024 * 1024LL; } else { mp_err(log, "The %.*s option must be an integer: %.*s\n", BSTR_P(name), BSTR_P(param)); mp_err(log, "The following suffixes are also allowed: " "KiB, MiB, GiB, TiB, B, K, M, G, T.\n"); return M_OPT_INVALID; } }
if (tmp_int < 0) { mp_err(log, "The %.*s option does not support negative numbers: %.*s\n", BSTR_P(name), BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
if (INT64_MAX / unit < tmp_int) { mp_err(log, "The %.*s option overflows: %.*s\n", BSTR_P(name), BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
tmp_int *= unit;
int64_t min = OPT_INT_MIN(opt, int64_t, INT64_MIN); if (tmp_int < min) { mp_err(log, "The %.*s option must be >= %"PRId64": %.*s\n", BSTR_P(name), min, BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
int64_t max = OPT_INT_MAX(opt, int64_t, INT64_MAX); if (tmp_int > max) { mp_err(log, "The %.*s option must be <= %"PRId64": %.*s\n", BSTR_P(name), max, BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
if (dst) *(int64_t *)dst = tmp_int;
return 1;}
char *format_file_size(int64_t size){ double s = size; if (size < 1024) return talloc_asprintf(NULL, "%.0f B", s);
if (size < (1024 * 1024)) return talloc_asprintf(NULL, "%.3f KiB", s / (1024.0));
if (size < (1024 * 1024 * 1024)) return talloc_asprintf(NULL, "%.3f MiB", s / (1024.0 * 1024.0));
if (size < (1024LL * 1024LL * 1024LL * 1024LL)) return talloc_asprintf(NULL, "%.3f GiB", s / (1024.0 * 1024.0 * 1024.0));
return talloc_asprintf(NULL, "%.3f TiB", s / (1024.0 * 1024.0 * 1024.0 * 1024.0));}
static char *pretty_print_byte_size(const m_option_t *opt, const void *val){ return format_file_size(*(int64_t *)val);}
const m_option_type_t m_option_type_byte_size = { .name = "ByteSize", .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(int64_t), .parse = parse_byte_size, .print = print_int, .pretty_print = pretty_print_byte_size, .copy = copy_opt, .add = add_int64, .multiply = multiply_int64, .set = int64_set, .get = int64_get, .equal = int64_equal,};
const char *m_opt_choice_str(const struct m_opt_choice_alternatives *choices, int value){ for (const struct m_opt_choice_alternatives *c = choices; c->name; c++) { if (c->value == value) return c->name; } return NULL;}
static void print_choice_values(struct mp_log *log, const struct m_option *opt){ const struct m_opt_choice_alternatives *alt = opt->priv; for ( ; alt->name; alt++) mp_info(log, " %s\n", alt->name[0] ? alt->name : "(passing nothing)"); if (opt->min < opt->max) mp_info(log, " %g-%g (integer range)\n", opt->min, opt->max);}
static int parse_choice(struct mp_log *log, const struct m_option *opt, struct bstr name, struct bstr param, void *dst){ const struct m_opt_choice_alternatives *alt = opt->priv; for ( ; alt->name; alt++) { if (!bstrcmp0(param, alt->name)) break; } if (!alt->name && param.len == 0) { // allow flag-style options, e.g. "--mute" implies "--mute=yes" for (alt = opt->priv; alt->name; alt++) { if (!strcmp("yes", alt->name)) break; } } if (!alt->name) { if (!bstrcmp0(param, "help")) { mp_info(log, "Valid values for option %.*s are:\n", BSTR_P(name)); print_choice_values(log, opt); return M_OPT_EXIT; } if (param.len == 0) return M_OPT_MISSING_PARAM; if (opt->min < opt->max) { long long val; if (parse_longlong(mp_null_log, opt, INT_MIN, INT_MAX, name, param, &val) == 1) { if (dst) *(int *)dst = val; return 1; } } mp_fatal(log, "Invalid value for option %.*s: %.*s\n", BSTR_P(name), BSTR_P(param)); mp_info(log, "Valid values are:\n"); print_choice_values(log, opt); return M_OPT_INVALID; } if (dst) *(int *)dst = alt->value;
return 1;}
static void choice_get_min_max(const struct m_option *opt, int *min, int *max){ assert(opt->type == &m_option_type_choice); *min = INT_MAX; *max = INT_MIN; for (const struct m_opt_choice_alternatives *alt = opt->priv; alt->name; alt++) { *min = MPMIN(*min, alt->value); *max = MPMAX(*max, alt->value); } if (opt->min < opt->max) { *min = MPMIN(*min, opt->min); *max = MPMAX(*max, opt->max); }}
static void check_choice(int dir, int val, bool *found, int *best, int choice){ if ((dir == -1 && (!(*found) || choice > (*best)) && choice < val) || (dir == +1 && (!(*found) || choice < (*best)) && choice > val)) { *found = true; *best = choice; }}
static void add_choice(const m_option_t *opt, void *val, double add, bool wrap){ assert(opt->type == &m_option_type_choice); int dir = add > 0 ? +1 : -1; bool found = false; int ival = *(int *)val; int best = 0; // init. value unused
if (fabs(add) < 0.5) return;
if (opt->min < opt->max) { int newval = ival + add; if (ival >= opt->min && ival <= opt->max && newval >= opt->min && newval <= opt->max) { found = true; best = newval; } else { check_choice(dir, ival, &found, &best, opt->min); check_choice(dir, ival, &found, &best, opt->max); } }
for (const struct m_opt_choice_alternatives *alt = opt->priv; alt->name; alt++) check_choice(dir, ival, &found, &best, alt->value);
if (!found) { int min, max; choice_get_min_max(opt, &min, &max); best = (dir == -1) ^ wrap ? min : max; }
*(int *)val = best;}
static int choice_set(const m_option_t *opt, void *dst, struct mpv_node *src){ char buf[80]; char *src_str = NULL; if (src->format == MPV_FORMAT_INT64) { snprintf(buf, sizeof(buf), "%" PRId64, src->u.int64); src_str = buf; } else if (src->format == MPV_FORMAT_STRING) { src_str = src->u.string; } else if (src->format == MPV_FORMAT_FLAG) { src_str = src->u.flag ? "yes" : "no"; } if (!src_str) return M_OPT_UNKNOWN; int val = 0; int r = parse_choice(mp_null_log, opt, (bstr){0}, bstr0(src_str), &val); if (r >= 0) *(int *)dst = val; return r;}
static const struct m_opt_choice_alternatives *get_choice(const m_option_t *opt, const void *val, int *out_val){ int v = *(int *)val; const struct m_opt_choice_alternatives *alt; for (alt = opt->priv; alt->name; alt++) { if (alt->value == v) return alt; } if (opt->min < opt->max) { if (v >= opt->min && v <= opt->max) { *out_val = v; return NULL; } } MP_ASSERT_UNREACHABLE();}
static int choice_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ int ival = 0; const struct m_opt_choice_alternatives *alt = get_choice(opt, src, &ival); // If a choice string looks like a number, return it as number if (alt) { char *end = NULL; ival = strtol(alt->name, &end, 10); if (end && !end[0]) alt = NULL; } if (alt) { int b = -1; if (strcmp(alt->name, "yes") == 0) { b = 1; } else if (strcmp(alt->name, "no") == 0) { b = 0; } if (b >= 0) { dst->format = MPV_FORMAT_FLAG; dst->u.flag = b; } else { dst->format = MPV_FORMAT_STRING; dst->u.string = talloc_strdup(ta_parent, alt->name); } } else { dst->format = MPV_FORMAT_INT64; dst->u.int64 = ival; } return 1;}
static char *print_choice(const m_option_t *opt, const void *val){ int ival = 0; const struct m_opt_choice_alternatives *alt = get_choice(opt, val, &ival); return alt ? talloc_strdup(NULL, alt->name) : talloc_asprintf(NULL, "%d", ival);}
const struct m_option_type m_option_type_choice = { .name = "Choice", .size = sizeof(int), .flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE, .parse = parse_choice, .print = print_choice, .copy = copy_opt, .add = add_choice, .set = choice_set, .get = choice_get, .equal = int_equal,};
static int apply_flag(const struct m_option *opt, int *val, bstr flag){ const struct m_opt_choice_alternatives *alt; for (alt = opt->priv; alt->name; alt++) { if (bstr_equals0(flag, alt->name)) { if (*val & alt->value) return M_OPT_INVALID; *val |= alt->value; return 0; } } return M_OPT_UNKNOWN;}
static const char *find_next_flag(const struct m_option *opt, int *val){ const struct m_opt_choice_alternatives *best = NULL; const struct m_opt_choice_alternatives *alt; for (alt = opt->priv; alt->name; alt++) { if (alt->value && (alt->value & (*val)) == alt->value) { if (!best || av_popcount64(alt->value) > av_popcount64(best->value)) best = alt; } } if (best) { *val = *val & ~(unsigned)best->value; return best->name; } *val = 0; // if there are still flags left, there's not much we can do return NULL;}
static int parse_flags(struct mp_log *log, const struct m_option *opt, struct bstr name, struct bstr param, void *dst){ int value = 0; while (param.len) { bstr flag; bstr_split_tok(param, "+", &flag, ¶m); int r = apply_flag(opt, &value, flag); if (r == M_OPT_UNKNOWN) { mp_fatal(log, "Invalid flag for option %.*s: %.*s\n", BSTR_P(name), BSTR_P(flag)); mp_info(log, "Valid flags are:\n"); const struct m_opt_choice_alternatives *alt; for (alt = opt->priv; alt->name; alt++) mp_info(log, " %s\n", alt->name); mp_info(log, "Flags can usually be combined with '+'.\n"); return M_OPT_INVALID; } else if (r < 0) { mp_fatal(log, "Option %.*s: flag '%.*s' conflicts with a previous " "flag value.\n", BSTR_P(name), BSTR_P(flag)); return M_OPT_INVALID; } } if (dst) *(int *)dst = value; return 1;}
static int flags_set(const m_option_t *opt, void *dst, struct mpv_node *src){ int value = 0; if (src->format != MPV_FORMAT_NODE_ARRAY) return M_OPT_UNKNOWN; struct mpv_node_list *srclist = src->u.list; for (int n = 0; n < srclist->num; n++) { if (srclist->values[n].format != MPV_FORMAT_STRING) return M_OPT_INVALID; if (apply_flag(opt, &value, bstr0(srclist->values[n].u.string)) < 0) return M_OPT_INVALID; } *(int *)dst = value; return 0;}
static int flags_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ int value = *(int *)src;
dst->format = MPV_FORMAT_NODE_ARRAY; dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); struct mpv_node_list *list = dst->u.list; while (1) { const char *flag = find_next_flag(opt, &value); if (!flag) break;
struct mpv_node node; node.format = MPV_FORMAT_STRING; node.u.string = (char *)flag; MP_TARRAY_APPEND(list, list->values, list->num, node); }
return 1;}
static char *print_flags(const m_option_t *opt, const void *val){ int value = *(int *)val; char *res = talloc_strdup(NULL, ""); while (1) { const char *flag = find_next_flag(opt, &value); if (!flag) break;
res = talloc_asprintf_append_buffer(res, "%s%s", res[0] ? "+" : "", flag); } return res;}
const struct m_option_type m_option_type_flags = { .name = "Flags", .size = sizeof(int), .parse = parse_flags, .print = print_flags, .copy = copy_opt, .set = flags_set, .get = flags_get, .equal = int_equal,};
// Float
#undef VAL#define VAL(x) (*(double *)(x))
static int clamp_double(const m_option_t *opt, void *val){ double v = VAL(val); int r = 0; if (opt->min < opt->max) { if (v > opt->max) { v = opt->max; r = M_OPT_OUT_OF_RANGE; } if (v < opt->min) { v = opt->min; r = M_OPT_OUT_OF_RANGE; } } // (setting max/min to INFINITY/-INFINITY is allowed) if (!isfinite(v) && v != opt->max && v != opt->min) { v = opt->min; r = M_OPT_OUT_OF_RANGE; } VAL(val) = v; return r;}
static int parse_double(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
struct bstr rest; double tmp_float = bstrtod(param, &rest);
if (bstr_eatstart0(&rest, ":") || bstr_eatstart0(&rest, "/")) tmp_float /= bstrtod(rest, &rest);
if ((opt->flags & M_OPT_DEFAULT_NAN) && bstr_equals0(param, "default")) { tmp_float = NAN; goto done; }
if (rest.len) { mp_err(log, "The %.*s option must be a floating point number or a " "ratio (numerator[:/]denominator): %.*s\n", BSTR_P(name), BSTR_P(param)); return M_OPT_INVALID; }
if (clamp_double(opt, &tmp_float) < 0) { mp_err(log, "The %.*s option is out of range: %.*s\n", BSTR_P(name), BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
done: if (dst) VAL(dst) = tmp_float; return 1;}
static char *print_double(const m_option_t *opt, const void *val){ double f = VAL(val); if (isnan(f) && (opt->flags & M_OPT_DEFAULT_NAN)) return talloc_strdup(NULL, "default"); return talloc_asprintf(NULL, "%f", f);}
static char *pretty_print_double(const m_option_t *opt, const void *val){ double f = VAL(val); if (isnan(f)) return print_double(opt, val); return mp_format_double(NULL, f, 4, false, false, !(opt->flags & M_OPT_FIXED_LEN_PRINT));}
static void add_double(const m_option_t *opt, void *val, double add, bool wrap){ double v = VAL(val);
v = v + add;
double min = opt->min < opt->max ? opt->min : -INFINITY; double max = opt->min < opt->max ? opt->max : +INFINITY;
if (v < min) v = wrap ? max : min; if (v > max) v = wrap ? min : max;
VAL(val) = v;}
static void multiply_double(const m_option_t *opt, void *val, double f){ VAL(val) *= f; clamp_double(opt, val);}
static int double_set(const m_option_t *opt, void *dst, struct mpv_node *src){ double val; if (src->format == MPV_FORMAT_INT64) { // Can't always be represented exactly, but don't care. val = src->u.int64; } else if (src->format == MPV_FORMAT_DOUBLE) { val = src->u.double_; } else { return M_OPT_UNKNOWN; } if (clamp_double(opt, &val) < 0) return M_OPT_OUT_OF_RANGE; VAL(dst) = val; return 1;}
static int double_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ double f = VAL(src); if (isnan(f) && (opt->flags & M_OPT_DEFAULT_NAN)) { dst->format = MPV_FORMAT_STRING; dst->u.string = talloc_strdup(ta_parent, "default"); } else { dst->format = MPV_FORMAT_DOUBLE; dst->u.double_ = f; } return 1;}
static bool double_equal(const m_option_t *opt, void *a, void *b){ double fa = VAL(a), fb = VAL(b); if (isnan(fa) || isnan(fb)) return isnan(fa) == isnan(fb); return fa == fb;}
const m_option_type_t m_option_type_double = { // double precision float or ratio (numerator[:/]denominator) .name = "Double", .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(double), .parse = parse_double, .print = print_double, .pretty_print = pretty_print_double, .copy = copy_opt, .add = add_double, .multiply = multiply_double, .set = double_set, .get = double_get, .equal = double_equal,};
static int parse_double_aspect(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (bstr_equals0(param, "no")) { if (dst) VAL(dst) = 0.0; return 1; }
// Potentially allow -1 but forbid all other negative values. if (opt->defval && *(double *)opt->defval == -1) { if (bstr_equals0(param, "original")) { if (dst) VAL(dst) = -1.0; return 1; }
struct bstr rest; double val = bstrtod(param, &rest); if (bstr_eatstart0(&rest, ":") || bstr_eatstart0(&rest, "/")) val /= bstrtod(rest, &rest); if (val == -1 && dst) { VAL(dst) = -1.0; mp_warn(log, "Setting '%.*s' to '%.*s' is deprecated. " "Set 'original' instead.\n", BSTR_P(param), BSTR_P(name)); return 1; } }
return parse_double(log, opt, name, param, dst);}
const m_option_type_t m_option_type_aspect = { .name = "Aspect", .size = sizeof(double), .flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE, .parse = parse_double_aspect, .print = print_double, .pretty_print = pretty_print_double, .copy = copy_opt, .add = add_double, .multiply = multiply_double, .set = double_set, .get = double_get, .equal = double_equal,};
#undef VAL#define VAL(x) (*(float *)(x))
static int clamp_float(const m_option_t *opt, double *val){ double v = *val; int r = clamp_double(opt, &v); // Handle the case where range is not set and v is finite // but overflows the float range. if (isfinite(v) && v > FLT_MAX) { v = FLT_MAX; r = M_OPT_OUT_OF_RANGE; } if (isfinite(v) && v < -FLT_MAX) { v = -FLT_MAX; r = M_OPT_OUT_OF_RANGE; } *val = v; return r;}
static int parse_float(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ double tmp; int r = parse_double(log, opt, name, param, &tmp);
if (r == 1 && clamp_float(opt, &tmp) < 0) { mp_err(log, "The %.*s option is out of range: %.*s\n", BSTR_P(name), BSTR_P(param)); return M_OPT_OUT_OF_RANGE; }
if (r == 1 && dst) VAL(dst) = tmp; return r;}
static char *print_float(const m_option_t *opt, const void *val){ double tmp = VAL(val); return print_double(opt, &tmp);}
static char *pretty_print_float(const m_option_t *opt, const void *val){ double tmp = VAL(val); return pretty_print_double(opt, &tmp);}
static void add_float(const m_option_t *opt, void *val, double add, bool wrap){ double tmp = VAL(val); add_double(opt, &tmp, add, wrap); clamp_float(opt, &tmp); VAL(val) = tmp;}
static void multiply_float(const m_option_t *opt, void *val, double f){ double tmp = VAL(val); multiply_double(opt, &tmp, f); clamp_float(opt, &tmp); VAL(val) = tmp;}
static int float_set(const m_option_t *opt, void *dst, struct mpv_node *src){ double tmp; int r = double_set(opt, &tmp, src); if (r >= 0 && clamp_double(opt, &tmp) < 0) return M_OPT_OUT_OF_RANGE; if (r >= 0) VAL(dst) = tmp; return r;}
static int float_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ double tmp = VAL(src); return double_get(opt, ta_parent, dst, &tmp);}
static bool float_equal(const m_option_t *opt, void *a, void *b){ return double_equal(opt, &(double){VAL(a)}, &(double){VAL(b)});}
const m_option_type_t m_option_type_float = { // floating point number or ratio (numerator[:/]denominator) .name = "Float", .flags = M_OPT_TYPE_USES_RANGE, .size = sizeof(float), .parse = parse_float, .print = print_float, .pretty_print = pretty_print_float, .copy = copy_opt, .add = add_float, .multiply = multiply_float, .set = float_set, .get = float_get, .equal = float_equal,};
///////////// String
#undef VAL#define VAL(x) (*(char **)(x))
static int parse_str(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (dst) { talloc_free(VAL(dst)); VAL(dst) = bstrdup0(NULL, param); }
return 0;}
static char *print_str(const m_option_t *opt, const void *val){ return talloc_strdup(NULL, VAL(val) ? VAL(val) : "");}
static void copy_str(const m_option_t *opt, void *dst, const void *src){ if (dst && src) talloc_replace(NULL, VAL(dst), VAL(src));}
static int str_set(const m_option_t *opt, void *dst, struct mpv_node *src){ if (src->format != MPV_FORMAT_STRING) return M_OPT_UNKNOWN; char *s = src->u.string; int r = s ? 0 : M_OPT_INVALID; if (r >= 0) copy_str(opt, dst, &s); return r;}
static int str_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ dst->format = MPV_FORMAT_STRING; dst->u.string = talloc_strdup(ta_parent, VAL(src) ? VAL(src) : ""); return 1;}
static bool str_equal(const m_option_t *opt, void *a, void *b){ return bstr_equals(bstr0(VAL(a)), bstr0(VAL(b)));}
static void free_str(void *src){ if (src && VAL(src)) { talloc_free(VAL(src)); VAL(src) = NULL; }}
const m_option_type_t m_option_type_string = { .name = "String", .size = sizeof(char *), .parse = parse_str, .print = print_str, .copy = copy_str, .free = free_str, .set = str_set, .get = str_get, .equal = str_equal,};
//////////// String list
#undef VAL#define VAL(x) (*(char ***)(x))
#define OP_NONE 0#define OP_ADD 1#define OP_PRE 2#define OP_CLR 3#define OP_DEL 4#define OP_TOGGLE 5#define OP_APPEND 6#define OP_REMOVE 7
static void free_str_list(void *dst){ char **d; int i;
if (!dst || !VAL(dst)) return; d = VAL(dst);
for (i = 0; d[i] != NULL; i++) talloc_free(d[i]); talloc_free(d); VAL(dst) = NULL;}
static int str_list_add(char **add, int n, void *dst, int pre){ char **lst = VAL(dst);
int ln; for (ln = 0; lst && lst[ln]; ln++) /**/;
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (ln >= 100) { while (n--) talloc_free(add[n]); talloc_free(add); return 0; }#endif
lst = talloc_realloc(NULL, lst, char *, n + ln + 1);
if (pre) { memmove(&lst[n], lst, ln * sizeof(char *)); memcpy(lst, add, n * sizeof(char *)); } else memcpy(&lst[ln], add, n * sizeof(char *)); // (re-)add NULL-termination lst[ln + n] = NULL;
talloc_free(add);
VAL(dst) = lst;
return 1;}
static struct bstr get_nextsep(struct bstr *ptr, char sep, bool modify){ struct bstr str = *ptr; struct bstr orig = str; for (;;) { int idx = sep ? bstrchr(str, sep) : -1; if (idx > 0 && str.start[idx - 1] == '\\') { if (modify) { memmove(str.start + idx - 1, str.start + idx, str.len - idx); str.len--; str = bstr_cut(str, idx); } else str = bstr_cut(str, idx + 1); } else { str = bstr_cut(str, idx < 0 ? str.len : idx); break; } } *ptr = str; return bstr_splice(orig, 0, str.start - orig.start);}
static int find_list_bstr(char **list, bstr item){ for (int n = 0; list && list[n]; n++) { if (bstr_equals0(item, list[n])) return n; } return -1;}
static char **separate_input_param(const m_option_t *opt, bstr param, int *len, int op){ char separator = opt->priv ? *(char *)opt->priv : OPTION_LIST_SEPARATOR; if (op == OP_APPEND || op == OP_REMOVE) separator = 0; // specially handled struct bstr str = param; int n = *len; while (str.len) { get_nextsep(&str, separator, 0); str = bstr_cut(str, 1); n++; } if (n == 0 && op != OP_NONE) return NULL;
char **list = talloc_array(NULL, char *, n + 2); str = bstrdup(NULL, param); char *ptr = str.start; n = 0;
while (1) {#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if (n >= 100) break;#endif struct bstr el = get_nextsep(&str, separator, 1); list[n] = bstrdup0(NULL, el); n++; if (!str.len) break; str = bstr_cut(str, 1); } list[n] = NULL; *len = n; talloc_free(ptr); return list;}
static int str_list_remove(char **remove, int n, void *dst){ bool found = false; char **list = VAL(dst); for (int i = 0; i < n; i++) { int index = 0; do { index = find_list_bstr(list, bstr0(remove[i])); if (index >= 0) { found = true; char *old = list[index]; for (int j = index; list[j]; j++) list[j] = list[j + 1]; talloc_free(old); } } while (index >= 0); talloc_free(remove[i]); } talloc_free(remove); return found;}
static int parse_str_list_impl(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst, int default_op){ char **res; int op = default_op;
if (bstr_endswith0(name, "-add")) { op = OP_ADD; } else if (bstr_endswith0(name, "-append")) { op = OP_APPEND; } else if (bstr_endswith0(name, "-pre")) { op = OP_PRE; } else if (bstr_endswith0(name, "-clr")) { op = OP_CLR; } else if (bstr_endswith0(name, "-del")) { op = OP_DEL; } else if (bstr_endswith0(name, "-set")) { op = OP_NONE; } else if (bstr_endswith0(name, "-toggle")) { op = OP_TOGGLE; } else if (bstr_endswith0(name, "-remove")) { op = OP_REMOVE; }
if (op == OP_TOGGLE || op == OP_REMOVE) { if (dst) { res = talloc_array(NULL, char *, 2); res[0] = bstrdup0(res, param); res[1] = NULL; bool found = str_list_remove(res, 2, dst); if (found) return 1; } if (op == OP_REMOVE) return 1; // ignore if not found op = OP_APPEND; }
// Clear the list ?? if (op == OP_CLR) { if (dst) free_str_list(dst); return 0; }
// All other ops need a param if (param.len == 0 && op != OP_NONE) return M_OPT_MISSING_PARAM;
if (!dst) return 1;
int n = 0; res = separate_input_param(opt, param, &n, op); if (!res) return M_OPT_INVALID;
switch (op) { case OP_ADD: case OP_APPEND: return str_list_add(res, n, dst, 0); case OP_PRE: return str_list_add(res, n, dst, 1); case OP_DEL: return str_list_remove(res, n, dst); }
if (VAL(dst)) free_str_list(dst); VAL(dst) = res;
if (!res[0]) free_str_list(dst);
return 1;}
static void copy_str_list(const m_option_t *opt, void *dst, const void *src){ int n; char **d, **s;
if (!(dst && src)) return; s = VAL(src);
if (VAL(dst)) free_str_list(dst);
if (!s) { VAL(dst) = NULL; return; }
for (n = 0; s[n] != NULL; n++) /* NOTHING */; d = talloc_array(NULL, char *, n + 1); for (; n >= 0; n--) d[n] = talloc_strdup(NULL, s[n]);
VAL(dst) = d;}
static char *print_str_list(const m_option_t *opt, const void *src){ char **lst = NULL; char *ret = NULL; const char sep = opt->priv ? *(char *)opt->priv : OPTION_LIST_SEPARATOR;
if (!(src && VAL(src))) return talloc_strdup(NULL, ""); lst = VAL(src);
for (int i = 0; lst[i]; i++) { if (ret) ret = talloc_strndup_append_buffer(ret, &sep, 1); ret = talloc_strdup_append_buffer(ret, lst[i]); } return ret;}
static int str_list_set(const m_option_t *opt, void *dst, struct mpv_node *src){ if (src->format != MPV_FORMAT_NODE_ARRAY) return M_OPT_UNKNOWN; struct mpv_node_list *srclist = src->u.list; for (int n = 0; n < srclist->num; n++) { if (srclist->values[n].format != MPV_FORMAT_STRING) return M_OPT_INVALID; } free_str_list(dst); if (srclist->num > 0) { VAL(dst) = talloc_array(NULL, char*, srclist->num + 1); for (int n = 0; n < srclist->num; n++) VAL(dst)[n] = talloc_strdup(NULL, srclist->values[n].u.string); VAL(dst)[srclist->num] = NULL; } return 1;}
static int str_list_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ dst->format = MPV_FORMAT_NODE_ARRAY; dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); struct mpv_node_list *list = dst->u.list; for (int n = 0; VAL(src) && VAL(src)[n]; n++) { struct mpv_node node; node.format = MPV_FORMAT_STRING; node.u.string = talloc_strdup(list, VAL(src)[n]); MP_TARRAY_APPEND(list, list->values, list->num, node); } return 1;}
static int parse_str_list(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ return parse_str_list_impl(log, opt, name, param, dst, OP_NONE);}
static bool str_list_equal(const m_option_t *opt, void *a, void *b){ char **la = VAL(a); char **lb = VAL(b);
bool a_empty = !la || !la[0]; bool b_empty = !lb || !lb[0]; if (a_empty || b_empty) return a_empty == b_empty;
for (int n = 0; la[n] || lb[n]; n++) { if (!la[n] || !lb[n]) return false; if (strcmp(la[n], lb[n]) != 0) return false; }
return true;}
const m_option_type_t m_option_type_string_list = { .name = "String list", .size = sizeof(char **), .parse = parse_str_list, .print = print_str_list, .copy = copy_str_list, .free = free_str_list, .get = str_list_get, .set = str_list_set, .equal = str_list_equal, .actions = (const struct m_option_action[]){ {"add"}, {"append"}, {"clr", M_OPT_TYPE_OPTIONAL_PARAM}, {"del"}, {"pre"}, {"set"}, {"toggle"}, {"remove"}, {0} },};
static int read_subparam(struct mp_log *log, bstr optname, char *termset, bstr *str, bstr *out_subparam);
static int keyvalue_list_find_key(char **lst, bstr str){ for (int n = 0; lst && lst[n] && lst[n + 1]; n += 2) { if (bstr_equals0(str, lst[n])) return n / 2; } return -1;}
static void keyvalue_list_del_key(char **lst, int index){ int count = 0; for (int n = 0; lst && lst[n]; n++) count++; assert(index * 2 + 1 < count); count += 1; // terminating item talloc_free(lst[index * 2 + 0]); talloc_free(lst[index * 2 + 1]); MP_TARRAY_REMOVE_AT(lst, count, index * 2 + 1); MP_TARRAY_REMOVE_AT(lst, count, index * 2 + 0);}
static int parse_keyvalue_list(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ char **lst = NULL; int num = 0; int r = 0; bool append = false; bool full_value = false;
if ((opt->flags & M_OPT_HAVE_HELP) && bstr_equals0(param, "help")) param = bstr0("help=");
int op = 0; if (bstr_endswith0(name, "-del")) { op = OP_DEL; } else if (bstr_endswith0(name, "-remove")) { op = OP_REMOVE; }
if (bstr_endswith0(name, "-add")) { append = true; } else if (bstr_endswith0(name, "-append")) { append = full_value = true; } else if (bstr_endswith0(name, "-clr")) { if (dst) free_str_list(dst); return 0; } else if (op == OP_DEL || op == OP_REMOVE) { int n = 0; char **res = separate_input_param(opt, param, &n, op); if (!res) return M_OPT_INVALID; lst = dst ? VAL(dst) : NULL; for (int i = 0; i < n; i++) { int index = dst ? keyvalue_list_find_key(lst, bstr0(res[i])) : -1; if (index >= 0) { keyvalue_list_del_key(lst, index); VAL(dst) = lst; } talloc_free(res[i]); } talloc_free(res); return 1; }
if (append && dst) { lst = VAL(dst); for (int n = 0; lst && lst[n]; n++) num++; }
while (param.len) { bstr key, val; r = read_subparam(log, name, "=", ¶m, &key); if (r < 0) break; if (!bstr_eatstart0(¶m, "=")) { mp_err(log, "Expected '=' and a value.\n"); r = M_OPT_INVALID; break; } if (full_value) { val = param; param.len = 0; } else { r = read_subparam(log, name, ",", ¶m, &val); if (r < 0) break; } if (dst) { int index = keyvalue_list_find_key(lst, key); if (index >= 0) { keyvalue_list_del_key(lst, index); num -= 2; } MP_TARRAY_APPEND(NULL, lst, num, bstrto0(NULL, key)); MP_TARRAY_APPEND(NULL, lst, num, bstrto0(NULL, val)); MP_TARRAY_APPEND(NULL, lst, num, NULL); num -= 1; }
if (!bstr_eatstart0(¶m, ",") && !bstr_eatstart0(¶m, ":")) break; }
if (param.len) { mp_err(log, "Unparsable garbage at end of option value: '%.*s'\n", BSTR_P(param)); r = M_OPT_INVALID; }
if (dst) { if (!append) free_str_list(dst); VAL(dst) = lst; if (r < 0) free_str_list(dst); } else { free_str_list(&lst); } return r;}
static char *print_keyvalue_list(const m_option_t *opt, const void *src){ char **lst = VAL(src); char *ret = talloc_strdup(NULL, ""); for (int n = 0; lst && lst[n] && lst[n + 1]; n += 2) { if (ret[0]) ret = talloc_strdup_append(ret, ","); ret = talloc_asprintf_append(ret, "%s=%s", lst[n], lst[n + 1]); } return ret;}
static int keyvalue_list_set(const m_option_t *opt, void *dst, struct mpv_node *src){ if (src->format != MPV_FORMAT_NODE_MAP) return M_OPT_UNKNOWN; struct mpv_node_list *srclist = src->u.list; for (int n = 0; n < srclist->num; n++) { if (srclist->values[n].format != MPV_FORMAT_STRING) return M_OPT_INVALID; } free_str_list(dst); if (srclist->num > 0) { VAL(dst) = talloc_array(NULL, char*, (srclist->num + 1) * 2); for (int n = 0; n < srclist->num; n++) { VAL(dst)[n * 2 + 0] = talloc_strdup(NULL, srclist->keys[n]); VAL(dst)[n * 2 + 1] = talloc_strdup(NULL, srclist->values[n].u.string); } VAL(dst)[srclist->num * 2 + 0] = NULL; VAL(dst)[srclist->num * 2 + 1] = NULL; } return 1;}
static int keyvalue_list_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ dst->format = MPV_FORMAT_NODE_MAP; dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); struct mpv_node_list *list = dst->u.list; for (int n = 0; VAL(src) && VAL(src)[n * 2 + 0]; n++) { MP_TARRAY_GROW(list, list->values, list->num); MP_TARRAY_GROW(list, list->keys, list->num); list->keys[list->num] = talloc_strdup(list, VAL(src)[n * 2 + 0]); list->values[list->num] = (struct mpv_node){ .format = MPV_FORMAT_STRING, .u.string = talloc_strdup(list, VAL(src)[n * 2 + 1]), }; list->num++; } return 1;}
const m_option_type_t m_option_type_keyvalue_list = { .name = "Key/value list", .size = sizeof(char **), .parse = parse_keyvalue_list, .print = print_keyvalue_list, .copy = copy_str_list, .free = free_str_list, .get = keyvalue_list_get, .set = keyvalue_list_set, .equal = str_list_equal, .actions = (const struct m_option_action[]){ {"add"}, {"append"}, {"clr", M_OPT_TYPE_OPTIONAL_PARAM}, {"del"}, {"set"}, {"remove"}, {0} },};
#undef VAL#define VAL(x) (*(char **)(x))
static int check_msg_levels(struct mp_log *log, char **list){ for (int n = 0; list && list[n * 2 + 0]; n++) { char *level = list[n * 2 + 1]; if (mp_msg_find_level(level) < 0 && strcmp(level, "no") != 0) { mp_err(log, "Invalid message level '%s'\n", level); return M_OPT_INVALID; } } return 1;}
static int parse_msglevels(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (bstr_equals0(param, "help")) { mp_info(log, "Syntax:\n\n --msg-level=module1=level,module2=level,...\n\n" "'module' is output prefix as shown with -v, or a prefix\n" "of it. level is one of:\n\n" " fatal error warn info status v debug trace\n\n" "The level specifies the minimum log level a message\n" "must have to be printed.\n" "The special module name 'all' affects all modules.\n"); return M_OPT_EXIT; }
char **dst_copy = NULL; int r = m_option_type_keyvalue_list.parse(log, opt, name, param, &dst_copy); if (r >= 0) r = check_msg_levels(log, dst_copy);
if (r >= 0) m_option_type_keyvalue_list.copy(opt, dst, &dst_copy); m_option_type_keyvalue_list.free(&dst_copy); return r;}
static int set_msglevels(const m_option_t *opt, void *dst, struct mpv_node *src){ char **dst_copy = NULL; int r = m_option_type_keyvalue_list.set(opt, &dst_copy, src); if (r >= 0) r = check_msg_levels(mp_null_log, dst_copy);
if (r >= 0) m_option_type_keyvalue_list.copy(opt, dst, &dst_copy); m_option_type_keyvalue_list.free(&dst_copy); return r;}
const m_option_type_t m_option_type_msglevels = { .name = "Output verbosity levels", .size = sizeof(char **), .parse = parse_msglevels, .print = print_keyvalue_list, .copy = copy_str_list, .free = free_str_list, .get = keyvalue_list_get, .set = set_msglevels, .equal = str_list_equal,};
static int parse_print(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ ((m_opt_print_fn) opt->priv)(log); return M_OPT_EXIT;}
const m_option_type_t m_option_type_print_fn = { .name = "Print", .flags = M_OPT_TYPE_OPTIONAL_PARAM, .parse = parse_print,};
static int parse_dummy_flag(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len) { mp_err(log, "Invalid parameter for %.*s flag: %.*s\n", BSTR_P(name), BSTR_P(param)); return M_OPT_DISALLOW_PARAM; } return 0;}
const m_option_type_t m_option_type_dummy_flag = { // can only be activated .name = "Flag", .flags = M_OPT_TYPE_OPTIONAL_PARAM, .parse = parse_dummy_flag,};
#undef VAL
// Read s sub-option name, or a positional sub-opt value.// termset is a string containing the set of chars that terminate an option.// Return 0 on success, M_OPT_ error code otherwise.// optname is for error reporting.static int read_subparam(struct mp_log *log, bstr optname, char *termset, bstr *str, bstr *out_subparam){ bstr p = *str; bstr subparam = {0};
if (bstr_eatstart0(&p, "\"")) { int optlen = bstrcspn(p, "\""); subparam = bstr_splice(p, 0, optlen); p = bstr_cut(p, optlen); if (!bstr_startswith0(p, "\"")) { mp_err(log, "Terminating '\"' missing for '%.*s'\n", BSTR_P(optname)); return M_OPT_INVALID; } p = bstr_cut(p, 1); } else if (bstr_eatstart0(&p, "[")) { bstr s = p; int balance = 1; while (p.len && balance > 0) { if (p.start[0] == '[') { balance++; } else if (p.start[0] == ']') { balance--; } p = bstr_cut(p, 1); } if (balance != 0) { mp_err(log, "Terminating ']' missing for '%.*s'\n", BSTR_P(optname)); return M_OPT_INVALID; } subparam = bstr_splice(s, 0, s.len - p.len - 1); } else if (bstr_eatstart0(&p, "%")) { int optlen = bstrtoll(p, &p, 0); if (!bstr_startswith0(p, "%") || (optlen > p.len - 1)) { mp_err(log, "Invalid length %d for '%.*s'\n", optlen, BSTR_P(optname)); return M_OPT_INVALID; } subparam = bstr_splice(p, 1, optlen + 1); p = bstr_cut(p, optlen + 1); } else { // Skip until the next character that could possibly be a meta // character in option parsing. int optlen = bstrcspn(p, termset); subparam = bstr_splice(p, 0, optlen); p = bstr_cut(p, optlen); }
*str = p; *out_subparam = subparam; return 0;}
// Return 0 on success, otherwise error code// On success, set *out_name and *out_val, and advance *str// out_val.start is NULL if there was no parameter.// optname is for error reporting.static int split_subconf(struct mp_log *log, bstr optname, bstr *str, bstr *out_name, bstr *out_val){ bstr p = *str; bstr subparam = {0}; bstr subopt; int r = read_subparam(log, optname, ":=,\\%\"'[]", &p, &subopt); if (r < 0) return r; if (bstr_eatstart0(&p, "=")) { r = read_subparam(log, subopt, ":=,\\%\"'[]", &p, &subparam); if (r < 0) return r; } *str = p; *out_name = subopt; *out_val = subparam; return 0;}
#undef VAL
// Split the string on the given split character.// out_arr is at least max entries long.// Return number of out_arr entries filled.static int split_char(bstr str, unsigned char split, int max, bstr *out_arr){ if (max < 1) return 0;
int count = 0; while (1) { int next = bstrchr(str, split); if (next >= 0 && max - count > 1) { out_arr[count++] = bstr_splice(str, 0, next); str = bstr_cut(str, next + 1); } else { out_arr[count++] = str; break; } } return count;}
static int parse_color(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
bool is_help = bstr_equals0(param, "help"); if (is_help) goto exit;
bstr val = param; struct m_color color = {0};
if (bstr_eatstart0(&val, "#")) { // #[AA]RRGGBB if (val.len != 6 && val.len != 8) goto exit; bool has_alpha = val.len == 8; uint32_t c = bstrtoll(val, &val, 16); if (val.len) goto exit; color = (struct m_color) { (c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF, has_alpha ? (c >> 24) & 0xFF : 0xFF, }; } else { bstr comp_str[5]; int num = split_char(param, '/', 5, comp_str); if (num < 1 || num > 4) goto exit; double comp[4] = {0, 0, 0, 1}; for (int n = 0; n < num; n++) { bstr rest; double d = bstrtod(comp_str[n], &rest); if (rest.len || !comp_str[n].len || d < 0 || d > 1 || !isfinite(d)) goto exit; comp[n] = d; } if (num == 2) comp[3] = comp[1]; if (num < 3) comp[2] = comp[1] = comp[0]; color = (struct m_color) { comp[0] * 0xFF, comp[1] * 0xFF, comp[2] * 0xFF, comp[3] * 0xFF }; }
if (dst) *((struct m_color *)dst) = color;
return 1;
exit: if (!is_help) { mp_err(log, "Option %.*s: invalid color: '%.*s'\n", BSTR_P(name), BSTR_P(param)); } mp_info(log, "Valid colors must be in the form #RRGGBB or #AARRGGBB (in hex)\n" "or in the form 'r/g/b/a', where each component is a value in the\n" "range 0.0-1.0. (Also allowed: 'gray', 'gray/a', 'r/g/b').\n"); return is_help ? M_OPT_EXIT : M_OPT_INVALID;}
static char *print_color(const m_option_t *opt, const void *val){ const struct m_color *c = val; return talloc_asprintf(NULL, "#%02X%02X%02X%02X", c->a, c->r, c->g, c->b);}
static bool color_equal(const m_option_t *opt, void *a, void *b){ struct m_color *ca = a; struct m_color *cb = b; return ca->a == cb->a && ca->r == cb->r && ca->g == cb->g && ca->b == cb->b;}
const m_option_type_t m_option_type_color = { .name = "Color", .size = sizeof(struct m_color), .parse = parse_color, .print = print_color, .copy = copy_opt, .equal = color_equal,};
// Parse a >=0 number starting at s. Set s to the string following the number.// If the number ends with '%', eat that and set *out_per to true, but only// if the number is between 0-100; if not, don't eat anything, even the number.static bool eat_num_per(bstr *s, int *out_num, bool *out_per){ bstr rest; long long v = bstrtoll(*s, &rest, 10); if (s->len == rest.len || v < INT_MIN || v > INT_MAX) return false; *out_num = v; *out_per = false; *s = rest; if (bstr_eatstart0(&rest, "%") && v >= 0 && v <= 100) { *out_per = true; *s = rest; } return true;}
static bool parse_geometry_str(struct m_geometry *gm, bstr s){ *gm = (struct m_geometry) { .x = INT_MIN, .y = INT_MIN }; if (s.len == 0) return true; // Approximate grammar: // [[W][xH]][{+-}X{+-}Y][/WS] | [X:Y] // (meaning: [optional] {one character of} one|alternative) // Every number can be followed by '%' int num; bool per;
#define READ_NUM(F, F_PER) do { \ if (!eat_num_per(&s, &num, &per)) \ goto error; \ gm->F = num; \ gm->F_PER = per; \} while(0)
#define READ_SIGN(F) do { \ if (bstr_eatstart0(&s, "+")) { \ gm->F = false; \ } else if (bstr_eatstart0(&s, "-")) {\ gm->F = true; \ } else goto error; \} while(0)
if (bstrchr(s, ':') < 0) { gm->wh_valid = true; if (!bstr_startswith0(s, "+") && !bstr_startswith0(s, "-")) { if (!bstr_startswith0(s, "x")) READ_NUM(w, w_per); if (bstr_eatstart0(&s, "x")) READ_NUM(h, h_per); } if (s.len > 0) { gm->xy_valid = true; READ_SIGN(x_sign); READ_NUM(x, x_per); READ_SIGN(y_sign); READ_NUM(y, y_per); } if (bstr_eatstart0(&s, "/")) { bstr rest; long long v = bstrtoll(s, &rest, 10); if (s.len == rest.len || v < 1 || v > INT_MAX) goto error; s = rest; gm->ws = v; } } else { gm->xy_valid = true; READ_NUM(x, x_per); if (!bstr_eatstart0(&s, ":")) goto error; READ_NUM(y, y_per); }
return s.len == 0;
error: return false;}
#undef READ_NUM#undef READ_SIGN
#define APPEND_PER(F, F_PER) \ res = talloc_asprintf_append(res, "%d%s", gm->F, gm->F_PER ? "%" : "")
static char *print_geometry(const m_option_t *opt, const void *val){ const struct m_geometry *gm = val; char *res = talloc_strdup(NULL, ""); if (gm->wh_valid || gm->xy_valid) { if (gm->wh_valid) { APPEND_PER(w, w_per); res = talloc_asprintf_append(res, "x"); APPEND_PER(h, h_per); } if (gm->xy_valid) { res = talloc_asprintf_append(res, gm->x_sign ? "-" : "+"); APPEND_PER(x, x_per); res = talloc_asprintf_append(res, gm->y_sign ? "-" : "+"); APPEND_PER(y, y_per); } if (gm->ws > 0) res = talloc_asprintf_append(res, "/%d", gm->ws); } return res;}
#undef APPEND_PER
// xpos,ypos: position of the left upper corner// widw,widh: width and height of the window// scrw,scrh: width and height of the current screen// The input parameters should be set to a centered window (default fallbacks).void m_geometry_apply(int *xpos, int *ypos, int *widw, int *widh, int scrw, int scrh, bool center, struct m_geometry *gm){ if (gm->wh_valid) { int prew = *widw, preh = *widh; if (gm->w > 0) *widw = gm->w_per ? scrw * (gm->w / 100.0) : gm->w; if (gm->h > 0) *widh = gm->h_per ? scrh * (gm->h / 100.0) : gm->h; // keep aspect if the other value is not set double asp = (double)prew / preh; if (gm->w > 0 && !(gm->h > 0)) { *widh = *widw / asp; } else if (!(gm->w > 0) && gm->h > 0) { *widw = *widh * asp; } if (center) { *xpos += prew / 2 - *widw / 2; *ypos += preh / 2 - *widh / 2; } }
if (gm->xy_valid) { if (gm->x != INT_MIN) { *xpos = gm->x; if (gm->x_per) *xpos = (scrw - *widw) * (*xpos / 100.0); if (gm->x_sign) *xpos = scrw - *widw - *xpos; } if (gm->y != INT_MIN) { *ypos = gm->y; if (gm->y_per) *ypos = (scrh - *widh) * (*ypos / 100.0); if (gm->y_sign) *ypos = scrh - *widh - *ypos; } }}
static int parse_geometry(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ bool is_help = bstr_equals0(param, "help"); if (is_help) goto exit;
struct m_geometry gm; if (!parse_geometry_str(&gm, param)) goto exit;
if (dst) *((struct m_geometry *)dst) = gm;
return 1;
exit: if (!is_help) { mp_err(log, "Option %.*s: invalid geometry: '%.*s'\n", BSTR_P(name), BSTR_P(param)); } mp_info(log, "Valid format: [W[%%][xH[%%]]][{+-}X[%%]{+-}Y[%%]] | [X[%%]:Y[%%]]\n"); return is_help ? M_OPT_EXIT : M_OPT_INVALID;}
static bool geometry_equal(const m_option_t *opt, void *a, void *b){ struct m_geometry *ga = a; struct m_geometry *gb = b; return ga->x == gb->x && ga->y == gb->y && ga->w == gb->w && ga->h == gb->h && ga->xy_valid == gb->xy_valid && ga->wh_valid == gb->wh_valid && ga->w_per == gb->w_per && ga->h_per == gb->h_per && ga->x_per == gb->x_per && ga->y_per == gb->y_per && ga->x_sign == gb->x_sign && ga->y_sign == gb->y_sign && ga->ws == gb->ws;}
const m_option_type_t m_option_type_geometry = { .name = "Window geometry", .size = sizeof(struct m_geometry), .parse = parse_geometry, .print = print_geometry, .copy = copy_opt, .equal = geometry_equal,};
static int parse_size_box(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ bool is_help = bstr_equals0(param, "help"); if (is_help) goto exit;
struct m_geometry gm; if (!parse_geometry_str(&gm, param)) goto exit;
if (gm.xy_valid) goto exit;
if (dst) *((struct m_geometry *)dst) = gm;
return 1;
exit: if (!is_help) { mp_err(log, "Option %.*s: invalid size: '%.*s'\n", BSTR_P(name), BSTR_P(param)); } mp_info(log, "Valid format: W[%%][xH[%%]] or empty string\n"); return is_help ? M_OPT_EXIT : M_OPT_INVALID;}
const m_option_type_t m_option_type_size_box = { .name = "Window size", .size = sizeof(struct m_geometry), .parse = parse_size_box, .print = print_geometry, .copy = copy_opt, .equal = geometry_equal,};
void m_rect_apply(struct mp_rect *rc, int w, int h, struct m_geometry *gm){ *rc = (struct mp_rect){0, 0, w, h}; if (!w || !h) return; m_geometry_apply(&rc->x0, &rc->y0, &rc->x1, &rc->y1, w, h, true, gm); if (!gm->xy_valid && gm->wh_valid && rc->x1 == 0 && rc->y1 == 0) return; if (!gm->wh_valid || rc->x1 == 0 || rc->x1 == INT_MIN) rc->x1 = w - rc->x0; if (!gm->wh_valid || rc->y1 == 0 || rc->y1 == INT_MIN) rc->y1 = h - rc->y0; if (gm->wh_valid && (gm->w || gm->h)) rc->x1 += rc->x0; if (gm->wh_valid && (gm->w || gm->h)) rc->y1 += rc->y0;}
static int parse_rect(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ bool is_help = bstr_equals0(param, "help"); if (is_help) goto exit;
struct m_geometry gm; if (!parse_geometry_str(&gm, param)) goto exit;
bool invalid = gm.x_sign || gm.y_sign || gm.ws; invalid |= gm.wh_valid && (gm.w < 0 || gm.h < 0); invalid |= gm.wh_valid && !gm.xy_valid && gm.w <= 0 && gm.h <= 0;
if (invalid) goto exit;
if (dst) *((struct m_geometry *)dst) = gm;
return 1;
exit: if (!is_help) { mp_err(log, "Option %.*s: invalid rect: '%.*s'\n", BSTR_P(name), BSTR_P(param)); } mp_info(log, "Valid format: W[%%][xH[%%]][+x+y]\n"); return is_help ? M_OPT_EXIT : M_OPT_INVALID;}
const m_option_type_t m_option_type_rect = { .name = "Video rect", .size = sizeof(struct m_geometry), .parse = parse_rect, .print = print_geometry, .copy = copy_opt, .equal = geometry_equal,};
#include "video/img_format.h"
static int parse_imgfmt(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
if (!bstrcmp0(param, "help")) { mp_info(log, "Available formats:"); char **list = mp_imgfmt_name_list(); for (int i = 0; list[i]; i++) mp_info(log, " %s", list[i]); mp_info(log, " no"); mp_info(log, "\n"); talloc_free(list); return M_OPT_EXIT; }
unsigned int fmt = mp_imgfmt_from_name(param); if (!fmt && !bstr_equals0(param, "no")) { mp_err(log, "Option %.*s: unknown format name: '%.*s'\n", BSTR_P(name), BSTR_P(param)); return M_OPT_INVALID; }
if (dst) *((int *)dst) = fmt;
return 1;}
static char *print_imgfmt(const m_option_t *opt, const void *val){ int fmt = *(int *)val; return talloc_strdup(NULL, fmt ? mp_imgfmt_to_name(fmt) : "no");}
const m_option_type_t m_option_type_imgfmt = { .name = "Image format", .size = sizeof(int), .parse = parse_imgfmt, .print = print_imgfmt, .copy = copy_opt, .equal = int_equal,};
static int parse_fourcc(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
unsigned int value;
if (param.len == 4) { uint8_t *s = param.start; value = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24); } else { bstr rest; value = bstrtoll(param, &rest, 16); if (rest.len != 0) { mp_err(log, "Option %.*s: invalid FourCC: '%.*s'\n", BSTR_P(name), BSTR_P(param)); return M_OPT_INVALID; } }
if (dst) *((unsigned int *)dst) = value;
return 1;}
static char *print_fourcc(const m_option_t *opt, const void *val){ unsigned int fourcc = *(unsigned int *)val; return talloc_asprintf(NULL, "%08x", fourcc);}
const m_option_type_t m_option_type_fourcc = { .name = "FourCC", .size = sizeof(unsigned int), .parse = parse_fourcc, .print = print_fourcc, .copy = copy_opt, .equal = int_equal,};
#include "audio/format.h"
static int parse_afmt(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
if (!bstrcmp0(param, "help")) { mp_info(log, "Available formats:"); for (int i = 1; i < AF_FORMAT_COUNT; i++) mp_info(log, " %s", af_fmt_to_str(i)); mp_info(log, "\n"); return M_OPT_EXIT; }
int fmt = 0; for (int i = 1; i < AF_FORMAT_COUNT; i++) { if (bstr_equals0(param, af_fmt_to_str(i))) fmt = i; } if (!fmt) { mp_err(log, "Option %.*s: unknown format name: '%.*s'\n", BSTR_P(name), BSTR_P(param)); return M_OPT_INVALID; }
if (dst) *((int *)dst) = fmt;
return 1;}
static char *print_afmt(const m_option_t *opt, const void *val){ int fmt = *(int *)val; return talloc_strdup(NULL, fmt ? af_fmt_to_str(fmt) : "no");}
const m_option_type_t m_option_type_afmt = { .name = "Audio format", .size = sizeof(int), .parse = parse_afmt, .print = print_afmt, .copy = copy_opt, .equal = int_equal,};
#include "audio/chmap.h"
static int parse_channels(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ bool limited = opt->flags & M_OPT_CHANNELS_LIMITED;
struct m_channels res = {0};
if (bstr_equals0(param, "help")) { mp_chmap_print_help(log); if (!limited) { mp_info(log, "\nOther values:\n" " auto-safe\n"); } return M_OPT_EXIT; }
bool auto_safe = bstr_equals0(param, "auto-safe"); if (bstr_equals0(param, "auto") || bstr_equals0(param, "empty") || auto_safe) { if (limited) { mp_err(log, "Disallowed parameter.\n"); return M_OPT_INVALID; } param.len = 0; res.set = true; res.auto_safe = auto_safe; }
while (param.len) { bstr item; if (limited) { item = param; param.len = 0; } else { bstr_split_tok(param, ",", &item, ¶m); }
struct mp_chmap map = {0}; if (!mp_chmap_from_str(&map, item) || !mp_chmap_is_valid(&map)) { mp_err(log, "Invalid channel layout: %.*s\n", BSTR_P(item)); talloc_free(res.chmaps); return M_OPT_INVALID; }
MP_TARRAY_APPEND(NULL, res.chmaps, res.num_chmaps, map); res.set = true; }
if (dst) { opt->type->free(dst); *(struct m_channels *)dst = res; } else { talloc_free(res.chmaps); }
return 1;}
static char *print_channels(const m_option_t *opt, const void *val){ const struct m_channels *ch = val; if (!ch->set) return talloc_strdup(NULL, ""); if (ch->auto_safe) return talloc_strdup(NULL, "auto-safe"); if (ch->num_chmaps > 0) { char *res = talloc_strdup(NULL, ""); for (int n = 0; n < ch->num_chmaps; n++) { if (n > 0) res = talloc_strdup_append(res, ","); res = talloc_strdup_append(res, mp_chmap_to_str(&ch->chmaps[n])); } return res; } return talloc_strdup(NULL, "auto");}
static void free_channels(void *src){ if (!src) return;
struct m_channels *ch = src; talloc_free(ch->chmaps); *ch = (struct m_channels){0};}
static void copy_channels(const m_option_t *opt, void *dst, const void *src){ if (!(dst && src)) return;
struct m_channels *ch = dst; free_channels(dst); *ch = *(struct m_channels *)src; ch->chmaps = talloc_memdup(NULL, ch->chmaps, sizeof(ch->chmaps[0]) * ch->num_chmaps);}
static bool channels_equal(const m_option_t *opt, void *a, void *b){ struct m_channels *ca = a; struct m_channels *cb = b;
if (ca->set != cb->set || ca->auto_safe != cb->auto_safe || ca->num_chmaps != cb->num_chmaps) return false;
for (int n = 0; n < ca->num_chmaps; n++) { if (!mp_chmap_equals(&ca->chmaps[n], &cb->chmaps[n])) return false; }
return true;}
const m_option_type_t m_option_type_channels = { .name = "Audio channels or channel map", .size = sizeof(struct m_channels), .parse = parse_channels, .print = print_channels, .copy = copy_channels, .free = free_channels, .equal = channels_equal,};
static int parse_timestring(struct bstr str, double *time, char endchar){ int len; unsigned h, m; double s; *time = 0; /* ensure initialization for error cases */ bool neg = bstr_eatstart0(&str, "-"); if (!neg) bstr_eatstart0(&str, "+"); bool sci = bstr_find0(str, "e-") >= 0 || bstr_find0(str, "e+") >= 0; /* non-scientific notation timestamps shouldn't contain anymore +/- after this point */ if (!sci && (bstrchr(str, '-') >= 0 || bstrchr(str, '+') >= 0)) return 0; if (bstr_sscanf(str, "%u:%u:%lf%n", &h, &m, &s, &len) >= 3) { if (m >= 60 || s >= 60) return 0; /* minutes or seconds are out of range */ *time = 3600.0 * h + 60 * m + s; } else if (bstr_sscanf(str, "%u:%lf%n", &m, &s, &len) >= 2) { if (s >= 60) return 0; /* seconds are out of range */ *time = 60.0 * m + s; } else if (bstr_sscanf(str, "%lf%n", &s, &len) >= 1) { *time = s; } else { return 0; /* unsupported time format */ } if (len < str.len && str.start[len] != endchar) return 0; /* invalid extra characters at the end */ if (!isfinite(*time)) return 0; if (neg) *time = -*time; return len;}
#define HAS_NOPTS(opt) ((opt)->flags & M_OPT_ALLOW_NO)
static int parse_time(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ if (param.len == 0) return M_OPT_MISSING_PARAM;
double time = MP_NOPTS_VALUE; if (HAS_NOPTS(opt) && bstr_equals0(param, "no")) { // nothing } else if (!parse_timestring(param, &time, 0)) { mp_err(log, "Option %.*s: invalid time: '%.*s'\n", BSTR_P(name), BSTR_P(param)); return M_OPT_INVALID; }
if (dst) *(double *)dst = time; return 1;}
static char *print_time(const m_option_t *opt, const void *val){ double pts = *(double *)val; if (pts == MP_NOPTS_VALUE && HAS_NOPTS(opt)) return talloc_strdup(NULL, "no"); // symmetry with parsing return talloc_asprintf(NULL, "%f", pts);}
static char *pretty_print_time(const m_option_t *opt, const void *val){ double pts = *(double *)val; if (pts == MP_NOPTS_VALUE && HAS_NOPTS(opt)) return talloc_strdup(NULL, "no"); // symmetry with parsing return mp_format_time(pts, false);}
static int time_set(const m_option_t *opt, void *dst, struct mpv_node *src){ if (HAS_NOPTS(opt) && src->format == MPV_FORMAT_STRING) { if (strcmp(src->u.string, "no") == 0) { *(double *)dst = MP_NOPTS_VALUE; return 1; } } return double_set(opt, dst, src);}
static int time_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ if (HAS_NOPTS(opt) && *(double *)src == MP_NOPTS_VALUE) { dst->format = MPV_FORMAT_STRING; dst->u.string = talloc_strdup(ta_parent, "no"); return 1; } return double_get(opt, ta_parent, dst, src);}
const m_option_type_t m_option_type_time = { .name = "Time", .size = sizeof(double), .parse = parse_time, .print = print_time, .pretty_print = pretty_print_time, .copy = copy_opt, .add = add_double, .set = time_set, .get = time_get, .equal = double_equal,};
// Relative time
static int parse_rel_time(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ struct m_rel_time t = {0};
if (param.len == 0) return M_OPT_MISSING_PARAM;
if (bstr_equals0(param, "none")) { t.type = REL_TIME_NONE; goto out; }
// Percent pos if (bstr_endswith0(param, "%")) { double percent = bstrtod(bstr_splice(param, 0, -1), ¶m); if (param.len == 0 && percent >= 0 && percent <= 100) { t.type = REL_TIME_PERCENT; t.pos = percent; goto out; } }
// Chapter pos if (bstr_startswith0(param, "#")) { int chapter = bstrtoll(bstr_cut(param, 1), ¶m, 10); if (param.len == 0 && chapter >= 1) { t.type = REL_TIME_CHAPTER; t.pos = chapter - 1; goto out; } }
double time; if (parse_timestring(param, &time, 0)) { if (bstr_startswith0(param, "+") || bstr_startswith0(param, "-")) { t.type = REL_TIME_RELATIVE; } else { t.type = REL_TIME_ABSOLUTE; } t.pos = time; goto out; }
mp_err(log, "Option %.*s: invalid time or position: '%.*s'\n", BSTR_P(name), BSTR_P(param)); return M_OPT_INVALID;
out: if (dst) *(struct m_rel_time *)dst = t; return 1;}
static char *print_rel_time(const m_option_t *opt, const void *val){ const struct m_rel_time *t = val; switch(t->type) { case REL_TIME_ABSOLUTE: return talloc_asprintf(NULL, "%g", t->pos); case REL_TIME_RELATIVE: return talloc_asprintf(NULL, "%+g", t->pos); case REL_TIME_CHAPTER: return talloc_asprintf(NULL, "#%g", t->pos); case REL_TIME_PERCENT: return talloc_asprintf(NULL, "%g%%", t->pos); } return talloc_strdup(NULL, "none");}
static bool rel_time_equal(const m_option_t *opt, void *a, void *b){ struct m_rel_time *ta = a; struct m_rel_time *tb = b; return ta->type == tb->type && ta->pos == tb->pos;}
const m_option_type_t m_option_type_rel_time = { .name = "Relative time or percent position", .size = sizeof(struct m_rel_time), .parse = parse_rel_time, .print = print_rel_time, .copy = copy_opt, .equal = rel_time_equal,};
//// Objects (i.e. filters, etc) settings
#undef VAL#define VAL(x) (*(m_obj_settings_t **)(x))
bool m_obj_list_find(struct m_obj_desc *dst, const struct m_obj_list *l, bstr name){ for (int i = 0; ; i++) { if (!l->get_desc(dst, i)) break; if (bstr_equals0(name, dst->name)) return true; } for (int i = 0; l->aliases[i][0]; i++) { const char *aname = l->aliases[i][0]; const char *alias = l->aliases[i][1]; if (bstr_equals0(name, aname) && m_obj_list_find(dst, l, bstr0(alias))) { dst->replaced_name = aname; return true; } } return false;}
static void obj_setting_free(m_obj_settings_t *item){ talloc_free(item->name); talloc_free(item->label); free_str_list(&(item->attribs));}
// If at least one item has a label, compare labels only - otherwise ignore them.static bool obj_setting_match(m_obj_settings_t *a, m_obj_settings_t *b){ bstr la = bstr0(a->label), lb = bstr0(b->label); if (la.len || lb.len) return bstr_equals(la, lb);
return m_obj_settings_equal(a, b);}
static int obj_settings_list_num_items(m_obj_settings_t *obj_list){ int num = 0; while (obj_list && obj_list[num].name) num++; return num;}
static void obj_settings_list_del_at(m_obj_settings_t **p_obj_list, int idx){ m_obj_settings_t *obj_list = *p_obj_list; int num = obj_settings_list_num_items(obj_list);
assert(idx >= 0 && idx < num);
obj_setting_free(&obj_list[idx]);
// Note: the NULL-terminating element is moved down as part of this memmove(&obj_list[idx], &obj_list[idx + 1], sizeof(m_obj_settings_t) * (num - idx));
*p_obj_list = talloc_realloc(NULL, obj_list, struct m_obj_settings, num);}
// Insert such that *p_obj_list[idx] is set to item.// If idx < 0, set idx = count + idx + 1 (i.e. -1 inserts it as last element).// Memory referenced by *item is not copied.static bool obj_settings_list_insert_at(struct mp_log *log, m_obj_settings_t **p_obj_list, int idx, m_obj_settings_t *item){ int num = obj_settings_list_num_items(*p_obj_list); // Limit list entries to 100. obj_settings_list is not designed to hold more // items, and it quickly starts taking ages to add all items. if (num > 100) { mp_warn(log, "Object settings list capacity exceeded: " "a maximum of 100 elements is allowed."); return false; } if (idx < 0) idx = num + idx + 1; assert(idx >= 0 && idx <= num); *p_obj_list = talloc_realloc(NULL, *p_obj_list, struct m_obj_settings, num + 2); memmove(*p_obj_list + idx + 1, *p_obj_list + idx, (num - idx) * sizeof(m_obj_settings_t)); (*p_obj_list)[idx] = *item; (*p_obj_list)[num + 1] = (m_obj_settings_t){0}; return true;}
static int obj_settings_list_find_by_label(m_obj_settings_t *obj_list, bstr label){ for (int n = 0; obj_list && obj_list[n].name; n++) { if (label.len && bstr_equals0(label, obj_list[n].label)) return n; } return -1;}
static int obj_settings_list_find_by_label0(m_obj_settings_t *obj_list, const char *label){ return obj_settings_list_find_by_label(obj_list, bstr0(label));}
static int obj_settings_find_by_content(m_obj_settings_t *obj_list, m_obj_settings_t *item){ for (int n = 0; obj_list && obj_list[n].name; n++) { if (obj_setting_match(&obj_list[n], item)) return n; } return -1;}
static void free_obj_settings_list(void *dst){ int n; m_obj_settings_t *d;
if (!dst || !VAL(dst)) return;
d = VAL(dst); for (n = 0; d[n].name; n++) obj_setting_free(&d[n]); talloc_free(d); VAL(dst) = NULL;}
static void copy_obj_settings_list(const m_option_t *opt, void *dst, const void *src){ m_obj_settings_t *d, *s; int n;
if (!(dst && src)) return;
s = VAL(src);
if (VAL(dst)) free_obj_settings_list(dst); if (!s) return;
for (n = 0; s[n].name; n++) /* NOP */; d = talloc_array(NULL, struct m_obj_settings, n + 1); for (n = 0; s[n].name; n++) { d[n].name = talloc_strdup(NULL, s[n].name); d[n].label = talloc_strdup(NULL, s[n].label); d[n].enabled = s[n].enabled; d[n].attribs = NULL; copy_str_list(NULL, &(d[n].attribs), &(s[n].attribs)); } d[n].name = NULL; d[n].label = NULL; d[n].attribs = NULL; VAL(dst) = d;}
// Consider -vf a=b=c:d=e. This verifies "b"="c" and "d"="e" and that the// option names/values are correct. Try to determine whether an option// without '=' sets a flag, or whether it's a positional argument.static int get_obj_param(struct mp_log *log, bstr opt_name, bstr obj_name, struct m_config *config, bstr name, bstr val, int flags, bool nopos, int *nold, bstr *out_name, bstr *out_val, char *tmp, size_t tmp_size){ int r;
if (!config) { // Duplicates the logic below, but with unknown parameter types/names. if (val.start || nopos) { *out_name = name; *out_val = val; } else { val = name; // positional fields if (val.len == 0) { // Empty field, count it and go on (*nold)++; return 0; } // Positional naming convention for/followed by mp_set_avopts(). snprintf(tmp, tmp_size, "@%d", *nold); *out_name = bstr0(tmp); *out_val = val; (*nold)++; } return 1; }
// val.start != NULL => of the form name=val (not positional) // If it's just "name", and the associated option exists and is a flag, // don't accept it as positional argument. if (val.start || m_config_option_requires_param(config, name) == 0 || nopos) { r = m_config_set_option_cli(config, name, val, flags); if (r < 0) { if (r == M_OPT_UNKNOWN) { mp_err(log, "Option %.*s: %.*s doesn't have a %.*s parameter.\n", BSTR_P(opt_name), BSTR_P(obj_name), BSTR_P(name)); return M_OPT_UNKNOWN; } if (r != M_OPT_EXIT) mp_err(log, "Option %.*s: " "Error while parsing %.*s parameter %.*s (%.*s)\n", BSTR_P(opt_name), BSTR_P(obj_name), BSTR_P(name), BSTR_P(val)); return r; } *out_name = name; *out_val = val; return 1; } else { val = name; // positional fields if (val.len == 0) { // Empty field, count it and go on (*nold)++; return 0; } const char *opt = m_config_get_positional_option(config, *nold); if (!opt) { mp_err(log, "Option %.*s: %.*s has only %d " "params, so you can't give more than %d unnamed params.\n", BSTR_P(opt_name), BSTR_P(obj_name), *nold, *nold); return M_OPT_OUT_OF_RANGE; } r = m_config_set_option_cli(config, bstr0(opt), val, flags); if (r < 0) { if (r != M_OPT_EXIT) mp_err(log, "Option %.*s: " "Error while parsing %.*s parameter %s (%.*s)\n", BSTR_P(opt_name), BSTR_P(obj_name), opt, BSTR_P(val)); return r; } *out_name = bstr0(opt); *out_val = val; (*nold)++; return 1; }}
// Consider -vf a=b:c:d. This parses "b:c:d" into name/value pairs, stored as// linear array in *_ret. In particular, config contains what options a the// object takes, and verifies the option values as well.// If config is NULL, all parameters are accepted without checking.// _ret set to NULL can be used for checking-only.// flags can contain any M_SETOPT_* flag.// desc is optional.static int m_obj_parse_sub_config(struct mp_log *log, struct bstr opt_name, struct bstr name, struct bstr *pstr, struct m_config *config, int flags, bool nopos, struct m_obj_desc *desc, const struct m_obj_list *list, char ***ret){ int nold = 0; char **args = NULL; int num_args = 0; int r = 1; char tmp[80];
if (ret) { args = *ret; while (args && args[num_args]) num_args++; }
while (pstr->len > 0) { bstr fname, fval; r = split_subconf(log, opt_name, pstr, &fname, &fval); if (r < 0) goto exit;
if (list->use_global_options) { mp_err(log, "Option %.*s: this option does not accept sub-options.\n", BSTR_P(opt_name)); mp_err(log, "Sub-options for --vo and --ao were removed from mpv in " "release 0.23.0.\nSee https://0x0.st/uM for details.\n"); r = M_OPT_INVALID; goto exit; }
if (bstr_equals0(fname, "help")) goto print_help; r = get_obj_param(log, opt_name, name, config, fname, fval, flags, nopos, &nold, &fname, &fval, tmp, sizeof(tmp)); if (r < 0) goto exit;
if (r > 0 && ret) { MP_TARRAY_APPEND(NULL, args, num_args, bstrto0(NULL, fname)); MP_TARRAY_APPEND(NULL, args, num_args, bstrto0(NULL, fval)); MP_TARRAY_APPEND(NULL, args, num_args, NULL); MP_TARRAY_APPEND(NULL, args, num_args, NULL); num_args -= 2; }
if (!bstr_eatstart0(pstr, ":")) break; }
if (ret) { if (num_args > 0) { *ret = args; args = NULL; } else { *ret = NULL; } }
goto exit;
print_help: ; if (config) { if (desc->print_help) desc->print_help(log); m_config_print_option_list(config, "*"); } else if (list->print_unknown_entry_help) { list->print_unknown_entry_help(log, mp_tprintf(80, "%.*s", BSTR_P(name))); } else { mp_warn(log, "Option %.*s: item '%.*s' isn't supported.\n", BSTR_P(opt_name), BSTR_P(name)); } r = M_OPT_EXIT;
exit: free_str_list(&args); return r;}
// Characters which may appear in a filter name#define NAMECH "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
// Parse one item, e.g. -vf a=b:c:d,e=f:g => parse a=b:c:d into "a" and "b:c:d"static int parse_obj_settings(struct mp_log *log, struct bstr opt, int op, struct bstr *pstr, const struct m_obj_list *list, m_obj_settings_t **_ret){ int r; char **plist = NULL; struct m_obj_desc desc; bstr str = {0}; bstr label = {0}; bool nopos = list->disallow_positional_parameters; bool enabled = true;
if (bstr_eatstart0(pstr, "@")) { bstr rest; if (!bstr_split_tok(*pstr, ":", &label, &rest)) { // "@labelname" is the special enable/disable toggle syntax if (op == OP_TOGGLE) { int idx = bstrspn(*pstr, NAMECH); label = bstr_splice(*pstr, 0, idx); if (label.len) { *pstr = bstr_cut(*pstr, idx); goto done; } } mp_err(log, "Option %.*s: ':' expected after label.\n", BSTR_P(opt)); return M_OPT_INVALID; } *pstr = rest; if (label.len == 0) { mp_err(log, "Option %.*s: label name expected.\n", BSTR_P(opt)); return M_OPT_INVALID; } }
if (list->allow_disable_entries && bstr_eatstart0(pstr, "!")) enabled = false;
bool has_param = false; int idx = bstrspn(*pstr, NAMECH); str = bstr_splice(*pstr, 0, idx); if (!str.len) { mp_err(log, "Option %.*s: item name expected.\n", BSTR_P(opt)); return M_OPT_INVALID; } *pstr = bstr_cut(*pstr, idx); // video filters use "=", VOs use ":" if (bstr_eatstart0(pstr, "=") || bstr_eatstart0(pstr, ":")) has_param = true;
bool skip = false; if (m_obj_list_find(&desc, list, str)) { if (desc.replaced_name) mp_warn(log, "Driver '%s' has been replaced with '%s'!\n", desc.replaced_name, desc.name); } else { char name[80]; snprintf(name, sizeof(name), "%.*s", BSTR_P(str)); if (list->check_unknown_entry && !list->check_unknown_entry(name)) { mp_err(log, "Option %.*s: '%.*s' isn't supported.\n", BSTR_P(opt), BSTR_P(str)); return M_OPT_INVALID; } desc = (struct m_obj_desc){0}; skip = true; }
if (has_param) { struct m_config *config = NULL; if (!skip) config = m_config_from_obj_desc_noalloc(NULL, log, &desc); r = m_obj_parse_sub_config(log, opt, str, pstr, config, M_SETOPT_CHECK_ONLY, nopos, &desc, list, _ret ? &plist : NULL); talloc_free(config); if (r < 0) return r; } if (!_ret) return 1;
done: ; m_obj_settings_t item = { .name = bstrto0(NULL, str), .label = bstrdup0(NULL, label), .enabled = enabled, .attribs = plist, }; if (!obj_settings_list_insert_at(log, _ret, -1, &item)) obj_setting_free(&item); return 1;}
// Parse a single entry for -vf-remove (return 0 if not applicable)// mark_del is bounded by the number of items in dststatic int parse_obj_settings_del(struct mp_log *log, struct bstr opt_name, struct bstr *param, void *dst, bool *mark_del){ bstr s = *param; if (bstr_eatstart0(&s, "@")) { // '@name:' -> parse as normal filter entry // '@name,' or '@name<end>' -> parse here int idx = bstrspn(s, NAMECH); bstr label = bstr_splice(s, 0, idx); s = bstr_cut(s, idx); if (bstr_startswith0(s, ":")) return 0; if (dst) { int label_index = 0; label_index = obj_settings_list_find_by_label(VAL(dst), label); if (label_index >= 0) { mark_del[label_index] = true; } else { mp_warn(log, "Option %.*s: item label @%.*s not found.\n", BSTR_P(opt_name), BSTR_P(label)); } } *param = s; return 1; } return 0;}
static int parse_obj_settings_list(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ m_obj_settings_t *res = NULL; int op = OP_NONE; bool *mark_del = NULL; int num_items = obj_settings_list_num_items(dst ? VAL(dst) : 0); const struct m_obj_list *ol = opt->priv; int ret = 1;
assert(opt->priv);
if (bstr_endswith0(name, "-add")) { op = OP_ADD; } else if (bstr_endswith0(name, "-append")) { op = OP_APPEND; } else if (bstr_endswith0(name, "-set")) { op = OP_NONE; } else if (bstr_endswith0(name, "-pre")) { op = OP_PRE; } else if (bstr_endswith0(name, "-remove")) { op = OP_REMOVE; } else if (bstr_endswith0(name, "-clr")) { op = OP_CLR; } else if (bstr_endswith0(name, "-toggle")) { op = OP_TOGGLE; } else if (bstr_endswith0(name, "-help")) { mp_err(log, "Option %s:\n" "Supported operations are:\n" " %s-set\n" " Overwrite the old list with the given list\n\n" " %s-append\n" " Append the given item to the current list\n\n" " %s-add\n" " Append the given list to the current list\n\n" " %s-pre\n" " Prepend the given list to the current list\n\n" " %s-remove\n" " Remove the given item from the current list\n\n" " %s-toggle\n" " Add the item to the list, or remove it if it's already added.\n\n" " %s-clr\n" " Clear the current list.\n\n", opt->name, opt->name, opt->name, opt->name, opt->name, opt->name, opt->name, opt->name);
ret = M_OPT_EXIT; goto done; }
if (!bstrcmp0(param, "help")) { mp_info(log, "Available %s:\n", ol->description); for (int n = 0; ; n++) { struct m_obj_desc desc; if (!ol->get_desc(&desc, n)) break; if (!desc.hidden) { mp_info(log, " %-16s %s\n", desc.name, desc.description); } } mp_info(log, "\n"); if (ol->print_help_list) ol->print_help_list(log); if (!ol->use_global_options) { mp_info(log, "Get help on individual entries via: --%s=entry=help\n", opt->name); } ret = M_OPT_EXIT; goto done; }
if (op == OP_CLR) { if (param.len) { mp_err(log, "Option %.*s: -clr does not take an argument.\n", BSTR_P(name)); ret = M_OPT_INVALID; goto done; } if (dst) free_obj_settings_list(dst); ret = 0; goto done; } else if (op == OP_REMOVE) { mark_del = talloc_zero_array(NULL, bool, num_items + 1); }
if (op != OP_NONE && param.len == 0) { ret = M_OPT_MISSING_PARAM; goto done; }
while (param.len > 0) { int r = 0; if (op == OP_REMOVE) r = parse_obj_settings_del(log, name, ¶m, dst, mark_del); if (r == 0) { r = parse_obj_settings(log, name, op, ¶m, ol, dst ? &res : NULL); } if (r < 0) { free_obj_settings_list(&res); ret = r; goto done; } if (param.len > 0) { const char sep[2] = {OPTION_LIST_SEPARATOR, 0}; if (!bstr_eatstart0(¶m, sep)) { free_obj_settings_list(&res); ret = M_OPT_INVALID; goto done; } if (param.len == 0) { if (!ol->allow_trailer) { free_obj_settings_list(&res); ret = M_OPT_INVALID; goto done; } if (dst) { m_obj_settings_t item = { .name = talloc_strdup(NULL, ""), }; if (!obj_settings_list_insert_at(log, &res, -1, &item)) obj_setting_free(&item); } } } }
if (op != OP_NONE && res && res[0].name && res[1].name) { if (op == OP_APPEND) { mp_err(log, "Option %.*s: -append takes only 1 item (no ',').\n", BSTR_P(name)); free_obj_settings_list(&res); ret = M_OPT_INVALID; goto done; } }
if (dst) { m_obj_settings_t *list = VAL(dst); if (op == OP_PRE) { int prepend_counter = 0; for (int n = 0; res && res[n].name; n++) { int label = obj_settings_list_find_by_label0(list, res[n].label); if (label < 0) { if (!obj_settings_list_insert_at(log, &list, prepend_counter, &res[n])) obj_setting_free(&res[n]); prepend_counter++; } else { // Prefer replacement semantics, instead of actually // prepending. obj_setting_free(&list[label]); list[label] = res[n]; } } talloc_free(res); } else if (op == OP_ADD || op == OP_APPEND) { for (int n = 0; res && res[n].name; n++) { int label = obj_settings_list_find_by_label0(list, res[n].label); if (label < 0) { if (!obj_settings_list_insert_at(log, &list, -1, &res[n])) obj_setting_free(&res[n]); } else { // Prefer replacement semantics, instead of actually // appending. obj_setting_free(&list[label]); list[label] = res[n]; } } talloc_free(res); } else if (op == OP_TOGGLE) { for (int n = 0; res && res[n].name; n++) { if (res[n].label && !res[n].name[0]) { // Toggle enable/disable special case. int found = obj_settings_list_find_by_label0(list, res[n].label); if (found < 0) { mp_warn(log, "Option %.*s: Label %s not found\n", BSTR_P(name), res[n].label); } else { list[found].enabled = !list[found].enabled; } obj_setting_free(&res[n]); } else { int found = obj_settings_find_by_content(list, &res[n]); if (found < 0) { if (!obj_settings_list_insert_at(log, &list, -1, &res[n])) obj_setting_free(&res[n]); } else { obj_settings_list_del_at(&list, found); obj_setting_free(&res[n]); } } } talloc_free(res); } else if (op == OP_REMOVE) { for (int n = num_items - 1; n >= 0; n--) { if (mark_del[n]) obj_settings_list_del_at(&list, n); } for (int n = 0; res && res[n].name; n++) { int found = obj_settings_find_by_content(list, &res[n]); if (found >= 0) obj_settings_list_del_at(&list, found); } free_obj_settings_list(&res); } else { assert(op == OP_NONE); free_obj_settings_list(&list); list = res; } VAL(dst) = list; }
done: talloc_free(mark_del); return ret;}
static void append_param(char **res, char *param){ if (strspn(param, NAMECH) == strlen(param)) { *res = talloc_strdup_append(*res, param); } else { // Simple escaping: %BYTECOUNT%STRING *res = talloc_asprintf_append(*res, "%%%zd%%%s", strlen(param), param); }}
static char *print_obj_settings_list(const m_option_t *opt, const void *val){ m_obj_settings_t *list = VAL(val); char *res = talloc_strdup(NULL, ""); for (int n = 0; list && list[n].name; n++) { m_obj_settings_t *entry = &list[n]; if (n > 0) res = talloc_strdup_append(res, ","); // Assume labels and names don't need escaping if (entry->label && entry->label[0]) res = talloc_asprintf_append(res, "@%s:", entry->label); if (!entry->enabled) res = talloc_strdup_append(res, "!"); res = talloc_strdup_append(res, entry->name); if (entry->attribs && entry->attribs[0]) { res = talloc_strdup_append(res, "="); for (int i = 0; entry->attribs[i * 2 + 0]; i++) { if (i > 0) res = talloc_strdup_append(res, ":"); append_param(&res, entry->attribs[i * 2 + 0]); res = talloc_strdup_append(res, "="); append_param(&res, entry->attribs[i * 2 + 1]); } } } return res;}
static int set_obj_settings_list(const m_option_t *opt, void *dst, struct mpv_node *src){ if (src->format != MPV_FORMAT_NODE_ARRAY) return M_OPT_INVALID; m_obj_settings_t *entries = talloc_zero_array(NULL, m_obj_settings_t, src->u.list->num + 1); for (int n = 0; n < src->u.list->num; n++) { m_obj_settings_t *entry = &entries[n]; entry->enabled = true; if (src->u.list->values[n].format != MPV_FORMAT_NODE_MAP) goto error; struct mpv_node_list *src_entry = src->u.list->values[n].u.list; for (int i = 0; i < src_entry->num; i++) { const char *key = src_entry->keys[i]; struct mpv_node *val = &src_entry->values[i]; if (strcmp(key, "name") == 0) { if (val->format != MPV_FORMAT_STRING) goto error; entry->name = talloc_strdup(NULL, val->u.string); } else if (strcmp(key, "label") == 0) { if (val->format != MPV_FORMAT_STRING) goto error; entry->label = talloc_strdup(NULL, val->u.string); } else if (strcmp(key, "enabled") == 0) { if (val->format != MPV_FORMAT_FLAG) goto error; entry->enabled = val->u.flag; } else if (strcmp(key, "params") == 0) { if (val->format != MPV_FORMAT_NODE_MAP) goto error; struct mpv_node_list *src_params = val->u.list; entry->attribs = talloc_zero_array(NULL, char*, (src_params->num + 1) * 2); for (int x = 0; x < src_params->num; x++) { if (src_params->values[x].format != MPV_FORMAT_STRING) goto error; entry->attribs[x * 2 + 0] = talloc_strdup(NULL, src_params->keys[x]); entry->attribs[x * 2 + 1] = talloc_strdup(NULL, src_params->values[x].u.string); } } } } free_obj_settings_list(dst); VAL(dst) = entries; return 0;error: free_obj_settings_list(&entries); return M_OPT_INVALID;}
static struct mpv_node *add_array_entry(struct mpv_node *dst){ struct mpv_node_list *list = dst->u.list; assert(dst->format == MPV_FORMAT_NODE_ARRAY&& dst->u.list); MP_TARRAY_GROW(list, list->values, list->num); return &list->values[list->num++];}
static struct mpv_node *add_map_entry(struct mpv_node *dst, const char *key){ struct mpv_node_list *list = dst->u.list; assert(dst->format == MPV_FORMAT_NODE_MAP && dst->u.list); MP_TARRAY_GROW(list, list->values, list->num); MP_TARRAY_GROW(list, list->keys, list->num); list->keys[list->num] = talloc_strdup(list, key); return &list->values[list->num++];}
static void add_map_string(struct mpv_node *dst, const char *key, const char *val){ struct mpv_node *entry = add_map_entry(dst, key); entry->format = MPV_FORMAT_STRING; entry->u.string = talloc_strdup(dst->u.list, val);}
static int get_obj_settings_list(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *val){ m_obj_settings_t *list = VAL(val); dst->format = MPV_FORMAT_NODE_ARRAY; dst->u.list = talloc_zero(ta_parent, struct mpv_node_list); ta_parent = dst->u.list; for (int n = 0; list && list[n].name; n++) { m_obj_settings_t *entry = &list[n]; struct mpv_node *nentry = add_array_entry(dst); nentry->format = MPV_FORMAT_NODE_MAP; nentry->u.list = talloc_zero(ta_parent, struct mpv_node_list); add_map_string(nentry, "name", entry->name); if (entry->label && entry->label[0]) add_map_string(nentry, "label", entry->label); struct mpv_node *enabled = add_map_entry(nentry, "enabled"); enabled->format = MPV_FORMAT_FLAG; enabled->u.flag = entry->enabled; struct mpv_node *params = add_map_entry(nentry, "params"); params->format = MPV_FORMAT_NODE_MAP; params->u.list = talloc_zero(ta_parent, struct mpv_node_list); for (int i = 0; entry->attribs && entry->attribs[i * 2 + 0]; i++) { add_map_string(params, entry->attribs[i * 2 + 0], entry->attribs[i * 2 + 1]); } } return 1;}
static bool obj_settings_list_equal(const m_option_t *opt, void *pa, void *pb){ struct m_obj_settings *a = VAL(pa); struct m_obj_settings *b = VAL(pb);
if (a == b || !a || !b) return a == b || (!a && !b[0].name) || (!b && !a[0].name);
for (int n = 0; a[n].name || b[n].name; n++) { if (!a[n].name || !b[n].name) return false; if (!m_obj_settings_equal(&a[n], &b[n])) return false; }
return true;}
bool m_obj_settings_equal(struct m_obj_settings *a, struct m_obj_settings *b){ if (!str_equal(NULL, &a->name, &b->name)) return false;
if (!str_equal(NULL, &a->label, &b->label)) return false;
if (a->enabled != b->enabled) return false;
return str_list_equal(NULL, &a->attribs, &b->attribs);}
const m_option_type_t m_option_type_obj_settings_list = { .name = "Object settings list", .size = sizeof(m_obj_settings_t *), .parse = parse_obj_settings_list, .print = print_obj_settings_list, .copy = copy_obj_settings_list, .free = free_obj_settings_list, .set = set_obj_settings_list, .get = get_obj_settings_list, .equal = obj_settings_list_equal, .actions = (const struct m_option_action[]){ {"add"}, {"append"}, {"clr", M_OPT_TYPE_OPTIONAL_PARAM}, {"help", M_OPT_TYPE_OPTIONAL_PARAM}, {"pre"}, {"set"}, {"toggle"}, {"remove"}, {0} },};
#undef VAL#define VAL(x) (*(struct mpv_node *)(x))
static int parse_node(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst){ // Maybe use JSON? mp_err(log, "option type doesn't accept strings"); return M_OPT_INVALID;}
static char *print_node(const m_option_t *opt, const void *val){ char *t = talloc_strdup(NULL, ""); if (json_write(&t, &VAL(val)) < 0) { talloc_free(t); t = NULL; } return t;}
static char *pretty_print_node(const m_option_t *opt, const void *val){ char *t = talloc_strdup(NULL, ""); if (json_write_pretty(&t, &VAL(val)) < 0) { talloc_free(t); t = NULL; } return t;}
static void dup_node(void *ta_parent, struct mpv_node *node){ switch (node->format) { case MPV_FORMAT_STRING: node->u.string = talloc_strdup(ta_parent, node->u.string); break; case MPV_FORMAT_NODE_ARRAY: case MPV_FORMAT_NODE_MAP: { struct mpv_node_list *oldlist = node->u.list; struct mpv_node_list *new = talloc_zero(ta_parent, struct mpv_node_list); node->u.list = new; if (oldlist->num > 0) { *new = *oldlist; new->values = talloc_array(new, struct mpv_node, new->num); for (int n = 0; n < new->num; n++) { new->values[n] = oldlist->values[n]; dup_node(new, &new->values[n]); } if (node->format == MPV_FORMAT_NODE_MAP) { new->keys = talloc_array(new, char*, new->num); for (int n = 0; n < new->num; n++) new->keys[n] = talloc_strdup(new, oldlist->keys[n]); } } break; } case MPV_FORMAT_BYTE_ARRAY: { struct mpv_byte_array *old = node->u.ba; struct mpv_byte_array *new = talloc_zero(ta_parent, struct mpv_byte_array); node->u.ba = new; if (old->size > 0) { *new = *old; new->data = talloc_memdup(new, old->data, old->size); } break; } case MPV_FORMAT_NONE: case MPV_FORMAT_FLAG: case MPV_FORMAT_INT64: case MPV_FORMAT_DOUBLE: break; default: // unknown entry - mark as invalid node->format = (mpv_format)-1; }}
static void copy_node(const m_option_t *opt, void *dst, const void *src){ static_assert(sizeof(struct mpv_node) <= sizeof(union m_option_value), "");
if (!(dst && src)) return;
opt->type->free(dst); VAL(dst) = VAL(src); dup_node(NULL, &VAL(dst));}
void *node_get_alloc(struct mpv_node *node){ // Assume it was allocated with copy_node(), which allocates all // sub-nodes with the parent node as talloc parent. switch (node->format) { case MPV_FORMAT_STRING: return node->u.string; case MPV_FORMAT_NODE_ARRAY: case MPV_FORMAT_NODE_MAP: return node->u.list; default: return NULL; }}
static void free_node(void *src){ if (src) { struct mpv_node *node = &VAL(src); talloc_free(node_get_alloc(node)); *node = (struct mpv_node){{0}}; }}
// idempotent functions for conveniencestatic int node_set(const m_option_t *opt, void *dst, struct mpv_node *src){ copy_node(opt, dst, src); return 1;}
static int node_get(const m_option_t *opt, void *ta_parent, struct mpv_node *dst, void *src){ *dst = VAL(src); dup_node(ta_parent, dst); return 1;}
static bool node_equal(const m_option_t *opt, void *a, void *b){ return equal_mpv_node(&VAL(a), &VAL(b));}
const m_option_type_t m_option_type_node = { .name = "Complex", .size = sizeof(struct mpv_node), .parse = parse_node, .print = print_node, .pretty_print = pretty_print_node, .copy = copy_node, .free = free_node, .set = node_set, .get = node_get, .equal = node_equal,};
static int parse_cycle_dir(struct mp_log *log, const struct m_option *opt, struct bstr name, struct bstr param, void *dst){ double val; if (bstrcmp0(param, "up") == 0) { val = +1; } else if (bstrcmp0(param, "down") == 0) { val = -1; } else { return m_option_type_double.parse(log, opt, name, param, dst); } *(double *)dst = val; return 1;}
static char *print_cycle_dir(const m_option_t *opt, const void *val){ return talloc_asprintf(NULL, "%f", *(double *)val);}
const m_option_type_t m_option_type_cycle_dir = { .name = "up|down", .parse = parse_cycle_dir, .print = print_cycle_dir, .copy = copy_opt, .size = sizeof(double),};
// Special-cased by m_config.c.const m_option_type_t m_option_type_alias = { .name = "alias",};const m_option_type_t m_option_type_cli_alias = { .name = "alias",};const m_option_type_t m_option_type_removed = { .name = "removed",};const m_option_type_t m_option_type_subconfig = { .name = "Subconfig",};