ffprobe.c 84 KB

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