ffprobe.c 158 KB

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