ffprobe.c 107 KB

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