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.

2154 lines
70KB

  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)
  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 (ts == AV_NOPTS_VALUE) {
  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)
  289. {
  290. if (ts == AV_NOPTS_VALUE) {
  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. default: av_bprint_chars(dst, *p, 1); break;
  678. }
  679. }
  680. return dst->str;
  681. }
  682. static void flat_print_chapter_header(WriterContext *wctx, const char *chapter)
  683. {
  684. FlatContext *flat = wctx->priv;
  685. flat->chapter = chapter;
  686. }
  687. static void flat_print_section_header(WriterContext *wctx, const char *section)
  688. {
  689. FlatContext *flat = wctx->priv;
  690. flat->section = section;
  691. }
  692. static void flat_print_section(WriterContext *wctx)
  693. {
  694. FlatContext *flat = wctx->priv;
  695. int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
  696. : wctx->nb_section;
  697. if (flat->hierarchical && wctx->multiple_sections)
  698. printf("%s%c", flat->chapter, flat->sep);
  699. printf("%s%c", flat->section, flat->sep);
  700. if (wctx->multiple_sections)
  701. printf("%d%c", n, flat->sep);
  702. }
  703. static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
  704. {
  705. flat_print_section(wctx);
  706. printf("%s=%lld\n", key, value);
  707. }
  708. static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
  709. {
  710. FlatContext *flat = wctx->priv;
  711. AVBPrint buf;
  712. flat_print_section(wctx);
  713. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  714. printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
  715. av_bprint_clear(&buf);
  716. printf("\"%s\"\n", flat_escape_value_str(&buf, value));
  717. av_bprint_finalize(&buf, NULL);
  718. }
  719. static void flat_show_tags(WriterContext *wctx, AVDictionary *dict)
  720. {
  721. FlatContext *flat = wctx->priv;
  722. AVBPrint buf;
  723. AVDictionaryEntry *tag = NULL;
  724. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  725. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  726. flat_print_section(wctx);
  727. av_bprint_clear(&buf);
  728. printf("tags%c%s=", flat->sep, flat_escape_key_str(&buf, tag->key, flat->sep));
  729. av_bprint_clear(&buf);
  730. printf("\"%s\"\n", flat_escape_value_str(&buf, tag->value));
  731. }
  732. av_bprint_finalize(&buf, NULL);
  733. }
  734. static const Writer flat_writer = {
  735. .name = "flat",
  736. .priv_size = sizeof(FlatContext),
  737. .init = flat_init,
  738. .print_chapter_header = flat_print_chapter_header,
  739. .print_section_header = flat_print_section_header,
  740. .print_integer = flat_print_int,
  741. .print_string = flat_print_str,
  742. .show_tags = flat_show_tags,
  743. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  744. };
  745. /* INI format output */
  746. typedef struct {
  747. const AVClass *class;
  748. AVBPrint chapter_name, section_name;
  749. int hierarchical;
  750. } INIContext;
  751. #undef OFFSET
  752. #define OFFSET(x) offsetof(INIContext, x)
  753. static const AVOption ini_options[] = {
  754. {"hierachical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  755. {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  756. {NULL},
  757. };
  758. static const char *ini_get_name(void *ctx)
  759. {
  760. return "ini";
  761. }
  762. static const AVClass ini_class = {
  763. "INIContext",
  764. ini_get_name,
  765. ini_options
  766. };
  767. static av_cold int ini_init(WriterContext *wctx, const char *args, void *opaque)
  768. {
  769. INIContext *ini = wctx->priv;
  770. int err;
  771. av_bprint_init(&ini->chapter_name, 1, AV_BPRINT_SIZE_UNLIMITED);
  772. av_bprint_init(&ini->section_name, 1, AV_BPRINT_SIZE_UNLIMITED);
  773. ini->class = &ini_class;
  774. av_opt_set_defaults(ini);
  775. if (args && (err = av_set_options_string(ini, args, "=", ":")) < 0) {
  776. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  777. return err;
  778. }
  779. return 0;
  780. }
  781. static av_cold void ini_uninit(WriterContext *wctx)
  782. {
  783. INIContext *ini = wctx->priv;
  784. av_bprint_finalize(&ini->chapter_name, NULL);
  785. av_bprint_finalize(&ini->section_name, NULL);
  786. }
  787. static void ini_print_header(WriterContext *wctx)
  788. {
  789. printf("# ffprobe output\n\n");
  790. }
  791. static char *ini_escape_str(AVBPrint *dst, const char *src)
  792. {
  793. int i = 0;
  794. char c = 0;
  795. while (c = src[i++]) {
  796. switch (c) {
  797. case '\b': av_bprintf(dst, "%s", "\\b"); break;
  798. case '\f': av_bprintf(dst, "%s", "\\f"); break;
  799. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  800. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  801. case '\t': av_bprintf(dst, "%s", "\\t"); break;
  802. case '\\':
  803. case '#' :
  804. case '=' :
  805. case ':' : av_bprint_chars(dst, '\\', 1);
  806. default:
  807. if ((unsigned char)c < 32)
  808. av_bprintf(dst, "\\x00%02x", c & 0xff);
  809. else
  810. av_bprint_chars(dst, c, 1);
  811. break;
  812. }
  813. }
  814. return dst->str;
  815. }
  816. static void ini_print_chapter_header(WriterContext *wctx, const char *chapter)
  817. {
  818. INIContext *ini = wctx->priv;
  819. av_bprint_clear(&ini->chapter_name);
  820. av_bprintf(&ini->chapter_name, "%s", chapter);
  821. if (wctx->nb_chapter)
  822. printf("\n");
  823. }
  824. static void ini_print_section_header(WriterContext *wctx, const char *section)
  825. {
  826. INIContext *ini = wctx->priv;
  827. int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
  828. : wctx->nb_section;
  829. if (wctx->nb_section)
  830. printf("\n");
  831. av_bprint_clear(&ini->section_name);
  832. if (ini->hierarchical && wctx->multiple_sections)
  833. av_bprintf(&ini->section_name, "%s.", ini->chapter_name.str);
  834. av_bprintf(&ini->section_name, "%s", section);
  835. if (wctx->multiple_sections)
  836. av_bprintf(&ini->section_name, ".%d", n);
  837. printf("[%s]\n", ini->section_name.str);
  838. }
  839. static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
  840. {
  841. AVBPrint buf;
  842. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  843. printf("%s=", ini_escape_str(&buf, key));
  844. av_bprint_clear(&buf);
  845. printf("%s\n", ini_escape_str(&buf, value));
  846. av_bprint_finalize(&buf, NULL);
  847. }
  848. static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
  849. {
  850. printf("%s=%lld\n", key, value);
  851. }
  852. static void ini_show_tags(WriterContext *wctx, AVDictionary *dict)
  853. {
  854. INIContext *ini = wctx->priv;
  855. AVDictionaryEntry *tag = NULL;
  856. int is_first = 1;
  857. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  858. if (is_first) {
  859. printf("\n[%s.tags]\n", ini->section_name.str);
  860. is_first = 0;
  861. }
  862. writer_print_string(wctx, tag->key, tag->value, 0);
  863. }
  864. }
  865. static const Writer ini_writer = {
  866. .name = "ini",
  867. .priv_size = sizeof(INIContext),
  868. .init = ini_init,
  869. .uninit = ini_uninit,
  870. .print_header = ini_print_header,
  871. .print_chapter_header = ini_print_chapter_header,
  872. .print_section_header = ini_print_section_header,
  873. .print_integer = ini_print_int,
  874. .print_string = ini_print_str,
  875. .show_tags = ini_show_tags,
  876. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  877. };
  878. /* JSON output */
  879. typedef struct {
  880. const AVClass *class;
  881. int indent_level;
  882. int compact;
  883. const char *item_sep, *item_start_end;
  884. } JSONContext;
  885. #undef OFFSET
  886. #define OFFSET(x) offsetof(JSONContext, x)
  887. static const AVOption json_options[]= {
  888. { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  889. { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  890. { NULL }
  891. };
  892. static const char *json_get_name(void *ctx)
  893. {
  894. return "json";
  895. }
  896. static const AVClass json_class = {
  897. "JSONContext",
  898. json_get_name,
  899. json_options
  900. };
  901. static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
  902. {
  903. JSONContext *json = wctx->priv;
  904. int err;
  905. json->class = &json_class;
  906. av_opt_set_defaults(json);
  907. if (args &&
  908. (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
  909. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  910. return err;
  911. }
  912. json->item_sep = json->compact ? ", " : ",\n";
  913. json->item_start_end = json->compact ? " " : "\n";
  914. return 0;
  915. }
  916. static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
  917. {
  918. static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
  919. static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
  920. const char *p;
  921. for (p = src; *p; p++) {
  922. char *s = strchr(json_escape, *p);
  923. if (s) {
  924. av_bprint_chars(dst, '\\', 1);
  925. av_bprint_chars(dst, json_subst[s - json_escape], 1);
  926. } else if ((unsigned char)*p < 32) {
  927. av_bprintf(dst, "\\u00%02x", *p & 0xff);
  928. } else {
  929. av_bprint_chars(dst, *p, 1);
  930. }
  931. }
  932. return dst->str;
  933. }
  934. static void json_print_header(WriterContext *wctx)
  935. {
  936. JSONContext *json = wctx->priv;
  937. printf("{");
  938. json->indent_level++;
  939. }
  940. static void json_print_footer(WriterContext *wctx)
  941. {
  942. JSONContext *json = wctx->priv;
  943. json->indent_level--;
  944. printf("\n}\n");
  945. }
  946. #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
  947. static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
  948. {
  949. JSONContext *json = wctx->priv;
  950. AVBPrint buf;
  951. if (wctx->nb_chapter)
  952. printf(",");
  953. printf("\n");
  954. if (wctx->multiple_sections) {
  955. JSON_INDENT();
  956. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  957. printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
  958. av_bprint_finalize(&buf, NULL);
  959. json->indent_level++;
  960. }
  961. }
  962. static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
  963. {
  964. JSONContext *json = wctx->priv;
  965. if (wctx->multiple_sections) {
  966. printf("\n");
  967. json->indent_level--;
  968. JSON_INDENT();
  969. printf("]");
  970. }
  971. }
  972. static void json_print_section_header(WriterContext *wctx, const char *section)
  973. {
  974. JSONContext *json = wctx->priv;
  975. if (wctx->nb_section)
  976. printf(",\n");
  977. JSON_INDENT();
  978. if (!wctx->multiple_sections)
  979. printf("\"%s\": ", section);
  980. printf("{%s", json->item_start_end);
  981. json->indent_level++;
  982. /* this is required so the parser can distinguish between packets and frames */
  983. if (wctx->is_packets_and_frames) {
  984. if (!json->compact)
  985. JSON_INDENT();
  986. printf("\"type\": \"%s\"%s", section, json->item_sep);
  987. }
  988. }
  989. static void json_print_section_footer(WriterContext *wctx, const char *section)
  990. {
  991. JSONContext *json = wctx->priv;
  992. printf("%s", json->item_start_end);
  993. json->indent_level--;
  994. if (!json->compact)
  995. JSON_INDENT();
  996. printf("}");
  997. }
  998. static inline void json_print_item_str(WriterContext *wctx,
  999. const char *key, const char *value)
  1000. {
  1001. AVBPrint buf;
  1002. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1003. printf("\"%s\":", json_escape_str(&buf, key, wctx));
  1004. av_bprint_clear(&buf);
  1005. printf(" \"%s\"", json_escape_str(&buf, value, wctx));
  1006. av_bprint_finalize(&buf, NULL);
  1007. }
  1008. static void json_print_str(WriterContext *wctx, const char *key, const char *value)
  1009. {
  1010. JSONContext *json = wctx->priv;
  1011. if (wctx->nb_item) printf("%s", json->item_sep);
  1012. if (!json->compact)
  1013. JSON_INDENT();
  1014. json_print_item_str(wctx, key, value);
  1015. }
  1016. static void json_print_int(WriterContext *wctx, const char *key, long long int value)
  1017. {
  1018. JSONContext *json = wctx->priv;
  1019. AVBPrint buf;
  1020. if (wctx->nb_item) printf("%s", json->item_sep);
  1021. if (!json->compact)
  1022. JSON_INDENT();
  1023. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1024. printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
  1025. av_bprint_finalize(&buf, NULL);
  1026. }
  1027. static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
  1028. {
  1029. JSONContext *json = wctx->priv;
  1030. AVDictionaryEntry *tag = NULL;
  1031. int is_first = 1;
  1032. if (!dict)
  1033. return;
  1034. printf("%s", json->item_sep);
  1035. if (!json->compact)
  1036. JSON_INDENT();
  1037. printf("\"tags\": {%s", json->item_start_end);
  1038. json->indent_level++;
  1039. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  1040. if (is_first) is_first = 0;
  1041. else printf("%s", json->item_sep);
  1042. if (!json->compact)
  1043. JSON_INDENT();
  1044. json_print_item_str(wctx, tag->key, tag->value);
  1045. }
  1046. json->indent_level--;
  1047. printf("%s", json->item_start_end);
  1048. if (!json->compact)
  1049. JSON_INDENT();
  1050. printf("}");
  1051. }
  1052. static const Writer json_writer = {
  1053. .name = "json",
  1054. .priv_size = sizeof(JSONContext),
  1055. .init = json_init,
  1056. .print_header = json_print_header,
  1057. .print_footer = json_print_footer,
  1058. .print_chapter_header = json_print_chapter_header,
  1059. .print_chapter_footer = json_print_chapter_footer,
  1060. .print_section_header = json_print_section_header,
  1061. .print_section_footer = json_print_section_footer,
  1062. .print_integer = json_print_int,
  1063. .print_string = json_print_str,
  1064. .show_tags = json_show_tags,
  1065. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1066. };
  1067. /* XML output */
  1068. typedef struct {
  1069. const AVClass *class;
  1070. int within_tag;
  1071. int indent_level;
  1072. int fully_qualified;
  1073. int xsd_strict;
  1074. } XMLContext;
  1075. #undef OFFSET
  1076. #define OFFSET(x) offsetof(XMLContext, x)
  1077. static const AVOption xml_options[] = {
  1078. {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  1079. {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  1080. {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  1081. {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  1082. {NULL},
  1083. };
  1084. static const char *xml_get_name(void *ctx)
  1085. {
  1086. return "xml";
  1087. }
  1088. static const AVClass xml_class = {
  1089. "XMLContext",
  1090. xml_get_name,
  1091. xml_options
  1092. };
  1093. static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
  1094. {
  1095. XMLContext *xml = wctx->priv;
  1096. int err;
  1097. xml->class = &xml_class;
  1098. av_opt_set_defaults(xml);
  1099. if (args &&
  1100. (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
  1101. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  1102. return err;
  1103. }
  1104. if (xml->xsd_strict) {
  1105. xml->fully_qualified = 1;
  1106. #define CHECK_COMPLIANCE(opt, opt_name) \
  1107. if (opt) { \
  1108. av_log(wctx, AV_LOG_ERROR, \
  1109. "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
  1110. "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
  1111. return AVERROR(EINVAL); \
  1112. }
  1113. CHECK_COMPLIANCE(show_private_data, "private");
  1114. CHECK_COMPLIANCE(show_value_unit, "unit");
  1115. CHECK_COMPLIANCE(use_value_prefix, "prefix");
  1116. if (do_show_frames && do_show_packets) {
  1117. av_log(wctx, AV_LOG_ERROR,
  1118. "Interleaved frames and packets are not allowed in XSD. "
  1119. "Select only one between the -show_frames and the -show_packets options.\n");
  1120. return AVERROR(EINVAL);
  1121. }
  1122. }
  1123. return 0;
  1124. }
  1125. static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
  1126. {
  1127. const char *p;
  1128. for (p = src; *p; p++) {
  1129. switch (*p) {
  1130. case '&' : av_bprintf(dst, "%s", "&amp;"); break;
  1131. case '<' : av_bprintf(dst, "%s", "&lt;"); break;
  1132. case '>' : av_bprintf(dst, "%s", "&gt;"); break;
  1133. case '\"': av_bprintf(dst, "%s", "&quot;"); break;
  1134. case '\'': av_bprintf(dst, "%s", "&apos;"); break;
  1135. default: av_bprint_chars(dst, *p, 1);
  1136. }
  1137. }
  1138. return dst->str;
  1139. }
  1140. static void xml_print_header(WriterContext *wctx)
  1141. {
  1142. XMLContext *xml = wctx->priv;
  1143. const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
  1144. "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
  1145. "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
  1146. printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  1147. printf("<%sffprobe%s>\n",
  1148. xml->fully_qualified ? "ffprobe:" : "",
  1149. xml->fully_qualified ? qual : "");
  1150. xml->indent_level++;
  1151. }
  1152. static void xml_print_footer(WriterContext *wctx)
  1153. {
  1154. XMLContext *xml = wctx->priv;
  1155. xml->indent_level--;
  1156. printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
  1157. }
  1158. #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
  1159. static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
  1160. {
  1161. XMLContext *xml = wctx->priv;
  1162. if (wctx->nb_chapter)
  1163. printf("\n");
  1164. if (wctx->multiple_sections) {
  1165. XML_INDENT(); printf("<%s>\n", chapter);
  1166. xml->indent_level++;
  1167. }
  1168. }
  1169. static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
  1170. {
  1171. XMLContext *xml = wctx->priv;
  1172. if (wctx->multiple_sections) {
  1173. xml->indent_level--;
  1174. XML_INDENT(); printf("</%s>\n", chapter);
  1175. }
  1176. }
  1177. static void xml_print_section_header(WriterContext *wctx, const char *section)
  1178. {
  1179. XMLContext *xml = wctx->priv;
  1180. XML_INDENT(); printf("<%s ", section);
  1181. xml->within_tag = 1;
  1182. }
  1183. static void xml_print_section_footer(WriterContext *wctx, const char *section)
  1184. {
  1185. XMLContext *xml = wctx->priv;
  1186. if (xml->within_tag)
  1187. printf("/>\n");
  1188. else {
  1189. XML_INDENT(); printf("</%s>\n", section);
  1190. }
  1191. }
  1192. static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
  1193. {
  1194. AVBPrint buf;
  1195. if (wctx->nb_item)
  1196. printf(" ");
  1197. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1198. printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
  1199. av_bprint_finalize(&buf, NULL);
  1200. }
  1201. static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
  1202. {
  1203. if (wctx->nb_item)
  1204. printf(" ");
  1205. printf("%s=\"%lld\"", key, value);
  1206. }
  1207. static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
  1208. {
  1209. XMLContext *xml = wctx->priv;
  1210. AVDictionaryEntry *tag = NULL;
  1211. int is_first = 1;
  1212. AVBPrint buf;
  1213. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1214. xml->indent_level++;
  1215. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  1216. if (is_first) {
  1217. /* close section tag */
  1218. printf(">\n");
  1219. xml->within_tag = 0;
  1220. is_first = 0;
  1221. }
  1222. XML_INDENT();
  1223. av_bprint_clear(&buf);
  1224. printf("<tag key=\"%s\"", xml_escape_str(&buf, tag->key, wctx));
  1225. av_bprint_clear(&buf);
  1226. printf(" value=\"%s\"/>\n", xml_escape_str(&buf, tag->value, wctx));
  1227. }
  1228. av_bprint_finalize(&buf, NULL);
  1229. xml->indent_level--;
  1230. }
  1231. static Writer xml_writer = {
  1232. .name = "xml",
  1233. .priv_size = sizeof(XMLContext),
  1234. .init = xml_init,
  1235. .print_header = xml_print_header,
  1236. .print_footer = xml_print_footer,
  1237. .print_chapter_header = xml_print_chapter_header,
  1238. .print_chapter_footer = xml_print_chapter_footer,
  1239. .print_section_header = xml_print_section_header,
  1240. .print_section_footer = xml_print_section_footer,
  1241. .print_integer = xml_print_int,
  1242. .print_string = xml_print_str,
  1243. .show_tags = xml_show_tags,
  1244. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1245. };
  1246. static void writer_register_all(void)
  1247. {
  1248. static int initialized;
  1249. if (initialized)
  1250. return;
  1251. initialized = 1;
  1252. writer_register(&default_writer);
  1253. writer_register(&compact_writer);
  1254. writer_register(&csv_writer);
  1255. writer_register(&flat_writer);
  1256. writer_register(&ini_writer);
  1257. writer_register(&json_writer);
  1258. writer_register(&xml_writer);
  1259. }
  1260. #define print_fmt(k, f, ...) do { \
  1261. av_bprint_clear(&pbuf); \
  1262. av_bprintf(&pbuf, f, __VA_ARGS__); \
  1263. writer_print_string(w, k, pbuf.str, 0); \
  1264. } while (0)
  1265. #define print_int(k, v) writer_print_integer(w, k, v)
  1266. #define print_str(k, v) writer_print_string(w, k, v, 0)
  1267. #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
  1268. #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
  1269. #define print_ts(k, v) writer_print_ts(w, k, v)
  1270. #define print_val(k, v, u) writer_print_string(w, k, \
  1271. value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
  1272. #define print_section_header(s) writer_print_section_header(w, s)
  1273. #define print_section_footer(s) writer_print_section_footer(w, s)
  1274. #define show_tags(metadata) writer_show_tags(w, metadata)
  1275. static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
  1276. {
  1277. char val_str[128];
  1278. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  1279. AVBPrint pbuf;
  1280. const char *s;
  1281. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1282. print_section_header("packet");
  1283. s = av_get_media_type_string(st->codec->codec_type);
  1284. if (s) print_str ("codec_type", s);
  1285. else print_str_opt("codec_type", "unknown");
  1286. print_int("stream_index", pkt->stream_index);
  1287. print_ts ("pts", pkt->pts);
  1288. print_time("pts_time", pkt->pts, &st->time_base);
  1289. print_ts ("dts", pkt->dts);
  1290. print_time("dts_time", pkt->dts, &st->time_base);
  1291. print_ts ("duration", pkt->duration);
  1292. print_time("duration_time", pkt->duration, &st->time_base);
  1293. print_val("size", pkt->size, unit_byte_str);
  1294. if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
  1295. else print_str_opt("pos", "N/A");
  1296. print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
  1297. print_section_footer("packet");
  1298. av_bprint_finalize(&pbuf, NULL);
  1299. fflush(stdout);
  1300. }
  1301. static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
  1302. {
  1303. AVBPrint pbuf;
  1304. const char *s;
  1305. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1306. print_section_header("frame");
  1307. s = av_get_media_type_string(stream->codec->codec_type);
  1308. if (s) print_str ("media_type", s);
  1309. else print_str_opt("media_type", "unknown");
  1310. print_int("key_frame", frame->key_frame);
  1311. print_ts ("pkt_pts", frame->pkt_pts);
  1312. print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
  1313. print_ts ("pkt_dts", frame->pkt_dts);
  1314. print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
  1315. if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
  1316. else print_str_opt("pkt_pos", "N/A");
  1317. switch (stream->codec->codec_type) {
  1318. case AVMEDIA_TYPE_VIDEO:
  1319. print_int("width", frame->width);
  1320. print_int("height", frame->height);
  1321. s = av_get_pix_fmt_name(frame->format);
  1322. if (s) print_str ("pix_fmt", s);
  1323. else print_str_opt("pix_fmt", "unknown");
  1324. if (frame->sample_aspect_ratio.num) {
  1325. print_fmt("sample_aspect_ratio", "%d:%d",
  1326. frame->sample_aspect_ratio.num,
  1327. frame->sample_aspect_ratio.den);
  1328. } else {
  1329. print_str_opt("sample_aspect_ratio", "N/A");
  1330. }
  1331. print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
  1332. print_int("coded_picture_number", frame->coded_picture_number);
  1333. print_int("display_picture_number", frame->display_picture_number);
  1334. print_int("interlaced_frame", frame->interlaced_frame);
  1335. print_int("top_field_first", frame->top_field_first);
  1336. print_int("repeat_pict", frame->repeat_pict);
  1337. print_int("reference", frame->reference);
  1338. break;
  1339. case AVMEDIA_TYPE_AUDIO:
  1340. s = av_get_sample_fmt_name(frame->format);
  1341. if (s) print_str ("sample_fmt", s);
  1342. else print_str_opt("sample_fmt", "unknown");
  1343. print_int("nb_samples", frame->nb_samples);
  1344. break;
  1345. }
  1346. print_section_footer("frame");
  1347. av_bprint_finalize(&pbuf, NULL);
  1348. fflush(stdout);
  1349. }
  1350. static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
  1351. AVFrame *frame, int *got_frame,
  1352. AVPacket *pkt)
  1353. {
  1354. AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
  1355. int ret = 0;
  1356. *got_frame = 0;
  1357. switch (dec_ctx->codec_type) {
  1358. case AVMEDIA_TYPE_VIDEO:
  1359. ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
  1360. break;
  1361. case AVMEDIA_TYPE_AUDIO:
  1362. ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
  1363. break;
  1364. }
  1365. return ret;
  1366. }
  1367. static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
  1368. {
  1369. AVPacket pkt, pkt1;
  1370. AVFrame frame;
  1371. int i = 0, ret, got_frame;
  1372. av_init_packet(&pkt);
  1373. while (!av_read_frame(fmt_ctx, &pkt)) {
  1374. if (do_read_packets) {
  1375. if (do_show_packets)
  1376. show_packet(w, fmt_ctx, &pkt, i++);
  1377. nb_streams_packets[pkt.stream_index]++;
  1378. }
  1379. if (do_read_frames) {
  1380. pkt1 = pkt;
  1381. while (pkt1.size) {
  1382. avcodec_get_frame_defaults(&frame);
  1383. ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
  1384. if (ret < 0 || !got_frame)
  1385. break;
  1386. if (do_show_frames)
  1387. show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
  1388. pkt1.data += ret;
  1389. pkt1.size -= ret;
  1390. nb_streams_frames[pkt.stream_index]++;
  1391. }
  1392. }
  1393. av_free_packet(&pkt);
  1394. }
  1395. av_init_packet(&pkt);
  1396. pkt.data = NULL;
  1397. pkt.size = 0;
  1398. //Flush remaining frames that are cached in the decoder
  1399. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1400. pkt.stream_index = i;
  1401. while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
  1402. if (do_read_frames) {
  1403. if (do_show_frames)
  1404. show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
  1405. nb_streams_frames[pkt.stream_index]++;
  1406. }
  1407. }
  1408. }
  1409. }
  1410. static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
  1411. {
  1412. AVStream *stream = fmt_ctx->streams[stream_idx];
  1413. AVCodecContext *dec_ctx;
  1414. AVCodec *dec;
  1415. char val_str[128];
  1416. const char *s;
  1417. AVRational display_aspect_ratio;
  1418. AVBPrint pbuf;
  1419. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1420. print_section_header("stream");
  1421. print_int("index", stream->index);
  1422. if ((dec_ctx = stream->codec)) {
  1423. const char *profile = NULL;
  1424. if ((dec = dec_ctx->codec)) {
  1425. print_str("codec_name", dec->name);
  1426. print_str("codec_long_name", dec->long_name);
  1427. } else {
  1428. print_str_opt("codec_name", "unknown");
  1429. print_str_opt("codec_long_name", "unknown");
  1430. }
  1431. if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
  1432. print_str("profile", profile);
  1433. else
  1434. print_str_opt("profile", "unknown");
  1435. s = av_get_media_type_string(dec_ctx->codec_type);
  1436. if (s) print_str ("codec_type", s);
  1437. else print_str_opt("codec_type", "unknown");
  1438. print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
  1439. /* print AVI/FourCC tag */
  1440. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  1441. print_str("codec_tag_string", val_str);
  1442. print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
  1443. switch (dec_ctx->codec_type) {
  1444. case AVMEDIA_TYPE_VIDEO:
  1445. print_int("width", dec_ctx->width);
  1446. print_int("height", dec_ctx->height);
  1447. print_int("has_b_frames", dec_ctx->has_b_frames);
  1448. if (dec_ctx->sample_aspect_ratio.num) {
  1449. print_fmt("sample_aspect_ratio", "%d:%d",
  1450. dec_ctx->sample_aspect_ratio.num,
  1451. dec_ctx->sample_aspect_ratio.den);
  1452. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1453. dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
  1454. dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
  1455. 1024*1024);
  1456. print_fmt("display_aspect_ratio", "%d:%d",
  1457. display_aspect_ratio.num,
  1458. display_aspect_ratio.den);
  1459. } else {
  1460. print_str_opt("sample_aspect_ratio", "N/A");
  1461. print_str_opt("display_aspect_ratio", "N/A");
  1462. }
  1463. s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
  1464. if (s) print_str ("pix_fmt", s);
  1465. else print_str_opt("pix_fmt", "unknown");
  1466. print_int("level", dec_ctx->level);
  1467. if (dec_ctx->timecode_frame_start >= 0) {
  1468. char tcbuf[AV_TIMECODE_STR_SIZE];
  1469. av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
  1470. print_str("timecode", tcbuf);
  1471. } else {
  1472. print_str_opt("timecode", "N/A");
  1473. }
  1474. break;
  1475. case AVMEDIA_TYPE_AUDIO:
  1476. s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
  1477. if (s) print_str ("sample_fmt", s);
  1478. else print_str_opt("sample_fmt", "unknown");
  1479. print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
  1480. print_int("channels", dec_ctx->channels);
  1481. print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
  1482. break;
  1483. }
  1484. } else {
  1485. print_str_opt("codec_type", "unknown");
  1486. }
  1487. if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
  1488. const AVOption *opt = NULL;
  1489. while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
  1490. uint8_t *str;
  1491. if (opt->flags) continue;
  1492. if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
  1493. print_str(opt->name, str);
  1494. av_free(str);
  1495. }
  1496. }
  1497. }
  1498. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  1499. else print_str_opt("id", "N/A");
  1500. print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
  1501. print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
  1502. print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
  1503. print_time("start_time", stream->start_time, &stream->time_base);
  1504. print_time("duration", stream->duration, &stream->time_base);
  1505. if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
  1506. else print_str_opt("bit_rate", "N/A");
  1507. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  1508. else print_str_opt("nb_frames", "N/A");
  1509. if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
  1510. else print_str_opt("nb_read_frames", "N/A");
  1511. if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
  1512. else print_str_opt("nb_read_packets", "N/A");
  1513. show_tags(stream->metadata);
  1514. print_section_footer("stream");
  1515. av_bprint_finalize(&pbuf, NULL);
  1516. fflush(stdout);
  1517. }
  1518. static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
  1519. {
  1520. int i;
  1521. for (i = 0; i < fmt_ctx->nb_streams; i++)
  1522. show_stream(w, fmt_ctx, i);
  1523. }
  1524. static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
  1525. {
  1526. char val_str[128];
  1527. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  1528. print_section_header("format");
  1529. print_str("filename", fmt_ctx->filename);
  1530. print_int("nb_streams", fmt_ctx->nb_streams);
  1531. print_str("format_name", fmt_ctx->iformat->name);
  1532. print_str("format_long_name", fmt_ctx->iformat->long_name);
  1533. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  1534. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  1535. if (size >= 0) print_val ("size", size, unit_byte_str);
  1536. else print_str_opt("size", "N/A");
  1537. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  1538. else print_str_opt("bit_rate", "N/A");
  1539. show_tags(fmt_ctx->metadata);
  1540. print_section_footer("format");
  1541. fflush(stdout);
  1542. }
  1543. static void show_error(WriterContext *w, int err)
  1544. {
  1545. char errbuf[128];
  1546. const char *errbuf_ptr = errbuf;
  1547. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  1548. errbuf_ptr = strerror(AVUNERROR(err));
  1549. writer_print_chapter_header(w, "error");
  1550. print_section_header("error");
  1551. print_int("code", err);
  1552. print_str("string", errbuf_ptr);
  1553. print_section_footer("error");
  1554. writer_print_chapter_footer(w, "error");
  1555. }
  1556. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  1557. {
  1558. int err, i;
  1559. AVFormatContext *fmt_ctx = NULL;
  1560. AVDictionaryEntry *t;
  1561. if ((err = avformat_open_input(&fmt_ctx, filename,
  1562. iformat, &format_opts)) < 0) {
  1563. print_error(filename, err);
  1564. return err;
  1565. }
  1566. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  1567. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  1568. return AVERROR_OPTION_NOT_FOUND;
  1569. }
  1570. /* fill the streams in the format context */
  1571. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  1572. print_error(filename, err);
  1573. return err;
  1574. }
  1575. av_dump_format(fmt_ctx, 0, filename, 0);
  1576. /* bind a decoder to each input stream */
  1577. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1578. AVStream *stream = fmt_ctx->streams[i];
  1579. AVCodec *codec;
  1580. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  1581. av_log(NULL, AV_LOG_ERROR,
  1582. "Unsupported codec with id %d for input stream %d\n",
  1583. stream->codec->codec_id, stream->index);
  1584. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  1585. av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
  1586. stream->index);
  1587. }
  1588. }
  1589. *fmt_ctx_ptr = fmt_ctx;
  1590. return 0;
  1591. }
  1592. static void close_input_file(AVFormatContext **ctx_ptr)
  1593. {
  1594. int i;
  1595. AVFormatContext *fmt_ctx = *ctx_ptr;
  1596. /* close decoder for each stream */
  1597. for (i = 0; i < fmt_ctx->nb_streams; i++)
  1598. if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
  1599. avcodec_close(fmt_ctx->streams[i]->codec);
  1600. avformat_close_input(ctx_ptr);
  1601. }
  1602. #define PRINT_CHAPTER(name) do { \
  1603. if (do_show_ ## name) { \
  1604. writer_print_chapter_header(wctx, #name); \
  1605. show_ ## name (wctx, fmt_ctx); \
  1606. writer_print_chapter_footer(wctx, #name); \
  1607. } \
  1608. } while (0)
  1609. static int probe_file(WriterContext *wctx, const char *filename)
  1610. {
  1611. AVFormatContext *fmt_ctx;
  1612. int ret;
  1613. do_read_frames = do_show_frames || do_count_frames;
  1614. do_read_packets = do_show_packets || do_count_packets;
  1615. ret = open_input_file(&fmt_ctx, filename);
  1616. if (ret >= 0) {
  1617. nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
  1618. nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
  1619. if (do_read_frames || do_read_packets) {
  1620. const char *chapter;
  1621. if (do_show_frames && do_show_packets &&
  1622. wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
  1623. chapter = "packets_and_frames";
  1624. else if (do_show_packets && !do_show_frames)
  1625. chapter = "packets";
  1626. else // (!do_show_packets && do_show_frames)
  1627. chapter = "frames";
  1628. if (do_show_frames || do_show_packets)
  1629. writer_print_chapter_header(wctx, chapter);
  1630. read_packets(wctx, fmt_ctx);
  1631. if (do_show_frames || do_show_packets)
  1632. writer_print_chapter_footer(wctx, chapter);
  1633. }
  1634. PRINT_CHAPTER(streams);
  1635. PRINT_CHAPTER(format);
  1636. close_input_file(&fmt_ctx);
  1637. av_freep(&nb_streams_frames);
  1638. av_freep(&nb_streams_packets);
  1639. }
  1640. return ret;
  1641. }
  1642. static void show_usage(void)
  1643. {
  1644. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  1645. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  1646. av_log(NULL, AV_LOG_INFO, "\n");
  1647. }
  1648. static void ffprobe_show_program_version(WriterContext *w)
  1649. {
  1650. AVBPrint pbuf;
  1651. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1652. writer_print_chapter_header(w, "program_version");
  1653. print_section_header("program_version");
  1654. print_str("version", FFMPEG_VERSION);
  1655. print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
  1656. program_birth_year, this_year);
  1657. print_str("build_date", __DATE__);
  1658. print_str("build_time", __TIME__);
  1659. print_str("compiler_type", CC_TYPE);
  1660. print_str("compiler_version", CC_VERSION);
  1661. print_str("configuration", FFMPEG_CONFIGURATION);
  1662. print_section_footer("program_version");
  1663. writer_print_chapter_footer(w, "program_version");
  1664. av_bprint_finalize(&pbuf, NULL);
  1665. }
  1666. #define SHOW_LIB_VERSION(libname, LIBNAME) \
  1667. do { \
  1668. if (CONFIG_##LIBNAME) { \
  1669. unsigned int version = libname##_version(); \
  1670. print_section_header("library_version"); \
  1671. print_str("name", "lib" #libname); \
  1672. print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
  1673. print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
  1674. print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
  1675. print_int("version", version); \
  1676. print_section_footer("library_version"); \
  1677. } \
  1678. } while (0)
  1679. static void ffprobe_show_library_versions(WriterContext *w)
  1680. {
  1681. writer_print_chapter_header(w, "library_versions");
  1682. SHOW_LIB_VERSION(avutil, AVUTIL);
  1683. SHOW_LIB_VERSION(avcodec, AVCODEC);
  1684. SHOW_LIB_VERSION(avformat, AVFORMAT);
  1685. SHOW_LIB_VERSION(avdevice, AVDEVICE);
  1686. SHOW_LIB_VERSION(avfilter, AVFILTER);
  1687. SHOW_LIB_VERSION(swscale, SWSCALE);
  1688. SHOW_LIB_VERSION(swresample, SWRESAMPLE);
  1689. SHOW_LIB_VERSION(postproc, POSTPROC);
  1690. writer_print_chapter_footer(w, "library_versions");
  1691. }
  1692. static int opt_format(const char *opt, const char *arg)
  1693. {
  1694. iformat = av_find_input_format(arg);
  1695. if (!iformat) {
  1696. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  1697. return AVERROR(EINVAL);
  1698. }
  1699. return 0;
  1700. }
  1701. static int opt_show_format_entry(const char *opt, const char *arg)
  1702. {
  1703. do_show_format = 1;
  1704. av_dict_set(&fmt_entries_to_show, arg, "", 0);
  1705. return 0;
  1706. }
  1707. static void opt_input_file(void *optctx, const char *arg)
  1708. {
  1709. if (input_filename) {
  1710. av_log(NULL, AV_LOG_ERROR,
  1711. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  1712. arg, input_filename);
  1713. exit(1);
  1714. }
  1715. if (!strcmp(arg, "-"))
  1716. arg = "pipe:";
  1717. input_filename = arg;
  1718. }
  1719. static int opt_help(const char *opt, const char *arg)
  1720. {
  1721. av_log_set_callback(log_callback_help);
  1722. show_usage();
  1723. show_help_options(options, "Main options:\n", 0, 0);
  1724. printf("\n");
  1725. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  1726. return 0;
  1727. }
  1728. static int opt_pretty(const char *opt, const char *arg)
  1729. {
  1730. show_value_unit = 1;
  1731. use_value_prefix = 1;
  1732. use_byte_value_binary_prefix = 1;
  1733. use_value_sexagesimal_format = 1;
  1734. return 0;
  1735. }
  1736. static int opt_show_versions(const char *opt, const char *arg)
  1737. {
  1738. do_show_program_version = 1;
  1739. do_show_library_versions = 1;
  1740. return 0;
  1741. }
  1742. static const OptionDef options[] = {
  1743. #include "cmdutils_common_opts.h"
  1744. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  1745. { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
  1746. { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
  1747. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  1748. "use binary prefixes for byte units" },
  1749. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  1750. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  1751. { "pretty", 0, {(void*)&opt_pretty},
  1752. "prettify the format of displayed values, make it more human readable" },
  1753. { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
  1754. "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
  1755. { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
  1756. { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
  1757. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  1758. { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
  1759. { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
  1760. "show a particular entry from the format/container info", "entry" },
  1761. { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
  1762. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  1763. { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
  1764. { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
  1765. { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
  1766. { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
  1767. { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
  1768. { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
  1769. { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
  1770. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
  1771. { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
  1772. { NULL, },
  1773. };
  1774. int main(int argc, char **argv)
  1775. {
  1776. const Writer *w;
  1777. WriterContext *wctx;
  1778. char *buf;
  1779. char *w_name = NULL, *w_args = NULL;
  1780. int ret;
  1781. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  1782. parse_loglevel(argc, argv, options);
  1783. av_register_all();
  1784. avformat_network_init();
  1785. init_opts();
  1786. #if CONFIG_AVDEVICE
  1787. avdevice_register_all();
  1788. #endif
  1789. show_banner(argc, argv, options);
  1790. parse_options(NULL, argc, argv, options, opt_input_file);
  1791. writer_register_all();
  1792. if (!print_format)
  1793. print_format = av_strdup("default");
  1794. w_name = av_strtok(print_format, "=", &buf);
  1795. w_args = buf;
  1796. w = writer_get_by_name(w_name);
  1797. if (!w) {
  1798. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  1799. ret = AVERROR(EINVAL);
  1800. goto end;
  1801. }
  1802. if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
  1803. writer_print_header(wctx);
  1804. if (do_show_program_version)
  1805. ffprobe_show_program_version(wctx);
  1806. if (do_show_library_versions)
  1807. ffprobe_show_library_versions(wctx);
  1808. if (!input_filename &&
  1809. ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
  1810. (!do_show_program_version && !do_show_library_versions))) {
  1811. show_usage();
  1812. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  1813. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  1814. ret = AVERROR(EINVAL);
  1815. } else if (input_filename) {
  1816. ret = probe_file(wctx, input_filename);
  1817. if (ret < 0 && do_show_error)
  1818. show_error(wctx, ret);
  1819. }
  1820. writer_print_footer(wctx);
  1821. writer_close(&wctx);
  1822. }
  1823. end:
  1824. av_freep(&print_format);
  1825. uninit_opts();
  1826. av_dict_free(&fmt_entries_to_show);
  1827. avformat_network_deinit();
  1828. return ret;
  1829. }