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.

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