ffprobe.c 82 KB

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