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/mathematics.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/replaygain.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, const 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, const AVPacket *pkt, int dump_payload, const 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, const 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 const char *p = tag->value;
136 av_log(ctx, AV_LOG_INFO,
137 "%s %-16s: ", indent, tag->key);
140 size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
141 av_strlcpy(tmp, p, FFMIN(sizeof(tmp), len+1));
142 av_log(ctx, AV_LOG_INFO, "%s", tmp);
144 if (*p == 0xd) av_log(ctx, AV_LOG_INFO, " ");
145 if (*p == 0xa) av_log(ctx, AV_LOG_INFO, "\n%s %-16s: ", indent, "");
148 av_log(ctx, AV_LOG_INFO, "\n");
153 /* param change side data*/
154 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
157 const uint8_t *data = sd->data;
158 uint32_t flags, channels, sample_rate, width, height;
161 if (!data || sd->size < 4)
164 flags = AV_RL32(data);
168 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
171 channels = AV_RL32(data);
174 av_log(ctx, AV_LOG_INFO, "channel count %d, ", channels);
176 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
179 layout = AV_RL64(data);
182 av_log(ctx, AV_LOG_INFO,
183 "channel layout: %s, ", av_get_channel_name(layout));
185 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
188 sample_rate = AV_RL32(data);
191 av_log(ctx, AV_LOG_INFO, "sample_rate %d, ", sample_rate);
193 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
196 width = AV_RL32(data);
199 height = AV_RL32(data);
202 av_log(ctx, AV_LOG_INFO, "width %d height %d", width, height);
207 av_log(ctx, AV_LOG_INFO, "unknown param");
210 /* replaygain side data*/
211 static void print_gain(void *ctx, const char *str, int32_t gain)
213 av_log(ctx, AV_LOG_INFO, "%s - ", str);
214 if (gain == INT32_MIN)
215 av_log(ctx, AV_LOG_INFO, "unknown");
217 av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
218 av_log(ctx, AV_LOG_INFO, ", ");
221 static void print_peak(void *ctx, const char *str, uint32_t peak)
223 av_log(ctx, AV_LOG_INFO, "%s - ", str);
225 av_log(ctx, AV_LOG_INFO, "unknown");
227 av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
228 av_log(ctx, AV_LOG_INFO, ", ");
231 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
235 if (sd->size < sizeof(*rg)) {
236 av_log(ctx, AV_LOG_INFO, "invalid data");
239 rg = (AVReplayGain*)sd->data;
241 print_gain(ctx, "track gain", rg->track_gain);
242 print_peak(ctx, "track peak", rg->track_peak);
243 print_gain(ctx, "album gain", rg->album_gain);
244 print_peak(ctx, "album peak", rg->album_peak);
247 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
251 if (st->nb_side_data)
252 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
254 for (i = 0; i < st->nb_side_data; i++) {
255 AVPacketSideData sd = st->side_data[i];
256 av_log(ctx, AV_LOG_INFO, "%s ", indent);
259 case AV_PKT_DATA_PALETTE:
260 av_log(ctx, AV_LOG_INFO, "palette");
262 case AV_PKT_DATA_NEW_EXTRADATA:
263 av_log(ctx, AV_LOG_INFO, "new extradata");
265 case AV_PKT_DATA_PARAM_CHANGE:
266 av_log(ctx, AV_LOG_INFO, "paramchange: ");
267 dump_paramchange(ctx, &sd);
269 case AV_PKT_DATA_H263_MB_INFO:
270 av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
272 case AV_PKT_DATA_REPLAYGAIN:
273 av_log(ctx, AV_LOG_INFO, "replaygain: ");
274 dump_replaygain(ctx, &sd);
276 case AV_PKT_DATA_DISPLAYMATRIX:
277 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
278 av_display_rotation_get((int32_t *)sd.data));
281 av_log(ctx, AV_LOG_WARNING,
282 "unknown side data type %d (%d bytes)", sd.type, sd.size);
286 av_log(ctx, AV_LOG_INFO, "\n");
290 /* "user interface" functions */
291 static void dump_stream_format(AVFormatContext *ic, int i,
292 int index, int is_output)
295 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
296 AVStream *st = ic->streams[i];
297 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
299 avcodec_string(buf, sizeof(buf), st->codec, is_output);
300 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
302 /* the pid is an important information, so we display it */
303 /* XXX: add a generic system */
304 if (flags & AVFMT_SHOW_IDS)
305 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
307 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
308 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
309 st->time_base.num, st->time_base.den);
310 av_log(NULL, AV_LOG_INFO, ": %s", buf);
312 if (st->sample_aspect_ratio.num && // default
313 av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
314 AVRational display_aspect_ratio;
315 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
316 st->codec->width * st->sample_aspect_ratio.num,
317 st->codec->height * st->sample_aspect_ratio.den,
319 av_log(NULL, AV_LOG_INFO, ", SAR %d:%d DAR %d:%d",
320 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
321 display_aspect_ratio.num, display_aspect_ratio.den);
324 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
325 if (st->avg_frame_rate.den && st->avg_frame_rate.num)
326 print_fps(av_q2d(st->avg_frame_rate), "fps");
327 #if FF_API_R_FRAME_RATE
328 if (st->r_frame_rate.den && st->r_frame_rate.num)
329 print_fps(av_q2d(st->r_frame_rate), "tbr");
331 if (st->time_base.den && st->time_base.num)
332 print_fps(1 / av_q2d(st->time_base), "tbn");
333 if (st->codec->time_base.den && st->codec->time_base.num)
334 print_fps(1 / av_q2d(st->codec->time_base), "tbc");
337 if (st->disposition & AV_DISPOSITION_DEFAULT)
338 av_log(NULL, AV_LOG_INFO, " (default)");
339 if (st->disposition & AV_DISPOSITION_DUB)
340 av_log(NULL, AV_LOG_INFO, " (dub)");
341 if (st->disposition & AV_DISPOSITION_ORIGINAL)
342 av_log(NULL, AV_LOG_INFO, " (original)");
343 if (st->disposition & AV_DISPOSITION_COMMENT)
344 av_log(NULL, AV_LOG_INFO, " (comment)");
345 if (st->disposition & AV_DISPOSITION_LYRICS)
346 av_log(NULL, AV_LOG_INFO, " (lyrics)");
347 if (st->disposition & AV_DISPOSITION_KARAOKE)
348 av_log(NULL, AV_LOG_INFO, " (karaoke)");
349 if (st->disposition & AV_DISPOSITION_FORCED)
350 av_log(NULL, AV_LOG_INFO, " (forced)");
351 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
352 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
353 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
354 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
355 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
356 av_log(NULL, AV_LOG_INFO, " (clean effects)");
357 av_log(NULL, AV_LOG_INFO, "\n");
359 dump_metadata(NULL, st->metadata, " ");
361 dump_sidedata(NULL, st, " ");
364 void av_dump_format(AVFormatContext *ic, int index,
365 const char *url, int is_output)
368 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
369 if (ic->nb_streams && !printed)
372 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
373 is_output ? "Output" : "Input",
375 is_output ? ic->oformat->name : ic->iformat->name,
376 is_output ? "to" : "from", url);
377 dump_metadata(NULL, ic->metadata, " ");
380 av_log(NULL, AV_LOG_INFO, " Duration: ");
381 if (ic->duration != AV_NOPTS_VALUE) {
382 int hours, mins, secs, us;
383 int64_t duration = ic->duration + 5000;
384 secs = duration / AV_TIME_BASE;
385 us = duration % AV_TIME_BASE;
390 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
391 (100 * us) / AV_TIME_BASE);
393 av_log(NULL, AV_LOG_INFO, "N/A");
395 if (ic->start_time != AV_NOPTS_VALUE) {
397 av_log(NULL, AV_LOG_INFO, ", start: ");
398 secs = ic->start_time / AV_TIME_BASE;
399 us = abs(ic->start_time % AV_TIME_BASE);
400 av_log(NULL, AV_LOG_INFO, "%d.%06d",
401 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
403 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
405 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
407 av_log(NULL, AV_LOG_INFO, "N/A");
408 av_log(NULL, AV_LOG_INFO, "\n");
411 for (i = 0; i < ic->nb_chapters; i++) {
412 AVChapter *ch = ic->chapters[i];
413 av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
414 av_log(NULL, AV_LOG_INFO,
415 "start %f, ", ch->start * av_q2d(ch->time_base));
416 av_log(NULL, AV_LOG_INFO,
417 "end %f\n", ch->end * av_q2d(ch->time_base));
419 dump_metadata(NULL, ch->metadata, " ");
422 if (ic->nb_programs) {
424 for (j = 0; j < ic->nb_programs; j++) {
425 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
427 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
428 name ? name->value : "");
429 dump_metadata(NULL, ic->programs[j]->metadata, " ");
430 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
431 dump_stream_format(ic, ic->programs[j]->stream_index[k],
433 printed[ic->programs[j]->stream_index[k]] = 1;
435 total += ic->programs[j]->nb_stream_indexes;
437 if (total < ic->nb_streams)
438 av_log(NULL, AV_LOG_INFO, " No Program\n");
441 for (i = 0; i < ic->nb_streams; i++)
443 dump_stream_format(ic, i, index, is_output);