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/stereo3d.h"
34 #define HEXDUMP_PRINT(...) \
37 av_log(avcl, level, __VA_ARGS__); \
39 fprintf(f, __VA_ARGS__); \
42 static void hex_dump_internal(void *avcl, FILE *f, int level,
43 const uint8_t *buf, int size)
47 for (i = 0; i < size; i += 16) {
51 HEXDUMP_PRINT("%08x ", i);
52 for (j = 0; j < 16; j++) {
54 HEXDUMP_PRINT(" %02x", buf[i + j]);
59 for (j = 0; j < len; j++) {
61 if (c < ' ' || c > '~')
63 HEXDUMP_PRINT("%c", c);
69 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
71 hex_dump_internal(NULL, f, 0, buf, size);
74 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
76 hex_dump_internal(avcl, NULL, level, buf, size);
79 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt,
80 int dump_payload, AVRational time_base)
82 HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
83 HEXDUMP_PRINT(" keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
84 HEXDUMP_PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
85 /* DTS is _always_ valid after av_read_frame() */
86 HEXDUMP_PRINT(" dts=");
87 if (pkt->dts == AV_NOPTS_VALUE)
90 HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
91 /* PTS may not be known if B-frames are present. */
92 HEXDUMP_PRINT(" pts=");
93 if (pkt->pts == AV_NOPTS_VALUE)
96 HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
98 HEXDUMP_PRINT(" size=%d\n", pkt->size);
100 av_hex_dump(f, pkt->data, pkt->size);
103 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
105 pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
108 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
111 pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
115 static void print_fps(double d, const char *postfix)
117 uint64_t v = lrintf(d * 100);
119 av_log(NULL, AV_LOG_INFO, "%3.2f %s", d, postfix);
120 else if (v % (100 * 1000))
121 av_log(NULL, AV_LOG_INFO, "%1.0f %s", d, postfix);
123 av_log(NULL, AV_LOG_INFO, "%1.0fk %s", d / 1000, postfix);
126 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
128 if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
129 AVDictionaryEntry *tag = NULL;
131 av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
132 while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
133 if (strcmp("language", tag->key))
134 av_log(ctx, AV_LOG_INFO,
135 "%s %-16s: %s\n", indent, tag->key, tag->value);
139 /* param change side data*/
140 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
143 const uint8_t *data = sd->data;
144 uint32_t flags, channels, sample_rate, width, height;
147 if (!data || sd->size < 4)
150 flags = AV_RL32(data);
154 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
157 channels = AV_RL32(data);
160 av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
162 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
165 layout = AV_RL64(data);
168 av_log(ctx, AV_LOG_INFO,
169 "channel layout: %s, ", av_get_channel_name(layout));
171 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
174 sample_rate = AV_RL32(data);
177 av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
179 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
182 width = AV_RL32(data);
185 height = AV_RL32(data);
188 av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
193 av_log(ctx, AV_LOG_INFO, "unknown param");
196 /* replaygain side data*/
197 static void print_gain(void *ctx, const char *str, int32_t gain)
199 av_log(ctx, AV_LOG_INFO, "%s - ", str);
200 if (gain == INT32_MIN)
201 av_log(ctx, AV_LOG_INFO, "unknown");
203 av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
204 av_log(ctx, AV_LOG_INFO, ", ");
207 static void print_peak(void *ctx, const char *str, uint32_t peak)
209 av_log(ctx, AV_LOG_INFO, "%s - ", str);
211 av_log(ctx, AV_LOG_INFO, "unknown");
213 av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
214 av_log(ctx, AV_LOG_INFO, ", ");
217 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
221 if (sd->size < sizeof(*rg)) {
222 av_log(ctx, AV_LOG_INFO, "invalid data");
225 rg = (AVReplayGain*)sd->data;
227 print_gain(ctx, "track gain", rg->track_gain);
228 print_peak(ctx, "track peak", rg->track_peak);
229 print_gain(ctx, "album gain", rg->album_gain);
230 print_peak(ctx, "album peak", rg->album_peak);
233 static void dump_stereo3d(void *ctx, AVPacketSideData *sd)
237 if (sd->size < sizeof(*stereo)) {
238 av_log(ctx, AV_LOG_INFO, "invalid data");
242 stereo = (AVStereo3D *)sd->data;
244 av_log(ctx, AV_LOG_INFO, "%s", av_stereo3d_type_name(stereo->type));
246 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
247 av_log(ctx, AV_LOG_INFO, " (inverted)");
250 static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
252 enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
254 if (sd->size < sizeof(*ast)) {
255 av_log(ctx, AV_LOG_INFO, "invalid data");
260 case AV_AUDIO_SERVICE_TYPE_MAIN:
261 av_log(ctx, AV_LOG_INFO, "main");
263 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
264 av_log(ctx, AV_LOG_INFO, "effects");
266 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
267 av_log(ctx, AV_LOG_INFO, "visually impaired");
269 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
270 av_log(ctx, AV_LOG_INFO, "hearing impaired");
272 case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
273 av_log(ctx, AV_LOG_INFO, "dialogue");
275 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
276 av_log(ctx, AV_LOG_INFO, "comentary");
278 case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
279 av_log(ctx, AV_LOG_INFO, "emergency");
281 case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
282 av_log(ctx, AV_LOG_INFO, "voice over");
284 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
285 av_log(ctx, AV_LOG_INFO, "karaoke");
288 av_log(ctx, AV_LOG_WARNING, "unknown");
293 static void dump_cpb(void *ctx, AVPacketSideData *sd)
295 AVCPBProperties *cpb = (AVCPBProperties *)sd->data;
297 if (sd->size < sizeof(*cpb)) {
298 av_log(ctx, AV_LOG_INFO, "invalid data");
302 av_log(ctx, AV_LOG_INFO,
303 "bitrate max/min/avg: %d/%d/%d buffer size: %d vbv_delay: %"PRId64,
304 cpb->max_bitrate, cpb->min_bitrate, cpb->avg_bitrate,
309 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
313 if (st->nb_side_data)
314 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
316 for (i = 0; i < st->nb_side_data; i++) {
317 AVPacketSideData sd = st->side_data[i];
318 av_log(ctx, AV_LOG_INFO, "%s ", indent);
321 case AV_PKT_DATA_PALETTE:
322 av_log(ctx, AV_LOG_INFO, "palette");
324 case AV_PKT_DATA_NEW_EXTRADATA:
325 av_log(ctx, AV_LOG_INFO, "new extradata");
327 case AV_PKT_DATA_PARAM_CHANGE:
328 av_log(ctx, AV_LOG_INFO, "paramchange: ");
329 dump_paramchange(ctx, &sd);
331 case AV_PKT_DATA_H263_MB_INFO:
332 av_log(ctx, AV_LOG_INFO, "H.263 macroblock info");
334 case AV_PKT_DATA_REPLAYGAIN:
335 av_log(ctx, AV_LOG_INFO, "replaygain: ");
336 dump_replaygain(ctx, &sd);
338 case AV_PKT_DATA_DISPLAYMATRIX:
339 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
340 av_display_rotation_get((int32_t *)sd.data));
342 case AV_PKT_DATA_STEREO3D:
343 av_log(ctx, AV_LOG_INFO, "stereo3d: ");
344 dump_stereo3d(ctx, &sd);
346 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
347 av_log(ctx, AV_LOG_INFO, "audio service type: ");
348 dump_audioservicetype(ctx, &sd);
350 case AV_PKT_DATA_QUALITY_FACTOR:
351 av_log(ctx, AV_LOG_INFO, "quality factor: %d", *(int *)sd.data);
353 case AV_PKT_DATA_CPB_PROPERTIES:
354 av_log(ctx, AV_LOG_INFO, "cpb: ");
358 av_log(ctx, AV_LOG_WARNING,
359 "unknown side data type %d (%d bytes)", sd.type, sd.size);
363 av_log(ctx, AV_LOG_INFO, "\n");
367 /* "user interface" functions */
368 static void dump_stream_format(AVFormatContext *ic, int i,
369 int index, int is_output)
372 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
373 AVStream *st = ic->streams[i];
374 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
375 AVCodecContext *avctx;
378 avctx = avcodec_alloc_context3(NULL);
382 ret = avcodec_parameters_to_context(avctx, st->codecpar);
384 avcodec_free_context(&avctx);
388 avcodec_string(buf, sizeof(buf), avctx, is_output);
389 avcodec_free_context(&avctx);
391 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
393 /* the pid is an important information, so we display it */
394 /* XXX: add a generic system */
395 if (flags & AVFMT_SHOW_IDS)
396 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
398 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
399 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
400 st->time_base.num, st->time_base.den);
401 av_log(NULL, AV_LOG_INFO, ": %s", buf);
403 if (st->sample_aspect_ratio.num) {
404 AVRational display_aspect_ratio;
405 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
406 st->codecpar->width * st->sample_aspect_ratio.num,
407 st->codecpar->height * st->sample_aspect_ratio.den,
409 av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
410 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
411 display_aspect_ratio.num, display_aspect_ratio.den);
414 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
415 int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
416 int tbn = st->time_base.den && st->time_base.num;
419 av_log(NULL, AV_LOG_INFO, "\n ");
421 print_fps(av_q2d(st->avg_frame_rate), tbn ? "fps, " : "fps");
423 print_fps(1 / av_q2d(st->time_base), "tbn");
426 if (st->disposition & AV_DISPOSITION_DEFAULT)
427 av_log(NULL, AV_LOG_INFO, " (default)");
428 if (st->disposition & AV_DISPOSITION_DUB)
429 av_log(NULL, AV_LOG_INFO, " (dub)");
430 if (st->disposition & AV_DISPOSITION_ORIGINAL)
431 av_log(NULL, AV_LOG_INFO, " (original)");
432 if (st->disposition & AV_DISPOSITION_COMMENT)
433 av_log(NULL, AV_LOG_INFO, " (comment)");
434 if (st->disposition & AV_DISPOSITION_LYRICS)
435 av_log(NULL, AV_LOG_INFO, " (lyrics)");
436 if (st->disposition & AV_DISPOSITION_KARAOKE)
437 av_log(NULL, AV_LOG_INFO, " (karaoke)");
438 if (st->disposition & AV_DISPOSITION_FORCED)
439 av_log(NULL, AV_LOG_INFO, " (forced)");
440 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
441 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
442 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
443 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
444 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
445 av_log(NULL, AV_LOG_INFO, " (clean effects)");
446 av_log(NULL, AV_LOG_INFO, "\n");
448 dump_metadata(NULL, st->metadata, " ");
450 dump_sidedata(NULL, st, " ");
453 void av_dump_format(AVFormatContext *ic, int index,
454 const char *url, int is_output)
457 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
458 if (ic->nb_streams && !printed)
461 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
462 is_output ? "Output" : "Input",
464 is_output ? ic->oformat->name : ic->iformat->name,
465 is_output ? "to" : "from", url);
466 dump_metadata(NULL, ic->metadata, " ");
469 av_log(NULL, AV_LOG_INFO, " Duration: ");
470 if (ic->duration != AV_NOPTS_VALUE) {
471 int hours, mins, secs, us;
472 secs = ic->duration / AV_TIME_BASE;
473 us = ic->duration % AV_TIME_BASE;
478 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
479 (100 * us) / AV_TIME_BASE);
481 av_log(NULL, AV_LOG_INFO, "N/A");
483 if (ic->start_time != AV_NOPTS_VALUE) {
485 av_log(NULL, AV_LOG_INFO, ", start: ");
486 secs = ic->start_time / AV_TIME_BASE;
487 us = llabs(ic->start_time % AV_TIME_BASE);
488 av_log(NULL, AV_LOG_INFO, "%d.%06d",
489 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
491 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
493 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
495 av_log(NULL, AV_LOG_INFO, "N/A");
496 av_log(NULL, AV_LOG_INFO, "\n");
499 for (i = 0; i < ic->nb_chapters; i++) {
500 AVChapter *ch = ic->chapters[i];
501 av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
502 av_log(NULL, AV_LOG_INFO,
503 "start %f, ", ch->start * av_q2d(ch->time_base));
504 av_log(NULL, AV_LOG_INFO,
505 "end %f\n", ch->end * av_q2d(ch->time_base));
507 dump_metadata(NULL, ch->metadata, " ");
510 if (ic->nb_programs) {
512 for (j = 0; j < ic->nb_programs; j++) {
513 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
515 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
516 name ? name->value : "");
517 dump_metadata(NULL, ic->programs[j]->metadata, " ");
518 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
519 dump_stream_format(ic, ic->programs[j]->stream_index[k],
521 printed[ic->programs[j]->stream_index[k]] = 1;
523 total += ic->programs[j]->nb_stream_indexes;
525 if (total < ic->nb_streams)
526 av_log(NULL, AV_LOG_INFO, " No Program\n");
529 for (i = 0; i < ic->nb_streams; i++)
531 dump_stream_format(ic, i, index, is_output);