ffprobe.c 121 KB

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