ffprobe.c 71 KB

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