00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "config.h"
00027 #include "version.h"
00028
00029 #include "libavformat/avformat.h"
00030 #include "libavcodec/avcodec.h"
00031 #include "libavutil/avstring.h"
00032 #include "libavutil/opt.h"
00033 #include "libavutil/pixdesc.h"
00034 #include "libavutil/dict.h"
00035 #include "libavdevice/avdevice.h"
00036 #include "libswscale/swscale.h"
00037 #include "libswresample/swresample.h"
00038 #include "libpostproc/postprocess.h"
00039 #include "cmdutils.h"
00040
00041 const char program_name[] = "ffprobe";
00042 const int program_birth_year = 2007;
00043
00044 static int do_show_error = 0;
00045 static int do_show_format = 0;
00046 static int do_show_frames = 0;
00047 static int do_show_packets = 0;
00048 static int do_show_streams = 0;
00049 static int do_show_program_version = 0;
00050 static int do_show_library_versions = 0;
00051
00052 static int show_value_unit = 0;
00053 static int use_value_prefix = 0;
00054 static int use_byte_value_binary_prefix = 0;
00055 static int use_value_sexagesimal_format = 0;
00056 static int show_private_data = 1;
00057
00058 static char *print_format;
00059
00060 static const OptionDef options[];
00061
00062
00063 static const char *input_filename;
00064 static AVInputFormat *iformat = NULL;
00065
00066 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
00067 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
00068
00069 static const char *unit_second_str = "s" ;
00070 static const char *unit_hertz_str = "Hz" ;
00071 static const char *unit_byte_str = "byte" ;
00072 static const char *unit_bit_per_second_str = "bit/s";
00073
00074 void av_noreturn exit_program(int ret)
00075 {
00076 exit(ret);
00077 }
00078
00079 struct unit_value {
00080 union { double d; long long int i; } val;
00081 const char *unit;
00082 };
00083
00084 static char *value_string(char *buf, int buf_size, struct unit_value uv)
00085 {
00086 double vald;
00087 int show_float = 0;
00088
00089 if (uv.unit == unit_second_str) {
00090 vald = uv.val.d;
00091 show_float = 1;
00092 } else {
00093 vald = uv.val.i;
00094 }
00095
00096 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
00097 double secs;
00098 int hours, mins;
00099 secs = vald;
00100 mins = (int)secs / 60;
00101 secs = secs - mins * 60;
00102 hours = mins / 60;
00103 mins %= 60;
00104 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
00105 } else {
00106 const char *prefix_string = "";
00107 int l;
00108
00109 if (use_value_prefix && vald > 1) {
00110 long long int index;
00111
00112 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
00113 index = (long long int) (log(vald)/log(2)) / 10;
00114 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
00115 vald /= pow(2, index * 10);
00116 prefix_string = binary_unit_prefixes[index];
00117 } else {
00118 index = (long long int) (log10(vald)) / 3;
00119 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
00120 vald /= pow(10, index * 3);
00121 prefix_string = decimal_unit_prefixes[index];
00122 }
00123 }
00124
00125 if (show_float || (use_value_prefix && vald != (long long int)vald))
00126 l = snprintf(buf, buf_size, "%f", vald);
00127 else
00128 l = snprintf(buf, buf_size, "%lld", (long long int)vald);
00129 snprintf(buf+l, buf_size-l, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
00130 prefix_string, show_value_unit ? uv.unit : "");
00131 }
00132
00133 return buf;
00134 }
00135
00136
00137
00138 typedef struct WriterContext WriterContext;
00139
00140 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
00141 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
00142
00143 typedef struct Writer {
00144 int priv_size;
00145 const char *name;
00146
00147 int (*init) (WriterContext *wctx, const char *args, void *opaque);
00148 void (*uninit)(WriterContext *wctx);
00149
00150 void (*print_header)(WriterContext *ctx);
00151 void (*print_footer)(WriterContext *ctx);
00152
00153 void (*print_chapter_header)(WriterContext *wctx, const char *);
00154 void (*print_chapter_footer)(WriterContext *wctx, const char *);
00155 void (*print_section_header)(WriterContext *wctx, const char *);
00156 void (*print_section_footer)(WriterContext *wctx, const char *);
00157 void (*print_integer) (WriterContext *wctx, const char *, long long int);
00158 void (*print_string) (WriterContext *wctx, const char *, const char *);
00159 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
00160 int flags;
00161 } Writer;
00162
00163 struct WriterContext {
00164 const AVClass *class;
00165 const Writer *writer;
00166 char *name;
00167 void *priv;
00168 unsigned int nb_item;
00169 unsigned int nb_section;
00170 unsigned int nb_chapter;
00171 };
00172
00173 static const char *writer_get_name(void *p)
00174 {
00175 WriterContext *wctx = p;
00176 return wctx->writer->name;
00177 }
00178
00179 static const AVClass writer_class = {
00180 "Writer",
00181 writer_get_name,
00182 NULL,
00183 LIBAVUTIL_VERSION_INT,
00184 };
00185
00186 static void writer_close(WriterContext **wctx)
00187 {
00188 if (!*wctx)
00189 return;
00190
00191 if ((*wctx)->writer->uninit)
00192 (*wctx)->writer->uninit(*wctx);
00193 av_freep(&((*wctx)->priv));
00194 av_freep(wctx);
00195 }
00196
00197 static int writer_open(WriterContext **wctx, const Writer *writer,
00198 const char *args, void *opaque)
00199 {
00200 int ret = 0;
00201
00202 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
00203 ret = AVERROR(ENOMEM);
00204 goto fail;
00205 }
00206
00207 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
00208 ret = AVERROR(ENOMEM);
00209 goto fail;
00210 }
00211
00212 (*wctx)->class = &writer_class;
00213 (*wctx)->writer = writer;
00214 if ((*wctx)->writer->init)
00215 ret = (*wctx)->writer->init(*wctx, args, opaque);
00216 if (ret < 0)
00217 goto fail;
00218
00219 return 0;
00220
00221 fail:
00222 writer_close(wctx);
00223 return ret;
00224 }
00225
00226 static inline void writer_print_header(WriterContext *wctx)
00227 {
00228 if (wctx->writer->print_header)
00229 wctx->writer->print_header(wctx);
00230 wctx->nb_chapter = 0;
00231 }
00232
00233 static inline void writer_print_footer(WriterContext *wctx)
00234 {
00235 if (wctx->writer->print_footer)
00236 wctx->writer->print_footer(wctx);
00237 }
00238
00239 static inline void writer_print_chapter_header(WriterContext *wctx,
00240 const char *chapter)
00241 {
00242 if (wctx->writer->print_chapter_header)
00243 wctx->writer->print_chapter_header(wctx, chapter);
00244 wctx->nb_section = 0;
00245 }
00246
00247 static inline void writer_print_chapter_footer(WriterContext *wctx,
00248 const char *chapter)
00249 {
00250 if (wctx->writer->print_chapter_footer)
00251 wctx->writer->print_chapter_footer(wctx, chapter);
00252 wctx->nb_chapter++;
00253 }
00254
00255 static inline void writer_print_section_header(WriterContext *wctx,
00256 const char *section)
00257 {
00258 if (wctx->writer->print_section_header)
00259 wctx->writer->print_section_header(wctx, section);
00260 wctx->nb_item = 0;
00261 }
00262
00263 static inline void writer_print_section_footer(WriterContext *wctx,
00264 const char *section)
00265 {
00266 if (wctx->writer->print_section_footer)
00267 wctx->writer->print_section_footer(wctx, section);
00268 wctx->nb_section++;
00269 }
00270
00271 static inline void writer_print_integer(WriterContext *wctx,
00272 const char *key, long long int val)
00273 {
00274 wctx->writer->print_integer(wctx, key, val);
00275 wctx->nb_item++;
00276 }
00277
00278 static inline void writer_print_string(WriterContext *wctx,
00279 const char *key, const char *val, int opt)
00280 {
00281 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
00282 return;
00283 wctx->writer->print_string(wctx, key, val);
00284 wctx->nb_item++;
00285 }
00286
00287 static void writer_print_time(WriterContext *wctx, const char *key,
00288 int64_t ts, const AVRational *time_base)
00289 {
00290 char buf[128];
00291
00292 if (ts == AV_NOPTS_VALUE) {
00293 writer_print_string(wctx, key, "N/A", 1);
00294 } else {
00295 double d = ts * av_q2d(*time_base);
00296 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
00297 writer_print_string(wctx, key, buf, 0);
00298 }
00299 }
00300
00301 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
00302 {
00303 if (ts == AV_NOPTS_VALUE) {
00304 writer_print_string(wctx, key, "N/A", 1);
00305 } else {
00306 writer_print_integer(wctx, key, ts);
00307 }
00308 }
00309
00310 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
00311 {
00312 wctx->writer->show_tags(wctx, dict);
00313 }
00314
00315 #define MAX_REGISTERED_WRITERS_NB 64
00316
00317 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
00318
00319 static int writer_register(const Writer *writer)
00320 {
00321 static int next_registered_writer_idx = 0;
00322
00323 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
00324 return AVERROR(ENOMEM);
00325
00326 registered_writers[next_registered_writer_idx++] = writer;
00327 return 0;
00328 }
00329
00330 static const Writer *writer_get_by_name(const char *name)
00331 {
00332 int i;
00333
00334 for (i = 0; registered_writers[i]; i++)
00335 if (!strcmp(registered_writers[i]->name, name))
00336 return registered_writers[i];
00337
00338 return NULL;
00339 }
00340
00341
00342
00343 struct print_buf {
00344 char *s;
00345 int len;
00346 };
00347
00348 static char *fast_asprintf(struct print_buf *pbuf, const char *fmt, ...)
00349 {
00350 va_list va;
00351 int len;
00352
00353 va_start(va, fmt);
00354 len = vsnprintf(NULL, 0, fmt, va);
00355 va_end(va);
00356 if (len < 0)
00357 goto fail;
00358
00359 if (pbuf->len < len) {
00360 char *p = av_realloc(pbuf->s, len + 1);
00361 if (!p)
00362 goto fail;
00363 pbuf->s = p;
00364 pbuf->len = len;
00365 }
00366
00367 va_start(va, fmt);
00368 len = vsnprintf(pbuf->s, len + 1, fmt, va);
00369 va_end(va);
00370 if (len < 0)
00371 goto fail;
00372 return pbuf->s;
00373
00374 fail:
00375 av_freep(&pbuf->s);
00376 pbuf->len = 0;
00377 return NULL;
00378 }
00379
00380 #define ESCAPE_INIT_BUF_SIZE 256
00381
00382 #define ESCAPE_CHECK_SIZE(src, size, max_size) \
00383 if (size > max_size) { \
00384 char buf[64]; \
00385 snprintf(buf, sizeof(buf), "%s", src); \
00386 av_log(log_ctx, AV_LOG_WARNING, \
00387 "String '%s...' with is too big\n", buf); \
00388 return "FFPROBE_TOO_BIG_STRING"; \
00389 }
00390
00391 #define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
00392 if (*dst_size_p < size) { \
00393 char *q = av_realloc(*dst_p, size); \
00394 if (!q) { \
00395 char buf[64]; \
00396 snprintf(buf, sizeof(buf), "%s", src); \
00397 av_log(log_ctx, AV_LOG_WARNING, \
00398 "String '%s...' could not be escaped\n", buf); \
00399 return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
00400 } \
00401 *dst_size_p = size; \
00402 *dst = q; \
00403 }
00404
00405
00406
00407
00408
00409 static void default_print_footer(WriterContext *wctx)
00410 {
00411 printf("\n");
00412 }
00413
00414 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
00415 {
00416 if (wctx->nb_chapter)
00417 printf("\n");
00418 }
00419
00420
00421 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
00422 {
00423 int i;
00424 for (i = 0; src[i] && i < dst_size-1; i++)
00425 dst[i] = av_toupper(src[i]);
00426 dst[i] = 0;
00427 return dst;
00428 }
00429
00430 static void default_print_section_header(WriterContext *wctx, const char *section)
00431 {
00432 char buf[32];
00433
00434 if (wctx->nb_section)
00435 printf("\n");
00436 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
00437 }
00438
00439 static void default_print_section_footer(WriterContext *wctx, const char *section)
00440 {
00441 char buf[32];
00442
00443 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
00444 }
00445
00446 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
00447 {
00448 printf("%s=%s\n", key, value);
00449 }
00450
00451 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
00452 {
00453 printf("%s=%lld\n", key, value);
00454 }
00455
00456 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
00457 {
00458 AVDictionaryEntry *tag = NULL;
00459 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00460 printf("TAG:");
00461 writer_print_string(wctx, tag->key, tag->value, 0);
00462 }
00463 }
00464
00465 static const Writer default_writer = {
00466 .name = "default",
00467 .print_footer = default_print_footer,
00468 .print_chapter_header = default_print_chapter_header,
00469 .print_section_header = default_print_section_header,
00470 .print_section_footer = default_print_section_footer,
00471 .print_integer = default_print_int,
00472 .print_string = default_print_str,
00473 .show_tags = default_show_tags,
00474 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
00475 };
00476
00477
00478
00483 static const char *c_escape_str(char **dst, size_t *dst_size,
00484 const char *src, const char sep, void *log_ctx)
00485 {
00486 const char *p;
00487 char *q;
00488 size_t size = 1;
00489
00490
00491 for (p = src; *p; p++, size++) {
00492 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-2);
00493 if (*p == '\n' || *p == '\r' || *p == '\\')
00494 size++;
00495 }
00496
00497 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
00498
00499 q = *dst;
00500 for (p = src; *p; p++) {
00501 switch (*src) {
00502 case '\n': *q++ = '\\'; *q++ = 'n'; break;
00503 case '\r': *q++ = '\\'; *q++ = 'r'; break;
00504 case '\\': *q++ = '\\'; *q++ = '\\'; break;
00505 default:
00506 if (*p == sep)
00507 *q++ = '\\';
00508 *q++ = *p;
00509 }
00510 }
00511 *q = 0;
00512 return *dst;
00513 }
00514
00518 static const char *csv_escape_str(char **dst, size_t *dst_size,
00519 const char *src, const char sep, void *log_ctx)
00520 {
00521 const char *p;
00522 char *q;
00523 size_t size = 1;
00524 int quote = 0;
00525
00526
00527 for (p = src; *p; p++, size++) {
00528 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-4);
00529 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
00530 if (!quote) {
00531 quote = 1;
00532 size += 2;
00533 }
00534 if (*p == '"')
00535 size++;
00536 }
00537
00538 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
00539
00540 q = *dst;
00541 p = src;
00542 if (quote)
00543 *q++ = '\"';
00544 while (*p) {
00545 if (*p == '"')
00546 *q++ = '\"';
00547 *q++ = *p++;
00548 }
00549 if (quote)
00550 *q++ = '\"';
00551 *q = 0;
00552
00553 return *dst;
00554 }
00555
00556 static const char *none_escape_str(char **dst, size_t *dst_size,
00557 const char *src, const char sep, void *log_ctx)
00558 {
00559 return src;
00560 }
00561
00562 typedef struct CompactContext {
00563 const AVClass *class;
00564 char *item_sep_str;
00565 char item_sep;
00566 int nokey;
00567 char *buf;
00568 size_t buf_size;
00569 char *escape_mode_str;
00570 const char * (*escape_str)(char **dst, size_t *dst_size,
00571 const char *src, const char sep, void *log_ctx);
00572 } CompactContext;
00573
00574 #define OFFSET(x) offsetof(CompactContext, x)
00575
00576 static const AVOption compact_options[]= {
00577 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
00578 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
00579 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00580 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00581 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
00582 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
00583 {NULL},
00584 };
00585
00586 static const char *compact_get_name(void *ctx)
00587 {
00588 return "compact";
00589 }
00590
00591 static const AVClass compact_class = {
00592 "CompactContext",
00593 compact_get_name,
00594 compact_options
00595 };
00596
00597 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
00598 {
00599 CompactContext *compact = wctx->priv;
00600 int err;
00601
00602 compact->class = &compact_class;
00603 av_opt_set_defaults(compact);
00604
00605 if (args &&
00606 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
00607 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00608 return err;
00609 }
00610 if (strlen(compact->item_sep_str) != 1) {
00611 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
00612 compact->item_sep_str);
00613 return AVERROR(EINVAL);
00614 }
00615 compact->item_sep = compact->item_sep_str[0];
00616
00617 compact->buf_size = ESCAPE_INIT_BUF_SIZE;
00618 if (!(compact->buf = av_malloc(compact->buf_size)))
00619 return AVERROR(ENOMEM);
00620
00621 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
00622 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
00623 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
00624 else {
00625 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
00626 return AVERROR(EINVAL);
00627 }
00628
00629 return 0;
00630 }
00631
00632 static av_cold void compact_uninit(WriterContext *wctx)
00633 {
00634 CompactContext *compact = wctx->priv;
00635
00636 av_freep(&compact->item_sep_str);
00637 av_freep(&compact->buf);
00638 av_freep(&compact->escape_mode_str);
00639 }
00640
00641 static void compact_print_section_header(WriterContext *wctx, const char *section)
00642 {
00643 CompactContext *compact = wctx->priv;
00644
00645 printf("%s%c", section, compact->item_sep);
00646 }
00647
00648 static void compact_print_section_footer(WriterContext *wctx, const char *section)
00649 {
00650 printf("\n");
00651 }
00652
00653 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
00654 {
00655 CompactContext *compact = wctx->priv;
00656
00657 if (wctx->nb_item) printf("%c", compact->item_sep);
00658 if (!compact->nokey)
00659 printf("%s=", key);
00660 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
00661 value, compact->item_sep, wctx));
00662 }
00663
00664 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
00665 {
00666 CompactContext *compact = wctx->priv;
00667
00668 if (wctx->nb_item) printf("%c", compact->item_sep);
00669 if (!compact->nokey)
00670 printf("%s=", key);
00671 printf("%lld", value);
00672 }
00673
00674 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
00675 {
00676 CompactContext *compact = wctx->priv;
00677 AVDictionaryEntry *tag = NULL;
00678
00679 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00680 if (wctx->nb_item) printf("%c", compact->item_sep);
00681 if (!compact->nokey)
00682 printf("tag:%s=", compact->escape_str(&compact->buf, &compact->buf_size,
00683 tag->key, compact->item_sep, wctx));
00684 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
00685 tag->value, compact->item_sep, wctx));
00686 }
00687 }
00688
00689 static const Writer compact_writer = {
00690 .name = "compact",
00691 .priv_size = sizeof(CompactContext),
00692 .init = compact_init,
00693 .uninit = compact_uninit,
00694 .print_section_header = compact_print_section_header,
00695 .print_section_footer = compact_print_section_footer,
00696 .print_integer = compact_print_int,
00697 .print_string = compact_print_str,
00698 .show_tags = compact_show_tags,
00699 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
00700 };
00701
00702
00703
00704 static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
00705 {
00706 return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
00707 }
00708
00709 static const Writer csv_writer = {
00710 .name = "csv",
00711 .priv_size = sizeof(CompactContext),
00712 .init = csv_init,
00713 .uninit = compact_uninit,
00714 .print_section_header = compact_print_section_header,
00715 .print_section_footer = compact_print_section_footer,
00716 .print_integer = compact_print_int,
00717 .print_string = compact_print_str,
00718 .show_tags = compact_show_tags,
00719 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
00720 };
00721
00722
00723
00724 typedef struct {
00725 const AVClass *class;
00726 int multiple_entries;
00727 char *buf;
00728 size_t buf_size;
00729 int print_packets_and_frames;
00730 int indent_level;
00731 int compact;
00732 const char *item_sep, *item_start_end;
00733 } JSONContext;
00734
00735 #undef OFFSET
00736 #define OFFSET(x) offsetof(JSONContext, x)
00737
00738 static const AVOption json_options[]= {
00739 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00740 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00741 { NULL }
00742 };
00743
00744 static const char *json_get_name(void *ctx)
00745 {
00746 return "json";
00747 }
00748
00749 static const AVClass json_class = {
00750 "JSONContext",
00751 json_get_name,
00752 json_options
00753 };
00754
00755 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
00756 {
00757 JSONContext *json = wctx->priv;
00758 int err;
00759
00760 json->class = &json_class;
00761 av_opt_set_defaults(json);
00762
00763 if (args &&
00764 (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
00765 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00766 return err;
00767 }
00768
00769 json->item_sep = json->compact ? ", " : ",\n";
00770 json->item_start_end = json->compact ? " " : "\n";
00771
00772 json->buf_size = ESCAPE_INIT_BUF_SIZE;
00773 if (!(json->buf = av_malloc(json->buf_size)))
00774 return AVERROR(ENOMEM);
00775
00776 return 0;
00777 }
00778
00779 static av_cold void json_uninit(WriterContext *wctx)
00780 {
00781 JSONContext *json = wctx->priv;
00782 av_freep(&json->buf);
00783 }
00784
00785 static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
00786 void *log_ctx)
00787 {
00788 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
00789 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
00790 const char *p;
00791 char *q;
00792 size_t size = 1;
00793
00794
00795 for (p = src; *p; p++) {
00796 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
00797 if (strchr(json_escape, *p)) size += 2;
00798 else if ((unsigned char)*p < 32) size += 6;
00799 else size += 1;
00800 }
00801 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
00802
00803 q = *dst;
00804 for (p = src; *p; p++) {
00805 char *s = strchr(json_escape, *p);
00806 if (s) {
00807 *q++ = '\\';
00808 *q++ = json_subst[s - json_escape];
00809 } else if ((unsigned char)*p < 32) {
00810 snprintf(q, 7, "\\u00%02x", *p & 0xff);
00811 q += 6;
00812 } else {
00813 *q++ = *p;
00814 }
00815 }
00816 *q = 0;
00817 return *dst;
00818 }
00819
00820 static void json_print_header(WriterContext *wctx)
00821 {
00822 JSONContext *json = wctx->priv;
00823 printf("{");
00824 json->indent_level++;
00825 }
00826
00827 static void json_print_footer(WriterContext *wctx)
00828 {
00829 JSONContext *json = wctx->priv;
00830 json->indent_level--;
00831 printf("\n}\n");
00832 }
00833
00834 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
00835
00836 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
00837 {
00838 JSONContext *json = wctx->priv;
00839
00840 if (wctx->nb_chapter)
00841 printf(",");
00842 printf("\n");
00843 json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
00844 !strcmp(chapter, "packets_and_frames") ||
00845 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
00846 if (json->multiple_entries) {
00847 JSON_INDENT();
00848 printf("\"%s\": [\n", json_escape_str(&json->buf, &json->buf_size, chapter, wctx));
00849 json->print_packets_and_frames = !strcmp(chapter, "packets_and_frames");
00850 json->indent_level++;
00851 }
00852 }
00853
00854 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
00855 {
00856 JSONContext *json = wctx->priv;
00857
00858 if (json->multiple_entries) {
00859 printf("\n");
00860 json->indent_level--;
00861 JSON_INDENT();
00862 printf("]");
00863 }
00864 }
00865
00866 static void json_print_section_header(WriterContext *wctx, const char *section)
00867 {
00868 JSONContext *json = wctx->priv;
00869
00870 if (wctx->nb_section)
00871 printf(",\n");
00872 JSON_INDENT();
00873 if (!json->multiple_entries)
00874 printf("\"%s\": ", section);
00875 printf("{%s", json->item_start_end);
00876 json->indent_level++;
00877
00878 if (json->print_packets_and_frames) {
00879 if (!json->compact)
00880 JSON_INDENT();
00881 printf("\"type\": \"%s\"%s", section, json->item_sep);
00882 }
00883 }
00884
00885 static void json_print_section_footer(WriterContext *wctx, const char *section)
00886 {
00887 JSONContext *json = wctx->priv;
00888
00889 printf("%s", json->item_start_end);
00890 json->indent_level--;
00891 if (!json->compact)
00892 JSON_INDENT();
00893 printf("}");
00894 }
00895
00896 static inline void json_print_item_str(WriterContext *wctx,
00897 const char *key, const char *value)
00898 {
00899 JSONContext *json = wctx->priv;
00900
00901 printf("\"%s\":", json_escape_str(&json->buf, &json->buf_size, key, wctx));
00902 printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
00903 }
00904
00905 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
00906 {
00907 JSONContext *json = wctx->priv;
00908
00909 if (wctx->nb_item) printf("%s", json->item_sep);
00910 if (!json->compact)
00911 JSON_INDENT();
00912 json_print_item_str(wctx, key, value);
00913 }
00914
00915 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
00916 {
00917 JSONContext *json = wctx->priv;
00918
00919 if (wctx->nb_item) printf("%s", json->item_sep);
00920 if (!json->compact)
00921 JSON_INDENT();
00922 printf("\"%s\": %lld",
00923 json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
00924 }
00925
00926 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
00927 {
00928 JSONContext *json = wctx->priv;
00929 AVDictionaryEntry *tag = NULL;
00930 int is_first = 1;
00931 if (!dict)
00932 return;
00933 printf("%s", json->item_sep);
00934 if (!json->compact)
00935 JSON_INDENT();
00936 printf("\"tags\": {%s", json->item_start_end);
00937 json->indent_level++;
00938 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00939 if (is_first) is_first = 0;
00940 else printf("%s", json->item_sep);
00941 if (!json->compact)
00942 JSON_INDENT();
00943 json_print_item_str(wctx, tag->key, tag->value);
00944 }
00945 json->indent_level--;
00946 printf("%s", json->item_start_end);
00947 if (!json->compact)
00948 JSON_INDENT();
00949 printf("}");
00950 }
00951
00952 static const Writer json_writer = {
00953 .name = "json",
00954 .priv_size = sizeof(JSONContext),
00955 .init = json_init,
00956 .uninit = json_uninit,
00957 .print_header = json_print_header,
00958 .print_footer = json_print_footer,
00959 .print_chapter_header = json_print_chapter_header,
00960 .print_chapter_footer = json_print_chapter_footer,
00961 .print_section_header = json_print_section_header,
00962 .print_section_footer = json_print_section_footer,
00963 .print_integer = json_print_int,
00964 .print_string = json_print_str,
00965 .show_tags = json_show_tags,
00966 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
00967 };
00968
00969
00970
00971 typedef struct {
00972 const AVClass *class;
00973 int within_tag;
00974 int multiple_entries;
00975 int indent_level;
00976 int fully_qualified;
00977 int xsd_strict;
00978 char *buf;
00979 size_t buf_size;
00980 } XMLContext;
00981
00982 #undef OFFSET
00983 #define OFFSET(x) offsetof(XMLContext, x)
00984
00985 static const AVOption xml_options[] = {
00986 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00987 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00988 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00989 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
00990 {NULL},
00991 };
00992
00993 static const char *xml_get_name(void *ctx)
00994 {
00995 return "xml";
00996 }
00997
00998 static const AVClass xml_class = {
00999 "XMLContext",
01000 xml_get_name,
01001 xml_options
01002 };
01003
01004 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
01005 {
01006 XMLContext *xml = wctx->priv;
01007 int err;
01008
01009 xml->class = &xml_class;
01010 av_opt_set_defaults(xml);
01011
01012 if (args &&
01013 (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
01014 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
01015 return err;
01016 }
01017
01018 if (xml->xsd_strict) {
01019 xml->fully_qualified = 1;
01020 #define CHECK_COMPLIANCE(opt, opt_name) \
01021 if (opt) { \
01022 av_log(wctx, AV_LOG_ERROR, \
01023 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
01024 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
01025 return AVERROR(EINVAL); \
01026 }
01027 CHECK_COMPLIANCE(show_private_data, "private");
01028 CHECK_COMPLIANCE(show_value_unit, "unit");
01029 CHECK_COMPLIANCE(use_value_prefix, "prefix");
01030
01031 if (do_show_frames && do_show_packets) {
01032 av_log(wctx, AV_LOG_ERROR,
01033 "Interleaved frames and packets are not allowed in XSD. "
01034 "Select only one between the -show_frames and the -show_packets options.\n");
01035 return AVERROR(EINVAL);
01036 }
01037 }
01038
01039 xml->buf_size = ESCAPE_INIT_BUF_SIZE;
01040 if (!(xml->buf = av_malloc(xml->buf_size)))
01041 return AVERROR(ENOMEM);
01042 return 0;
01043 }
01044
01045 static av_cold void xml_uninit(WriterContext *wctx)
01046 {
01047 XMLContext *xml = wctx->priv;
01048 av_freep(&xml->buf);
01049 }
01050
01051 static const char *xml_escape_str(char **dst, size_t *dst_size, const char *src,
01052 void *log_ctx)
01053 {
01054 const char *p;
01055 char *q;
01056 size_t size = 1;
01057
01058
01059 for (p = src; *p; p++, size++) {
01060 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-10);
01061 switch (*p) {
01062 case '&' : size += strlen("&"); break;
01063 case '<' : size += strlen("<"); break;
01064 case '>' : size += strlen(">"); break;
01065 case '\"': size += strlen("""); break;
01066 case '\'': size += strlen("'"); break;
01067 default: size++;
01068 }
01069 }
01070 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
01071
01072 #define COPY_STR(str) { \
01073 const char *s = str; \
01074 while (*s) \
01075 *q++ = *s++; \
01076 }
01077
01078 p = src;
01079 q = *dst;
01080 while (*p) {
01081 switch (*p) {
01082 case '&' : COPY_STR("&"); break;
01083 case '<' : COPY_STR("<"); break;
01084 case '>' : COPY_STR(">"); break;
01085 case '\"': COPY_STR("""); break;
01086 case '\'': COPY_STR("'"); break;
01087 default: *q++ = *p;
01088 }
01089 p++;
01090 }
01091 *q = 0;
01092
01093 return *dst;
01094 }
01095
01096 static void xml_print_header(WriterContext *wctx)
01097 {
01098 XMLContext *xml = wctx->priv;
01099 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
01100 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
01101 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
01102
01103 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
01104 printf("<%sffprobe%s>\n",
01105 xml->fully_qualified ? "ffprobe:" : "",
01106 xml->fully_qualified ? qual : "");
01107
01108 xml->indent_level++;
01109 }
01110
01111 static void xml_print_footer(WriterContext *wctx)
01112 {
01113 XMLContext *xml = wctx->priv;
01114
01115 xml->indent_level--;
01116 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
01117 }
01118
01119 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
01120
01121 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
01122 {
01123 XMLContext *xml = wctx->priv;
01124
01125 if (wctx->nb_chapter)
01126 printf("\n");
01127 xml->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames") ||
01128 !strcmp(chapter, "packets_and_frames") ||
01129 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
01130
01131 if (xml->multiple_entries) {
01132 XML_INDENT(); printf("<%s>\n", chapter);
01133 xml->indent_level++;
01134 }
01135 }
01136
01137 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
01138 {
01139 XMLContext *xml = wctx->priv;
01140
01141 if (xml->multiple_entries) {
01142 xml->indent_level--;
01143 XML_INDENT(); printf("</%s>\n", chapter);
01144 }
01145 }
01146
01147 static void xml_print_section_header(WriterContext *wctx, const char *section)
01148 {
01149 XMLContext *xml = wctx->priv;
01150
01151 XML_INDENT(); printf("<%s ", section);
01152 xml->within_tag = 1;
01153 }
01154
01155 static void xml_print_section_footer(WriterContext *wctx, const char *section)
01156 {
01157 XMLContext *xml = wctx->priv;
01158
01159 if (xml->within_tag)
01160 printf("/>\n");
01161 else {
01162 XML_INDENT(); printf("</%s>\n", section);
01163 }
01164 }
01165
01166 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
01167 {
01168 XMLContext *xml = wctx->priv;
01169
01170 if (wctx->nb_item)
01171 printf(" ");
01172 printf("%s=\"%s\"", key, xml_escape_str(&xml->buf, &xml->buf_size, value, wctx));
01173 }
01174
01175 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
01176 {
01177 if (wctx->nb_item)
01178 printf(" ");
01179 printf("%s=\"%lld\"", key, value);
01180 }
01181
01182 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
01183 {
01184 XMLContext *xml = wctx->priv;
01185 AVDictionaryEntry *tag = NULL;
01186 int is_first = 1;
01187
01188 xml->indent_level++;
01189 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
01190 if (is_first) {
01191
01192 printf(">\n");
01193 xml->within_tag = 0;
01194 is_first = 0;
01195 }
01196 XML_INDENT();
01197 printf("<tag key=\"%s\"",
01198 xml_escape_str(&xml->buf, &xml->buf_size, tag->key, wctx));
01199 printf(" value=\"%s\"/>\n",
01200 xml_escape_str(&xml->buf, &xml->buf_size, tag->value, wctx));
01201 }
01202 xml->indent_level--;
01203 }
01204
01205 static Writer xml_writer = {
01206 .name = "xml",
01207 .priv_size = sizeof(XMLContext),
01208 .init = xml_init,
01209 .uninit = xml_uninit,
01210 .print_header = xml_print_header,
01211 .print_footer = xml_print_footer,
01212 .print_chapter_header = xml_print_chapter_header,
01213 .print_chapter_footer = xml_print_chapter_footer,
01214 .print_section_header = xml_print_section_header,
01215 .print_section_footer = xml_print_section_footer,
01216 .print_integer = xml_print_int,
01217 .print_string = xml_print_str,
01218 .show_tags = xml_show_tags,
01219 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
01220 };
01221
01222 static void writer_register_all(void)
01223 {
01224 static int initialized;
01225
01226 if (initialized)
01227 return;
01228 initialized = 1;
01229
01230 writer_register(&default_writer);
01231 writer_register(&compact_writer);
01232 writer_register(&csv_writer);
01233 writer_register(&json_writer);
01234 writer_register(&xml_writer);
01235 }
01236
01237 #define print_fmt(k, f, ...) do { \
01238 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
01239 writer_print_string(w, k, pbuf.s, 0); \
01240 } while (0)
01241
01242 #define print_fmt_opt(k, f, ...) do { \
01243 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
01244 writer_print_string(w, k, pbuf.s, 1); \
01245 } while (0)
01246
01247 #define print_int(k, v) writer_print_integer(w, k, v)
01248 #define print_str(k, v) writer_print_string(w, k, v, 0)
01249 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
01250 #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
01251 #define print_ts(k, v) writer_print_ts(w, k, v)
01252 #define print_val(k, v, u) writer_print_string(w, k, \
01253 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
01254 #define print_section_header(s) writer_print_section_header(w, s)
01255 #define print_section_footer(s) writer_print_section_footer(w, s)
01256 #define show_tags(metadata) writer_show_tags(w, metadata)
01257
01258 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
01259 {
01260 char val_str[128];
01261 AVStream *st = fmt_ctx->streams[pkt->stream_index];
01262 struct print_buf pbuf = {.s = NULL};
01263 const char *s;
01264
01265 print_section_header("packet");
01266 s = av_get_media_type_string(st->codec->codec_type);
01267 if (s) print_str ("codec_type", s);
01268 else print_str_opt("codec_type", "unknown");
01269 print_int("stream_index", pkt->stream_index);
01270 print_ts ("pts", pkt->pts);
01271 print_time("pts_time", pkt->pts, &st->time_base);
01272 print_ts ("dts", pkt->dts);
01273 print_time("dts_time", pkt->dts, &st->time_base);
01274 print_ts ("duration", pkt->duration);
01275 print_time("duration_time", pkt->duration, &st->time_base);
01276 print_val("size", pkt->size, unit_byte_str);
01277 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
01278 else print_str_opt("pos", "N/A");
01279 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
01280 print_section_footer("packet");
01281
01282 av_free(pbuf.s);
01283 fflush(stdout);
01284 }
01285
01286 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
01287 {
01288 struct print_buf pbuf = {.s = NULL};
01289 const char *s;
01290
01291 print_section_header("frame");
01292
01293 s = av_get_media_type_string(stream->codec->codec_type);
01294 if (s) print_str ("media_type", s);
01295 else print_str_opt("media_type", "unknown");
01296 print_int("key_frame", frame->key_frame);
01297 print_ts ("pkt_pts", frame->pkt_pts);
01298 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
01299 print_ts ("pkt_dts", frame->pkt_dts);
01300 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
01301 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
01302 else print_str_opt("pkt_pos", "N/A");
01303
01304 switch (stream->codec->codec_type) {
01305 case AVMEDIA_TYPE_VIDEO:
01306 print_int("width", frame->width);
01307 print_int("height", frame->height);
01308 s = av_get_pix_fmt_name(frame->format);
01309 if (s) print_str ("pix_fmt", s);
01310 else print_str_opt("pix_fmt", "unknown");
01311 if (frame->sample_aspect_ratio.num) {
01312 print_fmt("sample_aspect_ratio", "%d:%d",
01313 frame->sample_aspect_ratio.num,
01314 frame->sample_aspect_ratio.den);
01315 } else {
01316 print_str_opt("sample_aspect_ratio", "N/A");
01317 }
01318 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
01319 print_int("coded_picture_number", frame->coded_picture_number);
01320 print_int("display_picture_number", frame->display_picture_number);
01321 print_int("interlaced_frame", frame->interlaced_frame);
01322 print_int("top_field_first", frame->top_field_first);
01323 print_int("repeat_pict", frame->repeat_pict);
01324 print_int("reference", frame->reference);
01325 break;
01326
01327 case AVMEDIA_TYPE_AUDIO:
01328 s = av_get_sample_fmt_name(frame->format);
01329 if (s) print_str ("sample_fmt", s);
01330 else print_str_opt("sample_fmt", "unknown");
01331 print_int("nb_samples", frame->nb_samples);
01332 break;
01333 }
01334
01335 print_section_footer("frame");
01336
01337 av_free(pbuf.s);
01338 fflush(stdout);
01339 }
01340
01341 static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
01342 AVFrame *frame, int *got_frame,
01343 AVPacket *pkt)
01344 {
01345 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
01346 int ret = 0;
01347
01348 *got_frame = 0;
01349 switch (dec_ctx->codec_type) {
01350 case AVMEDIA_TYPE_VIDEO:
01351 ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
01352 break;
01353
01354 case AVMEDIA_TYPE_AUDIO:
01355 ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
01356 break;
01357 }
01358
01359 return ret;
01360 }
01361
01362 static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
01363 {
01364 AVPacket pkt, pkt1;
01365 AVFrame frame;
01366 int i = 0, ret, got_frame;
01367
01368 av_init_packet(&pkt);
01369
01370 while (!av_read_frame(fmt_ctx, &pkt)) {
01371 if (do_show_packets)
01372 show_packet(w, fmt_ctx, &pkt, i++);
01373 if (do_show_frames) {
01374 pkt1 = pkt;
01375 while (1) {
01376 avcodec_get_frame_defaults(&frame);
01377 ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
01378 if (ret < 0 || !got_frame)
01379 break;
01380 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
01381 pkt1.data += ret;
01382 pkt1.size -= ret;
01383 }
01384 }
01385 av_free_packet(&pkt);
01386 }
01387 av_init_packet(&pkt);
01388 pkt.data = NULL;
01389 pkt.size = 0;
01390
01391 for (i = 0; i < fmt_ctx->nb_streams; i++) {
01392 pkt.stream_index = i;
01393 while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame)
01394 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
01395 }
01396 }
01397
01398 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
01399 {
01400 AVStream *stream = fmt_ctx->streams[stream_idx];
01401 AVCodecContext *dec_ctx;
01402 AVCodec *dec;
01403 char val_str[128];
01404 const char *s;
01405 AVRational display_aspect_ratio;
01406 struct print_buf pbuf = {.s = NULL};
01407
01408 print_section_header("stream");
01409
01410 print_int("index", stream->index);
01411
01412 if ((dec_ctx = stream->codec)) {
01413 if ((dec = dec_ctx->codec)) {
01414 print_str("codec_name", dec->name);
01415 print_str("codec_long_name", dec->long_name);
01416 } else {
01417 print_str_opt("codec_name", "unknown");
01418 print_str_opt("codec_long_name", "unknown");
01419 }
01420
01421 s = av_get_media_type_string(dec_ctx->codec_type);
01422 if (s) print_str ("codec_type", s);
01423 else print_str_opt("codec_type", "unknown");
01424 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
01425
01426
01427 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
01428 print_str("codec_tag_string", val_str);
01429 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
01430
01431 switch (dec_ctx->codec_type) {
01432 case AVMEDIA_TYPE_VIDEO:
01433 print_int("width", dec_ctx->width);
01434 print_int("height", dec_ctx->height);
01435 print_int("has_b_frames", dec_ctx->has_b_frames);
01436 if (dec_ctx->sample_aspect_ratio.num) {
01437 print_fmt("sample_aspect_ratio", "%d:%d",
01438 dec_ctx->sample_aspect_ratio.num,
01439 dec_ctx->sample_aspect_ratio.den);
01440 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
01441 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
01442 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
01443 1024*1024);
01444 print_fmt("display_aspect_ratio", "%d:%d",
01445 display_aspect_ratio.num,
01446 display_aspect_ratio.den);
01447 } else {
01448 print_str_opt("sample_aspect_ratio", "N/A");
01449 print_str_opt("display_aspect_ratio", "N/A");
01450 }
01451 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
01452 if (s) print_str ("pix_fmt", s);
01453 else print_str_opt("pix_fmt", "unknown");
01454 print_int("level", dec_ctx->level);
01455 if (dec_ctx->timecode_frame_start >= 0) {
01456 uint32_t tc = dec_ctx->timecode_frame_start;
01457 print_fmt("timecode", "%02d:%02d:%02d%c%02d",
01458 tc>>19 & 0x1f,
01459 tc>>13 & 0x3f,
01460 tc>>6 & 0x3f,
01461 tc & 1<<24 ? ';' : ':',
01462 tc & 0x3f);
01463 } else {
01464 print_str_opt("timecode", "N/A");
01465 }
01466 break;
01467
01468 case AVMEDIA_TYPE_AUDIO:
01469 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
01470 if (s) print_str ("sample_fmt", s);
01471 else print_str_opt("sample_fmt", "unknown");
01472 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
01473 print_int("channels", dec_ctx->channels);
01474 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
01475 break;
01476 }
01477 } else {
01478 print_str_opt("codec_type", "unknown");
01479 }
01480 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
01481 const AVOption *opt = NULL;
01482 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
01483 uint8_t *str;
01484 if (opt->flags) continue;
01485 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
01486 print_str(opt->name, str);
01487 av_free(str);
01488 }
01489 }
01490 }
01491
01492 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
01493 else print_str_opt("id", "N/A");
01494 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
01495 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
01496 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
01497 print_time("start_time", stream->start_time, &stream->time_base);
01498 print_time("duration", stream->duration, &stream->time_base);
01499 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
01500 else print_str_opt("nb_frames", "N/A");
01501 show_tags(stream->metadata);
01502
01503 print_section_footer("stream");
01504 av_free(pbuf.s);
01505 fflush(stdout);
01506 }
01507
01508 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
01509 {
01510 int i;
01511 for (i = 0; i < fmt_ctx->nb_streams; i++)
01512 show_stream(w, fmt_ctx, i);
01513 }
01514
01515 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
01516 {
01517 char val_str[128];
01518 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
01519
01520 print_section_header("format");
01521 print_str("filename", fmt_ctx->filename);
01522 print_int("nb_streams", fmt_ctx->nb_streams);
01523 print_str("format_name", fmt_ctx->iformat->name);
01524 print_str("format_long_name", fmt_ctx->iformat->long_name);
01525 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
01526 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
01527 if (size >= 0) print_val ("size", size, unit_byte_str);
01528 else print_str_opt("size", "N/A");
01529 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
01530 else print_str_opt("bit_rate", "N/A");
01531 show_tags(fmt_ctx->metadata);
01532 print_section_footer("format");
01533 fflush(stdout);
01534 }
01535
01536 static void show_error(WriterContext *w, int err)
01537 {
01538 char errbuf[128];
01539 const char *errbuf_ptr = errbuf;
01540
01541 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
01542 errbuf_ptr = strerror(AVUNERROR(err));
01543
01544 writer_print_chapter_header(w, "error");
01545 print_section_header("error");
01546 print_int("code", err);
01547 print_str("string", errbuf_ptr);
01548 print_section_footer("error");
01549 writer_print_chapter_footer(w, "error");
01550 }
01551
01552 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
01553 {
01554 int err, i;
01555 AVFormatContext *fmt_ctx = NULL;
01556 AVDictionaryEntry *t;
01557
01558 if ((err = avformat_open_input(&fmt_ctx, filename,
01559 iformat, &format_opts)) < 0) {
01560 print_error(filename, err);
01561 return err;
01562 }
01563 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
01564 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
01565 return AVERROR_OPTION_NOT_FOUND;
01566 }
01567
01568
01569
01570 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
01571 print_error(filename, err);
01572 return err;
01573 }
01574
01575 av_dump_format(fmt_ctx, 0, filename, 0);
01576
01577
01578 for (i = 0; i < fmt_ctx->nb_streams; i++) {
01579 AVStream *stream = fmt_ctx->streams[i];
01580 AVCodec *codec;
01581
01582 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
01583 av_log(NULL, AV_LOG_ERROR,
01584 "Unsupported codec with id %d for input stream %d\n",
01585 stream->codec->codec_id, stream->index);
01586 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
01587 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
01588 stream->index);
01589 }
01590 }
01591
01592 *fmt_ctx_ptr = fmt_ctx;
01593 return 0;
01594 }
01595
01596 #define PRINT_CHAPTER(name) do { \
01597 if (do_show_ ## name) { \
01598 writer_print_chapter_header(wctx, #name); \
01599 show_ ## name (wctx, fmt_ctx); \
01600 writer_print_chapter_footer(wctx, #name); \
01601 } \
01602 } while (0)
01603
01604 static int probe_file(WriterContext *wctx, const char *filename)
01605 {
01606 AVFormatContext *fmt_ctx;
01607 int ret, i;
01608
01609 ret = open_input_file(&fmt_ctx, filename);
01610 if (ret >= 0) {
01611 if (do_show_packets || do_show_frames) {
01612 const char *chapter;
01613 if (do_show_frames && do_show_packets &&
01614 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
01615 chapter = "packets_and_frames";
01616 else if (do_show_packets && !do_show_frames)
01617 chapter = "packets";
01618 else
01619 chapter = "frames";
01620 writer_print_chapter_header(wctx, chapter);
01621 show_packets(wctx, fmt_ctx);
01622 writer_print_chapter_footer(wctx, chapter);
01623 }
01624 PRINT_CHAPTER(streams);
01625 PRINT_CHAPTER(format);
01626 for (i = 0; i < fmt_ctx->nb_streams; i++)
01627 if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
01628 avcodec_close(fmt_ctx->streams[i]->codec);
01629 avformat_close_input(&fmt_ctx);
01630 }
01631
01632 return ret;
01633 }
01634
01635 static void show_usage(void)
01636 {
01637 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
01638 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
01639 av_log(NULL, AV_LOG_INFO, "\n");
01640 }
01641
01642 static void ffprobe_show_program_version(WriterContext *w)
01643 {
01644 struct print_buf pbuf = {.s = NULL};
01645
01646 writer_print_chapter_header(w, "program_version");
01647 print_section_header("program_version");
01648 print_str("version", FFMPEG_VERSION);
01649 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
01650 program_birth_year, this_year);
01651 print_str("build_date", __DATE__);
01652 print_str("build_time", __TIME__);
01653 print_str("compiler_type", CC_TYPE);
01654 print_str("compiler_version", CC_VERSION);
01655 print_str("configuration", FFMPEG_CONFIGURATION);
01656 print_section_footer("program_version");
01657 writer_print_chapter_footer(w, "program_version");
01658
01659 av_free(pbuf.s);
01660 }
01661
01662 #define SHOW_LIB_VERSION(libname, LIBNAME) \
01663 do { \
01664 if (CONFIG_##LIBNAME) { \
01665 unsigned int version = libname##_version(); \
01666 print_section_header("library_version"); \
01667 print_str("name", "lib" #libname); \
01668 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
01669 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
01670 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
01671 print_int("version", version); \
01672 print_section_footer("library_version"); \
01673 } \
01674 } while (0)
01675
01676 static void ffprobe_show_library_versions(WriterContext *w)
01677 {
01678 writer_print_chapter_header(w, "library_versions");
01679 SHOW_LIB_VERSION(avutil, AVUTIL);
01680 SHOW_LIB_VERSION(avcodec, AVCODEC);
01681 SHOW_LIB_VERSION(avformat, AVFORMAT);
01682 SHOW_LIB_VERSION(avdevice, AVDEVICE);
01683 SHOW_LIB_VERSION(avfilter, AVFILTER);
01684 SHOW_LIB_VERSION(swscale, SWSCALE);
01685 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
01686 SHOW_LIB_VERSION(postproc, POSTPROC);
01687 writer_print_chapter_footer(w, "library_versions");
01688 }
01689
01690 static int opt_format(const char *opt, const char *arg)
01691 {
01692 iformat = av_find_input_format(arg);
01693 if (!iformat) {
01694 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
01695 return AVERROR(EINVAL);
01696 }
01697 return 0;
01698 }
01699
01700 static void opt_input_file(void *optctx, const char *arg)
01701 {
01702 if (input_filename) {
01703 av_log(NULL, AV_LOG_ERROR,
01704 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
01705 arg, input_filename);
01706 exit(1);
01707 }
01708 if (!strcmp(arg, "-"))
01709 arg = "pipe:";
01710 input_filename = arg;
01711 }
01712
01713 static int opt_help(const char *opt, const char *arg)
01714 {
01715 av_log_set_callback(log_callback_help);
01716 show_usage();
01717 show_help_options(options, "Main options:\n", 0, 0);
01718 printf("\n");
01719
01720 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
01721
01722 return 0;
01723 }
01724
01725 static int opt_pretty(const char *opt, const char *arg)
01726 {
01727 show_value_unit = 1;
01728 use_value_prefix = 1;
01729 use_byte_value_binary_prefix = 1;
01730 use_value_sexagesimal_format = 1;
01731 return 0;
01732 }
01733
01734 static int opt_show_versions(const char *opt, const char *arg)
01735 {
01736 do_show_program_version = 1;
01737 do_show_library_versions = 1;
01738 return 0;
01739 }
01740
01741 static const OptionDef options[] = {
01742 #include "cmdutils_common_opts.h"
01743 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
01744 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
01745 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
01746 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
01747 "use binary prefixes for byte units" },
01748 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
01749 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
01750 { "pretty", 0, {(void*)&opt_pretty},
01751 "prettify the format of displayed values, make it more human readable" },
01752 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
01753 "set the output printing format (available formats are: default, compact, csv, json, xml)", "format" },
01754 { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
01755 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
01756 { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
01757 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
01758 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
01759 { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
01760 { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
01761 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
01762 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
01763 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
01764 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
01765 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
01766 { NULL, },
01767 };
01768
01769 int main(int argc, char **argv)
01770 {
01771 const Writer *w;
01772 WriterContext *wctx;
01773 char *buf;
01774 char *w_name = NULL, *w_args = NULL;
01775 int ret;
01776
01777 av_log_set_flags(AV_LOG_SKIP_REPEATED);
01778 parse_loglevel(argc, argv, options);
01779 av_register_all();
01780 avformat_network_init();
01781 init_opts();
01782 #if CONFIG_AVDEVICE
01783 avdevice_register_all();
01784 #endif
01785
01786 show_banner(argc, argv, options);
01787 parse_options(NULL, argc, argv, options, opt_input_file);
01788
01789 writer_register_all();
01790
01791 if (!print_format)
01792 print_format = av_strdup("default");
01793 if (!print_format) {
01794 ret = AVERROR(ENOMEM);
01795 goto end;
01796 }
01797 w_name = av_strtok(print_format, "=", &buf);
01798 w_args = buf;
01799
01800 w = writer_get_by_name(w_name);
01801 if (!w) {
01802 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
01803 ret = AVERROR(EINVAL);
01804 goto end;
01805 }
01806
01807 if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
01808 writer_print_header(wctx);
01809
01810 if (do_show_program_version)
01811 ffprobe_show_program_version(wctx);
01812 if (do_show_library_versions)
01813 ffprobe_show_library_versions(wctx);
01814
01815 if (!input_filename &&
01816 ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
01817 (!do_show_program_version && !do_show_library_versions))) {
01818 show_usage();
01819 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
01820 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
01821 ret = AVERROR(EINVAL);
01822 } else if (input_filename) {
01823 ret = probe_file(wctx, input_filename);
01824 if (ret < 0 && do_show_error)
01825 show_error(wctx, ret);
01826 }
01827
01828 writer_print_footer(wctx);
01829 writer_close(&wctx);
01830 }
01831
01832 end:
01833 av_freep(&print_format);
01834 avformat_network_deinit();
01835
01836 return ret;
01837 }