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