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.

428 lines
15KB

  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. int g = av_gcd(st->time_base.num, st->time_base.den);
  252. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
  253. avcodec_string(buf, sizeof(buf), st->codec, is_output);
  254. av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i);
  255. /* the pid is an important information, so we display it */
  256. /* XXX: add a generic system */
  257. if (flags & AVFMT_SHOW_IDS)
  258. av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
  259. if (lang)
  260. av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
  261. av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
  262. st->time_base.num / g, st->time_base.den / g);
  263. av_log(NULL, AV_LOG_INFO, ": %s", buf);
  264. if (st->sample_aspect_ratio.num && // default
  265. av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
  266. AVRational display_aspect_ratio;
  267. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  268. st->codec->width * st->sample_aspect_ratio.num,
  269. st->codec->height * st->sample_aspect_ratio.den,
  270. 1024 * 1024);
  271. av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
  272. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  273. display_aspect_ratio.num, display_aspect_ratio.den);
  274. }
  275. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  276. if (st->avg_frame_rate.den && st->avg_frame_rate.num)
  277. print_fps(av_q2d(st->avg_frame_rate), "fps");
  278. if (st->time_base.den && st->time_base.num)
  279. print_fps(1 / av_q2d(st->time_base), "tbn");
  280. if (st->codec->time_base.den && st->codec->time_base.num)
  281. print_fps(1 / av_q2d(st->codec->time_base), "tbc");
  282. }
  283. if (st->disposition & AV_DISPOSITION_DEFAULT)
  284. av_log(NULL, AV_LOG_INFO, " (default)");
  285. if (st->disposition & AV_DISPOSITION_DUB)
  286. av_log(NULL, AV_LOG_INFO, " (dub)");
  287. if (st->disposition & AV_DISPOSITION_ORIGINAL)
  288. av_log(NULL, AV_LOG_INFO, " (original)");
  289. if (st->disposition & AV_DISPOSITION_COMMENT)
  290. av_log(NULL, AV_LOG_INFO, " (comment)");
  291. if (st->disposition & AV_DISPOSITION_LYRICS)
  292. av_log(NULL, AV_LOG_INFO, " (lyrics)");
  293. if (st->disposition & AV_DISPOSITION_KARAOKE)
  294. av_log(NULL, AV_LOG_INFO, " (karaoke)");
  295. if (st->disposition & AV_DISPOSITION_FORCED)
  296. av_log(NULL, AV_LOG_INFO, " (forced)");
  297. if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
  298. av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
  299. if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
  300. av_log(NULL, AV_LOG_INFO, " (visual impaired)");
  301. if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
  302. av_log(NULL, AV_LOG_INFO, " (clean effects)");
  303. av_log(NULL, AV_LOG_INFO, "\n");
  304. dump_metadata(NULL, st->metadata, " ");
  305. dump_sidedata(NULL, st, " ");
  306. }
  307. void av_dump_format(AVFormatContext *ic, int index,
  308. const char *url, int is_output)
  309. {
  310. int i;
  311. uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
  312. if (ic->nb_streams && !printed)
  313. return;
  314. av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
  315. is_output ? "Output" : "Input",
  316. index,
  317. is_output ? ic->oformat->name : ic->iformat->name,
  318. is_output ? "to" : "from", url);
  319. dump_metadata(NULL, ic->metadata, " ");
  320. if (!is_output) {
  321. av_log(NULL, AV_LOG_INFO, " Duration: ");
  322. if (ic->duration != AV_NOPTS_VALUE) {
  323. int hours, mins, secs, us;
  324. secs = ic->duration / AV_TIME_BASE;
  325. us = ic->duration % AV_TIME_BASE;
  326. mins = secs / 60;
  327. secs %= 60;
  328. hours = mins / 60;
  329. mins %= 60;
  330. av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
  331. (100 * us) / AV_TIME_BASE);
  332. } else {
  333. av_log(NULL, AV_LOG_INFO, "N/A");
  334. }
  335. if (ic->start_time != AV_NOPTS_VALUE) {
  336. int secs, us;
  337. av_log(NULL, AV_LOG_INFO, ", start: ");
  338. secs = ic->start_time / AV_TIME_BASE;
  339. us = abs(ic->start_time % AV_TIME_BASE);
  340. av_log(NULL, AV_LOG_INFO, "%d.%06d",
  341. secs, (int) av_rescale(us, 1000000, AV_TIME_BASE));
  342. }
  343. av_log(NULL, AV_LOG_INFO, ", bitrate: ");
  344. if (ic->bit_rate)
  345. av_log(NULL, AV_LOG_INFO, "%d kb/s", ic->bit_rate / 1000);
  346. else
  347. av_log(NULL, AV_LOG_INFO, "N/A");
  348. av_log(NULL, AV_LOG_INFO, "\n");
  349. }
  350. for (i = 0; i < ic->nb_chapters; i++) {
  351. AVChapter *ch = ic->chapters[i];
  352. av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
  353. av_log(NULL, AV_LOG_INFO,
  354. "start %f, ", ch->start * av_q2d(ch->time_base));
  355. av_log(NULL, AV_LOG_INFO,
  356. "end %f\n", ch->end * av_q2d(ch->time_base));
  357. dump_metadata(NULL, ch->metadata, " ");
  358. }
  359. if (ic->nb_programs) {
  360. int j, k, total = 0;
  361. for (j = 0; j < ic->nb_programs; j++) {
  362. AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
  363. "name", NULL, 0);
  364. av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
  365. name ? name->value : "");
  366. dump_metadata(NULL, ic->programs[j]->metadata, " ");
  367. for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
  368. dump_stream_format(ic, ic->programs[j]->stream_index[k],
  369. index, is_output);
  370. printed[ic->programs[j]->stream_index[k]] = 1;
  371. }
  372. total += ic->programs[j]->nb_stream_indexes;
  373. }
  374. if (total < ic->nb_streams)
  375. av_log(NULL, AV_LOG_INFO, " No Program\n");
  376. }
  377. for (i = 0; i < ic->nb_streams; i++)
  378. if (!printed[i])
  379. dump_stream_format(ic, i, index, is_output);
  380. av_free(printed);
  381. }