FFmpeg
zlib_wrapper.c
Go to the documentation of this file.
1 /*
2  * Wrappers for zlib
3  * Copyright (C) 2022 Andreas Rheinhardt
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <zlib.h>
23 
24 #include "config.h"
25 #include "libavutil/error.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mem.h"
28 #include "zlib_wrapper.h"
29 
30 static void *alloc_wrapper(void *opaque, uInt items, uInt size)
31 {
32  return av_malloc_array(items, size);
33 }
34 
35 static void free_wrapper(void *opaque, void *ptr)
36 {
37  av_free(ptr);
38 }
39 
40 #if CONFIG_INFLATE_WRAPPER
41 int ff_inflate_init(FFZStream *z, void *logctx)
42 {
43  z_stream *const zstream = &z->zstream;
44  int zret;
45 
46  z->inited = 0;
47  zstream->next_in = Z_NULL;
48  zstream->avail_in = 0;
49  zstream->zalloc = alloc_wrapper;
50  zstream->zfree = free_wrapper;
51  zstream->opaque = Z_NULL;
52 
53  zret = inflateInit(zstream);
54  if (zret == Z_OK) {
55  z->inited = 1;
56  } else {
57  av_log(logctx, AV_LOG_ERROR, "inflateInit error %d, message: %s\n",
58  zret, zstream->msg ? zstream->msg : "");
59  return AVERROR_EXTERNAL;
60  }
61  return 0;
62 }
63 
65 {
66  if (z->inited) {
67  z->inited = 0;
68  inflateEnd(&z->zstream);
69  }
70 }
71 #endif
72 
73 #if CONFIG_DEFLATE_WRAPPER
74 int ff_deflate_init(FFZStream *z, int level, void *logctx)
75 {
76  z_stream *const zstream = &z->zstream;
77  int zret;
78 
79  z->inited = 0;
80  zstream->zalloc = alloc_wrapper;
81  zstream->zfree = free_wrapper;
82  zstream->opaque = Z_NULL;
83 
84  zret = deflateInit(zstream, level);
85  if (zret == Z_OK) {
86  z->inited = 1;
87  } else {
88  av_log(logctx, AV_LOG_ERROR, "deflateInit error %d, message: %s\n",
89  zret, zstream->msg ? zstream->msg : "");
90  return AVERROR_EXTERNAL;
91  }
92  return 0;
93 }
94 
96 {
97  if (z->inited) {
98  z->inited = 0;
99  deflateEnd(&z->zstream);
100  }
101 }
102 #endif
level
uint8_t level
Definition: svq3.c:206
FFZStream::zstream
z_stream zstream
Definition: zlib_wrapper.h:28
ff_deflate_end
void ff_deflate_end(FFZStream *zstream)
Wrapper around deflateEnd().
alloc_wrapper
static void * alloc_wrapper(void *opaque, uInt items, uInt size)
Definition: zlib_wrapper.c:30
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
zlib_wrapper.h
free_wrapper
static void free_wrapper(void *opaque, void *ptr)
Definition: zlib_wrapper.c:35
error.h
size
int size
Definition: twinvq_data.h:10344
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
log.h
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
ff_inflate_end
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
FFZStream
Definition: zlib_wrapper.h:27
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
FFZStream::inited
int inited
Definition: zlib_wrapper.h:29
ff_inflate_init
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_deflate_init
int ff_deflate_init(FFZStream *zstream, int level, void *logctx)
Wrapper around deflateInit().