2018-10-09 12:07:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_SLICES 8
|
|
|
|
|
|
|
|
// ./fate 2 ./crew_cif out.y4m
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "libavformat/network.h"
|
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "libavutil/hash.h"
|
|
|
|
|
|
|
|
static int header = 0;
|
|
|
|
|
2018-10-30 14:38:42 +00:00
|
|
|
static int decode(AVCodecContext *dec_ctx, AVFrame *frame,
|
2018-10-09 12:07:15 +00:00
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
static uint64_t frame_cnt = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = avcodec_send_packet(dec_ctx, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error sending a packet for decoding: %s\n", av_err2str(ret));
|
2018-10-30 14:38:42 +00:00
|
|
|
return ret;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (ret >= 0) {
|
|
|
|
const AVPixFmtDescriptor *desc;
|
2018-10-30 14:38:42 +00:00
|
|
|
char sum[AV_HASH_MAX_SIZE * 2 + 1];
|
2018-10-09 12:07:15 +00:00
|
|
|
struct AVHashContext *hash;
|
|
|
|
|
|
|
|
ret = avcodec_receive_frame(dec_ctx, frame);
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
|
2018-10-30 14:38:42 +00:00
|
|
|
return 0;
|
2018-10-09 12:07:15 +00:00
|
|
|
} else if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
|
2018-10-30 14:38:42 +00:00
|
|
|
return ret;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!header) {
|
|
|
|
printf(
|
|
|
|
"#format: frame checksums\n"
|
|
|
|
"#version: 2\n"
|
|
|
|
"#hash: MD5\n"
|
|
|
|
"#tb 0: 1/30\n"
|
|
|
|
"#media_type 0: video\n"
|
|
|
|
"#codec_id 0: rawvideo\n"
|
|
|
|
"#dimensions 0: 352x288\n"
|
|
|
|
"#sar 0: 128/117\n"
|
|
|
|
"#stream#, dts, pts, duration, size, hash\n");
|
|
|
|
header = 1;
|
|
|
|
}
|
|
|
|
desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
|
2018-10-30 14:38:42 +00:00
|
|
|
if ((ret = av_hash_alloc(&hash, "md5")) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-10-09 12:07:15 +00:00
|
|
|
av_hash_init(hash);
|
|
|
|
|
|
|
|
for (int i = 0; i < frame->height; i++)
|
|
|
|
av_hash_update(hash, &frame->data[0][i * frame->linesize[0]], frame->width);
|
|
|
|
for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
|
|
|
|
av_hash_update(hash, &frame->data[1][i * frame->linesize[1]], frame->width >> desc->log2_chroma_w);
|
|
|
|
for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
|
|
|
|
av_hash_update(hash, &frame->data[2][i * frame->linesize[2]], frame->width >> desc->log2_chroma_w);
|
|
|
|
|
|
|
|
av_hash_final_hex(hash, sum, av_hash_get_size(hash) * 2 + 1);
|
|
|
|
printf("0, %10"PRId64", %10"PRId64", 1, %8d, %s\n",
|
|
|
|
frame_cnt, frame_cnt,
|
|
|
|
(frame->width * frame->height + 2 * (frame->height >> desc->log2_chroma_h) * (frame->width >> desc->log2_chroma_w)), sum);
|
|
|
|
frame_cnt += 1;
|
2018-10-27 03:12:30 +00:00
|
|
|
av_hash_freep(&hash);
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
2018-10-30 14:38:42 +00:00
|
|
|
return 0;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-10-30 14:38:42 +00:00
|
|
|
const AVCodec *codec = NULL;
|
2018-10-09 12:07:15 +00:00
|
|
|
AVCodecContext *c = NULL;
|
2018-10-30 14:38:42 +00:00
|
|
|
AVFrame *frame = NULL;
|
2018-10-09 12:07:15 +00:00
|
|
|
unsigned int threads;
|
|
|
|
AVPacket *pkt;
|
2018-10-30 14:38:42 +00:00
|
|
|
FILE *file = NULL;
|
2018-10-09 12:07:15 +00:00
|
|
|
char nal[MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE];
|
2018-10-30 14:38:42 +00:00
|
|
|
int nals = 0, ret = 0;
|
2018-10-09 12:07:15 +00:00
|
|
|
char *p = nal;
|
|
|
|
|
|
|
|
if (argc < 4) {
|
|
|
|
fprintf(stderr, "Usage: %s <threads> <input file> <output file>\n", argv[0]);
|
2018-10-30 14:38:42 +00:00
|
|
|
return -1;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(threads = strtoul(argv[1], NULL, 0)))
|
|
|
|
threads = 1;
|
|
|
|
else if (threads > MAX_SLICES)
|
|
|
|
threads = MAX_SLICES;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
setmode(fileno(stdout), O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
2018-10-30 14:38:42 +00:00
|
|
|
if (!(pkt = av_packet_alloc())) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-10-09 12:07:15 +00:00
|
|
|
|
|
|
|
if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
|
|
|
|
fprintf(stderr, "Codec not found\n");
|
2018-10-30 14:38:42 +00:00
|
|
|
ret = -1;
|
|
|
|
goto err;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(c = avcodec_alloc_context3(codec))) {
|
|
|
|
fprintf(stderr, "Could not allocate video codec context\n");
|
2018-10-30 14:38:42 +00:00
|
|
|
ret = -1;
|
|
|
|
goto err;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c->width = 352;
|
|
|
|
c->height = 288;
|
|
|
|
|
|
|
|
c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
|
|
|
|
c->thread_type = FF_THREAD_SLICE;
|
|
|
|
c->thread_count = threads;
|
|
|
|
|
2018-10-30 14:38:42 +00:00
|
|
|
if ((ret = avcodec_open2(c, codec, NULL)) < 0) {
|
2018-10-09 12:07:15 +00:00
|
|
|
fprintf(stderr, "Could not open codec\n");
|
2018-10-30 14:38:42 +00:00
|
|
|
goto err;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_THREADS
|
|
|
|
if (c->active_thread_type != FF_THREAD_SLICE) {
|
|
|
|
fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
|
2018-10-30 14:38:42 +00:00
|
|
|
ret = -1;
|
|
|
|
goto err;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!(frame = av_frame_alloc())) {
|
|
|
|
fprintf(stderr, "Could not allocate video frame\n");
|
2018-10-30 14:38:42 +00:00
|
|
|
ret = -1;
|
|
|
|
goto err;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:38:42 +00:00
|
|
|
if (!(file = fopen(argv[2], "rb"))) {
|
2018-10-09 12:07:15 +00:00
|
|
|
fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
|
2018-10-30 14:38:42 +00:00
|
|
|
ret = -1;
|
|
|
|
goto err;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
uint16_t size = 0;
|
2018-10-30 14:38:42 +00:00
|
|
|
size_t ret = fread(&size, 1, sizeof(uint16_t), file);
|
2018-10-30 14:38:41 +00:00
|
|
|
if (ret != sizeof(uint16_t))
|
2018-10-09 12:07:15 +00:00
|
|
|
break;
|
2018-10-30 14:38:42 +00:00
|
|
|
|
2018-10-09 12:07:15 +00:00
|
|
|
size = ntohs(size);
|
2018-10-30 14:38:42 +00:00
|
|
|
ret = fread(p, 1, size, file);
|
2018-10-30 14:38:41 +00:00
|
|
|
if (ret != size) {
|
2018-10-09 12:07:15 +00:00
|
|
|
perror("Couldn't read data");
|
2018-10-30 14:38:42 +00:00
|
|
|
goto err;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
p += ret;
|
|
|
|
|
|
|
|
if (++nals >= threads) {
|
2018-10-30 14:38:42 +00:00
|
|
|
int decret = 0;
|
2018-10-09 12:07:15 +00:00
|
|
|
pkt->data = nal;
|
|
|
|
pkt->size = p - nal;
|
2018-10-30 14:38:42 +00:00
|
|
|
if ((decret = decode(c, frame, pkt)) < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
2018-10-09 12:07:15 +00:00
|
|
|
memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
nals = 0;
|
|
|
|
p = nal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nals) {
|
|
|
|
pkt->data = nal;
|
|
|
|
pkt->size = p - nal;
|
2018-10-30 14:38:42 +00:00
|
|
|
if ((ret = decode(c, frame, pkt)) < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:38:42 +00:00
|
|
|
ret = decode(c, frame, NULL);
|
2018-10-09 12:07:15 +00:00
|
|
|
|
2018-10-30 14:38:42 +00:00
|
|
|
err:
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
2018-10-09 12:07:15 +00:00
|
|
|
av_frame_free(&frame);
|
2018-10-30 14:38:42 +00:00
|
|
|
avcodec_free_context(&c);
|
2018-10-09 12:07:15 +00:00
|
|
|
av_packet_free(&pkt);
|
|
|
|
|
2018-10-30 14:38:42 +00:00
|
|
|
return ret;
|
2018-10-09 12:07:15 +00:00
|
|
|
}
|