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.

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