FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffprobe.c
Go to the documentation of this file.
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 /**
22  * @file
23  * simple media prober based on the FFmpeg libraries
24  */
25 
26 #include "config.h"
27 #include "version.h"
28 
29 #include <string.h>
30 
31 #include "libavformat/avformat.h"
32 #include "libavcodec/avcodec.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/bprint.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/libm.h"
40 #include "libavutil/timecode.h"
41 #include "libavdevice/avdevice.h"
42 #include "libswscale/swscale.h"
45 #include "cmdutils.h"
46 
47 const char program_name[] = "ffprobe";
48 const int program_birth_year = 2007;
49 
50 static int do_bitexact = 0;
51 static int do_count_frames = 0;
52 static int do_count_packets = 0;
53 static int do_read_frames = 0;
54 static int do_read_packets = 0;
55 static int do_show_error = 0;
56 static int do_show_format = 0;
57 static int do_show_frames = 0;
58 static int do_show_packets = 0;
59 static int do_show_streams = 0;
61 static int do_show_data = 0;
62 static int do_show_program_version = 0;
63 static int do_show_library_versions = 0;
64 
65 static int show_value_unit = 0;
66 static int use_value_prefix = 0;
69 static int show_private_data = 1;
70 
71 static char *print_format;
72 static char *stream_specifier;
73 
74 /* section structure definition */
75 
76 #define SECTION_MAX_NB_CHILDREN 10
77 
78 struct section {
79  int id; ///< unique id identifying a section
80  const char *name;
81 
82 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
83 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
84 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
85  /// For these sections the element_name field is mandatory.
86  int flags;
87  int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
88  const char *element_name; ///< name of the contained element, if provided
89  const char *unique_name; ///< unique section name, in case the name is ambiguous
92 };
93 
94 typedef enum {
113 } SectionID;
114 
115 static struct section sections[] = {
116  [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
117  [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
118  [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
120  [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, -1 } },
121  [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
123  [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
126  [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { -1 } },
127  [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
133  [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
134  [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
135 };
136 
137 static const OptionDef *options;
138 
139 /* FFprobe context */
140 static const char *input_filename;
142 
143 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
144 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
145 
146 static const char unit_second_str[] = "s" ;
147 static const char unit_hertz_str[] = "Hz" ;
148 static const char unit_byte_str[] = "byte" ;
149 static const char unit_bit_per_second_str[] = "bit/s";
150 
151 static uint64_t *nb_streams_packets;
152 static uint64_t *nb_streams_frames;
153 static int *selected_streams;
154 
155 static void exit_program(void)
156 {
157  int i;
158  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
159  av_dict_free(&(sections[i].entries_to_show));
160 }
161 
162 struct unit_value {
163  union { double d; long long int i; } val;
164  const char *unit;
165 };
166 
167 static char *value_string(char *buf, int buf_size, struct unit_value uv)
168 {
169  double vald;
170  long long int vali;
171  int show_float = 0;
172 
173  if (uv.unit == unit_second_str) {
174  vald = uv.val.d;
175  show_float = 1;
176  } else {
177  vald = vali = uv.val.i;
178  }
179 
181  double secs;
182  int hours, mins;
183  secs = vald;
184  mins = (int)secs / 60;
185  secs = secs - mins * 60;
186  hours = mins / 60;
187  mins %= 60;
188  snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
189  } else {
190  const char *prefix_string = "";
191 
192  if (use_value_prefix && vald > 1) {
193  long long int index;
194 
196  index = (long long int) (log2(vald)) / 10;
197  index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
198  vald /= exp2(index * 10);
199  prefix_string = binary_unit_prefixes[index];
200  } else {
201  index = (long long int) (log10(vald)) / 3;
202  index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
203  vald /= pow(10, index * 3);
204  prefix_string = decimal_unit_prefixes[index];
205  }
206  }
207 
208  if (show_float || (use_value_prefix && vald != (long long int)vald))
209  snprintf(buf, buf_size, "%f", vald);
210  else
211  snprintf(buf, buf_size, "%lld", vali);
212  av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
213  prefix_string, show_value_unit ? uv.unit : "");
214  }
215 
216  return buf;
217 }
218 
219 /* WRITERS API */
220 
221 typedef struct WriterContext WriterContext;
222 
223 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
224 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
225 
226 typedef struct Writer {
227  const AVClass *priv_class; ///< private class of the writer, if any
228  int priv_size; ///< private size for the writer context
229  const char *name;
230 
231  int (*init) (WriterContext *wctx);
233 
236  void (*print_integer) (WriterContext *wctx, const char *, long long int);
237  void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
238  void (*print_string) (WriterContext *wctx, const char *, const char *);
239  int flags; ///< a combination or WRITER_FLAG_*
240 } Writer;
241 
242 #define SECTION_MAX_NB_LEVELS 10
243 
245  const AVClass *class; ///< class of the writer
246  const Writer *writer; ///< the Writer of which this is an instance
247  char *name; ///< name of this writer instance
248  void *priv; ///< private data for use by the filter
249 
250  const struct section *sections; ///< array containing all sections
251  int nb_sections; ///< number of sections
252 
253  int level; ///< current level, starting from 0
254 
255  /** number of the item printed in the given section, starting from 0 */
257 
258  /** section per each level */
260  AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
261  /// used by various writers
262 
263  unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
264  unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
265  unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
266 };
267 
268 static const char *writer_get_name(void *p)
269 {
270  WriterContext *wctx = p;
271  return wctx->writer->name;
272 }
273 
274 static const AVClass writer_class = {
275  "Writer",
277  NULL,
279 };
280 
281 static void writer_close(WriterContext **wctx)
282 {
283  int i;
284 
285  if (!*wctx)
286  return;
287 
288  if ((*wctx)->writer->uninit)
289  (*wctx)->writer->uninit(*wctx);
290  for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
291  av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
292  if ((*wctx)->writer->priv_class)
293  av_opt_free((*wctx)->priv);
294  av_freep(&((*wctx)->priv));
295  av_freep(wctx);
296 }
297 
298 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
299  const struct section *sections, int nb_sections)
300 {
301  int i, ret = 0;
302 
303  if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
304  ret = AVERROR(ENOMEM);
305  goto fail;
306  }
307 
308  if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
309  ret = AVERROR(ENOMEM);
310  goto fail;
311  }
312 
313  (*wctx)->class = &writer_class;
314  (*wctx)->writer = writer;
315  (*wctx)->level = -1;
316  (*wctx)->sections = sections;
317  (*wctx)->nb_sections = nb_sections;
318 
319  if (writer->priv_class) {
320  void *priv_ctx = (*wctx)->priv;
321  *((const AVClass **)priv_ctx) = writer->priv_class;
322  av_opt_set_defaults(priv_ctx);
323 
324  if (args &&
325  (ret = av_set_options_string(priv_ctx, args, "=", ":")) < 0)
326  goto fail;
327  }
328 
329  for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
330  av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
331 
332  if ((*wctx)->writer->init)
333  ret = (*wctx)->writer->init(*wctx);
334  if (ret < 0)
335  goto fail;
336 
337  return 0;
338 
339 fail:
340  writer_close(wctx);
341  return ret;
342 }
343 
345  int section_id)
346 {
347  int parent_section_id;
348  wctx->level++;
350  parent_section_id = wctx->level ?
351  (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
352 
353  wctx->nb_item[wctx->level] = 0;
354  wctx->section[wctx->level] = &wctx->sections[section_id];
355 
356  if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
357  wctx->nb_section_packet = wctx->nb_section_frame =
358  wctx->nb_section_packet_frame = 0;
359  } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
360  wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
361  wctx->nb_section_packet : wctx->nb_section_frame;
362  }
363 
364  if (wctx->writer->print_section_header)
365  wctx->writer->print_section_header(wctx);
366 }
367 
369 {
370  int section_id = wctx->section[wctx->level]->id;
371  int parent_section_id = wctx->level ?
372  wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
373 
374  if (parent_section_id != SECTION_ID_NONE)
375  wctx->nb_item[wctx->level-1]++;
376  if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
377  if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
378  else wctx->nb_section_frame++;
379  }
380  if (wctx->writer->print_section_footer)
381  wctx->writer->print_section_footer(wctx);
382  wctx->level--;
383 }
384 
385 static inline void writer_print_integer(WriterContext *wctx,
386  const char *key, long long int val)
387 {
388  const struct section *section = wctx->section[wctx->level];
389 
390  if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
391  wctx->writer->print_integer(wctx, key, val);
392  wctx->nb_item[wctx->level]++;
393  }
394 }
395 
396 static inline void writer_print_string(WriterContext *wctx,
397  const char *key, const char *val, int opt)
398 {
399  const struct section *section = wctx->section[wctx->level];
400 
401  if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
402  return;
403 
404  if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
405  wctx->writer->print_string(wctx, key, val);
406  wctx->nb_item[wctx->level]++;
407  }
408 }
409 
410 static inline void writer_print_rational(WriterContext *wctx,
411  const char *key, AVRational q, char sep)
412 {
413  AVBPrint buf;
415  av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
416  writer_print_string(wctx, key, buf.str, 0);
417 }
418 
419 static void writer_print_time(WriterContext *wctx, const char *key,
420  int64_t ts, const AVRational *time_base, int is_duration)
421 {
422  char buf[128];
423 
424  if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
425  writer_print_string(wctx, key, "N/A", 1);
426  } else {
427  double d = ts * av_q2d(*time_base);
428  struct unit_value uv;
429  uv.val.d = d;
430  uv.unit = unit_second_str;
431  value_string(buf, sizeof(buf), uv);
432  writer_print_string(wctx, key, buf, 0);
433  }
434 }
435 
436 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
437 {
438  if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
439  writer_print_string(wctx, key, "N/A", 1);
440  } else {
441  writer_print_integer(wctx, key, ts);
442  }
443 }
444 
445 static void writer_print_data(WriterContext *wctx, const char *name,
446  uint8_t *data, int size)
447 {
448  AVBPrint bp;
449  int offset = 0, l, i;
450 
452  av_bprintf(&bp, "\n");
453  while (size) {
454  av_bprintf(&bp, "%08x: ", offset);
455  l = FFMIN(size, 16);
456  for (i = 0; i < l; i++) {
457  av_bprintf(&bp, "%02x", data[i]);
458  if (i & 1)
459  av_bprintf(&bp, " ");
460  }
461  av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
462  for (i = 0; i < l; i++)
463  av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
464  av_bprintf(&bp, "\n");
465  offset += l;
466  data += l;
467  size -= l;
468  }
469  writer_print_string(wctx, name, bp.str, 0);
470  av_bprint_finalize(&bp, NULL);
471 }
472 
473 #define MAX_REGISTERED_WRITERS_NB 64
474 
476 
477 static int writer_register(const Writer *writer)
478 {
479  static int next_registered_writer_idx = 0;
480 
481  if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
482  return AVERROR(ENOMEM);
483 
484  registered_writers[next_registered_writer_idx++] = writer;
485  return 0;
486 }
487 
488 static const Writer *writer_get_by_name(const char *name)
489 {
490  int i;
491 
492  for (i = 0; registered_writers[i]; i++)
493  if (!strcmp(registered_writers[i]->name, name))
494  return registered_writers[i];
495 
496  return NULL;
497 }
498 
499 
500 /* WRITERS */
501 
502 #define DEFINE_WRITER_CLASS(name) \
503 static const char *name##_get_name(void *ctx) \
504 { \
505  return #name ; \
506 } \
507 static const AVClass name##_class = { \
508  #name, \
509  name##_get_name, \
510  name##_options \
511 }
512 
513 /* Default output */
514 
515 typedef struct DefaultContext {
516  const AVClass *class;
517  int nokey;
521 
522 #define OFFSET(x) offsetof(DefaultContext, x)
523 
524 static const AVOption default_options[] = {
525  { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
526  { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
527  { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
528  { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
529  {NULL},
530 };
531 
532 DEFINE_WRITER_CLASS(default);
533 
534 /* lame uppercasing routine, assumes the string is lower case ASCII */
535 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
536 {
537  int i;
538  for (i = 0; src[i] && i < dst_size-1; i++)
539  dst[i] = av_toupper(src[i]);
540  dst[i] = 0;
541  return dst;
542 }
543 
545 {
546  DefaultContext *def = wctx->priv;
547  char buf[32];
548  const struct section *section = wctx->section[wctx->level];
549  const struct section *parent_section = wctx->level ?
550  wctx->section[wctx->level-1] : NULL;
551 
552  av_bprint_clear(&wctx->section_pbuf[wctx->level]);
553  if (parent_section &&
554  !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
555  def->nested_section[wctx->level] = 1;
556  av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
557  wctx->section_pbuf[wctx->level-1].str,
558  upcase_string(buf, sizeof(buf),
559  av_x_if_null(section->element_name, section->name)));
560  }
561 
562  if (def->noprint_wrappers || def->nested_section[wctx->level])
563  return;
564 
566  printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
567 }
568 
570 {
571  DefaultContext *def = wctx->priv;
572  const struct section *section = wctx->section[wctx->level];
573  char buf[32];
574 
575  if (def->noprint_wrappers || def->nested_section[wctx->level])
576  return;
577 
579  printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
580 }
581 
582 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
583 {
584  DefaultContext *def = wctx->priv;
585 
586  if (!def->nokey)
587  printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
588  printf("%s\n", value);
589 }
590 
591 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
592 {
593  DefaultContext *def = wctx->priv;
594 
595  if (!def->nokey)
596  printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
597  printf("%lld\n", value);
598 }
599 
600 static const Writer default_writer = {
601  .name = "default",
602  .priv_size = sizeof(DefaultContext),
605  .print_integer = default_print_int,
606  .print_string = default_print_str,
608  .priv_class = &default_class,
609 };
610 
611 /* Compact output */
612 
613 /**
614  * Apply C-language-like string escaping.
615  */
616 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
617 {
618  const char *p;
619 
620  for (p = src; *p; p++) {
621  switch (*p) {
622  case '\b': av_bprintf(dst, "%s", "\\b"); break;
623  case '\f': av_bprintf(dst, "%s", "\\f"); break;
624  case '\n': av_bprintf(dst, "%s", "\\n"); break;
625  case '\r': av_bprintf(dst, "%s", "\\r"); break;
626  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
627  default:
628  if (*p == sep)
629  av_bprint_chars(dst, '\\', 1);
630  av_bprint_chars(dst, *p, 1);
631  }
632  }
633  return dst->str;
634 }
635 
636 /**
637  * Quote fields containing special characters, check RFC4180.
638  */
639 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
640 {
641  char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
642  int needs_quoting = !!src[strcspn(src, meta_chars)];
643 
644  if (needs_quoting)
645  av_bprint_chars(dst, '"', 1);
646 
647  for (; *src; src++) {
648  if (*src == '"')
649  av_bprint_chars(dst, '"', 1);
650  av_bprint_chars(dst, *src, 1);
651  }
652  if (needs_quoting)
653  av_bprint_chars(dst, '"', 1);
654  return dst->str;
655 }
656 
657 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
658 {
659  return src;
660 }
661 
662 typedef struct CompactContext {
663  const AVClass *class;
665  char item_sep;
666  int nokey;
669  const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
672 
673 #undef OFFSET
674 #define OFFSET(x) offsetof(CompactContext, x)
675 
676 static const AVOption compact_options[]= {
677  {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
678  {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
679  {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
680  {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
681  {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
682  {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
683  {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
684  {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
685  {NULL},
686 };
687 
688 DEFINE_WRITER_CLASS(compact);
689 
691 {
692  CompactContext *compact = wctx->priv;
693 
694  if (strlen(compact->item_sep_str) != 1) {
695  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
696  compact->item_sep_str);
697  return AVERROR(EINVAL);
698  }
699  compact->item_sep = compact->item_sep_str[0];
700 
701  if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
702  else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
703  else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
704  else {
705  av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
706  return AVERROR(EINVAL);
707  }
708 
709  return 0;
710 }
711 
713 {
714  CompactContext *compact = wctx->priv;
715  const struct section *section = wctx->section[wctx->level];
716  const struct section *parent_section = wctx->level ?
717  wctx->section[wctx->level-1] : NULL;
718 
719  av_bprint_clear(&wctx->section_pbuf[wctx->level]);
720  if (parent_section &&
721  !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
722  compact->nested_section[wctx->level] = 1;
723  av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
724  wctx->section_pbuf[wctx->level-1].str,
725  (char *)av_x_if_null(section->element_name, section->name));
726  wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
727  } else if (compact->print_section &&
729  printf("%s%c", section->name, compact->item_sep);
730 }
731 
733 {
734  CompactContext *compact = wctx->priv;
735 
736  if (!compact->nested_section[wctx->level] &&
738  printf("\n");
739 }
740 
741 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
742 {
743  CompactContext *compact = wctx->priv;
744  AVBPrint buf;
745 
746  if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
747  if (!compact->nokey)
748  printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
750  printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
751  av_bprint_finalize(&buf, NULL);
752 }
753 
754 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
755 {
756  CompactContext *compact = wctx->priv;
757 
758  if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
759  if (!compact->nokey)
760  printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
761  printf("%lld", value);
762 }
763 
764 static const Writer compact_writer = {
765  .name = "compact",
766  .priv_size = sizeof(CompactContext),
767  .init = compact_init,
770  .print_integer = compact_print_int,
771  .print_string = compact_print_str,
773  .priv_class = &compact_class,
774 };
775 
776 /* CSV output */
777 
778 #undef OFFSET
779 #define OFFSET(x) offsetof(CompactContext, x)
780 
781 static const AVOption csv_options[] = {
782  {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
783  {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
784  {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
785  {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
786  {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
787  {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
788  {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
789  {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
790  {NULL},
791 };
792 
794 
795 static const Writer csv_writer = {
796  .name = "csv",
797  .priv_size = sizeof(CompactContext),
798  .init = compact_init,
801  .print_integer = compact_print_int,
802  .print_string = compact_print_str,
804  .priv_class = &csv_class,
805 };
806 
807 /* Flat output */
808 
809 typedef struct FlatContext {
810  const AVClass *class;
811  const char *sep_str;
812  char sep;
814 } FlatContext;
815 
816 #undef OFFSET
817 #define OFFSET(x) offsetof(FlatContext, x)
818 
819 static const AVOption flat_options[]= {
820  {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
821  {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
822  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
823  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
824  {NULL},
825 };
826 
827 DEFINE_WRITER_CLASS(flat);
828 
829 static av_cold int flat_init(WriterContext *wctx)
830 {
831  FlatContext *flat = wctx->priv;
832 
833  if (strlen(flat->sep_str) != 1) {
834  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
835  flat->sep_str);
836  return AVERROR(EINVAL);
837  }
838  flat->sep = flat->sep_str[0];
839 
840  return 0;
841 }
842 
843 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
844 {
845  const char *p;
846 
847  for (p = src; *p; p++) {
848  if (!((*p >= '0' && *p <= '9') ||
849  (*p >= 'a' && *p <= 'z') ||
850  (*p >= 'A' && *p <= 'Z')))
851  av_bprint_chars(dst, '_', 1);
852  else
853  av_bprint_chars(dst, *p, 1);
854  }
855  return dst->str;
856 }
857 
858 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
859 {
860  const char *p;
861 
862  for (p = src; *p; p++) {
863  switch (*p) {
864  case '\n': av_bprintf(dst, "%s", "\\n"); break;
865  case '\r': av_bprintf(dst, "%s", "\\r"); break;
866  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
867  case '"': av_bprintf(dst, "%s", "\\\""); break;
868  case '`': av_bprintf(dst, "%s", "\\`"); break;
869  case '$': av_bprintf(dst, "%s", "\\$"); break;
870  default: av_bprint_chars(dst, *p, 1); break;
871  }
872  }
873  return dst->str;
874 }
875 
877 {
878  FlatContext *flat = wctx->priv;
879  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
880  const struct section *section = wctx->section[wctx->level];
881  const struct section *parent_section = wctx->level ?
882  wctx->section[wctx->level-1] : NULL;
883 
884  /* build section header */
885  av_bprint_clear(buf);
886  if (!parent_section)
887  return;
888  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
889 
890  if (flat->hierarchical ||
892  av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
893 
894  if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
895  int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
896  wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
897  av_bprintf(buf, "%d%s", n, flat->sep_str);
898  }
899  }
900 }
901 
902 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
903 {
904  printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
905 }
906 
907 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
908 {
909  FlatContext *flat = wctx->priv;
910  AVBPrint buf;
911 
912  printf("%s", wctx->section_pbuf[wctx->level].str);
914  printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
915  av_bprint_clear(&buf);
916  printf("\"%s\"\n", flat_escape_value_str(&buf, value));
917  av_bprint_finalize(&buf, NULL);
918 }
919 
920 static const Writer flat_writer = {
921  .name = "flat",
922  .priv_size = sizeof(FlatContext),
923  .init = flat_init,
925  .print_integer = flat_print_int,
926  .print_string = flat_print_str,
928  .priv_class = &flat_class,
929 };
930 
931 /* INI format output */
932 
933 typedef struct {
934  const AVClass *class;
936 } INIContext;
937 
938 #undef OFFSET
939 #define OFFSET(x) offsetof(INIContext, x)
940 
941 static const AVOption ini_options[] = {
942  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
943  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
944  {NULL},
945 };
946 
948 
949 static char *ini_escape_str(AVBPrint *dst, const char *src)
950 {
951  int i = 0;
952  char c = 0;
953 
954  while (c = src[i++]) {
955  switch (c) {
956  case '\b': av_bprintf(dst, "%s", "\\b"); break;
957  case '\f': av_bprintf(dst, "%s", "\\f"); break;
958  case '\n': av_bprintf(dst, "%s", "\\n"); break;
959  case '\r': av_bprintf(dst, "%s", "\\r"); break;
960  case '\t': av_bprintf(dst, "%s", "\\t"); break;
961  case '\\':
962  case '#' :
963  case '=' :
964  case ':' : av_bprint_chars(dst, '\\', 1);
965  default:
966  if ((unsigned char)c < 32)
967  av_bprintf(dst, "\\x00%02x", c & 0xff);
968  else
969  av_bprint_chars(dst, c, 1);
970  break;
971  }
972  }
973  return dst->str;
974 }
975 
977 {
978  INIContext *ini = wctx->priv;
979  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
980  const struct section *section = wctx->section[wctx->level];
981  const struct section *parent_section = wctx->level ?
982  wctx->section[wctx->level-1] : NULL;
983 
984  av_bprint_clear(buf);
985  if (!parent_section) {
986  printf("# ffprobe output\n\n");
987  return;
988  }
989 
990  if (wctx->nb_item[wctx->level-1])
991  printf("\n");
992 
993  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
994  if (ini->hierarchical ||
996  av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
997 
998  if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
999  int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1000  wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1001  av_bprintf(buf, ".%d", n);
1002  }
1003  }
1004 
1006  printf("[%s]\n", buf->str);
1007 }
1008 
1009 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1010 {
1011  AVBPrint buf;
1012 
1014  printf("%s=", ini_escape_str(&buf, key));
1015  av_bprint_clear(&buf);
1016  printf("%s\n", ini_escape_str(&buf, value));
1017  av_bprint_finalize(&buf, NULL);
1018 }
1019 
1020 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1021 {
1022  printf("%s=%lld\n", key, value);
1023 }
1024 
1025 static const Writer ini_writer = {
1026  .name = "ini",
1027  .priv_size = sizeof(INIContext),
1029  .print_integer = ini_print_int,
1030  .print_string = ini_print_str,
1032  .priv_class = &ini_class,
1033 };
1034 
1035 /* JSON output */
1036 
1037 typedef struct {
1038  const AVClass *class;
1040  int compact;
1041  const char *item_sep, *item_start_end;
1042 } JSONContext;
1043 
1044 #undef OFFSET
1045 #define OFFSET(x) offsetof(JSONContext, x)
1046 
1047 static const AVOption json_options[]= {
1048  { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1049  { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1050  { NULL }
1051 };
1052 
1053 DEFINE_WRITER_CLASS(json);
1054 
1056 {
1057  JSONContext *json = wctx->priv;
1058 
1059  json->item_sep = json->compact ? ", " : ",\n";
1060  json->item_start_end = json->compact ? " " : "\n";
1061 
1062  return 0;
1063 }
1064 
1065 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1066 {
1067  static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1068  static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1069  const char *p;
1070 
1071  for (p = src; *p; p++) {
1072  char *s = strchr(json_escape, *p);
1073  if (s) {
1074  av_bprint_chars(dst, '\\', 1);
1075  av_bprint_chars(dst, json_subst[s - json_escape], 1);
1076  } else if ((unsigned char)*p < 32) {
1077  av_bprintf(dst, "\\u00%02x", *p & 0xff);
1078  } else {
1079  av_bprint_chars(dst, *p, 1);
1080  }
1081  }
1082  return dst->str;
1083 }
1084 
1085 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1086 
1088 {
1089  JSONContext *json = wctx->priv;
1090  AVBPrint buf;
1091  const struct section *section = wctx->section[wctx->level];
1092  const struct section *parent_section = wctx->level ?
1093  wctx->section[wctx->level-1] : NULL;
1094 
1095  if (wctx->level && wctx->nb_item[wctx->level-1])
1096  printf(",\n");
1097 
1098  if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1099  printf("{\n");
1100  json->indent_level++;
1101  } else {
1103  json_escape_str(&buf, section->name, wctx);
1104  JSON_INDENT();
1105 
1106  json->indent_level++;
1107  if (section->flags & SECTION_FLAG_IS_ARRAY) {
1108  printf("\"%s\": [\n", buf.str);
1109  } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1110  printf("\"%s\": {%s", buf.str, json->item_start_end);
1111  } else {
1112  printf("{%s", json->item_start_end);
1113 
1114  /* this is required so the parser can distinguish between packets and frames */
1115  if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1116  if (!json->compact)
1117  JSON_INDENT();
1118  printf("\"type\": \"%s\"%s", section->name, json->item_sep);
1119  }
1120  }
1121  av_bprint_finalize(&buf, NULL);
1122  }
1123 }
1124 
1126 {
1127  JSONContext *json = wctx->priv;
1128  const struct section *section = wctx->section[wctx->level];
1129 
1130  if (wctx->level == 0) {
1131  json->indent_level--;
1132  printf("\n}\n");
1133  } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1134  printf("\n");
1135  json->indent_level--;
1136  JSON_INDENT();
1137  printf("]");
1138  } else {
1139  printf("%s", json->item_start_end);
1140  json->indent_level--;
1141  if (!json->compact)
1142  JSON_INDENT();
1143  printf("}");
1144  }
1145 }
1146 
1147 static inline void json_print_item_str(WriterContext *wctx,
1148  const char *key, const char *value)
1149 {
1150  AVBPrint buf;
1151 
1153  printf("\"%s\":", json_escape_str(&buf, key, wctx));
1154  av_bprint_clear(&buf);
1155  printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1156  av_bprint_finalize(&buf, NULL);
1157 }
1158 
1159 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1160 {
1161  JSONContext *json = wctx->priv;
1162 
1163  if (wctx->nb_item[wctx->level])
1164  printf("%s", json->item_sep);
1165  if (!json->compact)
1166  JSON_INDENT();
1167  json_print_item_str(wctx, key, value);
1168 }
1169 
1170 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1171 {
1172  JSONContext *json = wctx->priv;
1173  AVBPrint buf;
1174 
1175  if (wctx->nb_item[wctx->level])
1176  printf("%s", json->item_sep);
1177  if (!json->compact)
1178  JSON_INDENT();
1179 
1181  printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1182  av_bprint_finalize(&buf, NULL);
1183 }
1184 
1185 static const Writer json_writer = {
1186  .name = "json",
1187  .priv_size = sizeof(JSONContext),
1188  .init = json_init,
1191  .print_integer = json_print_int,
1192  .print_string = json_print_str,
1194  .priv_class = &json_class,
1195 };
1196 
1197 /* XML output */
1198 
1199 typedef struct {
1200  const AVClass *class;
1205 } XMLContext;
1206 
1207 #undef OFFSET
1208 #define OFFSET(x) offsetof(XMLContext, x)
1209 
1210 static const AVOption xml_options[] = {
1211  {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1212  {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1213  {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1214  {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1215  {NULL},
1216 };
1217 
1218 DEFINE_WRITER_CLASS(xml);
1219 
1220 static av_cold int xml_init(WriterContext *wctx)
1221 {
1222  XMLContext *xml = wctx->priv;
1223 
1224  if (xml->xsd_strict) {
1225  xml->fully_qualified = 1;
1226 #define CHECK_COMPLIANCE(opt, opt_name) \
1227  if (opt) { \
1228  av_log(wctx, AV_LOG_ERROR, \
1229  "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1230  "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1231  return AVERROR(EINVAL); \
1232  }
1233  CHECK_COMPLIANCE(show_private_data, "private");
1236 
1238  av_log(wctx, AV_LOG_ERROR,
1239  "Interleaved frames and packets are not allowed in XSD. "
1240  "Select only one between the -show_frames and the -show_packets options.\n");
1241  return AVERROR(EINVAL);
1242  }
1243  }
1244 
1245  return 0;
1246 }
1247 
1248 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1249 {
1250  const char *p;
1251 
1252  for (p = src; *p; p++) {
1253  switch (*p) {
1254  case '&' : av_bprintf(dst, "%s", "&amp;"); break;
1255  case '<' : av_bprintf(dst, "%s", "&lt;"); break;
1256  case '>' : av_bprintf(dst, "%s", "&gt;"); break;
1257  case '"' : av_bprintf(dst, "%s", "&quot;"); break;
1258  case '\'': av_bprintf(dst, "%s", "&apos;"); break;
1259  default: av_bprint_chars(dst, *p, 1);
1260  }
1261  }
1262 
1263  return dst->str;
1264 }
1265 
1266 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1267 
1269 {
1270  XMLContext *xml = wctx->priv;
1271  const struct section *section = wctx->section[wctx->level];
1272  const struct section *parent_section = wctx->level ?
1273  wctx->section[wctx->level-1] : NULL;
1274 
1275  if (wctx->level == 0) {
1276  const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1277  "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1278  "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1279 
1280  printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1281  printf("<%sffprobe%s>\n",
1282  xml->fully_qualified ? "ffprobe:" : "",
1283  xml->fully_qualified ? qual : "");
1284  return;
1285  }
1286 
1287  if (xml->within_tag) {
1288  xml->within_tag = 0;
1289  printf(">\n");
1290  }
1291  if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1292  xml->indent_level++;
1293  } else {
1294  if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1295  wctx->level && wctx->nb_item[wctx->level-1])
1296  printf("\n");
1297  xml->indent_level++;
1298 
1299  if (section->flags & SECTION_FLAG_IS_ARRAY) {
1300  XML_INDENT(); printf("<%s>\n", section->name);
1301  } else {
1302  XML_INDENT(); printf("<%s ", section->name);
1303  xml->within_tag = 1;
1304  }
1305  }
1306 }
1307 
1309 {
1310  XMLContext *xml = wctx->priv;
1311  const struct section *section = wctx->section[wctx->level];
1312 
1313  if (wctx->level == 0) {
1314  printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1315  } else if (xml->within_tag) {
1316  xml->within_tag = 0;
1317  printf("/>\n");
1318  xml->indent_level--;
1319  } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1320  xml->indent_level--;
1321  } else {
1322  XML_INDENT(); printf("</%s>\n", section->name);
1323  xml->indent_level--;
1324  }
1325 }
1326 
1327 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1328 {
1329  AVBPrint buf;
1330  XMLContext *xml = wctx->priv;
1331  const struct section *section = wctx->section[wctx->level];
1332 
1334 
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 
1347  av_bprint_finalize(&buf, NULL);
1348 }
1349 
1350 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1351 {
1352  if (wctx->nb_item[wctx->level])
1353  printf(" ");
1354  printf("%s=\"%lld\"", key, value);
1355 }
1356 
1357 static Writer xml_writer = {
1358  .name = "xml",
1359  .priv_size = sizeof(XMLContext),
1360  .init = xml_init,
1363  .print_integer = xml_print_int,
1364  .print_string = xml_print_str,
1366  .priv_class = &xml_class,
1367 };
1368 
1369 static void writer_register_all(void)
1370 {
1371  static int initialized;
1372 
1373  if (initialized)
1374  return;
1375  initialized = 1;
1376 
1377  writer_register(&default_writer);
1378  writer_register(&compact_writer);
1379  writer_register(&csv_writer);
1380  writer_register(&flat_writer);
1381  writer_register(&ini_writer);
1382  writer_register(&json_writer);
1383  writer_register(&xml_writer);
1384 }
1385 
1386 #define print_fmt(k, f, ...) do { \
1387  av_bprint_clear(&pbuf); \
1388  av_bprintf(&pbuf, f, __VA_ARGS__); \
1389  writer_print_string(w, k, pbuf.str, 0); \
1390 } while (0)
1391 
1392 #define print_int(k, v) writer_print_integer(w, k, v)
1393 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1394 #define print_str(k, v) writer_print_string(w, k, v, 0)
1395 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1396 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1397 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1398 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1399 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1400 #define print_val(k, v, u) do { \
1401  struct unit_value uv; \
1402  uv.val.i = v; \
1403  uv.unit = u; \
1404  writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1405 } while (0)
1406 
1407 #define print_section_header(s) writer_print_section_header(w, s)
1408 #define print_section_footer(s) writer_print_section_footer(w, s)
1409 
1410 static inline void show_tags(WriterContext *wctx, AVDictionary *tags, int section_id)
1411 {
1413 
1414  if (!tags)
1415  return;
1416  writer_print_section_header(wctx, section_id);
1417  while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX)))
1418  writer_print_string(wctx, tag->key, tag->value, 0);
1420 }
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 
1430 
1432 
1434  if (s) print_str ("codec_type", s);
1435  else print_str_opt("codec_type", "unknown");
1436  print_int("stream_index", pkt->stream_index);
1437  print_ts ("pts", pkt->pts);
1438  print_time("pts_time", pkt->pts, &st->time_base);
1439  print_ts ("dts", pkt->dts);
1440  print_time("dts_time", pkt->dts, &st->time_base);
1441  print_duration_ts("duration", pkt->duration);
1442  print_duration_time("duration_time", pkt->duration, &st->time_base);
1443  print_duration_ts("convergence_duration", pkt->convergence_duration);
1444  print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1445  print_val("size", pkt->size, unit_byte_str);
1446  if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1447  else print_str_opt("pos", "N/A");
1448  print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1449  if (do_show_data)
1450  writer_print_data(w, "data", pkt->data, pkt->size);
1452 
1453  av_bprint_finalize(&pbuf, NULL);
1454  fflush(stdout);
1455 }
1456 
1457 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1459 {
1460  AVBPrint pbuf;
1461  const char *s;
1462 
1464 
1466 
1468  if (s) print_str ("media_type", s);
1469  else print_str_opt("media_type", "unknown");
1470  print_int("key_frame", frame->key_frame);
1471  print_ts ("pkt_pts", frame->pkt_pts);
1472  print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1473  print_ts ("pkt_dts", frame->pkt_dts);
1474  print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1475  print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
1476  print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
1477  if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
1478  else print_str_opt("pkt_pos", "N/A");
1479  if (av_frame_get_pkt_size(frame) != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
1480  else print_str_opt("pkt_size", "N/A");
1481 
1482  switch (stream->codec->codec_type) {
1483  AVRational sar;
1484 
1485  case AVMEDIA_TYPE_VIDEO:
1486  print_int("width", frame->width);
1487  print_int("height", frame->height);
1488  s = av_get_pix_fmt_name(frame->format);
1489  if (s) print_str ("pix_fmt", s);
1490  else print_str_opt("pix_fmt", "unknown");
1491  sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1492  if (sar.num) {
1493  print_q("sample_aspect_ratio", sar, ':');
1494  } else {
1495  print_str_opt("sample_aspect_ratio", "N/A");
1496  }
1497  print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1498  print_int("coded_picture_number", frame->coded_picture_number);
1499  print_int("display_picture_number", frame->display_picture_number);
1500  print_int("interlaced_frame", frame->interlaced_frame);
1501  print_int("top_field_first", frame->top_field_first);
1502  print_int("repeat_pict", frame->repeat_pict);
1503  print_int("reference", frame->reference);
1504  break;
1505 
1506  case AVMEDIA_TYPE_AUDIO:
1507  s = av_get_sample_fmt_name(frame->format);
1508  if (s) print_str ("sample_fmt", s);
1509  else print_str_opt("sample_fmt", "unknown");
1510  print_int("nb_samples", frame->nb_samples);
1511  print_int("channels", av_frame_get_channels(frame));
1512  if (av_frame_get_channel_layout(frame)) {
1513  av_bprint_clear(&pbuf);
1516  print_str ("channel_layout", pbuf.str);
1517  } else
1518  print_str_opt("channel_layout", "unknown");
1519  break;
1520  }
1522 
1524 
1525  av_bprint_finalize(&pbuf, NULL);
1526  fflush(stdout);
1527 }
1528 
1532 {
1533  AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1534  int ret = 0, got_frame = 0;
1535 
1537  if (dec_ctx->codec) {
1538  switch (dec_ctx->codec_type) {
1539  case AVMEDIA_TYPE_VIDEO:
1540  ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1541  break;
1542 
1543  case AVMEDIA_TYPE_AUDIO:
1544  ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1545  break;
1546  }
1547  }
1548 
1549  if (ret < 0)
1550  return ret;
1551  ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1552  pkt->data += ret;
1553  pkt->size -= ret;
1554  if (got_frame) {
1556  if (do_show_frames)
1557  show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1558  }
1559  return got_frame;
1560 }
1561 
1563 {
1564  AVPacket pkt, pkt1;
1565  AVFrame frame;
1566  int i = 0;
1567 
1568  av_init_packet(&pkt);
1569 
1570  while (!av_read_frame(fmt_ctx, &pkt)) {
1571  if (selected_streams[pkt.stream_index]) {
1572  if (do_read_packets) {
1573  if (do_show_packets)
1574  show_packet(w, fmt_ctx, &pkt, i++);
1576  }
1577  if (do_read_frames) {
1578  pkt1 = pkt;
1579  while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
1580  }
1581  }
1582  av_free_packet(&pkt);
1583  }
1584  av_init_packet(&pkt);
1585  pkt.data = NULL;
1586  pkt.size = 0;
1587  //Flush remaining frames that are cached in the decoder
1588  for (i = 0; i < fmt_ctx->nb_streams; i++) {
1589  pkt.stream_index = i;
1590  if (do_read_frames)
1591  while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
1592  }
1593 }
1594 
1595 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1596 {
1597  AVStream *stream = fmt_ctx->streams[stream_idx];
1599  const AVCodec *dec;
1600  char val_str[128];
1601  const char *s;
1602  AVRational sar, dar;
1603  AVBPrint pbuf;
1604 
1606 
1608 
1609  print_int("index", stream->index);
1610 
1611  if ((dec_ctx = stream->codec)) {
1612  const char *profile = NULL;
1613  dec = dec_ctx->codec;
1614  if (dec) {
1615  print_str("codec_name", dec->name);
1616  if (!do_bitexact) {
1617  if (dec->long_name) print_str ("codec_long_name", dec->long_name);
1618  else print_str_opt("codec_long_name", "unknown");
1619  }
1620  } else {
1621  print_str_opt("codec_name", "unknown");
1622  if (!do_bitexact) {
1623  print_str_opt("codec_long_name", "unknown");
1624  }
1625  }
1626 
1627  if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1628  print_str("profile", profile);
1629  else
1630  print_str_opt("profile", "unknown");
1631 
1632  s = av_get_media_type_string(dec_ctx->codec_type);
1633  if (s) print_str ("codec_type", s);
1634  else print_str_opt("codec_type", "unknown");
1635  print_q("codec_time_base", dec_ctx->time_base, '/');
1636 
1637  /* print AVI/FourCC tag */
1638  av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1639  print_str("codec_tag_string", val_str);
1640  print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1641 
1642  switch (dec_ctx->codec_type) {
1643  case AVMEDIA_TYPE_VIDEO:
1644  print_int("width", dec_ctx->width);
1645  print_int("height", dec_ctx->height);
1646  print_int("has_b_frames", dec_ctx->has_b_frames);
1647  sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
1648  if (sar.den) {
1649  print_q("sample_aspect_ratio", sar, ':');
1650  av_reduce(&dar.num, &dar.den,
1651  dec_ctx->width * sar.num,
1652  dec_ctx->height * sar.den,
1653  1024*1024);
1654  print_q("display_aspect_ratio", dar, ':');
1655  } else {
1656  print_str_opt("sample_aspect_ratio", "N/A");
1657  print_str_opt("display_aspect_ratio", "N/A");
1658  }
1659  s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1660  if (s) print_str ("pix_fmt", s);
1661  else print_str_opt("pix_fmt", "unknown");
1662  print_int("level", dec_ctx->level);
1663  if (dec_ctx->timecode_frame_start >= 0) {
1664  char tcbuf[AV_TIMECODE_STR_SIZE];
1666  print_str("timecode", tcbuf);
1667  } else {
1668  print_str_opt("timecode", "N/A");
1669  }
1670  break;
1671 
1672  case AVMEDIA_TYPE_AUDIO:
1673  s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1674  if (s) print_str ("sample_fmt", s);
1675  else print_str_opt("sample_fmt", "unknown");
1676  print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1677  print_int("channels", dec_ctx->channels);
1678  print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1679  break;
1680  }
1681  } else {
1682  print_str_opt("codec_type", "unknown");
1683  }
1684  if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1685  const AVOption *opt = NULL;
1686  while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1687  uint8_t *str;
1688  if (opt->flags) continue;
1689  if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1690  print_str(opt->name, str);
1691  av_free(str);
1692  }
1693  }
1694  }
1695 
1696  if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1697  else print_str_opt("id", "N/A");
1698  print_q("r_frame_rate", stream->r_frame_rate, '/');
1699  print_q("avg_frame_rate", stream->avg_frame_rate, '/');
1700  print_q("time_base", stream->time_base, '/');
1701  print_ts ("start_pts", stream->start_time);
1702  print_time("start_time", stream->start_time, &stream->time_base);
1703  print_ts ("duration_ts", stream->duration);
1704  print_time("duration", stream->duration, &stream->time_base);
1705  if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1706  else print_str_opt("bit_rate", "N/A");
1707  if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1708  else print_str_opt("nb_frames", "N/A");
1709  if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1710  else print_str_opt("nb_read_frames", "N/A");
1711  if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1712  else print_str_opt("nb_read_packets", "N/A");
1713  if (do_show_data)
1714  writer_print_data(w, "extradata", dec_ctx->extradata,
1715  dec_ctx->extradata_size);
1716 
1717  /* Print disposition information */
1718 #define PRINT_DISPOSITION(flagname, name) do { \
1719  print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
1720  } while (0)
1721 
1724  PRINT_DISPOSITION(DEFAULT, "default");
1725  PRINT_DISPOSITION(DUB, "dub");
1726  PRINT_DISPOSITION(ORIGINAL, "original");
1727  PRINT_DISPOSITION(COMMENT, "comment");
1728  PRINT_DISPOSITION(LYRICS, "lyrics");
1729  PRINT_DISPOSITION(KARAOKE, "karaoke");
1730  PRINT_DISPOSITION(FORCED, "forced");
1731  PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
1732  PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
1733  PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
1734  PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
1736  }
1737 
1739 
1741  av_bprint_finalize(&pbuf, NULL);
1742  fflush(stdout);
1743 }
1744 
1746 {
1747  int i;
1749  for (i = 0; i < fmt_ctx->nb_streams; i++)
1750  if (selected_streams[i])
1751  show_stream(w, fmt_ctx, i);
1753 }
1754 
1756 {
1757  char val_str[128];
1758  int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1759 
1761  print_str("filename", fmt_ctx->filename);
1762  print_int("nb_streams", fmt_ctx->nb_streams);
1763  print_str("format_name", fmt_ctx->iformat->name);
1764  if (!do_bitexact) {
1765  if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
1766  else print_str_opt("format_long_name", "unknown");
1767  }
1768  print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1769  print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1770  if (size >= 0) print_val ("size", size, unit_byte_str);
1771  else print_str_opt("size", "N/A");
1772  if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1773  else print_str_opt("bit_rate", "N/A");
1775 
1777  fflush(stdout);
1778 }
1779 
1780 static void show_error(WriterContext *w, int err)
1781 {
1782  char errbuf[128];
1783  const char *errbuf_ptr = errbuf;
1784 
1785  if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1786  errbuf_ptr = strerror(AVUNERROR(err));
1787 
1789  print_int("code", err);
1790  print_str("string", errbuf_ptr);
1792 }
1793 
1794 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1795 {
1796  int err, i;
1799 
1800  if ((err = avformat_open_input(&fmt_ctx, filename,
1801  iformat, &format_opts)) < 0) {
1802  print_error(filename, err);
1803  return err;
1804  }
1806  av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1807  return AVERROR_OPTION_NOT_FOUND;
1808  }
1809 
1810 
1811  /* fill the streams in the format context */
1812  if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1813  print_error(filename, err);
1814  return err;
1815  }
1816 
1817  av_dump_format(fmt_ctx, 0, filename, 0);
1818 
1819  /* bind a decoder to each input stream */
1820  for (i = 0; i < fmt_ctx->nb_streams; i++) {
1821  AVStream *stream = fmt_ctx->streams[i];
1822  AVCodec *codec;
1823 
1824  if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
1826  "Failed to probe codec for input stream %d\n",
1827  stream->index);
1828  } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1830  "Unsupported codec with id %d for input stream %d\n",
1831  stream->codec->codec_id, stream->index);
1832  } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1833  av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1834  stream->index);
1835  }
1836  }
1837 
1838  *fmt_ctx_ptr = fmt_ctx;
1839  return 0;
1840 }
1841 
1842 static void close_input_file(AVFormatContext **ctx_ptr)
1843 {
1844  int i;
1845  AVFormatContext *fmt_ctx = *ctx_ptr;
1846 
1847  /* close decoder for each stream */
1848  for (i = 0; i < fmt_ctx->nb_streams; i++)
1849  if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
1850  avcodec_close(fmt_ctx->streams[i]->codec);
1851 
1852  avformat_close_input(ctx_ptr);
1853 }
1854 
1855 static int probe_file(WriterContext *wctx, const char *filename)
1856 {
1858  int ret, i;
1859  int section_id;
1860 
1863 
1864  ret = open_input_file(&fmt_ctx, filename);
1865  if (ret >= 0) {
1868  selected_streams = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
1869 
1870  for (i = 0; i < fmt_ctx->nb_streams; i++) {
1871  if (stream_specifier) {
1872  ret = avformat_match_stream_specifier(fmt_ctx,
1873  fmt_ctx->streams[i],
1875  if (ret < 0)
1876  goto end;
1877  else
1878  selected_streams[i] = ret;
1879  } else {
1880  selected_streams[i] = 1;
1881  }
1882  }
1883 
1887  section_id = SECTION_ID_PACKETS_AND_FRAMES;
1888  else if (do_show_packets && !do_show_frames)
1889  section_id = SECTION_ID_PACKETS;
1890  else // (!do_show_packets && do_show_frames)
1891  section_id = SECTION_ID_FRAMES;
1893  writer_print_section_header(wctx, section_id);
1894  read_packets(wctx, fmt_ctx);
1897  }
1898  if (do_show_streams)
1899  show_streams(wctx, fmt_ctx);
1900  if (do_show_format)
1901  show_format(wctx, fmt_ctx);
1902 
1903  end:
1904  close_input_file(&fmt_ctx);
1908  }
1909  return ret;
1910 }
1911 
1912 static void show_usage(void)
1913 {
1914  av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1915  av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1916  av_log(NULL, AV_LOG_INFO, "\n");
1917 }
1918 
1920 {
1921  AVBPrint pbuf;
1923 
1925  print_str("version", FFMPEG_VERSION);
1926  print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
1928  print_str("build_date", __DATE__);
1929  print_str("build_time", __TIME__);
1930  print_str("compiler_ident", CC_IDENT);
1931  print_str("configuration", FFMPEG_CONFIGURATION);
1933 
1934  av_bprint_finalize(&pbuf, NULL);
1935 }
1936 
1937 #define SHOW_LIB_VERSION(libname, LIBNAME) \
1938  do { \
1939  if (CONFIG_##LIBNAME) { \
1940  unsigned int version = libname##_version(); \
1941  writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
1942  print_str("name", "lib" #libname); \
1943  print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
1944  print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
1945  print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
1946  print_int("version", version); \
1947  print_str("ident", LIB##LIBNAME##_IDENT); \
1948  writer_print_section_footer(w); \
1949  } \
1950  } while (0)
1951 
1953 {
1955  SHOW_LIB_VERSION(avutil, AVUTIL);
1956  SHOW_LIB_VERSION(avcodec, AVCODEC);
1957  SHOW_LIB_VERSION(avformat, AVFORMAT);
1958  SHOW_LIB_VERSION(avdevice, AVDEVICE);
1959  SHOW_LIB_VERSION(avfilter, AVFILTER);
1960  SHOW_LIB_VERSION(swscale, SWSCALE);
1961  SHOW_LIB_VERSION(swresample, SWRESAMPLE);
1962  SHOW_LIB_VERSION(postproc, POSTPROC);
1964 }
1965 
1966 static int opt_format(void *optctx, const char *opt, const char *arg)
1967 {
1968  iformat = av_find_input_format(arg);
1969  if (!iformat) {
1970  av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
1971  return AVERROR(EINVAL);
1972  }
1973  return 0;
1974 }
1975 
1976 static inline void mark_section_show_entries(SectionID section_id,
1977  int show_all_entries, AVDictionary *entries)
1978 {
1979  struct section *section = &sections[section_id];
1980 
1982  if (show_all_entries) {
1983  SectionID *id;
1984  for (id = section->children_ids; *id != -1; id++)
1985  mark_section_show_entries(*id, show_all_entries, entries);
1986  } else {
1987  av_dict_copy(&section->entries_to_show, entries, 0);
1988  }
1989 }
1990 
1991 static int match_section(const char *section_name,
1992  int show_all_entries, AVDictionary *entries)
1993 {
1994  int i, ret = 0;
1995 
1996  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
1997  const struct section *section = &sections[i];
1998  if (!strcmp(section_name, section->name) ||
1999  (section->unique_name && !strcmp(section_name, section->unique_name))) {
2001  "'%s' matches section with unique name '%s'\n", section_name,
2002  (char *)av_x_if_null(section->unique_name, section->name));
2003  ret++;
2004  mark_section_show_entries(section->id, show_all_entries, entries);
2005  }
2006  }
2007  return ret;
2008 }
2009 
2010 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
2011 {
2012  const char *p = arg;
2013  int ret = 0;
2014 
2015  while (*p) {
2016  AVDictionary *entries = NULL;
2017  char *section_name = av_get_token(&p, "=:");
2018  int show_all_entries = 0;
2019 
2020  if (!section_name) {
2022  "Missing section name for option '%s'\n", opt);
2023  return AVERROR(EINVAL);
2024  }
2025 
2026  if (*p == '=') {
2027  p++;
2028  while (*p && *p != ':') {
2029  char *entry = av_get_token(&p, ",:");
2030  if (!entry)
2031  break;
2033  "Adding '%s' to the entries to show in section '%s'\n",
2034  entry, section_name);
2035  av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
2036  if (*p == ',')
2037  p++;
2038  }
2039  } else {
2040  show_all_entries = 1;
2041  }
2042 
2043  ret = match_section(section_name, show_all_entries, entries);
2044  if (ret == 0) {
2045  av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
2046  ret = AVERROR(EINVAL);
2047  }
2048  av_dict_free(&entries);
2049  av_free(section_name);
2050 
2051  if (ret <= 0)
2052  break;
2053  if (*p)
2054  p++;
2055  }
2056 
2057  return ret;
2058 }
2059 
2060 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
2061 {
2062  char *buf = av_asprintf("format=%s", arg);
2063  int ret;
2064 
2066  "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
2067  opt, arg);
2068  ret = opt_show_entries(optctx, opt, buf);
2069  av_free(buf);
2070  return ret;
2071 }
2072 
2073 static void opt_input_file(void *optctx, const char *arg)
2074 {
2075  if (input_filename) {
2077  "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2078  arg, input_filename);
2079  exit(1);
2080  }
2081  if (!strcmp(arg, "-"))
2082  arg = "pipe:";
2083  input_filename = arg;
2084 }
2085 
2086 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2087 {
2088  opt_input_file(optctx, arg);
2089  return 0;
2090 }
2091 
2092 void show_help_default(const char *opt, const char *arg)
2093 {
2095  show_usage();
2096  show_help_options(options, "Main options:", 0, 0, 0);
2097  printf("\n");
2098 
2100 }
2101 
2102 static int opt_pretty(void *optctx, const char *opt, const char *arg)
2103 {
2104  show_value_unit = 1;
2105  use_value_prefix = 1;
2108  return 0;
2109 }
2110 
2111 static void print_section(SectionID id, int level)
2112 {
2113  const SectionID *pid;
2114  const struct section *section = &sections[id];
2115  printf("%c%c%c",
2116  section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
2117  section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
2118  section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
2119  printf("%*c %s", level * 4, ' ', section->name);
2120  if (section->unique_name)
2121  printf("/%s", section->unique_name);
2122  printf("\n");
2123 
2124  for (pid = section->children_ids; *pid != -1; pid++)
2125  print_section(*pid, level+1);
2126 }
2127 
2128 static int opt_sections(void *optctx, const char *opt, const char *arg)
2129 {
2130  printf("Sections:\n"
2131  "W.. = Section is a wrapper (contains other sections, no local entries)\n"
2132  ".A. = Section contains an array of elements of the same type\n"
2133  "..V = Section may contain a variable number of fields with variable keys\n"
2134  "FLAGS NAME/UNIQUE_NAME\n"
2135  "---\n");
2137  return 0;
2138 }
2139 
2140 static int opt_show_versions(const char *opt, const char *arg)
2141 {
2144  return 0;
2145 }
2146 
2147 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
2148  static int opt_show_##section(const char *opt, const char *arg) \
2149  { \
2150  mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
2151  return 0; \
2152  }
2153 
2154 DEFINE_OPT_SHOW_SECTION(error, ERROR);
2155 DEFINE_OPT_SHOW_SECTION(format, FORMAT);
2156 DEFINE_OPT_SHOW_SECTION(frames, FRAMES);
2157 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS);
2158 DEFINE_OPT_SHOW_SECTION(packets, PACKETS);
2159 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION);
2160 DEFINE_OPT_SHOW_SECTION(streams, STREAMS);
2161 
2162 static const OptionDef real_options[] = {
2163 #include "cmdutils_common_opts.h"
2164  { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
2165  { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
2166  { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
2167  { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
2168  "use binary prefixes for byte units" },
2169  { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
2170  "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2171  { "pretty", 0, {.func_arg = opt_pretty},
2172  "prettify the format of displayed values, make it more human readable" },
2173  { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2174  "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2175  { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2176  { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
2177  { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
2178  { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2179  { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
2180  { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
2181  { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
2182  { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
2183  "show a particular entry from the format/container info", "entry" },
2184  { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
2185  "show a set of specified entries", "entry_list" },
2186  { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
2187  { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
2188  { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2189  { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2190  { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
2191  { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
2192  { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2193  { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2194  { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2195  { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
2196  { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
2197  { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
2198  { NULL, },
2199 };
2200 
2201 static inline int check_section_show_entries(int section_id)
2202 {
2203  int *id;
2204  struct section *section = &sections[section_id];
2205  if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
2206  return 1;
2207  for (id = section->children_ids; *id != -1; id++)
2208  if (check_section_show_entries(*id))
2209  return 1;
2210  return 0;
2211 }
2212 
2213 #define SET_DO_SHOW(id, varname) do { \
2214  if (check_section_show_entries(SECTION_ID_##id)) \
2215  do_show_##varname = 1; \
2216  } while (0)
2217 
2218 int main(int argc, char **argv)
2219 {
2220  const Writer *w;
2221  WriterContext *wctx;
2222  char *buf;
2223  char *w_name = NULL, *w_args = NULL;
2224  int ret, i;
2225 
2227  atexit(exit_program);
2228 
2229  options = real_options;
2230  parse_loglevel(argc, argv, options);
2231  av_register_all();
2233  init_opts();
2234 #if CONFIG_AVDEVICE
2236 #endif
2237 
2238  show_banner(argc, argv, options);
2239  parse_options(NULL, argc, argv, options, opt_input_file);
2240 
2241  /* mark things to show, based on -show_entries */
2242  SET_DO_SHOW(ERROR, error);
2243  SET_DO_SHOW(FORMAT, format);
2244  SET_DO_SHOW(FRAMES, frames);
2245  SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
2246  SET_DO_SHOW(PACKETS, packets);
2247  SET_DO_SHOW(PROGRAM_VERSION, program_version);
2248  SET_DO_SHOW(STREAMS, streams);
2249  SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
2250 
2253  "-bitexact and -show_program_version or -show_library_versions "
2254  "options are incompatible\n");
2255  ret = AVERROR(EINVAL);
2256  goto end;
2257  }
2258 
2260 
2261  if (!print_format)
2262  print_format = av_strdup("default");
2263  if (!print_format) {
2264  ret = AVERROR(ENOMEM);
2265  goto end;
2266  }
2267  w_name = av_strtok(print_format, "=", &buf);
2268  w_args = buf;
2269 
2270  w = writer_get_by_name(w_name);
2271  if (!w) {
2272  av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2273  ret = AVERROR(EINVAL);
2274  goto end;
2275  }
2276 
2277  if ((ret = writer_open(&wctx, w, w_args,
2278  sections, FF_ARRAY_ELEMS(sections))) >= 0) {
2280 
2285 
2286  if (!input_filename &&
2289  show_usage();
2290  av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2291  av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2292  ret = AVERROR(EINVAL);
2293  } else if (input_filename) {
2294  ret = probe_file(wctx, input_filename);
2295  if (ret < 0 && do_show_error)
2296  show_error(wctx, ret);
2297  }
2298 
2300  writer_close(&wctx);
2301  }
2302 
2303 end:
2305 
2306  uninit_opts();
2307  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
2308  av_dict_free(&(sections[i].entries_to_show));
2309 
2311 
2312  return ret;
2313 }