ffprobe.c 157 KB

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