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.

530 lines
18KB

  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_audioservicetype(void *ctx, AVPacketSideData *sd)
  248. {
  249. enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
  250. if (sd->size < sizeof(*ast)) {
  251. av_log(ctx, AV_LOG_INFO, "invalid data");
  252. return;
  253. }
  254. switch (*ast) {
  255. case AV_AUDIO_SERVICE_TYPE_MAIN:
  256. av_log(ctx, AV_LOG_INFO, "main");
  257. break;
  258. case AV_AUDIO_SERVICE_TYPE_EFFECTS:
  259. av_log(ctx, AV_LOG_INFO, "effects");
  260. break;
  261. case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
  262. av_log(ctx, AV_LOG_INFO, "visually impaired");
  263. break;
  264. case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
  265. av_log(ctx, AV_LOG_INFO, "hearing impaired");
  266. break;
  267. case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
  268. av_log(ctx, AV_LOG_INFO, "dialogue");
  269. break;
  270. case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
  271. av_log(ctx, AV_LOG_INFO, "comentary");
  272. break;
  273. case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
  274. av_log(ctx, AV_LOG_INFO, "emergency");
  275. break;
  276. case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
  277. av_log(ctx, AV_LOG_INFO, "voice over");
  278. break;
  279. case AV_AUDIO_SERVICE_TYPE_KARAOKE:
  280. av_log(ctx, AV_LOG_INFO, "karaoke");
  281. break;
  282. default:
  283. av_log(ctx, AV_LOG_WARNING, "unknown");
  284. break;
  285. }
  286. }
  287. static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
  288. {
  289. int i;
  290. if (st->nb_side_data)
  291. av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
  292. for (i = 0; i < st->nb_side_data; i++) {
  293. AVPacketSideData sd = st->side_data[i];
  294. av_log(ctx, AV_LOG_INFO, "%s ", indent);
  295. switch (sd.type) {
  296. case AV_PKT_DATA_PALETTE:
  297. av_log(ctx, AV_LOG_INFO, "palette");
  298. break;
  299. case AV_PKT_DATA_NEW_EXTRADATA:
  300. av_log(ctx, AV_LOG_INFO, "new extradata");
  301. break;
  302. case AV_PKT_DATA_PARAM_CHANGE:
  303. av_log(ctx, AV_LOG_INFO, "paramchange: ");
  304. dump_paramchange(ctx, &sd);
  305. break;
  306. case AV_PKT_DATA_H263_MB_INFO:
  307. av_log(ctx, AV_LOG_INFO, "h263 macroblock info");
  308. break;
  309. case AV_PKT_DATA_REPLAYGAIN:
  310. av_log(ctx, AV_LOG_INFO, "replaygain: ");
  311. dump_replaygain(ctx, &sd);
  312. break;
  313. case AV_PKT_DATA_DISPLAYMATRIX:
  314. av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
  315. av_display_rotation_get((int32_t *)sd.data));
  316. break;
  317. case AV_PKT_DATA_STEREO3D:
  318. av_log(ctx, AV_LOG_INFO, "stereo3d: ");
  319. dump_stereo3d(ctx, &sd);
  320. break;
  321. case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
  322. av_log(ctx, AV_LOG_INFO, "audio service type: ");
  323. dump_audioservicetype(ctx, &sd);
  324. break;
  325. default:
  326. av_log(ctx, AV_LOG_WARNING,
  327. "unknown side data type %d (%d bytes)", sd.type, sd.size);
  328. break;
  329. }
  330. av_log(ctx, AV_LOG_INFO, "\n");
  331. }
  332. }
  333. /* "user interface" functions */
  334. static void dump_stream_format(AVFormatContext *ic, int i,
  335. int index, int is_output)
  336. {
  337. char buf[256];
  338. int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
  339. AVStream *st = ic->streams[i];
  340. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
  341. avcodec_string(buf, sizeof(buf), st->codec, is_output);
  342. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
  343. /* the pid is an important information, so we display it */
  344. /* XXX: add a generic system */
  345. if (flags & AVFMT_SHOW_IDS)
  346. av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
  347. if (lang)
  348. av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
  349. av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
  350. st->time_base.num, st->time_base.den);
  351. av_log(NULL, AV_LOG_INFO, ": %s", buf);
  352. if (st->sample_aspect_ratio.num && // default
  353. av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
  354. AVRational display_aspect_ratio;
  355. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  356. st->codec->width * st->sample_aspect_ratio.num,
  357. st->codec->height * st->sample_aspect_ratio.den,
  358. 1024 * 1024);
  359. av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
  360. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  361. display_aspect_ratio.num, display_aspect_ratio.den);
  362. }
  363. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  364. int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
  365. int tbn = st->time_base.den && st->time_base.num;
  366. int tbc = st->codec->time_base.den && st->codec->time_base.num;
  367. if (fps || tbn || tbc)
  368. av_log(NULL, AV_LOG_INFO, "\n ");
  369. if (fps)
  370. print_fps(av_q2d(st->avg_frame_rate), tbn || tbc ? "fps, " : "fps");
  371. if (tbn)
  372. print_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
  373. if (tbc)
  374. print_fps(1 / av_q2d(st->codec->time_base), "tbc");
  375. }
  376. if (st->disposition & AV_DISPOSITION_DEFAULT)
  377. av_log(NULL, AV_LOG_INFO, " (default)");
  378. if (st->disposition & AV_DISPOSITION_DUB)
  379. av_log(NULL, AV_LOG_INFO, " (dub)");
  380. if (st->disposition & AV_DISPOSITION_ORIGINAL)
  381. av_log(NULL, AV_LOG_INFO, " (original)");
  382. if (st->disposition & AV_DISPOSITION_COMMENT)
  383. av_log(NULL, AV_LOG_INFO, " (comment)");
  384. if (st->disposition & AV_DISPOSITION_LYRICS)
  385. av_log(NULL, AV_LOG_INFO, " (lyrics)");
  386. if (st->disposition & AV_DISPOSITION_KARAOKE)
  387. av_log(NULL, AV_LOG_INFO, " (karaoke)");
  388. if (st->disposition & AV_DISPOSITION_FORCED)
  389. av_log(NULL, AV_LOG_INFO, " (forced)");
  390. if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
  391. av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
  392. if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
  393. av_log(NULL, AV_LOG_INFO, " (visual impaired)");
  394. if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
  395. av_log(NULL, AV_LOG_INFO, " (clean effects)");
  396. av_log(NULL, AV_LOG_INFO, "\n");
  397. dump_metadata(NULL, st->metadata, " ");
  398. dump_sidedata(NULL, st, " ");
  399. }
  400. void av_dump_format(AVFormatContext *ic, int index,
  401. const char *url, int is_output)
  402. {
  403. int i;
  404. uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
  405. if (ic->nb_streams && !printed)
  406. return;
  407. av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
  408. is_output ? "Output" : "Input",
  409. index,
  410. is_output ? ic->oformat->name : ic->iformat->name,
  411. is_output ? "to" : "from", url);
  412. dump_metadata(NULL, ic->metadata, " ");
  413. if (!is_output) {
  414. av_log(NULL, AV_LOG_INFO, " Duration: ");
  415. if (ic->duration != AV_NOPTS_VALUE) {
  416. int hours, mins, secs, us;
  417. secs = ic->duration / AV_TIME_BASE;
  418. us = ic->duration % AV_TIME_BASE;
  419. mins = secs / 60;
  420. secs %= 60;
  421. hours = mins / 60;
  422. mins %= 60;
  423. av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
  424. (100 * us) / AV_TIME_BASE);
  425. } else {
  426. av_log(NULL, AV_LOG_INFO, "N/A");
  427. }
  428. if (ic->start_time != AV_NOPTS_VALUE) {
  429. int secs, us;
  430. av_log(NULL, AV_LOG_INFO, ", start: ");
  431. secs = ic->start_time / AV_TIME_BASE;
  432. us = abs(ic->start_time % AV_TIME_BASE);
  433. av_log(NULL, AV_LOG_INFO, "%d.%06d",
  434. secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
  435. }
  436. av_log(NULL, AV_LOG_INFO, ", bitrate: ");
  437. if (ic->bit_rate)
  438. av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
  439. else
  440. av_log(NULL, AV_LOG_INFO, "N/A");
  441. av_log(NULL, AV_LOG_INFO, "\n");
  442. }
  443. for (i = 0; i < ic->nb_chapters; i++) {
  444. AVChapter *ch = ic->chapters[i];
  445. av_log(NULL, AV_LOG_INFO, " Chapter #%d:%d: ", index, i);
  446. av_log(NULL, AV_LOG_INFO,
  447. "start %f, ", ch->start * av_q2d(ch->time_base));
  448. av_log(NULL, AV_LOG_INFO,
  449. "end %f\n", ch->end * av_q2d(ch->time_base));
  450. dump_metadata(NULL, ch->metadata, " ");
  451. }
  452. if (ic->nb_programs) {
  453. int j, k, total = 0;
  454. for (j = 0; j < ic->nb_programs; j++) {
  455. AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
  456. "name", NULL, 0);
  457. av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
  458. name ? name->value : "");
  459. dump_metadata(NULL, ic->programs[j]->metadata, " ");
  460. for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
  461. dump_stream_format(ic, ic->programs[j]->stream_index[k],
  462. index, is_output);
  463. printed[ic->programs[j]->stream_index[k]] = 1;
  464. }
  465. total += ic->programs[j]->nb_stream_indexes;
  466. }
  467. if (total < ic->nb_streams)
  468. av_log(NULL, AV_LOG_INFO, " No Program\n");
  469. }
  470. for (i = 0; i < ic->nb_streams; i++)
  471. if (!printed[i])
  472. dump_stream_format(ic, i, index, is_output);
  473. av_free(printed);
  474. }