ffprobe.c 190 KB

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