ffprobe.c 135 KB

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