2 * Copyright (c) 2001 Fabrice Bellard
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // ./fate 2 ./crew_cif out.y4m
40 #include <sys/types.h>
44 #include "libavcodec/avcodec.h"
45 #include "libavutil/pixdesc.h"
46 #include "libavutil/hash.h"
47 #include "libavutil/bswap.h"
49 static int header = 0;
51 static int decode(AVCodecContext *dec_ctx, AVFrame *frame,
54 static uint64_t frame_cnt = 0;
57 ret = avcodec_send_packet(dec_ctx, pkt);
59 fprintf(stderr, "Error sending a packet for decoding: %s\n", av_err2str(ret));
64 const AVPixFmtDescriptor *desc;
65 char sum[AV_HASH_MAX_SIZE * 2 + 1];
66 struct AVHashContext *hash;
68 ret = avcodec_receive_frame(dec_ctx, frame);
69 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
72 fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
78 "#format: frame checksums\n"
82 "#media_type 0: video\n"
83 "#codec_id 0: rawvideo\n"
84 "#dimensions 0: 352x288\n"
86 "#stream#, dts, pts, duration, size, hash\n");
89 desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
90 if ((ret = av_hash_alloc(&hash, "md5")) < 0) {
95 for (int i = 0; i < frame->height; i++)
96 av_hash_update(hash, &frame->data[0][i * frame->linesize[0]], frame->width);
97 for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
98 av_hash_update(hash, &frame->data[1][i * frame->linesize[1]], frame->width >> desc->log2_chroma_w);
99 for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
100 av_hash_update(hash, &frame->data[2][i * frame->linesize[2]], frame->width >> desc->log2_chroma_w);
102 av_hash_final_hex(hash, sum, av_hash_get_size(hash) * 2 + 1);
103 printf("0, %10"PRId64", %10"PRId64", 1, %8d, %s\n",
104 frame_cnt, frame_cnt,
105 (frame->width * frame->height + 2 * (frame->height >> desc->log2_chroma_h) * (frame->width >> desc->log2_chroma_w)), sum);
107 av_hash_freep(&hash);
112 int main(int argc, char **argv)
114 const AVCodec *codec = NULL;
115 AVCodecContext *c = NULL;
116 AVFrame *frame = NULL;
117 unsigned int threads;
120 char nal[MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE];
121 int nals = 0, ret = 0;
125 fprintf(stderr, "Usage: %s <threads> <input file> <output file>\n", argv[0]);
129 if (!(threads = strtoul(argv[1], NULL, 0)))
131 else if (threads > MAX_SLICES)
132 threads = MAX_SLICES;
135 setmode(fileno(stdout), O_BINARY);
138 if (!(pkt = av_packet_alloc())) {
142 if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
143 fprintf(stderr, "Codec not found\n");
148 if (!(c = avcodec_alloc_context3(codec))) {
149 fprintf(stderr, "Could not allocate video codec context\n");
157 c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
158 c->thread_type = FF_THREAD_SLICE;
159 c->thread_count = threads;
161 if ((ret = avcodec_open2(c, codec, NULL)) < 0) {
162 fprintf(stderr, "Could not open codec\n");
167 if (c->active_thread_type != FF_THREAD_SLICE) {
168 fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
173 fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
176 if (!(frame = av_frame_alloc())) {
177 fprintf(stderr, "Could not allocate video frame\n");
182 if (!(file = fopen(argv[2], "rb"))) {
183 fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
190 size_t ret = fread(&size, 1, sizeof(uint16_t), file);
191 if (ret != sizeof(uint16_t))
194 size = av_be2ne16(size);
195 ret = fread(p, 1, size, file);
197 perror("Couldn't read data");
202 if (++nals >= threads) {
206 if ((decret = decode(c, frame, pkt)) < 0) {
209 memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
218 if ((ret = decode(c, frame, pkt)) < 0) {
223 ret = decode(c, frame, NULL);
228 av_frame_free(&frame);
229 avcodec_free_context(&c);
230 av_packet_free(&pkt);