ffprobe.c 121 KB

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