avprobe.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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/display.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/stereo3d.h"
  29. #include "libavutil/dict.h"
  30. #include "libavutil/libm.h"
  31. #include "libavdevice/avdevice.h"
  32. #include "cmdutils.h"
  33. typedef struct InputStream {
  34. AVStream *st;
  35. AVCodecContext *dec_ctx;
  36. } InputStream;
  37. typedef struct InputFile {
  38. AVFormatContext *fmt_ctx;
  39. InputStream *streams;
  40. int nb_streams;
  41. } InputFile;
  42. const char program_name[] = "avprobe";
  43. const int program_birth_year = 2007;
  44. static int do_show_format = 0;
  45. static AVDictionary *fmt_entries_to_show = NULL;
  46. static int nb_fmt_entries_to_show;
  47. static int do_show_packets = 0;
  48. static int do_show_streams = 0;
  49. static AVDictionary *stream_entries_to_show = NULL;
  50. static int nb_stream_entries_to_show;
  51. /* key used to print when probe_{int,str}(NULL, ..) is invoked */
  52. static const char *header_key;
  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. /* globals */
  58. static const OptionDef *options;
  59. /* avprobe context */
  60. static const char *input_filename;
  61. static AVInputFormat *iformat = NULL;
  62. static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
  63. static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
  64. static const char unit_second_str[] = "s" ;
  65. static const char unit_hertz_str[] = "Hz" ;
  66. static const char unit_byte_str[] = "byte" ;
  67. static const char unit_bit_per_second_str[] = "bit/s";
  68. static void avprobe_cleanup(int ret)
  69. {
  70. av_dict_free(&fmt_entries_to_show);
  71. av_dict_free(&stream_entries_to_show);
  72. }
  73. /*
  74. * The output is structured in array and objects that might contain items
  75. * Array could require the objects within to not be named.
  76. * Object could require the items within to be named.
  77. *
  78. * For flat representation the name of each section is saved on prefix so it
  79. * can be rendered in order to represent nested structures (e.g. array of
  80. * objects for the packets list).
  81. *
  82. * Within an array each element can need an unique identifier or an index.
  83. *
  84. * Nesting level is accounted separately.
  85. */
  86. typedef enum {
  87. ARRAY,
  88. OBJECT
  89. } PrintElementType;
  90. typedef struct PrintElement {
  91. const char *name;
  92. PrintElementType type;
  93. int64_t index;
  94. int64_t nb_elems;
  95. } PrintElement;
  96. typedef struct PrintContext {
  97. PrintElement *prefix;
  98. int level;
  99. void (*print_header)(void);
  100. void (*print_footer)(void);
  101. void (*print_array_header) (const char *name, int plain_values);
  102. void (*print_array_footer) (const char *name, int plain_values);
  103. void (*print_object_header)(const char *name);
  104. void (*print_object_footer)(const char *name);
  105. void (*print_integer) (const char *key, int64_t value);
  106. void (*print_string) (const char *key, const char *value);
  107. } PrintContext;
  108. static AVIOContext *probe_out = NULL;
  109. static PrintContext octx;
  110. #define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
  111. /*
  112. * Default format, INI
  113. *
  114. * - all key and values are utf8
  115. * - '.' is the subgroup separator
  116. * - newlines and the following characters are escaped
  117. * - '\' is the escape character
  118. * - '#' is the comment
  119. * - '=' is the key/value separators
  120. * - ':' is not used but usually parsed as key/value separator
  121. */
  122. static void ini_print_header(void)
  123. {
  124. avio_printf(probe_out, "# avprobe output\n\n");
  125. }
  126. static void ini_print_footer(void)
  127. {
  128. avio_w8(probe_out, '\n');
  129. }
  130. static void ini_escape_print(const char *s)
  131. {
  132. int i = 0;
  133. char c = 0;
  134. while (c = s[i++]) {
  135. switch (c) {
  136. case '\r': avio_printf(probe_out, "%s", "\\r"); break;
  137. case '\n': avio_printf(probe_out, "%s", "\\n"); break;
  138. case '\f': avio_printf(probe_out, "%s", "\\f"); break;
  139. case '\b': avio_printf(probe_out, "%s", "\\b"); break;
  140. case '\t': avio_printf(probe_out, "%s", "\\t"); break;
  141. case '\\':
  142. case '#' :
  143. case '=' :
  144. case ':' : avio_w8(probe_out, '\\');
  145. default:
  146. if ((unsigned char)c < 32)
  147. avio_printf(probe_out, "\\x00%02x", c & 0xff);
  148. else
  149. avio_w8(probe_out, c);
  150. break;
  151. }
  152. }
  153. }
  154. static void ini_print_array_header(const char *name, int plain_values)
  155. {
  156. if (!plain_values) {
  157. /* Add a new line if we create a new full group */
  158. if (octx.prefix[octx.level -1].nb_elems)
  159. avio_printf(probe_out, "\n");
  160. } else {
  161. ini_escape_print(name);
  162. avio_w8(probe_out, '=');
  163. }
  164. }
  165. static void ini_print_array_footer(const char *name, int plain_values)
  166. {
  167. if (plain_values)
  168. avio_printf(probe_out, "\n");
  169. }
  170. static void ini_print_object_header(const char *name)
  171. {
  172. int i;
  173. PrintElement *el = octx.prefix + octx.level -1;
  174. if (el->nb_elems)
  175. avio_printf(probe_out, "\n");
  176. avio_printf(probe_out, "[");
  177. for (i = 1; i < octx.level; i++) {
  178. el = octx.prefix + i;
  179. avio_printf(probe_out, "%s.", el->name);
  180. if (el->index >= 0)
  181. avio_printf(probe_out, "%"PRId64".", el->index);
  182. }
  183. avio_printf(probe_out, "%s", name);
  184. if (el->type == ARRAY)
  185. avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
  186. avio_printf(probe_out, "]\n");
  187. }
  188. static void ini_print_integer(const char *key, int64_t value)
  189. {
  190. if (key) {
  191. ini_escape_print(key);
  192. avio_printf(probe_out, "=%"PRId64"\n", value);
  193. } else {
  194. if (octx.prefix[octx.level -1].nb_elems)
  195. avio_printf(probe_out, ",");
  196. avio_printf(probe_out, "%"PRId64, value);
  197. }
  198. }
  199. static void ini_print_string(const char *key, const char *value)
  200. {
  201. ini_escape_print(key);
  202. avio_printf(probe_out, "=");
  203. ini_escape_print(value);
  204. avio_w8(probe_out, '\n');
  205. }
  206. /*
  207. * Alternate format, JSON
  208. */
  209. static void json_print_header(void)
  210. {
  211. avio_printf(probe_out, "{");
  212. }
  213. static void json_print_footer(void)
  214. {
  215. avio_printf(probe_out, "}\n");
  216. }
  217. static void json_print_array_header(const char *name, int plain_values)
  218. {
  219. if (octx.prefix[octx.level -1].nb_elems)
  220. avio_printf(probe_out, ",\n");
  221. AVP_INDENT();
  222. avio_printf(probe_out, "\"%s\" : ", name);
  223. avio_printf(probe_out, "[\n");
  224. }
  225. static void json_print_array_footer(const char *name, int plain_values)
  226. {
  227. avio_printf(probe_out, "\n");
  228. AVP_INDENT();
  229. avio_printf(probe_out, "]");
  230. }
  231. static void json_print_object_header(const char *name)
  232. {
  233. if (octx.prefix[octx.level -1].nb_elems)
  234. avio_printf(probe_out, ",\n");
  235. AVP_INDENT();
  236. if (octx.prefix[octx.level -1].type == OBJECT)
  237. avio_printf(probe_out, "\"%s\" : ", name);
  238. avio_printf(probe_out, "{\n");
  239. }
  240. static void json_print_object_footer(const char *name)
  241. {
  242. avio_printf(probe_out, "\n");
  243. AVP_INDENT();
  244. avio_printf(probe_out, "}");
  245. }
  246. static void json_print_integer(const char *key, int64_t value)
  247. {
  248. if (key) {
  249. if (octx.prefix[octx.level -1].nb_elems)
  250. avio_printf(probe_out, ",\n");
  251. AVP_INDENT();
  252. avio_printf(probe_out, "\"%s\" : ", key);
  253. } else {
  254. if (octx.prefix[octx.level -1].nb_elems)
  255. avio_printf(probe_out, ", ");
  256. else
  257. AVP_INDENT();
  258. }
  259. avio_printf(probe_out, "%"PRId64, value);
  260. }
  261. static void json_escape_print(const char *s)
  262. {
  263. int i = 0;
  264. char c = 0;
  265. while (c = s[i++]) {
  266. switch (c) {
  267. case '\r': avio_printf(probe_out, "%s", "\\r"); break;
  268. case '\n': avio_printf(probe_out, "%s", "\\n"); break;
  269. case '\f': avio_printf(probe_out, "%s", "\\f"); break;
  270. case '\b': avio_printf(probe_out, "%s", "\\b"); break;
  271. case '\t': avio_printf(probe_out, "%s", "\\t"); break;
  272. case '\\':
  273. case '"' : avio_w8(probe_out, '\\');
  274. default:
  275. if ((unsigned char)c < 32)
  276. avio_printf(probe_out, "\\u00%02x", c & 0xff);
  277. else
  278. avio_w8(probe_out, c);
  279. break;
  280. }
  281. }
  282. }
  283. static void json_print_string(const char *key, const char *value)
  284. {
  285. if (octx.prefix[octx.level -1].nb_elems)
  286. avio_printf(probe_out, ",\n");
  287. AVP_INDENT();
  288. avio_w8(probe_out, '\"');
  289. json_escape_print(key);
  290. avio_printf(probe_out, "\" : \"");
  291. json_escape_print(value);
  292. avio_w8(probe_out, '\"');
  293. }
  294. /*
  295. * old-style pseudo-INI
  296. */
  297. static void old_print_object_header(const char *name)
  298. {
  299. char *str, *p;
  300. if (!strcmp(name, "tags"))
  301. return;
  302. str = p = av_strdup(name);
  303. if (!str)
  304. return;
  305. while (*p) {
  306. *p = av_toupper(*p);
  307. p++;
  308. }
  309. avio_printf(probe_out, "[%s]\n", str);
  310. av_freep(&str);
  311. }
  312. static void old_print_object_footer(const char *name)
  313. {
  314. char *str, *p;
  315. if (!strcmp(name, "tags"))
  316. return;
  317. str = p = av_strdup(name);
  318. if (!str)
  319. return;
  320. while (*p) {
  321. *p = av_toupper(*p);
  322. p++;
  323. }
  324. avio_printf(probe_out, "[/%s]\n", str);
  325. av_freep(&str);
  326. }
  327. static void old_print_string(const char *key, const char *value)
  328. {
  329. if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
  330. avio_printf(probe_out, "TAG:");
  331. ini_print_string(key, value);
  332. }
  333. /*
  334. * Simple Formatter for single entries.
  335. */
  336. static void show_format_entry_integer(const char *key, int64_t value)
  337. {
  338. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  339. if (nb_fmt_entries_to_show > 1)
  340. avio_printf(probe_out, "%s=", key);
  341. avio_printf(probe_out, "%"PRId64"\n", value);
  342. }
  343. }
  344. static void show_format_entry_string(const char *key, const char *value)
  345. {
  346. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  347. if (nb_fmt_entries_to_show > 1)
  348. avio_printf(probe_out, "%s=", key);
  349. avio_printf(probe_out, "%s\n", value);
  350. }
  351. }
  352. static void show_stream_entry_header(const char *key, int value)
  353. {
  354. header_key = key;
  355. }
  356. static void show_stream_entry_footer(const char *key, int value)
  357. {
  358. header_key = NULL;
  359. }
  360. static void show_stream_entry_integer(const char *key, int64_t value)
  361. {
  362. if (!key)
  363. key = header_key;
  364. if (key && av_dict_get(stream_entries_to_show, key, NULL, 0)) {
  365. if (nb_stream_entries_to_show > 1)
  366. avio_printf(probe_out, "%s=", key);
  367. avio_printf(probe_out, "%"PRId64"\n", value);
  368. }
  369. }
  370. static void show_stream_entry_string(const char *key, const char *value)
  371. {
  372. if (key && av_dict_get(stream_entries_to_show, key, NULL, 0)) {
  373. if (nb_stream_entries_to_show > 1)
  374. avio_printf(probe_out, "%s=", key);
  375. avio_printf(probe_out, "%s\n", value);
  376. }
  377. }
  378. static void probe_group_enter(const char *name, int type)
  379. {
  380. int64_t count = -1;
  381. octx.prefix =
  382. av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
  383. if (!octx.prefix || !name) {
  384. fprintf(stderr, "Out of memory\n");
  385. exit_program(1);
  386. }
  387. if (octx.level) {
  388. PrintElement *parent = octx.prefix + octx.level -1;
  389. if (parent->type == ARRAY)
  390. count = parent->nb_elems;
  391. parent->nb_elems++;
  392. }
  393. octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
  394. }
  395. static void probe_group_leave(void)
  396. {
  397. --octx.level;
  398. }
  399. static void probe_header(void)
  400. {
  401. if (octx.print_header)
  402. octx.print_header();
  403. probe_group_enter("root", OBJECT);
  404. }
  405. static void probe_footer(void)
  406. {
  407. if (octx.print_footer)
  408. octx.print_footer();
  409. probe_group_leave();
  410. }
  411. static void probe_array_header(const char *name, int plain_values)
  412. {
  413. if (octx.print_array_header)
  414. octx.print_array_header(name, plain_values);
  415. probe_group_enter(name, ARRAY);
  416. }
  417. static void probe_array_footer(const char *name, int plain_values)
  418. {
  419. probe_group_leave();
  420. if (octx.print_array_footer)
  421. octx.print_array_footer(name, plain_values);
  422. }
  423. static void probe_object_header(const char *name)
  424. {
  425. if (octx.print_object_header)
  426. octx.print_object_header(name);
  427. probe_group_enter(name, OBJECT);
  428. }
  429. static void probe_object_footer(const char *name)
  430. {
  431. probe_group_leave();
  432. if (octx.print_object_footer)
  433. octx.print_object_footer(name);
  434. }
  435. static void probe_int(const char *key, int64_t value)
  436. {
  437. octx.print_integer(key, value);
  438. octx.prefix[octx.level -1].nb_elems++;
  439. }
  440. static void probe_str(const char *key, const char *value)
  441. {
  442. octx.print_string(key, value);
  443. octx.prefix[octx.level -1].nb_elems++;
  444. }
  445. static void probe_dict(AVDictionary *dict, const char *name)
  446. {
  447. AVDictionaryEntry *entry = NULL;
  448. if (!dict)
  449. return;
  450. probe_object_header(name);
  451. while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
  452. probe_str(entry->key, entry->value);
  453. }
  454. probe_object_footer(name);
  455. }
  456. static char *value_string(char *buf, int buf_size, double val, const char *unit)
  457. {
  458. if (unit == unit_second_str && use_value_sexagesimal_format) {
  459. double secs;
  460. int hours, mins;
  461. secs = val;
  462. mins = (int)secs / 60;
  463. secs = secs - mins * 60;
  464. hours = mins / 60;
  465. mins %= 60;
  466. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  467. } else if (use_value_prefix) {
  468. const char *prefix_string;
  469. int index;
  470. if (unit == unit_byte_str && use_byte_value_binary_prefix) {
  471. index = (int) log2(val) / 10;
  472. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
  473. val /= pow(2, index * 10);
  474. prefix_string = binary_unit_prefixes[index];
  475. } else {
  476. index = (int) (log10(val)) / 3;
  477. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
  478. val /= pow(10, index * 3);
  479. prefix_string = decimal_unit_prefixes[index];
  480. }
  481. snprintf(buf, buf_size, "%.*f%s%s",
  482. index ? 3 : 0, val,
  483. prefix_string,
  484. show_value_unit ? unit : "");
  485. } else {
  486. snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
  487. }
  488. return buf;
  489. }
  490. static char *time_value_string(char *buf, int buf_size, int64_t val,
  491. const AVRational *time_base)
  492. {
  493. if (val == AV_NOPTS_VALUE) {
  494. snprintf(buf, buf_size, "N/A");
  495. } else {
  496. value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
  497. }
  498. return buf;
  499. }
  500. static char *ts_value_string(char *buf, int buf_size, int64_t ts)
  501. {
  502. if (ts == AV_NOPTS_VALUE) {
  503. snprintf(buf, buf_size, "N/A");
  504. } else {
  505. snprintf(buf, buf_size, "%"PRId64, ts);
  506. }
  507. return buf;
  508. }
  509. static char *rational_string(char *buf, int buf_size, const char *sep,
  510. const AVRational *rat)
  511. {
  512. snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
  513. return buf;
  514. }
  515. static char *tag_string(char *buf, int buf_size, int tag)
  516. {
  517. snprintf(buf, buf_size, "0x%04x", tag);
  518. return buf;
  519. }
  520. static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
  521. {
  522. char val_str[128];
  523. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  524. probe_object_header("packet");
  525. probe_str("codec_type", media_type_string(st->codecpar->codec_type));
  526. probe_int("stream_index", pkt->stream_index);
  527. probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
  528. probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
  529. pkt->pts, &st->time_base));
  530. probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
  531. probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
  532. pkt->dts, &st->time_base));
  533. probe_str("duration", ts_value_string(val_str, sizeof(val_str),
  534. pkt->duration));
  535. probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
  536. pkt->duration,
  537. &st->time_base));
  538. probe_str("size", value_string(val_str, sizeof(val_str),
  539. pkt->size, unit_byte_str));
  540. probe_int("pos", pkt->pos);
  541. probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
  542. probe_object_footer("packet");
  543. }
  544. static void show_packets(InputFile *ifile)
  545. {
  546. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  547. AVPacket pkt;
  548. av_init_packet(&pkt);
  549. probe_array_header("packets", 0);
  550. while (!av_read_frame(fmt_ctx, &pkt)) {
  551. show_packet(fmt_ctx, &pkt);
  552. av_packet_unref(&pkt);
  553. }
  554. probe_array_footer("packets", 0);
  555. }
  556. static void show_stream(InputFile *ifile, InputStream *ist)
  557. {
  558. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  559. AVStream *stream = ist->st;
  560. AVCodecParameters *par;
  561. AVCodecContext *dec_ctx;
  562. const AVCodecDescriptor *codec_desc;
  563. const char *profile;
  564. char val_str[128];
  565. AVRational display_aspect_ratio, *sar = NULL;
  566. const AVPixFmtDescriptor *desc;
  567. probe_object_header("stream");
  568. probe_int("index", stream->index);
  569. par = stream->codecpar;
  570. dec_ctx = ist->dec_ctx;
  571. codec_desc = avcodec_descriptor_get(par->codec_id);
  572. if (codec_desc) {
  573. probe_str("codec_name", codec_desc->name);
  574. probe_str("codec_long_name", codec_desc->long_name);
  575. } else {
  576. probe_str("codec_name", "unknown");
  577. }
  578. probe_str("codec_type", media_type_string(par->codec_type));
  579. /* print AVI/FourCC tag */
  580. av_get_codec_tag_string(val_str, sizeof(val_str), par->codec_tag);
  581. probe_str("codec_tag_string", val_str);
  582. probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
  583. par->codec_tag));
  584. /* print profile, if there is one */
  585. profile = avcodec_profile_name(par->codec_id, par->profile);
  586. if (profile)
  587. probe_str("profile", profile);
  588. switch (par->codec_type) {
  589. case AVMEDIA_TYPE_VIDEO:
  590. probe_int("width", par->width);
  591. probe_int("height", par->height);
  592. if (dec_ctx) {
  593. probe_int("coded_width", dec_ctx->coded_width);
  594. probe_int("coded_height", dec_ctx->coded_height);
  595. probe_int("has_b_frames", dec_ctx->has_b_frames);
  596. }
  597. if (dec_ctx && dec_ctx->sample_aspect_ratio.num)
  598. sar = &dec_ctx->sample_aspect_ratio;
  599. else if (par->sample_aspect_ratio.num)
  600. sar = &par->sample_aspect_ratio;
  601. else if (stream->sample_aspect_ratio.num)
  602. sar = &stream->sample_aspect_ratio;
  603. if (sar) {
  604. probe_str("sample_aspect_ratio",
  605. rational_string(val_str, sizeof(val_str), ":", sar));
  606. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  607. par->width * sar->num, par->height * sar->den,
  608. 1024*1024);
  609. probe_str("display_aspect_ratio",
  610. rational_string(val_str, sizeof(val_str), ":",
  611. &display_aspect_ratio));
  612. }
  613. desc = av_pix_fmt_desc_get(par->format);
  614. probe_str("pix_fmt", desc ? desc->name : "unknown");
  615. probe_int("level", par->level);
  616. probe_str("color_range", av_color_range_name (par->color_range));
  617. probe_str("color_space", av_color_space_name (par->color_space));
  618. probe_str("color_trc", av_color_transfer_name (par->color_trc));
  619. probe_str("color_pri", av_color_primaries_name(par->color_primaries));
  620. probe_str("chroma_loc", av_chroma_location_name (par->chroma_location));
  621. break;
  622. case AVMEDIA_TYPE_AUDIO:
  623. probe_str("sample_rate",
  624. value_string(val_str, sizeof(val_str),
  625. par->sample_rate,
  626. unit_hertz_str));
  627. probe_int("channels", par->channels);
  628. probe_int("bits_per_sample",
  629. av_get_bits_per_sample(par->codec_id));
  630. break;
  631. }
  632. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  633. probe_int("id", stream->id);
  634. probe_str("avg_frame_rate",
  635. rational_string(val_str, sizeof(val_str), "/",
  636. &stream->avg_frame_rate));
  637. if (par->bit_rate)
  638. probe_str("bit_rate",
  639. value_string(val_str, sizeof(val_str),
  640. par->bit_rate, unit_bit_per_second_str));
  641. probe_str("time_base",
  642. rational_string(val_str, sizeof(val_str), "/",
  643. &stream->time_base));
  644. probe_str("start_time",
  645. time_value_string(val_str, sizeof(val_str),
  646. stream->start_time, &stream->time_base));
  647. probe_str("duration",
  648. time_value_string(val_str, sizeof(val_str),
  649. stream->duration, &stream->time_base));
  650. if (stream->nb_frames)
  651. probe_int("nb_frames", stream->nb_frames);
  652. probe_dict(stream->metadata, "tags");
  653. if (stream->nb_side_data) {
  654. int i, j;
  655. probe_object_header("sidedata");
  656. for (i = 0; i < stream->nb_side_data; i++) {
  657. const AVPacketSideData* sd = &stream->side_data[i];
  658. AVStereo3D *stereo;
  659. switch (sd->type) {
  660. case AV_PKT_DATA_DISPLAYMATRIX:
  661. probe_object_header("displaymatrix");
  662. probe_array_header("matrix", 1);
  663. for (j = 0; j < 9; j++)
  664. probe_int(NULL, ((int32_t *)sd->data)[j]);
  665. probe_array_footer("matrix", 1);
  666. probe_int("rotation",
  667. av_display_rotation_get((int32_t *)sd->data));
  668. probe_object_footer("displaymatrix");
  669. break;
  670. case AV_PKT_DATA_STEREO3D:
  671. stereo = (AVStereo3D *)sd->data;
  672. probe_object_header("stereo3d");
  673. probe_str("type", av_stereo3d_type_name(stereo->type));
  674. probe_int("inverted",
  675. !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
  676. probe_object_footer("stereo3d");
  677. break;
  678. }
  679. }
  680. probe_object_footer("sidedata");
  681. }
  682. probe_object_footer("stream");
  683. }
  684. static void show_format(InputFile *ifile)
  685. {
  686. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  687. char val_str[128];
  688. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  689. probe_object_header("format");
  690. probe_str("filename", fmt_ctx->filename);
  691. probe_int("nb_streams", fmt_ctx->nb_streams);
  692. probe_str("format_name", fmt_ctx->iformat->name);
  693. probe_str("format_long_name", fmt_ctx->iformat->long_name);
  694. probe_str("start_time",
  695. time_value_string(val_str, sizeof(val_str),
  696. fmt_ctx->start_time, &AV_TIME_BASE_Q));
  697. probe_str("duration",
  698. time_value_string(val_str, sizeof(val_str),
  699. fmt_ctx->duration, &AV_TIME_BASE_Q));
  700. probe_str("size",
  701. size >= 0 ? value_string(val_str, sizeof(val_str),
  702. size, unit_byte_str)
  703. : "unknown");
  704. probe_str("bit_rate",
  705. value_string(val_str, sizeof(val_str),
  706. fmt_ctx->bit_rate, unit_bit_per_second_str));
  707. probe_dict(fmt_ctx->metadata, "tags");
  708. probe_object_footer("format");
  709. }
  710. static int open_input_file(InputFile *ifile, const char *filename)
  711. {
  712. int err, i;
  713. AVFormatContext *fmt_ctx = NULL;
  714. AVDictionaryEntry *t;
  715. if ((err = avformat_open_input(&fmt_ctx, filename,
  716. iformat, &format_opts)) < 0) {
  717. print_error(filename, err);
  718. return err;
  719. }
  720. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  721. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  722. return AVERROR_OPTION_NOT_FOUND;
  723. }
  724. /* fill the streams in the format context */
  725. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  726. print_error(filename, err);
  727. return err;
  728. }
  729. av_dump_format(fmt_ctx, 0, filename, 0);
  730. ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
  731. sizeof(*ifile->streams));
  732. if (!ifile->streams)
  733. exit(1);
  734. ifile->nb_streams = fmt_ctx->nb_streams;
  735. /* bind a decoder to each input stream */
  736. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  737. InputStream *ist = &ifile->streams[i];
  738. AVStream *stream = fmt_ctx->streams[i];
  739. AVCodec *codec;
  740. ist->st = stream;
  741. if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
  742. fprintf(stderr, "Failed to probe codec for input stream %d\n",
  743. stream->index);
  744. continue;
  745. }
  746. codec = avcodec_find_decoder(stream->codecpar->codec_id);
  747. if (!codec) {
  748. fprintf(stderr,
  749. "Unsupported codec with id %d for input stream %d\n",
  750. stream->codecpar->codec_id, stream->index);
  751. continue;
  752. }
  753. ist->dec_ctx = avcodec_alloc_context3(codec);
  754. if (!ist->dec_ctx)
  755. exit(1);
  756. err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
  757. if (err < 0)
  758. exit(1);
  759. err = avcodec_open2(ist->dec_ctx, NULL, NULL);
  760. if (err < 0) {
  761. fprintf(stderr, "Error while opening codec for input stream %d\n",
  762. stream->index);
  763. exit(1);
  764. }
  765. }
  766. ifile->fmt_ctx = fmt_ctx;
  767. return 0;
  768. }
  769. static void close_input_file(InputFile *ifile)
  770. {
  771. int i;
  772. /* close decoder for each stream */
  773. for (i = 0; i < ifile->nb_streams; i++) {
  774. InputStream *ist = &ifile->streams[i];
  775. avcodec_free_context(&ist->dec_ctx);
  776. }
  777. av_freep(&ifile->streams);
  778. ifile->nb_streams = 0;
  779. avformat_close_input(&ifile->fmt_ctx);
  780. }
  781. static int probe_file(const char *filename)
  782. {
  783. InputFile ifile;
  784. int ret, i;
  785. ret = open_input_file(&ifile, filename);
  786. if (ret < 0)
  787. return ret;
  788. if (do_show_format)
  789. show_format(&ifile);
  790. if (do_show_streams) {
  791. probe_array_header("streams", 0);
  792. for (i = 0; i < ifile.nb_streams; i++)
  793. show_stream(&ifile, &ifile.streams[i]);
  794. probe_array_footer("streams", 0);
  795. }
  796. if (do_show_packets)
  797. show_packets(&ifile);
  798. close_input_file(&ifile);
  799. return 0;
  800. }
  801. static void show_usage(void)
  802. {
  803. printf("Simple multimedia streams analyzer\n");
  804. printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  805. printf("\n");
  806. }
  807. static int opt_format(void *optctx, const char *opt, const char *arg)
  808. {
  809. iformat = av_find_input_format(arg);
  810. if (!iformat) {
  811. fprintf(stderr, "Unknown input format: %s\n", arg);
  812. return AVERROR(EINVAL);
  813. }
  814. return 0;
  815. }
  816. static int opt_output_format(void *optctx, const char *opt, const char *arg)
  817. {
  818. if (!strcmp(arg, "json")) {
  819. octx.print_header = json_print_header;
  820. octx.print_footer = json_print_footer;
  821. octx.print_array_header = json_print_array_header;
  822. octx.print_array_footer = json_print_array_footer;
  823. octx.print_object_header = json_print_object_header;
  824. octx.print_object_footer = json_print_object_footer;
  825. octx.print_integer = json_print_integer;
  826. octx.print_string = json_print_string;
  827. } else if (!strcmp(arg, "ini")) {
  828. octx.print_header = ini_print_header;
  829. octx.print_footer = ini_print_footer;
  830. octx.print_array_header = ini_print_array_header;
  831. octx.print_array_footer = ini_print_array_footer;
  832. octx.print_object_header = ini_print_object_header;
  833. octx.print_integer = ini_print_integer;
  834. octx.print_string = ini_print_string;
  835. } else if (!strcmp(arg, "old")) {
  836. octx.print_header = NULL;
  837. octx.print_object_header = old_print_object_header;
  838. octx.print_object_footer = old_print_object_footer;
  839. octx.print_string = old_print_string;
  840. } else {
  841. av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
  842. return AVERROR(EINVAL);
  843. }
  844. return 0;
  845. }
  846. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  847. {
  848. do_show_format = 1;
  849. nb_fmt_entries_to_show++;
  850. octx.print_header = NULL;
  851. octx.print_footer = NULL;
  852. octx.print_array_header = NULL;
  853. octx.print_array_footer = NULL;
  854. octx.print_object_header = NULL;
  855. octx.print_object_footer = NULL;
  856. octx.print_integer = show_format_entry_integer;
  857. octx.print_string = show_format_entry_string;
  858. av_dict_set(&fmt_entries_to_show, arg, "", 0);
  859. return 0;
  860. }
  861. static int opt_show_stream_entry(void *optctx, const char *opt, const char *arg)
  862. {
  863. do_show_streams = 1;
  864. nb_stream_entries_to_show++;
  865. octx.print_header = NULL;
  866. octx.print_footer = NULL;
  867. octx.print_array_header = show_stream_entry_header;
  868. octx.print_array_footer = show_stream_entry_footer;
  869. octx.print_object_header = NULL;
  870. octx.print_object_footer = NULL;
  871. octx.print_integer = show_stream_entry_integer;
  872. octx.print_string = show_stream_entry_string;
  873. av_dict_set(&stream_entries_to_show, arg, "", 0);
  874. return 0;
  875. }
  876. static void opt_input_file(void *optctx, const char *arg)
  877. {
  878. if (input_filename) {
  879. fprintf(stderr,
  880. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  881. arg, input_filename);
  882. exit_program(1);
  883. }
  884. if (!strcmp(arg, "-"))
  885. arg = "pipe:";
  886. input_filename = arg;
  887. }
  888. void show_help_default(const char *opt, const char *arg)
  889. {
  890. av_log_set_callback(log_callback_help);
  891. show_usage();
  892. show_help_options(options, "Main options:", 0, 0, 0);
  893. printf("\n");
  894. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  895. }
  896. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  897. {
  898. show_value_unit = 1;
  899. use_value_prefix = 1;
  900. use_byte_value_binary_prefix = 1;
  901. use_value_sexagesimal_format = 1;
  902. return 0;
  903. }
  904. static const OptionDef real_options[] = {
  905. #include "cmdutils_common_opts.h"
  906. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  907. { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
  908. { "unit", OPT_BOOL, {&show_value_unit},
  909. "show unit of the displayed values" },
  910. { "prefix", OPT_BOOL, {&use_value_prefix},
  911. "use SI prefixes for the displayed values" },
  912. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  913. "use binary prefixes for byte units" },
  914. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  915. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  916. { "pretty", 0, {.func_arg = opt_pretty},
  917. "prettify the format of displayed values, make it more human readable" },
  918. { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
  919. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  920. "show a particular entry from the format/container info", "entry" },
  921. { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
  922. { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
  923. { "show_stream_entry", HAS_ARG, {.func_arg = opt_show_stream_entry},
  924. "show a particular entry from all streams", "entry" },
  925. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
  926. "generic catch all option", "" },
  927. { NULL, },
  928. };
  929. static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
  930. {
  931. printf("%.*s", buf_size, buf);
  932. return 0;
  933. }
  934. #define AVP_BUFFSIZE 4096
  935. int main(int argc, char **argv)
  936. {
  937. int ret;
  938. uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
  939. if (!buffer)
  940. exit(1);
  941. register_exit(avprobe_cleanup);
  942. options = real_options;
  943. parse_loglevel(argc, argv, options);
  944. av_register_all();
  945. avformat_network_init();
  946. init_opts();
  947. #if CONFIG_AVDEVICE
  948. avdevice_register_all();
  949. #endif
  950. show_banner();
  951. octx.print_header = ini_print_header;
  952. octx.print_footer = ini_print_footer;
  953. octx.print_array_header = ini_print_array_header;
  954. octx.print_array_footer = ini_print_array_footer;
  955. octx.print_object_header = ini_print_object_header;
  956. octx.print_integer = ini_print_integer;
  957. octx.print_string = ini_print_string;
  958. parse_options(NULL, argc, argv, options, opt_input_file);
  959. if (!input_filename) {
  960. show_usage();
  961. fprintf(stderr, "You have to specify one input file.\n");
  962. fprintf(stderr,
  963. "Use -h to get full help or, even better, run 'man %s'.\n",
  964. program_name);
  965. exit_program(1);
  966. }
  967. probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
  968. probe_buf_write, NULL);
  969. if (!probe_out)
  970. exit_program(1);
  971. probe_header();
  972. ret = probe_file(input_filename);
  973. probe_footer();
  974. avio_flush(probe_out);
  975. av_freep(&probe_out);
  976. av_freep(&buffer);
  977. uninit_opts();
  978. avformat_network_deinit();
  979. return ret;
  980. }