ffmpeg_opt.c 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044
  1. /*
  2. * ffmpeg option parsing
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #include <stdint.h>
  22. #if HAVE_SYS_RESOURCE_H
  23. #include <sys/time.h>
  24. #include <sys/resource.h>
  25. #endif
  26. #include "ffmpeg.h"
  27. #include "fopen_utf8.h"
  28. #include "cmdutils.h"
  29. #include "opt_common.h"
  30. #include "libavformat/avformat.h"
  31. #include "libavcodec/avcodec.h"
  32. #include "libavcodec/bsf.h"
  33. #include "libavfilter/avfilter.h"
  34. #include "libavutil/avassert.h"
  35. #include "libavutil/avstring.h"
  36. #include "libavutil/avutil.h"
  37. #include "libavutil/bprint.h"
  38. #include "libavutil/channel_layout.h"
  39. #include "libavutil/getenv_utf8.h"
  40. #include "libavutil/intreadwrite.h"
  41. #include "libavutil/fifo.h"
  42. #include "libavutil/mathematics.h"
  43. #include "libavutil/opt.h"
  44. #include "libavutil/parseutils.h"
  45. #include "libavutil/pixdesc.h"
  46. #include "libavutil/pixfmt.h"
  47. #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
  48. #define SPECIFIER_OPT_FMT_str "%s"
  49. #define SPECIFIER_OPT_FMT_i "%i"
  50. #define SPECIFIER_OPT_FMT_i64 "%"PRId64
  51. #define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
  52. #define SPECIFIER_OPT_FMT_f "%f"
  53. #define SPECIFIER_OPT_FMT_dbl "%lf"
  54. static const char *const opt_name_codec_names[] = {"c", "codec", "acodec", "vcodec", "scodec", "dcodec", NULL};
  55. static const char *const opt_name_audio_channels[] = {"ac", NULL};
  56. static const char *const opt_name_audio_ch_layouts[] = {"channel_layout", "ch_layout", NULL};
  57. static const char *const opt_name_audio_sample_rate[] = {"ar", NULL};
  58. static const char *const opt_name_frame_rates[] = {"r", NULL};
  59. static const char *const opt_name_max_frame_rates[] = {"fpsmax", NULL};
  60. static const char *const opt_name_frame_sizes[] = {"s", NULL};
  61. static const char *const opt_name_frame_pix_fmts[] = {"pix_fmt", NULL};
  62. static const char *const opt_name_ts_scale[] = {"itsscale", NULL};
  63. static const char *const opt_name_hwaccels[] = {"hwaccel", NULL};
  64. static const char *const opt_name_hwaccel_devices[] = {"hwaccel_device", NULL};
  65. static const char *const opt_name_hwaccel_output_formats[] = {"hwaccel_output_format", NULL};
  66. static const char *const opt_name_autorotate[] = {"autorotate", NULL};
  67. static const char *const opt_name_autoscale[] = {"autoscale", NULL};
  68. static const char *const opt_name_max_frames[] = {"frames", "aframes", "vframes", "dframes", NULL};
  69. static const char *const opt_name_bitstream_filters[] = {"bsf", "absf", "vbsf", NULL};
  70. static const char *const opt_name_codec_tags[] = {"tag", "atag", "vtag", "stag", NULL};
  71. static const char *const opt_name_sample_fmts[] = {"sample_fmt", NULL};
  72. static const char *const opt_name_qscale[] = {"q", "qscale", NULL};
  73. static const char *const opt_name_forced_key_frames[] = {"forced_key_frames", NULL};
  74. static const char *const opt_name_fps_mode[] = {"fps_mode", NULL};
  75. static const char *const opt_name_force_fps[] = {"force_fps", NULL};
  76. static const char *const opt_name_frame_aspect_ratios[] = {"aspect", NULL};
  77. static const char *const opt_name_rc_overrides[] = {"rc_override", NULL};
  78. static const char *const opt_name_intra_matrices[] = {"intra_matrix", NULL};
  79. static const char *const opt_name_inter_matrices[] = {"inter_matrix", NULL};
  80. static const char *const opt_name_chroma_intra_matrices[] = {"chroma_intra_matrix", NULL};
  81. static const char *const opt_name_top_field_first[] = {"top", NULL};
  82. static const char *const opt_name_presets[] = {"pre", "apre", "vpre", "spre", NULL};
  83. static const char *const opt_name_copy_initial_nonkeyframes[] = {"copyinkf", NULL};
  84. static const char *const opt_name_copy_prior_start[] = {"copypriorss", NULL};
  85. static const char *const opt_name_filters[] = {"filter", "af", "vf", NULL};
  86. static const char *const opt_name_filter_scripts[] = {"filter_script", NULL};
  87. static const char *const opt_name_reinit_filters[] = {"reinit_filter", NULL};
  88. static const char *const opt_name_fix_sub_duration[] = {"fix_sub_duration", NULL};
  89. static const char *const opt_name_canvas_sizes[] = {"canvas_size", NULL};
  90. static const char *const opt_name_pass[] = {"pass", NULL};
  91. static const char *const opt_name_passlogfiles[] = {"passlogfile", NULL};
  92. static const char *const opt_name_max_muxing_queue_size[] = {"max_muxing_queue_size", NULL};
  93. static const char *const opt_name_muxing_queue_data_threshold[] = {"muxing_queue_data_threshold", NULL};
  94. static const char *const opt_name_guess_layout_max[] = {"guess_layout_max", NULL};
  95. static const char *const opt_name_apad[] = {"apad", NULL};
  96. static const char *const opt_name_discard[] = {"discard", NULL};
  97. static const char *const opt_name_disposition[] = {"disposition", NULL};
  98. static const char *const opt_name_time_bases[] = {"time_base", NULL};
  99. static const char *const opt_name_enc_time_bases[] = {"enc_time_base", NULL};
  100. static const char *const opt_name_bits_per_raw_sample[] = {"bits_per_raw_sample", NULL};
  101. #define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
  102. {\
  103. char namestr[128] = "";\
  104. const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
  105. for (i = 0; opt_name_##name[i]; i++)\
  106. av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[i], opt_name_##name[i+1] ? (opt_name_##name[i+2] ? ", " : " or ") : "");\
  107. av_log(NULL, AV_LOG_WARNING, "Multiple %s options specified for stream %d, only the last option '-%s%s%s "SPECIFIER_OPT_FMT_##type"' will be used.\n",\
  108. namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
  109. }
  110. #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
  111. {\
  112. int i, ret, matches = 0;\
  113. SpecifierOpt *so;\
  114. for (i = 0; i < o->nb_ ## name; i++) {\
  115. char *spec = o->name[i].specifier;\
  116. if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
  117. outvar = o->name[i].u.type;\
  118. so = &o->name[i];\
  119. matches++;\
  120. } else if (ret < 0)\
  121. exit_program(1);\
  122. }\
  123. if (matches > 1)\
  124. WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
  125. }
  126. #define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
  127. {\
  128. int i;\
  129. for (i = 0; i < o->nb_ ## name; i++) {\
  130. char *spec = o->name[i].specifier;\
  131. if (!strcmp(spec, mediatype))\
  132. outvar = o->name[i].u.type;\
  133. }\
  134. }
  135. HWDevice *filter_hw_device;
  136. char *vstats_filename;
  137. char *sdp_filename;
  138. float audio_drift_threshold = 0.1;
  139. float dts_delta_threshold = 10;
  140. float dts_error_threshold = 3600*30;
  141. int audio_volume = 256;
  142. int audio_sync_method = 0;
  143. enum VideoSyncMethod video_sync_method = VSYNC_AUTO;
  144. float frame_drop_threshold = 0;
  145. int do_benchmark = 0;
  146. int do_benchmark_all = 0;
  147. int do_hex_dump = 0;
  148. int do_pkt_dump = 0;
  149. int copy_ts = 0;
  150. int start_at_zero = 0;
  151. int copy_tb = -1;
  152. int debug_ts = 0;
  153. int exit_on_error = 0;
  154. int abort_on_flags = 0;
  155. int print_stats = -1;
  156. int qp_hist = 0;
  157. int stdin_interaction = 1;
  158. float max_error_rate = 2.0/3;
  159. char *filter_nbthreads;
  160. int filter_complex_nbthreads = 0;
  161. int vstats_version = 2;
  162. int auto_conversion_filters = 1;
  163. int64_t stats_period = 500000;
  164. static int file_overwrite = 0;
  165. static int no_file_overwrite = 0;
  166. static int do_psnr = 0;
  167. static int input_stream_potentially_available = 0;
  168. static int ignore_unknown_streams = 0;
  169. static int copy_unknown_streams = 0;
  170. static int recast_media = 0;
  171. static int find_stream_info = 1;
  172. static void uninit_options(OptionsContext *o)
  173. {
  174. const OptionDef *po = options;
  175. int i;
  176. /* all OPT_SPEC and OPT_STRING can be freed in generic way */
  177. while (po->name) {
  178. void *dst = (uint8_t*)o + po->u.off;
  179. if (po->flags & OPT_SPEC) {
  180. SpecifierOpt **so = dst;
  181. int i, *count = (int*)(so + 1);
  182. for (i = 0; i < *count; i++) {
  183. av_freep(&(*so)[i].specifier);
  184. if (po->flags & OPT_STRING)
  185. av_freep(&(*so)[i].u.str);
  186. }
  187. av_freep(so);
  188. *count = 0;
  189. } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
  190. av_freep(dst);
  191. po++;
  192. }
  193. for (i = 0; i < o->nb_stream_maps; i++)
  194. av_freep(&o->stream_maps[i].linklabel);
  195. av_freep(&o->stream_maps);
  196. av_freep(&o->audio_channel_maps);
  197. av_freep(&o->streamid_map);
  198. av_freep(&o->attachments);
  199. }
  200. static void init_options(OptionsContext *o)
  201. {
  202. memset(o, 0, sizeof(*o));
  203. o->stop_time = INT64_MAX;
  204. o->mux_max_delay = 0.7;
  205. o->start_time = AV_NOPTS_VALUE;
  206. o->start_time_eof = AV_NOPTS_VALUE;
  207. o->recording_time = INT64_MAX;
  208. o->limit_filesize = UINT64_MAX;
  209. o->chapters_input_file = INT_MAX;
  210. o->accurate_seek = 1;
  211. o->thread_queue_size = -1;
  212. o->input_sync_ref = -1;
  213. }
  214. static int show_hwaccels(void *optctx, const char *opt, const char *arg)
  215. {
  216. enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
  217. printf("Hardware acceleration methods:\n");
  218. while ((type = av_hwdevice_iterate_types(type)) !=
  219. AV_HWDEVICE_TYPE_NONE)
  220. printf("%s\n", av_hwdevice_get_type_name(type));
  221. printf("\n");
  222. return 0;
  223. }
  224. /* return a copy of the input with the stream specifiers removed from the keys */
  225. static AVDictionary *strip_specifiers(AVDictionary *dict)
  226. {
  227. const AVDictionaryEntry *e = NULL;
  228. AVDictionary *ret = NULL;
  229. while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) {
  230. char *p = strchr(e->key, ':');
  231. if (p)
  232. *p = 0;
  233. av_dict_set(&ret, e->key, e->value, 0);
  234. if (p)
  235. *p = ':';
  236. }
  237. return ret;
  238. }
  239. static int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global)
  240. {
  241. if (!av_strcasecmp(arg, "cfr")) *vsync_var = VSYNC_CFR;
  242. else if (!av_strcasecmp(arg, "vfr")) *vsync_var = VSYNC_VFR;
  243. else if (!av_strcasecmp(arg, "passthrough")) *vsync_var = VSYNC_PASSTHROUGH;
  244. else if (!av_strcasecmp(arg, "drop")) *vsync_var = VSYNC_DROP;
  245. else if (!is_global && !av_strcasecmp(arg, "auto")) *vsync_var = VSYNC_AUTO;
  246. else if (!is_global) {
  247. av_log(NULL, AV_LOG_FATAL, "Invalid value %s specified for fps_mode of #%d:%d.\n", arg, file_idx, st_idx);
  248. exit_program(1);
  249. }
  250. if (is_global && *vsync_var == VSYNC_AUTO) {
  251. video_sync_method = parse_number_or_die("vsync", arg, OPT_INT, VSYNC_AUTO, VSYNC_VFR);
  252. av_log(NULL, AV_LOG_WARNING, "Passing a number to -vsync is deprecated,"
  253. " use a string argument as described in the manual.\n");
  254. }
  255. return 0;
  256. }
  257. static int apply_sync_offsets(void)
  258. {
  259. for (int i = 0; i < nb_input_files; i++) {
  260. InputFile *ref, *self = input_files[i];
  261. int64_t adjustment;
  262. int64_t self_start_time, ref_start_time, self_seek_start, ref_seek_start;
  263. int start_times_set = 1;
  264. if (self->input_sync_ref == -1 || self->input_sync_ref == i) continue;
  265. if (self->input_sync_ref >= nb_input_files || self->input_sync_ref < -1) {
  266. av_log(NULL, AV_LOG_FATAL, "-isync for input %d references non-existent input %d.\n", i, self->input_sync_ref);
  267. exit_program(1);
  268. }
  269. if (copy_ts && !start_at_zero) {
  270. av_log(NULL, AV_LOG_FATAL, "Use of -isync requires that start_at_zero be set if copyts is set.\n");
  271. exit_program(1);
  272. }
  273. ref = input_files[self->input_sync_ref];
  274. if (ref->input_sync_ref != -1 && ref->input_sync_ref != self->input_sync_ref) {
  275. av_log(NULL, AV_LOG_ERROR, "-isync for input %d references a resynced input %d. Sync not set.\n", i, self->input_sync_ref);
  276. continue;
  277. }
  278. if (self->ctx->start_time_realtime != AV_NOPTS_VALUE && ref->ctx->start_time_realtime != AV_NOPTS_VALUE) {
  279. self_start_time = self->ctx->start_time_realtime;
  280. ref_start_time = ref->ctx->start_time_realtime;
  281. } else if (self->ctx->start_time != AV_NOPTS_VALUE && ref->ctx->start_time != AV_NOPTS_VALUE) {
  282. self_start_time = self->ctx->start_time;
  283. ref_start_time = ref->ctx->start_time;
  284. } else {
  285. start_times_set = 0;
  286. }
  287. if (start_times_set) {
  288. self_seek_start = self->start_time == AV_NOPTS_VALUE ? 0 : self->start_time;
  289. ref_seek_start = ref->start_time == AV_NOPTS_VALUE ? 0 : ref->start_time;
  290. adjustment = (self_start_time - ref_start_time) + !copy_ts*(self_seek_start - ref_seek_start) + ref->input_ts_offset;
  291. self->ts_offset += adjustment;
  292. av_log(NULL, AV_LOG_INFO, "Adjusted ts offset for Input #%d by %"PRId64" us to sync with Input #%d.\n", i, adjustment, self->input_sync_ref);
  293. } else {
  294. av_log(NULL, AV_LOG_INFO, "Unable to identify start times for Inputs #%d and %d both. No sync adjustment made.\n", i, self->input_sync_ref);
  295. }
  296. }
  297. return 0;
  298. }
  299. static int opt_filter_threads(void *optctx, const char *opt, const char *arg)
  300. {
  301. av_free(filter_nbthreads);
  302. filter_nbthreads = av_strdup(arg);
  303. return 0;
  304. }
  305. static int opt_abort_on(void *optctx, const char *opt, const char *arg)
  306. {
  307. static const AVOption opts[] = {
  308. { "abort_on" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
  309. { "empty_output" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = ABORT_ON_FLAG_EMPTY_OUTPUT }, .unit = "flags" },
  310. { "empty_output_stream", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM }, .unit = "flags" },
  311. { NULL },
  312. };
  313. static const AVClass class = {
  314. .class_name = "",
  315. .item_name = av_default_item_name,
  316. .option = opts,
  317. .version = LIBAVUTIL_VERSION_INT,
  318. };
  319. const AVClass *pclass = &class;
  320. return av_opt_eval_flags(&pclass, &opts[0], arg, &abort_on_flags);
  321. }
  322. static int opt_stats_period(void *optctx, const char *opt, const char *arg)
  323. {
  324. int64_t user_stats_period = parse_time_or_die(opt, arg, 1);
  325. if (user_stats_period <= 0) {
  326. av_log(NULL, AV_LOG_ERROR, "stats_period %s must be positive.\n", arg);
  327. return AVERROR(EINVAL);
  328. }
  329. stats_period = user_stats_period;
  330. av_log(NULL, AV_LOG_INFO, "ffmpeg stats and -progress period set to %s.\n", arg);
  331. return 0;
  332. }
  333. static int opt_audio_codec(void *optctx, const char *opt, const char *arg)
  334. {
  335. OptionsContext *o = optctx;
  336. return parse_option(o, "codec:a", arg, options);
  337. }
  338. static int opt_video_codec(void *optctx, const char *opt, const char *arg)
  339. {
  340. OptionsContext *o = optctx;
  341. return parse_option(o, "codec:v", arg, options);
  342. }
  343. static int opt_subtitle_codec(void *optctx, const char *opt, const char *arg)
  344. {
  345. OptionsContext *o = optctx;
  346. return parse_option(o, "codec:s", arg, options);
  347. }
  348. static int opt_data_codec(void *optctx, const char *opt, const char *arg)
  349. {
  350. OptionsContext *o = optctx;
  351. return parse_option(o, "codec:d", arg, options);
  352. }
  353. static int opt_map(void *optctx, const char *opt, const char *arg)
  354. {
  355. OptionsContext *o = optctx;
  356. StreamMap *m = NULL;
  357. int i, negative = 0, file_idx, disabled = 0;
  358. int sync_file_idx = -1, sync_stream_idx = 0;
  359. char *p, *sync;
  360. char *map;
  361. char *allow_unused;
  362. if (*arg == '-') {
  363. negative = 1;
  364. arg++;
  365. }
  366. map = av_strdup(arg);
  367. if (!map)
  368. return AVERROR(ENOMEM);
  369. /* parse sync stream first, just pick first matching stream */
  370. if (sync = strchr(map, ',')) {
  371. *sync = 0;
  372. sync_file_idx = strtol(sync + 1, &sync, 0);
  373. if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
  374. av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
  375. exit_program(1);
  376. }
  377. if (*sync)
  378. sync++;
  379. for (i = 0; i < input_files[sync_file_idx]->nb_streams; i++)
  380. if (check_stream_specifier(input_files[sync_file_idx]->ctx,
  381. input_files[sync_file_idx]->ctx->streams[i], sync) == 1) {
  382. sync_stream_idx = i;
  383. break;
  384. }
  385. if (i == input_files[sync_file_idx]->nb_streams) {
  386. av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
  387. "match any streams.\n", arg);
  388. exit_program(1);
  389. }
  390. if (input_streams[input_files[sync_file_idx]->ist_index + sync_stream_idx]->user_set_discard == AVDISCARD_ALL) {
  391. av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s matches a disabled input "
  392. "stream.\n", arg);
  393. exit_program(1);
  394. }
  395. }
  396. if (map[0] == '[') {
  397. /* this mapping refers to lavfi output */
  398. const char *c = map + 1;
  399. GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
  400. m = &o->stream_maps[o->nb_stream_maps - 1];
  401. m->linklabel = av_get_token(&c, "]");
  402. if (!m->linklabel) {
  403. av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", map);
  404. exit_program(1);
  405. }
  406. } else {
  407. if (allow_unused = strchr(map, '?'))
  408. *allow_unused = 0;
  409. file_idx = strtol(map, &p, 0);
  410. if (file_idx >= nb_input_files || file_idx < 0) {
  411. av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
  412. exit_program(1);
  413. }
  414. if (negative)
  415. /* disable some already defined maps */
  416. for (i = 0; i < o->nb_stream_maps; i++) {
  417. m = &o->stream_maps[i];
  418. if (file_idx == m->file_index &&
  419. check_stream_specifier(input_files[m->file_index]->ctx,
  420. input_files[m->file_index]->ctx->streams[m->stream_index],
  421. *p == ':' ? p + 1 : p) > 0)
  422. m->disabled = 1;
  423. }
  424. else
  425. for (i = 0; i < input_files[file_idx]->nb_streams; i++) {
  426. if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
  427. *p == ':' ? p + 1 : p) <= 0)
  428. continue;
  429. if (input_streams[input_files[file_idx]->ist_index + i]->user_set_discard == AVDISCARD_ALL) {
  430. disabled = 1;
  431. continue;
  432. }
  433. GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
  434. m = &o->stream_maps[o->nb_stream_maps - 1];
  435. m->file_index = file_idx;
  436. m->stream_index = i;
  437. if (sync_file_idx >= 0) {
  438. m->sync_file_index = sync_file_idx;
  439. m->sync_stream_index = sync_stream_idx;
  440. } else {
  441. m->sync_file_index = file_idx;
  442. m->sync_stream_index = i;
  443. }
  444. }
  445. }
  446. if (!m) {
  447. if (allow_unused) {
  448. av_log(NULL, AV_LOG_VERBOSE, "Stream map '%s' matches no streams; ignoring.\n", arg);
  449. } else if (disabled) {
  450. av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches disabled streams.\n"
  451. "To ignore this, add a trailing '?' to the map.\n", arg);
  452. exit_program(1);
  453. } else {
  454. av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n"
  455. "To ignore this, add a trailing '?' to the map.\n", arg);
  456. exit_program(1);
  457. }
  458. }
  459. av_freep(&map);
  460. return 0;
  461. }
  462. static int opt_attach(void *optctx, const char *opt, const char *arg)
  463. {
  464. OptionsContext *o = optctx;
  465. GROW_ARRAY(o->attachments, o->nb_attachments);
  466. o->attachments[o->nb_attachments - 1] = arg;
  467. return 0;
  468. }
  469. static int opt_map_channel(void *optctx, const char *opt, const char *arg)
  470. {
  471. OptionsContext *o = optctx;
  472. int n;
  473. AVStream *st;
  474. AudioChannelMap *m;
  475. char *allow_unused;
  476. char *mapchan;
  477. mapchan = av_strdup(arg);
  478. if (!mapchan)
  479. return AVERROR(ENOMEM);
  480. GROW_ARRAY(o->audio_channel_maps, o->nb_audio_channel_maps);
  481. m = &o->audio_channel_maps[o->nb_audio_channel_maps - 1];
  482. /* muted channel syntax */
  483. n = sscanf(arg, "%d:%d.%d", &m->channel_idx, &m->ofile_idx, &m->ostream_idx);
  484. if ((n == 1 || n == 3) && m->channel_idx == -1) {
  485. m->file_idx = m->stream_idx = -1;
  486. if (n == 1)
  487. m->ofile_idx = m->ostream_idx = -1;
  488. av_free(mapchan);
  489. return 0;
  490. }
  491. /* normal syntax */
  492. n = sscanf(arg, "%d.%d.%d:%d.%d",
  493. &m->file_idx, &m->stream_idx, &m->channel_idx,
  494. &m->ofile_idx, &m->ostream_idx);
  495. if (n != 3 && n != 5) {
  496. av_log(NULL, AV_LOG_FATAL, "Syntax error, mapchan usage: "
  497. "[file.stream.channel|-1][:syncfile:syncstream]\n");
  498. exit_program(1);
  499. }
  500. if (n != 5) // only file.stream.channel specified
  501. m->ofile_idx = m->ostream_idx = -1;
  502. /* check input */
  503. if (m->file_idx < 0 || m->file_idx >= nb_input_files) {
  504. av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file index: %d\n",
  505. m->file_idx);
  506. exit_program(1);
  507. }
  508. if (m->stream_idx < 0 ||
  509. m->stream_idx >= input_files[m->file_idx]->nb_streams) {
  510. av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file stream index #%d.%d\n",
  511. m->file_idx, m->stream_idx);
  512. exit_program(1);
  513. }
  514. st = input_files[m->file_idx]->ctx->streams[m->stream_idx];
  515. if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
  516. av_log(NULL, AV_LOG_FATAL, "mapchan: stream #%d.%d is not an audio stream.\n",
  517. m->file_idx, m->stream_idx);
  518. exit_program(1);
  519. }
  520. /* allow trailing ? to map_channel */
  521. if (allow_unused = strchr(mapchan, '?'))
  522. *allow_unused = 0;
  523. if (m->channel_idx < 0 || m->channel_idx >= st->codecpar->ch_layout.nb_channels ||
  524. input_streams[input_files[m->file_idx]->ist_index + m->stream_idx]->user_set_discard == AVDISCARD_ALL) {
  525. if (allow_unused) {
  526. av_log(NULL, AV_LOG_VERBOSE, "mapchan: invalid audio channel #%d.%d.%d\n",
  527. m->file_idx, m->stream_idx, m->channel_idx);
  528. } else {
  529. av_log(NULL, AV_LOG_FATAL, "mapchan: invalid audio channel #%d.%d.%d\n"
  530. "To ignore this, add a trailing '?' to the map_channel.\n",
  531. m->file_idx, m->stream_idx, m->channel_idx);
  532. exit_program(1);
  533. }
  534. }
  535. av_free(mapchan);
  536. return 0;
  537. }
  538. static int opt_sdp_file(void *optctx, const char *opt, const char *arg)
  539. {
  540. av_free(sdp_filename);
  541. sdp_filename = av_strdup(arg);
  542. return 0;
  543. }
  544. #if CONFIG_VAAPI
  545. static int opt_vaapi_device(void *optctx, const char *opt, const char *arg)
  546. {
  547. const char *prefix = "vaapi:";
  548. char *tmp;
  549. int err;
  550. tmp = av_asprintf("%s%s", prefix, arg);
  551. if (!tmp)
  552. return AVERROR(ENOMEM);
  553. err = hw_device_init_from_string(tmp, NULL);
  554. av_free(tmp);
  555. return err;
  556. }
  557. #endif
  558. #if CONFIG_QSV
  559. static int opt_qsv_device(void *optctx, const char *opt, const char *arg)
  560. {
  561. const char *prefix = "qsv=__qsv_device:hw_any,child_device=";
  562. int err;
  563. char *tmp = av_asprintf("%s%s", prefix, arg);
  564. if (!tmp)
  565. return AVERROR(ENOMEM);
  566. err = hw_device_init_from_string(tmp, NULL);
  567. av_free(tmp);
  568. return err;
  569. }
  570. #endif
  571. static int opt_init_hw_device(void *optctx, const char *opt, const char *arg)
  572. {
  573. if (!strcmp(arg, "list")) {
  574. enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
  575. printf("Supported hardware device types:\n");
  576. while ((type = av_hwdevice_iterate_types(type)) !=
  577. AV_HWDEVICE_TYPE_NONE)
  578. printf("%s\n", av_hwdevice_get_type_name(type));
  579. printf("\n");
  580. exit_program(0);
  581. } else {
  582. return hw_device_init_from_string(arg, NULL);
  583. }
  584. }
  585. static int opt_filter_hw_device(void *optctx, const char *opt, const char *arg)
  586. {
  587. if (filter_hw_device) {
  588. av_log(NULL, AV_LOG_ERROR, "Only one filter device can be used.\n");
  589. return AVERROR(EINVAL);
  590. }
  591. filter_hw_device = hw_device_get_by_name(arg);
  592. if (!filter_hw_device) {
  593. av_log(NULL, AV_LOG_ERROR, "Invalid filter device %s.\n", arg);
  594. return AVERROR(EINVAL);
  595. }
  596. return 0;
  597. }
  598. /**
  599. * Parse a metadata specifier passed as 'arg' parameter.
  600. * @param arg metadata string to parse
  601. * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram)
  602. * @param index for type c/p, chapter/program index is written here
  603. * @param stream_spec for type s, the stream specifier is written here
  604. */
  605. static void parse_meta_type(char *arg, char *type, int *index, const char **stream_spec)
  606. {
  607. if (*arg) {
  608. *type = *arg;
  609. switch (*arg) {
  610. case 'g':
  611. break;
  612. case 's':
  613. if (*(++arg) && *arg != ':') {
  614. av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
  615. exit_program(1);
  616. }
  617. *stream_spec = *arg == ':' ? arg + 1 : "";
  618. break;
  619. case 'c':
  620. case 'p':
  621. if (*(++arg) == ':')
  622. *index = strtol(++arg, NULL, 0);
  623. break;
  624. default:
  625. av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
  626. exit_program(1);
  627. }
  628. } else
  629. *type = 'g';
  630. }
  631. static int copy_metadata(char *outspec, char *inspec, AVFormatContext *oc, AVFormatContext *ic, OptionsContext *o)
  632. {
  633. AVDictionary **meta_in = NULL;
  634. AVDictionary **meta_out = NULL;
  635. int i, ret = 0;
  636. char type_in, type_out;
  637. const char *istream_spec = NULL, *ostream_spec = NULL;
  638. int idx_in = 0, idx_out = 0;
  639. parse_meta_type(inspec, &type_in, &idx_in, &istream_spec);
  640. parse_meta_type(outspec, &type_out, &idx_out, &ostream_spec);
  641. if (!ic) {
  642. if (type_out == 'g' || !*outspec)
  643. o->metadata_global_manual = 1;
  644. if (type_out == 's' || !*outspec)
  645. o->metadata_streams_manual = 1;
  646. if (type_out == 'c' || !*outspec)
  647. o->metadata_chapters_manual = 1;
  648. return 0;
  649. }
  650. if (type_in == 'g' || type_out == 'g')
  651. o->metadata_global_manual = 1;
  652. if (type_in == 's' || type_out == 's')
  653. o->metadata_streams_manual = 1;
  654. if (type_in == 'c' || type_out == 'c')
  655. o->metadata_chapters_manual = 1;
  656. /* ic is NULL when just disabling automatic mappings */
  657. if (!ic)
  658. return 0;
  659. #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
  660. if ((index) < 0 || (index) >= (nb_elems)) {\
  661. av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
  662. (desc), (index));\
  663. exit_program(1);\
  664. }
  665. #define SET_DICT(type, meta, context, index)\
  666. switch (type) {\
  667. case 'g':\
  668. meta = &context->metadata;\
  669. break;\
  670. case 'c':\
  671. METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
  672. meta = &context->chapters[index]->metadata;\
  673. break;\
  674. case 'p':\
  675. METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
  676. meta = &context->programs[index]->metadata;\
  677. break;\
  678. case 's':\
  679. break; /* handled separately below */ \
  680. default: av_assert0(0);\
  681. }\
  682. SET_DICT(type_in, meta_in, ic, idx_in);
  683. SET_DICT(type_out, meta_out, oc, idx_out);
  684. /* for input streams choose first matching stream */
  685. if (type_in == 's') {
  686. for (i = 0; i < ic->nb_streams; i++) {
  687. if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
  688. meta_in = &ic->streams[i]->metadata;
  689. break;
  690. } else if (ret < 0)
  691. exit_program(1);
  692. }
  693. if (!meta_in) {
  694. av_log(NULL, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec);
  695. exit_program(1);
  696. }
  697. }
  698. if (type_out == 's') {
  699. for (i = 0; i < oc->nb_streams; i++) {
  700. if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
  701. meta_out = &oc->streams[i]->metadata;
  702. av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
  703. } else if (ret < 0)
  704. exit_program(1);
  705. }
  706. } else
  707. av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
  708. return 0;
  709. }
  710. static int opt_recording_timestamp(void *optctx, const char *opt, const char *arg)
  711. {
  712. OptionsContext *o = optctx;
  713. char buf[128];
  714. int64_t recording_timestamp = parse_time_or_die(opt, arg, 0) / 1E6;
  715. struct tm time = *gmtime((time_t*)&recording_timestamp);
  716. if (!strftime(buf, sizeof(buf), "creation_time=%Y-%m-%dT%H:%M:%S%z", &time))
  717. return -1;
  718. parse_option(o, "metadata", buf, options);
  719. av_log(NULL, AV_LOG_WARNING, "%s is deprecated, set the 'creation_time' metadata "
  720. "tag instead.\n", opt);
  721. return 0;
  722. }
  723. static const AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
  724. {
  725. const AVCodecDescriptor *desc;
  726. const char *codec_string = encoder ? "encoder" : "decoder";
  727. const AVCodec *codec;
  728. codec = encoder ?
  729. avcodec_find_encoder_by_name(name) :
  730. avcodec_find_decoder_by_name(name);
  731. if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
  732. codec = encoder ? avcodec_find_encoder(desc->id) :
  733. avcodec_find_decoder(desc->id);
  734. if (codec)
  735. av_log(NULL, AV_LOG_VERBOSE, "Matched %s '%s' for codec '%s'.\n",
  736. codec_string, codec->name, desc->name);
  737. }
  738. if (!codec) {
  739. av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
  740. exit_program(1);
  741. }
  742. if (codec->type != type && !recast_media) {
  743. av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
  744. exit_program(1);
  745. }
  746. return codec;
  747. }
  748. static const AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
  749. {
  750. char *codec_name = NULL;
  751. MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
  752. if (codec_name) {
  753. const AVCodec *codec = find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
  754. st->codecpar->codec_id = codec->id;
  755. if (recast_media && st->codecpar->codec_type != codec->type)
  756. st->codecpar->codec_type = codec->type;
  757. return codec;
  758. } else
  759. return avcodec_find_decoder(st->codecpar->codec_id);
  760. }
  761. /* Add all the streams from the given input file to the global
  762. * list of input streams. */
  763. static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
  764. {
  765. int i, ret;
  766. for (i = 0; i < ic->nb_streams; i++) {
  767. AVStream *st = ic->streams[i];
  768. AVCodecParameters *par = st->codecpar;
  769. InputStream *ist;
  770. char *framerate = NULL, *hwaccel_device = NULL;
  771. const char *hwaccel = NULL;
  772. char *hwaccel_output_format = NULL;
  773. char *codec_tag = NULL;
  774. char *next;
  775. char *discard_str = NULL;
  776. const AVClass *cc = avcodec_get_class();
  777. const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
  778. 0, AV_OPT_SEARCH_FAKE_OBJ);
  779. ist = ALLOC_ARRAY_ELEM(input_streams, nb_input_streams);
  780. ist->st = st;
  781. ist->file_index = nb_input_files;
  782. ist->discard = 1;
  783. st->discard = AVDISCARD_ALL;
  784. ist->nb_samples = 0;
  785. ist->first_dts = AV_NOPTS_VALUE;
  786. ist->min_pts = INT64_MAX;
  787. ist->max_pts = INT64_MIN;
  788. ist->ts_scale = 1.0;
  789. MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
  790. ist->autorotate = 1;
  791. MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
  792. MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
  793. if (codec_tag) {
  794. uint32_t tag = strtol(codec_tag, &next, 0);
  795. if (*next) {
  796. uint8_t buf[4] = { 0 };
  797. memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
  798. tag = AV_RL32(buf);
  799. }
  800. st->codecpar->codec_tag = tag;
  801. }
  802. ist->dec = choose_decoder(o, ic, st);
  803. ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
  804. ist->reinit_filters = -1;
  805. MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
  806. MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st);
  807. ist->user_set_discard = AVDISCARD_NONE;
  808. if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
  809. (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
  810. (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
  811. (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
  812. ist->user_set_discard = AVDISCARD_ALL;
  813. if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard) < 0) {
  814. av_log(NULL, AV_LOG_ERROR, "Error parsing discard %s.\n",
  815. discard_str);
  816. exit_program(1);
  817. }
  818. ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
  819. ist->prev_pkt_pts = AV_NOPTS_VALUE;
  820. ist->dec_ctx = avcodec_alloc_context3(ist->dec);
  821. if (!ist->dec_ctx) {
  822. av_log(NULL, AV_LOG_ERROR, "Error allocating the decoder context.\n");
  823. exit_program(1);
  824. }
  825. ret = avcodec_parameters_to_context(ist->dec_ctx, par);
  826. if (ret < 0) {
  827. av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  828. exit_program(1);
  829. }
  830. ist->decoded_frame = av_frame_alloc();
  831. if (!ist->decoded_frame)
  832. exit_program(1);
  833. ist->pkt = av_packet_alloc();
  834. if (!ist->pkt)
  835. exit_program(1);
  836. if (o->bitexact)
  837. ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  838. switch (par->codec_type) {
  839. case AVMEDIA_TYPE_VIDEO:
  840. if(!ist->dec)
  841. ist->dec = avcodec_find_decoder(par->codec_id);
  842. // avformat_find_stream_info() doesn't set this for us anymore.
  843. ist->dec_ctx->framerate = st->avg_frame_rate;
  844. MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
  845. if (framerate && av_parse_video_rate(&ist->framerate,
  846. framerate) < 0) {
  847. av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n",
  848. framerate);
  849. exit_program(1);
  850. }
  851. ist->top_field_first = -1;
  852. MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
  853. MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
  854. MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
  855. hwaccel_output_format, ic, st);
  856. if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
  857. av_log(NULL, AV_LOG_WARNING,
  858. "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
  859. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  860. "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
  861. ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
  862. } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
  863. av_log(NULL, AV_LOG_WARNING,
  864. "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
  865. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  866. "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
  867. ist->hwaccel_output_format = AV_PIX_FMT_QSV;
  868. } else if (hwaccel_output_format) {
  869. ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
  870. if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
  871. av_log(NULL, AV_LOG_FATAL, "Unrecognised hwaccel output "
  872. "format: %s", hwaccel_output_format);
  873. }
  874. } else {
  875. ist->hwaccel_output_format = AV_PIX_FMT_NONE;
  876. }
  877. if (hwaccel) {
  878. // The NVDEC hwaccels use a CUDA device, so remap the name here.
  879. if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
  880. hwaccel = "cuda";
  881. if (!strcmp(hwaccel, "none"))
  882. ist->hwaccel_id = HWACCEL_NONE;
  883. else if (!strcmp(hwaccel, "auto"))
  884. ist->hwaccel_id = HWACCEL_AUTO;
  885. else {
  886. enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
  887. if (type != AV_HWDEVICE_TYPE_NONE) {
  888. ist->hwaccel_id = HWACCEL_GENERIC;
  889. ist->hwaccel_device_type = type;
  890. }
  891. if (!ist->hwaccel_id) {
  892. av_log(NULL, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
  893. hwaccel);
  894. av_log(NULL, AV_LOG_FATAL, "Supported hwaccels: ");
  895. type = AV_HWDEVICE_TYPE_NONE;
  896. while ((type = av_hwdevice_iterate_types(type)) !=
  897. AV_HWDEVICE_TYPE_NONE)
  898. av_log(NULL, AV_LOG_FATAL, "%s ",
  899. av_hwdevice_get_type_name(type));
  900. av_log(NULL, AV_LOG_FATAL, "\n");
  901. exit_program(1);
  902. }
  903. }
  904. }
  905. MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
  906. if (hwaccel_device) {
  907. ist->hwaccel_device = av_strdup(hwaccel_device);
  908. if (!ist->hwaccel_device)
  909. exit_program(1);
  910. }
  911. ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
  912. break;
  913. case AVMEDIA_TYPE_AUDIO:
  914. ist->guess_layout_max = INT_MAX;
  915. MATCH_PER_STREAM_OPT(guess_layout_max, i, ist->guess_layout_max, ic, st);
  916. guess_input_channel_layout(ist);
  917. break;
  918. case AVMEDIA_TYPE_DATA:
  919. case AVMEDIA_TYPE_SUBTITLE: {
  920. char *canvas_size = NULL;
  921. if(!ist->dec)
  922. ist->dec = avcodec_find_decoder(par->codec_id);
  923. MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
  924. MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
  925. if (canvas_size &&
  926. av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height, canvas_size) < 0) {
  927. av_log(NULL, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
  928. exit_program(1);
  929. }
  930. break;
  931. }
  932. case AVMEDIA_TYPE_ATTACHMENT:
  933. case AVMEDIA_TYPE_UNKNOWN:
  934. break;
  935. default:
  936. abort();
  937. }
  938. ret = avcodec_parameters_from_context(par, ist->dec_ctx);
  939. if (ret < 0) {
  940. av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  941. exit_program(1);
  942. }
  943. }
  944. }
  945. static void assert_file_overwrite(const char *filename)
  946. {
  947. const char *proto_name = avio_find_protocol_name(filename);
  948. if (file_overwrite && no_file_overwrite) {
  949. fprintf(stderr, "Error, both -y and -n supplied. Exiting.\n");
  950. exit_program(1);
  951. }
  952. if (!file_overwrite) {
  953. if (proto_name && !strcmp(proto_name, "file") && avio_check(filename, 0) == 0) {
  954. if (stdin_interaction && !no_file_overwrite) {
  955. fprintf(stderr,"File '%s' already exists. Overwrite? [y/N] ", filename);
  956. fflush(stderr);
  957. term_exit();
  958. signal(SIGINT, SIG_DFL);
  959. if (!read_yesno()) {
  960. av_log(NULL, AV_LOG_FATAL, "Not overwriting - exiting\n");
  961. exit_program(1);
  962. }
  963. term_init();
  964. }
  965. else {
  966. av_log(NULL, AV_LOG_FATAL, "File '%s' already exists. Exiting.\n", filename);
  967. exit_program(1);
  968. }
  969. }
  970. }
  971. if (proto_name && !strcmp(proto_name, "file")) {
  972. for (int i = 0; i < nb_input_files; i++) {
  973. InputFile *file = input_files[i];
  974. if (file->ctx->iformat->flags & AVFMT_NOFILE)
  975. continue;
  976. if (!strcmp(filename, file->ctx->url)) {
  977. av_log(NULL, AV_LOG_FATAL, "Output %s same as Input #%d - exiting\n", filename, i);
  978. av_log(NULL, AV_LOG_WARNING, "FFmpeg cannot edit existing files in-place.\n");
  979. exit_program(1);
  980. }
  981. }
  982. }
  983. }
  984. static void dump_attachment(AVStream *st, const char *filename)
  985. {
  986. int ret;
  987. AVIOContext *out = NULL;
  988. const AVDictionaryEntry *e;
  989. if (!st->codecpar->extradata_size) {
  990. av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
  991. nb_input_files - 1, st->index);
  992. return;
  993. }
  994. if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
  995. filename = e->value;
  996. if (!*filename) {
  997. av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
  998. "in stream #%d:%d.\n", nb_input_files - 1, st->index);
  999. exit_program(1);
  1000. }
  1001. assert_file_overwrite(filename);
  1002. if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
  1003. av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
  1004. filename);
  1005. exit_program(1);
  1006. }
  1007. avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
  1008. avio_flush(out);
  1009. avio_close(out);
  1010. }
  1011. static int open_input_file(OptionsContext *o, const char *filename)
  1012. {
  1013. InputFile *f;
  1014. AVFormatContext *ic;
  1015. const AVInputFormat *file_iformat = NULL;
  1016. int err, i, ret;
  1017. int64_t timestamp;
  1018. AVDictionary *unused_opts = NULL;
  1019. const AVDictionaryEntry *e = NULL;
  1020. char * video_codec_name = NULL;
  1021. char * audio_codec_name = NULL;
  1022. char *subtitle_codec_name = NULL;
  1023. char * data_codec_name = NULL;
  1024. int scan_all_pmts_set = 0;
  1025. if (o->stop_time != INT64_MAX && o->recording_time != INT64_MAX) {
  1026. o->stop_time = INT64_MAX;
  1027. av_log(NULL, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
  1028. }
  1029. if (o->stop_time != INT64_MAX && o->recording_time == INT64_MAX) {
  1030. int64_t start_time = o->start_time == AV_NOPTS_VALUE ? 0 : o->start_time;
  1031. if (o->stop_time <= start_time) {
  1032. av_log(NULL, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
  1033. exit_program(1);
  1034. } else {
  1035. o->recording_time = o->stop_time - start_time;
  1036. }
  1037. }
  1038. if (o->format) {
  1039. if (!(file_iformat = av_find_input_format(o->format))) {
  1040. av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
  1041. exit_program(1);
  1042. }
  1043. }
  1044. if (!strcmp(filename, "-"))
  1045. filename = "pipe:";
  1046. stdin_interaction &= strncmp(filename, "pipe:", 5) &&
  1047. strcmp(filename, "/dev/stdin");
  1048. /* get default parameters from command line */
  1049. ic = avformat_alloc_context();
  1050. if (!ic) {
  1051. print_error(filename, AVERROR(ENOMEM));
  1052. exit_program(1);
  1053. }
  1054. if (o->nb_audio_sample_rate) {
  1055. av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
  1056. }
  1057. if (o->nb_audio_channels) {
  1058. const AVClass *priv_class;
  1059. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1060. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  1061. AV_OPT_SEARCH_FAKE_OBJ)) {
  1062. char buf[32];
  1063. snprintf(buf, sizeof(buf), "%dC", o->audio_channels[o->nb_audio_channels - 1].u.i);
  1064. av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
  1065. }
  1066. }
  1067. if (o->nb_audio_ch_layouts) {
  1068. const AVClass *priv_class;
  1069. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1070. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  1071. AV_OPT_SEARCH_FAKE_OBJ)) {
  1072. av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts[o->nb_audio_ch_layouts - 1].u.str, 0);
  1073. }
  1074. }
  1075. if (o->nb_frame_rates) {
  1076. const AVClass *priv_class;
  1077. /* set the format-level framerate option;
  1078. * this is important for video grabbers, e.g. x11 */
  1079. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1080. av_opt_find(&priv_class, "framerate", NULL, 0,
  1081. AV_OPT_SEARCH_FAKE_OBJ)) {
  1082. av_dict_set(&o->g->format_opts, "framerate",
  1083. o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
  1084. }
  1085. }
  1086. if (o->nb_frame_sizes) {
  1087. av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
  1088. }
  1089. if (o->nb_frame_pix_fmts)
  1090. av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
  1091. MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
  1092. MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
  1093. MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
  1094. MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
  1095. if (video_codec_name)
  1096. ic->video_codec = find_codec_or_die(video_codec_name , AVMEDIA_TYPE_VIDEO , 0);
  1097. if (audio_codec_name)
  1098. ic->audio_codec = find_codec_or_die(audio_codec_name , AVMEDIA_TYPE_AUDIO , 0);
  1099. if (subtitle_codec_name)
  1100. ic->subtitle_codec = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
  1101. if (data_codec_name)
  1102. ic->data_codec = find_codec_or_die(data_codec_name , AVMEDIA_TYPE_DATA , 0);
  1103. ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
  1104. ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
  1105. ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
  1106. ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
  1107. ic->flags |= AVFMT_FLAG_NONBLOCK;
  1108. if (o->bitexact)
  1109. ic->flags |= AVFMT_FLAG_BITEXACT;
  1110. ic->interrupt_callback = int_cb;
  1111. if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  1112. av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  1113. scan_all_pmts_set = 1;
  1114. }
  1115. /* open the input file with generic avformat function */
  1116. err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
  1117. if (err < 0) {
  1118. print_error(filename, err);
  1119. if (err == AVERROR_PROTOCOL_NOT_FOUND)
  1120. av_log(NULL, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
  1121. exit_program(1);
  1122. }
  1123. if (scan_all_pmts_set)
  1124. av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  1125. remove_avoptions(&o->g->format_opts, o->g->codec_opts);
  1126. assert_avoptions(o->g->format_opts);
  1127. /* apply forced codec ids */
  1128. for (i = 0; i < ic->nb_streams; i++)
  1129. choose_decoder(o, ic, ic->streams[i]);
  1130. if (find_stream_info) {
  1131. AVDictionary **opts = setup_find_stream_info_opts(ic, o->g->codec_opts);
  1132. int orig_nb_streams = ic->nb_streams;
  1133. /* If not enough info to get the stream parameters, we decode the
  1134. first frames to get it. (used in mpeg case for example) */
  1135. ret = avformat_find_stream_info(ic, opts);
  1136. for (i = 0; i < orig_nb_streams; i++)
  1137. av_dict_free(&opts[i]);
  1138. av_freep(&opts);
  1139. if (ret < 0) {
  1140. av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
  1141. if (ic->nb_streams == 0) {
  1142. avformat_close_input(&ic);
  1143. exit_program(1);
  1144. }
  1145. }
  1146. }
  1147. if (o->start_time != AV_NOPTS_VALUE && o->start_time_eof != AV_NOPTS_VALUE) {
  1148. av_log(NULL, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss for %s\n", filename);
  1149. o->start_time_eof = AV_NOPTS_VALUE;
  1150. }
  1151. if (o->start_time_eof != AV_NOPTS_VALUE) {
  1152. if (o->start_time_eof >= 0) {
  1153. av_log(NULL, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
  1154. exit_program(1);
  1155. }
  1156. if (ic->duration > 0) {
  1157. o->start_time = o->start_time_eof + ic->duration;
  1158. if (o->start_time < 0) {
  1159. av_log(NULL, AV_LOG_WARNING, "-sseof value seeks to before start of file %s; ignored\n", filename);
  1160. o->start_time = AV_NOPTS_VALUE;
  1161. }
  1162. } else
  1163. av_log(NULL, AV_LOG_WARNING, "Cannot use -sseof, duration of %s not known\n", filename);
  1164. }
  1165. timestamp = (o->start_time == AV_NOPTS_VALUE) ? 0 : o->start_time;
  1166. /* add the stream start time */
  1167. if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
  1168. timestamp += ic->start_time;
  1169. /* if seeking requested, we execute it */
  1170. if (o->start_time != AV_NOPTS_VALUE) {
  1171. int64_t seek_timestamp = timestamp;
  1172. if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
  1173. int dts_heuristic = 0;
  1174. for (i=0; i<ic->nb_streams; i++) {
  1175. const AVCodecParameters *par = ic->streams[i]->codecpar;
  1176. if (par->video_delay) {
  1177. dts_heuristic = 1;
  1178. break;
  1179. }
  1180. }
  1181. if (dts_heuristic) {
  1182. seek_timestamp -= 3*AV_TIME_BASE / 23;
  1183. }
  1184. }
  1185. ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
  1186. if (ret < 0) {
  1187. av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
  1188. filename, (double)timestamp / AV_TIME_BASE);
  1189. }
  1190. }
  1191. /* update the current parameters so that they match the one of the input stream */
  1192. add_input_streams(o, ic);
  1193. /* dump the file content */
  1194. av_dump_format(ic, nb_input_files, filename, 0);
  1195. f = ALLOC_ARRAY_ELEM(input_files, nb_input_files);
  1196. f->ctx = ic;
  1197. f->ist_index = nb_input_streams - ic->nb_streams;
  1198. f->start_time = o->start_time;
  1199. f->recording_time = o->recording_time;
  1200. f->input_sync_ref = o->input_sync_ref;
  1201. f->input_ts_offset = o->input_ts_offset;
  1202. f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
  1203. f->nb_streams = ic->nb_streams;
  1204. f->rate_emu = o->rate_emu;
  1205. f->accurate_seek = o->accurate_seek;
  1206. f->loop = o->loop;
  1207. f->duration = 0;
  1208. f->time_base = (AVRational){ 1, 1 };
  1209. f->readrate = o->readrate ? o->readrate : 0.0;
  1210. if (f->readrate < 0.0f) {
  1211. av_log(NULL, AV_LOG_ERROR, "Option -readrate for Input #%d is %0.3f; it must be non-negative.\n", nb_input_files, f->readrate);
  1212. exit_program(1);
  1213. }
  1214. if (f->readrate && f->rate_emu) {
  1215. av_log(NULL, AV_LOG_WARNING, "Both -readrate and -re set for Input #%d. Using -readrate %0.3f.\n", nb_input_files, f->readrate);
  1216. f->rate_emu = 0;
  1217. }
  1218. f->pkt = av_packet_alloc();
  1219. if (!f->pkt)
  1220. exit_program(1);
  1221. #if HAVE_THREADS
  1222. f->thread_queue_size = o->thread_queue_size;
  1223. #endif
  1224. /* check if all codec options have been used */
  1225. unused_opts = strip_specifiers(o->g->codec_opts);
  1226. for (i = f->ist_index; i < nb_input_streams; i++) {
  1227. e = NULL;
  1228. while ((e = av_dict_get(input_streams[i]->decoder_opts, "", e,
  1229. AV_DICT_IGNORE_SUFFIX)))
  1230. av_dict_set(&unused_opts, e->key, NULL, 0);
  1231. }
  1232. e = NULL;
  1233. while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
  1234. const AVClass *class = avcodec_get_class();
  1235. const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
  1236. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  1237. const AVClass *fclass = avformat_get_class();
  1238. const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
  1239. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  1240. if (!option || foption)
  1241. continue;
  1242. if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
  1243. av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
  1244. "input file #%d (%s) is not a decoding option.\n", e->key,
  1245. option->help ? option->help : "", nb_input_files - 1,
  1246. filename);
  1247. exit_program(1);
  1248. }
  1249. av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
  1250. "input file #%d (%s) has not been used for any stream. The most "
  1251. "likely reason is either wrong type (e.g. a video option with "
  1252. "no video streams) or that it is a private option of some decoder "
  1253. "which was not actually used for any stream.\n", e->key,
  1254. option->help ? option->help : "", nb_input_files - 1, filename);
  1255. }
  1256. av_dict_free(&unused_opts);
  1257. for (i = 0; i < o->nb_dump_attachment; i++) {
  1258. int j;
  1259. for (j = 0; j < ic->nb_streams; j++) {
  1260. AVStream *st = ic->streams[j];
  1261. if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
  1262. dump_attachment(st, o->dump_attachment[i].u.str);
  1263. }
  1264. }
  1265. input_stream_potentially_available = 1;
  1266. return 0;
  1267. }
  1268. static char *get_line(AVIOContext *s, AVBPrint *bprint)
  1269. {
  1270. char c;
  1271. while ((c = avio_r8(s)) && c != '\n')
  1272. av_bprint_chars(bprint, c, 1);
  1273. if (!av_bprint_is_complete(bprint)) {
  1274. av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
  1275. exit_program(1);
  1276. }
  1277. return bprint->str;
  1278. }
  1279. static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
  1280. {
  1281. int i, ret = -1;
  1282. char filename[1000];
  1283. char *env_avconv_datadir = getenv_utf8("AVCONV_DATADIR");
  1284. char *env_home = getenv_utf8("HOME");
  1285. const char *base[3] = { env_avconv_datadir,
  1286. env_home,
  1287. AVCONV_DATADIR,
  1288. };
  1289. for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
  1290. if (!base[i])
  1291. continue;
  1292. if (codec_name) {
  1293. snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
  1294. i != 1 ? "" : "/.avconv", codec_name, preset_name);
  1295. ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
  1296. }
  1297. if (ret < 0) {
  1298. snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
  1299. i != 1 ? "" : "/.avconv", preset_name);
  1300. ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
  1301. }
  1302. }
  1303. freeenv_utf8(env_home);
  1304. freeenv_utf8(env_avconv_datadir);
  1305. return ret;
  1306. }
  1307. static int choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost)
  1308. {
  1309. enum AVMediaType type = ost->st->codecpar->codec_type;
  1310. char *codec_name = NULL;
  1311. if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
  1312. MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
  1313. if (!codec_name) {
  1314. ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
  1315. NULL, ost->st->codecpar->codec_type);
  1316. ost->enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
  1317. if (!ost->enc) {
  1318. av_log(NULL, AV_LOG_FATAL, "Automatic encoder selection failed for "
  1319. "output stream #%d:%d. Default encoder for format %s (codec %s) is "
  1320. "probably disabled. Please choose an encoder manually.\n",
  1321. ost->file_index, ost->index, s->oformat->name,
  1322. avcodec_get_name(ost->st->codecpar->codec_id));
  1323. return AVERROR_ENCODER_NOT_FOUND;
  1324. }
  1325. } else if (!strcmp(codec_name, "copy"))
  1326. ost->stream_copy = 1;
  1327. else {
  1328. ost->enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1);
  1329. ost->st->codecpar->codec_id = ost->enc->id;
  1330. }
  1331. ost->encoding_needed = !ost->stream_copy;
  1332. } else {
  1333. /* no encoding supported for other media types */
  1334. ost->stream_copy = 1;
  1335. ost->encoding_needed = 0;
  1336. }
  1337. return 0;
  1338. }
  1339. static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type, int source_index)
  1340. {
  1341. OutputStream *ost;
  1342. AVStream *st = avformat_new_stream(oc, NULL);
  1343. int idx = oc->nb_streams - 1, ret = 0;
  1344. const char *bsfs = NULL, *time_base = NULL;
  1345. char *next, *codec_tag = NULL;
  1346. double qscale = -1;
  1347. int i;
  1348. if (!st) {
  1349. av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
  1350. exit_program(1);
  1351. }
  1352. if (oc->nb_streams - 1 < o->nb_streamid_map)
  1353. st->id = o->streamid_map[oc->nb_streams - 1];
  1354. ost = ALLOC_ARRAY_ELEM(output_streams, nb_output_streams);
  1355. ost->file_index = nb_output_files - 1;
  1356. ost->index = idx;
  1357. ost->st = st;
  1358. ost->forced_kf_ref_pts = AV_NOPTS_VALUE;
  1359. st->codecpar->codec_type = type;
  1360. ret = choose_encoder(o, oc, ost);
  1361. if (ret < 0) {
  1362. av_log(NULL, AV_LOG_FATAL, "Error selecting an encoder for stream "
  1363. "%d:%d\n", ost->file_index, ost->index);
  1364. exit_program(1);
  1365. }
  1366. ost->enc_ctx = avcodec_alloc_context3(ost->enc);
  1367. if (!ost->enc_ctx) {
  1368. av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n");
  1369. exit_program(1);
  1370. }
  1371. ost->enc_ctx->codec_type = type;
  1372. ost->ref_par = avcodec_parameters_alloc();
  1373. if (!ost->ref_par) {
  1374. av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding parameters.\n");
  1375. exit_program(1);
  1376. }
  1377. ost->filtered_frame = av_frame_alloc();
  1378. if (!ost->filtered_frame)
  1379. exit_program(1);
  1380. ost->pkt = av_packet_alloc();
  1381. if (!ost->pkt)
  1382. exit_program(1);
  1383. if (ost->enc) {
  1384. AVIOContext *s = NULL;
  1385. char *buf = NULL, *arg = NULL, *preset = NULL;
  1386. ost->encoder_opts = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc);
  1387. MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
  1388. ost->autoscale = 1;
  1389. MATCH_PER_STREAM_OPT(autoscale, i, ost->autoscale, oc, st);
  1390. if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
  1391. AVBPrint bprint;
  1392. av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
  1393. do {
  1394. av_bprint_clear(&bprint);
  1395. buf = get_line(s, &bprint);
  1396. if (!buf[0] || buf[0] == '#')
  1397. continue;
  1398. if (!(arg = strchr(buf, '='))) {
  1399. av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
  1400. exit_program(1);
  1401. }
  1402. *arg++ = 0;
  1403. av_dict_set(&ost->encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
  1404. } while (!s->eof_reached);
  1405. av_bprint_finalize(&bprint, NULL);
  1406. avio_closep(&s);
  1407. }
  1408. if (ret) {
  1409. av_log(NULL, AV_LOG_FATAL,
  1410. "Preset %s specified for stream %d:%d, but could not be opened.\n",
  1411. preset, ost->file_index, ost->index);
  1412. exit_program(1);
  1413. }
  1414. } else {
  1415. ost->encoder_opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);
  1416. }
  1417. if (o->bitexact)
  1418. ost->enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  1419. MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st);
  1420. if (time_base) {
  1421. AVRational q;
  1422. if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
  1423. q.num <= 0 || q.den <= 0) {
  1424. av_log(NULL, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
  1425. exit_program(1);
  1426. }
  1427. st->time_base = q;
  1428. }
  1429. MATCH_PER_STREAM_OPT(enc_time_bases, str, time_base, oc, st);
  1430. if (time_base) {
  1431. AVRational q;
  1432. if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
  1433. q.den <= 0) {
  1434. av_log(NULL, AV_LOG_FATAL, "Invalid time base: %s\n", time_base);
  1435. exit_program(1);
  1436. }
  1437. ost->enc_timebase = q;
  1438. }
  1439. ost->max_frames = INT64_MAX;
  1440. MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
  1441. for (i = 0; i<o->nb_max_frames; i++) {
  1442. char *p = o->max_frames[i].specifier;
  1443. if (!*p && type != AVMEDIA_TYPE_VIDEO) {
  1444. av_log(NULL, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
  1445. break;
  1446. }
  1447. }
  1448. ost->copy_prior_start = -1;
  1449. MATCH_PER_STREAM_OPT(copy_prior_start, i, ost->copy_prior_start, oc ,st);
  1450. MATCH_PER_STREAM_OPT(bitstream_filters, str, bsfs, oc, st);
  1451. if (bsfs && *bsfs) {
  1452. ret = av_bsf_list_parse_str(bsfs, &ost->bsf_ctx);
  1453. if (ret < 0) {
  1454. av_log(NULL, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
  1455. exit_program(1);
  1456. }
  1457. }
  1458. MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
  1459. if (codec_tag) {
  1460. uint32_t tag = strtol(codec_tag, &next, 0);
  1461. if (*next) {
  1462. uint8_t buf[4] = { 0 };
  1463. memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
  1464. tag = AV_RL32(buf);
  1465. }
  1466. ost->st->codecpar->codec_tag =
  1467. ost->enc_ctx->codec_tag = tag;
  1468. }
  1469. MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
  1470. if (qscale >= 0) {
  1471. ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
  1472. ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
  1473. }
  1474. MATCH_PER_STREAM_OPT(disposition, str, ost->disposition, oc, st);
  1475. ost->disposition = av_strdup(ost->disposition);
  1476. ost->max_muxing_queue_size = 128;
  1477. MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ost->max_muxing_queue_size, oc, st);
  1478. ost->muxing_queue_data_size = 0;
  1479. ost->muxing_queue_data_threshold = 50*1024*1024;
  1480. MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, ost->muxing_queue_data_threshold, oc, st);
  1481. MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample,
  1482. oc, st);
  1483. if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  1484. ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  1485. av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0);
  1486. av_dict_copy(&ost->swr_opts, o->g->swr_opts, 0);
  1487. if (ost->enc && av_get_exact_bits_per_sample(ost->enc->id) == 24)
  1488. av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0);
  1489. ost->source_index = source_index;
  1490. if (source_index >= 0) {
  1491. ost->sync_ist = input_streams[source_index];
  1492. input_streams[source_index]->discard = 0;
  1493. input_streams[source_index]->st->discard = input_streams[source_index]->user_set_discard;
  1494. }
  1495. ost->last_mux_dts = AV_NOPTS_VALUE;
  1496. ost->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
  1497. if (!ost->muxing_queue)
  1498. exit_program(1);
  1499. MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
  1500. ost->copy_initial_nonkeyframes, oc, st);
  1501. return ost;
  1502. }
  1503. static void parse_matrix_coeffs(uint16_t *dest, const char *str)
  1504. {
  1505. int i;
  1506. const char *p = str;
  1507. for (i = 0;; i++) {
  1508. dest[i] = atoi(p);
  1509. if (i == 63)
  1510. break;
  1511. p = strchr(p, ',');
  1512. if (!p) {
  1513. av_log(NULL, AV_LOG_FATAL, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
  1514. exit_program(1);
  1515. }
  1516. p++;
  1517. }
  1518. }
  1519. /* read file contents into a string */
  1520. static char *read_file(const char *filename)
  1521. {
  1522. AVIOContext *pb = NULL;
  1523. int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
  1524. AVBPrint bprint;
  1525. char *str;
  1526. if (ret < 0) {
  1527. av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
  1528. return NULL;
  1529. }
  1530. av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
  1531. ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
  1532. avio_closep(&pb);
  1533. if (ret < 0) {
  1534. av_bprint_finalize(&bprint, NULL);
  1535. return NULL;
  1536. }
  1537. ret = av_bprint_finalize(&bprint, &str);
  1538. if (ret < 0)
  1539. return NULL;
  1540. return str;
  1541. }
  1542. static char *get_ost_filters(OptionsContext *o, AVFormatContext *oc,
  1543. OutputStream *ost)
  1544. {
  1545. AVStream *st = ost->st;
  1546. if (ost->filters_script && ost->filters) {
  1547. av_log(NULL, AV_LOG_ERROR, "Both -filter and -filter_script set for "
  1548. "output stream #%d:%d.\n", nb_output_files, st->index);
  1549. exit_program(1);
  1550. }
  1551. if (ost->filters_script)
  1552. return read_file(ost->filters_script);
  1553. else if (ost->filters)
  1554. return av_strdup(ost->filters);
  1555. return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
  1556. "null" : "anull");
  1557. }
  1558. static void check_streamcopy_filters(OptionsContext *o, AVFormatContext *oc,
  1559. const OutputStream *ost, enum AVMediaType type)
  1560. {
  1561. if (ost->filters_script || ost->filters) {
  1562. av_log(NULL, AV_LOG_ERROR,
  1563. "%s '%s' was defined for %s output stream %d:%d but codec copy was selected.\n"
  1564. "Filtering and streamcopy cannot be used together.\n",
  1565. ost->filters ? "Filtergraph" : "Filtergraph script",
  1566. ost->filters ? ost->filters : ost->filters_script,
  1567. av_get_media_type_string(type), ost->file_index, ost->index);
  1568. exit_program(1);
  1569. }
  1570. }
  1571. static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, int source_index)
  1572. {
  1573. AVStream *st;
  1574. OutputStream *ost;
  1575. AVCodecContext *video_enc;
  1576. char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
  1577. ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index);
  1578. st = ost->st;
  1579. video_enc = ost->enc_ctx;
  1580. MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
  1581. if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
  1582. av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
  1583. exit_program(1);
  1584. }
  1585. MATCH_PER_STREAM_OPT(max_frame_rates, str, max_frame_rate, oc, st);
  1586. if (max_frame_rate && av_parse_video_rate(&ost->max_frame_rate, max_frame_rate) < 0) {
  1587. av_log(NULL, AV_LOG_FATAL, "Invalid maximum framerate value: %s\n", max_frame_rate);
  1588. exit_program(1);
  1589. }
  1590. if (frame_rate && max_frame_rate) {
  1591. av_log(NULL, AV_LOG_ERROR, "Only one of -fpsmax and -r can be set for a stream.\n");
  1592. exit_program(1);
  1593. }
  1594. if ((frame_rate || max_frame_rate) &&
  1595. video_sync_method == VSYNC_PASSTHROUGH)
  1596. av_log(NULL, AV_LOG_ERROR, "Using -vsync passthrough and -r/-fpsmax can produce invalid output files\n");
  1597. MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
  1598. if (frame_aspect_ratio) {
  1599. AVRational q;
  1600. if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
  1601. q.num <= 0 || q.den <= 0) {
  1602. av_log(NULL, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
  1603. exit_program(1);
  1604. }
  1605. ost->frame_aspect_ratio = q;
  1606. }
  1607. MATCH_PER_STREAM_OPT(filter_scripts, str, ost->filters_script, oc, st);
  1608. MATCH_PER_STREAM_OPT(filters, str, ost->filters, oc, st);
  1609. if (!ost->stream_copy) {
  1610. const char *p = NULL;
  1611. char *frame_size = NULL;
  1612. char *frame_pix_fmt = NULL;
  1613. char *intra_matrix = NULL, *inter_matrix = NULL;
  1614. char *chroma_intra_matrix = NULL;
  1615. int do_pass = 0;
  1616. int i;
  1617. MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
  1618. if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
  1619. av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
  1620. exit_program(1);
  1621. }
  1622. MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
  1623. if (frame_pix_fmt && *frame_pix_fmt == '+') {
  1624. ost->keep_pix_fmt = 1;
  1625. if (!*++frame_pix_fmt)
  1626. frame_pix_fmt = NULL;
  1627. }
  1628. if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) {
  1629. av_log(NULL, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", frame_pix_fmt);
  1630. exit_program(1);
  1631. }
  1632. st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
  1633. MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
  1634. if (intra_matrix) {
  1635. if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
  1636. av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
  1637. exit_program(1);
  1638. }
  1639. parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
  1640. }
  1641. MATCH_PER_STREAM_OPT(chroma_intra_matrices, str, chroma_intra_matrix, oc, st);
  1642. if (chroma_intra_matrix) {
  1643. uint16_t *p = av_mallocz(sizeof(*video_enc->chroma_intra_matrix) * 64);
  1644. if (!p) {
  1645. av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
  1646. exit_program(1);
  1647. }
  1648. video_enc->chroma_intra_matrix = p;
  1649. parse_matrix_coeffs(p, chroma_intra_matrix);
  1650. }
  1651. MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
  1652. if (inter_matrix) {
  1653. if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
  1654. av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
  1655. exit_program(1);
  1656. }
  1657. parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
  1658. }
  1659. MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
  1660. for (i = 0; p; i++) {
  1661. int start, end, q;
  1662. int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
  1663. if (e != 3) {
  1664. av_log(NULL, AV_LOG_FATAL, "error parsing rc_override\n");
  1665. exit_program(1);
  1666. }
  1667. video_enc->rc_override =
  1668. av_realloc_array(video_enc->rc_override,
  1669. i + 1, sizeof(RcOverride));
  1670. if (!video_enc->rc_override) {
  1671. av_log(NULL, AV_LOG_FATAL, "Could not (re)allocate memory for rc_override.\n");
  1672. exit_program(1);
  1673. }
  1674. video_enc->rc_override[i].start_frame = start;
  1675. video_enc->rc_override[i].end_frame = end;
  1676. if (q > 0) {
  1677. video_enc->rc_override[i].qscale = q;
  1678. video_enc->rc_override[i].quality_factor = 1.0;
  1679. }
  1680. else {
  1681. video_enc->rc_override[i].qscale = 0;
  1682. video_enc->rc_override[i].quality_factor = -q/100.0;
  1683. }
  1684. p = strchr(p, '/');
  1685. if (p) p++;
  1686. }
  1687. video_enc->rc_override_count = i;
  1688. if (do_psnr)
  1689. video_enc->flags|= AV_CODEC_FLAG_PSNR;
  1690. /* two pass mode */
  1691. MATCH_PER_STREAM_OPT(pass, i, do_pass, oc, st);
  1692. if (do_pass) {
  1693. if (do_pass & 1) {
  1694. video_enc->flags |= AV_CODEC_FLAG_PASS1;
  1695. av_dict_set(&ost->encoder_opts, "flags", "+pass1", AV_DICT_APPEND);
  1696. }
  1697. if (do_pass & 2) {
  1698. video_enc->flags |= AV_CODEC_FLAG_PASS2;
  1699. av_dict_set(&ost->encoder_opts, "flags", "+pass2", AV_DICT_APPEND);
  1700. }
  1701. }
  1702. MATCH_PER_STREAM_OPT(passlogfiles, str, ost->logfile_prefix, oc, st);
  1703. if (ost->logfile_prefix &&
  1704. !(ost->logfile_prefix = av_strdup(ost->logfile_prefix)))
  1705. exit_program(1);
  1706. if (do_pass) {
  1707. char logfilename[1024];
  1708. FILE *f;
  1709. snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
  1710. ost->logfile_prefix ? ost->logfile_prefix :
  1711. DEFAULT_PASS_LOGFILENAME_PREFIX,
  1712. nb_output_streams - 1);
  1713. if (!strcmp(ost->enc->name, "libx264")) {
  1714. av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
  1715. } else {
  1716. if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
  1717. char *logbuffer = read_file(logfilename);
  1718. if (!logbuffer) {
  1719. av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
  1720. logfilename);
  1721. exit_program(1);
  1722. }
  1723. video_enc->stats_in = logbuffer;
  1724. }
  1725. if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
  1726. f = fopen_utf8(logfilename, "wb");
  1727. if (!f) {
  1728. av_log(NULL, AV_LOG_FATAL,
  1729. "Cannot write log file '%s' for pass-1 encoding: %s\n",
  1730. logfilename, strerror(errno));
  1731. exit_program(1);
  1732. }
  1733. ost->logfile = f;
  1734. }
  1735. }
  1736. }
  1737. MATCH_PER_STREAM_OPT(forced_key_frames, str, ost->forced_keyframes, oc, st);
  1738. if (ost->forced_keyframes)
  1739. ost->forced_keyframes = av_strdup(ost->forced_keyframes);
  1740. MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
  1741. ost->top_field_first = -1;
  1742. MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
  1743. ost->vsync_method = video_sync_method;
  1744. MATCH_PER_STREAM_OPT(fps_mode, str, ost->fps_mode, oc, st);
  1745. if (ost->fps_mode)
  1746. parse_and_set_vsync(ost->fps_mode, &ost->vsync_method, ost->file_index, ost->index, 0);
  1747. if (ost->vsync_method == VSYNC_AUTO) {
  1748. if (!strcmp(oc->oformat->name, "avi")) {
  1749. ost->vsync_method = VSYNC_VFR;
  1750. } else {
  1751. ost->vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ?
  1752. ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ?
  1753. VSYNC_PASSTHROUGH : VSYNC_VFR) :
  1754. VSYNC_CFR;
  1755. }
  1756. if (ost->source_index >= 0 && ost->vsync_method == VSYNC_CFR) {
  1757. const InputStream *ist = input_streams[ost->source_index];
  1758. const InputFile *ifile = input_files[ist->file_index];
  1759. if (ifile->nb_streams == 1 && ifile->input_ts_offset == 0)
  1760. ost->vsync_method = VSYNC_VSCFR;
  1761. }
  1762. if (ost->vsync_method == VSYNC_CFR && copy_ts) {
  1763. ost->vsync_method = VSYNC_VSCFR;
  1764. }
  1765. }
  1766. ost->is_cfr = (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR);
  1767. ost->avfilter = get_ost_filters(o, oc, ost);
  1768. if (!ost->avfilter)
  1769. exit_program(1);
  1770. ost->last_frame = av_frame_alloc();
  1771. if (!ost->last_frame)
  1772. exit_program(1);
  1773. }
  1774. if (ost->stream_copy)
  1775. check_streamcopy_filters(o, oc, ost, AVMEDIA_TYPE_VIDEO);
  1776. return ost;
  1777. }
  1778. static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc, int source_index)
  1779. {
  1780. int n;
  1781. AVStream *st;
  1782. OutputStream *ost;
  1783. AVCodecContext *audio_enc;
  1784. ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO, source_index);
  1785. st = ost->st;
  1786. audio_enc = ost->enc_ctx;
  1787. audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
  1788. MATCH_PER_STREAM_OPT(filter_scripts, str, ost->filters_script, oc, st);
  1789. MATCH_PER_STREAM_OPT(filters, str, ost->filters, oc, st);
  1790. if (!ost->stream_copy) {
  1791. int channels = 0;
  1792. char *layout = NULL;
  1793. char *sample_fmt = NULL;
  1794. MATCH_PER_STREAM_OPT(audio_channels, i, channels, oc, st);
  1795. if (channels) {
  1796. audio_enc->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  1797. audio_enc->ch_layout.nb_channels = channels;
  1798. }
  1799. MATCH_PER_STREAM_OPT(audio_ch_layouts, str, layout, oc, st);
  1800. if (layout) {
  1801. if (av_channel_layout_from_string(&audio_enc->ch_layout, layout) < 0) {
  1802. #if FF_API_OLD_CHANNEL_LAYOUT
  1803. uint64_t mask;
  1804. AV_NOWARN_DEPRECATED({
  1805. mask = av_get_channel_layout(layout);
  1806. })
  1807. if (!mask) {
  1808. #endif
  1809. av_log(NULL, AV_LOG_FATAL, "Unknown channel layout: %s\n", layout);
  1810. exit_program(1);
  1811. #if FF_API_OLD_CHANNEL_LAYOUT
  1812. }
  1813. av_log(NULL, AV_LOG_WARNING, "Channel layout '%s' uses a deprecated syntax.\n",
  1814. layout);
  1815. av_channel_layout_from_mask(&audio_enc->ch_layout, mask);
  1816. #endif
  1817. }
  1818. }
  1819. MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
  1820. if (sample_fmt &&
  1821. (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
  1822. av_log(NULL, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
  1823. exit_program(1);
  1824. }
  1825. MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
  1826. MATCH_PER_STREAM_OPT(apad, str, ost->apad, oc, st);
  1827. ost->apad = av_strdup(ost->apad);
  1828. ost->avfilter = get_ost_filters(o, oc, ost);
  1829. if (!ost->avfilter)
  1830. exit_program(1);
  1831. /* check for channel mapping for this audio stream */
  1832. for (n = 0; n < o->nb_audio_channel_maps; n++) {
  1833. AudioChannelMap *map = &o->audio_channel_maps[n];
  1834. if ((map->ofile_idx == -1 || ost->file_index == map->ofile_idx) &&
  1835. (map->ostream_idx == -1 || ost->st->index == map->ostream_idx)) {
  1836. InputStream *ist;
  1837. if (map->channel_idx == -1) {
  1838. ist = NULL;
  1839. } else if (ost->source_index < 0) {
  1840. av_log(NULL, AV_LOG_FATAL, "Cannot determine input stream for channel mapping %d.%d\n",
  1841. ost->file_index, ost->st->index);
  1842. continue;
  1843. } else {
  1844. ist = input_streams[ost->source_index];
  1845. }
  1846. if (!ist || (ist->file_index == map->file_idx && ist->st->index == map->stream_idx)) {
  1847. if (av_reallocp_array(&ost->audio_channels_map,
  1848. ost->audio_channels_mapped + 1,
  1849. sizeof(*ost->audio_channels_map)
  1850. ) < 0 )
  1851. exit_program(1);
  1852. ost->audio_channels_map[ost->audio_channels_mapped++] = map->channel_idx;
  1853. }
  1854. }
  1855. }
  1856. }
  1857. if (ost->stream_copy)
  1858. check_streamcopy_filters(o, oc, ost, AVMEDIA_TYPE_AUDIO);
  1859. return ost;
  1860. }
  1861. static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc, int source_index)
  1862. {
  1863. OutputStream *ost;
  1864. ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA, source_index);
  1865. if (!ost->stream_copy) {
  1866. av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
  1867. exit_program(1);
  1868. }
  1869. return ost;
  1870. }
  1871. static OutputStream *new_unknown_stream(OptionsContext *o, AVFormatContext *oc, int source_index)
  1872. {
  1873. OutputStream *ost;
  1874. ost = new_output_stream(o, oc, AVMEDIA_TYPE_UNKNOWN, source_index);
  1875. if (!ost->stream_copy) {
  1876. av_log(NULL, AV_LOG_FATAL, "Unknown stream encoding not supported yet (only streamcopy)\n");
  1877. exit_program(1);
  1878. }
  1879. return ost;
  1880. }
  1881. static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc, int source_index)
  1882. {
  1883. OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT, source_index);
  1884. ost->stream_copy = 1;
  1885. ost->finished = 1;
  1886. return ost;
  1887. }
  1888. static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc, int source_index)
  1889. {
  1890. AVStream *st;
  1891. OutputStream *ost;
  1892. AVCodecContext *subtitle_enc;
  1893. ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE, source_index);
  1894. st = ost->st;
  1895. subtitle_enc = ost->enc_ctx;
  1896. subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1897. if (!ost->stream_copy) {
  1898. char *frame_size = NULL;
  1899. MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
  1900. if (frame_size && av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size) < 0) {
  1901. av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
  1902. exit_program(1);
  1903. }
  1904. }
  1905. return ost;
  1906. }
  1907. /* arg format is "output-stream-index:streamid-value". */
  1908. static int opt_streamid(void *optctx, const char *opt, const char *arg)
  1909. {
  1910. OptionsContext *o = optctx;
  1911. int idx;
  1912. char *p;
  1913. char idx_str[16];
  1914. av_strlcpy(idx_str, arg, sizeof(idx_str));
  1915. p = strchr(idx_str, ':');
  1916. if (!p) {
  1917. av_log(NULL, AV_LOG_FATAL,
  1918. "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
  1919. arg, opt);
  1920. exit_program(1);
  1921. }
  1922. *p++ = '\0';
  1923. idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);
  1924. o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
  1925. o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
  1926. return 0;
  1927. }
  1928. static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os,
  1929. int copy_metadata)
  1930. {
  1931. AVFormatContext *is = ifile->ctx;
  1932. AVChapter **tmp;
  1933. int i;
  1934. tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters, sizeof(*os->chapters));
  1935. if (!tmp)
  1936. return AVERROR(ENOMEM);
  1937. os->chapters = tmp;
  1938. for (i = 0; i < is->nb_chapters; i++) {
  1939. AVChapter *in_ch = is->chapters[i], *out_ch;
  1940. int64_t start_time = (ofile->start_time == AV_NOPTS_VALUE) ? 0 : ofile->start_time;
  1941. int64_t ts_off = av_rescale_q(start_time - ifile->ts_offset,
  1942. AV_TIME_BASE_Q, in_ch->time_base);
  1943. int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
  1944. av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
  1945. if (in_ch->end < ts_off)
  1946. continue;
  1947. if (rt != INT64_MAX && in_ch->start > rt + ts_off)
  1948. break;
  1949. out_ch = av_mallocz(sizeof(AVChapter));
  1950. if (!out_ch)
  1951. return AVERROR(ENOMEM);
  1952. out_ch->id = in_ch->id;
  1953. out_ch->time_base = in_ch->time_base;
  1954. out_ch->start = FFMAX(0, in_ch->start - ts_off);
  1955. out_ch->end = FFMIN(rt, in_ch->end - ts_off);
  1956. if (copy_metadata)
  1957. av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
  1958. os->chapters[os->nb_chapters++] = out_ch;
  1959. }
  1960. return 0;
  1961. }
  1962. static int set_dispositions(OutputFile *of, AVFormatContext *ctx)
  1963. {
  1964. int nb_streams[AVMEDIA_TYPE_NB] = { 0 };
  1965. int have_default[AVMEDIA_TYPE_NB] = { 0 };
  1966. int have_manual = 0;
  1967. // first, copy the input dispositions
  1968. for (int i = 0; i < ctx->nb_streams; i++) {
  1969. OutputStream *ost = output_streams[of->ost_index + i];
  1970. nb_streams[ost->st->codecpar->codec_type]++;
  1971. have_manual |= !!ost->disposition;
  1972. if (ost->source_index >= 0) {
  1973. ost->st->disposition = input_streams[ost->source_index]->st->disposition;
  1974. if (ost->st->disposition & AV_DISPOSITION_DEFAULT)
  1975. have_default[ost->st->codecpar->codec_type] = 1;
  1976. }
  1977. }
  1978. if (have_manual) {
  1979. // process manually set dispositions - they override the above copy
  1980. for (int i = 0; i < ctx->nb_streams; i++) {
  1981. OutputStream *ost = output_streams[of->ost_index + i];
  1982. int ret;
  1983. if (!ost->disposition)
  1984. continue;
  1985. #if LIBAVFORMAT_VERSION_MAJOR >= 60
  1986. ret = av_opt_set(ost->st, "disposition", ost->disposition, 0);
  1987. #else
  1988. {
  1989. const AVClass *class = av_stream_get_class();
  1990. const AVOption *o = av_opt_find(&class, "disposition", NULL, 0, AV_OPT_SEARCH_FAKE_OBJ);
  1991. av_assert0(o);
  1992. ret = av_opt_eval_flags(&class, o, ost->disposition, &ost->st->disposition);
  1993. }
  1994. #endif
  1995. if (ret < 0)
  1996. return ret;
  1997. }
  1998. } else {
  1999. // For each media type with more than one stream, find a suitable stream to
  2000. // mark as default, unless one is already marked default.
  2001. // "Suitable" means the first of that type, skipping attached pictures.
  2002. for (int i = 0; i < ctx->nb_streams; i++) {
  2003. OutputStream *ost = output_streams[of->ost_index + i];
  2004. enum AVMediaType type = ost->st->codecpar->codec_type;
  2005. if (nb_streams[type] < 2 || have_default[type] ||
  2006. ost->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
  2007. continue;
  2008. ost->st->disposition |= AV_DISPOSITION_DEFAULT;
  2009. have_default[type] = 1;
  2010. }
  2011. }
  2012. return 0;
  2013. }
  2014. static void init_output_filter(OutputFilter *ofilter, OptionsContext *o,
  2015. AVFormatContext *oc)
  2016. {
  2017. OutputStream *ost;
  2018. switch (ofilter->type) {
  2019. case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc, -1); break;
  2020. case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc, -1); break;
  2021. default:
  2022. av_log(NULL, AV_LOG_FATAL, "Only video and audio filters are supported "
  2023. "currently.\n");
  2024. exit_program(1);
  2025. }
  2026. ost->filter = ofilter;
  2027. ofilter->ost = ost;
  2028. ofilter->format = -1;
  2029. if (ost->stream_copy) {
  2030. av_log(NULL, AV_LOG_ERROR, "Streamcopy requested for output stream %d:%d, "
  2031. "which is fed from a complex filtergraph. Filtering and streamcopy "
  2032. "cannot be used together.\n", ost->file_index, ost->index);
  2033. exit_program(1);
  2034. }
  2035. if (ost->avfilter && (ost->filters || ost->filters_script)) {
  2036. const char *opt = ost->filters ? "-vf/-af/-filter" : "-filter_script";
  2037. av_log(NULL, AV_LOG_ERROR,
  2038. "%s '%s' was specified through the %s option "
  2039. "for output stream %d:%d, which is fed from a complex filtergraph.\n"
  2040. "%s and -filter_complex cannot be used together for the same stream.\n",
  2041. ost->filters ? "Filtergraph" : "Filtergraph script",
  2042. ost->filters ? ost->filters : ost->filters_script,
  2043. opt, ost->file_index, ost->index, opt);
  2044. exit_program(1);
  2045. }
  2046. avfilter_inout_free(&ofilter->out_tmp);
  2047. }
  2048. static int init_complex_filters(void)
  2049. {
  2050. int i, ret = 0;
  2051. for (i = 0; i < nb_filtergraphs; i++) {
  2052. ret = init_complex_filtergraph(filtergraphs[i]);
  2053. if (ret < 0)
  2054. return ret;
  2055. }
  2056. return 0;
  2057. }
  2058. static void set_channel_layout(OutputFilter *f, OutputStream *ost)
  2059. {
  2060. int i, err;
  2061. if (ost->enc_ctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
  2062. /* Pass the layout through for all orders but UNSPEC */
  2063. err = av_channel_layout_copy(&f->ch_layout, &ost->enc_ctx->ch_layout);
  2064. if (err < 0)
  2065. exit_program(1);
  2066. return;
  2067. }
  2068. /* Requested layout is of order UNSPEC */
  2069. if (!ost->enc->ch_layouts) {
  2070. /* Use the default native layout for the requested amount of channels when the
  2071. encoder doesn't have a list of supported layouts */
  2072. av_channel_layout_default(&f->ch_layout, ost->enc_ctx->ch_layout.nb_channels);
  2073. return;
  2074. }
  2075. /* Encoder has a list of supported layouts. Pick the first layout in it with the
  2076. same amount of channels as the requested layout */
  2077. for (i = 0; ost->enc->ch_layouts[i].nb_channels; i++) {
  2078. if (ost->enc->ch_layouts[i].nb_channels == ost->enc_ctx->ch_layout.nb_channels)
  2079. break;
  2080. }
  2081. if (ost->enc->ch_layouts[i].nb_channels) {
  2082. /* Use it if one is found */
  2083. err = av_channel_layout_copy(&f->ch_layout, &ost->enc->ch_layouts[i]);
  2084. if (err < 0)
  2085. exit_program(1);
  2086. return;
  2087. }
  2088. /* If no layout for the amount of channels requested was found, use the default
  2089. native layout for it. */
  2090. av_channel_layout_default(&f->ch_layout, ost->enc_ctx->ch_layout.nb_channels);
  2091. }
  2092. static int open_output_file(OptionsContext *o, const char *filename)
  2093. {
  2094. AVFormatContext *oc;
  2095. int i, j, err;
  2096. OutputFile *of;
  2097. OutputStream *ost;
  2098. InputStream *ist;
  2099. AVDictionary *unused_opts = NULL;
  2100. const AVDictionaryEntry *e = NULL;
  2101. if (o->stop_time != INT64_MAX && o->recording_time != INT64_MAX) {
  2102. o->stop_time = INT64_MAX;
  2103. av_log(NULL, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
  2104. }
  2105. if (o->stop_time != INT64_MAX && o->recording_time == INT64_MAX) {
  2106. int64_t start_time = o->start_time == AV_NOPTS_VALUE ? 0 : o->start_time;
  2107. if (o->stop_time <= start_time) {
  2108. av_log(NULL, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
  2109. exit_program(1);
  2110. } else {
  2111. o->recording_time = o->stop_time - start_time;
  2112. }
  2113. }
  2114. of = ALLOC_ARRAY_ELEM(output_files, nb_output_files);
  2115. of->index = nb_output_files - 1;
  2116. of->ost_index = nb_output_streams;
  2117. of->recording_time = o->recording_time;
  2118. of->start_time = o->start_time;
  2119. of->limit_filesize = o->limit_filesize;
  2120. of->shortest = o->shortest;
  2121. av_dict_copy(&of->opts, o->g->format_opts, 0);
  2122. if (!strcmp(filename, "-"))
  2123. filename = "pipe:";
  2124. err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);
  2125. if (!oc) {
  2126. print_error(filename, err);
  2127. exit_program(1);
  2128. }
  2129. of->ctx = oc;
  2130. of->format = oc->oformat;
  2131. if (o->recording_time != INT64_MAX)
  2132. oc->duration = o->recording_time;
  2133. oc->interrupt_callback = int_cb;
  2134. if (o->bitexact) {
  2135. oc->flags |= AVFMT_FLAG_BITEXACT;
  2136. }
  2137. /* create streams for all unlabeled output pads */
  2138. for (i = 0; i < nb_filtergraphs; i++) {
  2139. FilterGraph *fg = filtergraphs[i];
  2140. for (j = 0; j < fg->nb_outputs; j++) {
  2141. OutputFilter *ofilter = fg->outputs[j];
  2142. if (!ofilter->out_tmp || ofilter->out_tmp->name)
  2143. continue;
  2144. switch (ofilter->type) {
  2145. case AVMEDIA_TYPE_VIDEO: o->video_disable = 1; break;
  2146. case AVMEDIA_TYPE_AUDIO: o->audio_disable = 1; break;
  2147. case AVMEDIA_TYPE_SUBTITLE: o->subtitle_disable = 1; break;
  2148. }
  2149. init_output_filter(ofilter, o, oc);
  2150. }
  2151. }
  2152. if (!o->nb_stream_maps) {
  2153. char *subtitle_codec_name = NULL;
  2154. /* pick the "best" stream of each type */
  2155. /* video: highest resolution */
  2156. if (!o->video_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_VIDEO) != AV_CODEC_ID_NONE) {
  2157. int best_score = 0, idx = -1;
  2158. int qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
  2159. for (j = 0; j < nb_input_files; j++) {
  2160. InputFile *ifile = input_files[j];
  2161. int file_best_score = 0, file_best_idx = -1;
  2162. for (i = 0; i < ifile->nb_streams; i++) {
  2163. int score;
  2164. ist = input_streams[ifile->ist_index + i];
  2165. score = ist->st->codecpar->width * ist->st->codecpar->height
  2166. + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
  2167. + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
  2168. if (ist->user_set_discard == AVDISCARD_ALL)
  2169. continue;
  2170. if((qcr!=MKTAG('A', 'P', 'I', 'C')) && (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  2171. score = 1;
  2172. if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  2173. score > file_best_score) {
  2174. if((qcr==MKTAG('A', 'P', 'I', 'C')) && !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  2175. continue;
  2176. file_best_score = score;
  2177. file_best_idx = ifile->ist_index + i;
  2178. }
  2179. }
  2180. if (file_best_idx >= 0) {
  2181. if((qcr == MKTAG('A', 'P', 'I', 'C')) || !(ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  2182. file_best_score -= 5000000*!!(input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT);
  2183. if (file_best_score > best_score) {
  2184. best_score = file_best_score;
  2185. idx = file_best_idx;
  2186. }
  2187. }
  2188. }
  2189. if (idx >= 0)
  2190. new_video_stream(o, oc, idx);
  2191. }
  2192. /* audio: most channels */
  2193. if (!o->audio_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_AUDIO) != AV_CODEC_ID_NONE) {
  2194. int best_score = 0, idx = -1;
  2195. for (j = 0; j < nb_input_files; j++) {
  2196. InputFile *ifile = input_files[j];
  2197. int file_best_score = 0, file_best_idx = -1;
  2198. for (i = 0; i < ifile->nb_streams; i++) {
  2199. int score;
  2200. ist = input_streams[ifile->ist_index + i];
  2201. score = ist->st->codecpar->ch_layout.nb_channels
  2202. + 100000000 * !!(ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
  2203. + 5000000*!!(ist->st->disposition & AV_DISPOSITION_DEFAULT);
  2204. if (ist->user_set_discard == AVDISCARD_ALL)
  2205. continue;
  2206. if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  2207. score > file_best_score) {
  2208. file_best_score = score;
  2209. file_best_idx = ifile->ist_index + i;
  2210. }
  2211. }
  2212. if (file_best_idx >= 0) {
  2213. file_best_score -= 5000000*!!(input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT);
  2214. if (file_best_score > best_score) {
  2215. best_score = file_best_score;
  2216. idx = file_best_idx;
  2217. }
  2218. }
  2219. }
  2220. if (idx >= 0)
  2221. new_audio_stream(o, oc, idx);
  2222. }
  2223. /* subtitles: pick first */
  2224. MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, oc, "s");
  2225. if (!o->subtitle_disable && (avcodec_find_encoder(oc->oformat->subtitle_codec) || subtitle_codec_name)) {
  2226. for (i = 0; i < nb_input_streams; i++)
  2227. if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  2228. AVCodecDescriptor const *input_descriptor =
  2229. avcodec_descriptor_get(input_streams[i]->st->codecpar->codec_id);
  2230. AVCodecDescriptor const *output_descriptor = NULL;
  2231. AVCodec const *output_codec =
  2232. avcodec_find_encoder(oc->oformat->subtitle_codec);
  2233. int input_props = 0, output_props = 0;
  2234. if (input_streams[i]->user_set_discard == AVDISCARD_ALL)
  2235. continue;
  2236. if (output_codec)
  2237. output_descriptor = avcodec_descriptor_get(output_codec->id);
  2238. if (input_descriptor)
  2239. input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
  2240. if (output_descriptor)
  2241. output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
  2242. if (subtitle_codec_name ||
  2243. input_props & output_props ||
  2244. // Map dvb teletext which has neither property to any output subtitle encoder
  2245. input_descriptor && output_descriptor &&
  2246. (!input_descriptor->props ||
  2247. !output_descriptor->props)) {
  2248. new_subtitle_stream(o, oc, i);
  2249. break;
  2250. }
  2251. }
  2252. }
  2253. /* Data only if codec id match */
  2254. if (!o->data_disable ) {
  2255. enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_DATA);
  2256. for (i = 0; codec_id != AV_CODEC_ID_NONE && i < nb_input_streams; i++) {
  2257. if (input_streams[i]->user_set_discard == AVDISCARD_ALL)
  2258. continue;
  2259. if (input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_DATA
  2260. && input_streams[i]->st->codecpar->codec_id == codec_id )
  2261. new_data_stream(o, oc, i);
  2262. }
  2263. }
  2264. } else {
  2265. for (i = 0; i < o->nb_stream_maps; i++) {
  2266. StreamMap *map = &o->stream_maps[i];
  2267. if (map->disabled)
  2268. continue;
  2269. if (map->linklabel) {
  2270. FilterGraph *fg;
  2271. OutputFilter *ofilter = NULL;
  2272. int j, k;
  2273. for (j = 0; j < nb_filtergraphs; j++) {
  2274. fg = filtergraphs[j];
  2275. for (k = 0; k < fg->nb_outputs; k++) {
  2276. AVFilterInOut *out = fg->outputs[k]->out_tmp;
  2277. if (out && !strcmp(out->name, map->linklabel)) {
  2278. ofilter = fg->outputs[k];
  2279. goto loop_end;
  2280. }
  2281. }
  2282. }
  2283. loop_end:
  2284. if (!ofilter) {
  2285. av_log(NULL, AV_LOG_FATAL, "Output with label '%s' does not exist "
  2286. "in any defined filter graph, or was already used elsewhere.\n", map->linklabel);
  2287. exit_program(1);
  2288. }
  2289. init_output_filter(ofilter, o, oc);
  2290. } else {
  2291. int src_idx = input_files[map->file_index]->ist_index + map->stream_index;
  2292. ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
  2293. if (ist->user_set_discard == AVDISCARD_ALL) {
  2294. av_log(NULL, AV_LOG_FATAL, "Stream #%d:%d is disabled and cannot be mapped.\n",
  2295. map->file_index, map->stream_index);
  2296. exit_program(1);
  2297. }
  2298. if(o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
  2299. continue;
  2300. if(o-> audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  2301. continue;
  2302. if(o-> video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  2303. continue;
  2304. if(o-> data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
  2305. continue;
  2306. ost = NULL;
  2307. switch (ist->st->codecpar->codec_type) {
  2308. case AVMEDIA_TYPE_VIDEO: ost = new_video_stream (o, oc, src_idx); break;
  2309. case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream (o, oc, src_idx); break;
  2310. case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream (o, oc, src_idx); break;
  2311. case AVMEDIA_TYPE_DATA: ost = new_data_stream (o, oc, src_idx); break;
  2312. case AVMEDIA_TYPE_ATTACHMENT: ost = new_attachment_stream(o, oc, src_idx); break;
  2313. case AVMEDIA_TYPE_UNKNOWN:
  2314. if (copy_unknown_streams) {
  2315. ost = new_unknown_stream (o, oc, src_idx);
  2316. break;
  2317. }
  2318. default:
  2319. av_log(NULL, ignore_unknown_streams ? AV_LOG_WARNING : AV_LOG_FATAL,
  2320. "Cannot map stream #%d:%d - unsupported type.\n",
  2321. map->file_index, map->stream_index);
  2322. if (!ignore_unknown_streams) {
  2323. av_log(NULL, AV_LOG_FATAL,
  2324. "If you want unsupported types ignored instead "
  2325. "of failing, please use the -ignore_unknown option\n"
  2326. "If you want them copied, please use -copy_unknown\n");
  2327. exit_program(1);
  2328. }
  2329. }
  2330. if (ost)
  2331. ost->sync_ist = input_streams[ input_files[map->sync_file_index]->ist_index
  2332. + map->sync_stream_index];
  2333. }
  2334. }
  2335. }
  2336. /* handle attached files */
  2337. for (i = 0; i < o->nb_attachments; i++) {
  2338. AVIOContext *pb;
  2339. uint8_t *attachment;
  2340. const char *p;
  2341. int64_t len;
  2342. if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
  2343. av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
  2344. o->attachments[i]);
  2345. exit_program(1);
  2346. }
  2347. if ((len = avio_size(pb)) <= 0) {
  2348. av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
  2349. o->attachments[i]);
  2350. exit_program(1);
  2351. }
  2352. if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
  2353. !(attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
  2354. av_log(NULL, AV_LOG_FATAL, "Attachment %s too large.\n",
  2355. o->attachments[i]);
  2356. exit_program(1);
  2357. }
  2358. avio_read(pb, attachment, len);
  2359. memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  2360. ost = new_attachment_stream(o, oc, -1);
  2361. ost->stream_copy = 0;
  2362. ost->attachment_filename = o->attachments[i];
  2363. ost->st->codecpar->extradata = attachment;
  2364. ost->st->codecpar->extradata_size = len;
  2365. p = strrchr(o->attachments[i], '/');
  2366. av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
  2367. avio_closep(&pb);
  2368. }
  2369. if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
  2370. av_dump_format(oc, nb_output_files - 1, oc->url, 1);
  2371. av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", nb_output_files - 1);
  2372. exit_program(1);
  2373. }
  2374. /* check if all codec options have been used */
  2375. unused_opts = strip_specifiers(o->g->codec_opts);
  2376. for (i = of->ost_index; i < nb_output_streams; i++) {
  2377. e = NULL;
  2378. while ((e = av_dict_get(output_streams[i]->encoder_opts, "", e,
  2379. AV_DICT_IGNORE_SUFFIX)))
  2380. av_dict_set(&unused_opts, e->key, NULL, 0);
  2381. }
  2382. e = NULL;
  2383. while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
  2384. const AVClass *class = avcodec_get_class();
  2385. const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
  2386. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  2387. const AVClass *fclass = avformat_get_class();
  2388. const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
  2389. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  2390. if (!option || foption)
  2391. continue;
  2392. if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
  2393. av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
  2394. "output file #%d (%s) is not an encoding option.\n", e->key,
  2395. option->help ? option->help : "", nb_output_files - 1,
  2396. filename);
  2397. exit_program(1);
  2398. }
  2399. // gop_timecode is injected by generic code but not always used
  2400. if (!strcmp(e->key, "gop_timecode"))
  2401. continue;
  2402. av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
  2403. "output file #%d (%s) has not been used for any stream. The most "
  2404. "likely reason is either wrong type (e.g. a video option with "
  2405. "no video streams) or that it is a private option of some encoder "
  2406. "which was not actually used for any stream.\n", e->key,
  2407. option->help ? option->help : "", nb_output_files - 1, filename);
  2408. }
  2409. av_dict_free(&unused_opts);
  2410. /* set the decoding_needed flags and create simple filtergraphs */
  2411. for (i = of->ost_index; i < nb_output_streams; i++) {
  2412. OutputStream *ost = output_streams[i];
  2413. if (ost->encoding_needed && ost->source_index >= 0) {
  2414. InputStream *ist = input_streams[ost->source_index];
  2415. ist->decoding_needed |= DECODING_FOR_OST;
  2416. ist->processing_needed = 1;
  2417. if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
  2418. ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  2419. err = init_simple_filtergraph(ist, ost);
  2420. if (err < 0) {
  2421. av_log(NULL, AV_LOG_ERROR,
  2422. "Error initializing a simple filtergraph between streams "
  2423. "%d:%d->%d:%d\n", ist->file_index, ost->source_index,
  2424. nb_output_files - 1, ost->st->index);
  2425. exit_program(1);
  2426. }
  2427. }
  2428. } else if (ost->stream_copy && ost->source_index >= 0) {
  2429. InputStream *ist = input_streams[ost->source_index];
  2430. ist->processing_needed = 1;
  2431. }
  2432. /* set the filter output constraints */
  2433. if (ost->filter) {
  2434. OutputFilter *f = ost->filter;
  2435. switch (ost->enc_ctx->codec_type) {
  2436. case AVMEDIA_TYPE_VIDEO:
  2437. f->frame_rate = ost->frame_rate;
  2438. f->width = ost->enc_ctx->width;
  2439. f->height = ost->enc_ctx->height;
  2440. if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
  2441. f->format = ost->enc_ctx->pix_fmt;
  2442. } else {
  2443. f->formats = ost->enc->pix_fmts;
  2444. }
  2445. break;
  2446. case AVMEDIA_TYPE_AUDIO:
  2447. if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2448. f->format = ost->enc_ctx->sample_fmt;
  2449. } else {
  2450. f->formats = ost->enc->sample_fmts;
  2451. }
  2452. if (ost->enc_ctx->sample_rate) {
  2453. f->sample_rate = ost->enc_ctx->sample_rate;
  2454. } else {
  2455. f->sample_rates = ost->enc->supported_samplerates;
  2456. }
  2457. if (ost->enc_ctx->ch_layout.nb_channels) {
  2458. set_channel_layout(f, ost);
  2459. } else if (ost->enc->ch_layouts) {
  2460. f->ch_layouts = ost->enc->ch_layouts;
  2461. }
  2462. break;
  2463. }
  2464. }
  2465. }
  2466. /* check filename in case of an image number is expected */
  2467. if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
  2468. if (!av_filename_number_test(oc->url)) {
  2469. print_error(oc->url, AVERROR(EINVAL));
  2470. exit_program(1);
  2471. }
  2472. }
  2473. if (!(oc->oformat->flags & AVFMT_NOSTREAMS) && !input_stream_potentially_available) {
  2474. av_log(NULL, AV_LOG_ERROR,
  2475. "No input streams but output needs an input stream\n");
  2476. exit_program(1);
  2477. }
  2478. if (!(oc->oformat->flags & AVFMT_NOFILE)) {
  2479. /* test if it already exists to avoid losing precious files */
  2480. assert_file_overwrite(filename);
  2481. /* open the file */
  2482. if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
  2483. &oc->interrupt_callback,
  2484. &of->opts)) < 0) {
  2485. print_error(filename, err);
  2486. exit_program(1);
  2487. }
  2488. } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename))
  2489. assert_file_overwrite(filename);
  2490. if (o->mux_preload) {
  2491. av_dict_set_int(&of->opts, "preload", o->mux_preload*AV_TIME_BASE, 0);
  2492. }
  2493. oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
  2494. /* copy metadata */
  2495. for (i = 0; i < o->nb_metadata_map; i++) {
  2496. char *p;
  2497. int in_file_index = strtol(o->metadata_map[i].u.str, &p, 0);
  2498. if (in_file_index >= nb_input_files) {
  2499. av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d while processing metadata maps\n", in_file_index);
  2500. exit_program(1);
  2501. }
  2502. copy_metadata(o->metadata_map[i].specifier, *p ? p + 1 : p, oc,
  2503. in_file_index >= 0 ?
  2504. input_files[in_file_index]->ctx : NULL, o);
  2505. }
  2506. /* copy chapters */
  2507. if (o->chapters_input_file >= nb_input_files) {
  2508. if (o->chapters_input_file == INT_MAX) {
  2509. /* copy chapters from the first input file that has them*/
  2510. o->chapters_input_file = -1;
  2511. for (i = 0; i < nb_input_files; i++)
  2512. if (input_files[i]->ctx->nb_chapters) {
  2513. o->chapters_input_file = i;
  2514. break;
  2515. }
  2516. } else {
  2517. av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d in chapter mapping.\n",
  2518. o->chapters_input_file);
  2519. exit_program(1);
  2520. }
  2521. }
  2522. if (o->chapters_input_file >= 0)
  2523. copy_chapters(input_files[o->chapters_input_file], of, oc,
  2524. !o->metadata_chapters_manual);
  2525. /* copy global metadata by default */
  2526. if (!o->metadata_global_manual && nb_input_files){
  2527. av_dict_copy(&oc->metadata, input_files[0]->ctx->metadata,
  2528. AV_DICT_DONT_OVERWRITE);
  2529. if(o->recording_time != INT64_MAX)
  2530. av_dict_set(&oc->metadata, "duration", NULL, 0);
  2531. av_dict_set(&oc->metadata, "creation_time", NULL, 0);
  2532. av_dict_set(&oc->metadata, "company_name", NULL, 0);
  2533. av_dict_set(&oc->metadata, "product_name", NULL, 0);
  2534. av_dict_set(&oc->metadata, "product_version", NULL, 0);
  2535. }
  2536. if (!o->metadata_streams_manual)
  2537. for (i = of->ost_index; i < nb_output_streams; i++) {
  2538. InputStream *ist;
  2539. if (output_streams[i]->source_index < 0) /* this is true e.g. for attached files */
  2540. continue;
  2541. ist = input_streams[output_streams[i]->source_index];
  2542. av_dict_copy(&output_streams[i]->st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
  2543. if (!output_streams[i]->stream_copy) {
  2544. av_dict_set(&output_streams[i]->st->metadata, "encoder", NULL, 0);
  2545. }
  2546. }
  2547. /* process manually set programs */
  2548. for (i = 0; i < o->nb_program; i++) {
  2549. const char *p = o->program[i].u.str;
  2550. int progid = i+1;
  2551. AVProgram *program;
  2552. while(*p) {
  2553. const char *p2 = av_get_token(&p, ":");
  2554. const char *to_dealloc = p2;
  2555. char *key;
  2556. if (!p2)
  2557. break;
  2558. if(*p) p++;
  2559. key = av_get_token(&p2, "=");
  2560. if (!key || !*p2) {
  2561. av_freep(&to_dealloc);
  2562. av_freep(&key);
  2563. break;
  2564. }
  2565. p2++;
  2566. if (!strcmp(key, "program_num"))
  2567. progid = strtol(p2, NULL, 0);
  2568. av_freep(&to_dealloc);
  2569. av_freep(&key);
  2570. }
  2571. program = av_new_program(oc, progid);
  2572. p = o->program[i].u.str;
  2573. while(*p) {
  2574. const char *p2 = av_get_token(&p, ":");
  2575. const char *to_dealloc = p2;
  2576. char *key;
  2577. if (!p2)
  2578. break;
  2579. if(*p) p++;
  2580. key = av_get_token(&p2, "=");
  2581. if (!key) {
  2582. av_log(NULL, AV_LOG_FATAL,
  2583. "No '=' character in program string %s.\n",
  2584. p2);
  2585. exit_program(1);
  2586. }
  2587. if (!*p2)
  2588. exit_program(1);
  2589. p2++;
  2590. if (!strcmp(key, "title")) {
  2591. av_dict_set(&program->metadata, "title", p2, 0);
  2592. } else if (!strcmp(key, "program_num")) {
  2593. } else if (!strcmp(key, "st")) {
  2594. int st_num = strtol(p2, NULL, 0);
  2595. av_program_add_stream_index(oc, progid, st_num);
  2596. } else {
  2597. av_log(NULL, AV_LOG_FATAL, "Unknown program key %s.\n", key);
  2598. exit_program(1);
  2599. }
  2600. av_freep(&to_dealloc);
  2601. av_freep(&key);
  2602. }
  2603. }
  2604. /* process manually set metadata */
  2605. for (i = 0; i < o->nb_metadata; i++) {
  2606. AVDictionary **m;
  2607. char type, *val;
  2608. const char *stream_spec;
  2609. int index = 0, j, ret = 0;
  2610. val = strchr(o->metadata[i].u.str, '=');
  2611. if (!val) {
  2612. av_log(NULL, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
  2613. o->metadata[i].u.str);
  2614. exit_program(1);
  2615. }
  2616. *val++ = 0;
  2617. parse_meta_type(o->metadata[i].specifier, &type, &index, &stream_spec);
  2618. if (type == 's') {
  2619. for (j = 0; j < oc->nb_streams; j++) {
  2620. ost = output_streams[nb_output_streams - oc->nb_streams + j];
  2621. if ((ret = check_stream_specifier(oc, oc->streams[j], stream_spec)) > 0) {
  2622. if (!strcmp(o->metadata[i].u.str, "rotate")) {
  2623. char *tail;
  2624. double theta = av_strtod(val, &tail);
  2625. if (!*tail) {
  2626. ost->rotate_overridden = 1;
  2627. ost->rotate_override_value = theta;
  2628. }
  2629. } else {
  2630. av_dict_set(&oc->streams[j]->metadata, o->metadata[i].u.str, *val ? val : NULL, 0);
  2631. }
  2632. } else if (ret < 0)
  2633. exit_program(1);
  2634. }
  2635. }
  2636. else {
  2637. switch (type) {
  2638. case 'g':
  2639. m = &oc->metadata;
  2640. break;
  2641. case 'c':
  2642. if (index < 0 || index >= oc->nb_chapters) {
  2643. av_log(NULL, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
  2644. exit_program(1);
  2645. }
  2646. m = &oc->chapters[index]->metadata;
  2647. break;
  2648. case 'p':
  2649. if (index < 0 || index >= oc->nb_programs) {
  2650. av_log(NULL, AV_LOG_FATAL, "Invalid program index %d in metadata specifier.\n", index);
  2651. exit_program(1);
  2652. }
  2653. m = &oc->programs[index]->metadata;
  2654. break;
  2655. default:
  2656. av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
  2657. exit_program(1);
  2658. }
  2659. av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
  2660. }
  2661. }
  2662. err = set_dispositions(of, oc);
  2663. if (err < 0) {
  2664. av_log(NULL, AV_LOG_FATAL, "Error setting output stream dispositions\n");
  2665. exit_program(1);
  2666. }
  2667. return 0;
  2668. }
  2669. static int opt_target(void *optctx, const char *opt, const char *arg)
  2670. {
  2671. OptionsContext *o = optctx;
  2672. enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
  2673. static const char *const frame_rates[] = { "25", "30000/1001", "24000/1001" };
  2674. if (!strncmp(arg, "pal-", 4)) {
  2675. norm = PAL;
  2676. arg += 4;
  2677. } else if (!strncmp(arg, "ntsc-", 5)) {
  2678. norm = NTSC;
  2679. arg += 5;
  2680. } else if (!strncmp(arg, "film-", 5)) {
  2681. norm = FILM;
  2682. arg += 5;
  2683. } else {
  2684. /* Try to determine PAL/NTSC by peeking in the input files */
  2685. if (nb_input_files) {
  2686. int i, j;
  2687. for (j = 0; j < nb_input_files; j++) {
  2688. for (i = 0; i < input_files[j]->nb_streams; i++) {
  2689. AVStream *st = input_files[j]->ctx->streams[i];
  2690. int64_t fr;
  2691. if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
  2692. continue;
  2693. fr = st->time_base.den * 1000LL / st->time_base.num;
  2694. if (fr == 25000) {
  2695. norm = PAL;
  2696. break;
  2697. } else if ((fr == 29970) || (fr == 23976)) {
  2698. norm = NTSC;
  2699. break;
  2700. }
  2701. }
  2702. if (norm != UNKNOWN)
  2703. break;
  2704. }
  2705. }
  2706. if (norm != UNKNOWN)
  2707. av_log(NULL, AV_LOG_INFO, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC");
  2708. }
  2709. if (norm == UNKNOWN) {
  2710. av_log(NULL, AV_LOG_FATAL, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
  2711. av_log(NULL, AV_LOG_FATAL, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
  2712. av_log(NULL, AV_LOG_FATAL, "or set a framerate with \"-r xxx\".\n");
  2713. exit_program(1);
  2714. }
  2715. if (!strcmp(arg, "vcd")) {
  2716. opt_video_codec(o, "c:v", "mpeg1video");
  2717. opt_audio_codec(o, "c:a", "mp2");
  2718. parse_option(o, "f", "vcd", options);
  2719. parse_option(o, "s", norm == PAL ? "352x288" : "352x240", options);
  2720. parse_option(o, "r", frame_rates[norm], options);
  2721. opt_default(NULL, "g", norm == PAL ? "15" : "18");
  2722. opt_default(NULL, "b:v", "1150000");
  2723. opt_default(NULL, "maxrate:v", "1150000");
  2724. opt_default(NULL, "minrate:v", "1150000");
  2725. opt_default(NULL, "bufsize:v", "327680"); // 40*1024*8;
  2726. opt_default(NULL, "b:a", "224000");
  2727. parse_option(o, "ar", "44100", options);
  2728. parse_option(o, "ac", "2", options);
  2729. opt_default(NULL, "packetsize", "2324");
  2730. opt_default(NULL, "muxrate", "1411200"); // 2352 * 75 * 8;
  2731. /* We have to offset the PTS, so that it is consistent with the SCR.
  2732. SCR starts at 36000, but the first two packs contain only padding
  2733. and the first pack from the other stream, respectively, may also have
  2734. been written before.
  2735. So the real data starts at SCR 36000+3*1200. */
  2736. o->mux_preload = (36000 + 3 * 1200) / 90000.0; // 0.44
  2737. } else if (!strcmp(arg, "svcd")) {
  2738. opt_video_codec(o, "c:v", "mpeg2video");
  2739. opt_audio_codec(o, "c:a", "mp2");
  2740. parse_option(o, "f", "svcd", options);
  2741. parse_option(o, "s", norm == PAL ? "480x576" : "480x480", options);
  2742. parse_option(o, "r", frame_rates[norm], options);
  2743. parse_option(o, "pix_fmt", "yuv420p", options);
  2744. opt_default(NULL, "g", norm == PAL ? "15" : "18");
  2745. opt_default(NULL, "b:v", "2040000");
  2746. opt_default(NULL, "maxrate:v", "2516000");
  2747. opt_default(NULL, "minrate:v", "0"); // 1145000;
  2748. opt_default(NULL, "bufsize:v", "1835008"); // 224*1024*8;
  2749. opt_default(NULL, "scan_offset", "1");
  2750. opt_default(NULL, "b:a", "224000");
  2751. parse_option(o, "ar", "44100", options);
  2752. opt_default(NULL, "packetsize", "2324");
  2753. } else if (!strcmp(arg, "dvd")) {
  2754. opt_video_codec(o, "c:v", "mpeg2video");
  2755. opt_audio_codec(o, "c:a", "ac3");
  2756. parse_option(o, "f", "dvd", options);
  2757. parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
  2758. parse_option(o, "r", frame_rates[norm], options);
  2759. parse_option(o, "pix_fmt", "yuv420p", options);
  2760. opt_default(NULL, "g", norm == PAL ? "15" : "18");
  2761. opt_default(NULL, "b:v", "6000000");
  2762. opt_default(NULL, "maxrate:v", "9000000");
  2763. opt_default(NULL, "minrate:v", "0"); // 1500000;
  2764. opt_default(NULL, "bufsize:v", "1835008"); // 224*1024*8;
  2765. opt_default(NULL, "packetsize", "2048"); // from www.mpucoder.com: DVD sectors contain 2048 bytes of data, this is also the size of one pack.
  2766. opt_default(NULL, "muxrate", "10080000"); // from mplex project: data_rate = 1260000. mux_rate = data_rate * 8
  2767. opt_default(NULL, "b:a", "448000");
  2768. parse_option(o, "ar", "48000", options);
  2769. } else if (!strncmp(arg, "dv", 2)) {
  2770. parse_option(o, "f", "dv", options);
  2771. parse_option(o, "s", norm == PAL ? "720x576" : "720x480", options);
  2772. parse_option(o, "pix_fmt", !strncmp(arg, "dv50", 4) ? "yuv422p" :
  2773. norm == PAL ? "yuv420p" : "yuv411p", options);
  2774. parse_option(o, "r", frame_rates[norm], options);
  2775. parse_option(o, "ar", "48000", options);
  2776. parse_option(o, "ac", "2", options);
  2777. } else {
  2778. av_log(NULL, AV_LOG_ERROR, "Unknown target: %s\n", arg);
  2779. return AVERROR(EINVAL);
  2780. }
  2781. av_dict_copy(&o->g->codec_opts, codec_opts, AV_DICT_DONT_OVERWRITE);
  2782. av_dict_copy(&o->g->format_opts, format_opts, AV_DICT_DONT_OVERWRITE);
  2783. return 0;
  2784. }
  2785. static int opt_vstats_file(void *optctx, const char *opt, const char *arg)
  2786. {
  2787. av_free (vstats_filename);
  2788. vstats_filename = av_strdup (arg);
  2789. return 0;
  2790. }
  2791. static int opt_vstats(void *optctx, const char *opt, const char *arg)
  2792. {
  2793. char filename[40];
  2794. time_t today2 = time(NULL);
  2795. struct tm *today = localtime(&today2);
  2796. if (!today) { // maybe tomorrow
  2797. av_log(NULL, AV_LOG_FATAL, "Unable to get current time: %s\n", strerror(errno));
  2798. exit_program(1);
  2799. }
  2800. snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
  2801. today->tm_sec);
  2802. return opt_vstats_file(NULL, opt, filename);
  2803. }
  2804. static int opt_video_frames(void *optctx, const char *opt, const char *arg)
  2805. {
  2806. OptionsContext *o = optctx;
  2807. return parse_option(o, "frames:v", arg, options);
  2808. }
  2809. static int opt_audio_frames(void *optctx, const char *opt, const char *arg)
  2810. {
  2811. OptionsContext *o = optctx;
  2812. return parse_option(o, "frames:a", arg, options);
  2813. }
  2814. static int opt_data_frames(void *optctx, const char *opt, const char *arg)
  2815. {
  2816. OptionsContext *o = optctx;
  2817. return parse_option(o, "frames:d", arg, options);
  2818. }
  2819. static int opt_default_new(OptionsContext *o, const char *opt, const char *arg)
  2820. {
  2821. int ret;
  2822. AVDictionary *cbak = codec_opts;
  2823. AVDictionary *fbak = format_opts;
  2824. codec_opts = NULL;
  2825. format_opts = NULL;
  2826. ret = opt_default(NULL, opt, arg);
  2827. av_dict_copy(&o->g->codec_opts , codec_opts, 0);
  2828. av_dict_copy(&o->g->format_opts, format_opts, 0);
  2829. av_dict_free(&codec_opts);
  2830. av_dict_free(&format_opts);
  2831. codec_opts = cbak;
  2832. format_opts = fbak;
  2833. return ret;
  2834. }
  2835. static int opt_preset(void *optctx, const char *opt, const char *arg)
  2836. {
  2837. OptionsContext *o = optctx;
  2838. FILE *f=NULL;
  2839. char filename[1000], line[1000], tmp_line[1000];
  2840. const char *codec_name = NULL;
  2841. tmp_line[0] = *opt;
  2842. tmp_line[1] = 0;
  2843. MATCH_PER_TYPE_OPT(codec_names, str, codec_name, NULL, tmp_line);
  2844. if (!(f = get_preset_file(filename, sizeof(filename), arg, *opt == 'f', codec_name))) {
  2845. if(!strncmp(arg, "libx264-lossless", strlen("libx264-lossless"))){
  2846. av_log(NULL, AV_LOG_FATAL, "Please use -preset <speed> -qp 0\n");
  2847. }else
  2848. av_log(NULL, AV_LOG_FATAL, "File for preset '%s' not found\n", arg);
  2849. exit_program(1);
  2850. }
  2851. while (fgets(line, sizeof(line), f)) {
  2852. char *key = tmp_line, *value, *endptr;
  2853. if (strcspn(line, "#\n\r") == 0)
  2854. continue;
  2855. av_strlcpy(tmp_line, line, sizeof(tmp_line));
  2856. if (!av_strtok(key, "=", &value) ||
  2857. !av_strtok(value, "\r\n", &endptr)) {
  2858. av_log(NULL, AV_LOG_FATAL, "%s: Invalid syntax: '%s'\n", filename, line);
  2859. exit_program(1);
  2860. }
  2861. av_log(NULL, AV_LOG_DEBUG, "ffpreset[%s]: set '%s' = '%s'\n", filename, key, value);
  2862. if (!strcmp(key, "acodec")) opt_audio_codec (o, key, value);
  2863. else if (!strcmp(key, "vcodec")) opt_video_codec (o, key, value);
  2864. else if (!strcmp(key, "scodec")) opt_subtitle_codec(o, key, value);
  2865. else if (!strcmp(key, "dcodec")) opt_data_codec (o, key, value);
  2866. else if (opt_default_new(o, key, value) < 0) {
  2867. av_log(NULL, AV_LOG_FATAL, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n",
  2868. filename, line, key, value);
  2869. exit_program(1);
  2870. }
  2871. }
  2872. fclose(f);
  2873. return 0;
  2874. }
  2875. static int opt_old2new(void *optctx, const char *opt, const char *arg)
  2876. {
  2877. OptionsContext *o = optctx;
  2878. int ret;
  2879. char *s = av_asprintf("%s:%c", opt + 1, *opt);
  2880. if (!s)
  2881. return AVERROR(ENOMEM);
  2882. ret = parse_option(o, s, arg, options);
  2883. av_free(s);
  2884. return ret;
  2885. }
  2886. static int opt_bitrate(void *optctx, const char *opt, const char *arg)
  2887. {
  2888. OptionsContext *o = optctx;
  2889. if(!strcmp(opt, "ab")){
  2890. av_dict_set(&o->g->codec_opts, "b:a", arg, 0);
  2891. return 0;
  2892. } else if(!strcmp(opt, "b")){
  2893. av_log(NULL, AV_LOG_WARNING, "Please use -b:a or -b:v, -b is ambiguous\n");
  2894. av_dict_set(&o->g->codec_opts, "b:v", arg, 0);
  2895. return 0;
  2896. }
  2897. av_dict_set(&o->g->codec_opts, opt, arg, 0);
  2898. return 0;
  2899. }
  2900. static int opt_qscale(void *optctx, const char *opt, const char *arg)
  2901. {
  2902. OptionsContext *o = optctx;
  2903. char *s;
  2904. int ret;
  2905. if(!strcmp(opt, "qscale")){
  2906. av_log(NULL, AV_LOG_WARNING, "Please use -q:a or -q:v, -qscale is ambiguous\n");
  2907. return parse_option(o, "q:v", arg, options);
  2908. }
  2909. s = av_asprintf("q%s", opt + 6);
  2910. if (!s)
  2911. return AVERROR(ENOMEM);
  2912. ret = parse_option(o, s, arg, options);
  2913. av_free(s);
  2914. return ret;
  2915. }
  2916. static int opt_profile(void *optctx, const char *opt, const char *arg)
  2917. {
  2918. OptionsContext *o = optctx;
  2919. if(!strcmp(opt, "profile")){
  2920. av_log(NULL, AV_LOG_WARNING, "Please use -profile:a or -profile:v, -profile is ambiguous\n");
  2921. av_dict_set(&o->g->codec_opts, "profile:v", arg, 0);
  2922. return 0;
  2923. }
  2924. av_dict_set(&o->g->codec_opts, opt, arg, 0);
  2925. return 0;
  2926. }
  2927. static int opt_video_filters(void *optctx, const char *opt, const char *arg)
  2928. {
  2929. OptionsContext *o = optctx;
  2930. return parse_option(o, "filter:v", arg, options);
  2931. }
  2932. static int opt_audio_filters(void *optctx, const char *opt, const char *arg)
  2933. {
  2934. OptionsContext *o = optctx;
  2935. return parse_option(o, "filter:a", arg, options);
  2936. }
  2937. static int opt_vsync(void *optctx, const char *opt, const char *arg)
  2938. {
  2939. av_log(NULL, AV_LOG_WARNING, "-vsync is deprecated. Use -fps_mode\n");
  2940. parse_and_set_vsync(arg, &video_sync_method, -1, -1, 1);
  2941. return 0;
  2942. }
  2943. static int opt_timecode(void *optctx, const char *opt, const char *arg)
  2944. {
  2945. OptionsContext *o = optctx;
  2946. int ret;
  2947. char *tcr = av_asprintf("timecode=%s", arg);
  2948. if (!tcr)
  2949. return AVERROR(ENOMEM);
  2950. ret = parse_option(o, "metadata:g", tcr, options);
  2951. if (ret >= 0)
  2952. ret = av_dict_set(&o->g->codec_opts, "gop_timecode", arg, 0);
  2953. av_free(tcr);
  2954. return ret;
  2955. }
  2956. static int opt_audio_qscale(void *optctx, const char *opt, const char *arg)
  2957. {
  2958. OptionsContext *o = optctx;
  2959. return parse_option(o, "q:a", arg, options);
  2960. }
  2961. static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
  2962. {
  2963. FilterGraph *fg = ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
  2964. fg->index = nb_filtergraphs - 1;
  2965. fg->graph_desc = av_strdup(arg);
  2966. if (!fg->graph_desc)
  2967. return AVERROR(ENOMEM);
  2968. input_stream_potentially_available = 1;
  2969. return 0;
  2970. }
  2971. static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
  2972. {
  2973. FilterGraph *fg;
  2974. char *graph_desc = read_file(arg);
  2975. if (!graph_desc)
  2976. return AVERROR(EINVAL);
  2977. fg = ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
  2978. fg->index = nb_filtergraphs - 1;
  2979. fg->graph_desc = graph_desc;
  2980. input_stream_potentially_available = 1;
  2981. return 0;
  2982. }
  2983. void show_help_default(const char *opt, const char *arg)
  2984. {
  2985. /* per-file options have at least one of those set */
  2986. const int per_file = OPT_SPEC | OPT_OFFSET | OPT_PERFILE;
  2987. int show_advanced = 0, show_avoptions = 0;
  2988. if (opt && *opt) {
  2989. if (!strcmp(opt, "long"))
  2990. show_advanced = 1;
  2991. else if (!strcmp(opt, "full"))
  2992. show_advanced = show_avoptions = 1;
  2993. else
  2994. av_log(NULL, AV_LOG_ERROR, "Unknown help option '%s'.\n", opt);
  2995. }
  2996. show_usage();
  2997. printf("Getting help:\n"
  2998. " -h -- print basic options\n"
  2999. " -h long -- print more options\n"
  3000. " -h full -- print all options (including all format and codec specific options, very long)\n"
  3001. " -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol\n"
  3002. " See man %s for detailed description of the options.\n"
  3003. "\n", program_name);
  3004. show_help_options(options, "Print help / information / capabilities:",
  3005. OPT_EXIT, 0, 0);
  3006. show_help_options(options, "Global options (affect whole program "
  3007. "instead of just one file):",
  3008. 0, per_file | OPT_EXIT | OPT_EXPERT, 0);
  3009. if (show_advanced)
  3010. show_help_options(options, "Advanced global options:", OPT_EXPERT,
  3011. per_file | OPT_EXIT, 0);
  3012. show_help_options(options, "Per-file main options:", 0,
  3013. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE |
  3014. OPT_EXIT, per_file);
  3015. if (show_advanced)
  3016. show_help_options(options, "Advanced per-file options:",
  3017. OPT_EXPERT, OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE, per_file);
  3018. show_help_options(options, "Video options:",
  3019. OPT_VIDEO, OPT_EXPERT | OPT_AUDIO, 0);
  3020. if (show_advanced)
  3021. show_help_options(options, "Advanced Video options:",
  3022. OPT_EXPERT | OPT_VIDEO, OPT_AUDIO, 0);
  3023. show_help_options(options, "Audio options:",
  3024. OPT_AUDIO, OPT_EXPERT | OPT_VIDEO, 0);
  3025. if (show_advanced)
  3026. show_help_options(options, "Advanced Audio options:",
  3027. OPT_EXPERT | OPT_AUDIO, OPT_VIDEO, 0);
  3028. show_help_options(options, "Subtitle options:",
  3029. OPT_SUBTITLE, 0, 0);
  3030. printf("\n");
  3031. if (show_avoptions) {
  3032. int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
  3033. show_help_children(avcodec_get_class(), flags);
  3034. show_help_children(avformat_get_class(), flags);
  3035. #if CONFIG_SWSCALE
  3036. show_help_children(sws_get_class(), flags);
  3037. #endif
  3038. #if CONFIG_SWRESAMPLE
  3039. show_help_children(swr_get_class(), AV_OPT_FLAG_AUDIO_PARAM);
  3040. #endif
  3041. show_help_children(avfilter_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM);
  3042. show_help_children(av_bsf_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM);
  3043. }
  3044. }
  3045. void show_usage(void)
  3046. {
  3047. av_log(NULL, AV_LOG_INFO, "Hyper fast Audio and Video encoder\n");
  3048. av_log(NULL, AV_LOG_INFO, "usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n", program_name);
  3049. av_log(NULL, AV_LOG_INFO, "\n");
  3050. }
  3051. enum OptGroup {
  3052. GROUP_OUTFILE,
  3053. GROUP_INFILE,
  3054. };
  3055. static const OptionGroupDef groups[] = {
  3056. [GROUP_OUTFILE] = { "output url", NULL, OPT_OUTPUT },
  3057. [GROUP_INFILE] = { "input url", "i", OPT_INPUT },
  3058. };
  3059. static int open_files(OptionGroupList *l, const char *inout,
  3060. int (*open_file)(OptionsContext*, const char*))
  3061. {
  3062. int i, ret;
  3063. for (i = 0; i < l->nb_groups; i++) {
  3064. OptionGroup *g = &l->groups[i];
  3065. OptionsContext o;
  3066. init_options(&o);
  3067. o.g = g;
  3068. ret = parse_optgroup(&o, g);
  3069. if (ret < 0) {
  3070. av_log(NULL, AV_LOG_ERROR, "Error parsing options for %s file "
  3071. "%s.\n", inout, g->arg);
  3072. uninit_options(&o);
  3073. return ret;
  3074. }
  3075. av_log(NULL, AV_LOG_DEBUG, "Opening an %s file: %s.\n", inout, g->arg);
  3076. ret = open_file(&o, g->arg);
  3077. uninit_options(&o);
  3078. if (ret < 0) {
  3079. av_log(NULL, AV_LOG_ERROR, "Error opening %s file %s.\n",
  3080. inout, g->arg);
  3081. return ret;
  3082. }
  3083. av_log(NULL, AV_LOG_DEBUG, "Successfully opened the file.\n");
  3084. }
  3085. return 0;
  3086. }
  3087. int ffmpeg_parse_options(int argc, char **argv)
  3088. {
  3089. OptionParseContext octx;
  3090. uint8_t error[128];
  3091. int ret;
  3092. memset(&octx, 0, sizeof(octx));
  3093. /* split the commandline into an internal representation */
  3094. ret = split_commandline(&octx, argc, argv, options, groups,
  3095. FF_ARRAY_ELEMS(groups));
  3096. if (ret < 0) {
  3097. av_log(NULL, AV_LOG_FATAL, "Error splitting the argument list: ");
  3098. goto fail;
  3099. }
  3100. /* apply global options */
  3101. ret = parse_optgroup(NULL, &octx.global_opts);
  3102. if (ret < 0) {
  3103. av_log(NULL, AV_LOG_FATAL, "Error parsing global options: ");
  3104. goto fail;
  3105. }
  3106. /* configure terminal and setup signal handlers */
  3107. term_init();
  3108. /* open input files */
  3109. ret = open_files(&octx.groups[GROUP_INFILE], "input", open_input_file);
  3110. if (ret < 0) {
  3111. av_log(NULL, AV_LOG_FATAL, "Error opening input files: ");
  3112. goto fail;
  3113. }
  3114. apply_sync_offsets();
  3115. /* create the complex filtergraphs */
  3116. ret = init_complex_filters();
  3117. if (ret < 0) {
  3118. av_log(NULL, AV_LOG_FATAL, "Error initializing complex filters.\n");
  3119. goto fail;
  3120. }
  3121. /* open output files */
  3122. ret = open_files(&octx.groups[GROUP_OUTFILE], "output", open_output_file);
  3123. if (ret < 0) {
  3124. av_log(NULL, AV_LOG_FATAL, "Error opening output files: ");
  3125. goto fail;
  3126. }
  3127. check_filter_outputs();
  3128. fail:
  3129. uninit_parse_context(&octx);
  3130. if (ret < 0) {
  3131. av_strerror(ret, error, sizeof(error));
  3132. av_log(NULL, AV_LOG_FATAL, "%s\n", error);
  3133. }
  3134. return ret;
  3135. }
  3136. static int opt_progress(void *optctx, const char *opt, const char *arg)
  3137. {
  3138. AVIOContext *avio = NULL;
  3139. int ret;
  3140. if (!strcmp(arg, "-"))
  3141. arg = "pipe:";
  3142. ret = avio_open2(&avio, arg, AVIO_FLAG_WRITE, &int_cb, NULL);
  3143. if (ret < 0) {
  3144. av_log(NULL, AV_LOG_ERROR, "Failed to open progress URL \"%s\": %s\n",
  3145. arg, av_err2str(ret));
  3146. return ret;
  3147. }
  3148. progress_avio = avio;
  3149. return 0;
  3150. }
  3151. int opt_timelimit(void *optctx, const char *opt, const char *arg)
  3152. {
  3153. #if HAVE_SETRLIMIT
  3154. int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  3155. struct rlimit rl = { lim, lim + 1 };
  3156. if (setrlimit(RLIMIT_CPU, &rl))
  3157. perror("setrlimit");
  3158. #else
  3159. av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
  3160. #endif
  3161. return 0;
  3162. }
  3163. #define OFFSET(x) offsetof(OptionsContext, x)
  3164. const OptionDef options[] = {
  3165. /* main options */
  3166. CMDUTILS_COMMON_OPTIONS
  3167. { "f", HAS_ARG | OPT_STRING | OPT_OFFSET |
  3168. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(format) },
  3169. "force format", "fmt" },
  3170. { "y", OPT_BOOL, { &file_overwrite },
  3171. "overwrite output files" },
  3172. { "n", OPT_BOOL, { &no_file_overwrite },
  3173. "never overwrite output files" },
  3174. { "ignore_unknown", OPT_BOOL, { &ignore_unknown_streams },
  3175. "Ignore unknown stream types" },
  3176. { "copy_unknown", OPT_BOOL | OPT_EXPERT, { &copy_unknown_streams },
  3177. "Copy unknown stream types" },
  3178. { "recast_media", OPT_BOOL | OPT_EXPERT, { &recast_media },
  3179. "allow recasting stream type in order to force a decoder of different media type" },
  3180. { "c", HAS_ARG | OPT_STRING | OPT_SPEC |
  3181. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(codec_names) },
  3182. "codec name", "codec" },
  3183. { "codec", HAS_ARG | OPT_STRING | OPT_SPEC |
  3184. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(codec_names) },
  3185. "codec name", "codec" },
  3186. { "pre", HAS_ARG | OPT_STRING | OPT_SPEC |
  3187. OPT_OUTPUT, { .off = OFFSET(presets) },
  3188. "preset name", "preset" },
  3189. { "map", HAS_ARG | OPT_EXPERT | OPT_PERFILE |
  3190. OPT_OUTPUT, { .func_arg = opt_map },
  3191. "set input stream mapping",
  3192. "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
  3193. { "map_channel", HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_map_channel },
  3194. "map an audio channel from one stream to another", "file.stream.channel[:syncfile.syncstream]" },
  3195. { "map_metadata", HAS_ARG | OPT_STRING | OPT_SPEC |
  3196. OPT_OUTPUT, { .off = OFFSET(metadata_map) },
  3197. "set metadata information of outfile from infile",
  3198. "outfile[,metadata]:infile[,metadata]" },
  3199. { "map_chapters", HAS_ARG | OPT_INT | OPT_EXPERT | OPT_OFFSET |
  3200. OPT_OUTPUT, { .off = OFFSET(chapters_input_file) },
  3201. "set chapters mapping", "input_file_index" },
  3202. { "t", HAS_ARG | OPT_TIME | OPT_OFFSET |
  3203. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(recording_time) },
  3204. "record or transcode \"duration\" seconds of audio/video",
  3205. "duration" },
  3206. { "to", HAS_ARG | OPT_TIME | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(stop_time) },
  3207. "record or transcode stop time", "time_stop" },
  3208. { "fs", HAS_ARG | OPT_INT64 | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(limit_filesize) },
  3209. "set the limit file size in bytes", "limit_size" },
  3210. { "ss", HAS_ARG | OPT_TIME | OPT_OFFSET |
  3211. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(start_time) },
  3212. "set the start time offset", "time_off" },
  3213. { "sseof", HAS_ARG | OPT_TIME | OPT_OFFSET |
  3214. OPT_INPUT, { .off = OFFSET(start_time_eof) },
  3215. "set the start time offset relative to EOF", "time_off" },
  3216. { "seek_timestamp", HAS_ARG | OPT_INT | OPT_OFFSET |
  3217. OPT_INPUT, { .off = OFFSET(seek_timestamp) },
  3218. "enable/disable seeking by timestamp with -ss" },
  3219. { "accurate_seek", OPT_BOOL | OPT_OFFSET | OPT_EXPERT |
  3220. OPT_INPUT, { .off = OFFSET(accurate_seek) },
  3221. "enable/disable accurate seeking with -ss" },
  3222. { "isync", HAS_ARG | OPT_INT | OPT_OFFSET |
  3223. OPT_EXPERT | OPT_INPUT, { .off = OFFSET(input_sync_ref) },
  3224. "Indicate the input index for sync reference", "sync ref" },
  3225. { "itsoffset", HAS_ARG | OPT_TIME | OPT_OFFSET |
  3226. OPT_EXPERT | OPT_INPUT, { .off = OFFSET(input_ts_offset) },
  3227. "set the input ts offset", "time_off" },
  3228. { "itsscale", HAS_ARG | OPT_DOUBLE | OPT_SPEC |
  3229. OPT_EXPERT | OPT_INPUT, { .off = OFFSET(ts_scale) },
  3230. "set the input ts scale", "scale" },
  3231. { "timestamp", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_recording_timestamp },
  3232. "set the recording timestamp ('now' to set the current time)", "time" },
  3233. { "metadata", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(metadata) },
  3234. "add metadata", "string=string" },
  3235. { "program", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(program) },
  3236. "add program with specified streams", "title=string:st=number..." },
  3237. { "dframes", HAS_ARG | OPT_PERFILE | OPT_EXPERT |
  3238. OPT_OUTPUT, { .func_arg = opt_data_frames },
  3239. "set the number of data frames to output", "number" },
  3240. { "benchmark", OPT_BOOL | OPT_EXPERT, { &do_benchmark },
  3241. "add timings for benchmarking" },
  3242. { "benchmark_all", OPT_BOOL | OPT_EXPERT, { &do_benchmark_all },
  3243. "add timings for each task" },
  3244. { "progress", HAS_ARG | OPT_EXPERT, { .func_arg = opt_progress },
  3245. "write program-readable progress information", "url" },
  3246. { "stdin", OPT_BOOL | OPT_EXPERT, { &stdin_interaction },
  3247. "enable or disable interaction on standard input" },
  3248. { "timelimit", HAS_ARG | OPT_EXPERT, { .func_arg = opt_timelimit },
  3249. "set max runtime in seconds in CPU user time", "limit" },
  3250. { "dump", OPT_BOOL | OPT_EXPERT, { &do_pkt_dump },
  3251. "dump each input packet" },
  3252. { "hex", OPT_BOOL | OPT_EXPERT, { &do_hex_dump },
  3253. "when dumping packets, also dump the payload" },
  3254. { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
  3255. OPT_INPUT, { .off = OFFSET(rate_emu) },
  3256. "read input at native frame rate; equivalent to -readrate 1", "" },
  3257. { "readrate", HAS_ARG | OPT_FLOAT | OPT_OFFSET |
  3258. OPT_EXPERT | OPT_INPUT, { .off = OFFSET(readrate) },
  3259. "read input at specified rate", "speed" },
  3260. { "target", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_target },
  3261. "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\" or \"dv50\" "
  3262. "with optional prefixes \"pal-\", \"ntsc-\" or \"film-\")", "type" },
  3263. { "vsync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vsync },
  3264. "set video sync method globally; deprecated, use -fps_mode", "" },
  3265. { "frame_drop_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &frame_drop_threshold },
  3266. "frame drop threshold", "" },
  3267. { "async", HAS_ARG | OPT_INT | OPT_EXPERT, { &audio_sync_method },
  3268. "audio sync method", "" },
  3269. { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &audio_drift_threshold },
  3270. "audio drift threshold", "threshold" },
  3271. { "copyts", OPT_BOOL | OPT_EXPERT, { &copy_ts },
  3272. "copy timestamps" },
  3273. { "start_at_zero", OPT_BOOL | OPT_EXPERT, { &start_at_zero },
  3274. "shift input timestamps to start at 0 when using copyts" },
  3275. { "copytb", HAS_ARG | OPT_INT | OPT_EXPERT, { &copy_tb },
  3276. "copy input stream time base when stream copying", "mode" },
  3277. { "shortest", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
  3278. OPT_OUTPUT, { .off = OFFSET(shortest) },
  3279. "finish encoding within shortest input" },
  3280. { "bitexact", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
  3281. OPT_OUTPUT | OPT_INPUT, { .off = OFFSET(bitexact) },
  3282. "bitexact mode" },
  3283. { "apad", OPT_STRING | HAS_ARG | OPT_SPEC |
  3284. OPT_OUTPUT, { .off = OFFSET(apad) },
  3285. "audio pad", "" },
  3286. { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &dts_delta_threshold },
  3287. "timestamp discontinuity delta threshold", "threshold" },
  3288. { "dts_error_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, { &dts_error_threshold },
  3289. "timestamp error delta threshold", "threshold" },
  3290. { "xerror", OPT_BOOL | OPT_EXPERT, { &exit_on_error },
  3291. "exit on error", "error" },
  3292. { "abort_on", HAS_ARG | OPT_EXPERT, { .func_arg = opt_abort_on },
  3293. "abort on the specified condition flags", "flags" },
  3294. { "copyinkf", OPT_BOOL | OPT_EXPERT | OPT_SPEC |
  3295. OPT_OUTPUT, { .off = OFFSET(copy_initial_nonkeyframes) },
  3296. "copy initial non-keyframes" },
  3297. { "copypriorss", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(copy_prior_start) },
  3298. "copy or discard frames before start time" },
  3299. { "frames", OPT_INT64 | HAS_ARG | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(max_frames) },
  3300. "set the number of frames to output", "number" },
  3301. { "tag", OPT_STRING | HAS_ARG | OPT_SPEC |
  3302. OPT_EXPERT | OPT_OUTPUT | OPT_INPUT, { .off = OFFSET(codec_tags) },
  3303. "force codec tag/fourcc", "fourcc/tag" },
  3304. { "q", HAS_ARG | OPT_EXPERT | OPT_DOUBLE |
  3305. OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(qscale) },
  3306. "use fixed quality scale (VBR)", "q" },
  3307. { "qscale", HAS_ARG | OPT_EXPERT | OPT_PERFILE |
  3308. OPT_OUTPUT, { .func_arg = opt_qscale },
  3309. "use fixed quality scale (VBR)", "q" },
  3310. { "profile", HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_profile },
  3311. "set profile", "profile" },
  3312. { "filter", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filters) },
  3313. "set stream filtergraph", "filter_graph" },
  3314. { "filter_threads", HAS_ARG, { .func_arg = opt_filter_threads },
  3315. "number of non-complex filter threads" },
  3316. { "filter_script", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filter_scripts) },
  3317. "read stream filtergraph description from a file", "filename" },
  3318. { "reinit_filter", HAS_ARG | OPT_INT | OPT_SPEC | OPT_INPUT, { .off = OFFSET(reinit_filters) },
  3319. "reinit filtergraph on input parameter changes", "" },
  3320. { "filter_complex", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex },
  3321. "create a complex filtergraph", "graph_description" },
  3322. { "filter_complex_threads", HAS_ARG | OPT_INT, { &filter_complex_nbthreads },
  3323. "number of threads for -filter_complex" },
  3324. { "lavfi", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex },
  3325. "create a complex filtergraph", "graph_description" },
  3326. { "filter_complex_script", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_complex_script },
  3327. "read complex filtergraph description from a file", "filename" },
  3328. { "auto_conversion_filters", OPT_BOOL | OPT_EXPERT, { &auto_conversion_filters },
  3329. "enable automatic conversion filters globally" },
  3330. { "stats", OPT_BOOL, { &print_stats },
  3331. "print progress report during encoding", },
  3332. { "stats_period", HAS_ARG | OPT_EXPERT, { .func_arg = opt_stats_period },
  3333. "set the period at which ffmpeg updates stats and -progress output", "time" },
  3334. { "attach", HAS_ARG | OPT_PERFILE | OPT_EXPERT |
  3335. OPT_OUTPUT, { .func_arg = opt_attach },
  3336. "add an attachment to the output file", "filename" },
  3337. { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC |
  3338. OPT_EXPERT | OPT_INPUT, { .off = OFFSET(dump_attachment) },
  3339. "extract an attachment into a file", "filename" },
  3340. { "stream_loop", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_INPUT |
  3341. OPT_OFFSET, { .off = OFFSET(loop) }, "set number of times input stream shall be looped", "loop count" },
  3342. { "debug_ts", OPT_BOOL | OPT_EXPERT, { &debug_ts },
  3343. "print timestamp debugging info" },
  3344. { "max_error_rate", HAS_ARG | OPT_FLOAT, { &max_error_rate },
  3345. "ratio of decoding errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.", "maximum error rate" },
  3346. { "discard", OPT_STRING | HAS_ARG | OPT_SPEC |
  3347. OPT_INPUT, { .off = OFFSET(discard) },
  3348. "discard", "" },
  3349. { "disposition", OPT_STRING | HAS_ARG | OPT_SPEC |
  3350. OPT_OUTPUT, { .off = OFFSET(disposition) },
  3351. "disposition", "" },
  3352. { "thread_queue_size", HAS_ARG | OPT_INT | OPT_OFFSET | OPT_EXPERT | OPT_INPUT,
  3353. { .off = OFFSET(thread_queue_size) },
  3354. "set the maximum number of queued packets from the demuxer" },
  3355. { "find_stream_info", OPT_BOOL | OPT_PERFILE | OPT_INPUT | OPT_EXPERT, { &find_stream_info },
  3356. "read and decode the streams to fill missing information with heuristics" },
  3357. { "bits_per_raw_sample", OPT_INT | HAS_ARG | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT,
  3358. { .off = OFFSET(bits_per_raw_sample) },
  3359. "set the number of bits per raw sample", "number" },
  3360. /* video options */
  3361. { "vframes", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_video_frames },
  3362. "set the number of video frames to output", "number" },
  3363. { "r", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
  3364. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_rates) },
  3365. "set frame rate (Hz value, fraction or abbreviation)", "rate" },
  3366. { "fpsmax", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
  3367. OPT_OUTPUT, { .off = OFFSET(max_frame_rates) },
  3368. "set max frame rate (Hz value, fraction or abbreviation)", "rate" },
  3369. { "s", OPT_VIDEO | HAS_ARG | OPT_SUBTITLE | OPT_STRING | OPT_SPEC |
  3370. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_sizes) },
  3371. "set frame size (WxH or abbreviation)", "size" },
  3372. { "aspect", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_SPEC |
  3373. OPT_OUTPUT, { .off = OFFSET(frame_aspect_ratios) },
  3374. "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
  3375. { "pix_fmt", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
  3376. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(frame_pix_fmts) },
  3377. "set pixel format", "format" },
  3378. { "vn", OPT_VIDEO | OPT_BOOL | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT,{ .off = OFFSET(video_disable) },
  3379. "disable video" },
  3380. { "rc_override", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
  3381. OPT_OUTPUT, { .off = OFFSET(rc_overrides) },
  3382. "rate control override for specific intervals", "override" },
  3383. { "vcodec", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_INPUT |
  3384. OPT_OUTPUT, { .func_arg = opt_video_codec },
  3385. "force video codec ('copy' to copy stream)", "codec" },
  3386. { "timecode", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_timecode },
  3387. "set initial TimeCode value.", "hh:mm:ss[:;.]ff" },
  3388. { "pass", OPT_VIDEO | HAS_ARG | OPT_SPEC | OPT_INT | OPT_OUTPUT, { .off = OFFSET(pass) },
  3389. "select the pass number (1 to 3)", "n" },
  3390. { "passlogfile", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC |
  3391. OPT_OUTPUT, { .off = OFFSET(passlogfiles) },
  3392. "select two pass log file name prefix", "prefix" },
  3393. { "psnr", OPT_VIDEO | OPT_BOOL | OPT_EXPERT, { &do_psnr },
  3394. "calculate PSNR of compressed frames" },
  3395. { "vstats", OPT_VIDEO | OPT_EXPERT , { .func_arg = opt_vstats },
  3396. "dump video coding statistics to file" },
  3397. { "vstats_file", OPT_VIDEO | HAS_ARG | OPT_EXPERT , { .func_arg = opt_vstats_file },
  3398. "dump video coding statistics to file", "file" },
  3399. { "vstats_version", OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT , { &vstats_version },
  3400. "Version of the vstats format to use."},
  3401. { "vf", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_video_filters },
  3402. "set video filters", "filter_graph" },
  3403. { "intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
  3404. OPT_OUTPUT, { .off = OFFSET(intra_matrices) },
  3405. "specify intra matrix coeffs", "matrix" },
  3406. { "inter_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
  3407. OPT_OUTPUT, { .off = OFFSET(inter_matrices) },
  3408. "specify inter matrix coeffs", "matrix" },
  3409. { "chroma_intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_STRING | OPT_SPEC |
  3410. OPT_OUTPUT, { .off = OFFSET(chroma_intra_matrices) },
  3411. "specify intra matrix coeffs", "matrix" },
  3412. { "top", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_INT| OPT_SPEC |
  3413. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(top_field_first) },
  3414. "top=1/bottom=0/auto=-1 field first", "" },
  3415. { "vtag", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
  3416. OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_old2new },
  3417. "force video tag/fourcc", "fourcc/tag" },
  3418. { "qphist", OPT_VIDEO | OPT_BOOL | OPT_EXPERT , { &qp_hist },
  3419. "show QP histogram" },
  3420. { "fps_mode", OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_EXPERT |
  3421. OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(fps_mode) },
  3422. "set framerate mode for matching video streams; overrides vsync" },
  3423. { "force_fps", OPT_VIDEO | OPT_BOOL | OPT_EXPERT | OPT_SPEC |
  3424. OPT_OUTPUT, { .off = OFFSET(force_fps) },
  3425. "force the selected framerate, disable the best supported framerate selection" },
  3426. { "streamid", OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
  3427. OPT_OUTPUT, { .func_arg = opt_streamid },
  3428. "set the value of an outfile streamid", "streamIndex:value" },
  3429. { "force_key_frames", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
  3430. OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(forced_key_frames) },
  3431. "force key frames at specified timestamps", "timestamps" },
  3432. { "ab", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_bitrate },
  3433. "audio bitrate (please use -b:a)", "bitrate" },
  3434. { "b", OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_bitrate },
  3435. "video bitrate (please use -b:v)", "bitrate" },
  3436. { "hwaccel", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
  3437. OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccels) },
  3438. "use HW accelerated decoding", "hwaccel name" },
  3439. { "hwaccel_device", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
  3440. OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccel_devices) },
  3441. "select a device for HW acceleration", "devicename" },
  3442. { "hwaccel_output_format", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
  3443. OPT_SPEC | OPT_INPUT, { .off = OFFSET(hwaccel_output_formats) },
  3444. "select output format used with HW accelerated decoding", "format" },
  3445. { "hwaccels", OPT_EXIT, { .func_arg = show_hwaccels },
  3446. "show available HW acceleration methods" },
  3447. { "autorotate", HAS_ARG | OPT_BOOL | OPT_SPEC |
  3448. OPT_EXPERT | OPT_INPUT, { .off = OFFSET(autorotate) },
  3449. "automatically insert correct rotate filters" },
  3450. { "autoscale", HAS_ARG | OPT_BOOL | OPT_SPEC |
  3451. OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(autoscale) },
  3452. "automatically insert a scale filter at the end of the filter graph" },
  3453. /* audio options */
  3454. { "aframes", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_frames },
  3455. "set the number of audio frames to output", "number" },
  3456. { "aq", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_qscale },
  3457. "set audio quality (codec-specific)", "quality", },
  3458. { "ar", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC |
  3459. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_sample_rate) },
  3460. "set audio sampling rate (in Hz)", "rate" },
  3461. { "ac", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC |
  3462. OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_channels) },
  3463. "set number of audio channels", "channels" },
  3464. { "an", OPT_AUDIO | OPT_BOOL | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT,{ .off = OFFSET(audio_disable) },
  3465. "disable audio" },
  3466. { "acodec", OPT_AUDIO | HAS_ARG | OPT_PERFILE |
  3467. OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_audio_codec },
  3468. "force audio codec ('copy' to copy stream)", "codec" },
  3469. { "atag", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
  3470. OPT_OUTPUT, { .func_arg = opt_old2new },
  3471. "force audio tag/fourcc", "fourcc/tag" },
  3472. { "vol", OPT_AUDIO | HAS_ARG | OPT_INT, { &audio_volume },
  3473. "change audio volume (256=normal)" , "volume" },
  3474. { "sample_fmt", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_SPEC |
  3475. OPT_STRING | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(sample_fmts) },
  3476. "set sample format", "format" },
  3477. { "channel_layout", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_SPEC |
  3478. OPT_STRING | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_ch_layouts) },
  3479. "set channel layout", "layout" },
  3480. { "ch_layout", OPT_AUDIO | HAS_ARG | OPT_EXPERT | OPT_SPEC |
  3481. OPT_STRING | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(audio_ch_layouts) },
  3482. "set channel layout", "layout" },
  3483. { "af", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_filters },
  3484. "set audio filters", "filter_graph" },
  3485. { "guess_layout_max", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_INPUT, { .off = OFFSET(guess_layout_max) },
  3486. "set the maximum number of channels to try to guess the channel layout" },
  3487. /* subtitle options */
  3488. { "sn", OPT_SUBTITLE | OPT_BOOL | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(subtitle_disable) },
  3489. "disable subtitle" },
  3490. { "scodec", OPT_SUBTITLE | HAS_ARG | OPT_PERFILE | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_subtitle_codec },
  3491. "force subtitle codec ('copy' to copy stream)", "codec" },
  3492. { "stag", OPT_SUBTITLE | HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new }
  3493. , "force subtitle tag/fourcc", "fourcc/tag" },
  3494. { "fix_sub_duration", OPT_BOOL | OPT_EXPERT | OPT_SUBTITLE | OPT_SPEC | OPT_INPUT, { .off = OFFSET(fix_sub_duration) },
  3495. "fix subtitles duration" },
  3496. { "canvas_size", OPT_SUBTITLE | HAS_ARG | OPT_STRING | OPT_SPEC | OPT_INPUT, { .off = OFFSET(canvas_sizes) },
  3497. "set canvas size (WxH or abbreviation)", "size" },
  3498. /* muxer options */
  3499. { "muxdelay", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_max_delay) },
  3500. "set the maximum demux-decode delay", "seconds" },
  3501. { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_preload) },
  3502. "set the initial demux-decode delay", "seconds" },
  3503. { "sdp_file", HAS_ARG | OPT_EXPERT | OPT_OUTPUT, { .func_arg = opt_sdp_file },
  3504. "specify a file in which to print sdp information", "file" },
  3505. { "time_base", HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(time_bases) },
  3506. "set the desired time base hint for output stream (1:24, 1:48000 or 0.04166, 2.0833e-5)", "ratio" },
  3507. { "enc_time_base", HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(enc_time_bases) },
  3508. "set the desired time base for the encoder (1:24, 1:48000 or 0.04166, 2.0833e-5). "
  3509. "two special values are defined - "
  3510. "0 = use frame rate (video) or sample rate (audio),"
  3511. "-1 = match source time base", "ratio" },
  3512. { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(bitstream_filters) },
  3513. "A comma-separated list of bitstream filters", "bitstream_filters" },
  3514. { "absf", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new },
  3515. "deprecated", "audio bitstream_filters" },
  3516. { "vbsf", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new },
  3517. "deprecated", "video bitstream_filters" },
  3518. { "apre", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
  3519. "set the audio options to the indicated preset", "preset" },
  3520. { "vpre", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
  3521. "set the video options to the indicated preset", "preset" },
  3522. { "spre", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
  3523. "set the subtitle options to the indicated preset", "preset" },
  3524. { "fpre", HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
  3525. "set options from indicated preset file", "filename" },
  3526. { "max_muxing_queue_size", HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(max_muxing_queue_size) },
  3527. "maximum number of packets that can be buffered while waiting for all streams to initialize", "packets" },
  3528. { "muxing_queue_data_threshold", HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(muxing_queue_data_threshold) },
  3529. "set the threshold after which max_muxing_queue_size is taken into account", "bytes" },
  3530. /* data codec support */
  3531. { "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_data_codec },
  3532. "force data codec ('copy' to copy stream)", "codec" },
  3533. { "dn", OPT_BOOL | OPT_VIDEO | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(data_disable) },
  3534. "disable data" },
  3535. #if CONFIG_VAAPI
  3536. { "vaapi_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vaapi_device },
  3537. "set VAAPI hardware device (DRM path or X11 display name)", "device" },
  3538. #endif
  3539. #if CONFIG_QSV
  3540. { "qsv_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_qsv_device },
  3541. "set QSV hardware device (DirectX adapter index, DRM path or X11 display name)", "device"},
  3542. #endif
  3543. { "init_hw_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_init_hw_device },
  3544. "initialise hardware device", "args" },
  3545. { "filter_hw_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_filter_hw_device },
  3546. "set hardware device used when filtering", "device" },
  3547. { NULL, },
  3548. };