ffprobe.c 109 KB

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