ffprobe.c 128 KB

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