FFmpeg
ffhash.c

This example is a simple command line application that takes one or more arguments. It demonstrates a typical use of the hashing API with allocation, initialization, updating, and finalizing.

/*
* Copyright (c) 2002 Fabrice Bellard
* Copyright (c) 2013 Michael Niedermayer
* Copyright (c) 2013 James Almer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "libavutil/hash.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#if HAVE_IO_H
#include <io.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#define SIZE 65536
static struct AVHashContext *hash;
static int out_b64;
static void usage(void)
{
int i = 0;
const char *name;
printf("usage: ffhash [b64:]algorithm [input]...\n");
printf("Supported hash algorithms:");
do {
if (name)
printf(" %s", name);
i++;
} while(name);
printf("\n");
}
static void finish(void)
{
char res[2 * AV_HASH_MAX_SIZE + 4];
if (out_b64) {
av_hash_final_b64(hash, res, sizeof(res));
printf("b64:%s", res);
} else {
av_hash_final_hex(hash, res, sizeof(res));
printf("0x%s", res);
}
}
static int check(char *file)
{
uint8_t buffer[SIZE];
int fd, flags = O_RDONLY;
int ret = 0;
#ifdef O_BINARY
#endif
if (file) fd = open(file, flags);
else fd = 0;
if (fd == -1) {
printf("%s=OPEN-FAILED: %s:", av_hash_get_name(hash), strerror(errno));
ret = 1;
goto end;
}
for (;;) {
int size = read(fd, buffer, SIZE);
if (size < 0) {
int err = errno;
close(fd);
finish();
printf("+READ-FAILED: %s", strerror(err));
ret = 2;
goto end;
} else if(!size)
break;
}
close(fd);
finish();
end:
if (file)
printf(" *%s", file);
printf("\n");
return ret;
}
int main(int argc, char **argv)
{
int i;
int ret = 0;
const char *hash_name;
if (argc == 1) {
usage();
return 0;
}
hash_name = argv[1];
out_b64 = av_strstart(hash_name, "b64:", &hash_name);
if ((ret = av_hash_alloc(&hash, hash_name)) < 0) {
switch(ret) {
case AVERROR(EINVAL):
printf("Invalid hash type: %s\n", hash_name);
break;
case AVERROR(ENOMEM):
printf("%s\n", strerror(errno));
break;
}
return 1;
}
for (i = 2; i < argc; i++)
ret |= check(argv[i]);
if (argc < 3)
ret |= check(NULL);
return ret;
}
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_HASH_MAX_SIZE
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition: hash.h:156
av_hash_final_b64
void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the Base64 representation of the actual hash value as a string.
Definition: hash.c:235
finish
static void finish(void)
Definition: ffhash.c:62
hash
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
av_hash_get_name
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:104
SIZE
#define SIZE
Definition: ffhash.c:41
av_hash_alloc
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition: hash.c:114
NULL
#define NULL
Definition: coverity.c:32
av_hash_names
const char * av_hash_names(int i)
Get the names of available hash algorithms.
Definition: hash.c:98
av_hash_init
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition: hash.c:151
av_hash_update
void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
Update a hash context with additional data.
Definition: hash.c:172
av_hash_freep
void av_hash_freep(AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
Definition: hash.c:248
error.h
usage
const char * usage
Definition: floatimg_cmp.c:60
size
int size
Definition: twinvq_data.h:10344
printf
printf("static const uint8_t my_array[100] = {\n")
AVHashContext
Definition: hash.c:66
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:36
check
static int check(char *file)
Definition: ffhash.c:76
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
main
int main(int argc, char **argv)
Definition: ffhash.c:118
O_BINARY
#define O_BINARY
ret
ret
Definition: filter_design.txt:187
hash.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
av_hash_final_hex
void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the hexadecimal representation of the actual hash value as a string...
Definition: hash.c:225
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
avstring.h
out_b64
static int out_b64
Definition: ffhash.c:44
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231