FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dump.c
Go to the documentation of this file.
1 /*
2  * Various pretty-printing functions for use within FFmpeg
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdio.h>
23 #include <stdint.h>
24 
26 #include "libavutil/display.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/log.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/replaygain.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, const 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, const AVPacket *pkt, int dump_payload, const 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, const AVPacket *pkt, int dump_payload,
110  const 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  const char *p = tag->value;
136  av_log(ctx, AV_LOG_INFO,
137  "%s %-16s: ", indent, tag->key);
138  while (*p) {
139  char tmp[256];
140  size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
141  av_strlcpy(tmp, p, FFMIN(sizeof(tmp), len+1));
142  av_log(ctx, AV_LOG_INFO, "%s", tmp);
143  p += len;
144  if (*p == 0xd) av_log(ctx, AV_LOG_INFO, " ");
145  if (*p == 0xa) av_log(ctx, AV_LOG_INFO, "\n%s %-16s: ", indent, "");
146  if (*p) p++;
147  }
148  av_log(ctx, AV_LOG_INFO, "\n");
149  }
150  }
151 }
152 
153 /* param change side data*/
154 static void dump_paramchange(void *ctx, AVPacketSideData *sd)
155 {
156  int size = sd->size;
157  const uint8_t *data = sd->data;
158  uint32_t flags, channels, sample_rate, width, height;
159  uint64_t layout;
160 
161  if (!data || sd->size < 4)
162  goto fail;
163 
164  flags = AV_RL32(data);
165  data += 4;
166  size -= 4;
167 
169  if (size < 4)
170  goto fail;
171  channels = AV_RL32(data);
172  data += 4;
173  size -= 4;
174  av_log(ctx, AV_LOG_INFO, "channel count %d, ", channels);
175  }
177  if (size < 8)
178  goto fail;
179  layout = AV_RL64(data);
180  data += 8;
181  size -= 8;
182  av_log(ctx, AV_LOG_INFO,
183  "channel layout: %s, ", av_get_channel_name(layout));
184  }
186  if (size < 4)
187  goto fail;
188  sample_rate = AV_RL32(data);
189  data += 4;
190  size -= 4;
191  av_log(ctx, AV_LOG_INFO, "sample_rate %d, ", sample_rate);
192  }
194  if (size < 8)
195  goto fail;
196  width = AV_RL32(data);
197  data += 4;
198  size -= 4;
199  height = AV_RL32(data);
200  data += 4;
201  size -= 4;
202  av_log(ctx, AV_LOG_INFO, "width %d height %d", width, height);
203  }
204 
205  return;
206 fail:
207  av_log(ctx, AV_LOG_INFO, "unknown param");
208 }
209 
210 /* replaygain side data*/
211 static void print_gain(void *ctx, const char *str, int32_t gain)
212 {
213  av_log(ctx, AV_LOG_INFO, "%s - ", str);
214  if (gain == INT32_MIN)
215  av_log(ctx, AV_LOG_INFO, "unknown");
216  else
217  av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
218  av_log(ctx, AV_LOG_INFO, ", ");
219 }
220 
221 static void print_peak(void *ctx, const char *str, uint32_t peak)
222 {
223  av_log(ctx, AV_LOG_INFO, "%s - ", str);
224  if (!peak)
225  av_log(ctx, AV_LOG_INFO, "unknown");
226  else
227  av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
228  av_log(ctx, AV_LOG_INFO, ", ");
229 }
230 
231 static void dump_replaygain(void *ctx, AVPacketSideData *sd)
232 {
233  AVReplayGain *rg;
234 
235  if (sd->size < sizeof(*rg)) {
236  av_log(ctx, AV_LOG_INFO, "invalid data");
237  return;
238  }
239  rg = (AVReplayGain*)sd->data;
240 
241  print_gain(ctx, "track gain", rg->track_gain);
242  print_peak(ctx, "track peak", rg->track_peak);
243  print_gain(ctx, "album gain", rg->album_gain);
244  print_peak(ctx, "album peak", rg->album_peak);
245 }
246 
247 static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
248 {
249  int i;
250 
251  if (st->nb_side_data)
252  av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
253 
254  for (i = 0; i < st->nb_side_data; i++) {
255  AVPacketSideData sd = st->side_data[i];
256  av_log(ctx, AV_LOG_INFO, "%s ", indent);
257 
258  switch (sd.type) {
259  case AV_PKT_DATA_PALETTE:
260  av_log(ctx, AV_LOG_INFO, "palette");
261  break;
263  av_log(ctx, AV_LOG_INFO, "new extradata");
264  break;
266  av_log(ctx, AV_LOG_INFO, "paramchange: ");
267  dump_paramchange(ctx, &sd);
268  break;
270  av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
271  break;
273  av_log(ctx, AV_LOG_INFO, "replaygain: ");
274  dump_replaygain(ctx, &sd);
275  break;
277  av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
279  break;
280  default:
281  av_log(ctx, AV_LOG_WARNING,
282  "unknown side data type %d (%d bytes)", sd.type, sd.size);
283  break;
284  }
285 
286  av_log(ctx, AV_LOG_INFO, "\n");
287  }
288 }
289 
290 /* "user interface" functions */
291 static void dump_stream_format(AVFormatContext *ic, int i,
292  int index, int is_output)
293 {
294  char buf[256];
295  int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
296  AVStream *st = ic->streams[i];
297  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
298 
299  avcodec_string(buf, sizeof(buf), st->codec, is_output);
300  av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
301 
302  /* the pid is an important information, so we display it */
303  /* XXX: add a generic system */
304  if (flags & AVFMT_SHOW_IDS)
305  av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
306  if (lang)
307  av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
308  av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
309  st->time_base.num, st->time_base.den);
310  av_log(NULL, AV_LOG_INFO, ": %s", buf);
311 
312  if (st->sample_aspect_ratio.num && // default
314  AVRational display_aspect_ratio;
315  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
316  st->codec->width * st->sample_aspect_ratio.num,
317  st->codec->height * st->sample_aspect_ratio.den,
318  1024 * 1024);
319  av_log(NULL, AV_LOG_INFO, ", SAR %d:%d DAR %d:%d",
321  display_aspect_ratio.num, display_aspect_ratio.den);
322  }
323 
324  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
325  if (st->avg_frame_rate.den && st->avg_frame_rate.num)
326  print_fps(av_q2d(st->avg_frame_rate), "fps");
327 #if FF_API_R_FRAME_RATE
328  if (st->r_frame_rate.den && st->r_frame_rate.num)
329  print_fps(av_q2d(st->r_frame_rate), "tbr");
330 #endif
331  if (st->time_base.den && st->time_base.num)
332  print_fps(1 / av_q2d(st->time_base), "tbn");
333  if (st->codec->time_base.den && st->codec->time_base.num)
334  print_fps(1 / av_q2d(st->codec->time_base), "tbc");
335  }
336 
338  av_log(NULL, AV_LOG_INFO, " (default)");
340  av_log(NULL, AV_LOG_INFO, " (dub)");
342  av_log(NULL, AV_LOG_INFO, " (original)");
344  av_log(NULL, AV_LOG_INFO, " (comment)");
346  av_log(NULL, AV_LOG_INFO, " (lyrics)");
348  av_log(NULL, AV_LOG_INFO, " (karaoke)");
350  av_log(NULL, AV_LOG_INFO, " (forced)");
352  av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
354  av_log(NULL, AV_LOG_INFO, " (visual impaired)");
356  av_log(NULL, AV_LOG_INFO, " (clean effects)");
357  av_log(NULL, AV_LOG_INFO, "\n");
358 
359  dump_metadata(NULL, st->metadata, " ");
360 
361  dump_sidedata(NULL, st, " ");
362 }
363 
365  const char *url, int is_output)
366 {
367  int i;
368  uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
369  if (ic->nb_streams && !printed)
370  return;
371 
372  av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
373  is_output ? "Output" : "Input",
374  index,
375  is_output ? ic->oformat->name : ic->iformat->name,
376  is_output ? "to" : "from", url);
377  dump_metadata(NULL, ic->metadata, " ");
378 
379  if (!is_output) {
380  av_log(NULL, AV_LOG_INFO, " Duration: ");
381  if (ic->duration != AV_NOPTS_VALUE) {
382  int hours, mins, secs, us;
383  int64_t duration = ic->duration + 5000;
384  secs = duration / AV_TIME_BASE;
385  us = duration % AV_TIME_BASE;
386  mins = secs / 60;
387  secs %= 60;
388  hours = mins / 60;
389  mins %= 60;
390  av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
391  (100 * us) / AV_TIME_BASE);
392  } else {
393  av_log(NULL, AV_LOG_INFO, "N/A");
394  }
395  if (ic->start_time != AV_NOPTS_VALUE) {
396  int secs, us;
397  av_log(NULL, AV_LOG_INFO, ", start: ");
398  secs = ic->start_time / AV_TIME_BASE;
399  us = abs(ic->start_time % AV_TIME_BASE);
400  av_log(NULL, AV_LOG_INFO, "%d.%06d",
401  secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
402  }
403  av_log(NULL, AV_LOG_INFO, ", bitrate: ");
404  if (ic->bit_rate)
405  av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
406  else
407  av_log(NULL, AV_LOG_INFO, "N/A");
408  av_log(NULL, AV_LOG_INFO, "\n");
409  }
410 
411  for (i = 0; i < ic->nb_chapters; i++) {
412  AVChapter *ch = ic->chapters[i];
413  av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
414  av_log(NULL, AV_LOG_INFO,
415  "start %f, ", ch->start * av_q2d(ch->time_base));
416  av_log(NULL, AV_LOG_INFO,
417  "end %f\n", ch->end * av_q2d(ch->time_base));
418 
419  dump_metadata(NULL, ch->metadata, " ");
420  }
421 
422  if (ic->nb_programs) {
423  int j, k, total = 0;
424  for (j = 0; j < ic->nb_programs; j++) {
426  "name", NULL, 0);
427  av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
428  name ? name->value : "");
429  dump_metadata(NULL, ic->programs[j]->metadata, " ");
430  for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
432  index, is_output);
433  printed[ic->programs[j]->stream_index[k]] = 1;
434  }
435  total += ic->programs[j]->nb_stream_indexes;
436  }
437  if (total < ic->nb_streams)
438  av_log(NULL, AV_LOG_INFO, " No Program\n");
439  }
440 
441  for (i = 0; i < ic->nb_streams; i++)
442  if (!printed[i])
443  dump_stream_format(ic, i, index, is_output);
444 
445  av_free(printed);
446 }