| Line | Branch | Exec | Source | 
|---|---|---|---|
| 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 | |||
| 21 | #include "libavutil/adler32.h" | ||
| 22 | #include "libavutil/avassert.h" | ||
| 23 | #include "libavutil/bprint.h" | ||
| 24 | #include "libavutil/frame.h" | ||
| 25 | #include "libavutil/imgutils.h" | ||
| 26 | #include "libavutil/pixdesc.h" | ||
| 27 | #include "libavformat/mux.h" | ||
| 28 | #include "avformat.h" | ||
| 29 | #include "internal.h" | ||
| 30 | |||
| 31 | /* Identical to Adler32 when the type is uint8_t. */ | ||
| 32 | #define DEFINE_CKSUM_LINE(name, type, conv) \ | ||
| 33 | static void cksum_line_ ## name(unsigned *cksum, void *data, unsigned size) \ | ||
| 34 | { \ | ||
| 35 | type *p = data; \ | ||
| 36 | unsigned a = *cksum & 0xFFFF, b = *cksum >> 16; \ | ||
| 37 | for (; size > 0; size--, p++) { \ | ||
| 38 | a = (a + (unsigned)(conv)) % 65521; \ | ||
| 39 | b = (b + a) % 65521; \ | ||
| 40 | } \ | ||
| 41 | *cksum = a | (b << 16); \ | ||
| 42 | } | ||
| 43 | |||
| 44 | ✗ | DEFINE_CKSUM_LINE(u8, uint8_t, *p) | |
| 45 | ✗ | DEFINE_CKSUM_LINE(s16, int16_t, *p + 0x8000) | |
| 46 | ✗ | DEFINE_CKSUM_LINE(s32, int32_t, *p + 0x80000000) | |
| 47 | ✗ | DEFINE_CKSUM_LINE(flt, float, *p * 0x80000000 + 0x80000000) | |
| 48 | ✗ | DEFINE_CKSUM_LINE(dbl, double, *p * 0x80000000 + 0x80000000) | |
| 49 | |||
| 50 | ✗ | static void video_frame_cksum(AVBPrint *bp, AVFrame *frame) | |
| 51 | { | ||
| 52 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); | |
| 53 | int i, y; | ||
| 54 | uint8_t *data; | ||
| 55 | ✗ | int linesize[5] = { 0 }; | |
| 56 | |||
| 57 | ✗ | av_bprintf(bp, ", %d x %d", frame->width, frame->height); | |
| 58 | ✗ | if (!desc) { | |
| 59 | ✗ | av_bprintf(bp, ", unknown"); | |
| 60 | ✗ | return; | |
| 61 | } | ||
| 62 | ✗ | if (av_image_fill_linesizes(linesize, frame->format, frame->width) < 0) | |
| 63 | ✗ | return; | |
| 64 | ✗ | av_bprintf(bp, ", %s", desc->name); | |
| 65 | ✗ | for (i = 0; linesize[i]; i++) { | |
| 66 | ✗ | unsigned cksum = 0; | |
| 67 | ✗ | int h = frame->height; | |
| 68 | ✗ | if ((i == 1 || i == 2) && desc->nb_components >= 3) | |
| 69 | ✗ | h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h); | |
| 70 | ✗ | data = frame->data[i]; | |
| 71 | ✗ | for (y = 0; y < h; y++) { | |
| 72 | ✗ | cksum = av_adler32_update(cksum, data, linesize[i]); | |
| 73 | ✗ | data += frame->linesize[i]; | |
| 74 | } | ||
| 75 | ✗ | av_bprintf(bp, ", 0x%08x", cksum); | |
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | ✗ | static void audio_frame_cksum(AVBPrint *bp, AVFrame *frame) | |
| 80 | { | ||
| 81 | int nb_planes, nb_samples, p; | ||
| 82 | const char *name; | ||
| 83 | |||
| 84 | ✗ | nb_planes = frame->ch_layout.nb_channels; | |
| 85 | ✗ | nb_samples = frame->nb_samples; | |
| 86 | ✗ | if (!av_sample_fmt_is_planar(frame->format)) { | |
| 87 | ✗ | nb_samples *= nb_planes; | |
| 88 | ✗ | nb_planes = 1; | |
| 89 | } | ||
| 90 | ✗ | name = av_get_sample_fmt_name(frame->format); | |
| 91 | ✗ | av_bprintf(bp, ", %d samples", frame->nb_samples); | |
| 92 | ✗ | av_bprintf(bp, ", %s", name ? name : "unknown"); | |
| 93 | ✗ | for (p = 0; p < nb_planes; p++) { | |
| 94 | ✗ | uint32_t cksum = 0; | |
| 95 | ✗ | void *d = frame->extended_data[p]; | |
| 96 | ✗ | switch (frame->format) { | |
| 97 | ✗ | case AV_SAMPLE_FMT_U8: | |
| 98 | case AV_SAMPLE_FMT_U8P: | ||
| 99 | ✗ | cksum_line_u8(&cksum, d, nb_samples); | |
| 100 | ✗ | break; | |
| 101 | ✗ | case AV_SAMPLE_FMT_S16: | |
| 102 | case AV_SAMPLE_FMT_S16P: | ||
| 103 | ✗ | cksum_line_s16(&cksum, d, nb_samples); | |
| 104 | ✗ | break; | |
| 105 | ✗ | case AV_SAMPLE_FMT_S32: | |
| 106 | case AV_SAMPLE_FMT_S32P: | ||
| 107 | ✗ | cksum_line_s32(&cksum, d, nb_samples); | |
| 108 | ✗ | break; | |
| 109 | ✗ | case AV_SAMPLE_FMT_FLT: | |
| 110 | case AV_SAMPLE_FMT_FLTP: | ||
| 111 | ✗ | cksum_line_flt(&cksum, d, nb_samples); | |
| 112 | ✗ | break; | |
| 113 | ✗ | case AV_SAMPLE_FMT_DBL: | |
| 114 | case AV_SAMPLE_FMT_DBLP: | ||
| 115 | ✗ | cksum_line_dbl(&cksum, d, nb_samples); | |
| 116 | ✗ | break; | |
| 117 | ✗ | default: | |
| 118 | ✗ | av_assert0(!"reached"); | |
| 119 | } | ||
| 120 | ✗ | av_bprintf(bp, ", 0x%08"PRIx32, cksum); | |
| 121 | } | ||
| 122 | ✗ | } | |
| 123 | |||
| 124 | ✗ | static int write_header(struct AVFormatContext *s) | |
| 125 | { | ||
| 126 | ✗ | return ff_framehash_write_header(s); | |
| 127 | } | ||
| 128 | |||
| 129 | ✗ | static int write_frame(struct AVFormatContext *s, int stream_index, | |
| 130 | AVFrame **frame, unsigned flags) | ||
| 131 | { | ||
| 132 | AVBPrint bp; | ||
| 133 | ✗ | int ret = 0; | |
| 134 | enum AVMediaType type; | ||
| 135 | const char *type_name; | ||
| 136 | |||
| 137 | ✗ | if ((flags & AV_WRITE_UNCODED_FRAME_QUERY)) | |
| 138 | ✗ | return 0; | |
| 139 | |||
| 140 | ✗ | av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 141 | ✗ | av_bprintf(&bp, "%d, %10"PRId64"", | |
| 142 | ✗ | stream_index, (*frame)->pts); | |
| 143 | ✗ | type = s->streams[stream_index]->codecpar->codec_type; | |
| 144 | ✗ | type_name = av_get_media_type_string(type); | |
| 145 | ✗ | av_bprintf(&bp, ", %s", type_name ? type_name : "unknown"); | |
| 146 | ✗ | switch (type) { | |
| 147 | ✗ | case AVMEDIA_TYPE_VIDEO: | |
| 148 | ✗ | video_frame_cksum(&bp, *frame); | |
| 149 | ✗ | break; | |
| 150 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
| 151 | ✗ | audio_frame_cksum(&bp, *frame); | |
| 152 | ✗ | break; | |
| 153 | } | ||
| 154 | |||
| 155 | ✗ | av_bprint_chars(&bp, '\n', 1); | |
| 156 | ✗ | if (av_bprint_is_complete(&bp)) | |
| 157 | ✗ | avio_write(s->pb, bp.str, bp.len); | |
| 158 | else | ||
| 159 | ✗ | ret = AVERROR(ENOMEM); | |
| 160 | ✗ | av_bprint_finalize(&bp, NULL); | |
| 161 | ✗ | return ret; | |
| 162 | } | ||
| 163 | |||
| 164 | ✗ | static int write_packet(struct AVFormatContext *s, AVPacket *pkt) | |
| 165 | { | ||
| 166 | ✗ | return AVERROR(ENOSYS); | |
| 167 | } | ||
| 168 | |||
| 169 | const FFOutputFormat ff_uncodedframecrc_muxer = { | ||
| 170 | .p.name = "uncodedframecrc", | ||
| 171 | .p.long_name = NULL_IF_CONFIG_SMALL("uncoded framecrc testing"), | ||
| 172 | .p.audio_codec = AV_CODEC_ID_PCM_S16LE, | ||
| 173 | .p.video_codec = AV_CODEC_ID_RAWVIDEO, | ||
| 174 | .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT | | ||
| 175 | AVFMT_TS_NEGATIVE, | ||
| 176 | .write_header = write_header, | ||
| 177 | .write_packet = write_packet, | ||
| 178 | .write_uncoded_frame = write_frame, | ||
| 179 | }; | ||
| 180 |