You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2164 lines
71KB

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