]> git.sesse.net Git - ffmpeg/blob - libavformat/dump.c
Merge commit '5c4b98de4d101ea715ef86847306a510afd0db0c'
[ffmpeg] / libavformat / dump.c
1 /*
2  * Various pretty-printing functions for use within FFmpeg
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdio.h>
23 #include <stdint.h>
24
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/opt.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/replaygain.h"
33 #include "libavutil/stereo3d.h"
34
35 #include "avformat.h"
36
37 #define HEXDUMP_PRINT(...)                                                    \
38     do {                                                                      \
39         if (!f)                                                               \
40             av_log(avcl, level, __VA_ARGS__);                                 \
41         else                                                                  \
42             fprintf(f, __VA_ARGS__);                                          \
43     } while (0)
44
45 static void hex_dump_internal(void *avcl, FILE *f, int level,
46                               const uint8_t *buf, int size)
47 {
48     int len, i, j, c;
49
50     for (i = 0; i < size; i += 16) {
51         len = size - i;
52         if (len > 16)
53             len = 16;
54         HEXDUMP_PRINT("%08x ", i);
55         for (j = 0; j < 16; j++) {
56             if (j < len)
57                 HEXDUMP_PRINT(" %02x", buf[i + j]);
58             else
59                 HEXDUMP_PRINT("   ");
60         }
61         HEXDUMP_PRINT(" ");
62         for (j = 0; j < len; j++) {
63             c = buf[i + j];
64             if (c < ' ' || c > '~')
65                 c = '.';
66             HEXDUMP_PRINT("%c", c);
67         }
68         HEXDUMP_PRINT("\n");
69     }
70 }
71
72 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
73 {
74     hex_dump_internal(NULL, f, 0, buf, size);
75 }
76
77 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
78 {
79     hex_dump_internal(avcl, NULL, level, buf, size);
80 }
81
82 static void pkt_dump_internal(void *avcl, FILE *f, int level, const AVPacket *pkt,
83                               int dump_payload, AVRational time_base)
84 {
85     HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
86     HEXDUMP_PRINT("  keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
87     HEXDUMP_PRINT("  duration=%0.3f\n", pkt->duration * av_q2d(time_base));
88     /* DTS is _always_ valid after av_read_frame() */
89     HEXDUMP_PRINT("  dts=");
90     if (pkt->dts == AV_NOPTS_VALUE)
91         HEXDUMP_PRINT("N/A");
92     else
93         HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
94     /* PTS may not be known if B-frames are present. */
95     HEXDUMP_PRINT("  pts=");
96     if (pkt->pts == AV_NOPTS_VALUE)
97         HEXDUMP_PRINT("N/A");
98     else
99         HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
100     HEXDUMP_PRINT("\n");
101     HEXDUMP_PRINT("  size=%d\n", pkt->size);
102     if (dump_payload)
103         av_hex_dump(f, pkt->data, pkt->size);
104 }
105
106 void av_pkt_dump2(FILE *f, const AVPacket *pkt, int dump_payload, const AVStream *st)
107 {
108     pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
109 }
110
111 void av_pkt_dump_log2(void *avcl, int level, const AVPacket *pkt, int dump_payload,
112                       const AVStream *st)
113 {
114     pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
115 }
116
117
118 static void print_fps(double d, const char *postfix)
119 {
120     uint64_t v = lrintf(d * 100);
121     if (v % 100)
122         av_log(NULL, AV_LOG_INFO, "%3.2f %s", d, postfix);
123     else if (v % (100 * 1000))
124         av_log(NULL, AV_LOG_INFO, "%1.0f %s", d, postfix);
125     else
126         av_log(NULL, AV_LOG_INFO, "%1.0fk %s", d / 1000, postfix);
127 }
128
129 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
130 {
131     if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
132         AVDictionaryEntry *tag = NULL;
133
134         av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
135         while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
136             if (strcmp("language", tag->key)) {
137                 const char *p = tag->value;
138                 av_log(ctx, AV_LOG_INFO,
139                        "%s  %-16s: ", indent, tag->key);
140                 while (*p) {
141                     char tmp[256];
142                     size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
143                     av_strlcpy(tmp, p, FFMIN(sizeof(tmp), len+1));
144                     av_log(ctx, AV_LOG_INFO, "%s", tmp);
145                     p += len;
146                     if (*p == 0xd) av_log(ctx, AV_LOG_INFO, " ");
147                     if (*p == 0xa) av_log(ctx, AV_LOG_INFO, "\n%s  %-16s: ", indent, "");
148                     if (*p) p++;
149                 }
150                 av_log(ctx, AV_LOG_INFO, "\n");
151             }
152     }
153 }
154
155 /* param change side data*/
156 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
157 {
158     int size = sd->size;
159     const uint8_t *data = sd->data;
160     uint32_t flags, channels, sample_rate, width, height;
161     uint64_t layout;
162
163     if (!data || sd->size < 4)
164         goto fail;
165
166     flags = AV_RL32(data);
167     data += 4;
168     size -= 4;
169
170     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
171         if (size < 4)
172             goto fail;
173         channels = AV_RL32(data);
174         data += 4;
175         size -= 4;
176         av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
177     }
178     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
179         if (size < 8)
180             goto fail;
181         layout = AV_RL64(data);
182         data += 8;
183         size -= 8;
184         av_log(ctx, AV_LOG_INFO,
185                "channel layout: %s, ", av_get_channel_name(layout));
186     }
187     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
188         if (size < 4)
189             goto fail;
190         sample_rate = AV_RL32(data);
191         data += 4;
192         size -= 4;
193         av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
194     }
195     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
196         if (size < 8)
197             goto fail;
198         width = AV_RL32(data);
199         data += 4;
200         size -= 4;
201         height = AV_RL32(data);
202         data += 4;
203         size -= 4;
204         av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
205     }
206
207     return;
208 fail:
209     av_log(ctx, AV_LOG_INFO, "unknown param");
210 }
211
212 /* replaygain side data*/
213 static void print_gain(void *ctx, const char *str, int32_t gain)
214 {
215     av_log(ctx, AV_LOG_INFO, "%s - ", str);
216     if (gain == INT32_MIN)
217         av_log(ctx, AV_LOG_INFO, "unknown");
218     else
219         av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
220     av_log(ctx, AV_LOG_INFO, ", ");
221 }
222
223 static void print_peak(void *ctx, const char *str, uint32_t peak)
224 {
225     av_log(ctx, AV_LOG_INFO, "%s - ", str);
226     if (!peak)
227         av_log(ctx, AV_LOG_INFO, "unknown");
228     else
229         av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
230     av_log(ctx, AV_LOG_INFO, ", ");
231 }
232
233 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
234 {
235     AVReplayGain *rg;
236
237     if (sd->size < sizeof(*rg)) {
238         av_log(ctx, AV_LOG_INFO, "invalid data");
239         return;
240     }
241     rg = (AVReplayGain*)sd->data;
242
243     print_gain(ctx, "track gain", rg->track_gain);
244     print_peak(ctx, "track peak", rg->track_peak);
245     print_gain(ctx, "album gain", rg->album_gain);
246     print_peak(ctx, "album peak", rg->album_peak);
247 }
248
249 static void dump_stereo3d(void *ctx, AVPacketSideData *sd)
250 {
251     AVStereo3D *stereo;
252
253     if (sd->size < sizeof(*stereo)) {
254         av_log(ctx, AV_LOG_INFO, "invalid data");
255         return;
256     }
257
258     stereo = (AVStereo3D *)sd->data;
259
260     switch (stereo->type) {
261     case AV_STEREO3D_2D:
262         av_log(ctx, AV_LOG_INFO, "2D");
263         break;
264     case AV_STEREO3D_SIDEBYSIDE:
265         av_log(ctx, AV_LOG_INFO, "side by side");
266         break;
267     case AV_STEREO3D_TOPBOTTOM:
268         av_log(ctx, AV_LOG_INFO, "top and bottom");
269         break;
270     case AV_STEREO3D_FRAMESEQUENCE:
271         av_log(ctx, AV_LOG_INFO, "frame alternate");
272         break;
273     case AV_STEREO3D_CHECKERBOARD:
274         av_log(ctx, AV_LOG_INFO, "checkerboard");
275         break;
276     case AV_STEREO3D_LINES:
277         av_log(ctx, AV_LOG_INFO, "interleaved lines");
278         break;
279     case AV_STEREO3D_COLUMNS:
280         av_log(ctx, AV_LOG_INFO, "interleaved columns");
281         break;
282     case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
283         av_log(ctx, AV_LOG_INFO, "side by side (quincunx subsampling)");
284         break;
285     default:
286         av_log(ctx, AV_LOG_WARNING, "unknown");
287         break;
288     }
289
290     if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
291         av_log(ctx, AV_LOG_INFO, " (inverted)");
292 }
293
294 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
295 {
296     int i;
297
298     if (st->nb_side_data)
299         av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
300
301     for (i = 0; i < st->nb_side_data; i++) {
302         AVPacketSideData sd = st->side_data[i];
303         av_log(ctx, AV_LOG_INFO, "%s  ", indent);
304
305         switch (sd.type) {
306         case AV_PKT_DATA_PALETTE:
307             av_log(ctx, AV_LOG_INFO, "palette");
308             break;
309         case AV_PKT_DATA_NEW_EXTRADATA:
310             av_log(ctx, AV_LOG_INFO, "new extradata");
311             break;
312         case AV_PKT_DATA_PARAM_CHANGE:
313             av_log(ctx, AV_LOG_INFO, "paramchange: ");
314             dump_paramchange(ctx, &sd);
315             break;
316         case AV_PKT_DATA_H263_MB_INFO:
317             av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
318             break;
319         case AV_PKT_DATA_REPLAYGAIN:
320             av_log(ctx, AV_LOG_INFO, "replaygain: ");
321             dump_replaygain(ctx, &sd);
322             break;
323         case AV_PKT_DATA_DISPLAYMATRIX:
324             av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
325                    av_display_rotation_get((int32_t *)sd.data));
326             break;
327         case AV_PKT_DATA_STEREO3D:
328             av_log(ctx, AV_LOG_INFO, "stereo3d: ");
329             dump_stereo3d(ctx, &sd);
330             break;
331         default:
332             av_log(ctx, AV_LOG_WARNING,
333                    "unknown side data type %d (%d bytes)", sd.type, sd.size);
334             break;
335         }
336
337         av_log(ctx, AV_LOG_INFO, "\n");
338     }
339 }
340
341 /* "user interface" functions */
342 static void dump_stream_format(AVFormatContext *ic, int i,
343                                int index, int is_output)
344 {
345     char buf[256];
346     int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
347     AVStream *st = ic->streams[i];
348     AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
349     char *separator = ic->dump_separator;
350     char **codec_separator = av_opt_ptr(st->codec->av_class, st->codec, "dump_separator");
351     int use_format_separator = !*codec_separator;
352
353     if (use_format_separator)
354         *codec_separator = av_strdup(separator);
355     avcodec_string(buf, sizeof(buf), st->codec, is_output);
356     if (use_format_separator)
357         av_freep(codec_separator);
358     av_log(NULL, AV_LOG_INFO, "    Stream #%d:%d", index, i);
359
360     /* the pid is an important information, so we display it */
361     /* XXX: add a generic system */
362     if (flags & AVFMT_SHOW_IDS)
363         av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
364     if (lang)
365         av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
366     av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
367            st->time_base.num, st->time_base.den);
368     av_log(NULL, AV_LOG_INFO, ": %s", buf);
369
370     if (st->sample_aspect_ratio.num && // default
371         av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
372         AVRational display_aspect_ratio;
373         av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
374                   st->codec->width  * st->sample_aspect_ratio.num,
375                   st->codec->height * st->sample_aspect_ratio.den,
376                   1024 * 1024);
377         av_log(NULL, AV_LOG_INFO, ", SAR %d:%d DAR %d:%d",
378                st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
379                display_aspect_ratio.num, display_aspect_ratio.den);
380     }
381
382     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
383         int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
384         int tbr = st->r_frame_rate.den && st->r_frame_rate.num;
385         int tbn = st->time_base.den && st->time_base.num;
386         int tbc = st->codec->time_base.den && st->codec->time_base.num;
387
388         if (fps || tbr || tbn || tbc)
389             av_log(NULL, AV_LOG_INFO, "%s", separator);
390
391         if (fps)
392             print_fps(av_q2d(st->avg_frame_rate), tbr || tbn || tbc ? "fps, " : "fps");
393         if (tbr)
394             print_fps(av_q2d(st->r_frame_rate), tbn || tbc ? "tbr, " : "tbr");
395         if (tbn)
396             print_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
397         if (tbc)
398             print_fps(1 / av_q2d(st->codec->time_base), "tbc");
399     }
400
401     if (st->disposition & AV_DISPOSITION_DEFAULT)
402         av_log(NULL, AV_LOG_INFO, " (default)");
403     if (st->disposition & AV_DISPOSITION_DUB)
404         av_log(NULL, AV_LOG_INFO, " (dub)");
405     if (st->disposition & AV_DISPOSITION_ORIGINAL)
406         av_log(NULL, AV_LOG_INFO, " (original)");
407     if (st->disposition & AV_DISPOSITION_COMMENT)
408         av_log(NULL, AV_LOG_INFO, " (comment)");
409     if (st->disposition & AV_DISPOSITION_LYRICS)
410         av_log(NULL, AV_LOG_INFO, " (lyrics)");
411     if (st->disposition & AV_DISPOSITION_KARAOKE)
412         av_log(NULL, AV_LOG_INFO, " (karaoke)");
413     if (st->disposition & AV_DISPOSITION_FORCED)
414         av_log(NULL, AV_LOG_INFO, " (forced)");
415     if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
416         av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
417     if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
418         av_log(NULL, AV_LOG_INFO, " (visual impaired)");
419     if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
420         av_log(NULL, AV_LOG_INFO, " (clean effects)");
421     av_log(NULL, AV_LOG_INFO, "\n");
422
423     dump_metadata(NULL, st->metadata, "    ");
424
425     dump_sidedata(NULL, st, "    ");
426 }
427
428 void av_dump_format(AVFormatContext *ic, int index,
429                     const char *url, int is_output)
430 {
431     int i;
432     uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
433     if (ic->nb_streams && !printed)
434         return;
435
436     av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
437            is_output ? "Output" : "Input",
438            index,
439            is_output ? ic->oformat->name : ic->iformat->name,
440            is_output ? "to" : "from", url);
441     dump_metadata(NULL, ic->metadata, "  ");
442
443     if (!is_output) {
444         av_log(NULL, AV_LOG_INFO, "  Duration: ");
445         if (ic->duration != AV_NOPTS_VALUE) {
446             int hours, mins, secs, us;
447             int64_t duration = ic->duration + 5000;
448             secs  = duration / AV_TIME_BASE;
449             us    = duration % AV_TIME_BASE;
450             mins  = secs / 60;
451             secs %= 60;
452             hours = mins / 60;
453             mins %= 60;
454             av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
455                    (100 * us) / AV_TIME_BASE);
456         } else {
457             av_log(NULL, AV_LOG_INFO, "N/A");
458         }
459         if (ic->start_time != AV_NOPTS_VALUE) {
460             int secs, us;
461             av_log(NULL, AV_LOG_INFO, ", start: ");
462             secs = ic->start_time / AV_TIME_BASE;
463             us   = abs(ic->start_time % AV_TIME_BASE);
464             av_log(NULL, AV_LOG_INFO, "%d.%06d",
465                    secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
466         }
467         av_log(NULL, AV_LOG_INFO, ", bitrate: ");
468         if (ic->bit_rate)
469             av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
470         else
471             av_log(NULL, AV_LOG_INFO, "N/A");
472         av_log(NULL, AV_LOG_INFO, "\n");
473     }
474
475     for (i = 0; i < ic->nb_chapters; i++) {
476         AVChapter *ch = ic->chapters[i];
477         av_log(NULL, AV_LOG_INFO, "    Chapter #%d:%d: ", index, i);
478         av_log(NULL, AV_LOG_INFO,
479                "start %f, ", ch->start * av_q2d(ch->time_base));
480         av_log(NULL, AV_LOG_INFO,
481                "end %f\n", ch->end * av_q2d(ch->time_base));
482
483         dump_metadata(NULL, ch->metadata, "    ");
484     }
485
486     if (ic->nb_programs) {
487         int j, k, total = 0;
488         for (j = 0; j < ic->nb_programs; j++) {
489             AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
490                                                   "name", NULL, 0);
491             av_log(NULL, AV_LOG_INFO, "  Program %d %s\n", ic->programs[j]->id,
492                    name ? name->value : "");
493             dump_metadata(NULL, ic->programs[j]->metadata, "    ");
494             for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
495                 dump_stream_format(ic, ic->programs[j]->stream_index[k],
496                                    index, is_output);
497                 printed[ic->programs[j]->stream_index[k]] = 1;
498             }
499             total += ic->programs[j]->nb_stream_indexes;
500         }
501         if (total < ic->nb_streams)
502             av_log(NULL, AV_LOG_INFO, "  No Program\n");
503     }
504
505     for (i = 0; i < ic->nb_streams; i++)
506         if (!printed[i])
507             dump_stream_format(ic, i, index, is_output);
508
509     av_free(printed);
510 }