FFmpeg
api-h264-slice-test.c
Go to the documentation of this file.
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 #include "config.h"
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #if HAVE_IO_H
35 #include <io.h>
36 #endif
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 
41 #include "libavcodec/avcodec.h"
42 #include "libavutil/pixdesc.h"
43 #include "libavutil/hash.h"
44 #include "libavutil/bswap.h"
45 
46 static int header = 0;
47 
49  AVPacket *pkt)
50 {
51  static uint64_t frame_cnt = 0;
52  int ret;
53 
55  if (ret < 0) {
56  fprintf(stderr, "Error sending a packet for decoding: %s\n", av_err2str(ret));
57  return ret;
58  }
59 
60  while (ret >= 0) {
61  const AVPixFmtDescriptor *desc;
62  char sum[AV_HASH_MAX_SIZE * 2 + 1];
63  struct AVHashContext *hash;
64 
66  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
67  return 0;
68  } else if (ret < 0) {
69  fprintf(stderr, "Error during decoding: %s\n", av_err2str(ret));
70  return ret;
71  }
72 
73  if (!header) {
74  printf(
75  "#format: frame checksums\n"
76  "#version: 2\n"
77  "#hash: MD5\n"
78  "#tb 0: 1/30\n"
79  "#media_type 0: video\n"
80  "#codec_id 0: rawvideo\n"
81  "#dimensions 0: 352x288\n"
82  "#sar 0: 128/117\n"
83  "#stream#, dts, pts, duration, size, hash\n");
84  header = 1;
85  }
87  if ((ret = av_hash_alloc(&hash, "md5")) < 0) {
88  return ret;
89  }
91 
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);
98 
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);
103  frame_cnt += 1;
105  }
106  return 0;
107 }
108 
109 int main(int argc, char **argv)
110 {
111  const AVCodec *codec = NULL;
112  AVCodecContext *c = NULL;
113  AVFrame *frame = NULL;
114  unsigned int threads;
115  AVPacket *pkt;
116  FILE *file = NULL;
117  char * nal = NULL;
118  int nals = 0, ret = 0;
119  char *p;
120 
121  if (argc < 3) {
122  fprintf(stderr, "Usage: %s <threads> <input file>\n", argv[0]);
123  return -1;
124  }
125 
126  if (!(threads = strtoul(argv[1], NULL, 0)))
127  threads = 1;
128  else if (threads > MAX_SLICES)
129  threads = MAX_SLICES;
130 
131 #ifdef _WIN32
132  setmode(fileno(stdout), O_BINARY);
133 #endif
134 
135  if (!(pkt = av_packet_alloc())) {
136  return -1;
137  }
138 
139  nal = av_malloc(MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
140  if (!nal)
141  goto err;
142  p = nal;
143 
144  if (!(codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
145  fprintf(stderr, "Codec not found\n");
146  ret = -1;
147  goto err;
148  }
149 
150  if (!(c = avcodec_alloc_context3(codec))) {
151  fprintf(stderr, "Could not allocate video codec context\n");
152  ret = -1;
153  goto err;
154  }
155 
156  c->width = 352;
157  c->height = 288;
158 
159  c->flags2 |= AV_CODEC_FLAG2_CHUNKS;
160  c->thread_type = FF_THREAD_SLICE;
161  c->thread_count = threads;
162 
163  if ((ret = avcodec_open2(c, codec, NULL)) < 0) {
164  fprintf(stderr, "Could not open codec\n");
165  goto err;
166  }
167 
168 #if HAVE_THREADS
169  if (c->active_thread_type != FF_THREAD_SLICE) {
170  fprintf(stderr, "Couldn't activate slice threading: %d\n", c->active_thread_type);
171  ret = -1;
172  goto err;
173  }
174 #else
175  fprintf(stderr, "WARN: not using threads, only checking decoding slice NALUs\n");
176 #endif
177 
178  if (!(frame = av_frame_alloc())) {
179  fprintf(stderr, "Could not allocate video frame\n");
180  ret = -1;
181  goto err;
182  }
183 
184  if (!(file = fopen(argv[2], "rb"))) {
185  fprintf(stderr, "Couldn't open NALU file: %s\n", argv[2]);
186  ret = -1;
187  goto err;
188  }
189 
190  while(1) {
191  uint16_t size = 0;
192  size_t ret = fread(&size, 1, sizeof(uint16_t), file);
193  if (ret != sizeof(uint16_t))
194  break;
195 
196  size = av_be2ne16(size);
197  ret = fread(p, 1, size, file);
198  if (ret != size) {
199  perror("Couldn't read data");
200  goto err;
201  }
202  p += ret;
203 
204  if (++nals >= threads) {
205  int decret = 0;
206  pkt->data = nal;
207  pkt->size = p - nal;
208  if ((decret = decode(c, frame, pkt)) < 0) {
209  goto err;
210  }
211  memset(nal, 0, MAX_SLICES * UINT16_MAX + AV_INPUT_BUFFER_PADDING_SIZE);
212  nals = 0;
213  p = nal;
214  }
215  }
216 
217  if (nals) {
218  pkt->data = nal;
219  pkt->size = p - nal;
220  if ((ret = decode(c, frame, pkt)) < 0) {
221  goto err;
222  }
223  }
224 
225  ret = decode(c, frame, NULL);
226 
227 err:
228  if (nal)
229  av_free(nal);
230  if (file)
231  fclose(file);
235 
236  return ret;
237 }
AVCodec
AVCodec.
Definition: codec.h:202
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
MAX_SLICES
#define MAX_SLICES
Definition: api-h264-slice-test.c:23
AV_HASH_MAX_SIZE
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition: hash.h:156
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
hash
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:97
pkt
AVPacket * pkt
Definition: movenc.c:59
decode
static int decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt)
Definition: api-h264-slice-test.c:48
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:141
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:102
avcodec_receive_frame
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:642
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
NULL
#define NULL
Definition: coverity.c:32
av_hash_init
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition: hash.c:139
avcodec_free_context
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:156
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:137
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:160
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_hash_freep
void av_hash_freep(AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
Definition: hash.c:236
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:921
AVPacket::size
int size
Definition: packet.h:374
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
size
int size
Definition: twinvq_data.h:10344
FF_THREAD_SLICE
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:1452
printf
printf("static const uint8_t my_array[100] = {\n")
AVHashContext
Definition: hash.c:58
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:579
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
O_BINARY
#define O_BINARY
main
int main(int argc, char **argv)
Definition: api-h264-slice-test.c:109
avcodec.h
ret
ret
Definition: filter_design.txt:187
bswap.h
header
static int header
Definition: api-h264-slice-test.c:46
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AV_CODEC_FLAG2_CHUNKS
#define AV_CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries.
Definition: avcodec.h:306
AVCodecContext
main external API structure.
Definition: avcodec.h:383
hash.h
av_hash_get_size
int av_hash_get_size(const AVHashContext *ctx)
Definition: hash.c:97
av_be2ne16
#define av_be2ne16(x)
Definition: bswap.h:92
desc
const char * desc
Definition: libsvtav1.c:79
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:213
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:350
dec_ctx
static AVCodecContext * dec_ctx
Definition: filtering_audio.c:44