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 switch (stereo->type) {
246 av_log(ctx, AV_LOG_INFO, "2D");
248 case AV_STEREO3D_SIDEBYSIDE:
249 av_log(ctx, AV_LOG_INFO, "side by side");
251 case AV_STEREO3D_TOPBOTTOM:
252 av_log(ctx, AV_LOG_INFO, "top and bottom");
254 case AV_STEREO3D_FRAMESEQUENCE:
255 av_log(ctx, AV_LOG_INFO, "frame alternate");
257 case AV_STEREO3D_CHECKERBOARD:
258 av_log(ctx, AV_LOG_INFO, "checkerboard");
260 case AV_STEREO3D_LINES:
261 av_log(ctx, AV_LOG_INFO, "interleaved lines");
263 case AV_STEREO3D_COLUMNS:
264 av_log(ctx, AV_LOG_INFO, "interleaved columns");
266 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
267 av_log(ctx, AV_LOG_INFO, "side by side (quincunx subsampling)");
270 av_log(ctx, AV_LOG_WARNING, "unknown");
274 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
275 av_log(ctx, AV_LOG_INFO, " (inverted)");
278 static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
280 enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
282 if (sd->size < sizeof(*ast)) {
283 av_log(ctx, AV_LOG_INFO, "invalid data");
288 case AV_AUDIO_SERVICE_TYPE_MAIN:
289 av_log(ctx, AV_LOG_INFO, "main");
291 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
292 av_log(ctx, AV_LOG_INFO, "effects");
294 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
295 av_log(ctx, AV_LOG_INFO, "visually impaired");
297 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
298 av_log(ctx, AV_LOG_INFO, "hearing impaired");
300 case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
301 av_log(ctx, AV_LOG_INFO, "dialogue");
303 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
304 av_log(ctx, AV_LOG_INFO, "comentary");
306 case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
307 av_log(ctx, AV_LOG_INFO, "emergency");
309 case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
310 av_log(ctx, AV_LOG_INFO, "voice over");
312 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
313 av_log(ctx, AV_LOG_INFO, "karaoke");
316 av_log(ctx, AV_LOG_WARNING, "unknown");
321 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
325 if (st->nb_side_data)
326 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
328 for (i = 0; i < st->nb_side_data; i++) {
329 AVPacketSideData sd = st->side_data[i];
330 av_log(ctx, AV_LOG_INFO, "%s ", indent);
333 case AV_PKT_DATA_PALETTE:
334 av_log(ctx, AV_LOG_INFO, "palette");
336 case AV_PKT_DATA_NEW_EXTRADATA:
337 av_log(ctx, AV_LOG_INFO, "new extradata");
339 case AV_PKT_DATA_PARAM_CHANGE:
340 av_log(ctx, AV_LOG_INFO, "paramchange: ");
341 dump_paramchange(ctx, &sd);
343 case AV_PKT_DATA_H263_MB_INFO:
344 av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
346 case AV_PKT_DATA_REPLAYGAIN:
347 av_log(ctx, AV_LOG_INFO, "replaygain: ");
348 dump_replaygain(ctx, &sd);
350 case AV_PKT_DATA_DISPLAYMATRIX:
351 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
352 av_display_rotation_get((int32_t *)sd.data));
354 case AV_PKT_DATA_STEREO3D:
355 av_log(ctx, AV_LOG_INFO, "stereo3d: ");
356 dump_stereo3d(ctx, &sd);
358 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
359 av_log(ctx, AV_LOG_INFO, "audio service type: ");
360 dump_audioservicetype(ctx, &sd);
362 case AV_PKT_DATA_QUALITY_FACTOR:
363 av_log(ctx, AV_LOG_INFO, "quality factor: %d", *(int *)sd.data);
366 av_log(ctx, AV_LOG_WARNING,
367 "unknown side data type %d (%d bytes)", sd.type, sd.size);
371 av_log(ctx, AV_LOG_INFO, "\n");
375 /* "user interface" functions */
376 static void dump_stream_format(AVFormatContext *ic, int i,
377 int index, int is_output)
380 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
381 AVStream *st = ic->streams[i];
382 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
384 avcodec_string(buf, sizeof(buf), st->codec, is_output);
385 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
387 /* the pid is an important information, so we display it */
388 /* XXX: add a generic system */
389 if (flags & AVFMT_SHOW_IDS)
390 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
392 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
393 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
394 st->time_base.num, st->time_base.den);
395 av_log(NULL, AV_LOG_INFO, ": %s", buf);
397 if (st->sample_aspect_ratio.num && // default
398 av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
399 AVRational display_aspect_ratio;
400 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
401 st->codec->width * st->sample_aspect_ratio.num,
402 st->codec->height * st->sample_aspect_ratio.den,
404 av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
405 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
406 display_aspect_ratio.num, display_aspect_ratio.den);
409 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
410 int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
411 int tbn = st->time_base.den && st->time_base.num;
412 int tbc = st->codec->time_base.den && st->codec->time_base.num;
414 if (fps || tbn || tbc)
415 av_log(NULL, AV_LOG_INFO, "\n ");
417 print_fps(av_q2d(st->avg_frame_rate), tbn || tbc ? "fps, " : "fps");
419 print_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
421 print_fps(1 / av_q2d(st->codec->time_base), "tbc");
424 if (st->disposition & AV_DISPOSITION_DEFAULT)
425 av_log(NULL, AV_LOG_INFO, " (default)");
426 if (st->disposition & AV_DISPOSITION_DUB)
427 av_log(NULL, AV_LOG_INFO, " (dub)");
428 if (st->disposition & AV_DISPOSITION_ORIGINAL)
429 av_log(NULL, AV_LOG_INFO, " (original)");
430 if (st->disposition & AV_DISPOSITION_COMMENT)
431 av_log(NULL, AV_LOG_INFO, " (comment)");
432 if (st->disposition & AV_DISPOSITION_LYRICS)
433 av_log(NULL, AV_LOG_INFO, " (lyrics)");
434 if (st->disposition & AV_DISPOSITION_KARAOKE)
435 av_log(NULL, AV_LOG_INFO, " (karaoke)");
436 if (st->disposition & AV_DISPOSITION_FORCED)
437 av_log(NULL, AV_LOG_INFO, " (forced)");
438 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
439 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
440 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
441 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
442 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
443 av_log(NULL, AV_LOG_INFO, " (clean effects)");
444 av_log(NULL, AV_LOG_INFO, "\n");
446 dump_metadata(NULL, st->metadata, " ");
448 dump_sidedata(NULL, st, " ");
451 void av_dump_format(AVFormatContext *ic, int index,
452 const char *url, int is_output)
455 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
456 if (ic->nb_streams && !printed)
459 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
460 is_output ? "Output" : "Input",
462 is_output ? ic->oformat->name : ic->iformat->name,
463 is_output ? "to" : "from", url);
464 dump_metadata(NULL, ic->metadata, " ");
467 av_log(NULL, AV_LOG_INFO, " Duration: ");
468 if (ic->duration != AV_NOPTS_VALUE) {
469 int hours, mins, secs, us;
470 secs = ic->duration / AV_TIME_BASE;
471 us = ic->duration % AV_TIME_BASE;
476 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
477 (100 * us) / AV_TIME_BASE);
479 av_log(NULL, AV_LOG_INFO, "N/A");
481 if (ic->start_time != AV_NOPTS_VALUE) {
483 av_log(NULL, AV_LOG_INFO, ", start: ");
484 secs = ic->start_time / AV_TIME_BASE;
485 us = llabs(ic->start_time % AV_TIME_BASE);
486 av_log(NULL, AV_LOG_INFO, "%d.%06d",
487 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
489 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
491 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
493 av_log(NULL, AV_LOG_INFO, "N/A");
494 av_log(NULL, AV_LOG_INFO, "\n");
497 for (i = 0; i < ic->nb_chapters; i++) {
498 AVChapter *ch = ic->chapters[i];
499 av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
500 av_log(NULL, AV_LOG_INFO,
501 "start %f, ", ch->start * av_q2d(ch->time_base));
502 av_log(NULL, AV_LOG_INFO,
503 "end %f\n", ch->end * av_q2d(ch->time_base));
505 dump_metadata(NULL, ch->metadata, " ");
508 if (ic->nb_programs) {
510 for (j = 0; j < ic->nb_programs; j++) {
511 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
513 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
514 name ? name->value : "");
515 dump_metadata(NULL, ic->programs[j]->metadata, " ");
516 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
517 dump_stream_format(ic, ic->programs[j]->stream_index[k],
519 printed[ic->programs[j]->stream_index[k]] = 1;
521 total += ic->programs[j]->nb_stream_indexes;
523 if (total < ic->nb_streams)
524 av_log(NULL, AV_LOG_INFO, " No Program\n");
527 for (i = 0; i < ic->nb_streams; i++)
529 dump_stream_format(ic, i, index, is_output);