ffprobe.c 82 KB

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