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.

178 lines
5.5KB

  1. /*
  2. * Copyright (c) 2013 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/adler32.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/bprint.h"
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. /* Identical to Adler32 when the type is uint8_t. */
  28. #define DEFINE_CKSUM_LINE(name, type, conv) \
  29. static void cksum_line_ ## name(unsigned *cksum, void *data, unsigned size) \
  30. { \
  31. type *p = data; \
  32. unsigned a = *cksum & 0xFFFF, b = *cksum >> 16; \
  33. for (; size > 0; size--, p++) { \
  34. a = (a + (unsigned)(conv)) % 65521; \
  35. b = (b + a) % 65521; \
  36. } \
  37. *cksum = a | (b << 16); \
  38. }
  39. DEFINE_CKSUM_LINE(u8, uint8_t, *p)
  40. DEFINE_CKSUM_LINE(s16, int16_t, *p + 0x8000)
  41. DEFINE_CKSUM_LINE(s32, int32_t, *p + 0x80000000)
  42. DEFINE_CKSUM_LINE(flt, float, *p * 0x80000000 + 0x80000000)
  43. DEFINE_CKSUM_LINE(dbl, double, *p * 0x80000000 + 0x80000000)
  44. static void video_frame_cksum(AVBPrint *bp, AVFrame *frame)
  45. {
  46. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  47. int i, y;
  48. uint8_t *data;
  49. int linesize[5] = { 0 };
  50. av_bprintf(bp, ", %d x %d", frame->width, frame->height);
  51. if (!desc) {
  52. av_bprintf(bp, ", unknown");
  53. return;
  54. }
  55. if (av_image_fill_linesizes(linesize, frame->format, frame->width) < 0)
  56. return;
  57. av_bprintf(bp, ", %s", desc->name);
  58. for (i = 0; linesize[i]; i++) {
  59. unsigned cksum = 0;
  60. int h = frame->height;
  61. if ((i == 1 || i == 2) && desc->nb_components >= 3)
  62. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  63. data = frame->data[i];
  64. for (y = 0; y < h; y++) {
  65. cksum = av_adler32_update(cksum, data, linesize[i]);
  66. data += frame->linesize[i];
  67. }
  68. av_bprintf(bp, ", 0x%08x", cksum);
  69. }
  70. }
  71. static void audio_frame_cksum(AVBPrint *bp, AVFrame *frame)
  72. {
  73. int nb_planes, nb_samples, p;
  74. const char *name;
  75. nb_planes = av_frame_get_channels(frame);
  76. nb_samples = frame->nb_samples;
  77. if (!av_sample_fmt_is_planar(frame->format)) {
  78. nb_samples *= nb_planes;
  79. nb_planes = 1;
  80. }
  81. name = av_get_sample_fmt_name(frame->format);
  82. av_bprintf(bp, ", %d samples", frame->nb_samples);
  83. av_bprintf(bp, ", %s", name ? name : "unknown");
  84. for (p = 0; p < nb_planes; p++) {
  85. uint32_t cksum = 0;
  86. void *d = frame->extended_data[p];
  87. switch (frame->format) {
  88. case AV_SAMPLE_FMT_U8:
  89. case AV_SAMPLE_FMT_U8P:
  90. cksum_line_u8(&cksum, d, nb_samples);
  91. break;
  92. case AV_SAMPLE_FMT_S16:
  93. case AV_SAMPLE_FMT_S16P:
  94. cksum_line_s16(&cksum, d, nb_samples);
  95. break;
  96. case AV_SAMPLE_FMT_S32:
  97. case AV_SAMPLE_FMT_S32P:
  98. cksum_line_s32(&cksum, d, nb_samples);
  99. break;
  100. case AV_SAMPLE_FMT_FLT:
  101. case AV_SAMPLE_FMT_FLTP:
  102. cksum_line_flt(&cksum, d, nb_samples);
  103. break;
  104. case AV_SAMPLE_FMT_DBL:
  105. case AV_SAMPLE_FMT_DBLP:
  106. cksum_line_dbl(&cksum, d, nb_samples);
  107. break;
  108. default:
  109. av_assert0(!"reached");
  110. }
  111. av_bprintf(bp, ", 0x%08"PRIx32, cksum);
  112. }
  113. }
  114. static int write_header(struct AVFormatContext *s)
  115. {
  116. return ff_framehash_write_header(s);
  117. }
  118. static int write_frame(struct AVFormatContext *s, int stream_index,
  119. AVFrame **frame, unsigned flags)
  120. {
  121. AVBPrint bp;
  122. int ret = 0;
  123. enum AVMediaType type;
  124. const char *type_name;
  125. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  126. return 0;
  127. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
  128. av_bprintf(&bp, "%d, %10"PRId64"",
  129. stream_index, (*frame)->pts);
  130. type = s->streams[stream_index]->codecpar->codec_type;
  131. type_name = av_get_media_type_string(type);
  132. av_bprintf(&bp, ", %s", type_name ? type_name : "unknown");
  133. switch (type) {
  134. case AVMEDIA_TYPE_VIDEO:
  135. video_frame_cksum(&bp, *frame);
  136. break;
  137. case AVMEDIA_TYPE_AUDIO:
  138. audio_frame_cksum(&bp, *frame);
  139. break;
  140. }
  141. av_bprint_chars(&bp, '\n', 1);
  142. if (av_bprint_is_complete(&bp))
  143. avio_write(s->pb, bp.str, bp.len);
  144. else
  145. ret = AVERROR(ENOMEM);
  146. av_bprint_finalize(&bp, NULL);
  147. return ret;
  148. }
  149. static int write_packet(struct AVFormatContext *s, AVPacket *pkt)
  150. {
  151. return AVERROR(ENOSYS);
  152. }
  153. AVOutputFormat ff_uncodedframecrc_muxer = {
  154. .name = "uncodedframecrc",
  155. .long_name = NULL_IF_CONFIG_SMALL("uncoded framecrc testing"),
  156. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  157. .video_codec = AV_CODEC_ID_RAWVIDEO,
  158. .write_header = write_header,
  159. .write_packet = write_packet,
  160. .write_uncoded_frame = write_frame,
  161. .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
  162. AVFMT_TS_NEGATIVE,
  163. };