ffprobe.c 136 KB

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