avprobe.c 32 KB

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