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.

497 lines
17KB

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