ffprobe.c 121 KB

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