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.

477 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. if (st->avg_frame_rate.den && st->avg_frame_rate.num)
  321. print_fps(av_q2d(st->avg_frame_rate), "fps");
  322. if (st->time_base.den && st->time_base.num)
  323. print_fps(1 / av_q2d(st->time_base), "tbn");
  324. if (st->codec->time_base.den && st->codec->time_base.num)
  325. print_fps(1 / av_q2d(st->codec->time_base), "tbc");
  326. }
  327. if (st->disposition & AV_DISPOSITION_DEFAULT)
  328. av_log(NULL, AV_LOG_INFO, " (default)");
  329. if (st->disposition & AV_DISPOSITION_DUB)
  330. av_log(NULL, AV_LOG_INFO, " (dub)");
  331. if (st->disposition & AV_DISPOSITION_ORIGINAL)
  332. av_log(NULL, AV_LOG_INFO, " (original)");
  333. if (st->disposition & AV_DISPOSITION_COMMENT)
  334. av_log(NULL, AV_LOG_INFO, " (comment)");
  335. if (st->disposition & AV_DISPOSITION_LYRICS)
  336. av_log(NULL, AV_LOG_INFO, " (lyrics)");
  337. if (st->disposition & AV_DISPOSITION_KARAOKE)
  338. av_log(NULL, AV_LOG_INFO, " (karaoke)");
  339. if (st->disposition & AV_DISPOSITION_FORCED)
  340. av_log(NULL, AV_LOG_INFO, " (forced)");
  341. if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
  342. av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
  343. if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
  344. av_log(NULL, AV_LOG_INFO, " (visual impaired)");
  345. if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
  346. av_log(NULL, AV_LOG_INFO, " (clean effects)");
  347. av_log(NULL, AV_LOG_INFO, "\n");
  348. dump_metadata(NULL, st->metadata, " ");
  349. dump_sidedata(NULL, st, " ");
  350. }
  351. void av_dump_format(AVFormatContext *ic, int index,
  352. const char *url, int is_output)
  353. {
  354. int i;
  355. uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
  356. if (ic->nb_streams && !printed)
  357. return;
  358. av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
  359. is_output ? "Output" : "Input",
  360. index,
  361. is_output ? ic->oformat->name : ic->iformat->name,
  362. is_output ? "to" : "from", url);
  363. dump_metadata(NULL, ic->metadata, " ");
  364. if (!is_output) {
  365. av_log(NULL, AV_LOG_INFO, " Duration: ");
  366. if (ic->duration != AV_NOPTS_VALUE) {
  367. int hours, mins, secs, us;
  368. secs = ic->duration / AV_TIME_BASE;
  369. us = ic->duration % AV_TIME_BASE;
  370. mins = secs / 60;
  371. secs %= 60;
  372. hours = mins / 60;
  373. mins %= 60;
  374. av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
  375. (100 * us) / AV_TIME_BASE);
  376. } else {
  377. av_log(NULL, AV_LOG_INFO, "N/A");
  378. }
  379. if (ic->start_time != AV_NOPTS_VALUE) {
  380. int secs, us;
  381. av_log(NULL, AV_LOG_INFO, ", start: ");
  382. secs = ic->start_time / AV_TIME_BASE;
  383. us = abs(ic->start_time % AV_TIME_BASE);
  384. av_log(NULL, AV_LOG_INFO, "%d.%06d",
  385. secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
  386. }
  387. av_log(NULL, AV_LOG_INFO, ", bitrate: ");
  388. if (ic->bit_rate)
  389. av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
  390. else
  391. av_log(NULL, AV_LOG_INFO, "N/A");
  392. av_log(NULL, AV_LOG_INFO, "\n");
  393. }
  394. for (i = 0; i < ic->nb_chapters; i++) {
  395. AVChapter *ch = ic->chapters[i];
  396. av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
  397. av_log(NULL, AV_LOG_INFO,
  398. "start %f, ", ch->start * av_q2d(ch->time_base));
  399. av_log(NULL, AV_LOG_INFO,
  400. "end %f\n", ch->end * av_q2d(ch->time_base));
  401. dump_metadata(NULL, ch->metadata, " ");
  402. }
  403. if (ic->nb_programs) {
  404. int j, k, total = 0;
  405. for (j = 0; j < ic->nb_programs; j++) {
  406. AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
  407. "name", NULL, 0);
  408. av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
  409. name ? name->value : "");
  410. dump_metadata(NULL, ic->programs[j]->metadata, " ");
  411. for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
  412. dump_stream_format(ic, ic->programs[j]->stream_index[k],
  413. index, is_output);
  414. printed[ic->programs[j]->stream_index[k]] = 1;
  415. }
  416. total += ic->programs[j]->nb_stream_indexes;
  417. }
  418. if (total < ic->nb_streams)
  419. av_log(NULL, AV_LOG_INFO, " No Program\n");
  420. }
  421. for (i = 0; i < ic->nb_streams; i++)
  422. if (!printed[i])
  423. dump_stream_format(ic, i, index, is_output);
  424. av_free(printed);
  425. }