ffprobe.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420
  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 print_color_range(WriterContext *w, enum AVColorRange color_range, const char *fallback)
  1521. {
  1522. const char *val = av_color_range_name(color_range);
  1523. if (!val || color_range == AVCOL_RANGE_UNSPECIFIED) {
  1524. print_str_opt("color_range", fallback);
  1525. } else {
  1526. print_str("color_range", val);
  1527. }
  1528. }
  1529. static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
  1530. {
  1531. const char *val = av_color_space_name(color_space);
  1532. if (!val || color_space == AVCOL_SPC_UNSPECIFIED) {
  1533. print_str_opt("color_space", "unknown");
  1534. } else {
  1535. print_str("color_space", val);
  1536. }
  1537. }
  1538. static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
  1539. {
  1540. const char *val = av_color_primaries_name(color_primaries);
  1541. if (!val || color_primaries == AVCOL_PRI_UNSPECIFIED) {
  1542. print_str_opt("color_primaries", "unknown");
  1543. } else {
  1544. print_str("color_primaries", val);
  1545. }
  1546. }
  1547. static void print_color_trc(WriterContext *w, enum AVColorTransferCharacteristic color_trc)
  1548. {
  1549. const char *val = av_color_transfer_name(color_trc);
  1550. if (!val || color_trc == AVCOL_TRC_UNSPECIFIED) {
  1551. print_str_opt("color_transfer", "unknown");
  1552. } else {
  1553. print_str("color_transfer", val);
  1554. }
  1555. }
  1556. static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
  1557. {
  1558. const char *val = av_chroma_location_name(chroma_location);
  1559. if (!val || chroma_location == AVCHROMA_LOC_UNSPECIFIED) {
  1560. print_str_opt("chroma_location", "unspecified");
  1561. } else {
  1562. print_str("chroma_location", val);
  1563. }
  1564. }
  1565. static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
  1566. {
  1567. char val_str[128];
  1568. AVStream *st = ifile->streams[pkt->stream_index].st;
  1569. AVBPrint pbuf;
  1570. const char *s;
  1571. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1572. writer_print_section_header(w, SECTION_ID_PACKET);
  1573. s = av_get_media_type_string(st->codecpar->codec_type);
  1574. if (s) print_str ("codec_type", s);
  1575. else print_str_opt("codec_type", "unknown");
  1576. print_int("stream_index", pkt->stream_index);
  1577. print_ts ("pts", pkt->pts);
  1578. print_time("pts_time", pkt->pts, &st->time_base);
  1579. print_ts ("dts", pkt->dts);
  1580. print_time("dts_time", pkt->dts, &st->time_base);
  1581. print_duration_ts("duration", pkt->duration);
  1582. print_duration_time("duration_time", pkt->duration, &st->time_base);
  1583. print_duration_ts("convergence_duration", pkt->convergence_duration);
  1584. print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
  1585. print_val("size", pkt->size, unit_byte_str);
  1586. if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
  1587. else print_str_opt("pos", "N/A");
  1588. print_fmt("flags", "%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
  1589. pkt->flags & AV_PKT_FLAG_DISCARD ? 'D' : '_');
  1590. if (pkt->side_data_elems) {
  1591. int size;
  1592. const uint8_t *side_metadata;
  1593. side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
  1594. if (side_metadata && size && do_show_packet_tags) {
  1595. AVDictionary *dict = NULL;
  1596. if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
  1597. show_tags(w, dict, SECTION_ID_PACKET_TAGS);
  1598. av_dict_free(&dict);
  1599. }
  1600. print_pkt_side_data(w, pkt->side_data, pkt->side_data_elems,
  1601. SECTION_ID_PACKET_SIDE_DATA_LIST,
  1602. SECTION_ID_PACKET_SIDE_DATA);
  1603. }
  1604. if (do_show_data)
  1605. writer_print_data(w, "data", pkt->data, pkt->size);
  1606. writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
  1607. writer_print_section_footer(w);
  1608. av_bprint_finalize(&pbuf, NULL);
  1609. fflush(stdout);
  1610. }
  1611. static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
  1612. AVFormatContext *fmt_ctx)
  1613. {
  1614. AVBPrint pbuf;
  1615. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1616. writer_print_section_header(w, SECTION_ID_SUBTITLE);
  1617. print_str ("media_type", "subtitle");
  1618. print_ts ("pts", sub->pts);
  1619. print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
  1620. print_int ("format", sub->format);
  1621. print_int ("start_display_time", sub->start_display_time);
  1622. print_int ("end_display_time", sub->end_display_time);
  1623. print_int ("num_rects", sub->num_rects);
  1624. writer_print_section_footer(w);
  1625. av_bprint_finalize(&pbuf, NULL);
  1626. fflush(stdout);
  1627. }
  1628. static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
  1629. AVFormatContext *fmt_ctx)
  1630. {
  1631. AVBPrint pbuf;
  1632. char val_str[128];
  1633. const char *s;
  1634. int i;
  1635. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1636. writer_print_section_header(w, SECTION_ID_FRAME);
  1637. s = av_get_media_type_string(stream->codecpar->codec_type);
  1638. if (s) print_str ("media_type", s);
  1639. else print_str_opt("media_type", "unknown");
  1640. print_int("stream_index", stream->index);
  1641. print_int("key_frame", frame->key_frame);
  1642. print_ts ("pkt_pts", frame->pts);
  1643. print_time("pkt_pts_time", frame->pts, &stream->time_base);
  1644. print_ts ("pkt_dts", frame->pkt_dts);
  1645. print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
  1646. print_ts ("best_effort_timestamp", av_frame_get_best_effort_timestamp(frame));
  1647. print_time("best_effort_timestamp_time", av_frame_get_best_effort_timestamp(frame), &stream->time_base);
  1648. print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
  1649. print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
  1650. if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
  1651. else print_str_opt("pkt_pos", "N/A");
  1652. if (av_frame_get_pkt_size(frame) != -1) print_val ("pkt_size", av_frame_get_pkt_size(frame), unit_byte_str);
  1653. else print_str_opt("pkt_size", "N/A");
  1654. switch (stream->codecpar->codec_type) {
  1655. AVRational sar;
  1656. case AVMEDIA_TYPE_VIDEO:
  1657. print_int("width", frame->width);
  1658. print_int("height", frame->height);
  1659. s = av_get_pix_fmt_name(frame->format);
  1660. if (s) print_str ("pix_fmt", s);
  1661. else print_str_opt("pix_fmt", "unknown");
  1662. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
  1663. if (sar.num) {
  1664. print_q("sample_aspect_ratio", sar, ':');
  1665. } else {
  1666. print_str_opt("sample_aspect_ratio", "N/A");
  1667. }
  1668. print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
  1669. print_int("coded_picture_number", frame->coded_picture_number);
  1670. print_int("display_picture_number", frame->display_picture_number);
  1671. print_int("interlaced_frame", frame->interlaced_frame);
  1672. print_int("top_field_first", frame->top_field_first);
  1673. print_int("repeat_pict", frame->repeat_pict);
  1674. break;
  1675. case AVMEDIA_TYPE_AUDIO:
  1676. s = av_get_sample_fmt_name(frame->format);
  1677. if (s) print_str ("sample_fmt", s);
  1678. else print_str_opt("sample_fmt", "unknown");
  1679. print_int("nb_samples", frame->nb_samples);
  1680. print_int("channels", av_frame_get_channels(frame));
  1681. if (av_frame_get_channel_layout(frame)) {
  1682. av_bprint_clear(&pbuf);
  1683. av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
  1684. av_frame_get_channel_layout(frame));
  1685. print_str ("channel_layout", pbuf.str);
  1686. } else
  1687. print_str_opt("channel_layout", "unknown");
  1688. break;
  1689. }
  1690. if (do_show_frame_tags)
  1691. show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
  1692. if (frame->nb_side_data) {
  1693. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_LIST);
  1694. for (i = 0; i < frame->nb_side_data; i++) {
  1695. AVFrameSideData *sd = frame->side_data[i];
  1696. const char *name;
  1697. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA);
  1698. name = av_frame_side_data_name(sd->type);
  1699. print_str("side_data_type", name ? name : "unknown");
  1700. print_int("side_data_size", sd->size);
  1701. if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
  1702. writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
  1703. print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
  1704. } else if (sd->type == AV_FRAME_DATA_GOP_TIMECODE && sd->size >= 8) {
  1705. char tcbuf[AV_TIMECODE_STR_SIZE];
  1706. av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
  1707. print_str("timecode", tcbuf);
  1708. }
  1709. writer_print_section_footer(w);
  1710. }
  1711. writer_print_section_footer(w);
  1712. }
  1713. writer_print_section_footer(w);
  1714. av_bprint_finalize(&pbuf, NULL);
  1715. fflush(stdout);
  1716. }
  1717. static av_always_inline int process_frame(WriterContext *w,
  1718. InputFile *ifile,
  1719. AVFrame *frame, AVPacket *pkt)
  1720. {
  1721. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  1722. AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
  1723. AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
  1724. AVSubtitle sub;
  1725. int ret = 0, got_frame = 0;
  1726. if (dec_ctx && dec_ctx->codec) {
  1727. switch (par->codec_type) {
  1728. case AVMEDIA_TYPE_VIDEO:
  1729. ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
  1730. break;
  1731. case AVMEDIA_TYPE_AUDIO:
  1732. ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
  1733. break;
  1734. case AVMEDIA_TYPE_SUBTITLE:
  1735. ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
  1736. break;
  1737. }
  1738. }
  1739. if (ret < 0)
  1740. return ret;
  1741. ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
  1742. pkt->data += ret;
  1743. pkt->size -= ret;
  1744. if (got_frame) {
  1745. int is_sub = (par->codec_type == AVMEDIA_TYPE_SUBTITLE);
  1746. nb_streams_frames[pkt->stream_index]++;
  1747. if (do_show_frames)
  1748. if (is_sub)
  1749. show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
  1750. else
  1751. show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
  1752. if (is_sub)
  1753. avsubtitle_free(&sub);
  1754. }
  1755. return got_frame;
  1756. }
  1757. static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
  1758. {
  1759. av_log(log_ctx, log_level, "id:%d", interval->id);
  1760. if (interval->has_start) {
  1761. av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
  1762. av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
  1763. } else {
  1764. av_log(log_ctx, log_level, " start:N/A");
  1765. }
  1766. if (interval->has_end) {
  1767. av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
  1768. if (interval->duration_frames)
  1769. av_log(log_ctx, log_level, "#%"PRId64, interval->end);
  1770. else
  1771. av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
  1772. } else {
  1773. av_log(log_ctx, log_level, " end:N/A");
  1774. }
  1775. av_log(log_ctx, log_level, "\n");
  1776. }
  1777. static int read_interval_packets(WriterContext *w, InputFile *ifile,
  1778. const ReadInterval *interval, int64_t *cur_ts)
  1779. {
  1780. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  1781. AVPacket pkt, pkt1;
  1782. AVFrame *frame = NULL;
  1783. int ret = 0, i = 0, frame_count = 0;
  1784. int64_t start = -INT64_MAX, end = interval->end;
  1785. int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
  1786. av_init_packet(&pkt);
  1787. av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
  1788. log_read_interval(interval, NULL, AV_LOG_VERBOSE);
  1789. if (interval->has_start) {
  1790. int64_t target;
  1791. if (interval->start_is_offset) {
  1792. if (*cur_ts == AV_NOPTS_VALUE) {
  1793. av_log(NULL, AV_LOG_ERROR,
  1794. "Could not seek to relative position since current "
  1795. "timestamp is not defined\n");
  1796. ret = AVERROR(EINVAL);
  1797. goto end;
  1798. }
  1799. target = *cur_ts + interval->start;
  1800. } else {
  1801. target = interval->start;
  1802. }
  1803. av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
  1804. av_ts2timestr(target, &AV_TIME_BASE_Q));
  1805. if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
  1806. av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
  1807. interval->start, av_err2str(ret));
  1808. goto end;
  1809. }
  1810. }
  1811. frame = av_frame_alloc();
  1812. if (!frame) {
  1813. ret = AVERROR(ENOMEM);
  1814. goto end;
  1815. }
  1816. while (!av_read_frame(fmt_ctx, &pkt)) {
  1817. if (ifile->nb_streams > nb_streams) {
  1818. REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
  1819. REALLOCZ_ARRAY_STREAM(nb_streams_packets, nb_streams, fmt_ctx->nb_streams);
  1820. REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
  1821. nb_streams = ifile->nb_streams;
  1822. }
  1823. if (selected_streams[pkt.stream_index]) {
  1824. AVRational tb = ifile->streams[pkt.stream_index].st->time_base;
  1825. if (pkt.pts != AV_NOPTS_VALUE)
  1826. *cur_ts = av_rescale_q(pkt.pts, tb, AV_TIME_BASE_Q);
  1827. if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
  1828. start = *cur_ts;
  1829. has_start = 1;
  1830. }
  1831. if (has_start && !has_end && interval->end_is_offset) {
  1832. end = start + interval->end;
  1833. has_end = 1;
  1834. }
  1835. if (interval->end_is_offset && interval->duration_frames) {
  1836. if (frame_count >= interval->end)
  1837. break;
  1838. } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
  1839. break;
  1840. }
  1841. frame_count++;
  1842. if (do_read_packets) {
  1843. if (do_show_packets)
  1844. show_packet(w, ifile, &pkt, i++);
  1845. nb_streams_packets[pkt.stream_index]++;
  1846. }
  1847. if (do_read_frames) {
  1848. pkt1 = pkt;
  1849. while (pkt1.size && process_frame(w, ifile, frame, &pkt1) > 0);
  1850. }
  1851. }
  1852. av_packet_unref(&pkt);
  1853. }
  1854. av_init_packet(&pkt);
  1855. pkt.data = NULL;
  1856. pkt.size = 0;
  1857. //Flush remaining frames that are cached in the decoder
  1858. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1859. pkt.stream_index = i;
  1860. if (do_read_frames)
  1861. while (process_frame(w, ifile, frame, &pkt) > 0);
  1862. }
  1863. end:
  1864. av_frame_free(&frame);
  1865. if (ret < 0) {
  1866. av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
  1867. log_read_interval(interval, NULL, AV_LOG_ERROR);
  1868. }
  1869. return ret;
  1870. }
  1871. static int read_packets(WriterContext *w, InputFile *ifile)
  1872. {
  1873. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  1874. int i, ret = 0;
  1875. int64_t cur_ts = fmt_ctx->start_time;
  1876. if (read_intervals_nb == 0) {
  1877. ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
  1878. ret = read_interval_packets(w, ifile, &interval, &cur_ts);
  1879. } else {
  1880. for (i = 0; i < read_intervals_nb; i++) {
  1881. ret = read_interval_packets(w, ifile, &read_intervals[i], &cur_ts);
  1882. if (ret < 0)
  1883. break;
  1884. }
  1885. }
  1886. return ret;
  1887. }
  1888. static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int in_program)
  1889. {
  1890. AVStream *stream = ist->st;
  1891. AVCodecParameters *par;
  1892. AVCodecContext *dec_ctx;
  1893. char val_str[128];
  1894. const char *s;
  1895. AVRational sar, dar;
  1896. AVBPrint pbuf;
  1897. const AVCodecDescriptor *cd;
  1898. int ret = 0;
  1899. const char *profile = NULL;
  1900. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1901. writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
  1902. print_int("index", stream->index);
  1903. par = stream->codecpar;
  1904. dec_ctx = ist->dec_ctx;
  1905. if (cd = avcodec_descriptor_get(par->codec_id)) {
  1906. print_str("codec_name", cd->name);
  1907. if (!do_bitexact) {
  1908. print_str("codec_long_name",
  1909. cd->long_name ? cd->long_name : "unknown");
  1910. }
  1911. } else {
  1912. print_str_opt("codec_name", "unknown");
  1913. if (!do_bitexact) {
  1914. print_str_opt("codec_long_name", "unknown");
  1915. }
  1916. }
  1917. if (!do_bitexact && (profile = avcodec_profile_name(par->codec_id, par->profile)))
  1918. print_str("profile", profile);
  1919. else {
  1920. if (par->profile != FF_PROFILE_UNKNOWN) {
  1921. char profile_num[12];
  1922. snprintf(profile_num, sizeof(profile_num), "%d", par->profile);
  1923. print_str("profile", profile_num);
  1924. } else
  1925. print_str_opt("profile", "unknown");
  1926. }
  1927. s = av_get_media_type_string(par->codec_type);
  1928. if (s) print_str ("codec_type", s);
  1929. else print_str_opt("codec_type", "unknown");
  1930. #if FF_API_LAVF_AVCTX
  1931. if (dec_ctx)
  1932. print_q("codec_time_base", dec_ctx->time_base, '/');
  1933. #endif
  1934. /* print AVI/FourCC tag */
  1935. av_get_codec_tag_string(val_str, sizeof(val_str), par->codec_tag);
  1936. print_str("codec_tag_string", val_str);
  1937. print_fmt("codec_tag", "0x%04x", par->codec_tag);
  1938. switch (par->codec_type) {
  1939. case AVMEDIA_TYPE_VIDEO:
  1940. print_int("width", par->width);
  1941. print_int("height", par->height);
  1942. if (dec_ctx) {
  1943. print_int("coded_width", dec_ctx->coded_width);
  1944. print_int("coded_height", dec_ctx->coded_height);
  1945. }
  1946. print_int("has_b_frames", par->video_delay);
  1947. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
  1948. if (sar.den) {
  1949. print_q("sample_aspect_ratio", sar, ':');
  1950. av_reduce(&dar.num, &dar.den,
  1951. par->width * sar.num,
  1952. par->height * sar.den,
  1953. 1024*1024);
  1954. print_q("display_aspect_ratio", dar, ':');
  1955. } else {
  1956. print_str_opt("sample_aspect_ratio", "N/A");
  1957. print_str_opt("display_aspect_ratio", "N/A");
  1958. }
  1959. s = av_get_pix_fmt_name(par->format);
  1960. if (s) print_str ("pix_fmt", s);
  1961. else print_str_opt("pix_fmt", "unknown");
  1962. print_int("level", par->level);
  1963. print_color_range(w, par->color_range, "N/A");
  1964. print_color_space(w, par->color_space);
  1965. print_color_trc(w, par->color_trc);
  1966. print_primaries(w, par->color_primaries);
  1967. print_chroma_location(w, par->chroma_location);
  1968. if (par->field_order == AV_FIELD_PROGRESSIVE)
  1969. print_str("field_order", "progressive");
  1970. else if (par->field_order == AV_FIELD_TT)
  1971. print_str("field_order", "tt");
  1972. else if (par->field_order == AV_FIELD_BB)
  1973. print_str("field_order", "bb");
  1974. else if (par->field_order == AV_FIELD_TB)
  1975. print_str("field_order", "tb");
  1976. else if (par->field_order == AV_FIELD_BT)
  1977. print_str("field_order", "bt");
  1978. else
  1979. print_str_opt("field_order", "unknown");
  1980. #if FF_API_PRIVATE_OPT
  1981. if (dec_ctx && dec_ctx->timecode_frame_start >= 0) {
  1982. char tcbuf[AV_TIMECODE_STR_SIZE];
  1983. av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
  1984. print_str("timecode", tcbuf);
  1985. } else {
  1986. print_str_opt("timecode", "N/A");
  1987. }
  1988. #endif
  1989. if (dec_ctx)
  1990. print_int("refs", dec_ctx->refs);
  1991. break;
  1992. case AVMEDIA_TYPE_AUDIO:
  1993. s = av_get_sample_fmt_name(par->format);
  1994. if (s) print_str ("sample_fmt", s);
  1995. else print_str_opt("sample_fmt", "unknown");
  1996. print_val("sample_rate", par->sample_rate, unit_hertz_str);
  1997. print_int("channels", par->channels);
  1998. if (par->channel_layout) {
  1999. av_bprint_clear(&pbuf);
  2000. av_bprint_channel_layout(&pbuf, par->channels, par->channel_layout);
  2001. print_str ("channel_layout", pbuf.str);
  2002. } else {
  2003. print_str_opt("channel_layout", "unknown");
  2004. }
  2005. print_int("bits_per_sample", av_get_bits_per_sample(par->codec_id));
  2006. break;
  2007. case AVMEDIA_TYPE_SUBTITLE:
  2008. if (par->width)
  2009. print_int("width", par->width);
  2010. else
  2011. print_str_opt("width", "N/A");
  2012. if (par->height)
  2013. print_int("height", par->height);
  2014. else
  2015. print_str_opt("height", "N/A");
  2016. break;
  2017. }
  2018. if (dec_ctx && dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
  2019. const AVOption *opt = NULL;
  2020. while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
  2021. uint8_t *str;
  2022. if (opt->flags) continue;
  2023. if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
  2024. print_str(opt->name, str);
  2025. av_free(str);
  2026. }
  2027. }
  2028. }
  2029. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  2030. else print_str_opt("id", "N/A");
  2031. print_q("r_frame_rate", stream->r_frame_rate, '/');
  2032. print_q("avg_frame_rate", stream->avg_frame_rate, '/');
  2033. print_q("time_base", stream->time_base, '/');
  2034. print_ts ("start_pts", stream->start_time);
  2035. print_time("start_time", stream->start_time, &stream->time_base);
  2036. print_ts ("duration_ts", stream->duration);
  2037. print_time("duration", stream->duration, &stream->time_base);
  2038. if (par->bit_rate > 0) print_val ("bit_rate", par->bit_rate, unit_bit_per_second_str);
  2039. else print_str_opt("bit_rate", "N/A");
  2040. #if FF_API_LAVF_AVCTX
  2041. if (stream->codec->rc_max_rate > 0) print_val ("max_bit_rate", stream->codec->rc_max_rate, unit_bit_per_second_str);
  2042. else print_str_opt("max_bit_rate", "N/A");
  2043. #endif
  2044. if (dec_ctx && dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
  2045. else print_str_opt("bits_per_raw_sample", "N/A");
  2046. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  2047. else print_str_opt("nb_frames", "N/A");
  2048. if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
  2049. else print_str_opt("nb_read_frames", "N/A");
  2050. if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
  2051. else print_str_opt("nb_read_packets", "N/A");
  2052. if (do_show_data)
  2053. writer_print_data(w, "extradata", par->extradata,
  2054. par->extradata_size);
  2055. writer_print_data_hash(w, "extradata_hash", par->extradata,
  2056. par->extradata_size);
  2057. /* Print disposition information */
  2058. #define PRINT_DISPOSITION(flagname, name) do { \
  2059. print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
  2060. } while (0)
  2061. if (do_show_stream_disposition) {
  2062. writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
  2063. PRINT_DISPOSITION(DEFAULT, "default");
  2064. PRINT_DISPOSITION(DUB, "dub");
  2065. PRINT_DISPOSITION(ORIGINAL, "original");
  2066. PRINT_DISPOSITION(COMMENT, "comment");
  2067. PRINT_DISPOSITION(LYRICS, "lyrics");
  2068. PRINT_DISPOSITION(KARAOKE, "karaoke");
  2069. PRINT_DISPOSITION(FORCED, "forced");
  2070. PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
  2071. PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
  2072. PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
  2073. PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
  2074. PRINT_DISPOSITION(TIMED_THUMBNAILS, "timed_thumbnails");
  2075. writer_print_section_footer(w);
  2076. }
  2077. if (do_show_stream_tags)
  2078. ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
  2079. if (stream->nb_side_data) {
  2080. print_pkt_side_data(w, stream->side_data, stream->nb_side_data,
  2081. SECTION_ID_STREAM_SIDE_DATA_LIST,
  2082. SECTION_ID_STREAM_SIDE_DATA);
  2083. }
  2084. writer_print_section_footer(w);
  2085. av_bprint_finalize(&pbuf, NULL);
  2086. fflush(stdout);
  2087. return ret;
  2088. }
  2089. static int show_streams(WriterContext *w, InputFile *ifile)
  2090. {
  2091. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2092. int i, ret = 0;
  2093. writer_print_section_header(w, SECTION_ID_STREAMS);
  2094. for (i = 0; i < ifile->nb_streams; i++)
  2095. if (selected_streams[i]) {
  2096. ret = show_stream(w, fmt_ctx, i, &ifile->streams[i], 0);
  2097. if (ret < 0)
  2098. break;
  2099. }
  2100. writer_print_section_footer(w);
  2101. return ret;
  2102. }
  2103. static int show_program(WriterContext *w, InputFile *ifile, AVProgram *program)
  2104. {
  2105. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2106. int i, ret = 0;
  2107. writer_print_section_header(w, SECTION_ID_PROGRAM);
  2108. print_int("program_id", program->id);
  2109. print_int("program_num", program->program_num);
  2110. print_int("nb_streams", program->nb_stream_indexes);
  2111. print_int("pmt_pid", program->pmt_pid);
  2112. print_int("pcr_pid", program->pcr_pid);
  2113. print_ts("start_pts", program->start_time);
  2114. print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
  2115. print_ts("end_pts", program->end_time);
  2116. print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
  2117. if (do_show_program_tags)
  2118. ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
  2119. if (ret < 0)
  2120. goto end;
  2121. writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
  2122. for (i = 0; i < program->nb_stream_indexes; i++) {
  2123. if (selected_streams[program->stream_index[i]]) {
  2124. ret = show_stream(w, fmt_ctx, program->stream_index[i], &ifile->streams[program->stream_index[i]], 1);
  2125. if (ret < 0)
  2126. break;
  2127. }
  2128. }
  2129. writer_print_section_footer(w);
  2130. end:
  2131. writer_print_section_footer(w);
  2132. return ret;
  2133. }
  2134. static int show_programs(WriterContext *w, InputFile *ifile)
  2135. {
  2136. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2137. int i, ret = 0;
  2138. writer_print_section_header(w, SECTION_ID_PROGRAMS);
  2139. for (i = 0; i < fmt_ctx->nb_programs; i++) {
  2140. AVProgram *program = fmt_ctx->programs[i];
  2141. if (!program)
  2142. continue;
  2143. ret = show_program(w, ifile, program);
  2144. if (ret < 0)
  2145. break;
  2146. }
  2147. writer_print_section_footer(w);
  2148. return ret;
  2149. }
  2150. static int show_chapters(WriterContext *w, InputFile *ifile)
  2151. {
  2152. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2153. int i, ret = 0;
  2154. writer_print_section_header(w, SECTION_ID_CHAPTERS);
  2155. for (i = 0; i < fmt_ctx->nb_chapters; i++) {
  2156. AVChapter *chapter = fmt_ctx->chapters[i];
  2157. writer_print_section_header(w, SECTION_ID_CHAPTER);
  2158. print_int("id", chapter->id);
  2159. print_q ("time_base", chapter->time_base, '/');
  2160. print_int("start", chapter->start);
  2161. print_time("start_time", chapter->start, &chapter->time_base);
  2162. print_int("end", chapter->end);
  2163. print_time("end_time", chapter->end, &chapter->time_base);
  2164. if (do_show_chapter_tags)
  2165. ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
  2166. writer_print_section_footer(w);
  2167. }
  2168. writer_print_section_footer(w);
  2169. return ret;
  2170. }
  2171. static int show_format(WriterContext *w, InputFile *ifile)
  2172. {
  2173. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2174. char val_str[128];
  2175. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  2176. int ret = 0;
  2177. writer_print_section_header(w, SECTION_ID_FORMAT);
  2178. print_str_validate("filename", fmt_ctx->filename);
  2179. print_int("nb_streams", fmt_ctx->nb_streams);
  2180. print_int("nb_programs", fmt_ctx->nb_programs);
  2181. print_str("format_name", fmt_ctx->iformat->name);
  2182. if (!do_bitexact) {
  2183. if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
  2184. else print_str_opt("format_long_name", "unknown");
  2185. }
  2186. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  2187. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  2188. if (size >= 0) print_val ("size", size, unit_byte_str);
  2189. else print_str_opt("size", "N/A");
  2190. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  2191. else print_str_opt("bit_rate", "N/A");
  2192. print_int("probe_score", av_format_get_probe_score(fmt_ctx));
  2193. if (do_show_format_tags)
  2194. ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
  2195. writer_print_section_footer(w);
  2196. fflush(stdout);
  2197. return ret;
  2198. }
  2199. static void show_error(WriterContext *w, int err)
  2200. {
  2201. char errbuf[128];
  2202. const char *errbuf_ptr = errbuf;
  2203. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  2204. errbuf_ptr = strerror(AVUNERROR(err));
  2205. writer_print_section_header(w, SECTION_ID_ERROR);
  2206. print_int("code", err);
  2207. print_str("string", errbuf_ptr);
  2208. writer_print_section_footer(w);
  2209. }
  2210. static int open_input_file(InputFile *ifile, const char *filename)
  2211. {
  2212. int err, i, orig_nb_streams;
  2213. AVFormatContext *fmt_ctx = NULL;
  2214. AVDictionaryEntry *t;
  2215. AVDictionary **opts;
  2216. int scan_all_pmts_set = 0;
  2217. if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  2218. av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  2219. scan_all_pmts_set = 1;
  2220. }
  2221. if ((err = avformat_open_input(&fmt_ctx, filename,
  2222. iformat, &format_opts)) < 0) {
  2223. print_error(filename, err);
  2224. return err;
  2225. }
  2226. ifile->fmt_ctx = fmt_ctx;
  2227. if (scan_all_pmts_set)
  2228. av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  2229. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2230. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  2231. return AVERROR_OPTION_NOT_FOUND;
  2232. }
  2233. /* fill the streams in the format context */
  2234. opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
  2235. orig_nb_streams = fmt_ctx->nb_streams;
  2236. err = avformat_find_stream_info(fmt_ctx, opts);
  2237. for (i = 0; i < orig_nb_streams; i++)
  2238. av_dict_free(&opts[i]);
  2239. av_freep(&opts);
  2240. if (err < 0) {
  2241. print_error(filename, err);
  2242. return err;
  2243. }
  2244. av_dump_format(fmt_ctx, 0, filename, 0);
  2245. ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
  2246. sizeof(*ifile->streams));
  2247. if (!ifile->streams)
  2248. exit(1);
  2249. ifile->nb_streams = fmt_ctx->nb_streams;
  2250. /* bind a decoder to each input stream */
  2251. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  2252. InputStream *ist = &ifile->streams[i];
  2253. AVStream *stream = fmt_ctx->streams[i];
  2254. AVCodec *codec;
  2255. ist->st = stream;
  2256. if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
  2257. av_log(NULL, AV_LOG_WARNING,
  2258. "Failed to probe codec for input stream %d\n",
  2259. stream->index);
  2260. continue;
  2261. }
  2262. codec = avcodec_find_decoder(stream->codecpar->codec_id);
  2263. if (!codec) {
  2264. av_log(NULL, AV_LOG_WARNING,
  2265. "Unsupported codec with id %d for input stream %d\n",
  2266. stream->codecpar->codec_id, stream->index);
  2267. continue;
  2268. }
  2269. {
  2270. AVDictionary *opts = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
  2271. fmt_ctx, stream, codec);
  2272. ist->dec_ctx = avcodec_alloc_context3(codec);
  2273. if (!ist->dec_ctx)
  2274. exit(1);
  2275. err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
  2276. if (err < 0)
  2277. exit(1);
  2278. av_codec_set_pkt_timebase(ist->dec_ctx, stream->time_base);
  2279. ist->dec_ctx->framerate = stream->avg_frame_rate;
  2280. if (avcodec_open2(ist->dec_ctx, codec, &opts) < 0) {
  2281. av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
  2282. stream->index);
  2283. exit(1);
  2284. }
  2285. if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2286. av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
  2287. t->key, stream->index);
  2288. return AVERROR_OPTION_NOT_FOUND;
  2289. }
  2290. }
  2291. }
  2292. ifile->fmt_ctx = fmt_ctx;
  2293. return 0;
  2294. }
  2295. static void close_input_file(InputFile *ifile)
  2296. {
  2297. int i;
  2298. /* close decoder for each stream */
  2299. for (i = 0; i < ifile->nb_streams; i++)
  2300. if (ifile->streams[i].st->codecpar->codec_id != AV_CODEC_ID_NONE)
  2301. avcodec_free_context(&ifile->streams[i].dec_ctx);
  2302. av_freep(&ifile->streams);
  2303. ifile->nb_streams = 0;
  2304. avformat_close_input(&ifile->fmt_ctx);
  2305. }
  2306. static int probe_file(WriterContext *wctx, const char *filename)
  2307. {
  2308. InputFile ifile = { 0 };
  2309. int ret, i;
  2310. int section_id;
  2311. do_read_frames = do_show_frames || do_count_frames;
  2312. do_read_packets = do_show_packets || do_count_packets;
  2313. ret = open_input_file(&ifile, filename);
  2314. if (ret < 0)
  2315. goto end;
  2316. #define CHECK_END if (ret < 0) goto end
  2317. nb_streams = ifile.fmt_ctx->nb_streams;
  2318. REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,ifile.fmt_ctx->nb_streams);
  2319. REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,ifile.fmt_ctx->nb_streams);
  2320. REALLOCZ_ARRAY_STREAM(selected_streams,0,ifile.fmt_ctx->nb_streams);
  2321. for (i = 0; i < ifile.fmt_ctx->nb_streams; i++) {
  2322. if (stream_specifier) {
  2323. ret = avformat_match_stream_specifier(ifile.fmt_ctx,
  2324. ifile.fmt_ctx->streams[i],
  2325. stream_specifier);
  2326. CHECK_END;
  2327. else
  2328. selected_streams[i] = ret;
  2329. ret = 0;
  2330. } else {
  2331. selected_streams[i] = 1;
  2332. }
  2333. }
  2334. if (do_read_frames || do_read_packets) {
  2335. if (do_show_frames && do_show_packets &&
  2336. wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
  2337. section_id = SECTION_ID_PACKETS_AND_FRAMES;
  2338. else if (do_show_packets && !do_show_frames)
  2339. section_id = SECTION_ID_PACKETS;
  2340. else // (!do_show_packets && do_show_frames)
  2341. section_id = SECTION_ID_FRAMES;
  2342. if (do_show_frames || do_show_packets)
  2343. writer_print_section_header(wctx, section_id);
  2344. ret = read_packets(wctx, &ifile);
  2345. if (do_show_frames || do_show_packets)
  2346. writer_print_section_footer(wctx);
  2347. CHECK_END;
  2348. }
  2349. if (do_show_programs) {
  2350. ret = show_programs(wctx, &ifile);
  2351. CHECK_END;
  2352. }
  2353. if (do_show_streams) {
  2354. ret = show_streams(wctx, &ifile);
  2355. CHECK_END;
  2356. }
  2357. if (do_show_chapters) {
  2358. ret = show_chapters(wctx, &ifile);
  2359. CHECK_END;
  2360. }
  2361. if (do_show_format) {
  2362. ret = show_format(wctx, &ifile);
  2363. CHECK_END;
  2364. }
  2365. end:
  2366. if (ifile.fmt_ctx)
  2367. close_input_file(&ifile);
  2368. av_freep(&nb_streams_frames);
  2369. av_freep(&nb_streams_packets);
  2370. av_freep(&selected_streams);
  2371. return ret;
  2372. }
  2373. static void show_usage(void)
  2374. {
  2375. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  2376. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  2377. av_log(NULL, AV_LOG_INFO, "\n");
  2378. }
  2379. static void ffprobe_show_program_version(WriterContext *w)
  2380. {
  2381. AVBPrint pbuf;
  2382. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2383. writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
  2384. print_str("version", FFMPEG_VERSION);
  2385. print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
  2386. program_birth_year, CONFIG_THIS_YEAR);
  2387. print_str("compiler_ident", CC_IDENT);
  2388. print_str("configuration", FFMPEG_CONFIGURATION);
  2389. writer_print_section_footer(w);
  2390. av_bprint_finalize(&pbuf, NULL);
  2391. }
  2392. #define SHOW_LIB_VERSION(libname, LIBNAME) \
  2393. do { \
  2394. if (CONFIG_##LIBNAME) { \
  2395. unsigned int version = libname##_version(); \
  2396. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
  2397. print_str("name", "lib" #libname); \
  2398. print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
  2399. print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
  2400. print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
  2401. print_int("version", version); \
  2402. print_str("ident", LIB##LIBNAME##_IDENT); \
  2403. writer_print_section_footer(w); \
  2404. } \
  2405. } while (0)
  2406. static void ffprobe_show_library_versions(WriterContext *w)
  2407. {
  2408. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
  2409. SHOW_LIB_VERSION(avutil, AVUTIL);
  2410. SHOW_LIB_VERSION(avcodec, AVCODEC);
  2411. SHOW_LIB_VERSION(avformat, AVFORMAT);
  2412. SHOW_LIB_VERSION(avdevice, AVDEVICE);
  2413. SHOW_LIB_VERSION(avfilter, AVFILTER);
  2414. SHOW_LIB_VERSION(swscale, SWSCALE);
  2415. SHOW_LIB_VERSION(swresample, SWRESAMPLE);
  2416. SHOW_LIB_VERSION(postproc, POSTPROC);
  2417. writer_print_section_footer(w);
  2418. }
  2419. #define PRINT_PIX_FMT_FLAG(flagname, name) \
  2420. do { \
  2421. print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
  2422. } while (0)
  2423. static void ffprobe_show_pixel_formats(WriterContext *w)
  2424. {
  2425. const AVPixFmtDescriptor *pixdesc = NULL;
  2426. int i, n;
  2427. writer_print_section_header(w, SECTION_ID_PIXEL_FORMATS);
  2428. while (pixdesc = av_pix_fmt_desc_next(pixdesc)) {
  2429. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT);
  2430. print_str("name", pixdesc->name);
  2431. print_int("nb_components", pixdesc->nb_components);
  2432. if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
  2433. print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
  2434. print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
  2435. } else {
  2436. print_str_opt("log2_chroma_w", "N/A");
  2437. print_str_opt("log2_chroma_h", "N/A");
  2438. }
  2439. n = av_get_bits_per_pixel(pixdesc);
  2440. if (n) print_int ("bits_per_pixel", n);
  2441. else print_str_opt("bits_per_pixel", "N/A");
  2442. if (do_show_pixel_format_flags) {
  2443. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_FLAGS);
  2444. PRINT_PIX_FMT_FLAG(BE, "big_endian");
  2445. PRINT_PIX_FMT_FLAG(PAL, "palette");
  2446. PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
  2447. PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
  2448. PRINT_PIX_FMT_FLAG(PLANAR, "planar");
  2449. PRINT_PIX_FMT_FLAG(RGB, "rgb");
  2450. PRINT_PIX_FMT_FLAG(PSEUDOPAL, "pseudopal");
  2451. PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
  2452. writer_print_section_footer(w);
  2453. }
  2454. if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
  2455. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENTS);
  2456. for (i = 0; i < pixdesc->nb_components; i++) {
  2457. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENT);
  2458. print_int("index", i + 1);
  2459. print_int("bit_depth", pixdesc->comp[i].depth);
  2460. writer_print_section_footer(w);
  2461. }
  2462. writer_print_section_footer(w);
  2463. }
  2464. writer_print_section_footer(w);
  2465. }
  2466. writer_print_section_footer(w);
  2467. }
  2468. static int opt_format(void *optctx, const char *opt, const char *arg)
  2469. {
  2470. iformat = av_find_input_format(arg);
  2471. if (!iformat) {
  2472. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  2473. return AVERROR(EINVAL);
  2474. }
  2475. return 0;
  2476. }
  2477. static inline void mark_section_show_entries(SectionID section_id,
  2478. int show_all_entries, AVDictionary *entries)
  2479. {
  2480. struct section *section = &sections[section_id];
  2481. section->show_all_entries = show_all_entries;
  2482. if (show_all_entries) {
  2483. SectionID *id;
  2484. for (id = section->children_ids; *id != -1; id++)
  2485. mark_section_show_entries(*id, show_all_entries, entries);
  2486. } else {
  2487. av_dict_copy(&section->entries_to_show, entries, 0);
  2488. }
  2489. }
  2490. static int match_section(const char *section_name,
  2491. int show_all_entries, AVDictionary *entries)
  2492. {
  2493. int i, ret = 0;
  2494. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
  2495. const struct section *section = &sections[i];
  2496. if (!strcmp(section_name, section->name) ||
  2497. (section->unique_name && !strcmp(section_name, section->unique_name))) {
  2498. av_log(NULL, AV_LOG_DEBUG,
  2499. "'%s' matches section with unique name '%s'\n", section_name,
  2500. (char *)av_x_if_null(section->unique_name, section->name));
  2501. ret++;
  2502. mark_section_show_entries(section->id, show_all_entries, entries);
  2503. }
  2504. }
  2505. return ret;
  2506. }
  2507. static int opt_show_entries(void *optctx, const char *opt, const char *arg)
  2508. {
  2509. const char *p = arg;
  2510. int ret = 0;
  2511. while (*p) {
  2512. AVDictionary *entries = NULL;
  2513. char *section_name = av_get_token(&p, "=:");
  2514. int show_all_entries = 0;
  2515. if (!section_name) {
  2516. av_log(NULL, AV_LOG_ERROR,
  2517. "Missing section name for option '%s'\n", opt);
  2518. return AVERROR(EINVAL);
  2519. }
  2520. if (*p == '=') {
  2521. p++;
  2522. while (*p && *p != ':') {
  2523. char *entry = av_get_token(&p, ",:");
  2524. if (!entry)
  2525. break;
  2526. av_log(NULL, AV_LOG_VERBOSE,
  2527. "Adding '%s' to the entries to show in section '%s'\n",
  2528. entry, section_name);
  2529. av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
  2530. if (*p == ',')
  2531. p++;
  2532. }
  2533. } else {
  2534. show_all_entries = 1;
  2535. }
  2536. ret = match_section(section_name, show_all_entries, entries);
  2537. if (ret == 0) {
  2538. av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
  2539. ret = AVERROR(EINVAL);
  2540. }
  2541. av_dict_free(&entries);
  2542. av_free(section_name);
  2543. if (ret <= 0)
  2544. break;
  2545. if (*p)
  2546. p++;
  2547. }
  2548. return ret;
  2549. }
  2550. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  2551. {
  2552. char *buf = av_asprintf("format=%s", arg);
  2553. int ret;
  2554. if (!buf)
  2555. return AVERROR(ENOMEM);
  2556. av_log(NULL, AV_LOG_WARNING,
  2557. "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
  2558. opt, arg);
  2559. ret = opt_show_entries(optctx, opt, buf);
  2560. av_free(buf);
  2561. return ret;
  2562. }
  2563. static void opt_input_file(void *optctx, const char *arg)
  2564. {
  2565. if (input_filename) {
  2566. av_log(NULL, AV_LOG_ERROR,
  2567. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  2568. arg, input_filename);
  2569. exit_program(1);
  2570. }
  2571. if (!strcmp(arg, "-"))
  2572. arg = "pipe:";
  2573. input_filename = arg;
  2574. }
  2575. static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
  2576. {
  2577. opt_input_file(optctx, arg);
  2578. return 0;
  2579. }
  2580. void show_help_default(const char *opt, const char *arg)
  2581. {
  2582. av_log_set_callback(log_callback_help);
  2583. show_usage();
  2584. show_help_options(options, "Main options:", 0, 0, 0);
  2585. printf("\n");
  2586. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  2587. }
  2588. /**
  2589. * Parse interval specification, according to the format:
  2590. * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
  2591. * INTERVALS ::= INTERVAL[,INTERVALS]
  2592. */
  2593. static int parse_read_interval(const char *interval_spec,
  2594. ReadInterval *interval)
  2595. {
  2596. int ret = 0;
  2597. char *next, *p, *spec = av_strdup(interval_spec);
  2598. if (!spec)
  2599. return AVERROR(ENOMEM);
  2600. if (!*spec) {
  2601. av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
  2602. ret = AVERROR(EINVAL);
  2603. goto end;
  2604. }
  2605. p = spec;
  2606. next = strchr(spec, '%');
  2607. if (next)
  2608. *next++ = 0;
  2609. /* parse first part */
  2610. if (*p) {
  2611. interval->has_start = 1;
  2612. if (*p == '+') {
  2613. interval->start_is_offset = 1;
  2614. p++;
  2615. } else {
  2616. interval->start_is_offset = 0;
  2617. }
  2618. ret = av_parse_time(&interval->start, p, 1);
  2619. if (ret < 0) {
  2620. av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
  2621. goto end;
  2622. }
  2623. } else {
  2624. interval->has_start = 0;
  2625. }
  2626. /* parse second part */
  2627. p = next;
  2628. if (p && *p) {
  2629. int64_t us;
  2630. interval->has_end = 1;
  2631. if (*p == '+') {
  2632. interval->end_is_offset = 1;
  2633. p++;
  2634. } else {
  2635. interval->end_is_offset = 0;
  2636. }
  2637. if (interval->end_is_offset && *p == '#') {
  2638. long long int lli;
  2639. char *tail;
  2640. interval->duration_frames = 1;
  2641. p++;
  2642. lli = strtoll(p, &tail, 10);
  2643. if (*tail || lli < 0) {
  2644. av_log(NULL, AV_LOG_ERROR,
  2645. "Invalid or negative value '%s' for duration number of frames\n", p);
  2646. goto end;
  2647. }
  2648. interval->end = lli;
  2649. } else {
  2650. ret = av_parse_time(&us, p, 1);
  2651. if (ret < 0) {
  2652. av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
  2653. goto end;
  2654. }
  2655. interval->end = us;
  2656. }
  2657. } else {
  2658. interval->has_end = 0;
  2659. }
  2660. end:
  2661. av_free(spec);
  2662. return ret;
  2663. }
  2664. static int parse_read_intervals(const char *intervals_spec)
  2665. {
  2666. int ret, n, i;
  2667. char *p, *spec = av_strdup(intervals_spec);
  2668. if (!spec)
  2669. return AVERROR(ENOMEM);
  2670. /* preparse specification, get number of intervals */
  2671. for (n = 0, p = spec; *p; p++)
  2672. if (*p == ',')
  2673. n++;
  2674. n++;
  2675. read_intervals = av_malloc_array(n, sizeof(*read_intervals));
  2676. if (!read_intervals) {
  2677. ret = AVERROR(ENOMEM);
  2678. goto end;
  2679. }
  2680. read_intervals_nb = n;
  2681. /* parse intervals */
  2682. p = spec;
  2683. for (i = 0; p; i++) {
  2684. char *next;
  2685. av_assert0(i < read_intervals_nb);
  2686. next = strchr(p, ',');
  2687. if (next)
  2688. *next++ = 0;
  2689. read_intervals[i].id = i;
  2690. ret = parse_read_interval(p, &read_intervals[i]);
  2691. if (ret < 0) {
  2692. av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
  2693. i, p);
  2694. goto end;
  2695. }
  2696. av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
  2697. log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
  2698. p = next;
  2699. }
  2700. av_assert0(i == read_intervals_nb);
  2701. end:
  2702. av_free(spec);
  2703. return ret;
  2704. }
  2705. static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
  2706. {
  2707. return parse_read_intervals(arg);
  2708. }
  2709. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  2710. {
  2711. show_value_unit = 1;
  2712. use_value_prefix = 1;
  2713. use_byte_value_binary_prefix = 1;
  2714. use_value_sexagesimal_format = 1;
  2715. return 0;
  2716. }
  2717. static void print_section(SectionID id, int level)
  2718. {
  2719. const SectionID *pid;
  2720. const struct section *section = &sections[id];
  2721. printf("%c%c%c",
  2722. section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
  2723. section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
  2724. section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
  2725. printf("%*c %s", level * 4, ' ', section->name);
  2726. if (section->unique_name)
  2727. printf("/%s", section->unique_name);
  2728. printf("\n");
  2729. for (pid = section->children_ids; *pid != -1; pid++)
  2730. print_section(*pid, level+1);
  2731. }
  2732. static int opt_sections(void *optctx, const char *opt, const char *arg)
  2733. {
  2734. printf("Sections:\n"
  2735. "W.. = Section is a wrapper (contains other sections, no local entries)\n"
  2736. ".A. = Section contains an array of elements of the same type\n"
  2737. "..V = Section may contain a variable number of fields with variable keys\n"
  2738. "FLAGS NAME/UNIQUE_NAME\n"
  2739. "---\n");
  2740. print_section(SECTION_ID_ROOT, 0);
  2741. return 0;
  2742. }
  2743. static int opt_show_versions(const char *opt, const char *arg)
  2744. {
  2745. mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
  2746. mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
  2747. return 0;
  2748. }
  2749. #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
  2750. static int opt_show_##section(const char *opt, const char *arg) \
  2751. { \
  2752. mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
  2753. return 0; \
  2754. }
  2755. DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
  2756. DEFINE_OPT_SHOW_SECTION(error, ERROR)
  2757. DEFINE_OPT_SHOW_SECTION(format, FORMAT)
  2758. DEFINE_OPT_SHOW_SECTION(frames, FRAMES)
  2759. DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
  2760. DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
  2761. DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
  2762. DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
  2763. DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
  2764. DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
  2765. static const OptionDef real_options[] = {
  2766. #include "cmdutils_common_opts.h"
  2767. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  2768. { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
  2769. { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
  2770. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  2771. "use binary prefixes for byte units" },
  2772. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  2773. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  2774. { "pretty", 0, {.func_arg = opt_pretty},
  2775. "prettify the format of displayed values, make it more human readable" },
  2776. { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
  2777. "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
  2778. { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
  2779. { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
  2780. { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
  2781. { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
  2782. { "show_data_hash", OPT_STRING | HAS_ARG, {(void*)&show_data_hash}, "show packets data hash" },
  2783. { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
  2784. { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
  2785. { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
  2786. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  2787. "show a particular entry from the format/container info", "entry" },
  2788. { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
  2789. "show a set of specified entries", "entry_list" },
  2790. { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
  2791. { "show_programs", 0, {(void*)&opt_show_programs}, "show programs info" },
  2792. { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
  2793. { "show_chapters", 0, {(void*)&opt_show_chapters}, "show chapters info" },
  2794. { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
  2795. { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
  2796. { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
  2797. { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
  2798. { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
  2799. { "show_pixel_formats", 0, {(void*)&opt_show_pixel_formats}, "show pixel format descriptions" },
  2800. { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
  2801. { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
  2802. { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
  2803. { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
  2804. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
  2805. { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
  2806. { NULL, },
  2807. };
  2808. static inline int check_section_show_entries(int section_id)
  2809. {
  2810. int *id;
  2811. struct section *section = &sections[section_id];
  2812. if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
  2813. return 1;
  2814. for (id = section->children_ids; *id != -1; id++)
  2815. if (check_section_show_entries(*id))
  2816. return 1;
  2817. return 0;
  2818. }
  2819. #define SET_DO_SHOW(id, varname) do { \
  2820. if (check_section_show_entries(SECTION_ID_##id)) \
  2821. do_show_##varname = 1; \
  2822. } while (0)
  2823. int main(int argc, char **argv)
  2824. {
  2825. const Writer *w;
  2826. WriterContext *wctx;
  2827. char *buf;
  2828. char *w_name = NULL, *w_args = NULL;
  2829. int ret, i;
  2830. init_dynload();
  2831. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  2832. register_exit(ffprobe_cleanup);
  2833. options = real_options;
  2834. parse_loglevel(argc, argv, options);
  2835. av_register_all();
  2836. avformat_network_init();
  2837. init_opts();
  2838. #if CONFIG_AVDEVICE
  2839. avdevice_register_all();
  2840. #endif
  2841. show_banner(argc, argv, options);
  2842. parse_options(NULL, argc, argv, options, opt_input_file);
  2843. /* mark things to show, based on -show_entries */
  2844. SET_DO_SHOW(CHAPTERS, chapters);
  2845. SET_DO_SHOW(ERROR, error);
  2846. SET_DO_SHOW(FORMAT, format);
  2847. SET_DO_SHOW(FRAMES, frames);
  2848. SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
  2849. SET_DO_SHOW(PACKETS, packets);
  2850. SET_DO_SHOW(PIXEL_FORMATS, pixel_formats);
  2851. SET_DO_SHOW(PIXEL_FORMAT_FLAGS, pixel_format_flags);
  2852. SET_DO_SHOW(PIXEL_FORMAT_COMPONENTS, pixel_format_components);
  2853. SET_DO_SHOW(PROGRAM_VERSION, program_version);
  2854. SET_DO_SHOW(PROGRAMS, programs);
  2855. SET_DO_SHOW(STREAMS, streams);
  2856. SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
  2857. SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
  2858. SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
  2859. SET_DO_SHOW(FORMAT_TAGS, format_tags);
  2860. SET_DO_SHOW(FRAME_TAGS, frame_tags);
  2861. SET_DO_SHOW(PROGRAM_TAGS, program_tags);
  2862. SET_DO_SHOW(STREAM_TAGS, stream_tags);
  2863. SET_DO_SHOW(PROGRAM_STREAM_TAGS, stream_tags);
  2864. SET_DO_SHOW(PACKET_TAGS, packet_tags);
  2865. if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
  2866. av_log(NULL, AV_LOG_ERROR,
  2867. "-bitexact and -show_program_version or -show_library_versions "
  2868. "options are incompatible\n");
  2869. ret = AVERROR(EINVAL);
  2870. goto end;
  2871. }
  2872. writer_register_all();
  2873. if (!print_format)
  2874. print_format = av_strdup("default");
  2875. if (!print_format) {
  2876. ret = AVERROR(ENOMEM);
  2877. goto end;
  2878. }
  2879. w_name = av_strtok(print_format, "=", &buf);
  2880. if (!w_name) {
  2881. av_log(NULL, AV_LOG_ERROR,
  2882. "No name specified for the output format\n");
  2883. ret = AVERROR(EINVAL);
  2884. goto end;
  2885. }
  2886. w_args = buf;
  2887. if (show_data_hash) {
  2888. if ((ret = av_hash_alloc(&hash, show_data_hash)) < 0) {
  2889. if (ret == AVERROR(EINVAL)) {
  2890. const char *n;
  2891. av_log(NULL, AV_LOG_ERROR,
  2892. "Unknown hash algorithm '%s'\nKnown algorithms:",
  2893. show_data_hash);
  2894. for (i = 0; (n = av_hash_names(i)); i++)
  2895. av_log(NULL, AV_LOG_ERROR, " %s", n);
  2896. av_log(NULL, AV_LOG_ERROR, "\n");
  2897. }
  2898. goto end;
  2899. }
  2900. }
  2901. w = writer_get_by_name(w_name);
  2902. if (!w) {
  2903. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  2904. ret = AVERROR(EINVAL);
  2905. goto end;
  2906. }
  2907. if ((ret = writer_open(&wctx, w, w_args,
  2908. sections, FF_ARRAY_ELEMS(sections))) >= 0) {
  2909. if (w == &xml_writer)
  2910. wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
  2911. writer_print_section_header(wctx, SECTION_ID_ROOT);
  2912. if (do_show_program_version)
  2913. ffprobe_show_program_version(wctx);
  2914. if (do_show_library_versions)
  2915. ffprobe_show_library_versions(wctx);
  2916. if (do_show_pixel_formats)
  2917. ffprobe_show_pixel_formats(wctx);
  2918. if (!input_filename &&
  2919. ((do_show_format || do_show_programs || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
  2920. (!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
  2921. show_usage();
  2922. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  2923. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  2924. ret = AVERROR(EINVAL);
  2925. } else if (input_filename) {
  2926. ret = probe_file(wctx, input_filename);
  2927. if (ret < 0 && do_show_error)
  2928. show_error(wctx, ret);
  2929. }
  2930. writer_print_section_footer(wctx);
  2931. writer_close(&wctx);
  2932. }
  2933. end:
  2934. av_freep(&print_format);
  2935. av_freep(&read_intervals);
  2936. av_hash_freep(&hash);
  2937. uninit_opts();
  2938. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
  2939. av_dict_free(&(sections[i].entries_to_show));
  2940. avformat_network_deinit();
  2941. return ret < 0;
  2942. }