ffprobe.c 134 KB

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