ffprobe.c 72 KB

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