2 * Various pretty-printing functions for use within Libav
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/display.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/log.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/replaygain.h"
30 #include "libavutil/spherical.h"
31 #include "libavutil/stereo3d.h"
35 #define HEXDUMP_PRINT(...) \
38 av_log(avcl, level, __VA_ARGS__); \
40 fprintf(f, __VA_ARGS__); \
43 static void hex_dump_internal(void *avcl, FILE *f, int level,
44 const uint8_t *buf, int size)
48 for (i = 0; i < size; i += 16) {
52 HEXDUMP_PRINT("%08x ", i);
53 for (j = 0; j < 16; j++) {
55 HEXDUMP_PRINT(" %02x", buf[i + j]);
60 for (j = 0; j < len; j++) {
62 if (c < ' ' || c > '~')
64 HEXDUMP_PRINT("%c", c);
70 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
72 hex_dump_internal(NULL, f, 0, buf, size);
75 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
77 hex_dump_internal(avcl, NULL, level, buf, size);
80 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt,
81 int dump_payload, AVRational time_base)
83 HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
84 HEXDUMP_PRINT(" keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
85 HEXDUMP_PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
86 /* DTS is _always_ valid after av_read_frame() */
87 HEXDUMP_PRINT(" dts=");
88 if (pkt->dts == AV_NOPTS_VALUE)
91 HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
92 /* PTS may not be known if B-frames are present. */
93 HEXDUMP_PRINT(" pts=");
94 if (pkt->pts == AV_NOPTS_VALUE)
97 HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
99 HEXDUMP_PRINT(" size=%d\n", pkt->size);
101 av_hex_dump(f, pkt->data, pkt->size);
104 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
106 pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
109 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
112 pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
116 static void print_fps(double d, const char *postfix)
118 uint64_t v = lrintf(d * 100);
120 av_log(NULL, AV_LOG_INFO, "%3.2f %s", d, postfix);
121 else if (v % (100 * 1000))
122 av_log(NULL, AV_LOG_INFO, "%1.0f %s", d, postfix);
124 av_log(NULL, AV_LOG_INFO, "%1.0fk %s", d / 1000, postfix);
127 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
129 if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
130 AVDictionaryEntry *tag = NULL;
132 av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
133 while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
134 if (strcmp("language", tag->key))
135 av_log(ctx, AV_LOG_INFO,
136 "%s %-16s: %s\n", indent, tag->key, tag->value);
140 /* param change side data*/
141 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
144 const uint8_t *data = sd->data;
145 uint32_t flags, channels, sample_rate, width, height;
148 if (!data || sd->size < 4)
151 flags = AV_RL32(data);
155 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
158 channels = AV_RL32(data);
161 av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
163 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
166 layout = AV_RL64(data);
169 av_log(ctx, AV_LOG_INFO,
170 "channel layout: %s, ", av_get_channel_name(layout));
172 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
175 sample_rate = AV_RL32(data);
178 av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
180 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
183 width = AV_RL32(data);
186 height = AV_RL32(data);
189 av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
194 av_log(ctx, AV_LOG_INFO, "unknown param");
197 /* replaygain side data*/
198 static void print_gain(void *ctx, const char *str, int32_t gain)
200 av_log(ctx, AV_LOG_INFO, "%s - ", str);
201 if (gain == INT32_MIN)
202 av_log(ctx, AV_LOG_INFO, "unknown");
204 av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
205 av_log(ctx, AV_LOG_INFO, ", ");
208 static void print_peak(void *ctx, const char *str, uint32_t peak)
210 av_log(ctx, AV_LOG_INFO, "%s - ", str);
212 av_log(ctx, AV_LOG_INFO, "unknown");
214 av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
215 av_log(ctx, AV_LOG_INFO, ", ");
218 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
222 if (sd->size < sizeof(*rg)) {
223 av_log(ctx, AV_LOG_INFO, "invalid data");
226 rg = (AVReplayGain*)sd->data;
228 print_gain(ctx, "track gain", rg->track_gain);
229 print_peak(ctx, "track peak", rg->track_peak);
230 print_gain(ctx, "album gain", rg->album_gain);
231 print_peak(ctx, "album peak", rg->album_peak);
234 static void dump_stereo3d(void *ctx, AVPacketSideData *sd)
238 if (sd->size < sizeof(*stereo)) {
239 av_log(ctx, AV_LOG_INFO, "invalid data");
243 stereo = (AVStereo3D *)sd->data;
245 av_log(ctx, AV_LOG_INFO, "%s", av_stereo3d_type_name(stereo->type));
247 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
248 av_log(ctx, AV_LOG_INFO, " (inverted)");
251 static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
253 enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
255 if (sd->size < sizeof(*ast)) {
256 av_log(ctx, AV_LOG_INFO, "invalid data");
261 case AV_AUDIO_SERVICE_TYPE_MAIN:
262 av_log(ctx, AV_LOG_INFO, "main");
264 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
265 av_log(ctx, AV_LOG_INFO, "effects");
267 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
268 av_log(ctx, AV_LOG_INFO, "visually impaired");
270 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
271 av_log(ctx, AV_LOG_INFO, "hearing impaired");
273 case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
274 av_log(ctx, AV_LOG_INFO, "dialogue");
276 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
277 av_log(ctx, AV_LOG_INFO, "comentary");
279 case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
280 av_log(ctx, AV_LOG_INFO, "emergency");
282 case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
283 av_log(ctx, AV_LOG_INFO, "voice over");
285 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
286 av_log(ctx, AV_LOG_INFO, "karaoke");
289 av_log(ctx, AV_LOG_WARNING, "unknown");
294 static void dump_cpb(void *ctx, AVPacketSideData *sd)
296 AVCPBProperties *cpb = (AVCPBProperties *)sd->data;
298 if (sd->size < sizeof(*cpb)) {
299 av_log(ctx, AV_LOG_INFO, "invalid data");
303 av_log(ctx, AV_LOG_INFO,
304 "bitrate max/min/avg: %d/%d/%d buffer size: %d vbv_delay: %"PRId64,
305 cpb->max_bitrate, cpb->min_bitrate, cpb->avg_bitrate,
310 static void dump_spherical(void *ctx, AVPacketSideData *sd)
312 AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
313 double yaw, pitch, roll;
315 if (sd->size < sizeof(*spherical)) {
316 av_log(ctx, AV_LOG_INFO, "invalid data");
320 if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR)
321 av_log(ctx, AV_LOG_INFO, "equirectangular ");
322 else if (spherical->projection == AV_SPHERICAL_CUBEMAP)
323 av_log(ctx, AV_LOG_INFO, "cubemap ");
325 av_log(ctx, AV_LOG_WARNING, "unknown");
329 yaw = ((double)spherical->yaw) / (1 << 16);
330 pitch = ((double)spherical->pitch) / (1 << 16);
331 roll = ((double)spherical->roll) / (1 << 16);
332 av_log(ctx, AV_LOG_INFO, "(%f/%f/%f) ", yaw, pitch, roll);
335 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
339 if (st->nb_side_data)
340 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
342 for (i = 0; i < st->nb_side_data; i++) {
343 AVPacketSideData sd = st->side_data[i];
344 av_log(ctx, AV_LOG_INFO, "%s ", indent);
347 case AV_PKT_DATA_PALETTE:
348 av_log(ctx, AV_LOG_INFO, "palette");
350 case AV_PKT_DATA_NEW_EXTRADATA:
351 av_log(ctx, AV_LOG_INFO, "new extradata");
353 case AV_PKT_DATA_PARAM_CHANGE:
354 av_log(ctx, AV_LOG_INFO, "paramchange: ");
355 dump_paramchange(ctx, &sd);
357 case AV_PKT_DATA_H263_MB_INFO:
358 av_log(ctx, AV_LOG_INFO, "H.263 macroblock info");
360 case AV_PKT_DATA_REPLAYGAIN:
361 av_log(ctx, AV_LOG_INFO, "replaygain: ");
362 dump_replaygain(ctx, &sd);
364 case AV_PKT_DATA_DISPLAYMATRIX:
365 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
366 av_display_rotation_get((int32_t *)sd.data));
368 case AV_PKT_DATA_STEREO3D:
369 av_log(ctx, AV_LOG_INFO, "stereo3d: ");
370 dump_stereo3d(ctx, &sd);
372 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
373 av_log(ctx, AV_LOG_INFO, "audio service type: ");
374 dump_audioservicetype(ctx, &sd);
376 case AV_PKT_DATA_QUALITY_FACTOR:
377 av_log(ctx, AV_LOG_INFO, "quality factor: %d", *(int *)sd.data);
379 case AV_PKT_DATA_CPB_PROPERTIES:
380 av_log(ctx, AV_LOG_INFO, "cpb: ");
383 case AV_PKT_DATA_SPHERICAL:
384 av_log(ctx, AV_LOG_INFO, "spherical: ");
385 dump_spherical(ctx, &sd);
388 av_log(ctx, AV_LOG_WARNING,
389 "unknown side data type %d (%d bytes)", sd.type, sd.size);
393 av_log(ctx, AV_LOG_INFO, "\n");
397 /* "user interface" functions */
398 static void dump_stream_format(AVFormatContext *ic, int i,
399 int index, int is_output)
402 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
403 AVStream *st = ic->streams[i];
404 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
405 AVCodecContext *avctx;
408 avctx = avcodec_alloc_context3(NULL);
412 ret = avcodec_parameters_to_context(avctx, st->codecpar);
414 avcodec_free_context(&avctx);
418 avcodec_string(buf, sizeof(buf), avctx, is_output);
419 avcodec_free_context(&avctx);
421 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
423 /* the pid is an important information, so we display it */
424 /* XXX: add a generic system */
425 if (flags & AVFMT_SHOW_IDS)
426 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
428 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
429 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
430 st->time_base.num, st->time_base.den);
431 av_log(NULL, AV_LOG_INFO, ": %s", buf);
433 if (st->sample_aspect_ratio.num) {
434 AVRational display_aspect_ratio;
435 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
436 st->codecpar->width * st->sample_aspect_ratio.num,
437 st->codecpar->height * st->sample_aspect_ratio.den,
439 av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
440 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
441 display_aspect_ratio.num, display_aspect_ratio.den);
444 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
445 int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
446 int tbn = st->time_base.den && st->time_base.num;
449 av_log(NULL, AV_LOG_INFO, "\n ");
451 print_fps(av_q2d(st->avg_frame_rate), tbn ? "fps, " : "fps");
453 print_fps(1 / av_q2d(st->time_base), "tbn");
456 if (st->disposition & AV_DISPOSITION_DEFAULT)
457 av_log(NULL, AV_LOG_INFO, " (default)");
458 if (st->disposition & AV_DISPOSITION_DUB)
459 av_log(NULL, AV_LOG_INFO, " (dub)");
460 if (st->disposition & AV_DISPOSITION_ORIGINAL)
461 av_log(NULL, AV_LOG_INFO, " (original)");
462 if (st->disposition & AV_DISPOSITION_COMMENT)
463 av_log(NULL, AV_LOG_INFO, " (comment)");
464 if (st->disposition & AV_DISPOSITION_LYRICS)
465 av_log(NULL, AV_LOG_INFO, " (lyrics)");
466 if (st->disposition & AV_DISPOSITION_KARAOKE)
467 av_log(NULL, AV_LOG_INFO, " (karaoke)");
468 if (st->disposition & AV_DISPOSITION_FORCED)
469 av_log(NULL, AV_LOG_INFO, " (forced)");
470 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
471 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
472 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
473 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
474 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
475 av_log(NULL, AV_LOG_INFO, " (clean effects)");
476 av_log(NULL, AV_LOG_INFO, "\n");
478 dump_metadata(NULL, st->metadata, " ");
480 dump_sidedata(NULL, st, " ");
483 void av_dump_format(AVFormatContext *ic, int index,
484 const char *url, int is_output)
487 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
488 if (ic->nb_streams && !printed)
491 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
492 is_output ? "Output" : "Input",
494 is_output ? ic->oformat->name : ic->iformat->name,
495 is_output ? "to" : "from", url);
496 dump_metadata(NULL, ic->metadata, " ");
499 av_log(NULL, AV_LOG_INFO, " Duration: ");
500 if (ic->duration != AV_NOPTS_VALUE) {
501 int hours, mins, secs, us;
502 secs = ic->duration / AV_TIME_BASE;
503 us = ic->duration % AV_TIME_BASE;
508 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
509 (100 * us) / AV_TIME_BASE);
511 av_log(NULL, AV_LOG_INFO, "N/A");
513 if (ic->start_time != AV_NOPTS_VALUE) {
515 av_log(NULL, AV_LOG_INFO, ", start: ");
516 secs = ic->start_time / AV_TIME_BASE;
517 us = llabs(ic->start_time % AV_TIME_BASE);
518 av_log(NULL, AV_LOG_INFO, "%d.%06d",
519 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
521 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
523 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
525 av_log(NULL, AV_LOG_INFO, "N/A");
526 av_log(NULL, AV_LOG_INFO, "\n");
529 for (i = 0; i < ic->nb_chapters; i++) {
530 AVChapter *ch = ic->chapters[i];
531 av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
532 av_log(NULL, AV_LOG_INFO,
533 "start %f, ", ch->start * av_q2d(ch->time_base));
534 av_log(NULL, AV_LOG_INFO,
535 "end %f\n", ch->end * av_q2d(ch->time_base));
537 dump_metadata(NULL, ch->metadata, " ");
540 if (ic->nb_programs) {
542 for (j = 0; j < ic->nb_programs; j++) {
543 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
545 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
546 name ? name->value : "");
547 dump_metadata(NULL, ic->programs[j]->metadata, " ");
548 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
549 dump_stream_format(ic, ic->programs[j]->stream_index[k],
551 printed[ic->programs[j]->stream_index[k]] = 1;
553 total += ic->programs[j]->nb_stream_indexes;
555 if (total < ic->nb_streams)
556 av_log(NULL, AV_LOG_INFO, " No Program\n");
559 for (i = 0; i < ic->nb_streams; i++)
561 dump_stream_format(ic, i, index, is_output);