ffprobe.c 118 KB

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