ffprobe.c 121 KB

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