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