ffprobe.c 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824
  1. /*
  2. * Copyright (c) 2007-2010 Stefano Sabatini
  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. /**
  21. * @file
  22. * simple media prober based on the FFmpeg libraries
  23. */
  24. #include "config.h"
  25. #include "libavutil/ffversion.h"
  26. #include <string.h>
  27. #include <math.h>
  28. #include "libavformat/avformat.h"
  29. #include "libavformat/version.h"
  30. #include "libavcodec/avcodec.h"
  31. #include "libavcodec/version.h"
  32. #include "libavutil/ambient_viewing_environment.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/bprint.h"
  36. #include "libavutil/channel_layout.h"
  37. #include "libavutil/display.h"
  38. #include "libavutil/film_grain_params.h"
  39. #include "libavutil/hash.h"
  40. #include "libavutil/hdr_dynamic_metadata.h"
  41. #include "libavutil/iamf.h"
  42. #include "libavutil/mastering_display_metadata.h"
  43. #include "libavutil/hdr_dynamic_vivid_metadata.h"
  44. #include "libavutil/dovi_meta.h"
  45. #include "libavutil/mem.h"
  46. #include "libavutil/opt.h"
  47. #include "libavutil/pixdesc.h"
  48. #include "libavutil/spherical.h"
  49. #include "libavutil/stereo3d.h"
  50. #include "libavutil/dict.h"
  51. #include "libavutil/intreadwrite.h"
  52. #include "libavutil/libm.h"
  53. #include "libavutil/parseutils.h"
  54. #include "libavutil/timecode.h"
  55. #include "libavutil/timestamp.h"
  56. #include "libavdevice/avdevice.h"
  57. #include "libavdevice/version.h"
  58. #include "libswscale/swscale.h"
  59. #include "libswscale/version.h"
  60. #include "libswresample/swresample.h"
  61. #include "libswresample/version.h"
  62. #include "libpostproc/postprocess.h"
  63. #include "libpostproc/version.h"
  64. #include "libavfilter/version.h"
  65. #include "cmdutils.h"
  66. #include "opt_common.h"
  67. #include "libavutil/thread.h"
  68. #if !HAVE_THREADS
  69. # ifdef pthread_mutex_lock
  70. # undef pthread_mutex_lock
  71. # endif
  72. # define pthread_mutex_lock(a) do{}while(0)
  73. # ifdef pthread_mutex_unlock
  74. # undef pthread_mutex_unlock
  75. # endif
  76. # define pthread_mutex_unlock(a) do{}while(0)
  77. #endif
  78. // attached as opaque_ref to packets/frames
  79. typedef struct FrameData {
  80. int64_t pkt_pos;
  81. int pkt_size;
  82. } FrameData;
  83. typedef struct InputStream {
  84. AVStream *st;
  85. AVCodecContext *dec_ctx;
  86. } InputStream;
  87. typedef struct InputFile {
  88. AVFormatContext *fmt_ctx;
  89. InputStream *streams;
  90. int nb_streams;
  91. } InputFile;
  92. const char program_name[] = "ffprobe";
  93. const int program_birth_year = 2007;
  94. static int do_analyze_frames = 0;
  95. static int do_bitexact = 0;
  96. static int do_count_frames = 0;
  97. static int do_count_packets = 0;
  98. static int do_read_frames = 0;
  99. static int do_read_packets = 0;
  100. static int do_show_chapters = 0;
  101. static int do_show_error = 0;
  102. static int do_show_format = 0;
  103. static int do_show_frames = 0;
  104. static int do_show_packets = 0;
  105. static int do_show_programs = 0;
  106. static int do_show_stream_groups = 0;
  107. static int do_show_stream_group_components = 0;
  108. static int do_show_streams = 0;
  109. static int do_show_stream_disposition = 0;
  110. static int do_show_stream_group_disposition = 0;
  111. static int do_show_data = 0;
  112. static int do_show_program_version = 0;
  113. static int do_show_library_versions = 0;
  114. static int do_show_pixel_formats = 0;
  115. static int do_show_pixel_format_flags = 0;
  116. static int do_show_pixel_format_components = 0;
  117. static int do_show_log = 0;
  118. static int do_show_chapter_tags = 0;
  119. static int do_show_format_tags = 0;
  120. static int do_show_frame_tags = 0;
  121. static int do_show_program_tags = 0;
  122. static int do_show_stream_group_tags = 0;
  123. static int do_show_stream_tags = 0;
  124. static int do_show_packet_tags = 0;
  125. static int show_value_unit = 0;
  126. static int use_value_prefix = 0;
  127. static int use_byte_value_binary_prefix = 0;
  128. static int use_value_sexagesimal_format = 0;
  129. static int show_private_data = 1;
  130. #define SHOW_OPTIONAL_FIELDS_AUTO -1
  131. #define SHOW_OPTIONAL_FIELDS_NEVER 0
  132. #define SHOW_OPTIONAL_FIELDS_ALWAYS 1
  133. static int show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
  134. static char *output_format;
  135. static char *stream_specifier;
  136. static char *show_data_hash;
  137. typedef struct ReadInterval {
  138. int id; ///< identifier
  139. int64_t start, end; ///< start, end in second/AV_TIME_BASE units
  140. int has_start, has_end;
  141. int start_is_offset, end_is_offset;
  142. int duration_frames;
  143. } ReadInterval;
  144. static ReadInterval *read_intervals;
  145. static int read_intervals_nb = 0;
  146. static int find_stream_info = 1;
  147. /* section structure definition */
  148. #define SECTION_MAX_NB_CHILDREN 11
  149. typedef enum {
  150. SECTION_ID_NONE = -1,
  151. SECTION_ID_CHAPTER,
  152. SECTION_ID_CHAPTER_TAGS,
  153. SECTION_ID_CHAPTERS,
  154. SECTION_ID_ERROR,
  155. SECTION_ID_FORMAT,
  156. SECTION_ID_FORMAT_TAGS,
  157. SECTION_ID_FRAME,
  158. SECTION_ID_FRAMES,
  159. SECTION_ID_FRAME_TAGS,
  160. SECTION_ID_FRAME_SIDE_DATA_LIST,
  161. SECTION_ID_FRAME_SIDE_DATA,
  162. SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST,
  163. SECTION_ID_FRAME_SIDE_DATA_TIMECODE,
  164. SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST,
  165. SECTION_ID_FRAME_SIDE_DATA_COMPONENT,
  166. SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST,
  167. SECTION_ID_FRAME_SIDE_DATA_PIECE,
  168. SECTION_ID_FRAME_LOG,
  169. SECTION_ID_FRAME_LOGS,
  170. SECTION_ID_LIBRARY_VERSION,
  171. SECTION_ID_LIBRARY_VERSIONS,
  172. SECTION_ID_PACKET,
  173. SECTION_ID_PACKET_TAGS,
  174. SECTION_ID_PACKETS,
  175. SECTION_ID_PACKETS_AND_FRAMES,
  176. SECTION_ID_PACKET_SIDE_DATA_LIST,
  177. SECTION_ID_PACKET_SIDE_DATA,
  178. SECTION_ID_PIXEL_FORMAT,
  179. SECTION_ID_PIXEL_FORMAT_FLAGS,
  180. SECTION_ID_PIXEL_FORMAT_COMPONENT,
  181. SECTION_ID_PIXEL_FORMAT_COMPONENTS,
  182. SECTION_ID_PIXEL_FORMATS,
  183. SECTION_ID_PROGRAM_STREAM_DISPOSITION,
  184. SECTION_ID_PROGRAM_STREAM_TAGS,
  185. SECTION_ID_PROGRAM,
  186. SECTION_ID_PROGRAM_STREAMS,
  187. SECTION_ID_PROGRAM_STREAM,
  188. SECTION_ID_PROGRAM_TAGS,
  189. SECTION_ID_PROGRAM_VERSION,
  190. SECTION_ID_PROGRAMS,
  191. SECTION_ID_STREAM_GROUP_STREAM_DISPOSITION,
  192. SECTION_ID_STREAM_GROUP_STREAM_TAGS,
  193. SECTION_ID_STREAM_GROUP,
  194. SECTION_ID_STREAM_GROUP_COMPONENTS,
  195. SECTION_ID_STREAM_GROUP_COMPONENT,
  196. SECTION_ID_STREAM_GROUP_SUBCOMPONENTS,
  197. SECTION_ID_STREAM_GROUP_SUBCOMPONENT,
  198. SECTION_ID_STREAM_GROUP_PIECES,
  199. SECTION_ID_STREAM_GROUP_PIECE,
  200. SECTION_ID_STREAM_GROUP_SUBPIECES,
  201. SECTION_ID_STREAM_GROUP_SUBPIECE,
  202. SECTION_ID_STREAM_GROUP_BLOCKS,
  203. SECTION_ID_STREAM_GROUP_BLOCK,
  204. SECTION_ID_STREAM_GROUP_STREAMS,
  205. SECTION_ID_STREAM_GROUP_STREAM,
  206. SECTION_ID_STREAM_GROUP_DISPOSITION,
  207. SECTION_ID_STREAM_GROUP_TAGS,
  208. SECTION_ID_STREAM_GROUPS,
  209. SECTION_ID_ROOT,
  210. SECTION_ID_STREAM,
  211. SECTION_ID_STREAM_DISPOSITION,
  212. SECTION_ID_STREAMS,
  213. SECTION_ID_STREAM_TAGS,
  214. SECTION_ID_STREAM_SIDE_DATA_LIST,
  215. SECTION_ID_STREAM_SIDE_DATA,
  216. SECTION_ID_SUBTITLE,
  217. } SectionID;
  218. struct section {
  219. int id; ///< unique id identifying a section
  220. const char *name;
  221. #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
  222. #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
  223. #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
  224. /// For these sections the element_name field is mandatory.
  225. #define SECTION_FLAG_HAS_TYPE 8 ///< the section contains a type to distinguish multiple nested elements
  226. int flags;
  227. const SectionID children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
  228. const char *element_name; ///< name of the contained element, if provided
  229. const char *unique_name; ///< unique section name, in case the name is ambiguous
  230. AVDictionary *entries_to_show;
  231. const char *(* get_type)(const void *data); ///< function returning a type if defined, must be defined when SECTION_FLAG_HAS_TYPE is defined
  232. int show_all_entries;
  233. };
  234. static const char *get_packet_side_data_type(const void *data)
  235. {
  236. const AVPacketSideData *sd = (const AVPacketSideData *)data;
  237. return av_x_if_null(av_packet_side_data_name(sd->type), "unknown");
  238. }
  239. static const char *get_frame_side_data_type(const void *data)
  240. {
  241. const AVFrameSideData *sd = (const AVFrameSideData *)data;
  242. return av_x_if_null(av_frame_side_data_name(sd->type), "unknown");
  243. }
  244. static const char *get_raw_string_type(const void *data)
  245. {
  246. return data;
  247. }
  248. static const char *get_stream_group_type(const void *data)
  249. {
  250. const AVStreamGroup *stg = (const AVStreamGroup *)data;
  251. return av_x_if_null(avformat_stream_group_name(stg->type), "unknown");
  252. }
  253. static struct section sections[] = {
  254. [SECTION_ID_CHAPTERS] = { SECTION_ID_CHAPTERS, "chapters", SECTION_FLAG_IS_ARRAY, { SECTION_ID_CHAPTER, -1 } },
  255. [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
  256. [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
  257. [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
  258. [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
  259. [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
  260. [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, SECTION_ID_SUBTITLE, -1 } },
  261. [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, SECTION_ID_FRAME_SIDE_DATA_LIST, SECTION_ID_FRAME_LOGS, -1 } },
  262. [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
  263. [SECTION_ID_FRAME_SIDE_DATA_LIST] ={ SECTION_ID_FRAME_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA, -1 }, .element_name = "side_data", .unique_name = "frame_side_data_list" },
  264. [SECTION_ID_FRAME_SIDE_DATA] = { SECTION_ID_FRAME_SIDE_DATA, "side_data", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST, -1 }, .unique_name = "frame_side_data", .element_name = "side_datum", .get_type = get_frame_side_data_type },
  265. [SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST, "timecodes", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, -1 } },
  266. [SECTION_ID_FRAME_SIDE_DATA_TIMECODE] = { SECTION_ID_FRAME_SIDE_DATA_TIMECODE, "timecode", 0, { -1 } },
  267. [SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST] = { SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST, "components", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_COMPONENT, -1 }, .element_name = "component", .unique_name = "frame_side_data_components" },
  268. [SECTION_ID_FRAME_SIDE_DATA_COMPONENT] = { SECTION_ID_FRAME_SIDE_DATA_COMPONENT, "component", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST, -1 }, .unique_name = "frame_side_data_component", .element_name = "component_entry", .get_type = get_raw_string_type },
  269. [SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST] = { SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST, "pieces", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA_PIECE, -1 }, .element_name = "piece", .unique_name = "frame_side_data_pieces" },
  270. [SECTION_ID_FRAME_SIDE_DATA_PIECE] = { SECTION_ID_FRAME_SIDE_DATA_PIECE, "piece", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { -1 }, .element_name = "piece_entry", .unique_name = "frame_side_data_piece", .get_type = get_raw_string_type },
  271. [SECTION_ID_FRAME_LOGS] = { SECTION_ID_FRAME_LOGS, "logs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_LOG, -1 } },
  272. [SECTION_ID_FRAME_LOG] = { SECTION_ID_FRAME_LOG, "log", 0, { -1 }, },
  273. [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
  274. [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
  275. [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
  276. [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
  277. [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { SECTION_ID_PACKET_TAGS, SECTION_ID_PACKET_SIDE_DATA_LIST, -1 } },
  278. [SECTION_ID_PACKET_TAGS] = { SECTION_ID_PACKET_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "packet_tags" },
  279. [SECTION_ID_PACKET_SIDE_DATA_LIST] ={ SECTION_ID_PACKET_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET_SIDE_DATA, -1 }, .element_name = "side_data", .unique_name = "packet_side_data_list" },
  280. [SECTION_ID_PACKET_SIDE_DATA] = { SECTION_ID_PACKET_SIDE_DATA, "side_data", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { -1 }, .unique_name = "packet_side_data", .element_name = "side_datum", .get_type = get_packet_side_data_type },
  281. [SECTION_ID_PIXEL_FORMATS] = { SECTION_ID_PIXEL_FORMATS, "pixel_formats", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PIXEL_FORMAT, -1 } },
  282. [SECTION_ID_PIXEL_FORMAT] = { SECTION_ID_PIXEL_FORMAT, "pixel_format", 0, { SECTION_ID_PIXEL_FORMAT_FLAGS, SECTION_ID_PIXEL_FORMAT_COMPONENTS, -1 } },
  283. [SECTION_ID_PIXEL_FORMAT_FLAGS] = { SECTION_ID_PIXEL_FORMAT_FLAGS, "flags", 0, { -1 }, .unique_name = "pixel_format_flags" },
  284. [SECTION_ID_PIXEL_FORMAT_COMPONENTS] = { SECTION_ID_PIXEL_FORMAT_COMPONENTS, "components", SECTION_FLAG_IS_ARRAY, {SECTION_ID_PIXEL_FORMAT_COMPONENT, -1 }, .unique_name = "pixel_format_components" },
  285. [SECTION_ID_PIXEL_FORMAT_COMPONENT] = { SECTION_ID_PIXEL_FORMAT_COMPONENT, "component", 0, { -1 } },
  286. [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
  287. [SECTION_ID_PROGRAM_STREAM_TAGS] = { SECTION_ID_PROGRAM_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_stream_tags" },
  288. [SECTION_ID_PROGRAM] = { SECTION_ID_PROGRAM, "program", 0, { SECTION_ID_PROGRAM_TAGS, SECTION_ID_PROGRAM_STREAMS, -1 } },
  289. [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
  290. [SECTION_ID_PROGRAM_STREAM] = { SECTION_ID_PROGRAM_STREAM, "stream", 0, { SECTION_ID_PROGRAM_STREAM_DISPOSITION, SECTION_ID_PROGRAM_STREAM_TAGS, -1 }, .unique_name = "program_stream" },
  291. [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
  292. [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
  293. [SECTION_ID_PROGRAMS] = { SECTION_ID_PROGRAMS, "programs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM, -1 } },
  294. [SECTION_ID_STREAM_GROUP_STREAM_DISPOSITION] = { SECTION_ID_STREAM_GROUP_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_group_stream_disposition" },
  295. [SECTION_ID_STREAM_GROUP_STREAM_TAGS] = { SECTION_ID_STREAM_GROUP_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_group_stream_tags" },
  296. [SECTION_ID_STREAM_GROUP] = { SECTION_ID_STREAM_GROUP, "stream_group", 0, { SECTION_ID_STREAM_GROUP_TAGS, SECTION_ID_STREAM_GROUP_DISPOSITION, SECTION_ID_STREAM_GROUP_COMPONENTS, SECTION_ID_STREAM_GROUP_STREAMS, -1 } },
  297. [SECTION_ID_STREAM_GROUP_COMPONENTS] = { SECTION_ID_STREAM_GROUP_COMPONENTS, "components", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_GROUP_COMPONENT, -1 }, .element_name = "component", .unique_name = "stream_group_components" },
  298. [SECTION_ID_STREAM_GROUP_COMPONENT] = { SECTION_ID_STREAM_GROUP_COMPONENT, "component", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { SECTION_ID_STREAM_GROUP_SUBCOMPONENTS, -1 }, .unique_name = "stream_group_component", .element_name = "component_entry", .get_type = get_stream_group_type },
  299. [SECTION_ID_STREAM_GROUP_SUBCOMPONENTS] = { SECTION_ID_STREAM_GROUP_SUBCOMPONENTS, "subcomponents", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_GROUP_SUBCOMPONENT, -1 }, .element_name = "component" },
  300. [SECTION_ID_STREAM_GROUP_SUBCOMPONENT] = { SECTION_ID_STREAM_GROUP_SUBCOMPONENT, "subcomponent", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { SECTION_ID_STREAM_GROUP_PIECES, -1 }, .element_name = "subcomponent_entry", .get_type = get_raw_string_type },
  301. [SECTION_ID_STREAM_GROUP_PIECES] = { SECTION_ID_STREAM_GROUP_PIECES, "pieces", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_GROUP_PIECE, -1 }, .element_name = "piece", .unique_name = "stream_group_pieces" },
  302. [SECTION_ID_STREAM_GROUP_PIECE] = { SECTION_ID_STREAM_GROUP_PIECE, "piece", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { SECTION_ID_STREAM_GROUP_SUBPIECES, -1 }, .unique_name = "stream_group_piece", .element_name = "piece_entry", .get_type = get_raw_string_type },
  303. [SECTION_ID_STREAM_GROUP_SUBPIECES] = { SECTION_ID_STREAM_GROUP_SUBPIECES, "subpieces", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_GROUP_SUBPIECE, -1 }, .element_name = "subpiece" },
  304. [SECTION_ID_STREAM_GROUP_SUBPIECE] = { SECTION_ID_STREAM_GROUP_SUBPIECE, "subpiece", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { SECTION_ID_STREAM_GROUP_BLOCKS, -1 }, .element_name = "subpiece_entry", .get_type = get_raw_string_type },
  305. [SECTION_ID_STREAM_GROUP_BLOCKS] = { SECTION_ID_STREAM_GROUP_BLOCKS, "blocks", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_GROUP_BLOCK, -1 }, .element_name = "block" },
  306. [SECTION_ID_STREAM_GROUP_BLOCK] = { SECTION_ID_STREAM_GROUP_BLOCK, "block", SECTION_FLAG_HAS_VARIABLE_FIELDS|SECTION_FLAG_HAS_TYPE, { -1 }, .element_name = "block_entry", .get_type = get_raw_string_type },
  307. [SECTION_ID_STREAM_GROUP_STREAMS] = { SECTION_ID_STREAM_GROUP_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_GROUP_STREAM, -1 }, .unique_name = "stream_group_streams" },
  308. [SECTION_ID_STREAM_GROUP_STREAM] = { SECTION_ID_STREAM_GROUP_STREAM, "stream", 0, { SECTION_ID_STREAM_GROUP_STREAM_DISPOSITION, SECTION_ID_STREAM_GROUP_STREAM_TAGS, -1 }, .unique_name = "stream_group_stream" },
  309. [SECTION_ID_STREAM_GROUP_DISPOSITION] = { SECTION_ID_STREAM_GROUP_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_group_disposition" },
  310. [SECTION_ID_STREAM_GROUP_TAGS] = { SECTION_ID_STREAM_GROUP_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_group_tags" },
  311. [SECTION_ID_STREAM_GROUPS] = { SECTION_ID_STREAM_GROUPS, "stream_groups", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_GROUP, -1 } },
  312. [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
  313. { SECTION_ID_CHAPTERS, SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_PROGRAMS, SECTION_ID_STREAM_GROUPS, SECTION_ID_STREAMS,
  314. SECTION_ID_PACKETS, SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS,
  315. SECTION_ID_PIXEL_FORMATS, -1} },
  316. [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
  317. [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, SECTION_ID_STREAM_SIDE_DATA_LIST, -1 } },
  318. [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
  319. [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
  320. [SECTION_ID_STREAM_SIDE_DATA_LIST] ={ SECTION_ID_STREAM_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_SIDE_DATA, -1 }, .element_name = "side_data", .unique_name = "stream_side_data_list" },
  321. [SECTION_ID_STREAM_SIDE_DATA] = { SECTION_ID_STREAM_SIDE_DATA, "side_data", SECTION_FLAG_HAS_TYPE|SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .unique_name = "stream_side_data", .element_name = "side_datum", .get_type = get_packet_side_data_type },
  322. [SECTION_ID_SUBTITLE] = { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
  323. };
  324. static const OptionDef *options;
  325. /* FFprobe context */
  326. static const char *input_filename;
  327. static const char *print_input_filename;
  328. static const AVInputFormat *iformat = NULL;
  329. static const char *output_filename = NULL;
  330. static struct AVHashContext *hash;
  331. static const struct {
  332. double bin_val;
  333. double dec_val;
  334. const char *bin_str;
  335. const char *dec_str;
  336. } si_prefixes[] = {
  337. { 1.0, 1.0, "", "" },
  338. { 1.024e3, 1e3, "Ki", "K" },
  339. { 1.048576e6, 1e6, "Mi", "M" },
  340. { 1.073741824e9, 1e9, "Gi", "G" },
  341. { 1.099511627776e12, 1e12, "Ti", "T" },
  342. { 1.125899906842624e15, 1e15, "Pi", "P" },
  343. };
  344. static const char unit_second_str[] = "s" ;
  345. static const char unit_hertz_str[] = "Hz" ;
  346. static const char unit_byte_str[] = "byte" ;
  347. static const char unit_bit_per_second_str[] = "bit/s";
  348. static int nb_streams;
  349. static uint64_t *nb_streams_packets;
  350. static uint64_t *nb_streams_frames;
  351. static int *selected_streams;
  352. static int *streams_with_closed_captions;
  353. static int *streams_with_film_grain;
  354. #if HAVE_THREADS
  355. pthread_mutex_t log_mutex;
  356. #endif
  357. typedef struct LogBuffer {
  358. char *context_name;
  359. int log_level;
  360. char *log_message;
  361. AVClassCategory category;
  362. char *parent_name;
  363. AVClassCategory parent_category;
  364. }LogBuffer;
  365. static LogBuffer *log_buffer;
  366. static int log_buffer_size;
  367. static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
  368. {
  369. AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
  370. va_list vl2;
  371. char line[1024];
  372. static int print_prefix = 1;
  373. void *new_log_buffer;
  374. va_copy(vl2, vl);
  375. av_log_default_callback(ptr, level, fmt, vl);
  376. av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
  377. va_end(vl2);
  378. #if HAVE_THREADS
  379. pthread_mutex_lock(&log_mutex);
  380. new_log_buffer = av_realloc_array(log_buffer, log_buffer_size + 1, sizeof(*log_buffer));
  381. if (new_log_buffer) {
  382. char *msg;
  383. int i;
  384. log_buffer = new_log_buffer;
  385. memset(&log_buffer[log_buffer_size], 0, sizeof(log_buffer[log_buffer_size]));
  386. log_buffer[log_buffer_size].context_name= avc ? av_strdup(avc->item_name(ptr)) : NULL;
  387. if (avc) {
  388. if (avc->get_category) log_buffer[log_buffer_size].category = avc->get_category(ptr);
  389. else log_buffer[log_buffer_size].category = avc->category;
  390. }
  391. log_buffer[log_buffer_size].log_level = level;
  392. msg = log_buffer[log_buffer_size].log_message = av_strdup(line);
  393. for (i=strlen(msg) - 1; i>=0 && msg[i] == '\n'; i--) {
  394. msg[i] = 0;
  395. }
  396. if (avc && avc->parent_log_context_offset) {
  397. AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
  398. avc->parent_log_context_offset);
  399. if (parent && *parent) {
  400. log_buffer[log_buffer_size].parent_name = av_strdup((*parent)->item_name(parent));
  401. log_buffer[log_buffer_size].parent_category =
  402. (*parent)->get_category ? (*parent)->get_category(parent) :(*parent)->category;
  403. }
  404. }
  405. log_buffer_size ++;
  406. }
  407. pthread_mutex_unlock(&log_mutex);
  408. #endif
  409. }
  410. struct unit_value {
  411. union { double d; int64_t i; } val;
  412. const char *unit;
  413. };
  414. static char *value_string(char *buf, int buf_size, struct unit_value uv)
  415. {
  416. double vald;
  417. int64_t vali;
  418. int show_float = 0;
  419. if (uv.unit == unit_second_str) {
  420. vald = uv.val.d;
  421. show_float = 1;
  422. } else {
  423. vald = vali = uv.val.i;
  424. }
  425. if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
  426. double secs;
  427. int hours, mins;
  428. secs = vald;
  429. mins = (int)secs / 60;
  430. secs = secs - mins * 60;
  431. hours = mins / 60;
  432. mins %= 60;
  433. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  434. } else {
  435. const char *prefix_string = "";
  436. if (use_value_prefix && vald > 1) {
  437. int64_t index;
  438. if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
  439. index = (int64_t) (log2(vald)) / 10;
  440. index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
  441. vald /= si_prefixes[index].bin_val;
  442. prefix_string = si_prefixes[index].bin_str;
  443. } else {
  444. index = (int64_t) (log10(vald)) / 3;
  445. index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
  446. vald /= si_prefixes[index].dec_val;
  447. prefix_string = si_prefixes[index].dec_str;
  448. }
  449. vali = vald;
  450. }
  451. if (show_float || (use_value_prefix && vald != (int64_t)vald))
  452. snprintf(buf, buf_size, "%f", vald);
  453. else
  454. snprintf(buf, buf_size, "%"PRId64, vali);
  455. av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
  456. prefix_string, show_value_unit ? uv.unit : "");
  457. }
  458. return buf;
  459. }
  460. /* WRITERS API */
  461. typedef struct WriterContext WriterContext;
  462. #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
  463. #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
  464. typedef enum {
  465. WRITER_STRING_VALIDATION_FAIL,
  466. WRITER_STRING_VALIDATION_REPLACE,
  467. WRITER_STRING_VALIDATION_IGNORE,
  468. WRITER_STRING_VALIDATION_NB
  469. } StringValidation;
  470. typedef struct Writer {
  471. const AVClass *priv_class; ///< private class of the writer, if any
  472. int priv_size; ///< private size for the writer context
  473. const char *name;
  474. int (*init) (WriterContext *wctx);
  475. void (*uninit)(WriterContext *wctx);
  476. void (*print_section_header)(WriterContext *wctx, const void *data);
  477. void (*print_section_footer)(WriterContext *wctx);
  478. void (*print_integer) (WriterContext *wctx, const char *, int64_t);
  479. void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
  480. void (*print_string) (WriterContext *wctx, const char *, const char *);
  481. int flags; ///< a combination or WRITER_FLAG_*
  482. } Writer;
  483. #define SECTION_MAX_NB_LEVELS 12
  484. struct WriterContext {
  485. const AVClass *class; ///< class of the writer
  486. const Writer *writer; ///< the Writer of which this is an instance
  487. AVIOContext *avio; ///< the I/O context used to write
  488. void (* writer_w8)(WriterContext *wctx, int b);
  489. void (* writer_put_str)(WriterContext *wctx, const char *str);
  490. void (* writer_printf)(WriterContext *wctx, const char *fmt, ...);
  491. char *name; ///< name of this writer instance
  492. void *priv; ///< private data for use by the filter
  493. const struct section *sections; ///< array containing all sections
  494. int nb_sections; ///< number of sections
  495. int level; ///< current level, starting from 0
  496. /** number of the item printed in the given section, starting from 0 */
  497. unsigned int nb_item[SECTION_MAX_NB_LEVELS];
  498. /** section per each level */
  499. const struct section *section[SECTION_MAX_NB_LEVELS];
  500. AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
  501. /// used by various writers
  502. unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
  503. unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
  504. unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
  505. int string_validation;
  506. char *string_validation_replacement;
  507. unsigned int string_validation_utf8_flags;
  508. };
  509. static const char *writer_get_name(void *p)
  510. {
  511. WriterContext *wctx = p;
  512. return wctx->writer->name;
  513. }
  514. #define OFFSET(x) offsetof(WriterContext, x)
  515. static const AVOption writer_options[] = {
  516. { "string_validation", "set string validation mode",
  517. OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
  518. { "sv", "set string validation mode",
  519. OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
  520. { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
  521. { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
  522. { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
  523. { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
  524. { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str="\xEF\xBF\xBD"}},
  525. { NULL }
  526. };
  527. static void *writer_child_next(void *obj, void *prev)
  528. {
  529. WriterContext *ctx = obj;
  530. if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
  531. return ctx->priv;
  532. return NULL;
  533. }
  534. static const AVClass writer_class = {
  535. .class_name = "Writer",
  536. .item_name = writer_get_name,
  537. .option = writer_options,
  538. .version = LIBAVUTIL_VERSION_INT,
  539. .child_next = writer_child_next,
  540. };
  541. static int writer_close(WriterContext **wctx)
  542. {
  543. int i;
  544. int ret = 0;
  545. if (!*wctx)
  546. return -1;
  547. if ((*wctx)->writer->uninit)
  548. (*wctx)->writer->uninit(*wctx);
  549. for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
  550. av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
  551. if ((*wctx)->writer->priv_class)
  552. av_opt_free((*wctx)->priv);
  553. av_freep(&((*wctx)->priv));
  554. av_opt_free(*wctx);
  555. if ((*wctx)->avio) {
  556. avio_flush((*wctx)->avio);
  557. ret = avio_close((*wctx)->avio);
  558. }
  559. av_freep(wctx);
  560. return ret;
  561. }
  562. static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
  563. {
  564. int i;
  565. av_bprintf(bp, "0X");
  566. for (i = 0; i < ubuf_size; i++)
  567. av_bprintf(bp, "%02X", ubuf[i]);
  568. }
  569. static inline void writer_w8_avio(WriterContext *wctx, int b)
  570. {
  571. avio_w8(wctx->avio, b);
  572. }
  573. static inline void writer_put_str_avio(WriterContext *wctx, const char *str)
  574. {
  575. avio_write(wctx->avio, str, strlen(str));
  576. }
  577. static inline void writer_printf_avio(WriterContext *wctx, const char *fmt, ...)
  578. {
  579. va_list ap;
  580. va_start(ap, fmt);
  581. avio_vprintf(wctx->avio, fmt, ap);
  582. va_end(ap);
  583. }
  584. static inline void writer_w8_printf(WriterContext *wctx, int b)
  585. {
  586. printf("%c", b);
  587. }
  588. static inline void writer_put_str_printf(WriterContext *wctx, const char *str)
  589. {
  590. printf("%s", str);
  591. }
  592. static inline void writer_printf_printf(WriterContext *wctx, const char *fmt, ...)
  593. {
  594. va_list ap;
  595. va_start(ap, fmt);
  596. vprintf(fmt, ap);
  597. va_end(ap);
  598. }
  599. static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
  600. const struct section *sections, int nb_sections, const char *output)
  601. {
  602. int i, ret = 0;
  603. if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
  604. ret = AVERROR(ENOMEM);
  605. goto fail;
  606. }
  607. if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
  608. ret = AVERROR(ENOMEM);
  609. goto fail;
  610. }
  611. (*wctx)->class = &writer_class;
  612. (*wctx)->writer = writer;
  613. (*wctx)->level = -1;
  614. (*wctx)->sections = sections;
  615. (*wctx)->nb_sections = nb_sections;
  616. av_opt_set_defaults(*wctx);
  617. if (writer->priv_class) {
  618. void *priv_ctx = (*wctx)->priv;
  619. *((const AVClass **)priv_ctx) = writer->priv_class;
  620. av_opt_set_defaults(priv_ctx);
  621. }
  622. /* convert options to dictionary */
  623. if (args) {
  624. AVDictionary *opts = NULL;
  625. const AVDictionaryEntry *opt = NULL;
  626. if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
  627. av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
  628. av_dict_free(&opts);
  629. goto fail;
  630. }
  631. while ((opt = av_dict_iterate(opts, opt))) {
  632. if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
  633. av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
  634. opt->key, opt->value);
  635. av_dict_free(&opts);
  636. goto fail;
  637. }
  638. }
  639. av_dict_free(&opts);
  640. }
  641. /* validate replace string */
  642. {
  643. const uint8_t *p = (*wctx)->string_validation_replacement;
  644. const uint8_t *endp = p + strlen(p);
  645. while (*p) {
  646. const uint8_t *p0 = p;
  647. int32_t code;
  648. ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
  649. if (ret < 0) {
  650. AVBPrint bp;
  651. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
  652. bprint_bytes(&bp, p0, p-p0),
  653. av_log(wctx, AV_LOG_ERROR,
  654. "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
  655. bp.str, (*wctx)->string_validation_replacement);
  656. return ret;
  657. }
  658. }
  659. }
  660. if (!output_filename) {
  661. (*wctx)->writer_w8 = writer_w8_printf;
  662. (*wctx)->writer_put_str = writer_put_str_printf;
  663. (*wctx)->writer_printf = writer_printf_printf;
  664. } else {
  665. if ((ret = avio_open(&(*wctx)->avio, output, AVIO_FLAG_WRITE)) < 0) {
  666. av_log(*wctx, AV_LOG_ERROR,
  667. "Failed to open output '%s' with error: %s\n", output, av_err2str(ret));
  668. goto fail;
  669. }
  670. (*wctx)->writer_w8 = writer_w8_avio;
  671. (*wctx)->writer_put_str = writer_put_str_avio;
  672. (*wctx)->writer_printf = writer_printf_avio;
  673. }
  674. for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
  675. av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
  676. if ((*wctx)->writer->init)
  677. ret = (*wctx)->writer->init(*wctx);
  678. if (ret < 0)
  679. goto fail;
  680. return 0;
  681. fail:
  682. writer_close(wctx);
  683. return ret;
  684. }
  685. static inline void writer_print_section_header(WriterContext *wctx,
  686. const void *data,
  687. int section_id)
  688. {
  689. int parent_section_id;
  690. wctx->level++;
  691. av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
  692. parent_section_id = wctx->level ?
  693. (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
  694. wctx->nb_item[wctx->level] = 0;
  695. wctx->section[wctx->level] = &wctx->sections[section_id];
  696. if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
  697. wctx->nb_section_packet = wctx->nb_section_frame =
  698. wctx->nb_section_packet_frame = 0;
  699. } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
  700. wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
  701. wctx->nb_section_packet : wctx->nb_section_frame;
  702. }
  703. if (wctx->writer->print_section_header)
  704. wctx->writer->print_section_header(wctx, data);
  705. }
  706. static inline void writer_print_section_footer(WriterContext *wctx)
  707. {
  708. int section_id = wctx->section[wctx->level]->id;
  709. int parent_section_id = wctx->level ?
  710. wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
  711. if (parent_section_id != SECTION_ID_NONE)
  712. wctx->nb_item[wctx->level-1]++;
  713. if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
  714. if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
  715. else wctx->nb_section_frame++;
  716. }
  717. if (wctx->writer->print_section_footer)
  718. wctx->writer->print_section_footer(wctx);
  719. wctx->level--;
  720. }
  721. static inline void writer_print_integer(WriterContext *wctx,
  722. const char *key, int64_t val)
  723. {
  724. const struct section *section = wctx->section[wctx->level];
  725. if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
  726. wctx->writer->print_integer(wctx, key, val);
  727. wctx->nb_item[wctx->level]++;
  728. }
  729. }
  730. static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
  731. {
  732. const uint8_t *p, *endp;
  733. AVBPrint dstbuf;
  734. int invalid_chars_nb = 0, ret = 0;
  735. av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
  736. endp = src + strlen(src);
  737. for (p = src; *p;) {
  738. uint32_t code;
  739. int invalid = 0;
  740. const uint8_t *p0 = p;
  741. if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
  742. AVBPrint bp;
  743. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
  744. bprint_bytes(&bp, p0, p-p0);
  745. av_log(wctx, AV_LOG_DEBUG,
  746. "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
  747. invalid = 1;
  748. }
  749. if (invalid) {
  750. invalid_chars_nb++;
  751. switch (wctx->string_validation) {
  752. case WRITER_STRING_VALIDATION_FAIL:
  753. av_log(wctx, AV_LOG_ERROR,
  754. "Invalid UTF-8 sequence found in string '%s'\n", src);
  755. ret = AVERROR_INVALIDDATA;
  756. goto end;
  757. break;
  758. case WRITER_STRING_VALIDATION_REPLACE:
  759. av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
  760. break;
  761. }
  762. }
  763. if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
  764. av_bprint_append_data(&dstbuf, p0, p-p0);
  765. }
  766. if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
  767. av_log(wctx, AV_LOG_WARNING,
  768. "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
  769. invalid_chars_nb, src, wctx->string_validation_replacement);
  770. }
  771. end:
  772. av_bprint_finalize(&dstbuf, dstp);
  773. return ret;
  774. }
  775. #define PRINT_STRING_OPT 1
  776. #define PRINT_STRING_VALIDATE 2
  777. static inline int writer_print_string(WriterContext *wctx,
  778. const char *key, const char *val, int flags)
  779. {
  780. const struct section *section = wctx->section[wctx->level];
  781. int ret = 0;
  782. if (show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER ||
  783. (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
  784. && (flags & PRINT_STRING_OPT)
  785. && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS)))
  786. return 0;
  787. if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
  788. if (flags & PRINT_STRING_VALIDATE) {
  789. char *key1 = NULL, *val1 = NULL;
  790. ret = validate_string(wctx, &key1, key);
  791. if (ret < 0) goto end;
  792. ret = validate_string(wctx, &val1, val);
  793. if (ret < 0) goto end;
  794. wctx->writer->print_string(wctx, key1, val1);
  795. end:
  796. if (ret < 0) {
  797. av_log(wctx, AV_LOG_ERROR,
  798. "Invalid key=value string combination %s=%s in section %s\n",
  799. key, val, section->unique_name);
  800. }
  801. av_free(key1);
  802. av_free(val1);
  803. } else {
  804. wctx->writer->print_string(wctx, key, val);
  805. }
  806. wctx->nb_item[wctx->level]++;
  807. }
  808. return ret;
  809. }
  810. static inline void writer_print_rational(WriterContext *wctx,
  811. const char *key, AVRational q, char sep)
  812. {
  813. AVBPrint buf;
  814. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
  815. av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
  816. writer_print_string(wctx, key, buf.str, 0);
  817. }
  818. static void writer_print_time(WriterContext *wctx, const char *key,
  819. int64_t ts, const AVRational *time_base, int is_duration)
  820. {
  821. char buf[128];
  822. if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
  823. writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
  824. } else {
  825. double d = ts * av_q2d(*time_base);
  826. struct unit_value uv;
  827. uv.val.d = d;
  828. uv.unit = unit_second_str;
  829. value_string(buf, sizeof(buf), uv);
  830. writer_print_string(wctx, key, buf, 0);
  831. }
  832. }
  833. static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
  834. {
  835. if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
  836. writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
  837. } else {
  838. writer_print_integer(wctx, key, ts);
  839. }
  840. }
  841. static void writer_print_data(WriterContext *wctx, const char *name,
  842. const uint8_t *data, int size)
  843. {
  844. AVBPrint bp;
  845. int offset = 0, l, i;
  846. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
  847. av_bprintf(&bp, "\n");
  848. while (size) {
  849. av_bprintf(&bp, "%08x: ", offset);
  850. l = FFMIN(size, 16);
  851. for (i = 0; i < l; i++) {
  852. av_bprintf(&bp, "%02x", data[i]);
  853. if (i & 1)
  854. av_bprintf(&bp, " ");
  855. }
  856. av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
  857. for (i = 0; i < l; i++)
  858. av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
  859. av_bprintf(&bp, "\n");
  860. offset += l;
  861. data += l;
  862. size -= l;
  863. }
  864. writer_print_string(wctx, name, bp.str, 0);
  865. av_bprint_finalize(&bp, NULL);
  866. }
  867. static void writer_print_data_hash(WriterContext *wctx, const char *name,
  868. const uint8_t *data, int size)
  869. {
  870. char *p, buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
  871. if (!hash)
  872. return;
  873. av_hash_init(hash);
  874. av_hash_update(hash, data, size);
  875. snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(hash));
  876. p = buf + strlen(buf);
  877. av_hash_final_hex(hash, p, buf + sizeof(buf) - p);
  878. writer_print_string(wctx, name, buf, 0);
  879. }
  880. static void writer_print_integers(WriterContext *wctx, const char *name,
  881. uint8_t *data, int size, const char *format,
  882. int columns, int bytes, int offset_add)
  883. {
  884. AVBPrint bp;
  885. int offset = 0, l, i;
  886. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
  887. av_bprintf(&bp, "\n");
  888. while (size) {
  889. av_bprintf(&bp, "%08x: ", offset);
  890. l = FFMIN(size, columns);
  891. for (i = 0; i < l; i++) {
  892. if (bytes == 1) av_bprintf(&bp, format, *data);
  893. else if (bytes == 2) av_bprintf(&bp, format, AV_RN16(data));
  894. else if (bytes == 4) av_bprintf(&bp, format, AV_RN32(data));
  895. data += bytes;
  896. size --;
  897. }
  898. av_bprintf(&bp, "\n");
  899. offset += offset_add;
  900. }
  901. writer_print_string(wctx, name, bp.str, 0);
  902. av_bprint_finalize(&bp, NULL);
  903. }
  904. #define writer_w8(wctx_, b_) (wctx_)->writer_w8(wctx_, b_)
  905. #define writer_put_str(wctx_, str_) (wctx_)->writer_put_str(wctx_, str_)
  906. #define writer_printf(wctx_, fmt_, ...) (wctx_)->writer_printf(wctx_, fmt_, __VA_ARGS__)
  907. #define MAX_REGISTERED_WRITERS_NB 64
  908. static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
  909. static int writer_register(const Writer *writer)
  910. {
  911. static int next_registered_writer_idx = 0;
  912. if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
  913. return AVERROR(ENOMEM);
  914. registered_writers[next_registered_writer_idx++] = writer;
  915. return 0;
  916. }
  917. static const Writer *writer_get_by_name(const char *name)
  918. {
  919. int i;
  920. for (i = 0; registered_writers[i]; i++)
  921. if (!strcmp(registered_writers[i]->name, name))
  922. return registered_writers[i];
  923. return NULL;
  924. }
  925. /* WRITERS */
  926. #define DEFINE_WRITER_CLASS(name) \
  927. static const char *name##_get_name(void *ctx) \
  928. { \
  929. return #name ; \
  930. } \
  931. static const AVClass name##_class = { \
  932. .class_name = #name, \
  933. .item_name = name##_get_name, \
  934. .option = name##_options \
  935. }
  936. /* Default output */
  937. typedef struct DefaultContext {
  938. const AVClass *class;
  939. int nokey;
  940. int noprint_wrappers;
  941. int nested_section[SECTION_MAX_NB_LEVELS];
  942. } DefaultContext;
  943. #undef OFFSET
  944. #define OFFSET(x) offsetof(DefaultContext, x)
  945. static const AVOption default_options[] = {
  946. { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  947. { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  948. { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  949. { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  950. {NULL},
  951. };
  952. DEFINE_WRITER_CLASS(default);
  953. /* lame uppercasing routine, assumes the string is lower case ASCII */
  954. static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
  955. {
  956. int i;
  957. for (i = 0; src[i] && i < dst_size-1; i++)
  958. dst[i] = av_toupper(src[i]);
  959. dst[i] = 0;
  960. return dst;
  961. }
  962. static void default_print_section_header(WriterContext *wctx, const void *data)
  963. {
  964. DefaultContext *def = wctx->priv;
  965. char buf[32];
  966. const struct section *section = wctx->section[wctx->level];
  967. const struct section *parent_section = wctx->level ?
  968. wctx->section[wctx->level-1] : NULL;
  969. av_bprint_clear(&wctx->section_pbuf[wctx->level]);
  970. if (parent_section &&
  971. !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
  972. def->nested_section[wctx->level] = 1;
  973. av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
  974. wctx->section_pbuf[wctx->level-1].str,
  975. upcase_string(buf, sizeof(buf),
  976. av_x_if_null(section->element_name, section->name)));
  977. }
  978. if (def->noprint_wrappers || def->nested_section[wctx->level])
  979. return;
  980. if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
  981. writer_printf(wctx, "[%s]\n", upcase_string(buf, sizeof(buf), section->name));
  982. }
  983. static void default_print_section_footer(WriterContext *wctx)
  984. {
  985. DefaultContext *def = wctx->priv;
  986. const struct section *section = wctx->section[wctx->level];
  987. char buf[32];
  988. if (def->noprint_wrappers || def->nested_section[wctx->level])
  989. return;
  990. if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
  991. writer_printf(wctx, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
  992. }
  993. static void default_print_str(WriterContext *wctx, const char *key, const char *value)
  994. {
  995. DefaultContext *def = wctx->priv;
  996. if (!def->nokey)
  997. writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
  998. writer_printf(wctx, "%s\n", value);
  999. }
  1000. static void default_print_int(WriterContext *wctx, const char *key, int64_t value)
  1001. {
  1002. DefaultContext *def = wctx->priv;
  1003. if (!def->nokey)
  1004. writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
  1005. writer_printf(wctx, "%"PRId64"\n", value);
  1006. }
  1007. static const Writer default_writer = {
  1008. .name = "default",
  1009. .priv_size = sizeof(DefaultContext),
  1010. .print_section_header = default_print_section_header,
  1011. .print_section_footer = default_print_section_footer,
  1012. .print_integer = default_print_int,
  1013. .print_string = default_print_str,
  1014. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  1015. .priv_class = &default_class,
  1016. };
  1017. /* Compact output */
  1018. /**
  1019. * Apply C-language-like string escaping.
  1020. */
  1021. static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  1022. {
  1023. const char *p;
  1024. for (p = src; *p; p++) {
  1025. switch (*p) {
  1026. case '\b': av_bprintf(dst, "%s", "\\b"); break;
  1027. case '\f': av_bprintf(dst, "%s", "\\f"); break;
  1028. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  1029. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  1030. case '\\': av_bprintf(dst, "%s", "\\\\"); break;
  1031. default:
  1032. if (*p == sep)
  1033. av_bprint_chars(dst, '\\', 1);
  1034. av_bprint_chars(dst, *p, 1);
  1035. }
  1036. }
  1037. return dst->str;
  1038. }
  1039. /**
  1040. * Quote fields containing special characters, check RFC4180.
  1041. */
  1042. static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  1043. {
  1044. char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
  1045. int needs_quoting = !!src[strcspn(src, meta_chars)];
  1046. if (needs_quoting)
  1047. av_bprint_chars(dst, '"', 1);
  1048. for (; *src; src++) {
  1049. if (*src == '"')
  1050. av_bprint_chars(dst, '"', 1);
  1051. av_bprint_chars(dst, *src, 1);
  1052. }
  1053. if (needs_quoting)
  1054. av_bprint_chars(dst, '"', 1);
  1055. return dst->str;
  1056. }
  1057. static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  1058. {
  1059. return src;
  1060. }
  1061. typedef struct CompactContext {
  1062. const AVClass *class;
  1063. char *item_sep_str;
  1064. char item_sep;
  1065. int nokey;
  1066. int print_section;
  1067. char *escape_mode_str;
  1068. const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
  1069. int nested_section[SECTION_MAX_NB_LEVELS];
  1070. int has_nested_elems[SECTION_MAX_NB_LEVELS];
  1071. int terminate_line[SECTION_MAX_NB_LEVELS];
  1072. } CompactContext;
  1073. #undef OFFSET
  1074. #define OFFSET(x) offsetof(CompactContext, x)
  1075. static const AVOption compact_options[]= {
  1076. {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
  1077. {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
  1078. {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1079. {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1080. {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
  1081. {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
  1082. {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1083. {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1084. {NULL},
  1085. };
  1086. DEFINE_WRITER_CLASS(compact);
  1087. static av_cold int compact_init(WriterContext *wctx)
  1088. {
  1089. CompactContext *compact = wctx->priv;
  1090. if (strlen(compact->item_sep_str) != 1) {
  1091. av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
  1092. compact->item_sep_str);
  1093. return AVERROR(EINVAL);
  1094. }
  1095. compact->item_sep = compact->item_sep_str[0];
  1096. if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
  1097. else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
  1098. else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
  1099. else {
  1100. av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
  1101. return AVERROR(EINVAL);
  1102. }
  1103. return 0;
  1104. }
  1105. static void compact_print_section_header(WriterContext *wctx, const void *data)
  1106. {
  1107. CompactContext *compact = wctx->priv;
  1108. const struct section *section = wctx->section[wctx->level];
  1109. const struct section *parent_section = wctx->level ?
  1110. wctx->section[wctx->level-1] : NULL;
  1111. compact->terminate_line[wctx->level] = 1;
  1112. compact->has_nested_elems[wctx->level] = 0;
  1113. av_bprint_clear(&wctx->section_pbuf[wctx->level]);
  1114. if (parent_section &&
  1115. (section->flags & SECTION_FLAG_HAS_TYPE ||
  1116. (!(section->flags & SECTION_FLAG_IS_ARRAY) &&
  1117. !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))))) {
  1118. /* define a prefix for elements not contained in an array or
  1119. in a wrapper, or for array elements with a type */
  1120. const char *element_name = (char *)av_x_if_null(section->element_name, section->name);
  1121. AVBPrint *section_pbuf = &wctx->section_pbuf[wctx->level];
  1122. compact->nested_section[wctx->level] = 1;
  1123. compact->has_nested_elems[wctx->level-1] = 1;
  1124. av_bprintf(section_pbuf, "%s%s",
  1125. wctx->section_pbuf[wctx->level-1].str, element_name);
  1126. if (section->flags & SECTION_FLAG_HAS_TYPE) {
  1127. // add /TYPE to prefix
  1128. av_bprint_chars(section_pbuf, '/', 1);
  1129. // normalize section type, replace special characters and lower case
  1130. for (const char *p = section->get_type(data); *p; p++) {
  1131. char c =
  1132. (*p >= '0' && *p <= '9') ||
  1133. (*p >= 'a' && *p <= 'z') ||
  1134. (*p >= 'A' && *p <= 'Z') ? av_tolower(*p) : '_';
  1135. av_bprint_chars(section_pbuf, c, 1);
  1136. }
  1137. }
  1138. av_bprint_chars(section_pbuf, ':', 1);
  1139. wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
  1140. } else {
  1141. if (parent_section && !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)) &&
  1142. wctx->level && wctx->nb_item[wctx->level-1])
  1143. writer_w8(wctx, compact->item_sep);
  1144. if (compact->print_section &&
  1145. !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
  1146. writer_printf(wctx, "%s%c", section->name, compact->item_sep);
  1147. }
  1148. }
  1149. static void compact_print_section_footer(WriterContext *wctx)
  1150. {
  1151. CompactContext *compact = wctx->priv;
  1152. if (!compact->nested_section[wctx->level] &&
  1153. compact->terminate_line[wctx->level] &&
  1154. !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
  1155. writer_w8(wctx, '\n');
  1156. }
  1157. static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
  1158. {
  1159. CompactContext *compact = wctx->priv;
  1160. AVBPrint buf;
  1161. if (wctx->nb_item[wctx->level]) writer_w8(wctx, compact->item_sep);
  1162. if (!compact->nokey)
  1163. writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
  1164. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1165. writer_put_str(wctx, compact->escape_str(&buf, value, compact->item_sep, wctx));
  1166. av_bprint_finalize(&buf, NULL);
  1167. }
  1168. static void compact_print_int(WriterContext *wctx, const char *key, int64_t value)
  1169. {
  1170. CompactContext *compact = wctx->priv;
  1171. if (wctx->nb_item[wctx->level]) writer_w8(wctx, compact->item_sep);
  1172. if (!compact->nokey)
  1173. writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
  1174. writer_printf(wctx, "%"PRId64, value);
  1175. }
  1176. static const Writer compact_writer = {
  1177. .name = "compact",
  1178. .priv_size = sizeof(CompactContext),
  1179. .init = compact_init,
  1180. .print_section_header = compact_print_section_header,
  1181. .print_section_footer = compact_print_section_footer,
  1182. .print_integer = compact_print_int,
  1183. .print_string = compact_print_str,
  1184. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  1185. .priv_class = &compact_class,
  1186. };
  1187. /* CSV output */
  1188. #undef OFFSET
  1189. #define OFFSET(x) offsetof(CompactContext, x)
  1190. static const AVOption csv_options[] = {
  1191. {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
  1192. {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
  1193. {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1194. {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1195. {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
  1196. {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
  1197. {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1198. {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1199. {NULL},
  1200. };
  1201. DEFINE_WRITER_CLASS(csv);
  1202. static const Writer csv_writer = {
  1203. .name = "csv",
  1204. .priv_size = sizeof(CompactContext),
  1205. .init = compact_init,
  1206. .print_section_header = compact_print_section_header,
  1207. .print_section_footer = compact_print_section_footer,
  1208. .print_integer = compact_print_int,
  1209. .print_string = compact_print_str,
  1210. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  1211. .priv_class = &csv_class,
  1212. };
  1213. /* Flat output */
  1214. typedef struct FlatContext {
  1215. const AVClass *class;
  1216. const char *sep_str;
  1217. char sep;
  1218. int hierarchical;
  1219. } FlatContext;
  1220. #undef OFFSET
  1221. #define OFFSET(x) offsetof(FlatContext, x)
  1222. static const AVOption flat_options[]= {
  1223. {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
  1224. {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
  1225. {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1226. {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1227. {NULL},
  1228. };
  1229. DEFINE_WRITER_CLASS(flat);
  1230. static av_cold int flat_init(WriterContext *wctx)
  1231. {
  1232. FlatContext *flat = wctx->priv;
  1233. if (strlen(flat->sep_str) != 1) {
  1234. av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
  1235. flat->sep_str);
  1236. return AVERROR(EINVAL);
  1237. }
  1238. flat->sep = flat->sep_str[0];
  1239. return 0;
  1240. }
  1241. static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
  1242. {
  1243. const char *p;
  1244. for (p = src; *p; p++) {
  1245. if (!((*p >= '0' && *p <= '9') ||
  1246. (*p >= 'a' && *p <= 'z') ||
  1247. (*p >= 'A' && *p <= 'Z')))
  1248. av_bprint_chars(dst, '_', 1);
  1249. else
  1250. av_bprint_chars(dst, *p, 1);
  1251. }
  1252. return dst->str;
  1253. }
  1254. static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
  1255. {
  1256. const char *p;
  1257. for (p = src; *p; p++) {
  1258. switch (*p) {
  1259. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  1260. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  1261. case '\\': av_bprintf(dst, "%s", "\\\\"); break;
  1262. case '"': av_bprintf(dst, "%s", "\\\""); break;
  1263. case '`': av_bprintf(dst, "%s", "\\`"); break;
  1264. case '$': av_bprintf(dst, "%s", "\\$"); break;
  1265. default: av_bprint_chars(dst, *p, 1); break;
  1266. }
  1267. }
  1268. return dst->str;
  1269. }
  1270. static void flat_print_section_header(WriterContext *wctx, const void *data)
  1271. {
  1272. FlatContext *flat = wctx->priv;
  1273. AVBPrint *buf = &wctx->section_pbuf[wctx->level];
  1274. const struct section *section = wctx->section[wctx->level];
  1275. const struct section *parent_section = wctx->level ?
  1276. wctx->section[wctx->level-1] : NULL;
  1277. /* build section header */
  1278. av_bprint_clear(buf);
  1279. if (!parent_section)
  1280. return;
  1281. av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
  1282. if (flat->hierarchical ||
  1283. !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
  1284. av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
  1285. if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
  1286. int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
  1287. wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
  1288. av_bprintf(buf, "%d%s", n, flat->sep_str);
  1289. }
  1290. }
  1291. }
  1292. static void flat_print_int(WriterContext *wctx, const char *key, int64_t value)
  1293. {
  1294. writer_printf(wctx, "%s%s=%"PRId64"\n", wctx->section_pbuf[wctx->level].str, key, value);
  1295. }
  1296. static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
  1297. {
  1298. FlatContext *flat = wctx->priv;
  1299. AVBPrint buf;
  1300. writer_put_str(wctx, wctx->section_pbuf[wctx->level].str);
  1301. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1302. writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep));
  1303. av_bprint_clear(&buf);
  1304. writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value));
  1305. av_bprint_finalize(&buf, NULL);
  1306. }
  1307. static const Writer flat_writer = {
  1308. .name = "flat",
  1309. .priv_size = sizeof(FlatContext),
  1310. .init = flat_init,
  1311. .print_section_header = flat_print_section_header,
  1312. .print_integer = flat_print_int,
  1313. .print_string = flat_print_str,
  1314. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1315. .priv_class = &flat_class,
  1316. };
  1317. /* INI format output */
  1318. typedef struct INIContext {
  1319. const AVClass *class;
  1320. int hierarchical;
  1321. } INIContext;
  1322. #undef OFFSET
  1323. #define OFFSET(x) offsetof(INIContext, x)
  1324. static const AVOption ini_options[] = {
  1325. {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1326. {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
  1327. {NULL},
  1328. };
  1329. DEFINE_WRITER_CLASS(ini);
  1330. static char *ini_escape_str(AVBPrint *dst, const char *src)
  1331. {
  1332. int i = 0;
  1333. char c = 0;
  1334. while (c = src[i++]) {
  1335. switch (c) {
  1336. case '\b': av_bprintf(dst, "%s", "\\b"); break;
  1337. case '\f': av_bprintf(dst, "%s", "\\f"); break;
  1338. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  1339. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  1340. case '\t': av_bprintf(dst, "%s", "\\t"); break;
  1341. case '\\':
  1342. case '#' :
  1343. case '=' :
  1344. case ':' : av_bprint_chars(dst, '\\', 1);
  1345. default:
  1346. if ((unsigned char)c < 32)
  1347. av_bprintf(dst, "\\x00%02x", c & 0xff);
  1348. else
  1349. av_bprint_chars(dst, c, 1);
  1350. break;
  1351. }
  1352. }
  1353. return dst->str;
  1354. }
  1355. static void ini_print_section_header(WriterContext *wctx, const void *data)
  1356. {
  1357. INIContext *ini = wctx->priv;
  1358. AVBPrint *buf = &wctx->section_pbuf[wctx->level];
  1359. const struct section *section = wctx->section[wctx->level];
  1360. const struct section *parent_section = wctx->level ?
  1361. wctx->section[wctx->level-1] : NULL;
  1362. av_bprint_clear(buf);
  1363. if (!parent_section) {
  1364. writer_put_str(wctx, "# ffprobe output\n\n");
  1365. return;
  1366. }
  1367. if (wctx->nb_item[wctx->level-1])
  1368. writer_w8(wctx, '\n');
  1369. av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
  1370. if (ini->hierarchical ||
  1371. !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
  1372. av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
  1373. if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
  1374. int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
  1375. wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
  1376. av_bprintf(buf, ".%d", n);
  1377. }
  1378. }
  1379. if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
  1380. writer_printf(wctx, "[%s]\n", buf->str);
  1381. }
  1382. static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
  1383. {
  1384. AVBPrint buf;
  1385. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1386. writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
  1387. av_bprint_clear(&buf);
  1388. writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
  1389. av_bprint_finalize(&buf, NULL);
  1390. }
  1391. static void ini_print_int(WriterContext *wctx, const char *key, int64_t value)
  1392. {
  1393. writer_printf(wctx, "%s=%"PRId64"\n", key, value);
  1394. }
  1395. static const Writer ini_writer = {
  1396. .name = "ini",
  1397. .priv_size = sizeof(INIContext),
  1398. .print_section_header = ini_print_section_header,
  1399. .print_integer = ini_print_int,
  1400. .print_string = ini_print_str,
  1401. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1402. .priv_class = &ini_class,
  1403. };
  1404. /* JSON output */
  1405. typedef struct JSONContext {
  1406. const AVClass *class;
  1407. int indent_level;
  1408. int compact;
  1409. const char *item_sep, *item_start_end;
  1410. } JSONContext;
  1411. #undef OFFSET
  1412. #define OFFSET(x) offsetof(JSONContext, x)
  1413. static const AVOption json_options[]= {
  1414. { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1415. { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1416. { NULL }
  1417. };
  1418. DEFINE_WRITER_CLASS(json);
  1419. static av_cold int json_init(WriterContext *wctx)
  1420. {
  1421. JSONContext *json = wctx->priv;
  1422. json->item_sep = json->compact ? ", " : ",\n";
  1423. json->item_start_end = json->compact ? " " : "\n";
  1424. return 0;
  1425. }
  1426. static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
  1427. {
  1428. static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
  1429. static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
  1430. const char *p;
  1431. for (p = src; *p; p++) {
  1432. char *s = strchr(json_escape, *p);
  1433. if (s) {
  1434. av_bprint_chars(dst, '\\', 1);
  1435. av_bprint_chars(dst, json_subst[s - json_escape], 1);
  1436. } else if ((unsigned char)*p < 32) {
  1437. av_bprintf(dst, "\\u00%02x", *p & 0xff);
  1438. } else {
  1439. av_bprint_chars(dst, *p, 1);
  1440. }
  1441. }
  1442. return dst->str;
  1443. }
  1444. #define JSON_INDENT() writer_printf(wctx, "%*c", json->indent_level * 4, ' ')
  1445. static void json_print_section_header(WriterContext *wctx, const void *data)
  1446. {
  1447. JSONContext *json = wctx->priv;
  1448. AVBPrint buf;
  1449. const struct section *section = wctx->section[wctx->level];
  1450. const struct section *parent_section = wctx->level ?
  1451. wctx->section[wctx->level-1] : NULL;
  1452. if (wctx->level && wctx->nb_item[wctx->level-1])
  1453. writer_put_str(wctx, ",\n");
  1454. if (section->flags & SECTION_FLAG_IS_WRAPPER) {
  1455. writer_put_str(wctx, "{\n");
  1456. json->indent_level++;
  1457. } else {
  1458. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1459. json_escape_str(&buf, section->name, wctx);
  1460. JSON_INDENT();
  1461. json->indent_level++;
  1462. if (section->flags & SECTION_FLAG_IS_ARRAY) {
  1463. writer_printf(wctx, "\"%s\": [\n", buf.str);
  1464. } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
  1465. writer_printf(wctx, "\"%s\": {%s", buf.str, json->item_start_end);
  1466. } else {
  1467. writer_printf(wctx, "{%s", json->item_start_end);
  1468. /* this is required so the parser can distinguish between packets and frames */
  1469. if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
  1470. if (!json->compact)
  1471. JSON_INDENT();
  1472. writer_printf(wctx, "\"type\": \"%s\"", section->name);
  1473. wctx->nb_item[wctx->level]++;
  1474. }
  1475. }
  1476. av_bprint_finalize(&buf, NULL);
  1477. }
  1478. }
  1479. static void json_print_section_footer(WriterContext *wctx)
  1480. {
  1481. JSONContext *json = wctx->priv;
  1482. const struct section *section = wctx->section[wctx->level];
  1483. if (wctx->level == 0) {
  1484. json->indent_level--;
  1485. writer_put_str(wctx, "\n}\n");
  1486. } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
  1487. writer_w8(wctx, '\n');
  1488. json->indent_level--;
  1489. JSON_INDENT();
  1490. writer_w8(wctx, ']');
  1491. } else {
  1492. writer_put_str(wctx, json->item_start_end);
  1493. json->indent_level--;
  1494. if (!json->compact)
  1495. JSON_INDENT();
  1496. writer_w8(wctx, '}');
  1497. }
  1498. }
  1499. static inline void json_print_item_str(WriterContext *wctx,
  1500. const char *key, const char *value)
  1501. {
  1502. AVBPrint buf;
  1503. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1504. writer_printf(wctx, "\"%s\":", json_escape_str(&buf, key, wctx));
  1505. av_bprint_clear(&buf);
  1506. writer_printf(wctx, " \"%s\"", json_escape_str(&buf, value, wctx));
  1507. av_bprint_finalize(&buf, NULL);
  1508. }
  1509. static void json_print_str(WriterContext *wctx, const char *key, const char *value)
  1510. {
  1511. JSONContext *json = wctx->priv;
  1512. const struct section *parent_section = wctx->level ?
  1513. wctx->section[wctx->level-1] : NULL;
  1514. if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
  1515. writer_put_str(wctx, json->item_sep);
  1516. if (!json->compact)
  1517. JSON_INDENT();
  1518. json_print_item_str(wctx, key, value);
  1519. }
  1520. static void json_print_int(WriterContext *wctx, const char *key, int64_t value)
  1521. {
  1522. JSONContext *json = wctx->priv;
  1523. const struct section *parent_section = wctx->level ?
  1524. wctx->section[wctx->level-1] : NULL;
  1525. AVBPrint buf;
  1526. if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
  1527. writer_put_str(wctx, json->item_sep);
  1528. if (!json->compact)
  1529. JSON_INDENT();
  1530. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1531. writer_printf(wctx, "\"%s\": %"PRId64, json_escape_str(&buf, key, wctx), value);
  1532. av_bprint_finalize(&buf, NULL);
  1533. }
  1534. static const Writer json_writer = {
  1535. .name = "json",
  1536. .priv_size = sizeof(JSONContext),
  1537. .init = json_init,
  1538. .print_section_header = json_print_section_header,
  1539. .print_section_footer = json_print_section_footer,
  1540. .print_integer = json_print_int,
  1541. .print_string = json_print_str,
  1542. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1543. .priv_class = &json_class,
  1544. };
  1545. /* XML output */
  1546. typedef struct XMLContext {
  1547. const AVClass *class;
  1548. int within_tag;
  1549. int indent_level;
  1550. int fully_qualified;
  1551. int xsd_strict;
  1552. } XMLContext;
  1553. #undef OFFSET
  1554. #define OFFSET(x) offsetof(XMLContext, x)
  1555. static const AVOption xml_options[] = {
  1556. {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1557. {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1558. {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1559. {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
  1560. {NULL},
  1561. };
  1562. DEFINE_WRITER_CLASS(xml);
  1563. static av_cold int xml_init(WriterContext *wctx)
  1564. {
  1565. XMLContext *xml = wctx->priv;
  1566. if (xml->xsd_strict) {
  1567. xml->fully_qualified = 1;
  1568. #define CHECK_COMPLIANCE(opt, opt_name) \
  1569. if (opt) { \
  1570. av_log(wctx, AV_LOG_ERROR, \
  1571. "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
  1572. "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
  1573. return AVERROR(EINVAL); \
  1574. }
  1575. CHECK_COMPLIANCE(show_private_data, "private");
  1576. CHECK_COMPLIANCE(show_value_unit, "unit");
  1577. CHECK_COMPLIANCE(use_value_prefix, "prefix");
  1578. }
  1579. return 0;
  1580. }
  1581. #define XML_INDENT() writer_printf(wctx, "%*c", xml->indent_level * 4, ' ')
  1582. static void xml_print_section_header(WriterContext *wctx, const void *data)
  1583. {
  1584. XMLContext *xml = wctx->priv;
  1585. const struct section *section = wctx->section[wctx->level];
  1586. const struct section *parent_section = wctx->level ?
  1587. wctx->section[wctx->level-1] : NULL;
  1588. if (wctx->level == 0) {
  1589. const char *qual = " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
  1590. "xmlns:ffprobe=\"http://www.ffmpeg.org/schema/ffprobe\" "
  1591. "xsi:schemaLocation=\"http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd\"";
  1592. writer_put_str(wctx, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  1593. writer_printf(wctx, "<%sffprobe%s>\n",
  1594. xml->fully_qualified ? "ffprobe:" : "",
  1595. xml->fully_qualified ? qual : "");
  1596. return;
  1597. }
  1598. if (xml->within_tag) {
  1599. xml->within_tag = 0;
  1600. writer_put_str(wctx, ">\n");
  1601. }
  1602. if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
  1603. wctx->level && wctx->nb_item[wctx->level-1])
  1604. writer_w8(wctx, '\n');
  1605. xml->indent_level++;
  1606. if (section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_HAS_VARIABLE_FIELDS)) {
  1607. XML_INDENT(); writer_printf(wctx, "<%s", section->name);
  1608. if (section->flags & SECTION_FLAG_HAS_TYPE) {
  1609. AVBPrint buf;
  1610. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1611. av_bprint_escape(&buf, section->get_type(data), NULL,
  1612. AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
  1613. writer_printf(wctx, " type=\"%s\"", buf.str);
  1614. }
  1615. writer_printf(wctx, ">\n", section->name);
  1616. } else {
  1617. XML_INDENT(); writer_printf(wctx, "<%s ", section->name);
  1618. xml->within_tag = 1;
  1619. }
  1620. }
  1621. static void xml_print_section_footer(WriterContext *wctx)
  1622. {
  1623. XMLContext *xml = wctx->priv;
  1624. const struct section *section = wctx->section[wctx->level];
  1625. if (wctx->level == 0) {
  1626. writer_printf(wctx, "</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
  1627. } else if (xml->within_tag) {
  1628. xml->within_tag = 0;
  1629. writer_put_str(wctx, "/>\n");
  1630. xml->indent_level--;
  1631. } else {
  1632. XML_INDENT(); writer_printf(wctx, "</%s>\n", section->name);
  1633. xml->indent_level--;
  1634. }
  1635. }
  1636. static void xml_print_value(WriterContext *wctx, const char *key,
  1637. const char *str, int64_t num, const int is_int)
  1638. {
  1639. AVBPrint buf;
  1640. XMLContext *xml = wctx->priv;
  1641. const struct section *section = wctx->section[wctx->level];
  1642. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1643. if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
  1644. xml->indent_level++;
  1645. XML_INDENT();
  1646. av_bprint_escape(&buf, key, NULL,
  1647. AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
  1648. writer_printf(wctx, "<%s key=\"%s\"",
  1649. section->element_name, buf.str);
  1650. av_bprint_clear(&buf);
  1651. if (is_int) {
  1652. writer_printf(wctx, " value=\"%"PRId64"\"/>\n", num);
  1653. } else {
  1654. av_bprint_escape(&buf, str, NULL,
  1655. AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
  1656. writer_printf(wctx, " value=\"%s\"/>\n", buf.str);
  1657. }
  1658. xml->indent_level--;
  1659. } else {
  1660. if (wctx->nb_item[wctx->level])
  1661. writer_w8(wctx, ' ');
  1662. if (is_int) {
  1663. writer_printf(wctx, "%s=\"%"PRId64"\"", key, num);
  1664. } else {
  1665. av_bprint_escape(&buf, str, NULL,
  1666. AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
  1667. writer_printf(wctx, "%s=\"%s\"", key, buf.str);
  1668. }
  1669. }
  1670. av_bprint_finalize(&buf, NULL);
  1671. }
  1672. static inline void xml_print_str(WriterContext *wctx, const char *key, const char *value) {
  1673. xml_print_value(wctx, key, value, 0, 0);
  1674. }
  1675. static void xml_print_int(WriterContext *wctx, const char *key, int64_t value)
  1676. {
  1677. xml_print_value(wctx, key, NULL, value, 1);
  1678. }
  1679. static Writer xml_writer = {
  1680. .name = "xml",
  1681. .priv_size = sizeof(XMLContext),
  1682. .init = xml_init,
  1683. .print_section_header = xml_print_section_header,
  1684. .print_section_footer = xml_print_section_footer,
  1685. .print_integer = xml_print_int,
  1686. .print_string = xml_print_str,
  1687. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1688. .priv_class = &xml_class,
  1689. };
  1690. static void writer_register_all(void)
  1691. {
  1692. static int initialized;
  1693. if (initialized)
  1694. return;
  1695. initialized = 1;
  1696. writer_register(&default_writer);
  1697. writer_register(&compact_writer);
  1698. writer_register(&csv_writer);
  1699. writer_register(&flat_writer);
  1700. writer_register(&ini_writer);
  1701. writer_register(&json_writer);
  1702. writer_register(&xml_writer);
  1703. }
  1704. #define print_fmt(k, f, ...) do { \
  1705. av_bprint_clear(&pbuf); \
  1706. av_bprintf(&pbuf, f, __VA_ARGS__); \
  1707. writer_print_string(w, k, pbuf.str, 0); \
  1708. } while (0)
  1709. #define print_list_fmt(k, f, n, m, ...) do { \
  1710. av_bprint_clear(&pbuf); \
  1711. for (int idx = 0; idx < n; idx++) { \
  1712. for (int idx2 = 0; idx2 < m; idx2++) { \
  1713. if (idx > 0 || idx2 > 0) \
  1714. av_bprint_chars(&pbuf, ' ', 1); \
  1715. av_bprintf(&pbuf, f, __VA_ARGS__); \
  1716. } \
  1717. } \
  1718. writer_print_string(w, k, pbuf.str, 0); \
  1719. } while (0)
  1720. #define print_int(k, v) writer_print_integer(w, k, v)
  1721. #define print_q(k, v, s) writer_print_rational(w, k, v, s)
  1722. #define print_str(k, v) writer_print_string(w, k, v, 0)
  1723. #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
  1724. #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
  1725. #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
  1726. #define print_ts(k, v) writer_print_ts(w, k, v, 0)
  1727. #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
  1728. #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
  1729. #define print_val(k, v, u) do { \
  1730. struct unit_value uv; \
  1731. uv.val.i = v; \
  1732. uv.unit = u; \
  1733. writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
  1734. } while (0)
  1735. #define print_section_header(s) writer_print_section_header(w, NULL, s)
  1736. #define print_section_header_data(s, d) writer_print_section_header(w, d, s)
  1737. #define print_section_footer(s) writer_print_section_footer(w, s)
  1738. #define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n) \
  1739. { \
  1740. ret = av_reallocp_array(&(ptr), (new_n), sizeof(*(ptr))); \
  1741. if (ret < 0) \
  1742. goto end; \
  1743. memset( (ptr) + (cur_n), 0, ((new_n) - (cur_n)) * sizeof(*(ptr)) ); \
  1744. }
  1745. static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
  1746. {
  1747. const AVDictionaryEntry *tag = NULL;
  1748. int ret = 0;
  1749. if (!tags)
  1750. return 0;
  1751. writer_print_section_header(w, NULL, section_id);
  1752. while ((tag = av_dict_iterate(tags, tag))) {
  1753. if ((ret = print_str_validate(tag->key, tag->value)) < 0)
  1754. break;
  1755. }
  1756. writer_print_section_footer(w);
  1757. return ret;
  1758. }
  1759. static void print_dovi_metadata(WriterContext *w, const AVDOVIMetadata *dovi)
  1760. {
  1761. if (!dovi)
  1762. return;
  1763. {
  1764. const AVDOVIRpuDataHeader *hdr = av_dovi_get_header(dovi);
  1765. const AVDOVIDataMapping *mapping = av_dovi_get_mapping(dovi);
  1766. const AVDOVIColorMetadata *color = av_dovi_get_color(dovi);
  1767. AVBPrint pbuf;
  1768. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1769. // header
  1770. print_int("rpu_type", hdr->rpu_type);
  1771. print_int("rpu_format", hdr->rpu_format);
  1772. print_int("vdr_rpu_profile", hdr->vdr_rpu_profile);
  1773. print_int("vdr_rpu_level", hdr->vdr_rpu_level);
  1774. print_int("chroma_resampling_explicit_filter_flag",
  1775. hdr->chroma_resampling_explicit_filter_flag);
  1776. print_int("coef_data_type", hdr->coef_data_type);
  1777. print_int("coef_log2_denom", hdr->coef_log2_denom);
  1778. print_int("vdr_rpu_normalized_idc", hdr->vdr_rpu_normalized_idc);
  1779. print_int("bl_video_full_range_flag", hdr->bl_video_full_range_flag);
  1780. print_int("bl_bit_depth", hdr->bl_bit_depth);
  1781. print_int("el_bit_depth", hdr->el_bit_depth);
  1782. print_int("vdr_bit_depth", hdr->vdr_bit_depth);
  1783. print_int("spatial_resampling_filter_flag",
  1784. hdr->spatial_resampling_filter_flag);
  1785. print_int("el_spatial_resampling_filter_flag",
  1786. hdr->el_spatial_resampling_filter_flag);
  1787. print_int("disable_residual_flag", hdr->disable_residual_flag);
  1788. // data mapping values
  1789. print_int("vdr_rpu_id", mapping->vdr_rpu_id);
  1790. print_int("mapping_color_space", mapping->mapping_color_space);
  1791. print_int("mapping_chroma_format_idc",
  1792. mapping->mapping_chroma_format_idc);
  1793. print_int("nlq_method_idc", mapping->nlq_method_idc);
  1794. switch (mapping->nlq_method_idc) {
  1795. case AV_DOVI_NLQ_NONE:
  1796. print_str("nlq_method_idc_name", "none");
  1797. break;
  1798. case AV_DOVI_NLQ_LINEAR_DZ:
  1799. print_str("nlq_method_idc_name", "linear_dz");
  1800. break;
  1801. default:
  1802. print_str("nlq_method_idc_name", "unknown");
  1803. break;
  1804. }
  1805. print_int("num_x_partitions", mapping->num_x_partitions);
  1806. print_int("num_y_partitions", mapping->num_y_partitions);
  1807. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST);
  1808. for (int c = 0; c < 3; c++) {
  1809. const AVDOVIReshapingCurve *curve = &mapping->curves[c];
  1810. writer_print_section_header(w, "Reshaping curve", SECTION_ID_FRAME_SIDE_DATA_COMPONENT);
  1811. print_list_fmt("pivots", "%"PRIu16, curve->num_pivots, 1, curve->pivots[idx]);
  1812. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST);
  1813. for (int i = 0; i < curve->num_pivots - 1; i++) {
  1814. AVBPrint piece_buf;
  1815. av_bprint_init(&piece_buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
  1816. switch (curve->mapping_idc[i]) {
  1817. case AV_DOVI_MAPPING_POLYNOMIAL:
  1818. av_bprintf(&piece_buf, "Polynomial");
  1819. break;
  1820. case AV_DOVI_MAPPING_MMR:
  1821. av_bprintf(&piece_buf, "MMR");
  1822. break;
  1823. default:
  1824. av_bprintf(&piece_buf, "Unknown");
  1825. break;
  1826. }
  1827. av_bprintf(&piece_buf, " mapping");
  1828. writer_print_section_header(w, piece_buf.str, SECTION_ID_FRAME_SIDE_DATA_PIECE);
  1829. print_int("mapping_idc", curve->mapping_idc[i]);
  1830. switch (curve->mapping_idc[i]) {
  1831. case AV_DOVI_MAPPING_POLYNOMIAL:
  1832. print_str("mapping_idc_name", "polynomial");
  1833. print_int("poly_order", curve->poly_order[i]);
  1834. print_list_fmt("poly_coef", "%"PRIi64,
  1835. curve->poly_order[i] + 1, 1,
  1836. curve->poly_coef[i][idx]);
  1837. break;
  1838. case AV_DOVI_MAPPING_MMR:
  1839. print_str("mapping_idc_name", "mmr");
  1840. print_int("mmr_order", curve->mmr_order[i]);
  1841. print_int("mmr_constant", curve->mmr_constant[i]);
  1842. print_list_fmt("mmr_coef", "%"PRIi64,
  1843. curve->mmr_order[i], 7,
  1844. curve->mmr_coef[i][idx][idx2]);
  1845. break;
  1846. default:
  1847. print_str("mapping_idc_name", "unknown");
  1848. break;
  1849. }
  1850. // SECTION_ID_FRAME_SIDE_DATA_PIECE
  1851. writer_print_section_footer(w);
  1852. }
  1853. // SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST
  1854. writer_print_section_footer(w);
  1855. if (mapping->nlq_method_idc != AV_DOVI_NLQ_NONE) {
  1856. const AVDOVINLQParams *nlq = &mapping->nlq[c];
  1857. print_int("nlq_offset", nlq->nlq_offset);
  1858. print_int("vdr_in_max", nlq->vdr_in_max);
  1859. switch (mapping->nlq_method_idc) {
  1860. case AV_DOVI_NLQ_LINEAR_DZ:
  1861. print_int("linear_deadzone_slope", nlq->linear_deadzone_slope);
  1862. print_int("linear_deadzone_threshold", nlq->linear_deadzone_threshold);
  1863. break;
  1864. }
  1865. }
  1866. // SECTION_ID_FRAME_SIDE_DATA_COMPONENT
  1867. writer_print_section_footer(w);
  1868. }
  1869. // SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST
  1870. writer_print_section_footer(w);
  1871. // color metadata
  1872. print_int("dm_metadata_id", color->dm_metadata_id);
  1873. print_int("scene_refresh_flag", color->scene_refresh_flag);
  1874. print_list_fmt("ycc_to_rgb_matrix", "%d/%d",
  1875. FF_ARRAY_ELEMS(color->ycc_to_rgb_matrix), 1,
  1876. color->ycc_to_rgb_matrix[idx].num,
  1877. color->ycc_to_rgb_matrix[idx].den);
  1878. print_list_fmt("ycc_to_rgb_offset", "%d/%d",
  1879. FF_ARRAY_ELEMS(color->ycc_to_rgb_offset), 1,
  1880. color->ycc_to_rgb_offset[idx].num,
  1881. color->ycc_to_rgb_offset[idx].den);
  1882. print_list_fmt("rgb_to_lms_matrix", "%d/%d",
  1883. FF_ARRAY_ELEMS(color->rgb_to_lms_matrix), 1,
  1884. color->rgb_to_lms_matrix[idx].num,
  1885. color->rgb_to_lms_matrix[idx].den);
  1886. print_int("signal_eotf", color->signal_eotf);
  1887. print_int("signal_eotf_param0", color->signal_eotf_param0);
  1888. print_int("signal_eotf_param1", color->signal_eotf_param1);
  1889. print_int("signal_eotf_param2", color->signal_eotf_param2);
  1890. print_int("signal_bit_depth", color->signal_bit_depth);
  1891. print_int("signal_color_space", color->signal_color_space);
  1892. print_int("signal_chroma_format", color->signal_chroma_format);
  1893. print_int("signal_full_range_flag", color->signal_full_range_flag);
  1894. print_int("source_min_pq", color->source_min_pq);
  1895. print_int("source_max_pq", color->source_max_pq);
  1896. print_int("source_diagonal", color->source_diagonal);
  1897. av_bprint_finalize(&pbuf, NULL);
  1898. }
  1899. }
  1900. static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
  1901. {
  1902. if (!metadata)
  1903. return;
  1904. print_int("application version", metadata->application_version);
  1905. print_int("num_windows", metadata->num_windows);
  1906. for (int n = 1; n < metadata->num_windows; n++) {
  1907. const AVHDRPlusColorTransformParams *params = &metadata->params[n];
  1908. print_q("window_upper_left_corner_x",
  1909. params->window_upper_left_corner_x,'/');
  1910. print_q("window_upper_left_corner_y",
  1911. params->window_upper_left_corner_y,'/');
  1912. print_q("window_lower_right_corner_x",
  1913. params->window_lower_right_corner_x,'/');
  1914. print_q("window_lower_right_corner_y",
  1915. params->window_lower_right_corner_y,'/');
  1916. print_q("window_upper_left_corner_x",
  1917. params->window_upper_left_corner_x,'/');
  1918. print_q("window_upper_left_corner_y",
  1919. params->window_upper_left_corner_y,'/');
  1920. print_int("center_of_ellipse_x",
  1921. params->center_of_ellipse_x ) ;
  1922. print_int("center_of_ellipse_y",
  1923. params->center_of_ellipse_y );
  1924. print_int("rotation_angle",
  1925. params->rotation_angle);
  1926. print_int("semimajor_axis_internal_ellipse",
  1927. params->semimajor_axis_internal_ellipse);
  1928. print_int("semimajor_axis_external_ellipse",
  1929. params->semimajor_axis_external_ellipse);
  1930. print_int("semiminor_axis_external_ellipse",
  1931. params->semiminor_axis_external_ellipse);
  1932. print_int("overlap_process_option",
  1933. params->overlap_process_option);
  1934. }
  1935. print_q("targeted_system_display_maximum_luminance",
  1936. metadata->targeted_system_display_maximum_luminance,'/');
  1937. if (metadata->targeted_system_display_actual_peak_luminance_flag) {
  1938. print_int("num_rows_targeted_system_display_actual_peak_luminance",
  1939. metadata->num_rows_targeted_system_display_actual_peak_luminance);
  1940. print_int("num_cols_targeted_system_display_actual_peak_luminance",
  1941. metadata->num_cols_targeted_system_display_actual_peak_luminance);
  1942. for (int i = 0; i < metadata->num_rows_targeted_system_display_actual_peak_luminance; i++) {
  1943. for (int j = 0; j < metadata->num_cols_targeted_system_display_actual_peak_luminance; j++) {
  1944. print_q("targeted_system_display_actual_peak_luminance",
  1945. metadata->targeted_system_display_actual_peak_luminance[i][j],'/');
  1946. }
  1947. }
  1948. }
  1949. for (int n = 0; n < metadata->num_windows; n++) {
  1950. const AVHDRPlusColorTransformParams *params = &metadata->params[n];
  1951. for (int i = 0; i < 3; i++) {
  1952. print_q("maxscl",params->maxscl[i],'/');
  1953. }
  1954. print_q("average_maxrgb",
  1955. params->average_maxrgb,'/');
  1956. print_int("num_distribution_maxrgb_percentiles",
  1957. params->num_distribution_maxrgb_percentiles);
  1958. for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
  1959. print_int("distribution_maxrgb_percentage",
  1960. params->distribution_maxrgb[i].percentage);
  1961. print_q("distribution_maxrgb_percentile",
  1962. params->distribution_maxrgb[i].percentile,'/');
  1963. }
  1964. print_q("fraction_bright_pixels",
  1965. params->fraction_bright_pixels,'/');
  1966. }
  1967. if (metadata->mastering_display_actual_peak_luminance_flag) {
  1968. print_int("num_rows_mastering_display_actual_peak_luminance",
  1969. metadata->num_rows_mastering_display_actual_peak_luminance);
  1970. print_int("num_cols_mastering_display_actual_peak_luminance",
  1971. metadata->num_cols_mastering_display_actual_peak_luminance);
  1972. for (int i = 0; i < metadata->num_rows_mastering_display_actual_peak_luminance; i++) {
  1973. for (int j = 0; j < metadata->num_cols_mastering_display_actual_peak_luminance; j++) {
  1974. print_q("mastering_display_actual_peak_luminance",
  1975. metadata->mastering_display_actual_peak_luminance[i][j],'/');
  1976. }
  1977. }
  1978. }
  1979. for (int n = 0; n < metadata->num_windows; n++) {
  1980. const AVHDRPlusColorTransformParams *params = &metadata->params[n];
  1981. if (params->tone_mapping_flag) {
  1982. print_q("knee_point_x", params->knee_point_x,'/');
  1983. print_q("knee_point_y", params->knee_point_y,'/');
  1984. print_int("num_bezier_curve_anchors",
  1985. params->num_bezier_curve_anchors );
  1986. for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
  1987. print_q("bezier_curve_anchors",
  1988. params->bezier_curve_anchors[i],'/');
  1989. }
  1990. }
  1991. if (params->color_saturation_mapping_flag) {
  1992. print_q("color_saturation_weight",
  1993. params->color_saturation_weight,'/');
  1994. }
  1995. }
  1996. }
  1997. static void print_dynamic_hdr_vivid(WriterContext *w, const AVDynamicHDRVivid *metadata)
  1998. {
  1999. if (!metadata)
  2000. return;
  2001. print_int("system_start_code", metadata->system_start_code);
  2002. print_int("num_windows", metadata->num_windows);
  2003. for (int n = 0; n < metadata->num_windows; n++) {
  2004. const AVHDRVividColorTransformParams *params = &metadata->params[n];
  2005. print_q("minimum_maxrgb", params->minimum_maxrgb, '/');
  2006. print_q("average_maxrgb", params->average_maxrgb, '/');
  2007. print_q("variance_maxrgb", params->variance_maxrgb, '/');
  2008. print_q("maximum_maxrgb", params->maximum_maxrgb, '/');
  2009. }
  2010. for (int n = 0; n < metadata->num_windows; n++) {
  2011. const AVHDRVividColorTransformParams *params = &metadata->params[n];
  2012. print_int("tone_mapping_mode_flag", params->tone_mapping_mode_flag);
  2013. if (params->tone_mapping_mode_flag) {
  2014. print_int("tone_mapping_param_num", params->tone_mapping_param_num);
  2015. for (int i = 0; i < params->tone_mapping_param_num; i++) {
  2016. const AVHDRVividColorToneMappingParams *tm_params = &params->tm_params[i];
  2017. print_q("targeted_system_display_maximum_luminance",
  2018. tm_params->targeted_system_display_maximum_luminance, '/');
  2019. print_int("base_enable_flag", tm_params->base_enable_flag);
  2020. if (tm_params->base_enable_flag) {
  2021. print_q("base_param_m_p", tm_params->base_param_m_p, '/');
  2022. print_q("base_param_m_m", tm_params->base_param_m_m, '/');
  2023. print_q("base_param_m_a", tm_params->base_param_m_a, '/');
  2024. print_q("base_param_m_b", tm_params->base_param_m_b, '/');
  2025. print_q("base_param_m_n", tm_params->base_param_m_n, '/');
  2026. print_int("base_param_k1", tm_params->base_param_k1);
  2027. print_int("base_param_k2", tm_params->base_param_k2);
  2028. print_int("base_param_k3", tm_params->base_param_k3);
  2029. print_int("base_param_Delta_enable_mode",
  2030. tm_params->base_param_Delta_enable_mode);
  2031. print_q("base_param_Delta", tm_params->base_param_Delta, '/');
  2032. }
  2033. print_int("3Spline_enable_flag", tm_params->three_Spline_enable_flag);
  2034. if (tm_params->three_Spline_enable_flag) {
  2035. print_int("3Spline_num", tm_params->three_Spline_num);
  2036. for (int j = 0; j < tm_params->three_Spline_num; j++) {
  2037. const AVHDRVivid3SplineParams *three_spline = &tm_params->three_spline[j];
  2038. print_int("3Spline_TH_mode", three_spline->th_mode);
  2039. if (three_spline->th_mode == 0 || three_spline->th_mode == 2)
  2040. print_q("3Spline_TH_enable_MB", three_spline->th_enable_mb, '/');
  2041. print_q("3Spline_TH_enable", three_spline->th_enable, '/');
  2042. print_q("3Spline_TH_Delta1", three_spline->th_delta1, '/');
  2043. print_q("3Spline_TH_Delta2", three_spline->th_delta2, '/');
  2044. print_q("3Spline_enable_Strength", three_spline->enable_strength, '/');
  2045. }
  2046. }
  2047. }
  2048. }
  2049. print_int("color_saturation_mapping_flag", params->color_saturation_mapping_flag);
  2050. if (params->color_saturation_mapping_flag) {
  2051. print_int("color_saturation_num", params->color_saturation_num);
  2052. for (int i = 0; i < params->color_saturation_num; i++) {
  2053. print_q("color_saturation_gain", params->color_saturation_gain[i], '/');
  2054. }
  2055. }
  2056. }
  2057. }
  2058. static void print_ambient_viewing_environment(WriterContext *w,
  2059. const AVAmbientViewingEnvironment *env)
  2060. {
  2061. if (!env)
  2062. return;
  2063. print_q("ambient_illuminance", env->ambient_illuminance, '/');
  2064. print_q("ambient_light_x", env->ambient_light_x, '/');
  2065. print_q("ambient_light_y", env->ambient_light_y, '/');
  2066. }
  2067. static void print_film_grain_params(WriterContext *w,
  2068. const AVFilmGrainParams *fgp)
  2069. {
  2070. const char *color_range, *color_primaries, *color_trc, *color_space;
  2071. const char *const film_grain_type_names[] = {
  2072. [AV_FILM_GRAIN_PARAMS_NONE] = "none",
  2073. [AV_FILM_GRAIN_PARAMS_AV1] = "av1",
  2074. [AV_FILM_GRAIN_PARAMS_H274] = "h274",
  2075. };
  2076. AVBPrint pbuf;
  2077. if (!fgp || fgp->type >= FF_ARRAY_ELEMS(film_grain_type_names))
  2078. return;
  2079. color_range = av_color_range_name(fgp->color_range);
  2080. color_primaries = av_color_primaries_name(fgp->color_primaries);
  2081. color_trc = av_color_transfer_name(fgp->color_trc);
  2082. color_space = av_color_space_name(fgp->color_space);
  2083. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2084. print_str("type", film_grain_type_names[fgp->type]);
  2085. print_fmt("seed", "%"PRIu64, fgp->seed);
  2086. print_int("width", fgp->width);
  2087. print_int("height", fgp->height);
  2088. print_int("subsampling_x", fgp->subsampling_x);
  2089. print_int("subsampling_y", fgp->subsampling_y);
  2090. print_str("color_range", color_range ? color_range : "unknown");
  2091. print_str("color_primaries", color_primaries ? color_primaries : "unknown");
  2092. print_str("color_trc", color_trc ? color_trc : "unknown");
  2093. print_str("color_space", color_space ? color_space : "unknown");
  2094. switch (fgp->type) {
  2095. case AV_FILM_GRAIN_PARAMS_NONE:
  2096. break;
  2097. case AV_FILM_GRAIN_PARAMS_AV1: {
  2098. const AVFilmGrainAOMParams *aom = &fgp->codec.aom;
  2099. const int num_ar_coeffs_y = 2 * aom->ar_coeff_lag * (aom->ar_coeff_lag + 1);
  2100. const int num_ar_coeffs_uv = num_ar_coeffs_y + !!aom->num_y_points;
  2101. print_int("chroma_scaling_from_luma", aom->chroma_scaling_from_luma);
  2102. print_int("scaling_shift", aom->scaling_shift);
  2103. print_int("ar_coeff_lag", aom->ar_coeff_lag);
  2104. print_int("ar_coeff_shift", aom->ar_coeff_shift);
  2105. print_int("grain_scale_shift", aom->grain_scale_shift);
  2106. print_int("overlap_flag", aom->overlap_flag);
  2107. print_int("limit_output_range", aom->limit_output_range);
  2108. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST);
  2109. if (aom->num_y_points) {
  2110. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_COMPONENT);
  2111. print_int("bit_depth_luma", fgp->bit_depth_luma);
  2112. print_list_fmt("y_points_value", "%"PRIu8, aom->num_y_points, 1, aom->y_points[idx][0]);
  2113. print_list_fmt("y_points_scaling", "%"PRIu8, aom->num_y_points, 1, aom->y_points[idx][1]);
  2114. print_list_fmt("ar_coeffs_y", "%"PRId8, num_ar_coeffs_y, 1, aom->ar_coeffs_y[idx]);
  2115. // SECTION_ID_FRAME_SIDE_DATA_COMPONENT
  2116. writer_print_section_footer(w);
  2117. }
  2118. for (int uv = 0; uv < 2; uv++) {
  2119. if (!aom->num_uv_points[uv] && !aom->chroma_scaling_from_luma)
  2120. continue;
  2121. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_COMPONENT);
  2122. print_int("bit_depth_chroma", fgp->bit_depth_chroma);
  2123. print_list_fmt("uv_points_value", "%"PRIu8, aom->num_uv_points[uv], 1, aom->uv_points[uv][idx][0]);
  2124. print_list_fmt("uv_points_scaling", "%"PRIu8, aom->num_uv_points[uv], 1, aom->uv_points[uv][idx][1]);
  2125. print_list_fmt("ar_coeffs_uv", "%"PRId8, num_ar_coeffs_uv, 1, aom->ar_coeffs_uv[uv][idx]);
  2126. print_int("uv_mult", aom->uv_mult[uv]);
  2127. print_int("uv_mult_luma", aom->uv_mult_luma[uv]);
  2128. print_int("uv_offset", aom->uv_offset[uv]);
  2129. // SECTION_ID_FRAME_SIDE_DATA_COMPONENT
  2130. writer_print_section_footer(w);
  2131. }
  2132. // SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST
  2133. writer_print_section_footer(w);
  2134. break;
  2135. }
  2136. case AV_FILM_GRAIN_PARAMS_H274: {
  2137. const AVFilmGrainH274Params *h274 = &fgp->codec.h274;
  2138. print_int("model_id", h274->model_id);
  2139. print_int("blending_mode_id", h274->blending_mode_id);
  2140. print_int("log2_scale_factor", h274->log2_scale_factor);
  2141. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST);
  2142. for (int c = 0; c < 3; c++) {
  2143. if (!h274->component_model_present[c])
  2144. continue;
  2145. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_COMPONENT);
  2146. print_int(c ? "bit_depth_chroma" : "bit_depth_luma", c ? fgp->bit_depth_chroma : fgp->bit_depth_luma);
  2147. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST);
  2148. for (int i = 0; i < h274->num_intensity_intervals[c]; i++) {
  2149. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_PIECE);
  2150. print_int("intensity_interval_lower_bound", h274->intensity_interval_lower_bound[c][i]);
  2151. print_int("intensity_interval_upper_bound", h274->intensity_interval_upper_bound[c][i]);
  2152. print_list_fmt("comp_model_value", "%"PRId16, h274->num_model_values[c], 1, h274->comp_model_value[c][i][idx]);
  2153. // SECTION_ID_FRAME_SIDE_DATA_PIECE
  2154. writer_print_section_footer(w);
  2155. }
  2156. // SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST
  2157. writer_print_section_footer(w);
  2158. // SECTION_ID_FRAME_SIDE_DATA_COMPONENT
  2159. writer_print_section_footer(w);
  2160. }
  2161. // SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST
  2162. writer_print_section_footer(w);
  2163. break;
  2164. }
  2165. }
  2166. av_bprint_finalize(&pbuf, NULL);
  2167. }
  2168. static void print_pkt_side_data(WriterContext *w,
  2169. AVCodecParameters *par,
  2170. const AVPacketSideData *sd,
  2171. SectionID id_data)
  2172. {
  2173. const char *name = av_packet_side_data_name(sd->type);
  2174. writer_print_section_header(w, sd, id_data);
  2175. print_str("side_data_type", name ? name : "unknown");
  2176. if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
  2177. double rotation = av_display_rotation_get((int32_t *)sd->data);
  2178. if (isnan(rotation))
  2179. rotation = 0;
  2180. writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
  2181. print_int("rotation", rotation);
  2182. } else if (sd->type == AV_PKT_DATA_STEREO3D) {
  2183. const AVStereo3D *stereo = (AVStereo3D *)sd->data;
  2184. print_str("type", av_stereo3d_type_name(stereo->type));
  2185. print_int("inverted", !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
  2186. print_str("view", av_stereo3d_view_name(stereo->view));
  2187. print_str("primary_eye", av_stereo3d_primary_eye_name(stereo->primary_eye));
  2188. print_int("baseline", stereo->baseline);
  2189. print_q("horizontal_disparity_adjustment", stereo->horizontal_disparity_adjustment, '/');
  2190. print_q("horizontal_field_of_view", stereo->horizontal_field_of_view, '/');
  2191. } else if (sd->type == AV_PKT_DATA_SPHERICAL) {
  2192. const AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
  2193. print_str("projection", av_spherical_projection_name(spherical->projection));
  2194. if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
  2195. print_int("padding", spherical->padding);
  2196. } else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
  2197. size_t l, t, r, b;
  2198. av_spherical_tile_bounds(spherical, par->width, par->height,
  2199. &l, &t, &r, &b);
  2200. print_int("bound_left", l);
  2201. print_int("bound_top", t);
  2202. print_int("bound_right", r);
  2203. print_int("bound_bottom", b);
  2204. }
  2205. print_int("yaw", (double) spherical->yaw / (1 << 16));
  2206. print_int("pitch", (double) spherical->pitch / (1 << 16));
  2207. print_int("roll", (double) spherical->roll / (1 << 16));
  2208. } else if (sd->type == AV_PKT_DATA_SKIP_SAMPLES && sd->size == 10) {
  2209. print_int("skip_samples", AV_RL32(sd->data));
  2210. print_int("discard_padding", AV_RL32(sd->data + 4));
  2211. print_int("skip_reason", AV_RL8(sd->data + 8));
  2212. print_int("discard_reason", AV_RL8(sd->data + 9));
  2213. } else if (sd->type == AV_PKT_DATA_MASTERING_DISPLAY_METADATA) {
  2214. AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
  2215. if (metadata->has_primaries) {
  2216. print_q("red_x", metadata->display_primaries[0][0], '/');
  2217. print_q("red_y", metadata->display_primaries[0][1], '/');
  2218. print_q("green_x", metadata->display_primaries[1][0], '/');
  2219. print_q("green_y", metadata->display_primaries[1][1], '/');
  2220. print_q("blue_x", metadata->display_primaries[2][0], '/');
  2221. print_q("blue_y", metadata->display_primaries[2][1], '/');
  2222. print_q("white_point_x", metadata->white_point[0], '/');
  2223. print_q("white_point_y", metadata->white_point[1], '/');
  2224. }
  2225. if (metadata->has_luminance) {
  2226. print_q("min_luminance", metadata->min_luminance, '/');
  2227. print_q("max_luminance", metadata->max_luminance, '/');
  2228. }
  2229. } else if (sd->type == AV_PKT_DATA_CONTENT_LIGHT_LEVEL) {
  2230. AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
  2231. print_int("max_content", metadata->MaxCLL);
  2232. print_int("max_average", metadata->MaxFALL);
  2233. } else if (sd->type == AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT) {
  2234. print_ambient_viewing_environment(
  2235. w, (const AVAmbientViewingEnvironment *)sd->data);
  2236. } else if (sd->type == AV_PKT_DATA_DYNAMIC_HDR10_PLUS) {
  2237. AVDynamicHDRPlus *metadata = (AVDynamicHDRPlus *)sd->data;
  2238. print_dynamic_hdr10_plus(w, metadata);
  2239. } else if (sd->type == AV_PKT_DATA_DOVI_CONF) {
  2240. AVDOVIDecoderConfigurationRecord *dovi = (AVDOVIDecoderConfigurationRecord *)sd->data;
  2241. const char *comp = "unknown";
  2242. print_int("dv_version_major", dovi->dv_version_major);
  2243. print_int("dv_version_minor", dovi->dv_version_minor);
  2244. print_int("dv_profile", dovi->dv_profile);
  2245. print_int("dv_level", dovi->dv_level);
  2246. print_int("rpu_present_flag", dovi->rpu_present_flag);
  2247. print_int("el_present_flag", dovi->el_present_flag);
  2248. print_int("bl_present_flag", dovi->bl_present_flag);
  2249. print_int("dv_bl_signal_compatibility_id", dovi->dv_bl_signal_compatibility_id);
  2250. switch (dovi->dv_md_compression)
  2251. {
  2252. case AV_DOVI_COMPRESSION_NONE: comp = "none"; break;
  2253. case AV_DOVI_COMPRESSION_LIMITED: comp = "limited"; break;
  2254. case AV_DOVI_COMPRESSION_RESERVED: comp = "reserved"; break;
  2255. case AV_DOVI_COMPRESSION_EXTENDED: comp = "extended"; break;
  2256. }
  2257. print_str("dv_md_compression", comp);
  2258. } else if (sd->type == AV_PKT_DATA_AUDIO_SERVICE_TYPE) {
  2259. enum AVAudioServiceType *t = (enum AVAudioServiceType *)sd->data;
  2260. print_int("service_type", *t);
  2261. } else if (sd->type == AV_PKT_DATA_MPEGTS_STREAM_ID) {
  2262. print_int("id", *sd->data);
  2263. } else if (sd->type == AV_PKT_DATA_CPB_PROPERTIES) {
  2264. const AVCPBProperties *prop = (AVCPBProperties *)sd->data;
  2265. print_int("max_bitrate", prop->max_bitrate);
  2266. print_int("min_bitrate", prop->min_bitrate);
  2267. print_int("avg_bitrate", prop->avg_bitrate);
  2268. print_int("buffer_size", prop->buffer_size);
  2269. print_int("vbv_delay", prop->vbv_delay);
  2270. } else if (sd->type == AV_PKT_DATA_WEBVTT_IDENTIFIER ||
  2271. sd->type == AV_PKT_DATA_WEBVTT_SETTINGS) {
  2272. if (do_show_data)
  2273. writer_print_data(w, "data", sd->data, sd->size);
  2274. writer_print_data_hash(w, "data_hash", sd->data, sd->size);
  2275. } else if (sd->type == AV_PKT_DATA_FRAME_CROPPING && sd->size >= sizeof(uint32_t) * 4) {
  2276. print_int("crop_top", AV_RL32(sd->data));
  2277. print_int("crop_bottom", AV_RL32(sd->data + 4));
  2278. print_int("crop_left", AV_RL32(sd->data + 8));
  2279. print_int("crop_right", AV_RL32(sd->data + 12));
  2280. } else if (sd->type == AV_PKT_DATA_AFD && sd->size > 0) {
  2281. print_int("active_format", *sd->data);
  2282. }
  2283. }
  2284. static void print_private_data(WriterContext *w, void *priv_data)
  2285. {
  2286. const AVOption *opt = NULL;
  2287. while (opt = av_opt_next(priv_data, opt)) {
  2288. uint8_t *str;
  2289. if (!(opt->flags & AV_OPT_FLAG_EXPORT)) continue;
  2290. if (av_opt_get(priv_data, opt->name, 0, &str) >= 0) {
  2291. print_str(opt->name, str);
  2292. av_free(str);
  2293. }
  2294. }
  2295. }
  2296. static void print_color_range(WriterContext *w, enum AVColorRange color_range)
  2297. {
  2298. const char *val = av_color_range_name(color_range);
  2299. if (!val || color_range == AVCOL_RANGE_UNSPECIFIED) {
  2300. print_str_opt("color_range", "unknown");
  2301. } else {
  2302. print_str("color_range", val);
  2303. }
  2304. }
  2305. static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
  2306. {
  2307. const char *val = av_color_space_name(color_space);
  2308. if (!val || color_space == AVCOL_SPC_UNSPECIFIED) {
  2309. print_str_opt("color_space", "unknown");
  2310. } else {
  2311. print_str("color_space", val);
  2312. }
  2313. }
  2314. static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
  2315. {
  2316. const char *val = av_color_primaries_name(color_primaries);
  2317. if (!val || color_primaries == AVCOL_PRI_UNSPECIFIED) {
  2318. print_str_opt("color_primaries", "unknown");
  2319. } else {
  2320. print_str("color_primaries", val);
  2321. }
  2322. }
  2323. static void print_color_trc(WriterContext *w, enum AVColorTransferCharacteristic color_trc)
  2324. {
  2325. const char *val = av_color_transfer_name(color_trc);
  2326. if (!val || color_trc == AVCOL_TRC_UNSPECIFIED) {
  2327. print_str_opt("color_transfer", "unknown");
  2328. } else {
  2329. print_str("color_transfer", val);
  2330. }
  2331. }
  2332. static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
  2333. {
  2334. const char *val = av_chroma_location_name(chroma_location);
  2335. if (!val || chroma_location == AVCHROMA_LOC_UNSPECIFIED) {
  2336. print_str_opt("chroma_location", "unspecified");
  2337. } else {
  2338. print_str("chroma_location", val);
  2339. }
  2340. }
  2341. static void clear_log(int need_lock)
  2342. {
  2343. int i;
  2344. if (need_lock)
  2345. pthread_mutex_lock(&log_mutex);
  2346. for (i=0; i<log_buffer_size; i++) {
  2347. av_freep(&log_buffer[i].context_name);
  2348. av_freep(&log_buffer[i].parent_name);
  2349. av_freep(&log_buffer[i].log_message);
  2350. }
  2351. log_buffer_size = 0;
  2352. if(need_lock)
  2353. pthread_mutex_unlock(&log_mutex);
  2354. }
  2355. static int show_log(WriterContext *w, int section_ids, int section_id, int log_level)
  2356. {
  2357. int i;
  2358. pthread_mutex_lock(&log_mutex);
  2359. if (!log_buffer_size) {
  2360. pthread_mutex_unlock(&log_mutex);
  2361. return 0;
  2362. }
  2363. writer_print_section_header(w, NULL, section_ids);
  2364. for (i=0; i<log_buffer_size; i++) {
  2365. if (log_buffer[i].log_level <= log_level) {
  2366. writer_print_section_header(w, NULL, section_id);
  2367. print_str("context", log_buffer[i].context_name);
  2368. print_int("level", log_buffer[i].log_level);
  2369. print_int("category", log_buffer[i].category);
  2370. if (log_buffer[i].parent_name) {
  2371. print_str("parent_context", log_buffer[i].parent_name);
  2372. print_int("parent_category", log_buffer[i].parent_category);
  2373. } else {
  2374. print_str_opt("parent_context", "N/A");
  2375. print_str_opt("parent_category", "N/A");
  2376. }
  2377. print_str("message", log_buffer[i].log_message);
  2378. writer_print_section_footer(w);
  2379. }
  2380. }
  2381. clear_log(0);
  2382. pthread_mutex_unlock(&log_mutex);
  2383. writer_print_section_footer(w);
  2384. return 0;
  2385. }
  2386. static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
  2387. {
  2388. char val_str[128];
  2389. AVStream *st = ifile->streams[pkt->stream_index].st;
  2390. AVBPrint pbuf;
  2391. const char *s;
  2392. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2393. writer_print_section_header(w, NULL, SECTION_ID_PACKET);
  2394. s = av_get_media_type_string(st->codecpar->codec_type);
  2395. if (s) print_str ("codec_type", s);
  2396. else print_str_opt("codec_type", "unknown");
  2397. print_int("stream_index", pkt->stream_index);
  2398. print_ts ("pts", pkt->pts);
  2399. print_time("pts_time", pkt->pts, &st->time_base);
  2400. print_ts ("dts", pkt->dts);
  2401. print_time("dts_time", pkt->dts, &st->time_base);
  2402. print_duration_ts("duration", pkt->duration);
  2403. print_duration_time("duration_time", pkt->duration, &st->time_base);
  2404. print_val("size", pkt->size, unit_byte_str);
  2405. if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
  2406. else print_str_opt("pos", "N/A");
  2407. print_fmt("flags", "%c%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
  2408. pkt->flags & AV_PKT_FLAG_DISCARD ? 'D' : '_',
  2409. pkt->flags & AV_PKT_FLAG_CORRUPT ? 'C' : '_');
  2410. if (do_show_data)
  2411. writer_print_data(w, "data", pkt->data, pkt->size);
  2412. writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
  2413. if (pkt->side_data_elems) {
  2414. size_t size;
  2415. const uint8_t *side_metadata;
  2416. side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
  2417. if (side_metadata && size && do_show_packet_tags) {
  2418. AVDictionary *dict = NULL;
  2419. if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
  2420. show_tags(w, dict, SECTION_ID_PACKET_TAGS);
  2421. av_dict_free(&dict);
  2422. }
  2423. writer_print_section_header(w, NULL, SECTION_ID_PACKET_SIDE_DATA_LIST);
  2424. for (int i = 0; i < pkt->side_data_elems; i++) {
  2425. print_pkt_side_data(w, st->codecpar, &pkt->side_data[i],
  2426. SECTION_ID_PACKET_SIDE_DATA);
  2427. writer_print_section_footer(w);
  2428. }
  2429. writer_print_section_footer(w);
  2430. }
  2431. writer_print_section_footer(w);
  2432. av_bprint_finalize(&pbuf, NULL);
  2433. fflush(stdout);
  2434. }
  2435. static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
  2436. AVFormatContext *fmt_ctx)
  2437. {
  2438. AVBPrint pbuf;
  2439. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2440. writer_print_section_header(w, NULL, SECTION_ID_SUBTITLE);
  2441. print_str ("media_type", "subtitle");
  2442. print_ts ("pts", sub->pts);
  2443. print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
  2444. print_int ("format", sub->format);
  2445. print_int ("start_display_time", sub->start_display_time);
  2446. print_int ("end_display_time", sub->end_display_time);
  2447. print_int ("num_rects", sub->num_rects);
  2448. writer_print_section_footer(w);
  2449. av_bprint_finalize(&pbuf, NULL);
  2450. fflush(stdout);
  2451. }
  2452. static void print_frame_side_data(WriterContext *w,
  2453. const AVFrame *frame,
  2454. const AVStream *stream)
  2455. {
  2456. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_LIST);
  2457. for (int i = 0; i < frame->nb_side_data; i++) {
  2458. const AVFrameSideData *sd = frame->side_data[i];
  2459. const char *name;
  2460. writer_print_section_header(w, sd, SECTION_ID_FRAME_SIDE_DATA);
  2461. name = av_frame_side_data_name(sd->type);
  2462. print_str("side_data_type", name ? name : "unknown");
  2463. if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
  2464. double rotation = av_display_rotation_get((int32_t *)sd->data);
  2465. if (isnan(rotation))
  2466. rotation = 0;
  2467. writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
  2468. print_int("rotation", rotation);
  2469. } else if (sd->type == AV_FRAME_DATA_AFD && sd->size > 0) {
  2470. print_int("active_format", *sd->data);
  2471. } else if (sd->type == AV_FRAME_DATA_GOP_TIMECODE && sd->size >= 8) {
  2472. char tcbuf[AV_TIMECODE_STR_SIZE];
  2473. av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
  2474. print_str("timecode", tcbuf);
  2475. } else if (sd->type == AV_FRAME_DATA_S12M_TIMECODE && sd->size == 16) {
  2476. uint32_t *tc = (uint32_t*)sd->data;
  2477. int m = FFMIN(tc[0],3);
  2478. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST);
  2479. for (int j = 1; j <= m ; j++) {
  2480. char tcbuf[AV_TIMECODE_STR_SIZE];
  2481. av_timecode_make_smpte_tc_string2(tcbuf, stream->avg_frame_rate, tc[j], 0, 0);
  2482. writer_print_section_header(w, NULL, SECTION_ID_FRAME_SIDE_DATA_TIMECODE);
  2483. print_str("value", tcbuf);
  2484. writer_print_section_footer(w);
  2485. }
  2486. writer_print_section_footer(w);
  2487. } else if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
  2488. AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
  2489. if (metadata->has_primaries) {
  2490. print_q("red_x", metadata->display_primaries[0][0], '/');
  2491. print_q("red_y", metadata->display_primaries[0][1], '/');
  2492. print_q("green_x", metadata->display_primaries[1][0], '/');
  2493. print_q("green_y", metadata->display_primaries[1][1], '/');
  2494. print_q("blue_x", metadata->display_primaries[2][0], '/');
  2495. print_q("blue_y", metadata->display_primaries[2][1], '/');
  2496. print_q("white_point_x", metadata->white_point[0], '/');
  2497. print_q("white_point_y", metadata->white_point[1], '/');
  2498. }
  2499. if (metadata->has_luminance) {
  2500. print_q("min_luminance", metadata->min_luminance, '/');
  2501. print_q("max_luminance", metadata->max_luminance, '/');
  2502. }
  2503. } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS) {
  2504. AVDynamicHDRPlus *metadata = (AVDynamicHDRPlus *)sd->data;
  2505. print_dynamic_hdr10_plus(w, metadata);
  2506. } else if (sd->type == AV_FRAME_DATA_CONTENT_LIGHT_LEVEL) {
  2507. AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
  2508. print_int("max_content", metadata->MaxCLL);
  2509. print_int("max_average", metadata->MaxFALL);
  2510. } else if (sd->type == AV_FRAME_DATA_ICC_PROFILE) {
  2511. const AVDictionaryEntry *tag = av_dict_get(sd->metadata, "name", NULL, AV_DICT_MATCH_CASE);
  2512. if (tag)
  2513. print_str(tag->key, tag->value);
  2514. print_int("size", sd->size);
  2515. } else if (sd->type == AV_FRAME_DATA_DOVI_METADATA) {
  2516. print_dovi_metadata(w, (const AVDOVIMetadata *)sd->data);
  2517. } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_VIVID) {
  2518. AVDynamicHDRVivid *metadata = (AVDynamicHDRVivid *)sd->data;
  2519. print_dynamic_hdr_vivid(w, metadata);
  2520. } else if (sd->type == AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT) {
  2521. print_ambient_viewing_environment(w, (const AVAmbientViewingEnvironment *)sd->data);
  2522. } else if (sd->type == AV_FRAME_DATA_FILM_GRAIN_PARAMS) {
  2523. AVFilmGrainParams *fgp = (AVFilmGrainParams *)sd->data;
  2524. print_film_grain_params(w, fgp);
  2525. } else if (sd->type == AV_FRAME_DATA_VIEW_ID) {
  2526. print_int("view_id", *(int*)sd->data);
  2527. }
  2528. writer_print_section_footer(w);
  2529. }
  2530. writer_print_section_footer(w);
  2531. }
  2532. static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
  2533. AVFormatContext *fmt_ctx)
  2534. {
  2535. FrameData *fd = frame->opaque_ref ? (FrameData*)frame->opaque_ref->data : NULL;
  2536. AVBPrint pbuf;
  2537. char val_str[128];
  2538. const char *s;
  2539. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2540. writer_print_section_header(w, NULL, SECTION_ID_FRAME);
  2541. s = av_get_media_type_string(stream->codecpar->codec_type);
  2542. if (s) print_str ("media_type", s);
  2543. else print_str_opt("media_type", "unknown");
  2544. print_int("stream_index", stream->index);
  2545. print_int("key_frame", !!(frame->flags & AV_FRAME_FLAG_KEY));
  2546. print_ts ("pts", frame->pts);
  2547. print_time("pts_time", frame->pts, &stream->time_base);
  2548. print_ts ("pkt_dts", frame->pkt_dts);
  2549. print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
  2550. print_ts ("best_effort_timestamp", frame->best_effort_timestamp);
  2551. print_time("best_effort_timestamp_time", frame->best_effort_timestamp, &stream->time_base);
  2552. print_duration_ts ("duration", frame->duration);
  2553. print_duration_time("duration_time", frame->duration, &stream->time_base);
  2554. if (fd && fd->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, fd->pkt_pos);
  2555. else print_str_opt("pkt_pos", "N/A");
  2556. if (fd && fd->pkt_size != -1) print_val ("pkt_size", fd->pkt_size, unit_byte_str);
  2557. else print_str_opt("pkt_size", "N/A");
  2558. switch (stream->codecpar->codec_type) {
  2559. AVRational sar;
  2560. case AVMEDIA_TYPE_VIDEO:
  2561. print_int("width", frame->width);
  2562. print_int("height", frame->height);
  2563. print_int("crop_top", frame->crop_top);
  2564. print_int("crop_bottom", frame->crop_bottom);
  2565. print_int("crop_left", frame->crop_left);
  2566. print_int("crop_right", frame->crop_right);
  2567. s = av_get_pix_fmt_name(frame->format);
  2568. if (s) print_str ("pix_fmt", s);
  2569. else print_str_opt("pix_fmt", "unknown");
  2570. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
  2571. if (sar.num) {
  2572. print_q("sample_aspect_ratio", sar, ':');
  2573. } else {
  2574. print_str_opt("sample_aspect_ratio", "N/A");
  2575. }
  2576. print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
  2577. print_int("interlaced_frame", !!(frame->flags & AV_FRAME_FLAG_INTERLACED));
  2578. print_int("top_field_first", !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
  2579. print_int("lossless", !!(frame->flags & AV_FRAME_FLAG_LOSSLESS));
  2580. print_int("repeat_pict", frame->repeat_pict);
  2581. print_color_range(w, frame->color_range);
  2582. print_color_space(w, frame->colorspace);
  2583. print_primaries(w, frame->color_primaries);
  2584. print_color_trc(w, frame->color_trc);
  2585. print_chroma_location(w, frame->chroma_location);
  2586. break;
  2587. case AVMEDIA_TYPE_AUDIO:
  2588. s = av_get_sample_fmt_name(frame->format);
  2589. if (s) print_str ("sample_fmt", s);
  2590. else print_str_opt("sample_fmt", "unknown");
  2591. print_int("nb_samples", frame->nb_samples);
  2592. print_int("channels", frame->ch_layout.nb_channels);
  2593. if (frame->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
  2594. av_channel_layout_describe(&frame->ch_layout, val_str, sizeof(val_str));
  2595. print_str ("channel_layout", val_str);
  2596. } else
  2597. print_str_opt("channel_layout", "unknown");
  2598. break;
  2599. }
  2600. if (do_show_frame_tags)
  2601. show_tags(w, frame->metadata, SECTION_ID_FRAME_TAGS);
  2602. if (do_show_log)
  2603. show_log(w, SECTION_ID_FRAME_LOGS, SECTION_ID_FRAME_LOG, do_show_log);
  2604. if (frame->nb_side_data)
  2605. print_frame_side_data(w, frame, stream);
  2606. writer_print_section_footer(w);
  2607. av_bprint_finalize(&pbuf, NULL);
  2608. fflush(stdout);
  2609. }
  2610. static av_always_inline int process_frame(WriterContext *w,
  2611. InputFile *ifile,
  2612. AVFrame *frame, const AVPacket *pkt,
  2613. int *packet_new)
  2614. {
  2615. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2616. AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
  2617. AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
  2618. AVSubtitle sub;
  2619. int ret = 0, got_frame = 0;
  2620. clear_log(1);
  2621. if (dec_ctx) {
  2622. switch (par->codec_type) {
  2623. case AVMEDIA_TYPE_VIDEO:
  2624. case AVMEDIA_TYPE_AUDIO:
  2625. if (*packet_new) {
  2626. ret = avcodec_send_packet(dec_ctx, pkt);
  2627. if (ret == AVERROR(EAGAIN)) {
  2628. ret = 0;
  2629. } else if (ret >= 0 || ret == AVERROR_EOF) {
  2630. ret = 0;
  2631. *packet_new = 0;
  2632. }
  2633. }
  2634. if (ret >= 0) {
  2635. ret = avcodec_receive_frame(dec_ctx, frame);
  2636. if (ret >= 0) {
  2637. got_frame = 1;
  2638. } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  2639. ret = 0;
  2640. }
  2641. }
  2642. break;
  2643. case AVMEDIA_TYPE_SUBTITLE:
  2644. if (*packet_new)
  2645. ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
  2646. *packet_new = 0;
  2647. break;
  2648. default:
  2649. *packet_new = 0;
  2650. }
  2651. } else {
  2652. *packet_new = 0;
  2653. }
  2654. if (ret < 0)
  2655. return ret;
  2656. if (got_frame) {
  2657. int is_sub = (par->codec_type == AVMEDIA_TYPE_SUBTITLE);
  2658. nb_streams_frames[pkt->stream_index]++;
  2659. if (do_show_frames)
  2660. if (is_sub)
  2661. show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
  2662. else
  2663. show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
  2664. if (!is_sub && do_analyze_frames) {
  2665. for (int i = 0; i < frame->nb_side_data; i++) {
  2666. if (frame->side_data[i]->type == AV_FRAME_DATA_A53_CC)
  2667. streams_with_closed_captions[pkt->stream_index] = 1;
  2668. else if (frame->side_data[i]->type == AV_FRAME_DATA_FILM_GRAIN_PARAMS)
  2669. streams_with_film_grain[pkt->stream_index] = 1;
  2670. }
  2671. }
  2672. if (is_sub)
  2673. avsubtitle_free(&sub);
  2674. }
  2675. return got_frame || *packet_new;
  2676. }
  2677. static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
  2678. {
  2679. av_log(log_ctx, log_level, "id:%d", interval->id);
  2680. if (interval->has_start) {
  2681. av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
  2682. av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
  2683. } else {
  2684. av_log(log_ctx, log_level, " start:N/A");
  2685. }
  2686. if (interval->has_end) {
  2687. av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
  2688. if (interval->duration_frames)
  2689. av_log(log_ctx, log_level, "#%"PRId64, interval->end);
  2690. else
  2691. av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
  2692. } else {
  2693. av_log(log_ctx, log_level, " end:N/A");
  2694. }
  2695. av_log(log_ctx, log_level, "\n");
  2696. }
  2697. static int read_interval_packets(WriterContext *w, InputFile *ifile,
  2698. const ReadInterval *interval, int64_t *cur_ts)
  2699. {
  2700. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2701. AVPacket *pkt = NULL;
  2702. AVFrame *frame = NULL;
  2703. int ret = 0, i = 0, frame_count = 0;
  2704. int64_t start = -INT64_MAX, end = interval->end;
  2705. int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
  2706. av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
  2707. log_read_interval(interval, NULL, AV_LOG_VERBOSE);
  2708. if (interval->has_start) {
  2709. int64_t target;
  2710. if (interval->start_is_offset) {
  2711. if (*cur_ts == AV_NOPTS_VALUE) {
  2712. av_log(NULL, AV_LOG_ERROR,
  2713. "Could not seek to relative position since current "
  2714. "timestamp is not defined\n");
  2715. ret = AVERROR(EINVAL);
  2716. goto end;
  2717. }
  2718. target = *cur_ts + interval->start;
  2719. } else {
  2720. target = interval->start;
  2721. }
  2722. av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
  2723. av_ts2timestr(target, &AV_TIME_BASE_Q));
  2724. if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
  2725. av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
  2726. interval->start, av_err2str(ret));
  2727. goto end;
  2728. }
  2729. }
  2730. frame = av_frame_alloc();
  2731. if (!frame) {
  2732. ret = AVERROR(ENOMEM);
  2733. goto end;
  2734. }
  2735. pkt = av_packet_alloc();
  2736. if (!pkt) {
  2737. ret = AVERROR(ENOMEM);
  2738. goto end;
  2739. }
  2740. while (!av_read_frame(fmt_ctx, pkt)) {
  2741. if (fmt_ctx->nb_streams > nb_streams) {
  2742. REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
  2743. REALLOCZ_ARRAY_STREAM(nb_streams_packets, nb_streams, fmt_ctx->nb_streams);
  2744. REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
  2745. REALLOCZ_ARRAY_STREAM(streams_with_closed_captions, nb_streams, fmt_ctx->nb_streams);
  2746. REALLOCZ_ARRAY_STREAM(streams_with_film_grain, nb_streams, fmt_ctx->nb_streams);
  2747. nb_streams = fmt_ctx->nb_streams;
  2748. }
  2749. if (selected_streams[pkt->stream_index]) {
  2750. AVRational tb = ifile->streams[pkt->stream_index].st->time_base;
  2751. int64_t pts = pkt->pts != AV_NOPTS_VALUE ? pkt->pts : pkt->dts;
  2752. if (pts != AV_NOPTS_VALUE)
  2753. *cur_ts = av_rescale_q(pts, tb, AV_TIME_BASE_Q);
  2754. if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
  2755. start = *cur_ts;
  2756. has_start = 1;
  2757. }
  2758. if (has_start && !has_end && interval->end_is_offset) {
  2759. end = start + interval->end;
  2760. has_end = 1;
  2761. }
  2762. if (interval->end_is_offset && interval->duration_frames) {
  2763. if (frame_count >= interval->end)
  2764. break;
  2765. } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
  2766. break;
  2767. }
  2768. frame_count++;
  2769. if (do_read_packets) {
  2770. if (do_show_packets)
  2771. show_packet(w, ifile, pkt, i++);
  2772. nb_streams_packets[pkt->stream_index]++;
  2773. }
  2774. if (do_read_frames) {
  2775. int packet_new = 1;
  2776. FrameData *fd;
  2777. pkt->opaque_ref = av_buffer_allocz(sizeof(*fd));
  2778. if (!pkt->opaque_ref) {
  2779. ret = AVERROR(ENOMEM);
  2780. goto end;
  2781. }
  2782. fd = (FrameData*)pkt->opaque_ref->data;
  2783. fd->pkt_pos = pkt->pos;
  2784. fd->pkt_size = pkt->size;
  2785. while (process_frame(w, ifile, frame, pkt, &packet_new) > 0);
  2786. }
  2787. }
  2788. av_packet_unref(pkt);
  2789. }
  2790. av_packet_unref(pkt);
  2791. //Flush remaining frames that are cached in the decoder
  2792. for (i = 0; i < ifile->nb_streams; i++) {
  2793. pkt->stream_index = i;
  2794. if (do_read_frames) {
  2795. while (process_frame(w, ifile, frame, pkt, &(int){1}) > 0);
  2796. if (ifile->streams[i].dec_ctx)
  2797. avcodec_flush_buffers(ifile->streams[i].dec_ctx);
  2798. }
  2799. }
  2800. end:
  2801. av_frame_free(&frame);
  2802. av_packet_free(&pkt);
  2803. if (ret < 0) {
  2804. av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
  2805. log_read_interval(interval, NULL, AV_LOG_ERROR);
  2806. }
  2807. return ret;
  2808. }
  2809. static int read_packets(WriterContext *w, InputFile *ifile)
  2810. {
  2811. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2812. int i, ret = 0;
  2813. int64_t cur_ts = fmt_ctx->start_time;
  2814. if (read_intervals_nb == 0) {
  2815. ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
  2816. ret = read_interval_packets(w, ifile, &interval, &cur_ts);
  2817. } else {
  2818. for (i = 0; i < read_intervals_nb; i++) {
  2819. ret = read_interval_packets(w, ifile, &read_intervals[i], &cur_ts);
  2820. if (ret < 0)
  2821. break;
  2822. }
  2823. }
  2824. return ret;
  2825. }
  2826. static void print_dispositions(WriterContext *w, uint32_t disposition, SectionID section_id)
  2827. {
  2828. writer_print_section_header(w, NULL, section_id);
  2829. for (int i = 0; i < sizeof(disposition) * CHAR_BIT; i++) {
  2830. const char *disposition_str = av_disposition_to_string(1U << i);
  2831. if (disposition_str)
  2832. print_int(disposition_str, !!(disposition & (1U << i)));
  2833. }
  2834. writer_print_section_footer(w);
  2835. }
  2836. #define IN_PROGRAM 1
  2837. #define IN_STREAM_GROUP 2
  2838. static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int container)
  2839. {
  2840. AVStream *stream = ist->st;
  2841. AVCodecParameters *par;
  2842. AVCodecContext *dec_ctx;
  2843. char val_str[128];
  2844. const char *s;
  2845. AVRational sar, dar;
  2846. AVBPrint pbuf;
  2847. const AVCodecDescriptor *cd;
  2848. const SectionID section_header[] = {
  2849. SECTION_ID_STREAM,
  2850. SECTION_ID_PROGRAM_STREAM,
  2851. SECTION_ID_STREAM_GROUP_STREAM,
  2852. };
  2853. const SectionID section_disposition[] = {
  2854. SECTION_ID_STREAM_DISPOSITION,
  2855. SECTION_ID_PROGRAM_STREAM_DISPOSITION,
  2856. SECTION_ID_STREAM_GROUP_STREAM_DISPOSITION,
  2857. };
  2858. const SectionID section_tags[] = {
  2859. SECTION_ID_STREAM_TAGS,
  2860. SECTION_ID_PROGRAM_STREAM_TAGS,
  2861. SECTION_ID_STREAM_GROUP_STREAM_TAGS,
  2862. };
  2863. int ret = 0;
  2864. const char *profile = NULL;
  2865. av_assert0(container < FF_ARRAY_ELEMS(section_header));
  2866. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2867. writer_print_section_header(w, NULL, section_header[container]);
  2868. print_int("index", stream->index);
  2869. par = stream->codecpar;
  2870. dec_ctx = ist->dec_ctx;
  2871. if (cd = avcodec_descriptor_get(par->codec_id)) {
  2872. print_str("codec_name", cd->name);
  2873. if (!do_bitexact) {
  2874. print_str("codec_long_name",
  2875. cd->long_name ? cd->long_name : "unknown");
  2876. }
  2877. } else {
  2878. print_str_opt("codec_name", "unknown");
  2879. if (!do_bitexact) {
  2880. print_str_opt("codec_long_name", "unknown");
  2881. }
  2882. }
  2883. if (!do_bitexact && (profile = avcodec_profile_name(par->codec_id, par->profile)))
  2884. print_str("profile", profile);
  2885. else {
  2886. if (par->profile != AV_PROFILE_UNKNOWN) {
  2887. char profile_num[12];
  2888. snprintf(profile_num, sizeof(profile_num), "%d", par->profile);
  2889. print_str("profile", profile_num);
  2890. } else
  2891. print_str_opt("profile", "unknown");
  2892. }
  2893. s = av_get_media_type_string(par->codec_type);
  2894. if (s) print_str ("codec_type", s);
  2895. else print_str_opt("codec_type", "unknown");
  2896. /* print AVI/FourCC tag */
  2897. print_str("codec_tag_string", av_fourcc2str(par->codec_tag));
  2898. print_fmt("codec_tag", "0x%04"PRIx32, par->codec_tag);
  2899. switch (par->codec_type) {
  2900. case AVMEDIA_TYPE_VIDEO:
  2901. print_int("width", par->width);
  2902. print_int("height", par->height);
  2903. if (dec_ctx) {
  2904. print_int("coded_width", dec_ctx->coded_width);
  2905. print_int("coded_height", dec_ctx->coded_height);
  2906. if (do_analyze_frames) {
  2907. print_int("closed_captions", streams_with_closed_captions[stream->index]);
  2908. print_int("film_grain", streams_with_film_grain[stream->index]);
  2909. }
  2910. }
  2911. print_int("has_b_frames", par->video_delay);
  2912. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
  2913. if (sar.num) {
  2914. print_q("sample_aspect_ratio", sar, ':');
  2915. av_reduce(&dar.num, &dar.den,
  2916. (int64_t) par->width * sar.num,
  2917. (int64_t) par->height * sar.den,
  2918. 1024*1024);
  2919. print_q("display_aspect_ratio", dar, ':');
  2920. } else {
  2921. print_str_opt("sample_aspect_ratio", "N/A");
  2922. print_str_opt("display_aspect_ratio", "N/A");
  2923. }
  2924. s = av_get_pix_fmt_name(par->format);
  2925. if (s) print_str ("pix_fmt", s);
  2926. else print_str_opt("pix_fmt", "unknown");
  2927. print_int("level", par->level);
  2928. print_color_range(w, par->color_range);
  2929. print_color_space(w, par->color_space);
  2930. print_color_trc(w, par->color_trc);
  2931. print_primaries(w, par->color_primaries);
  2932. print_chroma_location(w, par->chroma_location);
  2933. if (par->field_order == AV_FIELD_PROGRESSIVE)
  2934. print_str("field_order", "progressive");
  2935. else if (par->field_order == AV_FIELD_TT)
  2936. print_str("field_order", "tt");
  2937. else if (par->field_order == AV_FIELD_BB)
  2938. print_str("field_order", "bb");
  2939. else if (par->field_order == AV_FIELD_TB)
  2940. print_str("field_order", "tb");
  2941. else if (par->field_order == AV_FIELD_BT)
  2942. print_str("field_order", "bt");
  2943. else
  2944. print_str_opt("field_order", "unknown");
  2945. if (dec_ctx)
  2946. print_int("refs", dec_ctx->refs);
  2947. break;
  2948. case AVMEDIA_TYPE_AUDIO:
  2949. s = av_get_sample_fmt_name(par->format);
  2950. if (s) print_str ("sample_fmt", s);
  2951. else print_str_opt("sample_fmt", "unknown");
  2952. print_val("sample_rate", par->sample_rate, unit_hertz_str);
  2953. print_int("channels", par->ch_layout.nb_channels);
  2954. if (par->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
  2955. av_channel_layout_describe(&par->ch_layout, val_str, sizeof(val_str));
  2956. print_str ("channel_layout", val_str);
  2957. } else {
  2958. print_str_opt("channel_layout", "unknown");
  2959. }
  2960. print_int("bits_per_sample", av_get_bits_per_sample(par->codec_id));
  2961. print_int("initial_padding", par->initial_padding);
  2962. break;
  2963. case AVMEDIA_TYPE_SUBTITLE:
  2964. if (par->width)
  2965. print_int("width", par->width);
  2966. else
  2967. print_str_opt("width", "N/A");
  2968. if (par->height)
  2969. print_int("height", par->height);
  2970. else
  2971. print_str_opt("height", "N/A");
  2972. break;
  2973. }
  2974. if (show_private_data) {
  2975. if (dec_ctx && dec_ctx->codec->priv_class)
  2976. print_private_data(w, dec_ctx->priv_data);
  2977. if (fmt_ctx->iformat->priv_class)
  2978. print_private_data(w, fmt_ctx->priv_data);
  2979. }
  2980. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  2981. else print_str_opt("id", "N/A");
  2982. print_q("r_frame_rate", stream->r_frame_rate, '/');
  2983. print_q("avg_frame_rate", stream->avg_frame_rate, '/');
  2984. print_q("time_base", stream->time_base, '/');
  2985. print_ts ("start_pts", stream->start_time);
  2986. print_time("start_time", stream->start_time, &stream->time_base);
  2987. print_ts ("duration_ts", stream->duration);
  2988. print_time("duration", stream->duration, &stream->time_base);
  2989. if (par->bit_rate > 0) print_val ("bit_rate", par->bit_rate, unit_bit_per_second_str);
  2990. else print_str_opt("bit_rate", "N/A");
  2991. if (dec_ctx && dec_ctx->rc_max_rate > 0)
  2992. print_val ("max_bit_rate", dec_ctx->rc_max_rate, unit_bit_per_second_str);
  2993. else
  2994. print_str_opt("max_bit_rate", "N/A");
  2995. if (dec_ctx && dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
  2996. else print_str_opt("bits_per_raw_sample", "N/A");
  2997. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  2998. else print_str_opt("nb_frames", "N/A");
  2999. if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
  3000. else print_str_opt("nb_read_frames", "N/A");
  3001. if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
  3002. else print_str_opt("nb_read_packets", "N/A");
  3003. if (do_show_data)
  3004. writer_print_data(w, "extradata", par->extradata,
  3005. par->extradata_size);
  3006. if (par->extradata_size > 0) {
  3007. print_int("extradata_size", par->extradata_size);
  3008. writer_print_data_hash(w, "extradata_hash", par->extradata,
  3009. par->extradata_size);
  3010. }
  3011. /* Print disposition information */
  3012. if (do_show_stream_disposition) {
  3013. av_assert0(container < FF_ARRAY_ELEMS(section_disposition));
  3014. print_dispositions(w, stream->disposition, section_disposition[container]);
  3015. }
  3016. if (do_show_stream_tags) {
  3017. av_assert0(container < FF_ARRAY_ELEMS(section_tags));
  3018. ret = show_tags(w, stream->metadata, section_tags[container]);
  3019. }
  3020. if (stream->codecpar->nb_coded_side_data) {
  3021. writer_print_section_header(w, NULL, SECTION_ID_STREAM_SIDE_DATA_LIST);
  3022. for (int i = 0; i < stream->codecpar->nb_coded_side_data; i++) {
  3023. print_pkt_side_data(w, stream->codecpar, &stream->codecpar->coded_side_data[i],
  3024. SECTION_ID_STREAM_SIDE_DATA);
  3025. writer_print_section_footer(w);
  3026. }
  3027. writer_print_section_footer(w);
  3028. }
  3029. writer_print_section_footer(w);
  3030. av_bprint_finalize(&pbuf, NULL);
  3031. fflush(stdout);
  3032. return ret;
  3033. }
  3034. static int show_streams(WriterContext *w, InputFile *ifile)
  3035. {
  3036. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  3037. int i, ret = 0;
  3038. writer_print_section_header(w, NULL, SECTION_ID_STREAMS);
  3039. for (i = 0; i < ifile->nb_streams; i++)
  3040. if (selected_streams[i]) {
  3041. ret = show_stream(w, fmt_ctx, i, &ifile->streams[i], 0);
  3042. if (ret < 0)
  3043. break;
  3044. }
  3045. writer_print_section_footer(w);
  3046. return ret;
  3047. }
  3048. static int show_program(WriterContext *w, InputFile *ifile, AVProgram *program)
  3049. {
  3050. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  3051. int i, ret = 0;
  3052. writer_print_section_header(w, NULL, SECTION_ID_PROGRAM);
  3053. print_int("program_id", program->id);
  3054. print_int("program_num", program->program_num);
  3055. print_int("nb_streams", program->nb_stream_indexes);
  3056. print_int("pmt_pid", program->pmt_pid);
  3057. print_int("pcr_pid", program->pcr_pid);
  3058. if (do_show_program_tags)
  3059. ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
  3060. if (ret < 0)
  3061. goto end;
  3062. writer_print_section_header(w, NULL, SECTION_ID_PROGRAM_STREAMS);
  3063. for (i = 0; i < program->nb_stream_indexes; i++) {
  3064. if (selected_streams[program->stream_index[i]]) {
  3065. ret = show_stream(w, fmt_ctx, program->stream_index[i], &ifile->streams[program->stream_index[i]], IN_PROGRAM);
  3066. if (ret < 0)
  3067. break;
  3068. }
  3069. }
  3070. writer_print_section_footer(w);
  3071. end:
  3072. writer_print_section_footer(w);
  3073. return ret;
  3074. }
  3075. static int show_programs(WriterContext *w, InputFile *ifile)
  3076. {
  3077. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  3078. int i, ret = 0;
  3079. writer_print_section_header(w, NULL, SECTION_ID_PROGRAMS);
  3080. for (i = 0; i < fmt_ctx->nb_programs; i++) {
  3081. AVProgram *program = fmt_ctx->programs[i];
  3082. if (!program)
  3083. continue;
  3084. ret = show_program(w, ifile, program);
  3085. if (ret < 0)
  3086. break;
  3087. }
  3088. writer_print_section_footer(w);
  3089. return ret;
  3090. }
  3091. static void print_tile_grid_params(WriterContext *w, const AVStreamGroup *stg,
  3092. const AVStreamGroupTileGrid *tile_grid)
  3093. {
  3094. writer_print_section_header(w, stg, SECTION_ID_STREAM_GROUP_COMPONENT);
  3095. print_int("nb_tiles", tile_grid->nb_tiles);
  3096. print_int("coded_width", tile_grid->coded_width);
  3097. print_int("coded_height", tile_grid->coded_height);
  3098. print_int("horizontal_offset", tile_grid->horizontal_offset);
  3099. print_int("vertical_offset", tile_grid->vertical_offset);
  3100. print_int("width", tile_grid->width);
  3101. print_int("height", tile_grid->height);
  3102. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP_SUBCOMPONENTS);
  3103. for (int i = 0; i < tile_grid->nb_tiles; i++) {
  3104. writer_print_section_header(w, "tile_offset", SECTION_ID_STREAM_GROUP_SUBCOMPONENT);
  3105. print_int("stream_index", tile_grid->offsets[i].idx);
  3106. print_int("tile_horizontal_offset", tile_grid->offsets[i].horizontal);
  3107. print_int("tile_vertical_offset", tile_grid->offsets[i].vertical);
  3108. writer_print_section_footer(w);
  3109. }
  3110. writer_print_section_footer(w);
  3111. writer_print_section_footer(w);
  3112. }
  3113. static void print_iamf_param_definition(WriterContext *w, const char *name,
  3114. const AVIAMFParamDefinition *param, SectionID section_id)
  3115. {
  3116. SectionID subsection_id, parameter_section_id;
  3117. subsection_id = sections[section_id].children_ids[0];
  3118. av_assert0(subsection_id != -1);
  3119. parameter_section_id = sections[subsection_id].children_ids[0];
  3120. av_assert0(parameter_section_id != -1);
  3121. writer_print_section_header(w, "IAMF Param Definition", section_id);
  3122. print_str("name", name);
  3123. print_int("nb_subblocks", param->nb_subblocks);
  3124. print_int("type", param->type);
  3125. print_int("parameter_id", param->parameter_id);
  3126. print_int("parameter_rate", param->parameter_rate);
  3127. print_int("duration", param->duration);
  3128. print_int("constant_subblock_duration", param->constant_subblock_duration);
  3129. if (param->nb_subblocks > 0)
  3130. writer_print_section_header(w, NULL, subsection_id);
  3131. for (int i = 0; i < param->nb_subblocks; i++) {
  3132. const void *subblock = av_iamf_param_definition_get_subblock(param, i);
  3133. switch(param->type) {
  3134. case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
  3135. const AVIAMFMixGain *mix = subblock;
  3136. writer_print_section_header(w, "IAMF Mix Gain Parameters", parameter_section_id);
  3137. print_int("subblock_duration", mix->subblock_duration);
  3138. print_int("animation_type", mix->animation_type);
  3139. print_q("start_point_value", mix->start_point_value, '/');
  3140. print_q("end_point_value", mix->end_point_value, '/');
  3141. print_q("control_point_value", mix->control_point_value, '/');
  3142. print_q("control_point_relative_time", mix->control_point_relative_time, '/');
  3143. writer_print_section_footer(w); // parameter_section_id
  3144. break;
  3145. }
  3146. case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
  3147. const AVIAMFDemixingInfo *demix = subblock;
  3148. writer_print_section_header(w, "IAMF Demixing Info", parameter_section_id);
  3149. print_int("subblock_duration", demix->subblock_duration);
  3150. print_int("dmixp_mode", demix->dmixp_mode);
  3151. writer_print_section_footer(w); // parameter_section_id
  3152. break;
  3153. }
  3154. case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
  3155. const AVIAMFReconGain *recon = subblock;
  3156. writer_print_section_header(w, "IAMF Recon Gain", parameter_section_id);
  3157. print_int("subblock_duration", recon->subblock_duration);
  3158. writer_print_section_footer(w); // parameter_section_id
  3159. break;
  3160. }
  3161. }
  3162. }
  3163. if (param->nb_subblocks > 0)
  3164. writer_print_section_footer(w); // subsection_id
  3165. writer_print_section_footer(w); // section_id
  3166. }
  3167. static void print_iamf_audio_element_params(WriterContext *w, const AVStreamGroup *stg,
  3168. const AVIAMFAudioElement *audio_element)
  3169. {
  3170. writer_print_section_header(w, stg, SECTION_ID_STREAM_GROUP_COMPONENT);
  3171. print_int("nb_layers", audio_element->nb_layers);
  3172. print_int("audio_element_type", audio_element->audio_element_type);
  3173. print_int("default_w", audio_element->default_w);
  3174. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP_SUBCOMPONENTS);
  3175. for (int i = 0; i < audio_element->nb_layers; i++) {
  3176. const AVIAMFLayer *layer = audio_element->layers[i];
  3177. char val_str[128];
  3178. writer_print_section_header(w, "IAMF Audio Layer", SECTION_ID_STREAM_GROUP_SUBCOMPONENT);
  3179. av_channel_layout_describe(&layer->ch_layout, val_str, sizeof(val_str));
  3180. print_str("channel_layout", val_str);
  3181. if (audio_element->audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_CHANNEL) {
  3182. print_int("output_gain_flags", layer->output_gain_flags);
  3183. print_q("output_gain", layer->output_gain, '/');
  3184. } else if (audio_element->audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE)
  3185. print_int("ambisonics_mode", layer->ambisonics_mode);
  3186. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_SUBCOMPONENT
  3187. }
  3188. if (audio_element->demixing_info)
  3189. print_iamf_param_definition(w, "demixing_info", audio_element->demixing_info,
  3190. SECTION_ID_STREAM_GROUP_SUBCOMPONENT);
  3191. if (audio_element->recon_gain_info)
  3192. print_iamf_param_definition(w, "recon_gain_info", audio_element->recon_gain_info,
  3193. SECTION_ID_STREAM_GROUP_SUBCOMPONENT);
  3194. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_SUBCOMPONENTS
  3195. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_COMPONENT
  3196. }
  3197. static void print_iamf_submix_params(WriterContext *w, const AVIAMFSubmix *submix)
  3198. {
  3199. writer_print_section_header(w, "IAMF Submix", SECTION_ID_STREAM_GROUP_SUBCOMPONENT);
  3200. print_int("nb_elements", submix->nb_elements);
  3201. print_int("nb_layouts", submix->nb_layouts);
  3202. print_q("default_mix_gain", submix->default_mix_gain, '/');
  3203. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP_PIECES);
  3204. for (int i = 0; i < submix->nb_elements; i++) {
  3205. const AVIAMFSubmixElement *element = submix->elements[i];
  3206. writer_print_section_header(w, "IAMF Submix Element", SECTION_ID_STREAM_GROUP_PIECE);
  3207. print_int("stream_id", element->audio_element_id);
  3208. print_q("default_mix_gain", element->default_mix_gain, '/');
  3209. print_int("headphones_rendering_mode", element->headphones_rendering_mode);
  3210. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP_SUBPIECES);
  3211. if (element->annotations) {
  3212. const AVDictionaryEntry *annotation = NULL;
  3213. writer_print_section_header(w, "IAMF Annotations", SECTION_ID_STREAM_GROUP_SUBPIECE);
  3214. while (annotation = av_dict_iterate(element->annotations, annotation))
  3215. print_str(annotation->key, annotation->value);
  3216. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_SUBPIECE
  3217. }
  3218. if (element->element_mix_config)
  3219. print_iamf_param_definition(w, "element_mix_config", element->element_mix_config,
  3220. SECTION_ID_STREAM_GROUP_SUBPIECE);
  3221. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_SUBPIECES
  3222. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_PIECE
  3223. }
  3224. if (submix->output_mix_config)
  3225. print_iamf_param_definition(w, "output_mix_config", submix->output_mix_config,
  3226. SECTION_ID_STREAM_GROUP_PIECE);
  3227. for (int i = 0; i < submix->nb_layouts; i++) {
  3228. const AVIAMFSubmixLayout *layout = submix->layouts[i];
  3229. char val_str[128];
  3230. writer_print_section_header(w, "IAMF Submix Layout", SECTION_ID_STREAM_GROUP_PIECE);
  3231. av_channel_layout_describe(&layout->sound_system, val_str, sizeof(val_str));
  3232. print_str("sound_system", val_str);
  3233. print_q("integrated_loudness", layout->integrated_loudness, '/');
  3234. print_q("digital_peak", layout->digital_peak, '/');
  3235. print_q("true_peak", layout->true_peak, '/');
  3236. print_q("dialogue_anchored_loudness", layout->dialogue_anchored_loudness, '/');
  3237. print_q("album_anchored_loudness", layout->album_anchored_loudness, '/');
  3238. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_PIECE
  3239. }
  3240. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_PIECES
  3241. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_SUBCOMPONENT
  3242. }
  3243. static void print_iamf_mix_presentation_params(WriterContext *w, const AVStreamGroup *stg,
  3244. const AVIAMFMixPresentation *mix_presentation)
  3245. {
  3246. writer_print_section_header(w, stg, SECTION_ID_STREAM_GROUP_COMPONENT);
  3247. print_int("nb_submixes", mix_presentation->nb_submixes);
  3248. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP_SUBCOMPONENTS);
  3249. if (mix_presentation->annotations) {
  3250. const AVDictionaryEntry *annotation = NULL;
  3251. writer_print_section_header(w, "IAMF Annotations", SECTION_ID_STREAM_GROUP_SUBCOMPONENT);
  3252. while (annotation = av_dict_iterate(mix_presentation->annotations, annotation))
  3253. print_str(annotation->key, annotation->value);
  3254. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_SUBCOMPONENT
  3255. }
  3256. for (int i = 0; i < mix_presentation->nb_submixes; i++)
  3257. print_iamf_submix_params(w, mix_presentation->submixes[i]);
  3258. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_SUBCOMPONENTS
  3259. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_COMPONENT
  3260. }
  3261. static void print_stream_group_params(WriterContext *w, AVStreamGroup *stg)
  3262. {
  3263. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP_COMPONENTS);
  3264. if (stg->type == AV_STREAM_GROUP_PARAMS_TILE_GRID)
  3265. print_tile_grid_params(w, stg, stg->params.tile_grid);
  3266. else if (stg->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT)
  3267. print_iamf_audio_element_params(w, stg, stg->params.iamf_audio_element);
  3268. else if (stg->type == AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION)
  3269. print_iamf_mix_presentation_params(w, stg, stg->params.iamf_mix_presentation);
  3270. writer_print_section_footer(w); // SECTION_ID_STREAM_GROUP_COMPONENTS
  3271. }
  3272. static int show_stream_group(WriterContext *w, InputFile *ifile, AVStreamGroup *stg)
  3273. {
  3274. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  3275. AVBPrint pbuf;
  3276. int i, ret = 0;
  3277. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  3278. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP);
  3279. print_int("index", stg->index);
  3280. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%"PRIx64, stg->id);
  3281. else print_str_opt("id", "N/A");
  3282. print_int("nb_streams", stg->nb_streams);
  3283. if (stg->type != AV_STREAM_GROUP_PARAMS_NONE)
  3284. print_str("type", av_x_if_null(avformat_stream_group_name(stg->type), "unknown"));
  3285. else
  3286. print_str_opt("type", "unknown");
  3287. if (do_show_stream_group_components)
  3288. print_stream_group_params(w, stg);
  3289. /* Print disposition information */
  3290. if (do_show_stream_group_disposition)
  3291. print_dispositions(w, stg->disposition, SECTION_ID_STREAM_GROUP_DISPOSITION);
  3292. if (do_show_stream_group_tags)
  3293. ret = show_tags(w, stg->metadata, SECTION_ID_STREAM_GROUP_TAGS);
  3294. if (ret < 0)
  3295. goto end;
  3296. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUP_STREAMS);
  3297. for (i = 0; i < stg->nb_streams; i++) {
  3298. if (selected_streams[stg->streams[i]->index]) {
  3299. ret = show_stream(w, fmt_ctx, stg->streams[i]->index, &ifile->streams[stg->streams[i]->index], IN_STREAM_GROUP);
  3300. if (ret < 0)
  3301. break;
  3302. }
  3303. }
  3304. writer_print_section_footer(w);
  3305. end:
  3306. av_bprint_finalize(&pbuf, NULL);
  3307. writer_print_section_footer(w);
  3308. return ret;
  3309. }
  3310. static int show_stream_groups(WriterContext *w, InputFile *ifile)
  3311. {
  3312. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  3313. int i, ret = 0;
  3314. writer_print_section_header(w, NULL, SECTION_ID_STREAM_GROUPS);
  3315. for (i = 0; i < fmt_ctx->nb_stream_groups; i++) {
  3316. AVStreamGroup *stg = fmt_ctx->stream_groups[i];
  3317. ret = show_stream_group(w, ifile, stg);
  3318. if (ret < 0)
  3319. break;
  3320. }
  3321. writer_print_section_footer(w);
  3322. return ret;
  3323. }
  3324. static int show_chapters(WriterContext *w, InputFile *ifile)
  3325. {
  3326. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  3327. int i, ret = 0;
  3328. writer_print_section_header(w, NULL, SECTION_ID_CHAPTERS);
  3329. for (i = 0; i < fmt_ctx->nb_chapters; i++) {
  3330. AVChapter *chapter = fmt_ctx->chapters[i];
  3331. writer_print_section_header(w, NULL, SECTION_ID_CHAPTER);
  3332. print_int("id", chapter->id);
  3333. print_q ("time_base", chapter->time_base, '/');
  3334. print_int("start", chapter->start);
  3335. print_time("start_time", chapter->start, &chapter->time_base);
  3336. print_int("end", chapter->end);
  3337. print_time("end_time", chapter->end, &chapter->time_base);
  3338. if (do_show_chapter_tags)
  3339. ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
  3340. writer_print_section_footer(w);
  3341. }
  3342. writer_print_section_footer(w);
  3343. return ret;
  3344. }
  3345. static int show_format(WriterContext *w, InputFile *ifile)
  3346. {
  3347. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  3348. char val_str[128];
  3349. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  3350. int ret = 0;
  3351. writer_print_section_header(w, NULL, SECTION_ID_FORMAT);
  3352. print_str_validate("filename", fmt_ctx->url);
  3353. print_int("nb_streams", fmt_ctx->nb_streams);
  3354. print_int("nb_programs", fmt_ctx->nb_programs);
  3355. print_int("nb_stream_groups", fmt_ctx->nb_stream_groups);
  3356. print_str("format_name", fmt_ctx->iformat->name);
  3357. if (!do_bitexact) {
  3358. if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
  3359. else print_str_opt("format_long_name", "unknown");
  3360. }
  3361. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  3362. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  3363. if (size >= 0) print_val ("size", size, unit_byte_str);
  3364. else print_str_opt("size", "N/A");
  3365. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  3366. else print_str_opt("bit_rate", "N/A");
  3367. print_int("probe_score", fmt_ctx->probe_score);
  3368. if (do_show_format_tags)
  3369. ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
  3370. writer_print_section_footer(w);
  3371. fflush(stdout);
  3372. return ret;
  3373. }
  3374. static void show_error(WriterContext *w, int err)
  3375. {
  3376. writer_print_section_header(w, NULL, SECTION_ID_ERROR);
  3377. print_int("code", err);
  3378. print_str("string", av_err2str(err));
  3379. writer_print_section_footer(w);
  3380. }
  3381. static int open_input_file(InputFile *ifile, const char *filename,
  3382. const char *print_filename)
  3383. {
  3384. int err, i;
  3385. AVFormatContext *fmt_ctx = NULL;
  3386. const AVDictionaryEntry *t = NULL;
  3387. int scan_all_pmts_set = 0;
  3388. fmt_ctx = avformat_alloc_context();
  3389. if (!fmt_ctx)
  3390. return AVERROR(ENOMEM);
  3391. if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  3392. av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  3393. scan_all_pmts_set = 1;
  3394. }
  3395. if ((err = avformat_open_input(&fmt_ctx, filename,
  3396. iformat, &format_opts)) < 0) {
  3397. print_error(filename, err);
  3398. return err;
  3399. }
  3400. if (print_filename) {
  3401. av_freep(&fmt_ctx->url);
  3402. fmt_ctx->url = av_strdup(print_filename);
  3403. }
  3404. ifile->fmt_ctx = fmt_ctx;
  3405. if (scan_all_pmts_set)
  3406. av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  3407. while ((t = av_dict_iterate(format_opts, t)))
  3408. av_log(NULL, AV_LOG_WARNING, "Option %s skipped - not known to demuxer.\n", t->key);
  3409. if (find_stream_info) {
  3410. AVDictionary **opts;
  3411. int orig_nb_streams = fmt_ctx->nb_streams;
  3412. err = setup_find_stream_info_opts(fmt_ctx, codec_opts, &opts);
  3413. if (err < 0)
  3414. return err;
  3415. err = avformat_find_stream_info(fmt_ctx, opts);
  3416. for (i = 0; i < orig_nb_streams; i++)
  3417. av_dict_free(&opts[i]);
  3418. av_freep(&opts);
  3419. if (err < 0) {
  3420. print_error(filename, err);
  3421. return err;
  3422. }
  3423. }
  3424. av_dump_format(fmt_ctx, 0, filename, 0);
  3425. ifile->streams = av_calloc(fmt_ctx->nb_streams, sizeof(*ifile->streams));
  3426. if (!ifile->streams)
  3427. exit(1);
  3428. ifile->nb_streams = fmt_ctx->nb_streams;
  3429. /* bind a decoder to each input stream */
  3430. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  3431. InputStream *ist = &ifile->streams[i];
  3432. AVStream *stream = fmt_ctx->streams[i];
  3433. const AVCodec *codec;
  3434. ist->st = stream;
  3435. if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
  3436. av_log(NULL, AV_LOG_WARNING,
  3437. "Failed to probe codec for input stream %d\n",
  3438. stream->index);
  3439. continue;
  3440. }
  3441. codec = avcodec_find_decoder(stream->codecpar->codec_id);
  3442. if (!codec) {
  3443. av_log(NULL, AV_LOG_WARNING,
  3444. "Unsupported codec with id %d for input stream %d\n",
  3445. stream->codecpar->codec_id, stream->index);
  3446. continue;
  3447. }
  3448. {
  3449. AVDictionary *opts;
  3450. err = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
  3451. fmt_ctx, stream, codec, &opts, NULL);
  3452. if (err < 0)
  3453. exit(1);
  3454. ist->dec_ctx = avcodec_alloc_context3(codec);
  3455. if (!ist->dec_ctx)
  3456. exit(1);
  3457. err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
  3458. if (err < 0)
  3459. exit(1);
  3460. if (do_show_log) {
  3461. // For loging it is needed to disable at least frame threads as otherwise
  3462. // the log information would need to be reordered and matches up to contexts and frames
  3463. // That is in fact possible but not trivial
  3464. av_dict_set(&codec_opts, "threads", "1", 0);
  3465. }
  3466. av_dict_set(&opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
  3467. ist->dec_ctx->pkt_timebase = stream->time_base;
  3468. if (avcodec_open2(ist->dec_ctx, codec, &opts) < 0) {
  3469. av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
  3470. stream->index);
  3471. exit(1);
  3472. }
  3473. if ((t = av_dict_iterate(opts, NULL))) {
  3474. av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
  3475. t->key, stream->index);
  3476. return AVERROR_OPTION_NOT_FOUND;
  3477. }
  3478. }
  3479. }
  3480. ifile->fmt_ctx = fmt_ctx;
  3481. return 0;
  3482. }
  3483. static void close_input_file(InputFile *ifile)
  3484. {
  3485. int i;
  3486. /* close decoder for each stream */
  3487. for (i = 0; i < ifile->nb_streams; i++)
  3488. avcodec_free_context(&ifile->streams[i].dec_ctx);
  3489. av_freep(&ifile->streams);
  3490. ifile->nb_streams = 0;
  3491. avformat_close_input(&ifile->fmt_ctx);
  3492. }
  3493. static int probe_file(WriterContext *wctx, const char *filename,
  3494. const char *print_filename)
  3495. {
  3496. InputFile ifile = { 0 };
  3497. int ret, i;
  3498. int section_id;
  3499. do_analyze_frames = do_analyze_frames && do_show_streams;
  3500. do_read_frames = do_show_frames || do_count_frames || do_analyze_frames;
  3501. do_read_packets = do_show_packets || do_count_packets;
  3502. ret = open_input_file(&ifile, filename, print_filename);
  3503. if (ret < 0)
  3504. goto end;
  3505. #define CHECK_END if (ret < 0) goto end
  3506. nb_streams = ifile.fmt_ctx->nb_streams;
  3507. REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,ifile.fmt_ctx->nb_streams);
  3508. REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,ifile.fmt_ctx->nb_streams);
  3509. REALLOCZ_ARRAY_STREAM(selected_streams,0,ifile.fmt_ctx->nb_streams);
  3510. REALLOCZ_ARRAY_STREAM(streams_with_closed_captions,0,ifile.fmt_ctx->nb_streams);
  3511. REALLOCZ_ARRAY_STREAM(streams_with_film_grain,0,ifile.fmt_ctx->nb_streams);
  3512. for (i = 0; i < ifile.fmt_ctx->nb_streams; i++) {
  3513. if (stream_specifier) {
  3514. ret = avformat_match_stream_specifier(ifile.fmt_ctx,
  3515. ifile.fmt_ctx->streams[i],
  3516. stream_specifier);
  3517. CHECK_END;
  3518. else
  3519. selected_streams[i] = ret;
  3520. ret = 0;
  3521. } else {
  3522. selected_streams[i] = 1;
  3523. }
  3524. if (!selected_streams[i])
  3525. ifile.fmt_ctx->streams[i]->discard = AVDISCARD_ALL;
  3526. }
  3527. if (do_read_frames || do_read_packets) {
  3528. if (do_show_frames && do_show_packets &&
  3529. wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
  3530. section_id = SECTION_ID_PACKETS_AND_FRAMES;
  3531. else if (do_show_packets && !do_show_frames)
  3532. section_id = SECTION_ID_PACKETS;
  3533. else // (!do_show_packets && do_show_frames)
  3534. section_id = SECTION_ID_FRAMES;
  3535. if (do_show_frames || do_show_packets)
  3536. writer_print_section_header(wctx, NULL, section_id);
  3537. ret = read_packets(wctx, &ifile);
  3538. if (do_show_frames || do_show_packets)
  3539. writer_print_section_footer(wctx);
  3540. CHECK_END;
  3541. }
  3542. if (do_show_programs) {
  3543. ret = show_programs(wctx, &ifile);
  3544. CHECK_END;
  3545. }
  3546. if (do_show_stream_groups) {
  3547. ret = show_stream_groups(wctx, &ifile);
  3548. CHECK_END;
  3549. }
  3550. if (do_show_streams) {
  3551. ret = show_streams(wctx, &ifile);
  3552. CHECK_END;
  3553. }
  3554. if (do_show_chapters) {
  3555. ret = show_chapters(wctx, &ifile);
  3556. CHECK_END;
  3557. }
  3558. if (do_show_format) {
  3559. ret = show_format(wctx, &ifile);
  3560. CHECK_END;
  3561. }
  3562. end:
  3563. if (ifile.fmt_ctx)
  3564. close_input_file(&ifile);
  3565. av_freep(&nb_streams_frames);
  3566. av_freep(&nb_streams_packets);
  3567. av_freep(&selected_streams);
  3568. av_freep(&streams_with_closed_captions);
  3569. av_freep(&streams_with_film_grain);
  3570. return ret;
  3571. }
  3572. static void show_usage(void)
  3573. {
  3574. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  3575. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name);
  3576. av_log(NULL, AV_LOG_INFO, "\n");
  3577. }
  3578. static void ffprobe_show_program_version(WriterContext *w)
  3579. {
  3580. AVBPrint pbuf;
  3581. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  3582. writer_print_section_header(w, NULL, SECTION_ID_PROGRAM_VERSION);
  3583. print_str("version", FFMPEG_VERSION);
  3584. print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
  3585. program_birth_year, CONFIG_THIS_YEAR);
  3586. print_str("compiler_ident", CC_IDENT);
  3587. print_str("configuration", FFMPEG_CONFIGURATION);
  3588. writer_print_section_footer(w);
  3589. av_bprint_finalize(&pbuf, NULL);
  3590. }
  3591. #define SHOW_LIB_VERSION(libname, LIBNAME) \
  3592. do { \
  3593. if (CONFIG_##LIBNAME) { \
  3594. unsigned int version = libname##_version(); \
  3595. writer_print_section_header(w, NULL, SECTION_ID_LIBRARY_VERSION); \
  3596. print_str("name", "lib" #libname); \
  3597. print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
  3598. print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
  3599. print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
  3600. print_int("version", version); \
  3601. print_str("ident", LIB##LIBNAME##_IDENT); \
  3602. writer_print_section_footer(w); \
  3603. } \
  3604. } while (0)
  3605. static void ffprobe_show_library_versions(WriterContext *w)
  3606. {
  3607. writer_print_section_header(w, NULL, SECTION_ID_LIBRARY_VERSIONS);
  3608. SHOW_LIB_VERSION(avutil, AVUTIL);
  3609. SHOW_LIB_VERSION(avcodec, AVCODEC);
  3610. SHOW_LIB_VERSION(avformat, AVFORMAT);
  3611. SHOW_LIB_VERSION(avdevice, AVDEVICE);
  3612. SHOW_LIB_VERSION(avfilter, AVFILTER);
  3613. SHOW_LIB_VERSION(swscale, SWSCALE);
  3614. SHOW_LIB_VERSION(swresample, SWRESAMPLE);
  3615. SHOW_LIB_VERSION(postproc, POSTPROC);
  3616. writer_print_section_footer(w);
  3617. }
  3618. #define PRINT_PIX_FMT_FLAG(flagname, name) \
  3619. do { \
  3620. print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
  3621. } while (0)
  3622. static void ffprobe_show_pixel_formats(WriterContext *w)
  3623. {
  3624. const AVPixFmtDescriptor *pixdesc = NULL;
  3625. int i, n;
  3626. writer_print_section_header(w, NULL, SECTION_ID_PIXEL_FORMATS);
  3627. while (pixdesc = av_pix_fmt_desc_next(pixdesc)) {
  3628. writer_print_section_header(w, NULL, SECTION_ID_PIXEL_FORMAT);
  3629. print_str("name", pixdesc->name);
  3630. print_int("nb_components", pixdesc->nb_components);
  3631. if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
  3632. print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
  3633. print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
  3634. } else {
  3635. print_str_opt("log2_chroma_w", "N/A");
  3636. print_str_opt("log2_chroma_h", "N/A");
  3637. }
  3638. n = av_get_bits_per_pixel(pixdesc);
  3639. if (n) print_int ("bits_per_pixel", n);
  3640. else print_str_opt("bits_per_pixel", "N/A");
  3641. if (do_show_pixel_format_flags) {
  3642. writer_print_section_header(w, NULL, SECTION_ID_PIXEL_FORMAT_FLAGS);
  3643. PRINT_PIX_FMT_FLAG(BE, "big_endian");
  3644. PRINT_PIX_FMT_FLAG(PAL, "palette");
  3645. PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
  3646. PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
  3647. PRINT_PIX_FMT_FLAG(PLANAR, "planar");
  3648. PRINT_PIX_FMT_FLAG(RGB, "rgb");
  3649. PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
  3650. writer_print_section_footer(w);
  3651. }
  3652. if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
  3653. writer_print_section_header(w, NULL, SECTION_ID_PIXEL_FORMAT_COMPONENTS);
  3654. for (i = 0; i < pixdesc->nb_components; i++) {
  3655. writer_print_section_header(w, NULL, SECTION_ID_PIXEL_FORMAT_COMPONENT);
  3656. print_int("index", i + 1);
  3657. print_int("bit_depth", pixdesc->comp[i].depth);
  3658. writer_print_section_footer(w);
  3659. }
  3660. writer_print_section_footer(w);
  3661. }
  3662. writer_print_section_footer(w);
  3663. }
  3664. writer_print_section_footer(w);
  3665. }
  3666. static int opt_show_optional_fields(void *optctx, const char *opt, const char *arg)
  3667. {
  3668. if (!av_strcasecmp(arg, "always")) show_optional_fields = SHOW_OPTIONAL_FIELDS_ALWAYS;
  3669. else if (!av_strcasecmp(arg, "never")) show_optional_fields = SHOW_OPTIONAL_FIELDS_NEVER;
  3670. else if (!av_strcasecmp(arg, "auto")) show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
  3671. if (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO && av_strcasecmp(arg, "auto")) {
  3672. double num;
  3673. int ret = parse_number("show_optional_fields", arg, OPT_TYPE_INT,
  3674. SHOW_OPTIONAL_FIELDS_AUTO, SHOW_OPTIONAL_FIELDS_ALWAYS, &num);
  3675. if (ret < 0)
  3676. return ret;
  3677. show_optional_fields = num;
  3678. }
  3679. return 0;
  3680. }
  3681. static int opt_format(void *optctx, const char *opt, const char *arg)
  3682. {
  3683. iformat = av_find_input_format(arg);
  3684. if (!iformat) {
  3685. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  3686. return AVERROR(EINVAL);
  3687. }
  3688. return 0;
  3689. }
  3690. static inline void mark_section_show_entries(SectionID section_id,
  3691. int show_all_entries, AVDictionary *entries)
  3692. {
  3693. struct section *section = &sections[section_id];
  3694. section->show_all_entries = show_all_entries;
  3695. if (show_all_entries) {
  3696. for (const SectionID *id = section->children_ids; *id != -1; id++)
  3697. mark_section_show_entries(*id, show_all_entries, entries);
  3698. } else {
  3699. av_dict_copy(&section->entries_to_show, entries, 0);
  3700. }
  3701. }
  3702. static int match_section(const char *section_name,
  3703. int show_all_entries, AVDictionary *entries)
  3704. {
  3705. int i, ret = 0;
  3706. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
  3707. const struct section *section = &sections[i];
  3708. if (!strcmp(section_name, section->name) ||
  3709. (section->unique_name && !strcmp(section_name, section->unique_name))) {
  3710. av_log(NULL, AV_LOG_DEBUG,
  3711. "'%s' matches section with unique name '%s'\n", section_name,
  3712. (char *)av_x_if_null(section->unique_name, section->name));
  3713. ret++;
  3714. mark_section_show_entries(section->id, show_all_entries, entries);
  3715. }
  3716. }
  3717. return ret;
  3718. }
  3719. static int opt_show_entries(void *optctx, const char *opt, const char *arg)
  3720. {
  3721. const char *p = arg;
  3722. int ret = 0;
  3723. while (*p) {
  3724. AVDictionary *entries = NULL;
  3725. char *section_name = av_get_token(&p, "=:");
  3726. int show_all_entries = 0;
  3727. if (!section_name) {
  3728. av_log(NULL, AV_LOG_ERROR,
  3729. "Missing section name for option '%s'\n", opt);
  3730. return AVERROR(EINVAL);
  3731. }
  3732. if (*p == '=') {
  3733. p++;
  3734. while (*p && *p != ':') {
  3735. char *entry = av_get_token(&p, ",:");
  3736. if (!entry)
  3737. break;
  3738. av_log(NULL, AV_LOG_VERBOSE,
  3739. "Adding '%s' to the entries to show in section '%s'\n",
  3740. entry, section_name);
  3741. av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
  3742. if (*p == ',')
  3743. p++;
  3744. }
  3745. } else {
  3746. show_all_entries = 1;
  3747. }
  3748. ret = match_section(section_name, show_all_entries, entries);
  3749. if (ret == 0) {
  3750. av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
  3751. ret = AVERROR(EINVAL);
  3752. }
  3753. av_dict_free(&entries);
  3754. av_free(section_name);
  3755. if (ret <= 0)
  3756. break;
  3757. if (*p)
  3758. p++;
  3759. }
  3760. return ret;
  3761. }
  3762. static int opt_input_file(void *optctx, const char *arg)
  3763. {
  3764. if (input_filename) {
  3765. av_log(NULL, AV_LOG_ERROR,
  3766. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  3767. arg, input_filename);
  3768. return AVERROR(EINVAL);
  3769. }
  3770. if (!strcmp(arg, "-"))
  3771. arg = "fd:";
  3772. input_filename = av_strdup(arg);
  3773. if (!input_filename)
  3774. return AVERROR(ENOMEM);
  3775. return 0;
  3776. }
  3777. static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
  3778. {
  3779. opt_input_file(optctx, arg);
  3780. return 0;
  3781. }
  3782. static int opt_output_file_o(void *optctx, const char *opt, const char *arg)
  3783. {
  3784. if (output_filename) {
  3785. av_log(NULL, AV_LOG_ERROR,
  3786. "Argument '%s' provided as output filename, but '%s' was already specified.\n",
  3787. arg, output_filename);
  3788. return AVERROR(EINVAL);
  3789. }
  3790. if (!strcmp(arg, "-"))
  3791. arg = "fd:";
  3792. output_filename = av_strdup(arg);
  3793. if (!output_filename)
  3794. return AVERROR(ENOMEM);
  3795. return 0;
  3796. }
  3797. static int opt_print_filename(void *optctx, const char *opt, const char *arg)
  3798. {
  3799. av_freep(&print_input_filename);
  3800. print_input_filename = av_strdup(arg);
  3801. return print_input_filename ? 0 : AVERROR(ENOMEM);
  3802. }
  3803. void show_help_default(const char *opt, const char *arg)
  3804. {
  3805. av_log_set_callback(log_callback_help);
  3806. show_usage();
  3807. show_help_options(options, "Main options:", 0, 0);
  3808. printf("\n");
  3809. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  3810. show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  3811. }
  3812. /**
  3813. * Parse interval specification, according to the format:
  3814. * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
  3815. * INTERVALS ::= INTERVAL[,INTERVALS]
  3816. */
  3817. static int parse_read_interval(const char *interval_spec,
  3818. ReadInterval *interval)
  3819. {
  3820. int ret = 0;
  3821. char *next, *p, *spec = av_strdup(interval_spec);
  3822. if (!spec)
  3823. return AVERROR(ENOMEM);
  3824. if (!*spec) {
  3825. av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
  3826. ret = AVERROR(EINVAL);
  3827. goto end;
  3828. }
  3829. p = spec;
  3830. next = strchr(spec, '%');
  3831. if (next)
  3832. *next++ = 0;
  3833. /* parse first part */
  3834. if (*p) {
  3835. interval->has_start = 1;
  3836. if (*p == '+') {
  3837. interval->start_is_offset = 1;
  3838. p++;
  3839. } else {
  3840. interval->start_is_offset = 0;
  3841. }
  3842. ret = av_parse_time(&interval->start, p, 1);
  3843. if (ret < 0) {
  3844. av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
  3845. goto end;
  3846. }
  3847. } else {
  3848. interval->has_start = 0;
  3849. }
  3850. /* parse second part */
  3851. p = next;
  3852. if (p && *p) {
  3853. int64_t us;
  3854. interval->has_end = 1;
  3855. if (*p == '+') {
  3856. interval->end_is_offset = 1;
  3857. p++;
  3858. } else {
  3859. interval->end_is_offset = 0;
  3860. }
  3861. if (interval->end_is_offset && *p == '#') {
  3862. long long int lli;
  3863. char *tail;
  3864. interval->duration_frames = 1;
  3865. p++;
  3866. lli = strtoll(p, &tail, 10);
  3867. if (*tail || lli < 0) {
  3868. av_log(NULL, AV_LOG_ERROR,
  3869. "Invalid or negative value '%s' for duration number of frames\n", p);
  3870. goto end;
  3871. }
  3872. interval->end = lli;
  3873. } else {
  3874. interval->duration_frames = 0;
  3875. ret = av_parse_time(&us, p, 1);
  3876. if (ret < 0) {
  3877. av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
  3878. goto end;
  3879. }
  3880. interval->end = us;
  3881. }
  3882. } else {
  3883. interval->has_end = 0;
  3884. }
  3885. end:
  3886. av_free(spec);
  3887. return ret;
  3888. }
  3889. static int parse_read_intervals(const char *intervals_spec)
  3890. {
  3891. int ret, n, i;
  3892. char *p, *spec = av_strdup(intervals_spec);
  3893. if (!spec)
  3894. return AVERROR(ENOMEM);
  3895. /* preparse specification, get number of intervals */
  3896. for (n = 0, p = spec; *p; p++)
  3897. if (*p == ',')
  3898. n++;
  3899. n++;
  3900. read_intervals = av_malloc_array(n, sizeof(*read_intervals));
  3901. if (!read_intervals) {
  3902. ret = AVERROR(ENOMEM);
  3903. goto end;
  3904. }
  3905. read_intervals_nb = n;
  3906. /* parse intervals */
  3907. p = spec;
  3908. for (i = 0; p; i++) {
  3909. char *next;
  3910. av_assert0(i < read_intervals_nb);
  3911. next = strchr(p, ',');
  3912. if (next)
  3913. *next++ = 0;
  3914. read_intervals[i].id = i;
  3915. ret = parse_read_interval(p, &read_intervals[i]);
  3916. if (ret < 0) {
  3917. av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
  3918. i, p);
  3919. goto end;
  3920. }
  3921. av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
  3922. log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
  3923. p = next;
  3924. }
  3925. av_assert0(i == read_intervals_nb);
  3926. end:
  3927. av_free(spec);
  3928. return ret;
  3929. }
  3930. static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
  3931. {
  3932. return parse_read_intervals(arg);
  3933. }
  3934. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  3935. {
  3936. show_value_unit = 1;
  3937. use_value_prefix = 1;
  3938. use_byte_value_binary_prefix = 1;
  3939. use_value_sexagesimal_format = 1;
  3940. return 0;
  3941. }
  3942. static void print_section(SectionID id, int level)
  3943. {
  3944. const SectionID *pid;
  3945. const struct section *section = &sections[id];
  3946. printf("%c%c%c%c",
  3947. section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
  3948. section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
  3949. section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.',
  3950. section->flags & SECTION_FLAG_HAS_TYPE ? 'T' : '.');
  3951. printf("%*c %s", level * 4, ' ', section->name);
  3952. if (section->unique_name)
  3953. printf("/%s", section->unique_name);
  3954. printf("\n");
  3955. for (pid = section->children_ids; *pid != -1; pid++)
  3956. print_section(*pid, level+1);
  3957. }
  3958. static int opt_sections(void *optctx, const char *opt, const char *arg)
  3959. {
  3960. printf("Sections:\n"
  3961. "W... = Section is a wrapper (contains other sections, no local entries)\n"
  3962. ".A.. = Section contains an array of elements of the same type\n"
  3963. "..V. = Section may contain a variable number of fields with variable keys\n"
  3964. "...T = Section contain a unique type\n"
  3965. "FLAGS NAME/UNIQUE_NAME\n"
  3966. "----\n");
  3967. print_section(SECTION_ID_ROOT, 0);
  3968. return 0;
  3969. }
  3970. static int opt_show_versions(void *optctx, const char *opt, const char *arg)
  3971. {
  3972. mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
  3973. mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
  3974. return 0;
  3975. }
  3976. #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
  3977. static int opt_show_##section(void *optctx, const char *opt, const char *arg) \
  3978. { \
  3979. mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
  3980. return 0; \
  3981. }
  3982. DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
  3983. DEFINE_OPT_SHOW_SECTION(error, ERROR)
  3984. DEFINE_OPT_SHOW_SECTION(format, FORMAT)
  3985. DEFINE_OPT_SHOW_SECTION(frames, FRAMES)
  3986. DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
  3987. DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
  3988. DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
  3989. DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
  3990. DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
  3991. DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
  3992. DEFINE_OPT_SHOW_SECTION(stream_groups, STREAM_GROUPS)
  3993. static const OptionDef real_options[] = {
  3994. CMDUTILS_COMMON_OPTIONS
  3995. { "f", OPT_TYPE_FUNC, OPT_FUNC_ARG, {.func_arg = opt_format}, "force format", "format" },
  3996. { "unit", OPT_TYPE_BOOL, 0, {&show_value_unit}, "show unit of the displayed values" },
  3997. { "prefix", OPT_TYPE_BOOL, 0, {&use_value_prefix}, "use SI prefixes for the displayed values" },
  3998. { "byte_binary_prefix", OPT_TYPE_BOOL, 0, {&use_byte_value_binary_prefix},
  3999. "use binary prefixes for byte units" },
  4000. { "sexagesimal", OPT_TYPE_BOOL, 0, {&use_value_sexagesimal_format},
  4001. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  4002. { "pretty", OPT_TYPE_FUNC, 0, {.func_arg = opt_pretty},
  4003. "prettify the format of displayed values, make it more human readable" },
  4004. { "output_format", OPT_TYPE_STRING, 0, { &output_format },
  4005. "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
  4006. { "print_format", OPT_TYPE_STRING, 0, { &output_format }, "alias for -output_format (deprecated)" },
  4007. { "of", OPT_TYPE_STRING, 0, { &output_format }, "alias for -output_format", "format" },
  4008. { "select_streams", OPT_TYPE_STRING, 0, { &stream_specifier }, "select the specified streams", "stream_specifier" },
  4009. { "sections", OPT_TYPE_FUNC, OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
  4010. { "show_data", OPT_TYPE_BOOL, 0, { &do_show_data }, "show packets data" },
  4011. { "show_data_hash", OPT_TYPE_STRING, 0, { &show_data_hash }, "show packets data hash" },
  4012. { "show_error", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_error }, "show probing error" },
  4013. { "show_format", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_format }, "show format/container info" },
  4014. { "show_frames", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_frames }, "show frames info" },
  4015. { "show_entries", OPT_TYPE_FUNC, OPT_FUNC_ARG, {.func_arg = opt_show_entries},
  4016. "show a set of specified entries", "entry_list" },
  4017. #if HAVE_THREADS
  4018. { "show_log", OPT_TYPE_INT, 0, { &do_show_log }, "show log" },
  4019. #endif
  4020. { "show_packets", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_packets }, "show packets info" },
  4021. { "show_programs", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_programs }, "show programs info" },
  4022. { "show_stream_groups", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_stream_groups }, "show stream groups info" },
  4023. { "show_streams", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_streams }, "show streams info" },
  4024. { "show_chapters", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_chapters }, "show chapters info" },
  4025. { "count_frames", OPT_TYPE_BOOL, 0, { &do_count_frames }, "count the number of frames per stream" },
  4026. { "count_packets", OPT_TYPE_BOOL, 0, { &do_count_packets }, "count the number of packets per stream" },
  4027. { "show_program_version", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_program_version }, "show ffprobe version" },
  4028. { "show_library_versions", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_library_versions }, "show library versions" },
  4029. { "show_versions", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_versions }, "show program and library versions" },
  4030. { "show_pixel_formats", OPT_TYPE_FUNC, 0, { .func_arg = &opt_show_pixel_formats }, "show pixel format descriptions" },
  4031. { "show_optional_fields", OPT_TYPE_FUNC, OPT_FUNC_ARG, { .func_arg = &opt_show_optional_fields }, "show optional fields" },
  4032. { "show_private_data", OPT_TYPE_BOOL, 0, { &show_private_data }, "show private data" },
  4033. { "private", OPT_TYPE_BOOL, 0, { &show_private_data }, "same as show_private_data" },
  4034. { "analyze_frames", OPT_TYPE_BOOL, 0, { &do_analyze_frames }, "analyze frames to provide additional stream-level information" },
  4035. { "bitexact", OPT_TYPE_BOOL, 0, {&do_bitexact}, "force bitexact output" },
  4036. { "read_intervals", OPT_TYPE_FUNC, OPT_FUNC_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
  4037. { "i", OPT_TYPE_FUNC, OPT_FUNC_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
  4038. { "o", OPT_TYPE_FUNC, OPT_FUNC_ARG, {.func_arg = opt_output_file_o}, "write to specified output", "output_file"},
  4039. { "print_filename", OPT_TYPE_FUNC, OPT_FUNC_ARG, {.func_arg = opt_print_filename}, "override the printed input filename", "print_file"},
  4040. { "find_stream_info", OPT_TYPE_BOOL, OPT_INPUT | OPT_EXPERT, { &find_stream_info },
  4041. "read and decode the streams to fill missing information with heuristics" },
  4042. { NULL, },
  4043. };
  4044. static inline int check_section_show_entries(int section_id)
  4045. {
  4046. struct section *section = &sections[section_id];
  4047. if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
  4048. return 1;
  4049. for (const SectionID *id = section->children_ids; *id != -1; id++)
  4050. if (check_section_show_entries(*id))
  4051. return 1;
  4052. return 0;
  4053. }
  4054. #define SET_DO_SHOW(id, varname) do { \
  4055. if (check_section_show_entries(SECTION_ID_##id)) \
  4056. do_show_##varname = 1; \
  4057. } while (0)
  4058. int main(int argc, char **argv)
  4059. {
  4060. const Writer *w;
  4061. WriterContext *wctx;
  4062. char *buf;
  4063. char *w_name = NULL, *w_args = NULL;
  4064. int ret, input_ret, i;
  4065. init_dynload();
  4066. #if HAVE_THREADS
  4067. ret = pthread_mutex_init(&log_mutex, NULL);
  4068. if (ret != 0) {
  4069. goto end;
  4070. }
  4071. #endif
  4072. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  4073. options = real_options;
  4074. parse_loglevel(argc, argv, options);
  4075. avformat_network_init();
  4076. #if CONFIG_AVDEVICE
  4077. avdevice_register_all();
  4078. #endif
  4079. show_banner(argc, argv, options);
  4080. ret = parse_options(NULL, argc, argv, options, opt_input_file);
  4081. if (ret < 0) {
  4082. ret = (ret == AVERROR_EXIT) ? 0 : ret;
  4083. goto end;
  4084. }
  4085. if (do_show_log)
  4086. av_log_set_callback(log_callback);
  4087. /* mark things to show, based on -show_entries */
  4088. SET_DO_SHOW(CHAPTERS, chapters);
  4089. SET_DO_SHOW(ERROR, error);
  4090. SET_DO_SHOW(FORMAT, format);
  4091. SET_DO_SHOW(FRAMES, frames);
  4092. SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
  4093. SET_DO_SHOW(PACKETS, packets);
  4094. SET_DO_SHOW(PIXEL_FORMATS, pixel_formats);
  4095. SET_DO_SHOW(PIXEL_FORMAT_FLAGS, pixel_format_flags);
  4096. SET_DO_SHOW(PIXEL_FORMAT_COMPONENTS, pixel_format_components);
  4097. SET_DO_SHOW(PROGRAM_VERSION, program_version);
  4098. SET_DO_SHOW(PROGRAMS, programs);
  4099. SET_DO_SHOW(STREAM_GROUP_DISPOSITION, stream_group_disposition);
  4100. SET_DO_SHOW(STREAM_GROUPS, stream_groups);
  4101. SET_DO_SHOW(STREAM_GROUP_COMPONENTS, stream_group_components);
  4102. SET_DO_SHOW(STREAMS, streams);
  4103. SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
  4104. SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
  4105. SET_DO_SHOW(STREAM_GROUP_STREAM_DISPOSITION, stream_disposition);
  4106. SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
  4107. SET_DO_SHOW(FORMAT_TAGS, format_tags);
  4108. SET_DO_SHOW(FRAME_TAGS, frame_tags);
  4109. SET_DO_SHOW(PROGRAM_TAGS, program_tags);
  4110. SET_DO_SHOW(STREAM_GROUP_TAGS, stream_group_tags);
  4111. SET_DO_SHOW(STREAM_TAGS, stream_tags);
  4112. SET_DO_SHOW(PROGRAM_STREAM_TAGS, stream_tags);
  4113. SET_DO_SHOW(STREAM_GROUP_STREAM_TAGS, stream_tags);
  4114. SET_DO_SHOW(PACKET_TAGS, packet_tags);
  4115. if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
  4116. av_log(NULL, AV_LOG_ERROR,
  4117. "-bitexact and -show_program_version or -show_library_versions "
  4118. "options are incompatible\n");
  4119. ret = AVERROR(EINVAL);
  4120. goto end;
  4121. }
  4122. writer_register_all();
  4123. if (!output_format)
  4124. output_format = av_strdup("default");
  4125. if (!output_format) {
  4126. ret = AVERROR(ENOMEM);
  4127. goto end;
  4128. }
  4129. w_name = av_strtok(output_format, "=", &buf);
  4130. if (!w_name) {
  4131. av_log(NULL, AV_LOG_ERROR,
  4132. "No name specified for the output format\n");
  4133. ret = AVERROR(EINVAL);
  4134. goto end;
  4135. }
  4136. w_args = buf;
  4137. if (show_data_hash) {
  4138. if ((ret = av_hash_alloc(&hash, show_data_hash)) < 0) {
  4139. if (ret == AVERROR(EINVAL)) {
  4140. const char *n;
  4141. av_log(NULL, AV_LOG_ERROR,
  4142. "Unknown hash algorithm '%s'\nKnown algorithms:",
  4143. show_data_hash);
  4144. for (i = 0; (n = av_hash_names(i)); i++)
  4145. av_log(NULL, AV_LOG_ERROR, " %s", n);
  4146. av_log(NULL, AV_LOG_ERROR, "\n");
  4147. }
  4148. goto end;
  4149. }
  4150. }
  4151. w = writer_get_by_name(w_name);
  4152. if (!w) {
  4153. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  4154. ret = AVERROR(EINVAL);
  4155. goto end;
  4156. }
  4157. if ((ret = writer_open(&wctx, w, w_args,
  4158. sections, FF_ARRAY_ELEMS(sections), output_filename)) >= 0) {
  4159. if (w == &xml_writer)
  4160. wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
  4161. writer_print_section_header(wctx, NULL, SECTION_ID_ROOT);
  4162. if (do_show_program_version)
  4163. ffprobe_show_program_version(wctx);
  4164. if (do_show_library_versions)
  4165. ffprobe_show_library_versions(wctx);
  4166. if (do_show_pixel_formats)
  4167. ffprobe_show_pixel_formats(wctx);
  4168. if (!input_filename &&
  4169. ((do_show_format || do_show_programs || do_show_stream_groups || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
  4170. (!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
  4171. show_usage();
  4172. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  4173. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  4174. ret = AVERROR(EINVAL);
  4175. } else if (input_filename) {
  4176. ret = probe_file(wctx, input_filename, print_input_filename);
  4177. if (ret < 0 && do_show_error)
  4178. show_error(wctx, ret);
  4179. }
  4180. input_ret = ret;
  4181. writer_print_section_footer(wctx);
  4182. ret = writer_close(&wctx);
  4183. if (ret < 0)
  4184. av_log(NULL, AV_LOG_ERROR, "Writing output failed: %s\n", av_err2str(ret));
  4185. ret = FFMIN(ret, input_ret);
  4186. }
  4187. end:
  4188. av_freep(&output_format);
  4189. av_freep(&output_filename);
  4190. av_freep(&input_filename);
  4191. av_freep(&print_input_filename);
  4192. av_freep(&read_intervals);
  4193. av_hash_freep(&hash);
  4194. uninit_opts();
  4195. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
  4196. av_dict_free(&(sections[i].entries_to_show));
  4197. avformat_network_deinit();
  4198. #if HAVE_THREADS
  4199. pthread_mutex_destroy(&log_mutex);
  4200. #endif
  4201. return ret < 0;
  4202. }