]> git.sesse.net Git - ffmpeg/blob - tests/api/api-h264-slice-test.c
fate: add api-h264-slice test
[ffmpeg] / tests / api / api-h264-slice-test.c
1 /*
2  * Copyright (c) 2001 Fabrice Bellard
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * THE SOFTWARE.
21  */
22
23 #define MAX_SLICES 8
24
25 // ./fate 2 ./crew_cif out.y4m
26
27 #include "config.h"
28
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #if HAVE_IO_H
38 #include <io.h>
39 #endif
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43
44 #include "libavformat/network.h"
45 #include "libavcodec/avcodec.h"
46 #include "libavutil/pixdesc.h"
47 #include "libavutil/hash.h"
48
49 static int header = 0;
50
51 static void decode(AVCodecContext *dec_ctx, AVFrame *frame,
52            AVPacket *pkt)
53 {
54     static uint64_t frame_cnt = 0;
55     int ret;
56
57     ret = avcodec_send_packet(dec_ctx, pkt);
58     if (ret < 0) {
59         fprintf(stderr, "Error sending a packet for decoding: %s\n", av_err2str(ret));
60         exit(1);
61     }
62
63     while (ret >= 0) {
64         const AVPixFmtDescriptor *desc;
65         char *sum;
66         struct AVHashContext *hash;
67
68         ret = avcodec_receive_frame(dec_ctx, frame);
69         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
70             return;
71         } else if (ret < 0) {
72             fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
73             exit(1);
74         }
75
76         if (!header) {
77             printf(
78             "#format: frame checksums\n"
79             "#version: 2\n"
80             "#hash: MD5\n"
81             "#tb 0: 1/30\n"
82             "#media_type 0: video\n"
83             "#codec_id 0: rawvideo\n"
84             "#dimensions 0: 352x288\n"
85             "#sar 0: 128/117\n"
86             "#stream#, dts,        pts, duration,     size, hash\n");
87             header = 1;
88         }
89         desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
90         av_hash_alloc(&hash, "md5");
91         av_hash_init(hash);
92         sum = av_mallocz(av_hash_get_size(hash) * 2 + 1);
93
94         for (int i = 0; i < frame->height; i++)
95             av_hash_update(hash, &frame->data[0][i * frame->linesize[0]], frame->width);
96         for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
97             av_hash_update(hash, &frame->data[1][i * frame->linesize[1]], frame->width >> desc->log2_chroma_w);
98         for (int i = 0; i < frame->height >> desc->log2_chroma_h; i++)
99             av_hash_update(hash, &frame->data[2][i * frame->linesize[2]], frame->width >> desc->log2_chroma_w);
100
101         av_hash_final_hex(hash, sum, av_hash_get_size(hash) * 2 + 1);
102         printf("0, %10"PRId64", %10"PRId64",        1, %8d, %s\n",
103             frame_cnt, frame_cnt,
104             (frame->width * frame->height + 2 * (frame->height >> desc->log2_chroma_h) * (frame->width >> desc->log2_chroma_w)), sum);
105         frame_cnt += 1;
106         av_free(hash);
107         av_free(sum);
108     }
109 }
110
111 int main(int argc, char **argv)
112 {
113     const AVCodec *codec;
114     AVCodecContext *c = NULL;
115     AVFrame *frame;
116     unsigned int threads;
117     AVPacket *pkt;
118     FILE *fd;
119     char nal[MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE];
120     int nals = 0;
121     char *p = nal;
122
123     if (argc < 4) {
124         fprintf(stderr, "Usage: %s <threads> <input file> <output file>\n", argv[0]);
125         exit(1);
126     }
127
128     if (!(threads = strtoul(argv[1], NULL, 0)))
129         threads = 1;
130     else if (threads > MAX_SLICES)
131         threads = MAX_SLICES;
132
133 #ifdef _WIN32
134     setmode(fileno(stdout), O_BINARY);
135 #endif
136
137     if (!(pkt = av_packet_alloc()))
138         exit(1);
139
140     if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
141         fprintf(stderr, "Codec not found\n");
142         exit(1);
143     }
144
145     if (!(c = avcodec_alloc_context3(codec))) {
146         fprintf(stderr, "Could not allocate video codec context\n");
147         exit(1);
148     }
149
150     c->width  = 352;
151     c->height = 288;
152
153     c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
154     c->thread_type = FF_THREAD_SLICE;
155     c->thread_count = threads;
156
157     if (avcodec_open2(c, codec, NULL) < 0) {
158         fprintf(stderr, "Could not open codec\n");
159         exit(1);
160     }
161
162 #if HAVE_THREADS
163     if (c->active_thread_type != FF_THREAD_SLICE) {
164         fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
165         exit(1);
166     }
167 #else
168     fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
169 #endif
170
171     if (!(frame = av_frame_alloc())) {
172         fprintf(stderr, "Could not allocate video frame\n");
173         exit(1);
174     }
175
176     if (!(fd = fopen(argv[2], "rb"))) {
177         fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
178         exit(1);
179     }
180
181     while(1) {
182         uint16_t size = 0;
183         ssize_t ret = fread(&size, 1, sizeof(uint16_t), fd);
184         if (ret < 0) {
185             perror("Couldn't read size");
186             exit(1);
187         } else if (ret != sizeof(uint16_t))
188             break;
189         size = ntohs(size);
190         ret = fread(p, 1, size, fd);
191         if (ret < 0 || ret != size) {
192             perror("Couldn't read data");
193             exit(1);
194         }
195         p += ret;
196
197         if (++nals >= threads) {
198             pkt->data = nal;
199             pkt->size = p - nal;
200             decode(c, frame, pkt);
201             memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
202             nals = 0;
203             p = nal;
204         }
205     }
206
207     if (nals) {
208         pkt->data = nal;
209         pkt->size = p - nal;
210         decode(c, frame, pkt);
211     }
212
213     decode(c, frame, NULL);
214
215     fclose(fd);
216     avcodec_free_context(&c);
217     av_frame_free(&frame);
218     av_packet_free(&pkt);
219
220     return 0;
221 }