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.

427 lines
14KB

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