annotate ffmpeg/ffprobe.c @ 13:844d341cf643 tip

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