1 /*
2 * frame CRC encoder (for codec/format testing)
3 * Copyright (c) 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <inttypes.h>
23
24 #include "config.h"
25 #include "libavutil/adler32.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavcodec/avcodec.h"
29 #include "avformat.h"
30 #include "internal.h"
31
framecrc_write_header(struct AVFormatContext * s)32 static int framecrc_write_header(struct AVFormatContext *s)
33 {
34 int i;
35 for (i = 0; i < s->nb_streams; i++) {
36 AVStream *st = s->streams[i];
37 AVCodecParameters *par = st->codecpar;
38 if (par->extradata) {
39 uint32_t crc = av_adler32_update(0, par->extradata, par->extradata_size);
40 avio_printf(s->pb, "#extradata %d: %8d, 0x%08"PRIx32"\n",
41 i, par->extradata_size, crc);
42 }
43 }
44
45 return ff_framehash_write_header(s);
46 }
47
bswap(char * buf,int offset,int size)48 static av_unused void inline bswap(char *buf, int offset, int size)
49 {
50 if (size == 8) {
51 uint64_t val = AV_RN64(buf + offset);
52 AV_WN64(buf + offset, av_bswap64(val));
53 } else if (size == 4) {
54 uint32_t val = AV_RN32(buf + offset);
55 AV_WN32(buf + offset, av_bswap32(val));
56 }
57 }
58
framecrc_write_packet(struct AVFormatContext * s,AVPacket * pkt)59 static int framecrc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
60 {
61 uint32_t crc = av_adler32_update(0, pkt->data, pkt->size);
62 char buf[256];
63
64 snprintf(buf, sizeof(buf), "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, 0x%08"PRIx32,
65 pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size, crc);
66 if (pkt->flags != AV_PKT_FLAG_KEY)
67 av_strlcatf(buf, sizeof(buf), ", F=0x%0X", pkt->flags);
68 if (pkt->side_data_elems) {
69 int i;
70 av_strlcatf(buf, sizeof(buf), ", S=%d", pkt->side_data_elems);
71
72 for (i=0; i<pkt->side_data_elems; i++) {
73 const AVPacketSideData *const sd = &pkt->side_data[i];
74 const uint8_t *data = sd->data;
75 uint32_t side_data_crc = 0;
76
77 switch (sd->type) {
78 #if HAVE_BIGENDIAN
79 uint8_t bswap_buf[FFMAX(sizeof(AVCPBProperties),
80 sizeof(AVProducerReferenceTime))];
81 case AV_PKT_DATA_PALETTE:
82 case AV_PKT_DATA_REPLAYGAIN:
83 case AV_PKT_DATA_DISPLAYMATRIX:
84 case AV_PKT_DATA_STEREO3D:
85 case AV_PKT_DATA_AUDIO_SERVICE_TYPE:
86 case AV_PKT_DATA_FALLBACK_TRACK:
87 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA:
88 case AV_PKT_DATA_SPHERICAL:
89 case AV_PKT_DATA_CONTENT_LIGHT_LEVEL:
90 case AV_PKT_DATA_S12M_TIMECODE:
91 for (int j = 0; j < sd->size / 4; j++) {
92 uint8_t buf[4];
93 AV_WL32(buf, AV_RB32(sd->data + 4 * j));
94 side_data_crc = av_adler32_update(side_data_crc, buf, 4);
95 }
96 break;
97 case AV_PKT_DATA_CPB_PROPERTIES:
98 #define BSWAP(struct, field) bswap(bswap_buf, offsetof(struct, field), sizeof(((struct){0}).field))
99 if (sd->size == sizeof(AVCPBProperties)) {
100 memcpy(bswap_buf, sd->data, sizeof(AVCPBProperties));
101 data = bswap_buf;
102 BSWAP(AVCPBProperties, max_bitrate);
103 BSWAP(AVCPBProperties, min_bitrate);
104 BSWAP(AVCPBProperties, avg_bitrate);
105 BSWAP(AVCPBProperties, buffer_size);
106 BSWAP(AVCPBProperties, vbv_delay);
107 }
108 goto pod;
109 case AV_PKT_DATA_PRFT:
110 if (sd->size == sizeof(AVProducerReferenceTime)) {
111 memcpy(bswap_buf, sd->data, sizeof(AVProducerReferenceTime));
112 data = bswap_buf;
113 BSWAP(AVProducerReferenceTime, wallclock);
114 BSWAP(AVProducerReferenceTime, flags);
115 }
116 goto pod;
117 pod:
118 #endif
119 default:
120 side_data_crc = av_adler32_update(0, data, sd->size);
121 }
122 av_strlcatf(buf, sizeof(buf), ", %8d, 0x%08"PRIx32, pkt->side_data[i].size, side_data_crc);
123 }
124 }
125 av_strlcatf(buf, sizeof(buf), "\n");
126 avio_write(s->pb, buf, strlen(buf));
127 return 0;
128 }
129
130 AVOutputFormat ff_framecrc_muxer = {
131 .name = "framecrc",
132 .long_name = NULL_IF_CONFIG_SMALL("framecrc testing"),
133 .audio_codec = AV_CODEC_ID_PCM_S16LE,
134 .video_codec = AV_CODEC_ID_RAWVIDEO,
135 .write_header = framecrc_write_header,
136 .write_packet = framecrc_write_packet,
137 .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT |
138 AVFMT_TS_NEGATIVE,
139 };
140