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.

536 lines
17KB

  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. const char *name;
  211. if (sd->size < sizeof(*stereo)) {
  212. av_log(ctx, AV_LOG_INFO, "invalid data");
  213. return;
  214. }
  215. stereo = (AVStereo3D *)sd->data;
  216. av_log(ctx, AV_LOG_INFO, "%s", av_stereo3d_type_name(stereo->type));
  217. if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
  218. av_log(ctx, AV_LOG_INFO, " (inverted)");
  219. }
  220. static void dump_audioservicetype(void *ctx, AVPacketSideData *sd)
  221. {
  222. enum AVAudioServiceType *ast = (enum AVAudioServiceType *)sd->data;
  223. if (sd->size < sizeof(*ast)) {
  224. av_log(ctx, AV_LOG_INFO, "invalid data");
  225. return;
  226. }
  227. switch (*ast) {
  228. case AV_AUDIO_SERVICE_TYPE_MAIN:
  229. av_log(ctx, AV_LOG_INFO, "main");
  230. break;
  231. case AV_AUDIO_SERVICE_TYPE_EFFECTS:
  232. av_log(ctx, AV_LOG_INFO, "effects");
  233. break;
  234. case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
  235. av_log(ctx, AV_LOG_INFO, "visually impaired");
  236. break;
  237. case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
  238. av_log(ctx, AV_LOG_INFO, "hearing impaired");
  239. break;
  240. case AV_AUDIO_SERVICE_TYPE_DIALOGUE:
  241. av_log(ctx, AV_LOG_INFO, "dialogue");
  242. break;
  243. case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
  244. av_log(ctx, AV_LOG_INFO, "comentary");
  245. break;
  246. case AV_AUDIO_SERVICE_TYPE_EMERGENCY:
  247. av_log(ctx, AV_LOG_INFO, "emergency");
  248. break;
  249. case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:
  250. av_log(ctx, AV_LOG_INFO, "voice over");
  251. break;
  252. case AV_AUDIO_SERVICE_TYPE_KARAOKE:
  253. av_log(ctx, AV_LOG_INFO, "karaoke");
  254. break;
  255. default:
  256. av_log(ctx, AV_LOG_WARNING, "unknown");
  257. break;
  258. }
  259. }
  260. static void dump_cpb(void *ctx, AVPacketSideData *sd)
  261. {
  262. AVCPBProperties *cpb = (AVCPBProperties *)sd->data;
  263. if (sd->size < sizeof(*cpb)) {
  264. av_log(ctx, AV_LOG_INFO, "invalid data");
  265. return;
  266. }
  267. av_log(ctx, AV_LOG_INFO,
  268. "bitrate max/min/avg: %d/%d/%d buffer size: %d vbv_delay: %"PRId64,
  269. cpb->max_bitrate, cpb->min_bitrate, cpb->avg_bitrate,
  270. cpb->buffer_size,
  271. cpb->vbv_delay);
  272. }
  273. static void dump_sidedata(void *ctx, AVStream *st, const char *indent)
  274. {
  275. int i;
  276. if (st->nb_side_data)
  277. av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);
  278. for (i = 0; i < st->nb_side_data; i++) {
  279. AVPacketSideData sd = st->side_data[i];
  280. av_log(ctx, AV_LOG_INFO, "%s ", indent);
  281. switch (sd.type) {
  282. case AV_PKT_DATA_PALETTE:
  283. av_log(ctx, AV_LOG_INFO, "palette");
  284. break;
  285. case AV_PKT_DATA_NEW_EXTRADATA:
  286. av_log(ctx, AV_LOG_INFO, "new extradata");
  287. break;
  288. case AV_PKT_DATA_PARAM_CHANGE:
  289. av_log(ctx, AV_LOG_INFO, "paramchange: ");
  290. dump_paramchange(ctx, &sd);
  291. break;
  292. case AV_PKT_DATA_H263_MB_INFO:
  293. av_log(ctx, AV_LOG_INFO, "H.263 macroblock info");
  294. break;
  295. case AV_PKT_DATA_REPLAYGAIN:
  296. av_log(ctx, AV_LOG_INFO, "replaygain: ");
  297. dump_replaygain(ctx, &sd);
  298. break;
  299. case AV_PKT_DATA_DISPLAYMATRIX:
  300. av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
  301. av_display_rotation_get((int32_t *)sd.data));
  302. break;
  303. case AV_PKT_DATA_STEREO3D:
  304. av_log(ctx, AV_LOG_INFO, "stereo3d: ");
  305. dump_stereo3d(ctx, &sd);
  306. break;
  307. case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
  308. av_log(ctx, AV_LOG_INFO, "audio service type: ");
  309. dump_audioservicetype(ctx, &sd);
  310. break;
  311. case AV_PKT_DATA_QUALITY_FACTOR:
  312. av_log(ctx, AV_LOG_INFO, "quality factor: %d", *(int *)sd.data);
  313. break;
  314. case AV_PKT_DATA_CPB_PROPERTIES:
  315. av_log(ctx, AV_LOG_INFO, "cpb: ");
  316. dump_cpb(ctx, &sd);
  317. break;
  318. default:
  319. av_log(ctx, AV_LOG_WARNING,
  320. "unknown side data type %d (%d bytes)", sd.type, sd.size);
  321. break;
  322. }
  323. av_log(ctx, AV_LOG_INFO, "\n");
  324. }
  325. }
  326. /* "user interface" functions */
  327. static void dump_stream_format(AVFormatContext *ic, int i,
  328. int index, int is_output)
  329. {
  330. char buf[256];
  331. int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
  332. AVStream *st = ic->streams[i];
  333. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
  334. AVCodecContext *avctx;
  335. int ret;
  336. avctx = avcodec_alloc_context3(NULL);
  337. if (!avctx)
  338. return;
  339. ret = avcodec_parameters_to_context(avctx, st->codecpar);
  340. if (ret < 0) {
  341. avcodec_free_context(&avctx);
  342. return;
  343. }
  344. avcodec_string(buf, sizeof(buf), avctx, is_output);
  345. avcodec_free_context(&avctx);
  346. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d", index, i);
  347. /* the pid is an important information, so we display it */
  348. /* XXX: add a generic system */
  349. if (flags & AVFMT_SHOW_IDS)
  350. av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
  351. if (lang)
  352. av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
  353. av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
  354. st->time_base.num, st->time_base.den);
  355. av_log(NULL, AV_LOG_INFO, ": %s", buf);
  356. if (st->sample_aspect_ratio.num) {
  357. AVRational display_aspect_ratio;
  358. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  359. st->codecpar->width * st->sample_aspect_ratio.num,
  360. st->codecpar->height * st->sample_aspect_ratio.den,
  361. 1024 * 1024);
  362. av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
  363. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  364. display_aspect_ratio.num, display_aspect_ratio.den);
  365. }
  366. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  367. int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
  368. int tbn = st->time_base.den && st->time_base.num;
  369. if (fps || tbn)
  370. av_log(NULL, AV_LOG_INFO, "\n ");
  371. if (fps)
  372. print_fps(av_q2d(st->avg_frame_rate), tbn ? "fps, " : "fps");
  373. if (tbn)
  374. print_fps(1 / av_q2d(st->time_base), "tbn");
  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 = llabs(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. }