ffprobe.c 105 KB

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