]> git.sesse.net Git - ffmpeg/blob - libavformat/dump.c
cdf2da1ca87d231f5a88ac6b1c8bd56d1aed5128
[ffmpeg] / libavformat / dump.c
1 /*
2  * Various pretty-printing functions for use within Libav
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdio.h>
22 #include <stdint.h>
23
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
31 #include "avformat.h"
32
33 #define HEXDUMP_PRINT(...)                                                    \
34     do {                                                                      \
35         if (!f)                                                               \
36             av_log(avcl, level, __VA_ARGS__);                                 \
37         else                                                                  \
38             fprintf(f, __VA_ARGS__);                                          \
39     } while (0)
40
41 static void hex_dump_internal(void *avcl, FILE *f, int level,
42                               const uint8_t *buf, int size)
43 {
44     int len, i, j, c;
45
46     for (i = 0; i < size; i += 16) {
47         len = size - i;
48         if (len > 16)
49             len = 16;
50         HEXDUMP_PRINT("%08x ", i);
51         for (j = 0; j < 16; j++) {
52             if (j < len)
53                 HEXDUMP_PRINT(" %02x", buf[i + j]);
54             else
55                 HEXDUMP_PRINT("   ");
56         }
57         HEXDUMP_PRINT(" ");
58         for (j = 0; j < len; j++) {
59             c = buf[i + j];
60             if (c < ' ' || c > '~')
61                 c = '.';
62             HEXDUMP_PRINT("%c", c);
63         }
64         HEXDUMP_PRINT("\n");
65     }
66 }
67
68 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
69 {
70     hex_dump_internal(NULL, f, 0, buf, size);
71 }
72
73 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
74 {
75     hex_dump_internal(avcl, NULL, level, buf, size);
76 }
77
78 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt,
79                               int dump_payload, AVRational time_base)
80 {
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)
87         HEXDUMP_PRINT("N/A");
88     else
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)
93         HEXDUMP_PRINT("N/A");
94     else
95         HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
96     HEXDUMP_PRINT("\n");
97     HEXDUMP_PRINT("  size=%d\n", pkt->size);
98     if (dump_payload)
99         av_hex_dump(f, pkt->data, pkt->size);
100 }
101
102 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
103 {
104     pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
105 }
106
107 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
108                       AVStream *st)
109 {
110     pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
111 }
112
113
114 static void print_fps(double d, const char *postfix)
115 {
116     uint64_t v = lrintf(d * 100);
117     if (v % 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);
121     else
122         av_log(NULL, AV_LOG_INFO, ", %1.0fk %s", d / 1000, postfix);
123 }
124
125 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
126 {
127     if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
128         AVDictionaryEntry *tag = NULL;
129
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);
135     }
136 }
137
138 /* param change side data*/
139 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
140 {
141     int size = sd->size;
142     const uint8_t *data = sd->data;
143     uint32_t flags, channels, sample_rate, width, height;
144     uint64_t layout;
145
146     if (!data || sd->size < 4)
147         goto fail;
148
149     flags = AV_RL32(data);
150     data += 4;
151     size -= 4;
152
153     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
154         if (size < 4)
155             goto fail;
156         channels = AV_RL32(data);
157         data += 4;
158         size -= 4;
159         av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
160     }
161     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
162         if (size < 8)
163             goto fail;
164         layout = AV_RL64(data);
165         data += 8;
166         size -= 8;
167         av_log(ctx, AV_LOG_INFO,
168                "channel layout: %s, ", av_get_channel_name(layout));
169     }
170     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
171         if (size < 4)
172             goto fail;
173         sample_rate = AV_RL32(data);
174         data += 4;
175         size -= 4;
176         av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
177     }
178     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
179         if (size < 8)
180             goto fail;
181         width = AV_RL32(data);
182         data += 4;
183         size -= 4;
184         height = AV_RL32(data);
185         data += 4;
186         size -= 4;
187         av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
188     }
189
190     return;
191 fail:
192     av_log(ctx, AV_LOG_INFO, "unknown param");
193 }
194
195 /* replaygain side data*/
196 static void print_gain(void *ctx, const char *str, int32_t gain)
197 {
198     av_log(ctx, AV_LOG_INFO, "%s - ", str);
199     if (gain == INT32_MIN)
200         av_log(ctx, AV_LOG_INFO, "unknown");
201     else
202         av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
203     av_log(ctx, AV_LOG_INFO, ", ");
204 }
205
206 static void print_peak(void *ctx, const char *str, uint32_t peak)
207 {
208     av_log(ctx, AV_LOG_INFO, "%s - ", str);
209     if (!peak)
210         av_log(ctx, AV_LOG_INFO, "unknown");
211     else
212         av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
213     av_log(ctx, AV_LOG_INFO, ", ");
214 }
215
216 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
217 {
218     AVReplayGain *rg;
219
220     if (sd->size < sizeof(*rg)) {
221         av_log(ctx, AV_LOG_INFO, "invalid data");
222         return;
223     }
224     rg = (AVReplayGain*)sd->data;
225
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);
230 }
231
232 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
233 {
234     int i;
235
236     if (st->nb_side_data)
237         av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
238
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);
242
243         switch (sd.type) {
244         case AV_PKT_DATA_PALETTE:
245             av_log(ctx, AV_LOG_INFO, "palette");
246             break;
247         case AV_PKT_DATA_NEW_EXTRADATA:
248             av_log(ctx, AV_LOG_INFO, "new extradata");
249             break;
250         case AV_PKT_DATA_PARAM_CHANGE:
251             av_log(ctx, AV_LOG_INFO, "paramchange: ");
252             dump_paramchange(ctx, &sd);
253             break;
254         case AV_PKT_DATA_H263_MB_INFO:
255             av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
256             break;
257         case AV_PKT_DATA_REPLAYGAIN:
258             av_log(ctx, AV_LOG_INFO, "replaygain: ");
259             dump_replaygain(ctx, &sd);
260             break;
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));
264             break;
265         default:
266             av_log(ctx, AV_LOG_WARNING,
267                    "unknown side data type %d (%d bytes)", sd.type, sd.size);
268             break;
269         }
270
271         av_log(ctx, AV_LOG_INFO, "\n");
272     }
273 }
274
275 /* "user interface" functions */
276 static void dump_stream_format(AVFormatContext *ic, int i,
277                                int index, int is_output)
278 {
279     char buf[256];
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);
283
284     avcodec_string(buf, sizeof(buf), st->codec, is_output);
285     av_log(NULL, AV_LOG_INFO, "    Stream #%d.%d", index, i);
286
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);
291     if (lang)
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);
296
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,
303                   1024 * 1024);
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);
307     }
308
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");
316     }
317
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");
339
340     dump_metadata(NULL, st->metadata, "    ");
341
342     dump_sidedata(NULL, st, "    ");
343 }
344
345 void av_dump_format(AVFormatContext *ic, int index,
346                     const char *url, int is_output)
347 {
348     int i;
349     uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
350     if (ic->nb_streams && !printed)
351         return;
352
353     av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
354            is_output ? "Output" : "Input",
355            index,
356            is_output ? ic->oformat->name : ic->iformat->name,
357            is_output ? "to" : "from", url);
358     dump_metadata(NULL, ic->metadata, "  ");
359
360     if (!is_output) {
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;
366             mins  = secs / 60;
367             secs %= 60;
368             hours = mins / 60;
369             mins %= 60;
370             av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
371                    (100 * us) / AV_TIME_BASE);
372         } else {
373             av_log(NULL, AV_LOG_INFO, "N/A");
374         }
375         if (ic->start_time != AV_NOPTS_VALUE) {
376             int secs, us;
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));
382         }
383         av_log(NULL, AV_LOG_INFO, ", bitrate: ");
384         if (ic->bit_rate)
385             av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
386         else
387             av_log(NULL, AV_LOG_INFO, "N/A");
388         av_log(NULL, AV_LOG_INFO, "\n");
389     }
390
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));
398
399         dump_metadata(NULL, ch->metadata, "    ");
400     }
401
402     if (ic->nb_programs) {
403         int j, k, total = 0;
404         for (j = 0; j < ic->nb_programs; j++) {
405             AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
406                                                   "name", NULL, 0);
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],
412                                    index, is_output);
413                 printed[ic->programs[j]->stream_index[k]] = 1;
414             }
415             total += ic->programs[j]->nb_stream_indexes;
416         }
417         if (total < ic->nb_streams)
418             av_log(NULL, AV_LOG_INFO, "  No Program\n");
419     }
420
421     for (i = 0; i < ic->nb_streams; i++)
422         if (!printed[i])
423             dump_stream_format(ic, i, index, is_output);
424
425     av_free(printed);
426 }