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.

483 lines
16KB

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