ffprobe.c 88 KB

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