2 * Various pretty-printing functions for use within FFmpeg
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/display.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/log.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/replaygain.h"
33 #include "libavutil/stereo3d.h"
37 #define HEXDUMP_PRINT(...) \
40 av_log(avcl, level, __VA_ARGS__); \
42 fprintf(f, __VA_ARGS__); \
45 static void hex_dump_internal(void *avcl, FILE *f, int level,
46 const uint8_t *buf, int size)
50 for (i = 0; i < size; i += 16) {
54 HEXDUMP_PRINT("%08x ", i);
55 for (j = 0; j < 16; j++) {
57 HEXDUMP_PRINT(" %02x", buf[i + j]);
62 for (j = 0; j < len; j++) {
64 if (c < ' ' || c > '~')
66 HEXDUMP_PRINT("%c", c);
72 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
74 hex_dump_internal(NULL, f, 0, buf, size);
77 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
79 hex_dump_internal(avcl, NULL, level, buf, size);
82 static void pkt_dump_internal(void *avcl, FILE *f, int level, const AVPacket *pkt,
83 int dump_payload, AVRational time_base)
85 HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
86 HEXDUMP_PRINT(" keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
87 HEXDUMP_PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
88 /* DTS is _always_ valid after av_read_frame() */
89 HEXDUMP_PRINT(" dts=");
90 if (pkt->dts == AV_NOPTS_VALUE)
93 HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
94 /* PTS may not be known if B-frames are present. */
95 HEXDUMP_PRINT(" pts=");
96 if (pkt->pts == AV_NOPTS_VALUE)
99 HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
101 HEXDUMP_PRINT(" size=%d\n", pkt->size);
103 av_hex_dump(f, pkt->data, pkt->size);
106 void av_pkt_dump2(FILE *f, const AVPacket *pkt, int dump_payload, const AVStream *st)
108 pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
111 void av_pkt_dump_log2(void *avcl, int level, const AVPacket *pkt, int dump_payload,
114 pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
118 static void print_fps(double d, const char *postfix)
120 uint64_t v = lrintf(d * 100);
122 av_log(NULL, AV_LOG_INFO, "%1.4f %s", d, postfix);
124 av_log(NULL, AV_LOG_INFO, "%3.2f %s", d, postfix);
125 else if (v % (100 * 1000))
126 av_log(NULL, AV_LOG_INFO, "%1.0f %s", d, postfix);
128 av_log(NULL, AV_LOG_INFO, "%1.0fk %s", d / 1000, postfix);
131 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
133 if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
134 AVDictionaryEntry *tag = NULL;
136 av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
137 while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
138 if (strcmp("language", tag->key)) {
139 const char *p = tag->value;
140 av_log(ctx, AV_LOG_INFO,
141 "%s %-16s: ", indent, tag->key);
144 size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
145 av_strlcpy(tmp, p, FFMIN(sizeof(tmp), len+1));
146 av_log(ctx, AV_LOG_INFO, "%s", tmp);
148 if (*p == 0xd) av_log(ctx, AV_LOG_INFO, " ");
149 if (*p == 0xa) av_log(ctx, AV_LOG_INFO, "\n%s %-16s: ", indent, "");
152 av_log(ctx, AV_LOG_INFO, "\n");
157 /* param change side data*/
158 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
161 const uint8_t *data = sd->data;
162 uint32_t flags, channels, sample_rate, width, height;
165 if (!data || sd->size < 4)
168 flags = AV_RL32(data);
172 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
175 channels = AV_RL32(data);
178 av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
180 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
183 layout = AV_RL64(data);
186 av_log(ctx, AV_LOG_INFO,
187 "channel layout: %s, ", av_get_channel_name(layout));
189 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
192 sample_rate = AV_RL32(data);
195 av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
197 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
200 width = AV_RL32(data);
203 height = AV_RL32(data);
206 av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
211 av_log(ctx, AV_LOG_INFO, "unknown param");
214 /* replaygain side data*/
215 static void print_gain(void *ctx, const char *str, int32_t gain)
217 av_log(ctx, AV_LOG_INFO, "%s - ", str);
218 if (gain == INT32_MIN)
219 av_log(ctx, AV_LOG_INFO, "unknown");
221 av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
222 av_log(ctx, AV_LOG_INFO, ", ");
225 static void print_peak(void *ctx, const char *str, uint32_t peak)
227 av_log(ctx, AV_LOG_INFO, "%s - ", str);
229 av_log(ctx, AV_LOG_INFO, "unknown");
231 av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
232 av_log(ctx, AV_LOG_INFO, ", ");
235 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
239 if (sd->size < sizeof(*rg)) {
240 av_log(ctx, AV_LOG_INFO, "invalid data");
243 rg = (AVReplayGain*)sd->data;
245 print_gain(ctx, "track gain", rg->track_gain);
246 print_peak(ctx, "track peak", rg->track_peak);
247 print_gain(ctx, "album gain", rg->album_gain);
248 print_peak(ctx, "album peak", rg->album_peak);
251 static void dump_stereo3d(void *ctx, AVPacketSideData *sd)
255 if (sd->size < sizeof(*stereo)) {
256 av_log(ctx, AV_LOG_INFO, "invalid data");
260 stereo = (AVStereo3D *)sd->data;
262 switch (stereo->type) {
264 av_log(ctx, AV_LOG_INFO, "2D");
266 case AV_STEREO3D_SIDEBYSIDE:
267 av_log(ctx, AV_LOG_INFO, "side by side");
269 case AV_STEREO3D_TOPBOTTOM:
270 av_log(ctx, AV_LOG_INFO, "top and bottom");
272 case AV_STEREO3D_FRAMESEQUENCE:
273 av_log(ctx, AV_LOG_INFO, "frame alternate");
275 case AV_STEREO3D_CHECKERBOARD:
276 av_log(ctx, AV_LOG_INFO, "checkerboard");
278 case AV_STEREO3D_LINES:
279 av_log(ctx, AV_LOG_INFO, "interleaved lines");
281 case AV_STEREO3D_COLUMNS:
282 av_log(ctx, AV_LOG_INFO, "interleaved columns");
284 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
285 av_log(ctx, AV_LOG_INFO, "side by side (quincunx subsampling)");
288 av_log(ctx, AV_LOG_WARNING, "unknown");
292 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
293 av_log(ctx, AV_LOG_INFO, " (inverted)");
296 static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
298 enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
300 if (sd->size < sizeof(*ast)) {
301 av_log(ctx, AV_LOG_INFO, "invalid data");
306 case AV_AUDIO_SERVICE_TYPE_MAIN:
307 av_log(ctx, AV_LOG_INFO, "main");
309 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
310 av_log(ctx, AV_LOG_INFO, "effects");
312 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
313 av_log(ctx, AV_LOG_INFO, "visually impaired");
315 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
316 av_log(ctx, AV_LOG_INFO, "hearing impaired");
318 case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
319 av_log(ctx, AV_LOG_INFO, "dialogue");
321 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
322 av_log(ctx, AV_LOG_INFO, "comentary");
324 case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
325 av_log(ctx, AV_LOG_INFO, "emergency");
327 case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
328 av_log(ctx, AV_LOG_INFO, "voice over");
330 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
331 av_log(ctx, AV_LOG_INFO, "karaoke");
334 av_log(ctx, AV_LOG_WARNING, "unknown");
339 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
343 if (st->nb_side_data)
344 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
346 for (i = 0; i < st->nb_side_data; i++) {
347 AVPacketSideData sd = st->side_data[i];
348 av_log(ctx, AV_LOG_INFO, "%s ", indent);
351 case AV_PKT_DATA_PALETTE:
352 av_log(ctx, AV_LOG_INFO, "palette");
354 case AV_PKT_DATA_NEW_EXTRADATA:
355 av_log(ctx, AV_LOG_INFO, "new extradata");
357 case AV_PKT_DATA_PARAM_CHANGE:
358 av_log(ctx, AV_LOG_INFO, "paramchange: ");
359 dump_paramchange(ctx, &sd);
361 case AV_PKT_DATA_H263_MB_INFO:
362 av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
364 case AV_PKT_DATA_REPLAYGAIN:
365 av_log(ctx, AV_LOG_INFO, "replaygain: ");
366 dump_replaygain(ctx, &sd);
368 case AV_PKT_DATA_DISPLAYMATRIX:
369 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
370 av_display_rotation_get((int32_t *)sd.data));
372 case AV_PKT_DATA_STEREO3D:
373 av_log(ctx, AV_LOG_INFO, "stereo3d: ");
374 dump_stereo3d(ctx, &sd);
376 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
377 av_log(ctx, AV_LOG_INFO, "audio service type: ");
378 dump_audioservicetype(ctx, &sd);
381 av_log(ctx, AV_LOG_WARNING,
382 "unknown side data type %d (%d bytes)", sd.type, sd.size);
386 av_log(ctx, AV_LOG_INFO, "\n");
390 /* "user interface" functions */
391 static void dump_stream_format(AVFormatContext *ic, int i,
392 int index, int is_output)
395 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
396 AVStream *st = ic->streams[i];
397 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
398 char *separator = ic->dump_separator;
399 char **codec_separator = av_opt_ptr(st->codec->av_class, st->codec, "dump_separator");
400 int use_format_separator = !*codec_separator;
402 if (use_format_separator)
403 *codec_separator = av_strdup(separator);
404 avcodec_string(buf, sizeof(buf), st->codec, is_output);
405 if (use_format_separator)
406 av_freep(codec_separator);
407 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
409 /* the pid is an important information, so we display it */
410 /* XXX: add a generic system */
411 if (flags & AVFMT_SHOW_IDS)
412 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
414 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
415 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
416 st->time_base.num, st->time_base.den);
417 av_log(NULL, AV_LOG_INFO, ": %s", buf);
419 if (st->sample_aspect_ratio.num && // default
420 av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
421 AVRational display_aspect_ratio;
422 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
423 st->codec->width * st->sample_aspect_ratio.num,
424 st->codec->height * st->sample_aspect_ratio.den,
426 av_log(NULL, AV_LOG_INFO, ", SAR %d:%d DAR %d:%d",
427 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
428 display_aspect_ratio.num, display_aspect_ratio.den);
431 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
432 int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
433 int tbr = st->r_frame_rate.den && st->r_frame_rate.num;
434 int tbn = st->time_base.den && st->time_base.num;
435 int tbc = st->codec->time_base.den && st->codec->time_base.num;
437 if (fps || tbr || tbn || tbc)
438 av_log(NULL, AV_LOG_INFO, "%s", separator);
441 print_fps(av_q2d(st->avg_frame_rate), tbr || tbn || tbc ? "fps, " : "fps");
443 print_fps(av_q2d(st->r_frame_rate), tbn || tbc ? "tbr, " : "tbr");
445 print_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
447 print_fps(1 / av_q2d(st->codec->time_base), "tbc");
450 if (st->disposition & AV_DISPOSITION_DEFAULT)
451 av_log(NULL, AV_LOG_INFO, " (default)");
452 if (st->disposition & AV_DISPOSITION_DUB)
453 av_log(NULL, AV_LOG_INFO, " (dub)");
454 if (st->disposition & AV_DISPOSITION_ORIGINAL)
455 av_log(NULL, AV_LOG_INFO, " (original)");
456 if (st->disposition & AV_DISPOSITION_COMMENT)
457 av_log(NULL, AV_LOG_INFO, " (comment)");
458 if (st->disposition & AV_DISPOSITION_LYRICS)
459 av_log(NULL, AV_LOG_INFO, " (lyrics)");
460 if (st->disposition & AV_DISPOSITION_KARAOKE)
461 av_log(NULL, AV_LOG_INFO, " (karaoke)");
462 if (st->disposition & AV_DISPOSITION_FORCED)
463 av_log(NULL, AV_LOG_INFO, " (forced)");
464 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
465 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
466 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
467 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
468 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
469 av_log(NULL, AV_LOG_INFO, " (clean effects)");
470 av_log(NULL, AV_LOG_INFO, "\n");
472 dump_metadata(NULL, st->metadata, " ");
474 dump_sidedata(NULL, st, " ");
477 void av_dump_format(AVFormatContext *ic, int index,
478 const char *url, int is_output)
481 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
482 if (ic->nb_streams && !printed)
485 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
486 is_output ? "Output" : "Input",
488 is_output ? ic->oformat->name : ic->iformat->name,
489 is_output ? "to" : "from", url);
490 dump_metadata(NULL, ic->metadata, " ");
493 av_log(NULL, AV_LOG_INFO, " Duration: ");
494 if (ic->duration != AV_NOPTS_VALUE) {
495 int hours, mins, secs, us;
496 int64_t duration = ic->duration + 5000;
497 secs = duration / AV_TIME_BASE;
498 us = duration % AV_TIME_BASE;
503 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
504 (100 * us) / AV_TIME_BASE);
506 av_log(NULL, AV_LOG_INFO, "N/A");
508 if (ic->start_time != AV_NOPTS_VALUE) {
510 av_log(NULL, AV_LOG_INFO, ", start: ");
511 secs = ic->start_time / AV_TIME_BASE;
512 us = llabs(ic->start_time % AV_TIME_BASE);
513 av_log(NULL, AV_LOG_INFO, "%d.%06d",
514 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
516 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
518 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
520 av_log(NULL, AV_LOG_INFO, "N/A");
521 av_log(NULL, AV_LOG_INFO, "\n");
524 for (i = 0; i < ic->nb_chapters; i++) {
525 AVChapter *ch = ic->chapters[i];
526 av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
527 av_log(NULL, AV_LOG_INFO,
528 "start %f, ", ch->start * av_q2d(ch->time_base));
529 av_log(NULL, AV_LOG_INFO,
530 "end %f\n", ch->end * av_q2d(ch->time_base));
532 dump_metadata(NULL, ch->metadata, " ");
535 if (ic->nb_programs) {
537 for (j = 0; j < ic->nb_programs; j++) {
538 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
540 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
541 name ? name->value : "");
542 dump_metadata(NULL, ic->programs[j]->metadata, " ");
543 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
544 dump_stream_format(ic, ic->programs[j]->stream_index[k],
546 printed[ic->programs[j]->stream_index[k]] = 1;
548 total += ic->programs[j]->nb_stream_indexes;
550 if (total < ic->nb_streams)
551 av_log(NULL, AV_LOG_INFO, " No Program\n");
554 for (i = 0; i < ic->nb_streams; i++)
556 dump_stream_format(ic, i, index, is_output);