ffprobe.c 189 KB

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