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