]> git.sesse.net Git - ffmpeg/blob - libavformat/dump.c
3b50f5d944227bb729ce0562a1084f5ac5bf985a
[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 #include "libavutil/stereo3d.h"
31
32 #include "avformat.h"
33
34 #define HEXDUMP_PRINT(...)                                                    \
35     do {                                                                      \
36         if (!f)                                                               \
37             av_log(avcl, level, __VA_ARGS__);                                 \
38         else                                                                  \
39             fprintf(f, __VA_ARGS__);                                          \
40     } while (0)
41
42 static void hex_dump_internal(void *avcl, FILE *f, int level,
43                               const uint8_t *buf, int size)
44 {
45     int len, i, j, c;
46
47     for (i = 0; i < size; i += 16) {
48         len = size - i;
49         if (len > 16)
50             len = 16;
51         HEXDUMP_PRINT("%08x ", i);
52         for (j = 0; j < 16; j++) {
53             if (j < len)
54                 HEXDUMP_PRINT(" %02x", buf[i + j]);
55             else
56                 HEXDUMP_PRINT("   ");
57         }
58         HEXDUMP_PRINT(" ");
59         for (j = 0; j < len; j++) {
60             c = buf[i + j];
61             if (c < ' ' || c > '~')
62                 c = '.';
63             HEXDUMP_PRINT("%c", c);
64         }
65         HEXDUMP_PRINT("\n");
66     }
67 }
68
69 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
70 {
71     hex_dump_internal(NULL, f, 0, buf, size);
72 }
73
74 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
75 {
76     hex_dump_internal(avcl, NULL, level, buf, size);
77 }
78
79 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt,
80                               int dump_payload, AVRational time_base)
81 {
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)
88         HEXDUMP_PRINT("N/A");
89     else
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)
94         HEXDUMP_PRINT("N/A");
95     else
96         HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
97     HEXDUMP_PRINT("\n");
98     HEXDUMP_PRINT("  size=%d\n", pkt->size);
99     if (dump_payload)
100         av_hex_dump(f, pkt->data, pkt->size);
101 }
102
103 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
104 {
105     pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
106 }
107
108 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
109                       AVStream *st)
110 {
111     pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
112 }
113
114
115 static void print_fps(double d, const char *postfix)
116 {
117     uint64_t v = lrintf(d * 100);
118     if (v % 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);
122     else
123         av_log(NULL, AV_LOG_INFO, "%1.0fk %s", d / 1000, postfix);
124 }
125
126 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
127 {
128     if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
129         AVDictionaryEntry *tag = NULL;
130
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);
136     }
137 }
138
139 /* param change side data*/
140 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
141 {
142     int size = sd->size;
143     const uint8_t *data = sd->data;
144     uint32_t flags, channels, sample_rate, width, height;
145     uint64_t layout;
146
147     if (!data || sd->size < 4)
148         goto fail;
149
150     flags = AV_RL32(data);
151     data += 4;
152     size -= 4;
153
154     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
155         if (size < 4)
156             goto fail;
157         channels = AV_RL32(data);
158         data += 4;
159         size -= 4;
160         av_log(ctx, AV_LOG_INFO, "channel count %"PRIu32", ", channels);
161     }
162     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
163         if (size < 8)
164             goto fail;
165         layout = AV_RL64(data);
166         data += 8;
167         size -= 8;
168         av_log(ctx, AV_LOG_INFO,
169                "channel layout: %s, ", av_get_channel_name(layout));
170     }
171     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
172         if (size < 4)
173             goto fail;
174         sample_rate = AV_RL32(data);
175         data += 4;
176         size -= 4;
177         av_log(ctx, AV_LOG_INFO, "sample_rate %"PRIu32", ", sample_rate);
178     }
179     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
180         if (size < 8)
181             goto fail;
182         width = AV_RL32(data);
183         data += 4;
184         size -= 4;
185         height = AV_RL32(data);
186         data += 4;
187         size -= 4;
188         av_log(ctx, AV_LOG_INFO, "width %"PRIu32" height %"PRIu32, width, height);
189     }
190
191     return;
192 fail:
193     av_log(ctx, AV_LOG_INFO, "unknown param");
194 }
195
196 /* replaygain side data*/
197 static void print_gain(void *ctx, const char *str, int32_t gain)
198 {
199     av_log(ctx, AV_LOG_INFO, "%s - ", str);
200     if (gain == INT32_MIN)
201         av_log(ctx, AV_LOG_INFO, "unknown");
202     else
203         av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
204     av_log(ctx, AV_LOG_INFO, ", ");
205 }
206
207 static void print_peak(void *ctx, const char *str, uint32_t peak)
208 {
209     av_log(ctx, AV_LOG_INFO, "%s - ", str);
210     if (!peak)
211         av_log(ctx, AV_LOG_INFO, "unknown");
212     else
213         av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
214     av_log(ctx, AV_LOG_INFO, ", ");
215 }
216
217 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
218 {
219     AVReplayGain *rg;
220
221     if (sd->size < sizeof(*rg)) {
222         av_log(ctx, AV_LOG_INFO, "invalid data");
223         return;
224     }
225     rg = (AVReplayGain*)sd->data;
226
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);
231 }
232
233 static void dump_stereo3d(void *ctx, AVPacketSideData *sd)
234 {
235     AVStereo3D *stereo;
236
237     if (sd->size < sizeof(*stereo)) {
238         av_log(ctx, AV_LOG_INFO, "invalid data");
239         return;
240     }
241
242     stereo = (AVStereo3D *)sd->data;
243
244     av_log(ctx, AV_LOG_INFO, "%s", av_stereo3d_type_name(stereo->type));
245
246     if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
247         av_log(ctx, AV_LOG_INFO, " (inverted)");
248 }
249
250 static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
251 {
252     enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
253
254     if (sd->size < sizeof(*ast)) {
255         av_log(ctx, AV_LOG_INFO, "invalid data");
256         return;
257     }
258
259     switch (*ast) {
260     case AV_AUDIO_SERVICE_TYPE_MAIN:
261         av_log(ctx, AV_LOG_INFO, "main");
262         break;
263     case AV_AUDIO_SERVICE_TYPE_EFFECTS:
264         av_log(ctx, AV_LOG_INFO, "effects");
265         break;
266     case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
267         av_log(ctx, AV_LOG_INFO, "visually impaired");
268         break;
269     case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
270         av_log(ctx, AV_LOG_INFO, "hearing impaired");
271         break;
272     case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
273         av_log(ctx, AV_LOG_INFO, "dialogue");
274         break;
275     case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
276         av_log(ctx, AV_LOG_INFO, "comentary");
277         break;
278     case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
279         av_log(ctx, AV_LOG_INFO, "emergency");
280         break;
281     case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
282         av_log(ctx, AV_LOG_INFO, "voice over");
283         break;
284     case AV_AUDIO_SERVICE_TYPE_KARAOKE:
285         av_log(ctx, AV_LOG_INFO, "karaoke");
286         break;
287     default:
288         av_log(ctx, AV_LOG_WARNING, "unknown");
289         break;
290     }
291 }
292
293 static void dump_cpb(void *ctx, AVPacketSideData *sd)
294 {
295     AVCPBProperties *cpb = (AVCPBProperties *)sd->data;
296
297     if (sd->size < sizeof(*cpb)) {
298         av_log(ctx, AV_LOG_INFO, "invalid data");
299         return;
300     }
301
302     av_log(ctx, AV_LOG_INFO,
303            "bitrate max/min/avg: %d/%d/%d buffer size: %d vbv_delay: %"PRId64,
304            cpb->max_bitrate, cpb->min_bitrate, cpb->avg_bitrate,
305            cpb->buffer_size,
306            cpb->vbv_delay);
307 }
308
309 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
310 {
311     int i;
312
313     if (st->nb_side_data)
314         av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
315
316     for (i = 0; i < st->nb_side_data; i++) {
317         AVPacketSideData sd = st->side_data[i];
318         av_log(ctx, AV_LOG_INFO, "%s  ", indent);
319
320         switch (sd.type) {
321         case AV_PKT_DATA_PALETTE:
322             av_log(ctx, AV_LOG_INFO, "palette");
323             break;
324         case AV_PKT_DATA_NEW_EXTRADATA:
325             av_log(ctx, AV_LOG_INFO, "new extradata");
326             break;
327         case AV_PKT_DATA_PARAM_CHANGE:
328             av_log(ctx, AV_LOG_INFO, "paramchange: ");
329             dump_paramchange(ctx, &sd);
330             break;
331         case AV_PKT_DATA_H263_MB_INFO:
332             av_log(ctx, AV_LOG_INFO, "H.263 macroblock info");
333             break;
334         case AV_PKT_DATA_REPLAYGAIN:
335             av_log(ctx, AV_LOG_INFO, "replaygain: ");
336             dump_replaygain(ctx, &sd);
337             break;
338         case AV_PKT_DATA_DISPLAYMATRIX:
339             av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
340                    av_display_rotation_get((int32_t *)sd.data));
341             break;
342         case AV_PKT_DATA_STEREO3D:
343             av_log(ctx, AV_LOG_INFO, "stereo3d: ");
344             dump_stereo3d(ctx, &sd);
345             break;
346         case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
347             av_log(ctx, AV_LOG_INFO, "audio service type: ");
348             dump_audioservicetype(ctx, &sd);
349             break;
350         case AV_PKT_DATA_QUALITY_FACTOR:
351             av_log(ctx, AV_LOG_INFO, "quality factor: %d", *(int *)sd.data);
352             break;
353         case AV_PKT_DATA_CPB_PROPERTIES:
354             av_log(ctx, AV_LOG_INFO, "cpb: ");
355             dump_cpb(ctx, &sd);
356             break;
357         default:
358             av_log(ctx, AV_LOG_WARNING,
359                    "unknown side data type %d (%d bytes)", sd.type, sd.size);
360             break;
361         }
362
363         av_log(ctx, AV_LOG_INFO, "\n");
364     }
365 }
366
367 /* "user interface" functions */
368 static void dump_stream_format(AVFormatContext *ic, int i,
369                                int index, int is_output)
370 {
371     char buf[256];
372     int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
373     AVStream *st = ic->streams[i];
374     AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
375     AVCodecContext *avctx;
376     int ret;
377
378     avctx = avcodec_alloc_context3(NULL);
379     if (!avctx)
380         return;
381
382     ret = avcodec_parameters_to_context(avctx, st->codecpar);
383     if (ret < 0) {
384         avcodec_free_context(&avctx);
385         return;
386     }
387
388     avcodec_string(buf, sizeof(buf), avctx, is_output);
389     avcodec_free_context(&avctx);
390
391     av_log(NULL, AV_LOG_INFO, "    Stream #%d:%d", index, i);
392
393     /* the pid is an important information, so we display it */
394     /* XXX: add a generic system */
395     if (flags & AVFMT_SHOW_IDS)
396         av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
397     if (lang)
398         av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
399     av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
400            st->time_base.num, st->time_base.den);
401     av_log(NULL, AV_LOG_INFO, ": %s", buf);
402
403     if (st->sample_aspect_ratio.num) {
404         AVRational display_aspect_ratio;
405         av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
406                   st->codecpar->width  * st->sample_aspect_ratio.num,
407                   st->codecpar->height * st->sample_aspect_ratio.den,
408                   1024 * 1024);
409         av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
410                st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
411                display_aspect_ratio.num, display_aspect_ratio.den);
412     }
413
414     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
415         int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
416         int tbn = st->time_base.den && st->time_base.num;
417
418         if (fps || tbn)
419             av_log(NULL, AV_LOG_INFO, "\n      ");
420         if (fps)
421             print_fps(av_q2d(st->avg_frame_rate), tbn ? "fps, " : "fps");
422         if (tbn)
423             print_fps(1 / av_q2d(st->time_base), "tbn");
424     }
425
426     if (st->disposition & AV_DISPOSITION_DEFAULT)
427         av_log(NULL, AV_LOG_INFO, " (default)");
428     if (st->disposition & AV_DISPOSITION_DUB)
429         av_log(NULL, AV_LOG_INFO, " (dub)");
430     if (st->disposition & AV_DISPOSITION_ORIGINAL)
431         av_log(NULL, AV_LOG_INFO, " (original)");
432     if (st->disposition & AV_DISPOSITION_COMMENT)
433         av_log(NULL, AV_LOG_INFO, " (comment)");
434     if (st->disposition & AV_DISPOSITION_LYRICS)
435         av_log(NULL, AV_LOG_INFO, " (lyrics)");
436     if (st->disposition & AV_DISPOSITION_KARAOKE)
437         av_log(NULL, AV_LOG_INFO, " (karaoke)");
438     if (st->disposition & AV_DISPOSITION_FORCED)
439         av_log(NULL, AV_LOG_INFO, " (forced)");
440     if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
441         av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
442     if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
443         av_log(NULL, AV_LOG_INFO, " (visual impaired)");
444     if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
445         av_log(NULL, AV_LOG_INFO, " (clean effects)");
446     av_log(NULL, AV_LOG_INFO, "\n");
447
448     dump_metadata(NULL, st->metadata, "    ");
449
450     dump_sidedata(NULL, st, "    ");
451 }
452
453 void av_dump_format(AVFormatContext *ic, int index,
454                     const char *url, int is_output)
455 {
456     int i;
457     uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
458     if (ic->nb_streams && !printed)
459         return;
460
461     av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
462            is_output ? "Output" : "Input",
463            index,
464            is_output ? ic->oformat->name : ic->iformat->name,
465            is_output ? "to" : "from", url);
466     dump_metadata(NULL, ic->metadata, "  ");
467
468     if (!is_output) {
469         av_log(NULL, AV_LOG_INFO, "  Duration: ");
470         if (ic->duration != AV_NOPTS_VALUE) {
471             int hours, mins, secs, us;
472             secs  = ic->duration / AV_TIME_BASE;
473             us    = ic->duration % AV_TIME_BASE;
474             mins  = secs / 60;
475             secs %= 60;
476             hours = mins / 60;
477             mins %= 60;
478             av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
479                    (100 * us) / AV_TIME_BASE);
480         } else {
481             av_log(NULL, AV_LOG_INFO, "N/A");
482         }
483         if (ic->start_time != AV_NOPTS_VALUE) {
484             int secs, us;
485             av_log(NULL, AV_LOG_INFO, ", start: ");
486             secs = ic->start_time / AV_TIME_BASE;
487             us   = llabs(ic->start_time % AV_TIME_BASE);
488             av_log(NULL, AV_LOG_INFO, "%d.%06d",
489                    secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
490         }
491         av_log(NULL, AV_LOG_INFO, ", bitrate: ");
492         if (ic->bit_rate)
493             av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
494         else
495             av_log(NULL, AV_LOG_INFO, "N/A");
496         av_log(NULL, AV_LOG_INFO, "\n");
497     }
498
499     for (i = 0; i < ic->nb_chapters; i++) {
500         AVChapter *ch = ic->chapters[i];
501         av_log(NULL, AV_LOG_INFO, "    Chapter #%d:%d: ", index, i);
502         av_log(NULL, AV_LOG_INFO,
503                "start %f, ", ch->start * av_q2d(ch->time_base));
504         av_log(NULL, AV_LOG_INFO,
505                "end %f\n", ch->end * av_q2d(ch->time_base));
506
507         dump_metadata(NULL, ch->metadata, "    ");
508     }
509
510     if (ic->nb_programs) {
511         int j, k, total = 0;
512         for (j = 0; j < ic->nb_programs; j++) {
513             AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
514                                                   "name", NULL, 0);
515             av_log(NULL, AV_LOG_INFO, "  Program %d %s\n", ic->programs[j]->id,
516                    name ? name->value : "");
517             dump_metadata(NULL, ic->programs[j]->metadata, "    ");
518             for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
519                 dump_stream_format(ic, ic->programs[j]->stream_index[k],
520                                    index, is_output);
521                 printed[ic->programs[j]->stream_index[k]] = 1;
522             }
523             total += ic->programs[j]->nb_stream_indexes;
524         }
525         if (total < ic->nb_streams)
526             av_log(NULL, AV_LOG_INFO, "  No Program\n");
527     }
528
529     for (i = 0; i < ic->nb_streams; i++)
530         if (!printed[i])
531             dump_stream_format(ic, i, index, is_output);
532
533     av_free(printed);
534 }