ffprobe.c 82 KB

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