]> git.sesse.net Git - ffmpeg/blob - libavformat/dump.c
img2enc: Refactor the atomic renaming code
[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     switch (stereo->type) {
245     case AV_STEREO3D_2D:
246         av_log(ctx, AV_LOG_INFO, "2D");
247         break;
248     case AV_STEREO3D_SIDEBYSIDE:
249         av_log(ctx, AV_LOG_INFO, "side by side");
250         break;
251     case AV_STEREO3D_TOPBOTTOM:
252         av_log(ctx, AV_LOG_INFO, "top and bottom");
253         break;
254     case AV_STEREO3D_FRAMESEQUENCE:
255         av_log(ctx, AV_LOG_INFO, "frame alternate");
256         break;
257     case AV_STEREO3D_CHECKERBOARD:
258         av_log(ctx, AV_LOG_INFO, "checkerboard");
259         break;
260     case AV_STEREO3D_LINES:
261         av_log(ctx, AV_LOG_INFO, "interleaved lines");
262         break;
263     case AV_STEREO3D_COLUMNS:
264         av_log(ctx, AV_LOG_INFO, "interleaved columns");
265         break;
266     case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
267         av_log(ctx, AV_LOG_INFO, "side by side (quincunx subsampling)");
268         break;
269     default:
270         av_log(ctx, AV_LOG_WARNING, "unknown");
271         break;
272     }
273
274     if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
275         av_log(ctx, AV_LOG_INFO, " (inverted)");
276 }
277
278 static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
279 {
280     enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
281
282     if (sd->size < sizeof(*ast)) {
283         av_log(ctx, AV_LOG_INFO, "invalid data");
284         return;
285     }
286
287     switch (*ast) {
288     case AV_AUDIO_SERVICE_TYPE_MAIN:
289         av_log(ctx, AV_LOG_INFO, "main");
290         break;
291     case AV_AUDIO_SERVICE_TYPE_EFFECTS:
292         av_log(ctx, AV_LOG_INFO, "effects");
293         break;
294     case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
295         av_log(ctx, AV_LOG_INFO, "visually impaired");
296         break;
297     case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
298         av_log(ctx, AV_LOG_INFO, "hearing impaired");
299         break;
300     case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
301         av_log(ctx, AV_LOG_INFO, "dialogue");
302         break;
303     case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
304         av_log(ctx, AV_LOG_INFO, "comentary");
305         break;
306     case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
307         av_log(ctx, AV_LOG_INFO, "emergency");
308         break;
309     case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
310         av_log(ctx, AV_LOG_INFO, "voice over");
311         break;
312     case AV_AUDIO_SERVICE_TYPE_KARAOKE:
313         av_log(ctx, AV_LOG_INFO, "karaoke");
314         break;
315     default:
316         av_log(ctx, AV_LOG_WARNING, "unknown");
317         break;
318     }
319 }
320
321 static void dump_cpb(void *ctx, AVPacketSideData *sd)
322 {
323     AVCPBProperties *cpb = (AVCPBProperties *)sd->data;
324
325     if (sd->size < sizeof(*cpb)) {
326         av_log(ctx, AV_LOG_INFO, "invalid data");
327         return;
328     }
329
330     av_log(ctx, AV_LOG_INFO,
331            "bitrate max/min/avg: %d/%d/%d buffer size: %d vbv_delay: %"PRId64,
332            cpb->max_bitrate, cpb->min_bitrate, cpb->avg_bitrate,
333            cpb->buffer_size,
334            cpb->vbv_delay);
335 }
336
337 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
338 {
339     int i;
340
341     if (st->nb_side_data)
342         av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
343
344     for (i = 0; i < st->nb_side_data; i++) {
345         AVPacketSideData sd = st->side_data[i];
346         av_log(ctx, AV_LOG_INFO, "%s  ", indent);
347
348         switch (sd.type) {
349         case AV_PKT_DATA_PALETTE:
350             av_log(ctx, AV_LOG_INFO, "palette");
351             break;
352         case AV_PKT_DATA_NEW_EXTRADATA:
353             av_log(ctx, AV_LOG_INFO, "new extradata");
354             break;
355         case AV_PKT_DATA_PARAM_CHANGE:
356             av_log(ctx, AV_LOG_INFO, "paramchange: ");
357             dump_paramchange(ctx, &sd);
358             break;
359         case AV_PKT_DATA_H263_MB_INFO:
360             av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
361             break;
362         case AV_PKT_DATA_REPLAYGAIN:
363             av_log(ctx, AV_LOG_INFO, "replaygain: ");
364             dump_replaygain(ctx, &sd);
365             break;
366         case AV_PKT_DATA_DISPLAYMATRIX:
367             av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
368                    av_display_rotation_get((int32_t *)sd.data));
369             break;
370         case AV_PKT_DATA_STEREO3D:
371             av_log(ctx, AV_LOG_INFO, "stereo3d: ");
372             dump_stereo3d(ctx, &sd);
373             break;
374         case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
375             av_log(ctx, AV_LOG_INFO, "audio service type: ");
376             dump_audioservicetype(ctx, &sd);
377             break;
378         case AV_PKT_DATA_QUALITY_FACTOR:
379             av_log(ctx, AV_LOG_INFO, "quality factor: %d", *(int *)sd.data);
380             break;
381         case AV_PKT_DATA_CPB_PROPERTIES:
382             av_log(ctx, AV_LOG_INFO, "cpb: ");
383             dump_cpb(ctx, &sd);
384             break;
385         default:
386             av_log(ctx, AV_LOG_WARNING,
387                    "unknown side data type %d (%d bytes)", sd.type, sd.size);
388             break;
389         }
390
391         av_log(ctx, AV_LOG_INFO, "\n");
392     }
393 }
394
395 /* "user interface" functions */
396 static void dump_stream_format(AVFormatContext *ic, int i,
397                                int index, int is_output)
398 {
399     char buf[256];
400     int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
401     AVStream *st = ic->streams[i];
402     AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
403     AVCodecContext *avctx;
404     int ret;
405
406     avctx = avcodec_alloc_context3(NULL);
407     if (!avctx)
408         return;
409
410     ret = avcodec_parameters_to_context(avctx, st->codecpar);
411     if (ret < 0) {
412         avcodec_free_context(&avctx);
413         return;
414     }
415
416     avcodec_string(buf, sizeof(buf), avctx, is_output);
417     avcodec_free_context(&avctx);
418
419     av_log(NULL, AV_LOG_INFO, "    Stream #%d:%d", index, i);
420
421     /* the pid is an important information, so we display it */
422     /* XXX: add a generic system */
423     if (flags & AVFMT_SHOW_IDS)
424         av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
425     if (lang)
426         av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
427     av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
428            st->time_base.num, st->time_base.den);
429     av_log(NULL, AV_LOG_INFO, ": %s", buf);
430
431     if (st->sample_aspect_ratio.num) {
432         AVRational display_aspect_ratio;
433         av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
434                   st->codecpar->width  * st->sample_aspect_ratio.num,
435                   st->codecpar->height * st->sample_aspect_ratio.den,
436                   1024 * 1024);
437         av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
438                st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
439                display_aspect_ratio.num, display_aspect_ratio.den);
440     }
441
442     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
443         int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
444         int tbn = st->time_base.den && st->time_base.num;
445
446         if (fps || tbn)
447             av_log(NULL, AV_LOG_INFO, "\n      ");
448         if (fps)
449             print_fps(av_q2d(st->avg_frame_rate), tbn ? "fps, " : "fps");
450         if (tbn)
451             print_fps(1 / av_q2d(st->time_base), "tbn");
452     }
453
454     if (st->disposition & AV_DISPOSITION_DEFAULT)
455         av_log(NULL, AV_LOG_INFO, " (default)");
456     if (st->disposition & AV_DISPOSITION_DUB)
457         av_log(NULL, AV_LOG_INFO, " (dub)");
458     if (st->disposition & AV_DISPOSITION_ORIGINAL)
459         av_log(NULL, AV_LOG_INFO, " (original)");
460     if (st->disposition & AV_DISPOSITION_COMMENT)
461         av_log(NULL, AV_LOG_INFO, " (comment)");
462     if (st->disposition & AV_DISPOSITION_LYRICS)
463         av_log(NULL, AV_LOG_INFO, " (lyrics)");
464     if (st->disposition & AV_DISPOSITION_KARAOKE)
465         av_log(NULL, AV_LOG_INFO, " (karaoke)");
466     if (st->disposition & AV_DISPOSITION_FORCED)
467         av_log(NULL, AV_LOG_INFO, " (forced)");
468     if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
469         av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
470     if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
471         av_log(NULL, AV_LOG_INFO, " (visual impaired)");
472     if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
473         av_log(NULL, AV_LOG_INFO, " (clean effects)");
474     av_log(NULL, AV_LOG_INFO, "\n");
475
476     dump_metadata(NULL, st->metadata, "    ");
477
478     dump_sidedata(NULL, st, "    ");
479 }
480
481 void av_dump_format(AVFormatContext *ic, int index,
482                     const char *url, int is_output)
483 {
484     int i;
485     uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
486     if (ic->nb_streams && !printed)
487         return;
488
489     av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
490            is_output ? "Output" : "Input",
491            index,
492            is_output ? ic->oformat->name : ic->iformat->name,
493            is_output ? "to" : "from", url);
494     dump_metadata(NULL, ic->metadata, "  ");
495
496     if (!is_output) {
497         av_log(NULL, AV_LOG_INFO, "  Duration: ");
498         if (ic->duration != AV_NOPTS_VALUE) {
499             int hours, mins, secs, us;
500             secs  = ic->duration / AV_TIME_BASE;
501             us    = ic->duration % AV_TIME_BASE;
502             mins  = secs / 60;
503             secs %= 60;
504             hours = mins / 60;
505             mins %= 60;
506             av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
507                    (100 * us) / AV_TIME_BASE);
508         } else {
509             av_log(NULL, AV_LOG_INFO, "N/A");
510         }
511         if (ic->start_time != AV_NOPTS_VALUE) {
512             int secs, us;
513             av_log(NULL, AV_LOG_INFO, ", start: ");
514             secs = ic->start_time / AV_TIME_BASE;
515             us   = llabs(ic->start_time % AV_TIME_BASE);
516             av_log(NULL, AV_LOG_INFO, "%d.%06d",
517                    secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
518         }
519         av_log(NULL, AV_LOG_INFO, ", bitrate: ");
520         if (ic->bit_rate)
521             av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
522         else
523             av_log(NULL, AV_LOG_INFO, "N/A");
524         av_log(NULL, AV_LOG_INFO, "\n");
525     }
526
527     for (i = 0; i < ic->nb_chapters; i++) {
528         AVChapter *ch = ic->chapters[i];
529         av_log(NULL, AV_LOG_INFO, "    Chapter #%d:%d: ", index, i);
530         av_log(NULL, AV_LOG_INFO,
531                "start %f, ", ch->start * av_q2d(ch->time_base));
532         av_log(NULL, AV_LOG_INFO,
533                "end %f\n", ch->end * av_q2d(ch->time_base));
534
535         dump_metadata(NULL, ch->metadata, "    ");
536     }
537
538     if (ic->nb_programs) {
539         int j, k, total = 0;
540         for (j = 0; j < ic->nb_programs; j++) {
541             AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
542                                                   "name", NULL, 0);
543             av_log(NULL, AV_LOG_INFO, "  Program %d %s\n", ic->programs[j]->id,
544                    name ? name->value : "");
545             dump_metadata(NULL, ic->programs[j]->metadata, "    ");
546             for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
547                 dump_stream_format(ic, ic->programs[j]->stream_index[k],
548                                    index, is_output);
549                 printed[ic->programs[j]->stream_index[k]] = 1;
550             }
551             total += ic->programs[j]->nb_stream_indexes;
552         }
553         if (total < ic->nb_streams)
554             av_log(NULL, AV_LOG_INFO, "  No Program\n");
555     }
556
557     for (i = 0; i < ic->nb_streams; i++)
558         if (!printed[i])
559             dump_stream_format(ic, i, index, is_output);
560
561     av_free(printed);
562 }