ffprobe.c 96 KB

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