ffprobe.c 115 KB

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