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_sidedata(void *ctx, AVStream *st, const char *indent)
282 if (st->nb_side_data)
283 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
285 for (i = 0; i < st->nb_side_data; i++) {
286 AVPacketSideData sd = st->side_data[i];
287 av_log(ctx, AV_LOG_INFO, "%s ", indent);
290 case AV_PKT_DATA_PALETTE:
291 av_log(ctx, AV_LOG_INFO, "palette");
293 case AV_PKT_DATA_NEW_EXTRADATA:
294 av_log(ctx, AV_LOG_INFO, "new extradata");
296 case AV_PKT_DATA_PARAM_CHANGE:
297 av_log(ctx, AV_LOG_INFO, "paramchange: ");
298 dump_paramchange(ctx, &sd);
300 case AV_PKT_DATA_H263_MB_INFO:
301 av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
303 case AV_PKT_DATA_REPLAYGAIN:
304 av_log(ctx, AV_LOG_INFO, "replaygain: ");
305 dump_replaygain(ctx, &sd);
307 case AV_PKT_DATA_DISPLAYMATRIX:
308 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
309 av_display_rotation_get((int32_t *)sd.data));
311 case AV_PKT_DATA_STEREO3D:
312 av_log(ctx, AV_LOG_INFO, "stereo3d: ");
313 dump_stereo3d(ctx, &sd);
316 av_log(ctx, AV_LOG_WARNING,
317 "unknown side data type %d (%d bytes)", sd.type, sd.size);
321 av_log(ctx, AV_LOG_INFO, "\n");
325 /* "user interface" functions */
326 static void dump_stream_format(AVFormatContext *ic, int i,
327 int index, int is_output)
330 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
331 AVStream *st = ic->streams[i];
332 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
334 avcodec_string(buf, sizeof(buf), st->codec, is_output);
335 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
337 /* the pid is an important information, so we display it */
338 /* XXX: add a generic system */
339 if (flags & AVFMT_SHOW_IDS)
340 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
342 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
343 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
344 st->time_base.num, st->time_base.den);
345 av_log(NULL, AV_LOG_INFO, ": %s", buf);
347 if (st->sample_aspect_ratio.num && // default
348 av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
349 AVRational display_aspect_ratio;
350 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
351 st->codec->width * st->sample_aspect_ratio.num,
352 st->codec->height * st->sample_aspect_ratio.den,
354 av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
355 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
356 display_aspect_ratio.num, display_aspect_ratio.den);
359 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
360 int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
361 int tbn = st->time_base.den && st->time_base.num;
362 int tbc = st->codec->time_base.den && st->codec->time_base.num;
364 if (fps || tbn || tbc)
365 av_log(NULL, AV_LOG_INFO, "\n ");
367 print_fps(av_q2d(st->avg_frame_rate), tbn || tbc ? "fps, " : "fps");
369 print_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
371 print_fps(1 / av_q2d(st->codec->time_base), "tbc");
374 if (st->disposition & AV_DISPOSITION_DEFAULT)
375 av_log(NULL, AV_LOG_INFO, " (default)");
376 if (st->disposition & AV_DISPOSITION_DUB)
377 av_log(NULL, AV_LOG_INFO, " (dub)");
378 if (st->disposition & AV_DISPOSITION_ORIGINAL)
379 av_log(NULL, AV_LOG_INFO, " (original)");
380 if (st->disposition & AV_DISPOSITION_COMMENT)
381 av_log(NULL, AV_LOG_INFO, " (comment)");
382 if (st->disposition & AV_DISPOSITION_LYRICS)
383 av_log(NULL, AV_LOG_INFO, " (lyrics)");
384 if (st->disposition & AV_DISPOSITION_KARAOKE)
385 av_log(NULL, AV_LOG_INFO, " (karaoke)");
386 if (st->disposition & AV_DISPOSITION_FORCED)
387 av_log(NULL, AV_LOG_INFO, " (forced)");
388 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
389 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
390 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
391 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
392 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
393 av_log(NULL, AV_LOG_INFO, " (clean effects)");
394 av_log(NULL, AV_LOG_INFO, "\n");
396 dump_metadata(NULL, st->metadata, " ");
398 dump_sidedata(NULL, st, " ");
401 void av_dump_format(AVFormatContext *ic, int index,
402 const char *url, int is_output)
405 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
406 if (ic->nb_streams && !printed)
409 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
410 is_output ? "Output" : "Input",
412 is_output ? ic->oformat->name : ic->iformat->name,
413 is_output ? "to" : "from", url);
414 dump_metadata(NULL, ic->metadata, " ");
417 av_log(NULL, AV_LOG_INFO, " Duration: ");
418 if (ic->duration != AV_NOPTS_VALUE) {
419 int hours, mins, secs, us;
420 secs = ic->duration / AV_TIME_BASE;
421 us = ic->duration % AV_TIME_BASE;
426 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
427 (100 * us) / AV_TIME_BASE);
429 av_log(NULL, AV_LOG_INFO, "N/A");
431 if (ic->start_time != AV_NOPTS_VALUE) {
433 av_log(NULL, AV_LOG_INFO, ", start: ");
434 secs = ic->start_time / AV_TIME_BASE;
435 us = abs(ic->start_time % AV_TIME_BASE);
436 av_log(NULL, AV_LOG_INFO, "%d.%06d",
437 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
439 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
441 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
443 av_log(NULL, AV_LOG_INFO, "N/A");
444 av_log(NULL, AV_LOG_INFO, "\n");
447 for (i = 0; i < ic->nb_chapters; i++) {
448 AVChapter *ch = ic->chapters[i];
449 av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
450 av_log(NULL, AV_LOG_INFO,
451 "start %f, ", ch->start * av_q2d(ch->time_base));
452 av_log(NULL, AV_LOG_INFO,
453 "end %f\n", ch->end * av_q2d(ch->time_base));
455 dump_metadata(NULL, ch->metadata, " ");
458 if (ic->nb_programs) {
460 for (j = 0; j < ic->nb_programs; j++) {
461 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
463 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
464 name ? name->value : "");
465 dump_metadata(NULL, ic->programs[j]->metadata, " ");
466 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
467 dump_stream_format(ic, ic->programs[j]->stream_index[k],
469 printed[ic->programs[j]->stream_index[k]] = 1;
471 total += ic->programs[j]->nb_stream_indexes;
473 if (total < ic->nb_streams)
474 av_log(NULL, AV_LOG_INFO, " No Program\n");
477 for (i = 0; i < ic->nb_streams; i++)
479 dump_stream_format(ic, i, index, is_output);