FFmpeg
zlib_utils.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVUTIL_ZLIB_UTILS_H
20 #define AVUTIL_ZLIB_UTILS_H
21 
22 #include <stdint.h>
23 #include "log.h"
24 #include "error.h"
25 #include "mem.h"
26 
27 #include <zlib.h>
28 #define CHUNK_SIZE 1024 * 64
29 
30 static inline int ff_zlib_expand(void *ctx, uint8_t **out, size_t *out_len,
31  const uint8_t *src, int src_len)
32 {
33  int ret;
34 
35  z_stream stream = { 0 };
36  if (inflateInit2(&stream, 32 + 15) != Z_OK) {
37  av_log(ctx, AV_LOG_ERROR, "Error during zlib initialisation: %s\n",
38  stream.msg);
39  return AVERROR(ENOSYS);
40  }
41 
42  uint64_t buf_size = CHUNK_SIZE * 4;
43  uint8_t *buf = av_realloc(NULL, buf_size);
44  if (!buf) {
45  inflateEnd(&stream);
46  return AVERROR(ENOMEM);
47  }
48 
49  stream.next_in = src;
50  stream.avail_in = src_len;
51 
52  do {
53  stream.avail_out = buf_size - stream.total_out;
54  stream.next_out = buf + stream.total_out;
55 
56  ret = inflate(&stream, Z_FINISH);
57  if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) {
58  av_log(ctx, AV_LOG_ERROR, "zlib inflate error(%d): %s\n",
59  ret, stream.msg);
60  inflateEnd(&stream);
61  av_free(buf);
62  return AVERROR(EINVAL);
63  }
64 
65  if (stream.avail_out == 0) {
66  buf_size += CHUNK_SIZE;
67  uint8_t *tmp = av_realloc(buf, buf_size);
68  if (!tmp) {
69  inflateEnd(&stream);
70  av_free(buf);
71  return AVERROR(ENOMEM);
72  }
73  buf = tmp;
74  }
75  } while (ret != Z_STREAM_END);
76 
77  // NULL-terminate string
78  // there is guaranteed to be space for this, due to condition in loop
79  buf[stream.total_out] = 0;
80 
81  inflateEnd(&stream);
82 
83  *out = buf;
84  *out_len = stream.total_out;
85 
86  return 0;
87 }
88 #endif /* AVUTIL_ZLIB_UTILS_H */
ff_zlib_expand
static int ff_zlib_expand(void *ctx, uint8_t **out, size_t *out_len, const uint8_t *src, int src_len)
Definition: zlib_utils.h:30
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
out
FILE * out
Definition: movenc.c:55
inflate
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:194
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
ctx
AVFormatContext * ctx
Definition: movenc.c:49
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
NULL
#define NULL
Definition: coverity.c:32
error.h
CHUNK_SIZE
#define CHUNK_SIZE
Definition: zlib_utils.h:28
log.h
ret
ret
Definition: filter_design.txt:187
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
src
#define src
Definition: vp8dsp.c:248
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155