FFmpeg
Loading...
Searching...
No Matches
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
30static 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 */
#define CHUNK_SIZE
Definition act.c:29
#define NULL
Definition coverity.c:32
error code definitions
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
Memory handling functions.
#define av_realloc(p, s)
Definition ops_static.c:54
#define av_free(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
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