ffprobe.c 189 KB

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