ffprobe.c 129 KB

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