dump: Use the correct abs() version
[libav.git] / 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_sidedata(void *ctx, AVStream *st, const char *indent)
322 {
323 int i;
324
325 if (st->nb_side_data)
326 av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
327
328 for (i = 0; i < st->nb_side_data; i++) {
329 AVPacketSideData sd = st->side_data[i];
330 av_log(ctx, AV_LOG_INFO, "%s ", indent);
331
332 switch (sd.type) {
333 case AV_PKT_DATA_PALETTE:
334 av_log(ctx, AV_LOG_INFO, "palette");
335 break;
336 case AV_PKT_DATA_NEW_EXTRADATA:
337 av_log(ctx, AV_LOG_INFO, "new extradata");
338 break;
339 case AV_PKT_DATA_PARAM_CHANGE:
340 av_log(ctx, AV_LOG_INFO, "paramchange: ");
341 dump_paramchange(ctx, &sd);
342 break;
343 case AV_PKT_DATA_H263_MB_INFO:
344 av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
345 break;
346 case AV_PKT_DATA_REPLAYGAIN:
347 av_log(ctx, AV_LOG_INFO, "replaygain: ");
348 dump_replaygain(ctx, &sd);
349 break;
350 case AV_PKT_DATA_DISPLAYMATRIX:
351 av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
352 av_display_rotation_get((int32_t *)sd.data));
353 break;
354 case AV_PKT_DATA_STEREO3D:
355 av_log(ctx, AV_LOG_INFO, "stereo3d: ");
356 dump_stereo3d(ctx, &sd);
357 break;
358 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
359 av_log(ctx, AV_LOG_INFO, "audio service type: ");
360 dump_audioservicetype(ctx, &sd);
361 break;
362 default:
363 av_log(ctx, AV_LOG_WARNING,
364 "unknown side data type %d (%d bytes)", sd.type, sd.size);
365 break;
366 }
367
368 av_log(ctx, AV_LOG_INFO, "\n");
369 }
370 }
371
372 /* "user interface" functions */
373 static void dump_stream_format(AVFormatContext *ic, int i,
374 int index, int is_output)
375 {
376 char buf[256];
377 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
378 AVStream *st = ic->streams[i];
379 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
380
381 avcodec_string(buf, sizeof(buf), st->codec, is_output);
382 av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
383
384 /* the pid is an important information, so we display it */
385 /* XXX: add a generic system */
386 if (flags & AVFMT_SHOW_IDS)
387 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
388 if (lang)
389 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
390 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
391 st->time_base.num, st->time_base.den);
392 av_log(NULL, AV_LOG_INFO, ": %s", buf);
393
394 if (st->sample_aspect_ratio.num && // default
395 av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
396 AVRational display_aspect_ratio;
397 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
398 st->codec->width * st->sample_aspect_ratio.num,
399 st->codec->height * st->sample_aspect_ratio.den,
400 1024 * 1024);
401 av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
402 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
403 display_aspect_ratio.num, display_aspect_ratio.den);
404 }
405
406 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
407 int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
408 int tbn = st->time_base.den && st->time_base.num;
409 int tbc = st->codec->time_base.den && st->codec->time_base.num;
410
411 if (fps || tbn || tbc)
412 av_log(NULL, AV_LOG_INFO, "\n ");
413 if (fps)
414 print_fps(av_q2d(st->avg_frame_rate), tbn || tbc ? "fps, " : "fps");
415 if (tbn)
416 print_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
417 if (tbc)
418 print_fps(1 / av_q2d(st->codec->time_base), "tbc");
419 }
420
421 if (st->disposition & AV_DISPOSITION_DEFAULT)
422 av_log(NULL, AV_LOG_INFO, " (default)");
423 if (st->disposition & AV_DISPOSITION_DUB)
424 av_log(NULL, AV_LOG_INFO, " (dub)");
425 if (st->disposition & AV_DISPOSITION_ORIGINAL)
426 av_log(NULL, AV_LOG_INFO, " (original)");
427 if (st->disposition & AV_DISPOSITION_COMMENT)
428 av_log(NULL, AV_LOG_INFO, " (comment)");
429 if (st->disposition & AV_DISPOSITION_LYRICS)
430 av_log(NULL, AV_LOG_INFO, " (lyrics)");
431 if (st->disposition & AV_DISPOSITION_KARAOKE)
432 av_log(NULL, AV_LOG_INFO, " (karaoke)");
433 if (st->disposition & AV_DISPOSITION_FORCED)
434 av_log(NULL, AV_LOG_INFO, " (forced)");
435 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
436 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
437 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
438 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
439 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
440 av_log(NULL, AV_LOG_INFO, " (clean effects)");
441 av_log(NULL, AV_LOG_INFO, "\n");
442
443 dump_metadata(NULL, st->metadata, " ");
444
445 dump_sidedata(NULL, st, " ");
446 }
447
448 void av_dump_format(AVFormatContext *ic, int index,
449 const char *url, int is_output)
450 {
451 int i;
452 uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
453 if (ic->nb_streams && !printed)
454 return;
455
456 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
457 is_output ? "Output" : "Input",
458 index,
459 is_output ? ic->oformat->name : ic->iformat->name,
460 is_output ? "to" : "from", url);
461 dump_metadata(NULL, ic->metadata, " ");
462
463 if (!is_output) {
464 av_log(NULL, AV_LOG_INFO, " Duration: ");
465 if (ic->duration != AV_NOPTS_VALUE) {
466 int hours, mins, secs, us;
467 secs = ic->duration / AV_TIME_BASE;
468 us = ic->duration % AV_TIME_BASE;
469 mins = secs / 60;
470 secs %= 60;
471 hours = mins / 60;
472 mins %= 60;
473 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
474 (100 * us) / AV_TIME_BASE);
475 } else {
476 av_log(NULL, AV_LOG_INFO, "N/A");
477 }
478 if (ic->start_time != AV_NOPTS_VALUE) {
479 int secs, us;
480 av_log(NULL, AV_LOG_INFO, ", start: ");
481 secs = ic->start_time / AV_TIME_BASE;
482 us = llabs(ic->start_time % AV_TIME_BASE);
483 av_log(NULL, AV_LOG_INFO, "%d.%06d",
484 secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
485 }
486 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
487 if (ic->bit_rate)
488 av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
489 else
490 av_log(NULL, AV_LOG_INFO, "N/A");
491 av_log(NULL, AV_LOG_INFO, "\n");
492 }
493
494 for (i = 0; i < ic->nb_chapters; i++) {
495 AVChapter *ch = ic->chapters[i];
496 av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
497 av_log(NULL, AV_LOG_INFO,
498 "start %f, ", ch->start * av_q2d(ch->time_base));
499 av_log(NULL, AV_LOG_INFO,
500 "end %f\n", ch->end * av_q2d(ch->time_base));
501
502 dump_metadata(NULL, ch->metadata, " ");
503 }
504
505 if (ic->nb_programs) {
506 int j, k, total = 0;
507 for (j = 0; j < ic->nb_programs; j++) {
508 AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
509 "name", NULL, 0);
510 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
511 name ? name->value : "");
512 dump_metadata(NULL, ic->programs[j]->metadata, " ");
513 for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
514 dump_stream_format(ic, ic->programs[j]->stream_index[k],
515 index, is_output);
516 printed[ic->programs[j]->stream_index[k]] = 1;
517 }
518 total += ic->programs[j]->nb_stream_indexes;
519 }
520 if (total < ic->nb_streams)
521 av_log(NULL, AV_LOG_INFO, " No Program\n");
522 }
523
524 for (i = 0; i < ic->nb_streams; i++)
525 if (!printed[i])
526 dump_stream_format(ic, i, index, is_output);
527
528 av_free(printed);
529 }