aboutsummaryrefslogtreecommitdiffstats
path: root/tests/api/api-h264-slice-test.c
blob: dee93b8349e6d4221f8bf353027afb8b68bd4e18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * 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

#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 "libavcodec/avcodec.h"
#include "libavutil/pixdesc.h"
#include "libavutil/hash.h"
#include "libavutil/bswap.h"

static int header = 0;

static int decode(AVCodecContext *dec_ctx, AVFrame *frame,
           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));
        return ret;
    }

    while (ret >= 0) {
        const AVPixFmtDescriptor *desc;
        char sum[AV_HASH_MAX_SIZE * 2 + 1];
        struct AVHashContext *hash;

        ret = avcodec_receive_frame(dec_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            return 0;
        } else if (ret < 0) {
            fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
            return ret;
        }

        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);
        if ((ret = av_hash_alloc(&hash, "md5")) < 0) {
            return ret;
        }
        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;
        av_hash_freep(&hash);
    }
    return 0;
}

int main(int argc, char **argv)
{
    const AVCodec *codec = NULL;
    AVCodecContext *c = NULL;
    AVFrame *frame = NULL;
    unsigned int threads;
    AVPacket *pkt;
    FILE *file = NULL;
    char * nal = NULL;
    int nals = 0, ret = 0;
    char *p;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <threads> <input file>\n", argv[0]);
        return -1;
    }

    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

    if (!(pkt = av_packet_alloc())) {
        return -1;
    }

    nal = av_malloc(MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
    if (!nal)
        goto err;
    p = nal;

    if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
        fprintf(stderr, "Codec not found\n");
        ret = -1;
        goto err;
    }

    if (!(c = avcodec_alloc_context3(codec))) {
        fprintf(stderr, "Could not allocate video codec context\n");
        ret = -1;
        goto err;
    }

    c->width  = 352;
    c->height = 288;

    c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
    c->thread_type = FF_THREAD_SLICE;
    c->thread_count = threads;

    if ((ret = avcodec_open2(c, codec, NULL)) < 0) {
        fprintf(stderr, "Could not open codec\n");
        goto err;
    }

#if HAVE_THREADS
    if (c->active_thread_type != FF_THREAD_SLICE) {
        fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
        ret = -1;
        goto err;
    }
#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");
        ret = -1;
        goto err;
    }

    if (!(file = fopen(argv[2], "rb"))) {
        fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
        ret = -1;
        goto err;
    }

    while(1) {
        uint16_t size = 0;
        size_t ret = fread(&size, 1, sizeof(uint16_t), file);
        if (ret != sizeof(uint16_t))
            break;

        size = av_be2ne16(size);
        ret = fread(p, 1, size, file);
        if (ret != size) {
            perror("Couldn't read data");
            goto err;
        }
        p += ret;

        if (++nals >= threads) {
            int decret = 0;
            pkt->data = nal;
            pkt->size = p - nal;
            if ((decret = decode(c, frame, pkt)) < 0) {
                goto err;
            }
            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;
        if ((ret = decode(c, frame, pkt)) < 0) {
            goto err;
        }
    }

    ret = decode(c, frame, NULL);

err:
    if (nal)
        av_free(nal);
    if (file)
        fclose(file);
    av_frame_free(&frame);
    avcodec_free_context(&c);
    av_packet_free(&pkt);

    return ret;
}