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.

447 lines
15KB

  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. #include <stdio.h>
  22. #include <stdint.h>
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/display.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/replaygain.h"
  30. #include "avformat.h"
  31. #define HEXDUMP_PRINT(...) \
  32. do { \
  33. if (!f) \
  34. av_log(avcl, level, __VA_ARGS__); \
  35. else \
  36. fprintf(f, __VA_ARGS__); \
  37. } while (0)
  38. static void hex_dump_internal(void *avcl, FILE *f, int level,
  39. const uint8_t *buf, int size)
  40. {
  41. int len, i, j, c;
  42. for (i = 0; i < size; i += 16) {
  43. len = size - i;
  44. if (len > 16)
  45. len = 16;
  46. HEXDUMP_PRINT("%08x ", i);
  47. for (j = 0; j < 16; j++) {
  48. if (j < len)
  49. HEXDUMP_PRINT(" %02x", buf[i + j]);
  50. else
  51. HEXDUMP_PRINT(" ");
  52. }
  53. HEXDUMP_PRINT(" ");
  54. for (j = 0; j < len; j++) {
  55. c = buf[i + j];
  56. if (c < ' ' || c > '~')
  57. c = '.';
  58. HEXDUMP_PRINT("%c", c);
  59. }
  60. HEXDUMP_PRINT("\n");
  61. }
  62. }
  63. void av_hex_dump(FILE *f, const uint8_t *buf, int size)
  64. {
  65. hex_dump_internal(NULL, f, 0, buf, size);
  66. }
  67. void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
  68. {
  69. hex_dump_internal(avcl, NULL, level, buf, size);
  70. }
  71. static void pkt_dump_internal(void *avcl, FILE *f, int level, const AVPacket *pkt,
  72. int dump_payload, AVRational time_base)
  73. {
  74. HEXDUMP_PRINT("stream #%d:\n", pkt->stream_index);
  75. HEXDUMP_PRINT(" keyframe=%d\n", (pkt->flags & AV_PKT_FLAG_KEY) != 0);
  76. HEXDUMP_PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
  77. /* DTS is _always_ valid after av_read_frame() */
  78. HEXDUMP_PRINT(" dts=");
  79. if (pkt->dts == AV_NOPTS_VALUE)
  80. HEXDUMP_PRINT("N/A");
  81. else
  82. HEXDUMP_PRINT("%0.3f", pkt->dts * av_q2d(time_base));
  83. /* PTS may not be known if B-frames are present. */
  84. HEXDUMP_PRINT(" pts=");
  85. if (pkt->pts == AV_NOPTS_VALUE)
  86. HEXDUMP_PRINT("N/A");
  87. else
  88. HEXDUMP_PRINT("%0.3f", pkt->pts * av_q2d(time_base));
  89. HEXDUMP_PRINT("\n");
  90. HEXDUMP_PRINT(" size=%d\n", pkt->size);
  91. if (dump_payload)
  92. av_hex_dump(f, pkt->data, pkt->size);
  93. }
  94. void av_pkt_dump2(FILE *f, const AVPacket *pkt, int dump_payload, const AVStream *st)
  95. {
  96. pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
  97. }
  98. void av_pkt_dump_log2(void *avcl, int level, const AVPacket *pkt, int dump_payload,
  99. const AVStream *st)
  100. {
  101. pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
  102. }
  103. static void print_fps(double d, const char *postfix)
  104. {
  105. uint64_t v = lrintf(d * 100);
  106. if (v % 100)
  107. av_log(NULL, AV_LOG_INFO, ", %3.2f %s", d, postfix);
  108. else if (v % (100 * 1000))
  109. av_log(NULL, AV_LOG_INFO, ", %1.0f %s", d, postfix);
  110. else
  111. av_log(NULL, AV_LOG_INFO, ", %1.0fk %s", d / 1000, postfix);
  112. }
  113. static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
  114. {
  115. if (m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))) {
  116. AVDictionaryEntry *tag = NULL;
  117. av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
  118. while ((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX)))
  119. if (strcmp("language", tag->key)) {
  120. const char *p = tag->value;
  121. av_log(ctx, AV_LOG_INFO,
  122. "%s %-16s: ", indent, tag->key);
  123. while (*p) {
  124. char tmp[256];
  125. size_t len = strcspn(p, "\x8\xa\xb\xc\xd");
  126. av_strlcpy(tmp, p, FFMIN(sizeof(tmp), len+1));
  127. av_log(ctx, AV_LOG_INFO, "%s", tmp);
  128. p += len;
  129. if (*p == 0xd) av_log(ctx, AV_LOG_INFO, " ");
  130. if (*p == 0xa) av_log(ctx, AV_LOG_INFO, "\n%s %-16s: ", indent, "");
  131. if (*p) p++;
  132. }
  133. av_log(ctx, AV_LOG_INFO, "\n");
  134. }
  135. }
  136. }
  137. /* param change side data*/
  138. static void dump_paramchange(void *ctx, AVPacketSideData *sd)
  139. {
  140. int size = sd->size;
  141. const uint8_t *data = sd->data;
  142. uint32_t flags, channels, sample_rate, width, height;
  143. uint64_t layout;
  144. if (!data || sd->size < 4)
  145. goto fail;
  146. flags = AV_RL32(data);
  147. data += 4;
  148. size -= 4;
  149. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  150. if (size < 4)
  151. goto fail;
  152. channels = AV_RL32(data);
  153. data += 4;
  154. size -= 4;
  155. av_log(ctx, AV_LOG_INFO, "channel count %d, ", channels);
  156. }
  157. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  158. if (size < 8)
  159. goto fail;
  160. layout = AV_RL64(data);
  161. data += 8;
  162. size -= 8;
  163. av_log(ctx, AV_LOG_INFO,
  164. "channel layout: %s, ", av_get_channel_name(layout));
  165. }
  166. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  167. if (size < 4)
  168. goto fail;
  169. sample_rate = AV_RL32(data);
  170. data += 4;
  171. size -= 4;
  172. av_log(ctx, AV_LOG_INFO, "sample_rate %d, ", sample_rate);
  173. }
  174. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  175. if (size < 8)
  176. goto fail;
  177. width = AV_RL32(data);
  178. data += 4;
  179. size -= 4;
  180. height = AV_RL32(data);
  181. data += 4;
  182. size -= 4;
  183. av_log(ctx, AV_LOG_INFO, "width %d height %d", width, height);
  184. }
  185. return;
  186. fail:
  187. av_log(ctx, AV_LOG_INFO, "unknown param");
  188. }
  189. /* replaygain side data*/
  190. static void print_gain(void *ctx, const char *str, int32_t gain)
  191. {
  192. av_log(ctx, AV_LOG_INFO, "%s - ", str);
  193. if (gain == INT32_MIN)
  194. av_log(ctx, AV_LOG_INFO, "unknown");
  195. else
  196. av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
  197. av_log(ctx, AV_LOG_INFO, ", ");
  198. }
  199. static void print_peak(void *ctx, const char *str, uint32_t peak)
  200. {
  201. av_log(ctx, AV_LOG_INFO, "%s - ", str);
  202. if (!peak)
  203. av_log(ctx, AV_LOG_INFO, "unknown");
  204. else
  205. av_log(ctx, AV_LOG_INFO, "%f", (float) peak / UINT32_MAX);
  206. av_log(ctx, AV_LOG_INFO, ", ");
  207. }
  208. static void dump_replaygain(void *ctx, AVPacketSideData *sd)
  209. {
  210. AVReplayGain *rg;
  211. if (sd->size < sizeof(*rg)) {
  212. av_log(ctx, AV_LOG_INFO, "invalid data");
  213. return;
  214. }
  215. rg = (AVReplayGain*)sd->data;
  216. print_gain(ctx, "track gain", rg->track_gain);
  217. print_peak(ctx, "track peak", rg->track_peak);
  218. print_gain(ctx, "album gain", rg->album_gain);
  219. print_peak(ctx, "album peak", rg->album_peak);
  220. }
  221. static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
  222. {
  223. int i;
  224. if (st->nb_side_data)
  225. av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
  226. for (i = 0; i < st->nb_side_data; i++) {
  227. AVPacketSideData sd = st->side_data[i];
  228. av_log(ctx, AV_LOG_INFO, "%s ", indent);
  229. switch (sd.type) {
  230. case AV_PKT_DATA_PALETTE:
  231. av_log(ctx, AV_LOG_INFO, "palette");
  232. break;
  233. case AV_PKT_DATA_NEW_EXTRADATA:
  234. av_log(ctx, AV_LOG_INFO, "new extradata");
  235. break;
  236. case AV_PKT_DATA_PARAM_CHANGE:
  237. av_log(ctx, AV_LOG_INFO, "paramchange: ");
  238. dump_paramchange(ctx, &sd);
  239. break;
  240. case AV_PKT_DATA_H263_MB_INFO:
  241. av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
  242. break;
  243. case AV_PKT_DATA_REPLAYGAIN:
  244. av_log(ctx, AV_LOG_INFO, "replaygain: ");
  245. dump_replaygain(ctx, &sd);
  246. break;
  247. case AV_PKT_DATA_DISPLAYMATRIX:
  248. av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
  249. av_display_rotation_get((int32_t *)sd.data));
  250. break;
  251. default:
  252. av_log(ctx, AV_LOG_WARNING,
  253. "unknown side data type %d (%d bytes)", sd.type, sd.size);
  254. break;
  255. }
  256. av_log(ctx, AV_LOG_INFO, "\n");
  257. }
  258. }
  259. /* "user interface" functions */
  260. static void dump_stream_format(AVFormatContext *ic, int i,
  261. int index, int is_output)
  262. {
  263. char buf[256];
  264. int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
  265. AVStream *st = ic->streams[i];
  266. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
  267. avcodec_string(buf, sizeof(buf), st->codec, is_output);
  268. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
  269. /* the pid is an important information, so we display it */
  270. /* XXX: add a generic system */
  271. if (flags & AVFMT_SHOW_IDS)
  272. av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
  273. if (lang)
  274. av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
  275. av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
  276. st->time_base.num, st->time_base.den);
  277. av_log(NULL, AV_LOG_INFO, ": %s", buf);
  278. if (st->sample_aspect_ratio.num && // default
  279. av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
  280. AVRational display_aspect_ratio;
  281. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  282. st->codec->width * st->sample_aspect_ratio.num,
  283. st->codec->height * st->sample_aspect_ratio.den,
  284. 1024 * 1024);
  285. av_log(NULL, AV_LOG_INFO, ", SAR %d:%d DAR %d:%d",
  286. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  287. display_aspect_ratio.num, display_aspect_ratio.den);
  288. }
  289. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  290. if (st->avg_frame_rate.den && st->avg_frame_rate.num)
  291. print_fps(av_q2d(st->avg_frame_rate), "fps");
  292. #if FF_API_R_FRAME_RATE
  293. if (st->r_frame_rate.den && st->r_frame_rate.num)
  294. print_fps(av_q2d(st->r_frame_rate), "tbr");
  295. #endif
  296. if (st->time_base.den && st->time_base.num)
  297. print_fps(1 / av_q2d(st->time_base), "tbn");
  298. if (st->codec->time_base.den && st->codec->time_base.num)
  299. print_fps(1 / av_q2d(st->codec->time_base), "tbc");
  300. }
  301. if (st->disposition & AV_DISPOSITION_DEFAULT)
  302. av_log(NULL, AV_LOG_INFO, " (default)");
  303. if (st->disposition & AV_DISPOSITION_DUB)
  304. av_log(NULL, AV_LOG_INFO, " (dub)");
  305. if (st->disposition & AV_DISPOSITION_ORIGINAL)
  306. av_log(NULL, AV_LOG_INFO, " (original)");
  307. if (st->disposition & AV_DISPOSITION_COMMENT)
  308. av_log(NULL, AV_LOG_INFO, " (comment)");
  309. if (st->disposition & AV_DISPOSITION_LYRICS)
  310. av_log(NULL, AV_LOG_INFO, " (lyrics)");
  311. if (st->disposition & AV_DISPOSITION_KARAOKE)
  312. av_log(NULL, AV_LOG_INFO, " (karaoke)");
  313. if (st->disposition & AV_DISPOSITION_FORCED)
  314. av_log(NULL, AV_LOG_INFO, " (forced)");
  315. if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
  316. av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
  317. if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
  318. av_log(NULL, AV_LOG_INFO, " (visual impaired)");
  319. if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
  320. av_log(NULL, AV_LOG_INFO, " (clean effects)");
  321. av_log(NULL, AV_LOG_INFO, "\n");
  322. dump_metadata(NULL, st->metadata, " ");
  323. dump_sidedata(NULL, st, " ");
  324. }
  325. void av_dump_format(AVFormatContext *ic, int index,
  326. const char *url, int is_output)
  327. {
  328. int i;
  329. uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
  330. if (ic->nb_streams && !printed)
  331. return;
  332. av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
  333. is_output ? "Output" : "Input",
  334. index,
  335. is_output ? ic->oformat->name : ic->iformat->name,
  336. is_output ? "to" : "from", url);
  337. dump_metadata(NULL, ic->metadata, " ");
  338. if (!is_output) {
  339. av_log(NULL, AV_LOG_INFO, " Duration: ");
  340. if (ic->duration != AV_NOPTS_VALUE) {
  341. int hours, mins, secs, us;
  342. int64_t duration = ic->duration + 5000;
  343. secs = duration / AV_TIME_BASE;
  344. us = duration % AV_TIME_BASE;
  345. mins = secs / 60;
  346. secs %= 60;
  347. hours = mins / 60;
  348. mins %= 60;
  349. av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
  350. (100 * us) / AV_TIME_BASE);
  351. } else {
  352. av_log(NULL, AV_LOG_INFO, "N/A");
  353. }
  354. if (ic->start_time != AV_NOPTS_VALUE) {
  355. int secs, us;
  356. av_log(NULL, AV_LOG_INFO, ", start: ");
  357. secs = ic->start_time / AV_TIME_BASE;
  358. us = abs(ic->start_time % AV_TIME_BASE);
  359. av_log(NULL, AV_LOG_INFO, "%d.%06d",
  360. secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
  361. }
  362. av_log(NULL, AV_LOG_INFO, ", bitrate: ");
  363. if (ic->bit_rate)
  364. av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
  365. else
  366. av_log(NULL, AV_LOG_INFO, "N/A");
  367. av_log(NULL, AV_LOG_INFO, "\n");
  368. }
  369. for (i = 0; i < ic->nb_chapters; i++) {
  370. AVChapter *ch = ic->chapters[i];
  371. av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
  372. av_log(NULL, AV_LOG_INFO,
  373. "start %f, ", ch->start * av_q2d(ch->time_base));
  374. av_log(NULL, AV_LOG_INFO,
  375. "end %f\n", ch->end * av_q2d(ch->time_base));
  376. dump_metadata(NULL, ch->metadata, " ");
  377. }
  378. if (ic->nb_programs) {
  379. int j, k, total = 0;
  380. for (j = 0; j < ic->nb_programs; j++) {
  381. AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
  382. "name", NULL, 0);
  383. av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
  384. name ? name->value : "");
  385. dump_metadata(NULL, ic->programs[j]->metadata, " ");
  386. for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
  387. dump_stream_format(ic, ic->programs[j]->stream_index[k],
  388. index, is_output);
  389. printed[ic->programs[j]->stream_index[k]] = 1;
  390. }
  391. total += ic->programs[j]->nb_stream_indexes;
  392. }
  393. if (total < ic->nb_streams)
  394. av_log(NULL, AV_LOG_INFO, " No Program\n");
  395. }
  396. for (i = 0; i < ic->nb_streams; i++)
  397. if (!printed[i])
  398. dump_stream_format(ic, i, index, is_output);
  399. av_free(printed);
  400. }