ffprobe.c 77 KB

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