ffprobe.c 122 KB

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