ffprobe.c 115 KB

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