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
37 #include <sys/types.h>
41 #include "libavcodec/avcodec.h"
42 #include "libavutil/pixdesc.h"
43 #include "libavutil/hash.h"
44 #include "libavutil/bswap.h"
46 static int header = 0;
48 static int decode(AVCodecContext *dec_ctx, AVFrame *frame,
51 static uint64_t frame_cnt = 0;
54 ret = avcodec_send_packet(dec_ctx, pkt);
56 fprintf(stderr, "Error sending a packet for decoding: %s\n", av_err2str(ret));
61 const AVPixFmtDescriptor *desc;
62 char sum[AV_HASH_MAX_SIZE * 2 + 1];
63 struct AVHashContext *hash;
65 ret = avcodec_receive_frame(dec_ctx, frame);
66 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
69 fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
75 "#format: frame checksums\n"
79 "#media_type 0: video\n"
80 "#codec_id 0: rawvideo\n"
81 "#dimensions 0: 352x288\n"
83 "#stream#, dts, pts, duration, size, hash\n");
86 desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
87 if ((ret = av_hash_alloc(&hash, "md5")) < 0) {
92 for (int i = 0; i < frame->height; i++)
93 av_hash_update(hash, &frame->data[0][i * frame->linesize[0]], frame->width);
94 for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
95 av_hash_update(hash, &frame->data[1][i * frame->linesize[1]], frame->width >> desc->log2_chroma_w);
96 for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
97 av_hash_update(hash, &frame->data[2][i * frame->linesize[2]], frame->width >> desc->log2_chroma_w);
99 av_hash_final_hex(hash, sum, av_hash_get_size(hash) * 2 + 1);
100 printf("0, %10"PRId64", %10"PRId64", 1, %8d, %s\n",
101 frame_cnt, frame_cnt,
102 (frame->width * frame->height + 2 * (frame->height >> desc->log2_chroma_h) * (frame->width >> desc->log2_chroma_w)), sum);
104 av_hash_freep(&hash);
109 int main(int argc, char **argv)
111 const AVCodec *codec = NULL;
112 AVCodecContext *c = NULL;
113 AVFrame *frame = NULL;
114 unsigned int threads;
118 int nals = 0, ret = 0;
122 fprintf(stderr, "Usage: %s <threads> <input file>\n", argv[0]);
126 if (!(threads = strtoul(argv[1], NULL, 0)))
128 else if (threads > MAX_SLICES)
129 threads = MAX_SLICES;
132 setmode(fileno(stdout), O_BINARY);
135 if (!(pkt = av_packet_alloc())) {
139 nal = av_malloc(MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
144 if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
145 fprintf(stderr, "Codec not found\n");
150 if (!(c = avcodec_alloc_context3(codec))) {
151 fprintf(stderr, "Could not allocate video codec context\n");
159 c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
160 c->thread_type = FF_THREAD_SLICE;
161 c->thread_count = threads;
163 if ((ret = avcodec_open2(c, codec, NULL)) < 0) {
164 fprintf(stderr, "Could not open codec\n");
169 if (c->active_thread_type != FF_THREAD_SLICE) {
170 fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
175 fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
178 if (!(frame = av_frame_alloc())) {
179 fprintf(stderr, "Could not allocate video frame\n");
184 if (!(file = fopen(argv[2], "rb"))) {
185 fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
192 size_t ret = fread(&size, 1, sizeof(uint16_t), file);
193 if (ret != sizeof(uint16_t))
196 size = av_be2ne16(size);
197 ret = fread(p, 1, size, file);
199 perror("Couldn't read data");
204 if (++nals >= threads) {
208 if ((decret = decode(c, frame, pkt)) < 0) {
211 memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
220 if ((ret = decode(c, frame, pkt)) < 0) {
225 ret = decode(c, frame, NULL);
232 av_frame_free(&frame);
233 avcodec_free_context(&c);
234 av_packet_free(&pkt);