ffprobe.c 133 KB

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