ffprobe.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  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_mallocz(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", av_frame_get_pkt_duration(frame));
  1239. print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
  1240. if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
  1241. else print_str_opt("pkt_pos", "N/A");
  1242. if (av_frame_get_pkt_size(frame) != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
  1243. else print_str_opt("pkt_size", "N/A");
  1244. switch (stream->codec->codec_type) {
  1245. AVRational sar;
  1246. case AVMEDIA_TYPE_VIDEO:
  1247. print_int("width", frame->width);
  1248. print_int("height", frame->height);
  1249. s = av_get_pix_fmt_name(frame->format);
  1250. if (s) print_str ("pix_fmt", s);
  1251. else print_str_opt("pix_fmt", "unknown");
  1252. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
  1253. if (sar.num) {
  1254. print_q("sample_aspect_ratio", sar, ':');
  1255. } else {
  1256. print_str_opt("sample_aspect_ratio", "N/A");
  1257. }
  1258. print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
  1259. print_int("coded_picture_number", frame->coded_picture_number);
  1260. print_int("display_picture_number", frame->display_picture_number);
  1261. print_int("interlaced_frame", frame->interlaced_frame);
  1262. print_int("top_field_first", frame->top_field_first);
  1263. print_int("repeat_pict", frame->repeat_pict);
  1264. break;
  1265. case AVMEDIA_TYPE_AUDIO:
  1266. s = av_get_sample_fmt_name(frame->format);
  1267. if (s) print_str ("sample_fmt", s);
  1268. else print_str_opt("sample_fmt", "unknown");
  1269. print_int("nb_samples", frame->nb_samples);
  1270. print_int("channels", av_frame_get_channels(frame));
  1271. if (av_frame_get_channel_layout(frame)) {
  1272. av_bprint_clear(&pbuf);
  1273. av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
  1274. av_frame_get_channel_layout(frame));
  1275. print_str ("channel_layout", pbuf.str);
  1276. } else
  1277. print_str_opt("channel_layout", "unknown");
  1278. break;
  1279. }
  1280. show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
  1281. writer_print_section_footer(w);
  1282. av_bprint_finalize(&pbuf, NULL);
  1283. fflush(stdout);
  1284. }
  1285. static av_always_inline int process_frame(WriterContext *w,
  1286. AVFormatContext *fmt_ctx,
  1287. AVFrame *frame, AVPacket *pkt)
  1288. {
  1289. AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
  1290. int ret = 0, got_frame = 0;
  1291. avcodec_get_frame_defaults(frame);
  1292. if (dec_ctx->codec) {
  1293. switch (dec_ctx->codec_type) {
  1294. case AVMEDIA_TYPE_VIDEO:
  1295. ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
  1296. break;
  1297. case AVMEDIA_TYPE_AUDIO:
  1298. ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
  1299. break;
  1300. }
  1301. }
  1302. if (ret < 0)
  1303. return ret;
  1304. ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
  1305. pkt->data += ret;
  1306. pkt->size -= ret;
  1307. if (got_frame) {
  1308. nb_streams_frames[pkt->stream_index]++;
  1309. if (do_show_frames)
  1310. show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
  1311. }
  1312. return got_frame;
  1313. }
  1314. static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
  1315. {
  1316. AVPacket pkt, pkt1;
  1317. AVFrame frame;
  1318. int i = 0;
  1319. av_init_packet(&pkt);
  1320. while (!av_read_frame(fmt_ctx, &pkt)) {
  1321. if (selected_streams[pkt.stream_index]) {
  1322. if (do_read_packets) {
  1323. if (do_show_packets)
  1324. show_packet(w, fmt_ctx, &pkt, i++);
  1325. nb_streams_packets[pkt.stream_index]++;
  1326. }
  1327. if (do_read_frames) {
  1328. pkt1 = pkt;
  1329. while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
  1330. }
  1331. }
  1332. av_free_packet(&pkt);
  1333. }
  1334. av_init_packet(&pkt);
  1335. pkt.data = NULL;
  1336. pkt.size = 0;
  1337. //Flush remaining frames that are cached in the decoder
  1338. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1339. pkt.stream_index = i;
  1340. if (do_read_frames)
  1341. while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
  1342. }
  1343. }
  1344. static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
  1345. {
  1346. AVStream *stream = fmt_ctx->streams[stream_idx];
  1347. AVCodecContext *dec_ctx;
  1348. const AVCodec *dec;
  1349. char val_str[128];
  1350. const char *s;
  1351. AVRational sar, dar;
  1352. AVBPrint pbuf;
  1353. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1354. writer_print_section_header(w, SECTION_ID_STREAM);
  1355. print_int("index", stream->index);
  1356. if ((dec_ctx = stream->codec)) {
  1357. const char *profile = NULL;
  1358. dec = dec_ctx->codec;
  1359. if (dec) {
  1360. print_str("codec_name", dec->name);
  1361. if (!do_bitexact) {
  1362. if (dec->long_name) print_str ("codec_long_name", dec->long_name);
  1363. else print_str_opt("codec_long_name", "unknown");
  1364. }
  1365. } else {
  1366. print_str_opt("codec_name", "unknown");
  1367. if (!do_bitexact) {
  1368. print_str_opt("codec_long_name", "unknown");
  1369. }
  1370. }
  1371. if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
  1372. print_str("profile", profile);
  1373. else
  1374. print_str_opt("profile", "unknown");
  1375. s = av_get_media_type_string(dec_ctx->codec_type);
  1376. if (s) print_str ("codec_type", s);
  1377. else print_str_opt("codec_type", "unknown");
  1378. print_q("codec_time_base", dec_ctx->time_base, '/');
  1379. /* print AVI/FourCC tag */
  1380. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  1381. print_str("codec_tag_string", val_str);
  1382. print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
  1383. switch (dec_ctx->codec_type) {
  1384. case AVMEDIA_TYPE_VIDEO:
  1385. print_int("width", dec_ctx->width);
  1386. print_int("height", dec_ctx->height);
  1387. print_int("has_b_frames", dec_ctx->has_b_frames);
  1388. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
  1389. if (sar.den) {
  1390. print_q("sample_aspect_ratio", sar, ':');
  1391. av_reduce(&dar.num, &dar.den,
  1392. dec_ctx->width * sar.num,
  1393. dec_ctx->height * sar.den,
  1394. 1024*1024);
  1395. print_q("display_aspect_ratio", dar, ':');
  1396. } else {
  1397. print_str_opt("sample_aspect_ratio", "N/A");
  1398. print_str_opt("display_aspect_ratio", "N/A");
  1399. }
  1400. s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
  1401. if (s) print_str ("pix_fmt", s);
  1402. else print_str_opt("pix_fmt", "unknown");
  1403. print_int("level", dec_ctx->level);
  1404. if (dec_ctx->timecode_frame_start >= 0) {
  1405. char tcbuf[AV_TIMECODE_STR_SIZE];
  1406. av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
  1407. print_str("timecode", tcbuf);
  1408. } else {
  1409. print_str_opt("timecode", "N/A");
  1410. }
  1411. break;
  1412. case AVMEDIA_TYPE_AUDIO:
  1413. s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
  1414. if (s) print_str ("sample_fmt", s);
  1415. else print_str_opt("sample_fmt", "unknown");
  1416. print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
  1417. print_int("channels", dec_ctx->channels);
  1418. print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
  1419. break;
  1420. }
  1421. } else {
  1422. print_str_opt("codec_type", "unknown");
  1423. }
  1424. if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
  1425. const AVOption *opt = NULL;
  1426. while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
  1427. uint8_t *str;
  1428. if (opt->flags) continue;
  1429. if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
  1430. print_str(opt->name, str);
  1431. av_free(str);
  1432. }
  1433. }
  1434. }
  1435. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  1436. else print_str_opt("id", "N/A");
  1437. print_q("r_frame_rate", stream->r_frame_rate, '/');
  1438. print_q("avg_frame_rate", stream->avg_frame_rate, '/');
  1439. print_q("time_base", stream->time_base, '/');
  1440. print_ts ("start_pts", stream->start_time);
  1441. print_time("start_time", stream->start_time, &stream->time_base);
  1442. print_ts ("duration_ts", stream->duration);
  1443. print_time("duration", stream->duration, &stream->time_base);
  1444. if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
  1445. else print_str_opt("bit_rate", "N/A");
  1446. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  1447. else print_str_opt("nb_frames", "N/A");
  1448. if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
  1449. else print_str_opt("nb_read_frames", "N/A");
  1450. if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
  1451. else print_str_opt("nb_read_packets", "N/A");
  1452. if (do_show_data)
  1453. writer_print_data(w, "extradata", dec_ctx->extradata,
  1454. dec_ctx->extradata_size);
  1455. /* Print disposition information */
  1456. #define PRINT_DISPOSITION(flagname, name) do { \
  1457. print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
  1458. } while (0)
  1459. if (do_show_stream_disposition) {
  1460. writer_print_section_header(w, SECTION_ID_STREAM_DISPOSITION);
  1461. PRINT_DISPOSITION(DEFAULT, "default");
  1462. PRINT_DISPOSITION(DUB, "dub");
  1463. PRINT_DISPOSITION(ORIGINAL, "original");
  1464. PRINT_DISPOSITION(COMMENT, "comment");
  1465. PRINT_DISPOSITION(LYRICS, "lyrics");
  1466. PRINT_DISPOSITION(KARAOKE, "karaoke");
  1467. PRINT_DISPOSITION(FORCED, "forced");
  1468. PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
  1469. PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
  1470. PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
  1471. PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
  1472. writer_print_section_footer(w);
  1473. }
  1474. show_tags(w, stream->metadata, SECTION_ID_STREAM_TAGS);
  1475. writer_print_section_footer(w);
  1476. av_bprint_finalize(&pbuf, NULL);
  1477. fflush(stdout);
  1478. }
  1479. static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
  1480. {
  1481. int i;
  1482. writer_print_section_header(w, SECTION_ID_STREAMS);
  1483. for (i = 0; i < fmt_ctx->nb_streams; i++)
  1484. if (selected_streams[i])
  1485. show_stream(w, fmt_ctx, i);
  1486. writer_print_section_footer(w);
  1487. }
  1488. static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
  1489. {
  1490. char val_str[128];
  1491. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  1492. writer_print_section_header(w, SECTION_ID_FORMAT);
  1493. print_str("filename", fmt_ctx->filename);
  1494. print_int("nb_streams", fmt_ctx->nb_streams);
  1495. print_str("format_name", fmt_ctx->iformat->name);
  1496. if (!do_bitexact) {
  1497. if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
  1498. else print_str_opt("format_long_name", "unknown");
  1499. }
  1500. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  1501. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  1502. if (size >= 0) print_val ("size", size, unit_byte_str);
  1503. else print_str_opt("size", "N/A");
  1504. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  1505. else print_str_opt("bit_rate", "N/A");
  1506. show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
  1507. writer_print_section_footer(w);
  1508. fflush(stdout);
  1509. }
  1510. static void show_error(WriterContext *w, int err)
  1511. {
  1512. char errbuf[128];
  1513. const char *errbuf_ptr = errbuf;
  1514. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  1515. errbuf_ptr = strerror(AVUNERROR(err));
  1516. writer_print_section_header(w, SECTION_ID_ERROR);
  1517. print_int("code", err);
  1518. print_str("string", errbuf_ptr);
  1519. writer_print_section_footer(w);
  1520. }
  1521. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  1522. {
  1523. int err, i, orig_nb_streams;
  1524. AVFormatContext *fmt_ctx = NULL;
  1525. AVDictionaryEntry *t;
  1526. AVDictionary **opts;
  1527. if ((err = avformat_open_input(&fmt_ctx, filename,
  1528. iformat, &format_opts)) < 0) {
  1529. print_error(filename, err);
  1530. return err;
  1531. }
  1532. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  1533. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  1534. return AVERROR_OPTION_NOT_FOUND;
  1535. }
  1536. /* fill the streams in the format context */
  1537. opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
  1538. orig_nb_streams = fmt_ctx->nb_streams;
  1539. if ((err = avformat_find_stream_info(fmt_ctx, opts)) < 0) {
  1540. print_error(filename, err);
  1541. return err;
  1542. }
  1543. for (i = 0; i < orig_nb_streams; i++)
  1544. av_dict_free(&opts[i]);
  1545. av_freep(&opts);
  1546. av_dump_format(fmt_ctx, 0, filename, 0);
  1547. /* bind a decoder to each input stream */
  1548. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1549. AVStream *stream = fmt_ctx->streams[i];
  1550. AVCodec *codec;
  1551. if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
  1552. av_log(NULL, AV_LOG_ERROR,
  1553. "Failed to probe codec for input stream %d\n",
  1554. stream->index);
  1555. } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  1556. av_log(NULL, AV_LOG_ERROR,
  1557. "Unsupported codec with id %d for input stream %d\n",
  1558. stream->codec->codec_id, stream->index);
  1559. } else {
  1560. AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
  1561. fmt_ctx, stream, codec);
  1562. if (avcodec_open2(stream->codec, codec, &opts) < 0) {
  1563. av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
  1564. stream->index);
  1565. }
  1566. if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  1567. av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
  1568. t->key, stream->index);
  1569. return AVERROR_OPTION_NOT_FOUND;
  1570. }
  1571. }
  1572. }
  1573. *fmt_ctx_ptr = fmt_ctx;
  1574. return 0;
  1575. }
  1576. static void close_input_file(AVFormatContext **ctx_ptr)
  1577. {
  1578. int i;
  1579. AVFormatContext *fmt_ctx = *ctx_ptr;
  1580. /* close decoder for each stream */
  1581. for (i = 0; i < fmt_ctx->nb_streams; i++)
  1582. if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
  1583. avcodec_close(fmt_ctx->streams[i]->codec);
  1584. avformat_close_input(ctx_ptr);
  1585. }
  1586. static int probe_file(WriterContext *wctx, const char *filename)
  1587. {
  1588. AVFormatContext *fmt_ctx;
  1589. int ret, i;
  1590. int section_id;
  1591. do_read_frames = do_show_frames || do_count_frames;
  1592. do_read_packets = do_show_packets || do_count_packets;
  1593. ret = open_input_file(&fmt_ctx, filename);
  1594. if (ret >= 0) {
  1595. nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
  1596. nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
  1597. selected_streams = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
  1598. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1599. if (stream_specifier) {
  1600. ret = avformat_match_stream_specifier(fmt_ctx,
  1601. fmt_ctx->streams[i],
  1602. stream_specifier);
  1603. if (ret < 0)
  1604. goto end;
  1605. else
  1606. selected_streams[i] = ret;
  1607. } else {
  1608. selected_streams[i] = 1;
  1609. }
  1610. }
  1611. if (do_read_frames || do_read_packets) {
  1612. if (do_show_frames && do_show_packets &&
  1613. wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
  1614. section_id = SECTION_ID_PACKETS_AND_FRAMES;
  1615. else if (do_show_packets && !do_show_frames)
  1616. section_id = SECTION_ID_PACKETS;
  1617. else // (!do_show_packets && do_show_frames)
  1618. section_id = SECTION_ID_FRAMES;
  1619. if (do_show_frames || do_show_packets)
  1620. writer_print_section_header(wctx, section_id);
  1621. read_packets(wctx, fmt_ctx);
  1622. if (do_show_frames || do_show_packets)
  1623. writer_print_section_footer(wctx);
  1624. }
  1625. if (do_show_streams)
  1626. show_streams(wctx, fmt_ctx);
  1627. if (do_show_format)
  1628. show_format(wctx, fmt_ctx);
  1629. end:
  1630. close_input_file(&fmt_ctx);
  1631. av_freep(&nb_streams_frames);
  1632. av_freep(&nb_streams_packets);
  1633. av_freep(&selected_streams);
  1634. }
  1635. return ret;
  1636. }
  1637. static void show_usage(void)
  1638. {
  1639. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  1640. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  1641. av_log(NULL, AV_LOG_INFO, "\n");
  1642. }
  1643. static void ffprobe_show_program_version(WriterContext *w)
  1644. {
  1645. AVBPrint pbuf;
  1646. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1647. writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
  1648. print_str("version", FFMPEG_VERSION);
  1649. print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
  1650. program_birth_year, this_year);
  1651. print_str("build_date", __DATE__);
  1652. print_str("build_time", __TIME__);
  1653. print_str("compiler_ident", CC_IDENT);
  1654. print_str("configuration", FFMPEG_CONFIGURATION);
  1655. writer_print_section_footer(w);
  1656. av_bprint_finalize(&pbuf, NULL);
  1657. }
  1658. #define SHOW_LIB_VERSION(libname, LIBNAME) \
  1659. do { \
  1660. if (CONFIG_##LIBNAME) { \
  1661. unsigned int version = libname##_version(); \
  1662. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
  1663. print_str("name", "lib" #libname); \
  1664. print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
  1665. print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
  1666. print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
  1667. print_int("version", version); \
  1668. print_str("ident", LIB##LIBNAME##_IDENT); \
  1669. writer_print_section_footer(w); \
  1670. } \
  1671. } while (0)
  1672. static void ffprobe_show_library_versions(WriterContext *w)
  1673. {
  1674. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
  1675. SHOW_LIB_VERSION(avutil, AVUTIL);
  1676. SHOW_LIB_VERSION(avcodec, AVCODEC);
  1677. SHOW_LIB_VERSION(avformat, AVFORMAT);
  1678. SHOW_LIB_VERSION(avdevice, AVDEVICE);
  1679. SHOW_LIB_VERSION(avfilter, AVFILTER);
  1680. SHOW_LIB_VERSION(swscale, SWSCALE);
  1681. SHOW_LIB_VERSION(swresample, SWRESAMPLE);
  1682. SHOW_LIB_VERSION(postproc, POSTPROC);
  1683. writer_print_section_footer(w);
  1684. }
  1685. static int opt_format(void *optctx, const char *opt, const char *arg)
  1686. {
  1687. iformat = av_find_input_format(arg);
  1688. if (!iformat) {
  1689. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  1690. return AVERROR(EINVAL);
  1691. }
  1692. return 0;
  1693. }
  1694. static inline void mark_section_show_entries(SectionID section_id,
  1695. int show_all_entries, AVDictionary *entries)
  1696. {
  1697. struct section *section = &sections[section_id];
  1698. section->show_all_entries = show_all_entries;
  1699. if (show_all_entries) {
  1700. SectionID *id;
  1701. for (id = section->children_ids; *id != -1; id++)
  1702. mark_section_show_entries(*id, show_all_entries, entries);
  1703. } else {
  1704. av_dict_copy(&section->entries_to_show, entries, 0);
  1705. }
  1706. }
  1707. static int match_section(const char *section_name,
  1708. int show_all_entries, AVDictionary *entries)
  1709. {
  1710. int i, ret = 0;
  1711. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
  1712. const struct section *section = &sections[i];
  1713. if (!strcmp(section_name, section->name) ||
  1714. (section->unique_name && !strcmp(section_name, section->unique_name))) {
  1715. av_log(NULL, AV_LOG_DEBUG,
  1716. "'%s' matches section with unique name '%s'\n", section_name,
  1717. (char *)av_x_if_null(section->unique_name, section->name));
  1718. ret++;
  1719. mark_section_show_entries(section->id, show_all_entries, entries);
  1720. }
  1721. }
  1722. return ret;
  1723. }
  1724. static int opt_show_entries(void *optctx, const char *opt, const char *arg)
  1725. {
  1726. const char *p = arg;
  1727. int ret = 0;
  1728. while (*p) {
  1729. AVDictionary *entries = NULL;
  1730. char *section_name = av_get_token(&p, "=:");
  1731. int show_all_entries = 0;
  1732. if (!section_name) {
  1733. av_log(NULL, AV_LOG_ERROR,
  1734. "Missing section name for option '%s'\n", opt);
  1735. return AVERROR(EINVAL);
  1736. }
  1737. if (*p == '=') {
  1738. p++;
  1739. while (*p && *p != ':') {
  1740. char *entry = av_get_token(&p, ",:");
  1741. if (!entry)
  1742. break;
  1743. av_log(NULL, AV_LOG_VERBOSE,
  1744. "Adding '%s' to the entries to show in section '%s'\n",
  1745. entry, section_name);
  1746. av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
  1747. if (*p == ',')
  1748. p++;
  1749. }
  1750. } else {
  1751. show_all_entries = 1;
  1752. }
  1753. ret = match_section(section_name, show_all_entries, entries);
  1754. if (ret == 0) {
  1755. av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
  1756. ret = AVERROR(EINVAL);
  1757. }
  1758. av_dict_free(&entries);
  1759. av_free(section_name);
  1760. if (ret <= 0)
  1761. break;
  1762. if (*p)
  1763. p++;
  1764. }
  1765. return ret;
  1766. }
  1767. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  1768. {
  1769. char *buf = av_asprintf("format=%s", arg);
  1770. int ret;
  1771. av_log(NULL, AV_LOG_WARNING,
  1772. "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
  1773. opt, arg);
  1774. ret = opt_show_entries(optctx, opt, buf);
  1775. av_free(buf);
  1776. return ret;
  1777. }
  1778. static void opt_input_file(void *optctx, const char *arg)
  1779. {
  1780. if (input_filename) {
  1781. av_log(NULL, AV_LOG_ERROR,
  1782. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  1783. arg, input_filename);
  1784. exit(1);
  1785. }
  1786. if (!strcmp(arg, "-"))
  1787. arg = "pipe:";
  1788. input_filename = arg;
  1789. }
  1790. static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
  1791. {
  1792. opt_input_file(optctx, arg);
  1793. return 0;
  1794. }
  1795. void show_help_default(const char *opt, const char *arg)
  1796. {
  1797. av_log_set_callback(log_callback_help);
  1798. show_usage();
  1799. show_help_options(options, "Main options:", 0, 0, 0);
  1800. printf("\n");
  1801. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  1802. }
  1803. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  1804. {
  1805. show_value_unit = 1;
  1806. use_value_prefix = 1;
  1807. use_byte_value_binary_prefix = 1;
  1808. use_value_sexagesimal_format = 1;
  1809. return 0;
  1810. }
  1811. static void print_section(SectionID id, int level)
  1812. {
  1813. const SectionID *pid;
  1814. const struct section *section = &sections[id];
  1815. printf("%c%c%c",
  1816. section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
  1817. section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
  1818. section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
  1819. printf("%*c %s", level * 4, ' ', section->name);
  1820. if (section->unique_name)
  1821. printf("/%s", section->unique_name);
  1822. printf("\n");
  1823. for (pid = section->children_ids; *pid != -1; pid++)
  1824. print_section(*pid, level+1);
  1825. }
  1826. static int opt_sections(void *optctx, const char *opt, const char *arg)
  1827. {
  1828. printf("Sections:\n"
  1829. "W.. = Section is a wrapper (contains other sections, no local entries)\n"
  1830. ".A. = Section contains an array of elements of the same type\n"
  1831. "..V = Section may contain a variable number of fields with variable keys\n"
  1832. "FLAGS NAME/UNIQUE_NAME\n"
  1833. "---\n");
  1834. print_section(SECTION_ID_ROOT, 0);
  1835. return 0;
  1836. }
  1837. static int opt_show_versions(const char *opt, const char *arg)
  1838. {
  1839. mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
  1840. mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
  1841. return 0;
  1842. }
  1843. #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
  1844. static int opt_show_##section(const char *opt, const char *arg) \
  1845. { \
  1846. mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
  1847. return 0; \
  1848. }
  1849. DEFINE_OPT_SHOW_SECTION(error, ERROR);
  1850. DEFINE_OPT_SHOW_SECTION(format, FORMAT);
  1851. DEFINE_OPT_SHOW_SECTION(frames, FRAMES);
  1852. DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS);
  1853. DEFINE_OPT_SHOW_SECTION(packets, PACKETS);
  1854. DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION);
  1855. DEFINE_OPT_SHOW_SECTION(streams, STREAMS);
  1856. static const OptionDef real_options[] = {
  1857. #include "cmdutils_common_opts.h"
  1858. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  1859. { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
  1860. { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
  1861. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  1862. "use binary prefixes for byte units" },
  1863. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  1864. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  1865. { "pretty", 0, {.func_arg = opt_pretty},
  1866. "prettify the format of displayed values, make it more human readable" },
  1867. { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
  1868. "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
  1869. { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
  1870. { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
  1871. { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
  1872. { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
  1873. { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
  1874. { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
  1875. { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
  1876. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  1877. "show a particular entry from the format/container info", "entry" },
  1878. { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
  1879. "show a set of specified entries", "entry_list" },
  1880. { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
  1881. { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
  1882. { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
  1883. { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
  1884. { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
  1885. { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
  1886. { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
  1887. { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
  1888. { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
  1889. { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
  1890. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
  1891. { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
  1892. { NULL, },
  1893. };
  1894. static inline int check_section_show_entries(int section_id)
  1895. {
  1896. int *id;
  1897. struct section *section = &sections[section_id];
  1898. if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
  1899. return 1;
  1900. for (id = section->children_ids; *id != -1; id++)
  1901. if (check_section_show_entries(*id))
  1902. return 1;
  1903. return 0;
  1904. }
  1905. #define SET_DO_SHOW(id, varname) do { \
  1906. if (check_section_show_entries(SECTION_ID_##id)) \
  1907. do_show_##varname = 1; \
  1908. } while (0)
  1909. int main(int argc, char **argv)
  1910. {
  1911. const Writer *w;
  1912. WriterContext *wctx;
  1913. char *buf;
  1914. char *w_name = NULL, *w_args = NULL;
  1915. int ret, i;
  1916. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  1917. atexit(exit_program);
  1918. options = real_options;
  1919. parse_loglevel(argc, argv, options);
  1920. av_register_all();
  1921. avformat_network_init();
  1922. init_opts();
  1923. #if CONFIG_AVDEVICE
  1924. avdevice_register_all();
  1925. #endif
  1926. show_banner(argc, argv, options);
  1927. parse_options(NULL, argc, argv, options, opt_input_file);
  1928. /* mark things to show, based on -show_entries */
  1929. SET_DO_SHOW(ERROR, error);
  1930. SET_DO_SHOW(FORMAT, format);
  1931. SET_DO_SHOW(FRAMES, frames);
  1932. SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
  1933. SET_DO_SHOW(PACKETS, packets);
  1934. SET_DO_SHOW(PROGRAM_VERSION, program_version);
  1935. SET_DO_SHOW(STREAMS, streams);
  1936. SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
  1937. if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
  1938. av_log(NULL, AV_LOG_ERROR,
  1939. "-bitexact and -show_program_version or -show_library_versions "
  1940. "options are incompatible\n");
  1941. ret = AVERROR(EINVAL);
  1942. goto end;
  1943. }
  1944. writer_register_all();
  1945. if (!print_format)
  1946. print_format = av_strdup("default");
  1947. if (!print_format) {
  1948. ret = AVERROR(ENOMEM);
  1949. goto end;
  1950. }
  1951. w_name = av_strtok(print_format, "=", &buf);
  1952. w_args = buf;
  1953. w = writer_get_by_name(w_name);
  1954. if (!w) {
  1955. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  1956. ret = AVERROR(EINVAL);
  1957. goto end;
  1958. }
  1959. if ((ret = writer_open(&wctx, w, w_args,
  1960. sections, FF_ARRAY_ELEMS(sections))) >= 0) {
  1961. writer_print_section_header(wctx, SECTION_ID_ROOT);
  1962. if (do_show_program_version)
  1963. ffprobe_show_program_version(wctx);
  1964. if (do_show_library_versions)
  1965. ffprobe_show_library_versions(wctx);
  1966. if (!input_filename &&
  1967. ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
  1968. (!do_show_program_version && !do_show_library_versions))) {
  1969. show_usage();
  1970. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  1971. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  1972. ret = AVERROR(EINVAL);
  1973. } else if (input_filename) {
  1974. ret = probe_file(wctx, input_filename);
  1975. if (ret < 0 && do_show_error)
  1976. show_error(wctx, ret);
  1977. }
  1978. writer_print_section_footer(wctx);
  1979. writer_close(&wctx);
  1980. }
  1981. end:
  1982. av_freep(&print_format);
  1983. uninit_opts();
  1984. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
  1985. av_dict_free(&(sections[i].entries_to_show));
  1986. avformat_network_deinit();
  1987. return ret;
  1988. }