avprobe.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * avprobe : Simple Media Prober based on the Libav libraries
  3. * Copyright (c) 2007-2010 Stefano Sabatini
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include "libavformat/avformat.h"
  23. #include "libavcodec/avcodec.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/dict.h"
  28. #include "libavutil/libm.h"
  29. #include "libavdevice/avdevice.h"
  30. #include "cmdutils.h"
  31. const char program_name[] = "avprobe";
  32. const int program_birth_year = 2007;
  33. static int do_show_format = 0;
  34. static AVDictionary *fmt_entries_to_show = NULL;
  35. static int nb_fmt_entries_to_show;
  36. static int do_show_packets = 0;
  37. static int do_show_streams = 0;
  38. static int show_value_unit = 0;
  39. static int use_value_prefix = 0;
  40. static int use_byte_value_binary_prefix = 0;
  41. static int use_value_sexagesimal_format = 0;
  42. /* globals */
  43. static const OptionDef *options;
  44. /* AVprobe context */
  45. static const char *input_filename;
  46. static AVInputFormat *iformat = NULL;
  47. static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
  48. static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
  49. static const char unit_second_str[] = "s" ;
  50. static const char unit_hertz_str[] = "Hz" ;
  51. static const char unit_byte_str[] = "byte" ;
  52. static const char unit_bit_per_second_str[] = "bit/s";
  53. static void exit_program(void)
  54. {
  55. av_dict_free(&fmt_entries_to_show);
  56. }
  57. /*
  58. * The output is structured in array and objects that might contain items
  59. * Array could require the objects within to not be named.
  60. * Object could require the items within to be named.
  61. *
  62. * For flat representation the name of each section is saved on prefix so it
  63. * can be rendered in order to represent nested structures (e.g. array of
  64. * objects for the packets list).
  65. *
  66. * Within an array each element can need an unique identifier or an index.
  67. *
  68. * Nesting level is accounted separately.
  69. */
  70. typedef enum {
  71. ARRAY,
  72. OBJECT
  73. } ProbeElementType;
  74. typedef struct {
  75. const char *name;
  76. ProbeElementType type;
  77. int64_t index;
  78. int64_t nb_elems;
  79. } ProbeElement;
  80. typedef struct {
  81. ProbeElement *prefix;
  82. int level;
  83. void (*print_header)(void);
  84. void (*print_footer)(void);
  85. void (*print_array_header) (const char *name);
  86. void (*print_array_footer) (const char *name);
  87. void (*print_object_header)(const char *name);
  88. void (*print_object_footer)(const char *name);
  89. void (*print_integer) (const char *key, int64_t value);
  90. void (*print_string) (const char *key, const char *value);
  91. } OutputContext;
  92. static AVIOContext *probe_out = NULL;
  93. static OutputContext octx;
  94. #define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
  95. /*
  96. * Default format, INI
  97. *
  98. * - all key and values are utf8
  99. * - '.' is the subgroup separator
  100. * - newlines and the following characters are escaped
  101. * - '\' is the escape character
  102. * - '#' is the comment
  103. * - '=' is the key/value separators
  104. * - ':' is not used but usually parsed as key/value separator
  105. */
  106. static void ini_print_header(void)
  107. {
  108. avio_printf(probe_out, "# avprobe output\n\n");
  109. }
  110. static void ini_print_footer(void)
  111. {
  112. avio_w8(probe_out, '\n');
  113. }
  114. static void ini_escape_print(const char *s)
  115. {
  116. int i = 0;
  117. char c = 0;
  118. while (c = s[i++]) {
  119. switch (c) {
  120. case '\r': avio_printf(probe_out, "%s", "\\r"); break;
  121. case '\n': avio_printf(probe_out, "%s", "\\n"); break;
  122. case '\f': avio_printf(probe_out, "%s", "\\f"); break;
  123. case '\b': avio_printf(probe_out, "%s", "\\b"); break;
  124. case '\t': avio_printf(probe_out, "%s", "\\t"); break;
  125. case '\\':
  126. case '#' :
  127. case '=' :
  128. case ':' : avio_w8(probe_out, '\\');
  129. default:
  130. if ((unsigned char)c < 32)
  131. avio_printf(probe_out, "\\x00%02x", c & 0xff);
  132. else
  133. avio_w8(probe_out, c);
  134. break;
  135. }
  136. }
  137. }
  138. static void ini_print_array_header(const char *name)
  139. {
  140. if (octx.prefix[octx.level -1].nb_elems)
  141. avio_printf(probe_out, "\n");
  142. }
  143. static void ini_print_object_header(const char *name)
  144. {
  145. int i;
  146. ProbeElement *el = octx.prefix + octx.level -1;
  147. if (el->nb_elems)
  148. avio_printf(probe_out, "\n");
  149. avio_printf(probe_out, "[");
  150. for (i = 1; i < octx.level; i++) {
  151. el = octx.prefix + i;
  152. avio_printf(probe_out, "%s.", el->name);
  153. if (el->index >= 0)
  154. avio_printf(probe_out, "%"PRId64".", el->index);
  155. }
  156. avio_printf(probe_out, "%s", name);
  157. if (el && el->type == ARRAY)
  158. avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
  159. avio_printf(probe_out, "]\n");
  160. }
  161. static void ini_print_integer(const char *key, int64_t value)
  162. {
  163. ini_escape_print(key);
  164. avio_printf(probe_out, "=%"PRId64"\n", value);
  165. }
  166. static void ini_print_string(const char *key, const char *value)
  167. {
  168. ini_escape_print(key);
  169. avio_printf(probe_out, "=");
  170. ini_escape_print(value);
  171. avio_w8(probe_out, '\n');
  172. }
  173. /*
  174. * Alternate format, JSON
  175. */
  176. static void json_print_header(void)
  177. {
  178. avio_printf(probe_out, "{");
  179. }
  180. static void json_print_footer(void)
  181. {
  182. avio_printf(probe_out, "}\n");
  183. }
  184. static void json_print_array_header(const char *name)
  185. {
  186. if (octx.prefix[octx.level -1].nb_elems)
  187. avio_printf(probe_out, ",\n");
  188. AVP_INDENT();
  189. avio_printf(probe_out, "\"%s\" : ", name);
  190. avio_printf(probe_out, "[\n");
  191. }
  192. static void json_print_array_footer(const char *name)
  193. {
  194. avio_printf(probe_out, "\n");
  195. AVP_INDENT();
  196. avio_printf(probe_out, "]");
  197. }
  198. static void json_print_object_header(const char *name)
  199. {
  200. if (octx.prefix[octx.level -1].nb_elems)
  201. avio_printf(probe_out, ",\n");
  202. AVP_INDENT();
  203. if (octx.prefix[octx.level -1].type == OBJECT)
  204. avio_printf(probe_out, "\"%s\" : ", name);
  205. avio_printf(probe_out, "{\n");
  206. }
  207. static void json_print_object_footer(const char *name)
  208. {
  209. avio_printf(probe_out, "\n");
  210. AVP_INDENT();
  211. avio_printf(probe_out, "}");
  212. }
  213. static void json_print_integer(const char *key, int64_t value)
  214. {
  215. if (octx.prefix[octx.level -1].nb_elems)
  216. avio_printf(probe_out, ",\n");
  217. AVP_INDENT();
  218. avio_printf(probe_out, "\"%s\" : %"PRId64"", key, value);
  219. }
  220. static void json_escape_print(const char *s)
  221. {
  222. int i = 0;
  223. char c = 0;
  224. while (c = s[i++]) {
  225. switch (c) {
  226. case '\r': avio_printf(probe_out, "%s", "\\r"); break;
  227. case '\n': avio_printf(probe_out, "%s", "\\n"); break;
  228. case '\f': avio_printf(probe_out, "%s", "\\f"); break;
  229. case '\b': avio_printf(probe_out, "%s", "\\b"); break;
  230. case '\t': avio_printf(probe_out, "%s", "\\t"); break;
  231. case '\\':
  232. case '"' : avio_w8(probe_out, '\\');
  233. default:
  234. if ((unsigned char)c < 32)
  235. avio_printf(probe_out, "\\u00%02x", c & 0xff);
  236. else
  237. avio_w8(probe_out, c);
  238. break;
  239. }
  240. }
  241. }
  242. static void json_print_string(const char *key, const char *value)
  243. {
  244. if (octx.prefix[octx.level -1].nb_elems)
  245. avio_printf(probe_out, ",\n");
  246. AVP_INDENT();
  247. avio_w8(probe_out, '\"');
  248. json_escape_print(key);
  249. avio_printf(probe_out, "\" : \"");
  250. json_escape_print(value);
  251. avio_w8(probe_out, '\"');
  252. }
  253. /*
  254. * old-style pseudo-INI
  255. */
  256. static void old_print_object_header(const char *name)
  257. {
  258. char *str, *p;
  259. if (!strcmp(name, "tags"))
  260. return;
  261. str = p = av_strdup(name);
  262. while (*p) {
  263. *p = av_toupper(*p);
  264. p++;
  265. }
  266. avio_printf(probe_out, "[%s]\n", str);
  267. av_freep(&str);
  268. }
  269. static void old_print_object_footer(const char *name)
  270. {
  271. char *str, *p;
  272. if (!strcmp(name, "tags"))
  273. return;
  274. str = p = av_strdup(name);
  275. while (*p) {
  276. *p = av_toupper(*p);
  277. p++;
  278. }
  279. avio_printf(probe_out, "[/%s]\n", str);
  280. av_freep(&str);
  281. }
  282. static void old_print_string(const char *key, const char *value)
  283. {
  284. if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
  285. avio_printf(probe_out, "TAG:");
  286. ini_print_string(key, value);
  287. }
  288. /*
  289. * Simple Formatter for single entries.
  290. */
  291. static void show_format_entry_integer(const char *key, int64_t value)
  292. {
  293. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  294. if (nb_fmt_entries_to_show > 1)
  295. avio_printf(probe_out, "%s=", key);
  296. avio_printf(probe_out, "%"PRId64"\n", value);
  297. }
  298. }
  299. static void show_format_entry_string(const char *key, const char *value)
  300. {
  301. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  302. if (nb_fmt_entries_to_show > 1)
  303. avio_printf(probe_out, "%s=", key);
  304. avio_printf(probe_out, "%s\n", value);
  305. }
  306. }
  307. static void probe_group_enter(const char *name, int type)
  308. {
  309. int64_t count = -1;
  310. octx.prefix =
  311. av_realloc(octx.prefix, sizeof(ProbeElement) * (octx.level + 1));
  312. if (!octx.prefix || !name) {
  313. fprintf(stderr, "Out of memory\n");
  314. exit(1);
  315. }
  316. if (octx.level) {
  317. ProbeElement *parent = octx.prefix + octx.level -1;
  318. if (parent->type == ARRAY)
  319. count = parent->nb_elems;
  320. parent->nb_elems++;
  321. }
  322. octx.prefix[octx.level++] = (ProbeElement){name, type, count, 0};
  323. }
  324. static void probe_group_leave(void)
  325. {
  326. --octx.level;
  327. }
  328. static void probe_header(void)
  329. {
  330. if (octx.print_header)
  331. octx.print_header();
  332. probe_group_enter("root", OBJECT);
  333. }
  334. static void probe_footer(void)
  335. {
  336. if (octx.print_footer)
  337. octx.print_footer();
  338. probe_group_leave();
  339. }
  340. static void probe_array_header(const char *name)
  341. {
  342. if (octx.print_array_header)
  343. octx.print_array_header(name);
  344. probe_group_enter(name, ARRAY);
  345. }
  346. static void probe_array_footer(const char *name)
  347. {
  348. probe_group_leave();
  349. if (octx.print_array_footer)
  350. octx.print_array_footer(name);
  351. }
  352. static void probe_object_header(const char *name)
  353. {
  354. if (octx.print_object_header)
  355. octx.print_object_header(name);
  356. probe_group_enter(name, OBJECT);
  357. }
  358. static void probe_object_footer(const char *name)
  359. {
  360. probe_group_leave();
  361. if (octx.print_object_footer)
  362. octx.print_object_footer(name);
  363. }
  364. static void probe_int(const char *key, int64_t value)
  365. {
  366. octx.print_integer(key, value);
  367. octx.prefix[octx.level -1].nb_elems++;
  368. }
  369. static void probe_str(const char *key, const char *value)
  370. {
  371. octx.print_string(key, value);
  372. octx.prefix[octx.level -1].nb_elems++;
  373. }
  374. static void probe_dict(AVDictionary *dict, const char *name)
  375. {
  376. AVDictionaryEntry *entry = NULL;
  377. if (!dict)
  378. return;
  379. probe_object_header(name);
  380. while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
  381. probe_str(entry->key, entry->value);
  382. }
  383. probe_object_footer(name);
  384. }
  385. static char *value_string(char *buf, int buf_size, double val, const char *unit)
  386. {
  387. if (unit == unit_second_str && use_value_sexagesimal_format) {
  388. double secs;
  389. int hours, mins;
  390. secs = val;
  391. mins = (int)secs / 60;
  392. secs = secs - mins * 60;
  393. hours = mins / 60;
  394. mins %= 60;
  395. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  396. } else if (use_value_prefix) {
  397. const char *prefix_string;
  398. int index;
  399. if (unit == unit_byte_str && use_byte_value_binary_prefix) {
  400. index = (int) log2(val) / 10;
  401. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
  402. val /= pow(2, index * 10);
  403. prefix_string = binary_unit_prefixes[index];
  404. } else {
  405. index = (int) (log10(val)) / 3;
  406. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
  407. val /= pow(10, index * 3);
  408. prefix_string = decimal_unit_prefixes[index];
  409. }
  410. snprintf(buf, buf_size, "%.*f%s%s",
  411. index ? 3 : 0, val,
  412. prefix_string,
  413. show_value_unit ? unit : "");
  414. } else {
  415. snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
  416. }
  417. return buf;
  418. }
  419. static char *time_value_string(char *buf, int buf_size, int64_t val,
  420. const AVRational *time_base)
  421. {
  422. if (val == AV_NOPTS_VALUE) {
  423. snprintf(buf, buf_size, "N/A");
  424. } else {
  425. value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
  426. }
  427. return buf;
  428. }
  429. static char *ts_value_string(char *buf, int buf_size, int64_t ts)
  430. {
  431. if (ts == AV_NOPTS_VALUE) {
  432. snprintf(buf, buf_size, "N/A");
  433. } else {
  434. snprintf(buf, buf_size, "%"PRId64, ts);
  435. }
  436. return buf;
  437. }
  438. static char *rational_string(char *buf, int buf_size, const char *sep,
  439. const AVRational *rat)
  440. {
  441. snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
  442. return buf;
  443. }
  444. static char *tag_string(char *buf, int buf_size, int tag)
  445. {
  446. snprintf(buf, buf_size, "0x%04x", tag);
  447. return buf;
  448. }
  449. static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
  450. {
  451. char val_str[128];
  452. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  453. probe_object_header("packet");
  454. probe_str("codec_type", media_type_string(st->codec->codec_type));
  455. probe_int("stream_index", pkt->stream_index);
  456. probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
  457. probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
  458. pkt->pts, &st->time_base));
  459. probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
  460. probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
  461. pkt->dts, &st->time_base));
  462. probe_str("duration", ts_value_string(val_str, sizeof(val_str),
  463. pkt->duration));
  464. probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
  465. pkt->duration,
  466. &st->time_base));
  467. probe_str("size", value_string(val_str, sizeof(val_str),
  468. pkt->size, unit_byte_str));
  469. probe_int("pos", pkt->pos);
  470. probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
  471. probe_object_footer("packet");
  472. }
  473. static void show_packets(AVFormatContext *fmt_ctx)
  474. {
  475. AVPacket pkt;
  476. av_init_packet(&pkt);
  477. probe_array_header("packets");
  478. while (!av_read_frame(fmt_ctx, &pkt))
  479. show_packet(fmt_ctx, &pkt);
  480. probe_array_footer("packets");
  481. }
  482. static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
  483. {
  484. AVStream *stream = fmt_ctx->streams[stream_idx];
  485. AVCodecContext *dec_ctx;
  486. const AVCodec *dec;
  487. const char *profile;
  488. char val_str[128];
  489. AVRational display_aspect_ratio, *sar = NULL;
  490. const AVPixFmtDescriptor *desc;
  491. probe_object_header("stream");
  492. probe_int("index", stream->index);
  493. if ((dec_ctx = stream->codec)) {
  494. if ((dec = dec_ctx->codec)) {
  495. probe_str("codec_name", dec->name);
  496. probe_str("codec_long_name", dec->long_name);
  497. } else {
  498. probe_str("codec_name", "unknown");
  499. }
  500. probe_str("codec_type", media_type_string(dec_ctx->codec_type));
  501. probe_str("codec_time_base",
  502. rational_string(val_str, sizeof(val_str),
  503. "/", &dec_ctx->time_base));
  504. /* print AVI/FourCC tag */
  505. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  506. probe_str("codec_tag_string", val_str);
  507. probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
  508. dec_ctx->codec_tag));
  509. /* print profile, if there is one */
  510. if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
  511. probe_str("profile", profile);
  512. switch (dec_ctx->codec_type) {
  513. case AVMEDIA_TYPE_VIDEO:
  514. probe_int("width", dec_ctx->width);
  515. probe_int("height", dec_ctx->height);
  516. probe_int("has_b_frames", dec_ctx->has_b_frames);
  517. if (dec_ctx->sample_aspect_ratio.num)
  518. sar = &dec_ctx->sample_aspect_ratio;
  519. else if (stream->sample_aspect_ratio.num)
  520. sar = &stream->sample_aspect_ratio;
  521. if (sar) {
  522. probe_str("sample_aspect_ratio",
  523. rational_string(val_str, sizeof(val_str), ":", sar));
  524. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  525. dec_ctx->width * sar->num, dec_ctx->height * sar->den,
  526. 1024*1024);
  527. probe_str("display_aspect_ratio",
  528. rational_string(val_str, sizeof(val_str), ":",
  529. &display_aspect_ratio));
  530. }
  531. desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
  532. probe_str("pix_fmt", desc ? desc->name : "unknown");
  533. probe_int("level", dec_ctx->level);
  534. break;
  535. case AVMEDIA_TYPE_AUDIO:
  536. probe_str("sample_rate",
  537. value_string(val_str, sizeof(val_str),
  538. dec_ctx->sample_rate,
  539. unit_hertz_str));
  540. probe_int("channels", dec_ctx->channels);
  541. probe_int("bits_per_sample",
  542. av_get_bits_per_sample(dec_ctx->codec_id));
  543. break;
  544. }
  545. } else {
  546. probe_str("codec_type", "unknown");
  547. }
  548. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  549. probe_int("id", stream->id);
  550. probe_str("avg_frame_rate",
  551. rational_string(val_str, sizeof(val_str), "/",
  552. &stream->avg_frame_rate));
  553. if (dec_ctx->bit_rate)
  554. probe_str("bit_rate",
  555. value_string(val_str, sizeof(val_str),
  556. dec_ctx->bit_rate, unit_bit_per_second_str));
  557. probe_str("time_base",
  558. rational_string(val_str, sizeof(val_str), "/",
  559. &stream->time_base));
  560. probe_str("start_time",
  561. time_value_string(val_str, sizeof(val_str),
  562. stream->start_time, &stream->time_base));
  563. probe_str("duration",
  564. time_value_string(val_str, sizeof(val_str),
  565. stream->duration, &stream->time_base));
  566. if (stream->nb_frames)
  567. probe_int("nb_frames", stream->nb_frames);
  568. probe_dict(stream->metadata, "tags");
  569. probe_object_footer("stream");
  570. }
  571. static void show_format(AVFormatContext *fmt_ctx)
  572. {
  573. char val_str[128];
  574. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  575. probe_object_header("format");
  576. probe_str("filename", fmt_ctx->filename);
  577. probe_int("nb_streams", fmt_ctx->nb_streams);
  578. probe_str("format_name", fmt_ctx->iformat->name);
  579. probe_str("format_long_name", fmt_ctx->iformat->long_name);
  580. probe_str("start_time",
  581. time_value_string(val_str, sizeof(val_str),
  582. fmt_ctx->start_time, &AV_TIME_BASE_Q));
  583. probe_str("duration",
  584. time_value_string(val_str, sizeof(val_str),
  585. fmt_ctx->duration, &AV_TIME_BASE_Q));
  586. probe_str("size",
  587. size >= 0 ? value_string(val_str, sizeof(val_str),
  588. size, unit_byte_str)
  589. : "unknown");
  590. probe_str("bit_rate",
  591. value_string(val_str, sizeof(val_str),
  592. fmt_ctx->bit_rate, unit_bit_per_second_str));
  593. probe_dict(fmt_ctx->metadata, "tags");
  594. probe_object_footer("format");
  595. }
  596. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  597. {
  598. int err, i;
  599. AVFormatContext *fmt_ctx = NULL;
  600. AVDictionaryEntry *t;
  601. if ((err = avformat_open_input(&fmt_ctx, filename,
  602. iformat, &format_opts)) < 0) {
  603. print_error(filename, err);
  604. return err;
  605. }
  606. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  607. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  608. return AVERROR_OPTION_NOT_FOUND;
  609. }
  610. /* fill the streams in the format context */
  611. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  612. print_error(filename, err);
  613. return err;
  614. }
  615. av_dump_format(fmt_ctx, 0, filename, 0);
  616. /* bind a decoder to each input stream */
  617. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  618. AVStream *stream = fmt_ctx->streams[i];
  619. AVCodec *codec;
  620. if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
  621. fprintf(stderr, "Failed to probe codec for input stream %d\n",
  622. stream->index);
  623. } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  624. fprintf(stderr,
  625. "Unsupported codec with id %d for input stream %d\n",
  626. stream->codec->codec_id, stream->index);
  627. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  628. fprintf(stderr, "Error while opening codec for input stream %d\n",
  629. stream->index);
  630. }
  631. }
  632. *fmt_ctx_ptr = fmt_ctx;
  633. return 0;
  634. }
  635. static void close_input_file(AVFormatContext **ctx_ptr)
  636. {
  637. int i;
  638. AVFormatContext *fmt_ctx = *ctx_ptr;
  639. /* close decoder for each stream */
  640. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  641. AVStream *stream = fmt_ctx->streams[i];
  642. avcodec_close(stream->codec);
  643. }
  644. avformat_close_input(ctx_ptr);
  645. }
  646. static int probe_file(const char *filename)
  647. {
  648. AVFormatContext *fmt_ctx;
  649. int ret, i;
  650. if ((ret = open_input_file(&fmt_ctx, filename)))
  651. return ret;
  652. if (do_show_format)
  653. show_format(fmt_ctx);
  654. if (do_show_streams) {
  655. probe_array_header("streams");
  656. for (i = 0; i < fmt_ctx->nb_streams; i++)
  657. show_stream(fmt_ctx, i);
  658. probe_array_footer("streams");
  659. }
  660. if (do_show_packets)
  661. show_packets(fmt_ctx);
  662. close_input_file(&fmt_ctx);
  663. return 0;
  664. }
  665. static void show_usage(void)
  666. {
  667. printf("Simple multimedia streams analyzer\n");
  668. printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  669. printf("\n");
  670. }
  671. static int opt_format(void *optctx, const char *opt, const char *arg)
  672. {
  673. iformat = av_find_input_format(arg);
  674. if (!iformat) {
  675. fprintf(stderr, "Unknown input format: %s\n", arg);
  676. return AVERROR(EINVAL);
  677. }
  678. return 0;
  679. }
  680. static int opt_output_format(void *optctx, const char *opt, const char *arg)
  681. {
  682. if (!strcmp(arg, "json")) {
  683. octx.print_header = json_print_header;
  684. octx.print_footer = json_print_footer;
  685. octx.print_array_header = json_print_array_header;
  686. octx.print_array_footer = json_print_array_footer;
  687. octx.print_object_header = json_print_object_header;
  688. octx.print_object_footer = json_print_object_footer;
  689. octx.print_integer = json_print_integer;
  690. octx.print_string = json_print_string;
  691. } else if (!strcmp(arg, "ini")) {
  692. octx.print_header = ini_print_header;
  693. octx.print_footer = ini_print_footer;
  694. octx.print_array_header = ini_print_array_header;
  695. octx.print_object_header = ini_print_object_header;
  696. octx.print_integer = ini_print_integer;
  697. octx.print_string = ini_print_string;
  698. } else if (!strcmp(arg, "old")) {
  699. octx.print_header = NULL;
  700. octx.print_object_header = old_print_object_header;
  701. octx.print_object_footer = old_print_object_footer;
  702. octx.print_string = old_print_string;
  703. } else {
  704. av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
  705. return AVERROR(EINVAL);
  706. }
  707. return 0;
  708. }
  709. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  710. {
  711. do_show_format = 1;
  712. nb_fmt_entries_to_show++;
  713. octx.print_header = NULL;
  714. octx.print_footer = NULL;
  715. octx.print_array_header = NULL;
  716. octx.print_array_footer = NULL;
  717. octx.print_object_header = NULL;
  718. octx.print_object_footer = NULL;
  719. octx.print_integer = show_format_entry_integer;
  720. octx.print_string = show_format_entry_string;
  721. av_dict_set(&fmt_entries_to_show, arg, "", 0);
  722. return 0;
  723. }
  724. static void opt_input_file(void *optctx, const char *arg)
  725. {
  726. if (input_filename) {
  727. fprintf(stderr,
  728. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  729. arg, input_filename);
  730. exit(1);
  731. }
  732. if (!strcmp(arg, "-"))
  733. arg = "pipe:";
  734. input_filename = arg;
  735. }
  736. void show_help_default(const char *opt, const char *arg)
  737. {
  738. av_log_set_callback(log_callback_help);
  739. show_usage();
  740. show_help_options(options, "Main options:", 0, 0, 0);
  741. printf("\n");
  742. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  743. }
  744. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  745. {
  746. show_value_unit = 1;
  747. use_value_prefix = 1;
  748. use_byte_value_binary_prefix = 1;
  749. use_value_sexagesimal_format = 1;
  750. return 0;
  751. }
  752. static const OptionDef real_options[] = {
  753. #include "cmdutils_common_opts.h"
  754. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  755. { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
  756. { "unit", OPT_BOOL, {&show_value_unit},
  757. "show unit of the displayed values" },
  758. { "prefix", OPT_BOOL, {&use_value_prefix},
  759. "use SI prefixes for the displayed values" },
  760. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  761. "use binary prefixes for byte units" },
  762. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  763. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  764. { "pretty", 0, {.func_arg = opt_pretty},
  765. "prettify the format of displayed values, make it more human readable" },
  766. { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
  767. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  768. "show a particular entry from the format/container info", "entry" },
  769. { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
  770. { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
  771. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
  772. "generic catch all option", "" },
  773. { NULL, },
  774. };
  775. static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
  776. {
  777. printf("%.*s", buf_size, buf);
  778. return 0;
  779. }
  780. #define AVP_BUFFSIZE 4096
  781. int main(int argc, char **argv)
  782. {
  783. int ret;
  784. uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
  785. if (!buffer)
  786. exit(1);
  787. atexit(exit_program);
  788. options = real_options;
  789. parse_loglevel(argc, argv, options);
  790. av_register_all();
  791. avformat_network_init();
  792. init_opts();
  793. #if CONFIG_AVDEVICE
  794. avdevice_register_all();
  795. #endif
  796. show_banner();
  797. octx.print_header = ini_print_header;
  798. octx.print_footer = ini_print_footer;
  799. octx.print_array_header = ini_print_array_header;
  800. octx.print_object_header = ini_print_object_header;
  801. octx.print_integer = ini_print_integer;
  802. octx.print_string = ini_print_string;
  803. parse_options(NULL, argc, argv, options, opt_input_file);
  804. if (!input_filename) {
  805. show_usage();
  806. fprintf(stderr, "You have to specify one input file.\n");
  807. fprintf(stderr,
  808. "Use -h to get full help or, even better, run 'man %s'.\n",
  809. program_name);
  810. exit(1);
  811. }
  812. probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
  813. probe_buf_write, NULL);
  814. if (!probe_out)
  815. exit(1);
  816. probe_header();
  817. ret = probe_file(input_filename);
  818. probe_footer();
  819. avio_flush(probe_out);
  820. avio_close(probe_out);
  821. avformat_network_deinit();
  822. return ret;
  823. }