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.

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