ffprobe.c 191 KB

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