ffprobe.c 72 KB

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