ffprobe.c 72 KB

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