You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
11KB

  1. /*
  2. * Various pretty-printing functions for use within FFmpeg
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include "libavutil/log.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/avstring.h"
  25. #include "avformat.h"
  26. #define HEXDUMP_PRINT(...) \
  27. do { \
  28. if (!f) \
  29. av_log(avcl, level, __VA_ARGS__); \
  30. else \
  31. fprintf(f, __VA_ARGS__); \
  32. } while (0)
  33. static void hex_dump_internal(void *avcl, FILE *f, int level,
  34. const uint8_t *buf, int size)
  35. {
  36. int len, i, j, c;
  37. for (i = 0; i < size; i += 16) {
  38. len = size - i;
  39. if (len > 16)
  40. len = 16;
  41. HEXDUMP_PRINT("%08x ", i);
  42. for (j = 0; j < 16; j++) {
  43. if (j < len)
  44. HEXDUMP_PRINT(" %02x", buf[i + j]);
  45. else
  46. HEXDUMP_PRINT(" ");
  47. }
  48. HEXDUMP_PRINT(" ");
  49. for (j = 0; j < len; j++) {
  50. c = buf[i + j];
  51. if (c < ' ' || c > '~')
  52. c = '.';
  53. HEXDUMP_PRINT("%c", c);
  54. }
  55. HEXDUMP_PRINT("\n");
  56. }
  57. }
  58. void av_hex_dump(FILE *f, const uint8_t *buf, int size)
  59. {
  60. hex_dump_internal(NULL, f, 0, buf, size);
  61. }
  62. void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
  63. {
  64. hex_dump_internal(avcl, NULL, level, buf, size);
  65. }
  66. static void pkt_dump_internal(void *avcl, FILE *f, int level, const AVPacket *pkt,
  67. int dump_payload, AVRational time_base)
  68. {
  69. HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
  70. HEXDUMP_PRINT(" keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
  71. HEXDUMP_PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
  72. /* DTS is _always_ valid after av_read_frame() */
  73. HEXDUMP_PRINT(" dts=");
  74. if (pkt->dts == AV_NOPTS_VALUE)
  75. HEXDUMP_PRINT("N/A");
  76. else
  77. HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
  78. /* PTS may not be known if B-frames are present. */
  79. HEXDUMP_PRINT(" pts=");
  80. if (pkt->pts == AV_NOPTS_VALUE)
  81. HEXDUMP_PRINT("N/A");
  82. else
  83. HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
  84. HEXDUMP_PRINT("\n");
  85. HEXDUMP_PRINT(" size=%d\n", pkt->size);
  86. if (dump_payload)
  87. av_hex_dump(f, pkt->data, pkt->size);
  88. }
  89. void av_pkt_dump2(FILE *f, const AVPacket *pkt, int dump_payload, const AVStream *st)
  90. {
  91. pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
  92. }
  93. void av_pkt_dump_log2(void *avcl, int level, const AVPacket *pkt, int dump_payload,
  94. const AVStream *st)
  95. {
  96. pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
  97. }
  98. static void print_fps(double d, const char *postfix)
  99. {
  100. uint64_t v = lrintf(d * 100);
  101. if (v % 100)
  102. av_log(NULL, AV_LOG_INFO, ", %3.2f %s", d, postfix);
  103. else if (v % (100 * 1000))
  104. av_log(NULL, AV_LOG_INFO, ", %1.0f %s", d, postfix);
  105. else
  106. av_log(NULL, AV_LOG_INFO, ", %1.0fk %s", d / 1000, postfix);
  107. }
  108. static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
  109. {
  110. if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
  111. AVDictionaryEntry *tag = NULL;
  112. av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
  113. while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
  114. if (strcmp("language", tag->key)) {
  115. const char *p = tag->value;
  116. av_log(ctx, AV_LOG_INFO,
  117. "%s %-16s: ", indent, tag->key);
  118. while (*p) {
  119. char tmp[256];
  120. size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
  121. av_strlcpy(tmp, p, FFMIN(sizeof(tmp), len+1));
  122. av_log(ctx, AV_LOG_INFO, "%s", tmp);
  123. p += len;
  124. if (*p == 0xd) av_log(ctx, AV_LOG_INFO, " ");
  125. if (*p == 0xa) av_log(ctx, AV_LOG_INFO, "\n%s %-16s: ", indent, "");
  126. if (*p) p++;
  127. }
  128. av_log(ctx, AV_LOG_INFO, "\n");
  129. }
  130. }
  131. }
  132. /* "user interface" functions */
  133. static void dump_stream_format(AVFormatContext *ic, int i,
  134. int index, int is_output)
  135. {
  136. char buf[256];
  137. int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
  138. AVStream *st = ic->streams[i];
  139. int g = av_gcd(st->time_base.num, st->time_base.den);
  140. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
  141. avcodec_string(buf, sizeof(buf), st->codec, is_output);
  142. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
  143. /* the pid is an important information, so we display it */
  144. /* XXX: add a generic system */
  145. if (flags & AVFMT_SHOW_IDS)
  146. av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
  147. if (lang)
  148. av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
  149. av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
  150. st->time_base.num / g, st->time_base.den / g);
  151. av_log(NULL, AV_LOG_INFO, ": %s", buf);
  152. if (st->sample_aspect_ratio.num && // default
  153. av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
  154. AVRational display_aspect_ratio;
  155. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  156. st->codec->width * st->sample_aspect_ratio.num,
  157. st->codec->height * st->sample_aspect_ratio.den,
  158. 1024 * 1024);
  159. av_log(NULL, AV_LOG_INFO, ", SAR %d:%d DAR %d:%d",
  160. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  161. display_aspect_ratio.num, display_aspect_ratio.den);
  162. }
  163. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  164. if (st->avg_frame_rate.den && st->avg_frame_rate.num)
  165. print_fps(av_q2d(st->avg_frame_rate), "fps");
  166. #if FF_API_R_FRAME_RATE
  167. if (st->r_frame_rate.den && st->r_frame_rate.num)
  168. print_fps(av_q2d(st->r_frame_rate), "tbr");
  169. #endif
  170. if (st->time_base.den && st->time_base.num)
  171. print_fps(1 / av_q2d(st->time_base), "tbn");
  172. if (st->codec->time_base.den && st->codec->time_base.num)
  173. print_fps(1 / av_q2d(st->codec->time_base), "tbc");
  174. }
  175. if (st->disposition & AV_DISPOSITION_DEFAULT)
  176. av_log(NULL, AV_LOG_INFO, " (default)");
  177. if (st->disposition & AV_DISPOSITION_DUB)
  178. av_log(NULL, AV_LOG_INFO, " (dub)");
  179. if (st->disposition & AV_DISPOSITION_ORIGINAL)
  180. av_log(NULL, AV_LOG_INFO, " (original)");
  181. if (st->disposition & AV_DISPOSITION_COMMENT)
  182. av_log(NULL, AV_LOG_INFO, " (comment)");
  183. if (st->disposition & AV_DISPOSITION_LYRICS)
  184. av_log(NULL, AV_LOG_INFO, " (lyrics)");
  185. if (st->disposition & AV_DISPOSITION_KARAOKE)
  186. av_log(NULL, AV_LOG_INFO, " (karaoke)");
  187. if (st->disposition & AV_DISPOSITION_FORCED)
  188. av_log(NULL, AV_LOG_INFO, " (forced)");
  189. if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
  190. av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
  191. if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
  192. av_log(NULL, AV_LOG_INFO, " (visual impaired)");
  193. if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
  194. av_log(NULL, AV_LOG_INFO, " (clean effects)");
  195. av_log(NULL, AV_LOG_INFO, "\n");
  196. dump_metadata(NULL, st->metadata, " ");
  197. }
  198. void av_dump_format(AVFormatContext *ic, int index,
  199. const char *url, int is_output)
  200. {
  201. int i;
  202. uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
  203. if (ic->nb_streams && !printed)
  204. return;
  205. av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
  206. is_output ? "Output" : "Input",
  207. index,
  208. is_output ? ic->oformat->name : ic->iformat->name,
  209. is_output ? "to" : "from", url);
  210. dump_metadata(NULL, ic->metadata, " ");
  211. if (!is_output) {
  212. av_log(NULL, AV_LOG_INFO, " Duration: ");
  213. if (ic->duration != AV_NOPTS_VALUE) {
  214. int hours, mins, secs, us;
  215. int64_t duration = ic->duration + 5000;
  216. secs = duration / AV_TIME_BASE;
  217. us = duration % AV_TIME_BASE;
  218. mins = secs / 60;
  219. secs %= 60;
  220. hours = mins / 60;
  221. mins %= 60;
  222. av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
  223. (100 * us) / AV_TIME_BASE);
  224. } else {
  225. av_log(NULL, AV_LOG_INFO, "N/A");
  226. }
  227. if (ic->start_time != AV_NOPTS_VALUE) {
  228. int secs, us;
  229. av_log(NULL, AV_LOG_INFO, ", start: ");
  230. secs = ic->start_time / AV_TIME_BASE;
  231. us = abs(ic->start_time % AV_TIME_BASE);
  232. av_log(NULL, AV_LOG_INFO, "%d.%06d",
  233. secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
  234. }
  235. av_log(NULL, AV_LOG_INFO, ", bitrate: ");
  236. if (ic->bit_rate)
  237. av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
  238. else
  239. av_log(NULL, AV_LOG_INFO, "N/A");
  240. av_log(NULL, AV_LOG_INFO, "\n");
  241. }
  242. for (i = 0; i < ic->nb_chapters; i++) {
  243. AVChapter *ch = ic->chapters[i];
  244. av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
  245. av_log(NULL, AV_LOG_INFO,
  246. "start %f, ", ch->start * av_q2d(ch->time_base));
  247. av_log(NULL, AV_LOG_INFO,
  248. "end %f\n", ch->end * av_q2d(ch->time_base));
  249. dump_metadata(NULL, ch->metadata, " ");
  250. }
  251. if (ic->nb_programs) {
  252. int j, k, total = 0;
  253. for (j = 0; j < ic->nb_programs; j++) {
  254. AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
  255. "name", NULL, 0);
  256. av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
  257. name ? name->value : "");
  258. dump_metadata(NULL, ic->programs[j]->metadata, " ");
  259. for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
  260. dump_stream_format(ic, ic->programs[j]->stream_index[k],
  261. index, is_output);
  262. printed[ic->programs[j]->stream_index[k]] = 1;
  263. }
  264. total += ic->programs[j]->nb_stream_indexes;
  265. }
  266. if (total < ic->nb_streams)
  267. av_log(NULL, AV_LOG_INFO, " No Program\n");
  268. }
  269. for (i = 0; i < ic->nb_streams; i++)
  270. if (!printed[i])
  271. dump_stream_format(ic, i, index, is_output);
  272. av_free(printed);
  273. }