ffprobe.c 189 KB

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