FFmpeg
load_helper.c
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 #include "config.h"
20 
21 #include "libavutil/hwcontext.h"
23 #include "libavutil/cuda_check.h"
24 #include "libavutil/mem.h"
25 
26 #if CONFIG_PTX_COMPRESSION
27 #include <zlib.h>
28 #define CHUNK_SIZE 1024 * 64
29 #endif
30 
31 #include "load_helper.h"
32 
33 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, cu, x)
34 
35 int ff_cuda_load_module(void *avctx, AVCUDADeviceContext *hwctx, CUmodule *cu_module,
36  const unsigned char *data, const unsigned int length)
37 {
38  CudaFunctions *cu = hwctx->internal->cuda_dl;
39 
40 #if CONFIG_PTX_COMPRESSION
41  z_stream stream = { 0 };
42  uint8_t *buf, *tmp;
43  uint64_t buf_size;
44  int ret;
45 
46  if (inflateInit2(&stream, 32 + 15) != Z_OK) {
47  av_log(avctx, AV_LOG_ERROR, "Error during zlib initialisation: %s\n", stream.msg);
48  return AVERROR(ENOSYS);
49  }
50 
51  buf_size = CHUNK_SIZE * 4;
52  buf = av_realloc(NULL, buf_size);
53  if (!buf) {
54  inflateEnd(&stream);
55  return AVERROR(ENOMEM);
56  }
57 
58  stream.next_in = data;
59  stream.avail_in = length;
60 
61  do {
62  stream.avail_out = buf_size - stream.total_out;
63  stream.next_out = buf + stream.total_out;
64 
65  ret = inflate(&stream, Z_FINISH);
66  if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) {
67  av_log(avctx, AV_LOG_ERROR, "zlib inflate error(%d): %s\n", ret, stream.msg);
68  inflateEnd(&stream);
69  av_free(buf);
70  return AVERROR(EINVAL);
71  }
72 
73  if (stream.avail_out == 0) {
74  buf_size += CHUNK_SIZE;
75  tmp = av_realloc(buf, buf_size);
76  if (!tmp) {
77  inflateEnd(&stream);
78  av_free(buf);
79  return AVERROR(ENOMEM);
80  }
81  buf = tmp;
82  }
83  } while (ret != Z_STREAM_END);
84 
85  // NULL-terminate string
86  // there is guaranteed to be space for this, due to condition in loop
87  buf[stream.total_out] = 0;
88 
89  inflateEnd(&stream);
90 
91  ret = CHECK_CU(cu->cuModuleLoadData(cu_module, buf));
92  av_free(buf);
93  return ret;
94 #else
95  return CHECK_CU(cu->cuModuleLoadData(cu_module, data));
96 #endif
97 }
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
hwcontext_cuda_internal.h
CHUNK_SIZE
#define CHUNK_SIZE
Definition: act.c:29
ff_cuda_load_module
int ff_cuda_load_module(void *avctx, AVCUDADeviceContext *hwctx, CUmodule *cu_module, const unsigned char *data, const unsigned int length)
Loads a CUDA module and applies any decompression, if necessary.
Definition: load_helper.c:35
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
data
const char data[16]
Definition: mxf.c:149
CHECK_CU
#define CHECK_CU(x)
Definition: load_helper.c:33
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:209
load_helper.h
NULL
#define NULL
Definition: coverity.c:32
AVCUDADeviceContext::internal
AVCUDADeviceContextInternal * internal
Definition: hwcontext_cuda.h:45
AVCUDADeviceContextInternal::cuda_dl
CudaFunctions * cuda_dl
Definition: hwcontext_cuda_internal.h:32
AVCUDADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_cuda.h:42
ret
ret
Definition: filter_design.txt:187
cuda_check.h
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155