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/mastering_display_metadata.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/replaygain.h"
34 #include "libavutil/stereo3d.h"
38 #define HEXDUMP_PRINT(...) \
41 av_log(avcl, level, __VA_ARGS__); \
43 fprintf(f, __VA_ARGS__); \
46 static void hex_dump_internal(void *avcl, FILE *f, int level,
47 const uint8_t *buf, int size)
51 for (i = 0; i < size; i += 16) {
55 HEXDUMP_PRINT("%08x ", i);
56 for (j = 0; j < 16; j++) {
58 HEXDUMP_PRINT(" %02x", buf[i + j]);
63 for (j = 0; j < len; j++) {
65 if (c < ' ' || c > '~')
67 HEXDUMP_PRINT("%c", c);
73 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
75 hex_dump_internal(NULL, f, 0, buf, size);
78 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
80 hex_dump_internal(avcl, NULL, level, buf, size);
83 static void pkt_dump_internal(void *avcl, FILE *f, int level, const AVPacket *pkt,
84 int dump_payload, AVRational time_base)
86 HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
87 HEXDUMP_PRINT(" keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
88 HEXDUMP_PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
89 /* DTS is _always_ valid after av_read_frame() */
90 HEXDUMP_PRINT(" dts=");
91 if (pkt->dts == AV_NOPTS_VALUE)
94 HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
95 /* PTS may not be known if B-frames are present. */
96 HEXDUMP_PRINT(" pts=");
97 if (pkt->pts == AV_NOPTS_VALUE)
100 HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
102 HEXDUMP_PRINT(" size=%d\n", pkt->size);
104 hex_dump_internal(avcl, f, level, pkt->data, pkt->size);
107 void av_pkt_dump2(FILE *f, const AVPacket *pkt, int dump_payload, const AVStream *st)
109 pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
112 void av_pkt_dump_log2(void *avcl, int level, const AVPacket *pkt, int dump_payload,
115 pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
119 static void print_fps(double d, const char *postfix)
121 uint64_t v = lrintf(d * 100);
123 av_log(NULL, AV_LOG_INFO, "%1.4f %s", d, postfix);
125 av_log(NULL, AV_LOG_INFO, "%3.2f %s", d, postfix);
126 else if (v % (100 * 1000))
127 av_log(NULL, AV_LOG_INFO, "%1.0f %s", d, postfix);
129 av_log(NULL, AV_LOG_INFO, "%1.0fk %s", d / 1000, postfix);
132 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
134 if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
135 AVDictionaryEntry *tag = NULL;
137 av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
138 while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
139 if (strcmp("language", tag->key)) {
140 const char *p = tag->value;
141 av_log(ctx, AV_LOG_INFO,
142 "%s %-16s: ", indent, tag->key);
145 size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
146 av_strlcpy(tmp, p, FFMIN(sizeof(tmp), len+1));
147 av_log(ctx, AV_LOG_INFO, "%s", tmp);
149 if (*p == 0xd) av_log(ctx, AV_LOG_INFO, " ");
150 if (*p == 0xa) av_log(ctx, AV_LOG_INFO, "\n%s %-16s: ", indent, "");
153 av_log(ctx, AV_LOG_INFO, "\n");
158 /* param change side data*/
159 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
162 const uint8_t *data = sd->data;
163 uint32_t flags, channels, sample_rate, width, height;
166 if (!data || sd->size < 4)
169 flags = AV_RL32(data);
173 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
176 channels = AV_RL32(data);
179 av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
181 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
184 layout = AV_RL64(data);
187 av_log(ctx, AV_LOG_INFO,
188 "channel layout: %s, ", av_get_channel_name(layout));
190 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
193 sample_rate = AV_RL32(data);
196 av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
198 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
201 width = AV_RL32(data);
204 height = AV_RL32(data);
207 av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
212 av_log(ctx, AV_LOG_INFO, "unknown param");
215 /* replaygain side data*/
216 static void print_gain(void *ctx, const char *str, int32_t gain)
218 av_log(ctx, AV_LOG_INFO, "%s - ", str);
219 if (gain == INT32_MIN)
220 av_log(ctx, AV_LOG_INFO, "unknown");
222 av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
223 av_log(ctx, AV_LOG_INFO, ", ");
226 static void print_peak(void *ctx, const char *str, uint32_t peak)
228 av_log(ctx, AV_LOG_INFO, "%s - ", str);
230 av_log(ctx, AV_LOG_INFO, "unknown");
232 av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
233 av_log(ctx, AV_LOG_INFO, ", ");
236 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
240 if (sd->size < sizeof(*rg)) {
241 av_log(ctx, AV_LOG_INFO, "invalid data");
244 rg = (AVReplayGain*)sd->data;
246 print_gain(ctx, "track gain", rg->track_gain);
247 print_peak(ctx, "track peak", rg->track_peak);
248 print_gain(ctx, "album gain", rg->album_gain);
249 print_peak(ctx, "album peak", rg->album_peak);
252 static void dump_stereo3d(void *ctx, AVPacketSideData *sd)
256 if (sd->size < sizeof(*stereo)) {
257 av_log(ctx, AV_LOG_INFO, "invalid data");
261 stereo = (AVStereo3D *)sd->data;
263 av_log(ctx, AV_LOG_INFO, "%s", av_stereo3d_type_name(stereo->type));
265 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
266 av_log(ctx, AV_LOG_INFO, " (inverted)");
269 static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
271 enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
273 if (sd->size < sizeof(*ast)) {
274 av_log(ctx, AV_LOG_INFO, "invalid data");
279 case AV_AUDIO_SERVICE_TYPE_MAIN:
280 av_log(ctx, AV_LOG_INFO, "main");
282 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
283 av_log(ctx, AV_LOG_INFO, "effects");
285 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
286 av_log(ctx, AV_LOG_INFO, "visually impaired");
288 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
289 av_log(ctx, AV_LOG_INFO, "hearing impaired");
291 case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
292 av_log(ctx, AV_LOG_INFO, "dialogue");
294 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
295 av_log(ctx, AV_LOG_INFO, "comentary");
297 case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
298 av_log(ctx, AV_LOG_INFO, "emergency");
300 case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
301 av_log(ctx, AV_LOG_INFO, "voice over");
303 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
304 av_log(ctx, AV_LOG_INFO, "karaoke");
307 av_log(ctx, AV_LOG_WARNING, "unknown");
312 static void dump_cpb(void *ctx, AVPacketSideData *sd)
314 AVCPBProperties *cpb = (AVCPBProperties *)sd->data;
316 if (sd->size < sizeof(*cpb)) {
317 av_log(ctx, AV_LOG_INFO, "invalid data");
321 av_log(ctx, AV_LOG_INFO,
322 "bitrate max/min/avg: %d/%d/%d buffer size: %d vbv_delay: %"PRId64,
323 cpb->max_bitrate, cpb->min_bitrate, cpb->avg_bitrate,
328 static void dump_mastering_display_metadata(void *ctx, AVPacketSideData* sd) {
329 AVMasteringDisplayMetadata* metadata = (AVMasteringDisplayMetadata*)sd->data;
330 av_log(ctx, AV_LOG_INFO, "Mastering Display Metadata, "
331 "has_primaries:%d has_luminance:%d "
332 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f) "
333 "min_luminance=%f, max_luminance=%f\n",
334 metadata->has_primaries, metadata->has_luminance,
335 av_q2d(metadata->display_primaries[0][0]),
336 av_q2d(metadata->display_primaries[0][1]),
337 av_q2d(metadata->display_primaries[1][0]),
338 av_q2d(metadata->display_primaries[1][1]),
339 av_q2d(metadata->display_primaries[2][0]),
340 av_q2d(metadata->display_primaries[2][1]),
341 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]),
342 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
345 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
349 if (st->nb_side_data)
350 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
352 for (i = 0; i < st->nb_side_data; i++) {
353 AVPacketSideData sd = st->side_data[i];
354 av_log(ctx, AV_LOG_INFO, "%s ", indent);
357 case AV_PKT_DATA_PALETTE:
358 av_log(ctx, AV_LOG_INFO, "palette");
360 case AV_PKT_DATA_NEW_EXTRADATA:
361 av_log(ctx, AV_LOG_INFO, "new extradata");
363 case AV_PKT_DATA_PARAM_CHANGE:
364 av_log(ctx, AV_LOG_INFO, "paramchange: ");
365 dump_paramchange(ctx, &sd);
367 case AV_PKT_DATA_H263_MB_INFO:
368 av_log(ctx, AV_LOG_INFO, "H.263 macroblock info");
370 case AV_PKT_DATA_REPLAYGAIN:
371 av_log(ctx, AV_LOG_INFO, "replaygain: ");
372 dump_replaygain(ctx, &sd);
374 case AV_PKT_DATA_DISPLAYMATRIX:
375 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
376 av_display_rotation_get((int32_t *)sd.data));
378 case AV_PKT_DATA_STEREO3D:
379 av_log(ctx, AV_LOG_INFO, "stereo3d: ");
380 dump_stereo3d(ctx, &sd);
382 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
383 av_log(ctx, AV_LOG_INFO, "audio service type: ");
384 dump_audioservicetype(ctx, &sd);
386 case AV_PKT_DATA_QUALITY_STATS:
387 av_log(ctx, AV_LOG_INFO, "quality factor: %d, pict_type: %c", AV_RL32(sd.data), av_get_picture_type_char(sd.data[4]));
389 case AV_PKT_DATA_CPB_PROPERTIES:
390 av_log(ctx, AV_LOG_INFO, "cpb: ");
393 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA:
394 dump_mastering_display_metadata(ctx, &sd);
397 av_log(ctx, AV_LOG_INFO,
398 "unknown side data type %d (%d bytes)", sd.type, sd.size);
402 av_log(ctx, AV_LOG_INFO, "\n");
406 /* "user interface" functions */
407 static void dump_stream_format(AVFormatContext *ic, int i,
408 int index, int is_output)
411 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
412 AVStream *st = ic->streams[i];
413 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
414 char *separator = ic->dump_separator;
415 AVCodecContext *avctx;
418 avctx = avcodec_alloc_context3(NULL);
422 ret = avcodec_parameters_to_context(avctx, st->codecpar);
424 avcodec_free_context(&avctx);
428 // Fields which are missing from AVCodecParameters need to be taken from the AVCodecContext
429 avctx->properties = st->codec->properties;
430 avctx->codec = st->codec->codec;
431 avctx->qmin = st->codec->qmin;
432 avctx->qmax = st->codec->qmax;
433 avctx->coded_width = st->codec->coded_width;
434 avctx->coded_height = st->codec->coded_height;
437 av_opt_set(avctx, "dump_separator", separator, 0);
438 avcodec_string(buf, sizeof(buf), avctx, is_output);
439 avcodec_free_context(&avctx);
441 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
443 /* the pid is an important information, so we display it */
444 /* XXX: add a generic system */
445 if (flags & AVFMT_SHOW_IDS)
446 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
448 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
449 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
450 st->time_base.num, st->time_base.den);
451 av_log(NULL, AV_LOG_INFO, ": %s", buf);
453 if (st->sample_aspect_ratio.num &&
454 av_cmp_q(st->sample_aspect_ratio, st->codecpar->sample_aspect_ratio)) {
455 AVRational display_aspect_ratio;
456 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
457 st->codecpar->width * (int64_t)st->sample_aspect_ratio.num,
458 st->codecpar->height * (int64_t)st->sample_aspect_ratio.den,
460 av_log(NULL, AV_LOG_INFO, ", SAR %d:%d DAR %d:%d",
461 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
462 display_aspect_ratio.num, display_aspect_ratio.den);
465 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
466 int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
467 int tbr = st->r_frame_rate.den && st->r_frame_rate.num;
468 int tbn = st->time_base.den && st->time_base.num;
469 int tbc = st->codec->time_base.den && st->codec->time_base.num;
471 if (fps || tbr || tbn || tbc)
472 av_log(NULL, AV_LOG_INFO, "%s", separator);
475 print_fps(av_q2d(st->avg_frame_rate), tbr || tbn || tbc ? "fps, " : "fps");
477 print_fps(av_q2d(st->r_frame_rate), tbn || tbc ? "tbr, " : "tbr");
479 print_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
481 print_fps(1 / av_q2d(st->codec->time_base), "tbc");
484 if (st->disposition & AV_DISPOSITION_DEFAULT)
485 av_log(NULL, AV_LOG_INFO, " (default)");
486 if (st->disposition & AV_DISPOSITION_DUB)
487 av_log(NULL, AV_LOG_INFO, " (dub)");
488 if (st->disposition & AV_DISPOSITION_ORIGINAL)
489 av_log(NULL, AV_LOG_INFO, " (original)");
490 if (st->disposition & AV_DISPOSITION_COMMENT)
491 av_log(NULL, AV_LOG_INFO, " (comment)");
492 if (st->disposition & AV_DISPOSITION_LYRICS)
493 av_log(NULL, AV_LOG_INFO, " (lyrics)");
494 if (st->disposition & AV_DISPOSITION_KARAOKE)
495 av_log(NULL, AV_LOG_INFO, " (karaoke)");
496 if (st->disposition & AV_DISPOSITION_FORCED)
497 av_log(NULL, AV_LOG_INFO, " (forced)");
498 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
499 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
500 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
501 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
502 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
503 av_log(NULL, AV_LOG_INFO, " (clean effects)");
504 av_log(NULL, AV_LOG_INFO, "\n");
506 dump_metadata(NULL, st->metadata, " ");
508 dump_sidedata(NULL, st, " ");
511 void av_dump_format(AVFormatContext *ic, int index,
512 const char *url, int is_output)
515 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
516 if (ic->nb_streams && !printed)
519 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
520 is_output ? "Output" : "Input",
522 is_output ? ic->oformat->name : ic->iformat->name,
523 is_output ? "to" : "from", url);
524 dump_metadata(NULL, ic->metadata, " ");
527 av_log(NULL, AV_LOG_INFO, " Duration: ");
528 if (ic->duration != AV_NOPTS_VALUE) {
529 int hours, mins, secs, us;
530 int64_t duration = ic->duration + (ic->duration <= INT64_MAX - 5000 ? 5000 : 0);
531 secs = duration / AV_TIME_BASE;
532 us = duration % AV_TIME_BASE;
537 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
538 (100 * us) / AV_TIME_BASE);
540 av_log(NULL, AV_LOG_INFO, "N/A");
542 if (ic->start_time != AV_NOPTS_VALUE) {
544 av_log(NULL, AV_LOG_INFO, ", start: ");
545 secs = llabs(ic->start_time / AV_TIME_BASE);
546 us = llabs(ic->start_time % AV_TIME_BASE);
547 av_log(NULL, AV_LOG_INFO, "%s%d.%06d",
548 ic->start_time >= 0 ? "" : "-",
550 (int) av_rescale(us, 1000000, AV_TIME_BASE));
552 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
554 av_log(NULL, AV_LOG_INFO, "%"PRId64" kb/s", (int64_t)ic->bit_rate / 1000);
556 av_log(NULL, AV_LOG_INFO, "N/A");
557 av_log(NULL, AV_LOG_INFO, "\n");
560 for (i = 0; i < ic->nb_chapters; i++) {
561 AVChapter *ch = ic->chapters[i];
562 av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
563 av_log(NULL, AV_LOG_INFO,
564 "start %f, ", ch->start * av_q2d(ch->time_base));
565 av_log(NULL, AV_LOG_INFO,
566 "end %f\n", ch->end * av_q2d(ch->time_base));
568 dump_metadata(NULL, ch->metadata, " ");
571 if (ic->nb_programs) {
573 for (j = 0; j < ic->nb_programs; j++) {
574 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
576 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
577 name ? name->value : "");
578 dump_metadata(NULL, ic->programs[j]->metadata, " ");
579 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
580 dump_stream_format(ic, ic->programs[j]->stream_index[k],
582 printed[ic->programs[j]->stream_index[k]] = 1;
584 total += ic->programs[j]->nb_stream_indexes;
586 if (total < ic->nb_streams)
587 av_log(NULL, AV_LOG_INFO, " No Program\n");
590 for (i = 0; i < ic->nb_streams; i++)
592 dump_stream_format(ic, i, index, is_output);