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"
33 #define HEXDUMP_PRINT(...) \
36 av_log(avcl, level, __VA_ARGS__); \
38 fprintf(f, __VA_ARGS__); \
41 static void hex_dump_internal(void *avcl, FILE *f, int level,
42 const uint8_t *buf, int size)
46 for (i = 0; i < size; i += 16) {
50 HEXDUMP_PRINT("%08x ", i);
51 for (j = 0; j < 16; j++) {
53 HEXDUMP_PRINT(" %02x", buf[i + j]);
58 for (j = 0; j < len; j++) {
60 if (c < ' ' || c > '~')
62 HEXDUMP_PRINT("%c", c);
68 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
70 hex_dump_internal(NULL, f, 0, buf, size);
73 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
75 hex_dump_internal(avcl, NULL, level, buf, size);
78 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt,
79 int dump_payload, AVRational time_base)
81 HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
82 HEXDUMP_PRINT(" keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
83 HEXDUMP_PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
84 /* DTS is _always_ valid after av_read_frame() */
85 HEXDUMP_PRINT(" dts=");
86 if (pkt->dts == AV_NOPTS_VALUE)
89 HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
90 /* PTS may not be known if B-frames are present. */
91 HEXDUMP_PRINT(" pts=");
92 if (pkt->pts == AV_NOPTS_VALUE)
95 HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
97 HEXDUMP_PRINT(" size=%d\n", pkt->size);
99 av_hex_dump(f, pkt->data, pkt->size);
102 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
104 pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
107 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
110 pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
114 static void print_fps(double d, const char *postfix)
116 uint64_t v = lrintf(d * 100);
118 av_log(NULL, AV_LOG_INFO, ", %3.2f %s", d, postfix);
119 else if (v % (100 * 1000))
120 av_log(NULL, AV_LOG_INFO, ", %1.0f %s", d, postfix);
122 av_log(NULL, AV_LOG_INFO, ", %1.0fk %s", d / 1000, postfix);
125 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
127 if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
128 AVDictionaryEntry *tag = NULL;
130 av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
131 while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
132 if (strcmp("language", tag->key))
133 av_log(ctx, AV_LOG_INFO,
134 "%s %-16s: %s\n", indent, tag->key, tag->value);
138 /* param change side data*/
139 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
142 const uint8_t *data = sd->data;
143 uint32_t flags, channels, sample_rate, width, height;
146 if (!data || sd->size < 4)
149 flags = AV_RL32(data);
153 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
156 channels = AV_RL32(data);
159 av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
161 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
164 layout = AV_RL64(data);
167 av_log(ctx, AV_LOG_INFO,
168 "channel layout: %s, ", av_get_channel_name(layout));
170 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
173 sample_rate = AV_RL32(data);
176 av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
178 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
181 width = AV_RL32(data);
184 height = AV_RL32(data);
187 av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
192 av_log(ctx, AV_LOG_INFO, "unknown param");
195 /* replaygain side data*/
196 static void print_gain(void *ctx, const char *str, int32_t gain)
198 av_log(ctx, AV_LOG_INFO, "%s - ", str);
199 if (gain == INT32_MIN)
200 av_log(ctx, AV_LOG_INFO, "unknown");
202 av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
203 av_log(ctx, AV_LOG_INFO, ", ");
206 static void print_peak(void *ctx, const char *str, uint32_t peak)
208 av_log(ctx, AV_LOG_INFO, "%s - ", str);
210 av_log(ctx, AV_LOG_INFO, "unknown");
212 av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
213 av_log(ctx, AV_LOG_INFO, ", ");
216 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
220 if (sd->size < sizeof(*rg)) {
221 av_log(ctx, AV_LOG_INFO, "invalid data");
224 rg = (AVReplayGain*)sd->data;
226 print_gain(ctx, "track gain", rg->track_gain);
227 print_peak(ctx, "track peak", rg->track_peak);
228 print_gain(ctx, "album gain", rg->album_gain);
229 print_peak(ctx, "album peak", rg->album_peak);
232 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
236 if (st->nb_side_data)
237 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
239 for (i = 0; i < st->nb_side_data; i++) {
240 AVPacketSideData sd = st->side_data[i];
241 av_log(ctx, AV_LOG_INFO, "%s ", indent);
244 case AV_PKT_DATA_PALETTE:
245 av_log(ctx, AV_LOG_INFO, "palette");
247 case AV_PKT_DATA_NEW_EXTRADATA:
248 av_log(ctx, AV_LOG_INFO, "new extradata");
250 case AV_PKT_DATA_PARAM_CHANGE:
251 av_log(ctx, AV_LOG_INFO, "paramchange: ");
252 dump_paramchange(ctx, &sd);
254 case AV_PKT_DATA_H263_MB_INFO:
255 av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
257 case AV_PKT_DATA_REPLAYGAIN:
258 av_log(ctx, AV_LOG_INFO, "replaygain: ");
259 dump_replaygain(ctx, &sd);
261 case AV_PKT_DATA_DISPLAYMATRIX:
262 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
263 av_display_rotation_get((int32_t *)sd.data));
266 av_log(ctx, AV_LOG_WARNING,
267 "unknown side data type %d (%d bytes)", sd.type, sd.size);
271 av_log(ctx, AV_LOG_INFO, "\n");
275 /* "user interface" functions */
276 static void dump_stream_format(AVFormatContext *ic, int i,
277 int index, int is_output)
280 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
281 AVStream *st = ic->streams[i];
282 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
284 avcodec_string(buf, sizeof(buf), st->codec, is_output);
285 av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i);
287 /* the pid is an important information, so we display it */
288 /* XXX: add a generic system */
289 if (flags & AVFMT_SHOW_IDS)
290 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
292 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
293 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
294 st->time_base.num, st->time_base.den);
295 av_log(NULL, AV_LOG_INFO, ": %s", buf);
297 if (st->sample_aspect_ratio.num && // default
298 av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
299 AVRational display_aspect_ratio;
300 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
301 st->codec->width * st->sample_aspect_ratio.num,
302 st->codec->height * st->sample_aspect_ratio.den,
304 av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
305 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
306 display_aspect_ratio.num, display_aspect_ratio.den);
309 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
310 if (st->avg_frame_rate.den && st->avg_frame_rate.num)
311 print_fps(av_q2d(st->avg_frame_rate), "fps");
312 if (st->time_base.den && st->time_base.num)
313 print_fps(1 / av_q2d(st->time_base), "tbn");
314 if (st->codec->time_base.den && st->codec->time_base.num)
315 print_fps(1 / av_q2d(st->codec->time_base), "tbc");
318 if (st->disposition & AV_DISPOSITION_DEFAULT)
319 av_log(NULL, AV_LOG_INFO, " (default)");
320 if (st->disposition & AV_DISPOSITION_DUB)
321 av_log(NULL, AV_LOG_INFO, " (dub)");
322 if (st->disposition & AV_DISPOSITION_ORIGINAL)
323 av_log(NULL, AV_LOG_INFO, " (original)");
324 if (st->disposition & AV_DISPOSITION_COMMENT)
325 av_log(NULL, AV_LOG_INFO, " (comment)");
326 if (st->disposition & AV_DISPOSITION_LYRICS)
327 av_log(NULL, AV_LOG_INFO, " (lyrics)");
328 if (st->disposition & AV_DISPOSITION_KARAOKE)
329 av_log(NULL, AV_LOG_INFO, " (karaoke)");
330 if (st->disposition & AV_DISPOSITION_FORCED)
331 av_log(NULL, AV_LOG_INFO, " (forced)");
332 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
333 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
334 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
335 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
336 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
337 av_log(NULL, AV_LOG_INFO, " (clean effects)");
338 av_log(NULL, AV_LOG_INFO, "\n");
340 dump_metadata(NULL, st->metadata, " ");
342 dump_sidedata(NULL, st, " ");
345 void av_dump_format(AVFormatContext *ic, int index,
346 const char *url, int is_output)
349 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
350 if (ic->nb_streams && !printed)
353 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
354 is_output ? "Output" : "Input",
356 is_output ? ic->oformat->name : ic->iformat->name,
357 is_output ? "to" : "from", url);
358 dump_metadata(NULL, ic->metadata, " ");
361 av_log(NULL, AV_LOG_INFO, " Duration: ");
362 if (ic->duration != AV_NOPTS_VALUE) {
363 int hours, mins, secs, us;
364 secs = ic->duration / AV_TIME_BASE;
365 us = ic->duration % AV_TIME_BASE;
370 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
371 (100 * us) / AV_TIME_BASE);
373 av_log(NULL, AV_LOG_INFO, "N/A");
375 if (ic->start_time != AV_NOPTS_VALUE) {
377 av_log(NULL, AV_LOG_INFO, ", start: ");
378 secs = ic->start_time / AV_TIME_BASE;
379 us = abs(ic->start_time % AV_TIME_BASE);
380 av_log(NULL, AV_LOG_INFO, "%d.%06d",
381 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
383 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
385 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
387 av_log(NULL, AV_LOG_INFO, "N/A");
388 av_log(NULL, AV_LOG_INFO, "\n");
391 for (i = 0; i < ic->nb_chapters; i++) {
392 AVChapter *ch = ic->chapters[i];
393 av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
394 av_log(NULL, AV_LOG_INFO,
395 "start %f, ", ch->start * av_q2d(ch->time_base));
396 av_log(NULL, AV_LOG_INFO,
397 "end %f\n", ch->end * av_q2d(ch->time_base));
399 dump_metadata(NULL, ch->metadata, " ");
402 if (ic->nb_programs) {
404 for (j = 0; j < ic->nb_programs; j++) {
405 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
407 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
408 name ? name->value : "");
409 dump_metadata(NULL, ic->programs[j]->metadata, " ");
410 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
411 dump_stream_format(ic, ic->programs[j]->stream_index[k],
413 printed[ic->programs[j]->stream_index[k]] = 1;
415 total += ic->programs[j]->nb_stream_indexes;
417 if (total < ic->nb_streams)
418 av_log(NULL, AV_LOG_INFO, " No Program\n");
421 for (i = 0; i < ic->nb_streams; i++)
423 dump_stream_format(ic, i, index, is_output);