FFmpeg  4.1.9
rpi_zc.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018 Raspberry Pi (Trading) Ltd.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the copyright holder nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 Authors: John Cox
28 */
29 
30 #ifndef LIBAVCODEC_RPI_ZC_H
31 #define LIBAVCODEC_RPI_ZC_H
32 
33 // Zero-Copy frame code for RPi
34 // RPi needs Y/U/V planes to be contiguous for display. By default
35 // ffmpeg will allocate separated planes so a memcpy is needed before
36 // display. This code provides a method a making ffmpeg allocate a single
37 // bit of memory for the frame when can then be reference counted until
38 // display has finished with it.
39 
40 // Frame buffer number in which to stuff an 8-bit copy of a 16-bit frame
41 // 0 disables
42 // *** This option still in development
43 // Only works if SAO active
44 // Allocates buffers that are twice the required size
45 #define RPI_ZC_SAND_8_IN_10_BUF 0
46 
47 struct AVBufferRef;
48 struct AVFrame;
49 struct AVCodecContext;
50 enum AVPixelFormat;
51 
52 // "Opaque" pointer to whatever we are using as a buffer reference
53 typedef struct AVBufferRef * AVRpiZcRefPtr;
54 
55 struct AVZcEnv;
56 typedef struct AVZcEnv * AVZcEnvPtr;
57 
58 typedef struct AVRpiZcFrameGeometry
59 {
60  unsigned int stride_y; // Luma stride (bytes)
61  unsigned int height_y; // Luma height (lines)
62  unsigned int stride_c; // Chroma stride (bytes)
63  unsigned int height_c; // Chroma stride (lines)
64  unsigned int planes_c; // Chroma plane count (U, V = 2, interleaved = 1)
65  unsigned int stripes; // Number of stripes (sand)
66  unsigned int bytes_per_pel;
67  int stripe_is_yc; // A single stripe is Y then C (false for tall sand)
68 
69  int format; // Requested format
70  unsigned int video_width; // Requested width
71  unsigned int video_height; // Requested height
73 
74 
76  const int format,
77  const unsigned int video_width, const unsigned int video_height);
78 
79 // Generate a ZC reference to the buffer(s) in this frame
80 // If the buffer doesn't appear to be one allocated by ZC
81 // then the behaviour depends on maycopy:
82 // If maycopy=0 then return NULL
83 // If maycopy=1 && the src frame is in a form where we can easily copy
84 // the data, then allocate a new buffer and copy the data into it
85 // Otherwise return NULL
86 // If maycopy == 0 then ZC may be NULL
87 AVRpiZcRefPtr av_rpi_zc_ref(void * const logging_context, const AVZcEnvPtr zc,
88  const struct AVFrame * const frame, const enum AVPixelFormat expected_format, const int maycopy);
89 
90 // Get the vc_handle from the frame ref
91 // Returns -1 if ref doesn't look valid
92 int av_rpi_zc_vc_handle(const AVRpiZcRefPtr fr_ref);
93 // Get the vcsm_handle from the frame ref
94 // Returns 0 if ref doesn't look valid
95 unsigned int av_rpi_zc_vcsm_handle(const AVRpiZcRefPtr fr_ref);
96 // Get offset from the start of the memory referenced
97 // by the vc_handle to valid data
98 int av_rpi_zc_offset(const AVRpiZcRefPtr fr_ref);
99 // Length of buffer data
100 int av_rpi_zc_length(const AVRpiZcRefPtr fr_ref);
101 // Get the number of bytes allocated from the frame ref
102 // Returns 0 if ref doesn't look valid
103 int av_rpi_zc_numbytes(const AVRpiZcRefPtr fr_ref);
104 // Geometry this frame was allocated with
105 const AVRpiZcFrameGeometry * av_rpi_zc_geometry(const AVRpiZcRefPtr fr_ref);
106 
107 // Unreference the buffer refed/allocated by _zc_ref
108 // If fr_ref is NULL then this will NOP
109 void av_rpi_zc_unref(AVRpiZcRefPtr fr_ref);
110 
111 // Test to see if the context is using zc (checks get_buffer2)
112 int av_rpi_zc_in_use(const struct AVCodecContext * const s);
113 
114 // Init ZC into a context
115 // Sets opaque, get_buffer2, thread_safe_callbacks
116 // Use if you want to allocate your own pools and/or create ZC buffers for
117 // all decoders
118 // RPI HEVC decoders will allocate appropriate VCSM buffers which can be taken
119 // apart by av_rpi_zc_xxx calls without this
120 
121 typedef AVBufferRef * av_rpi_zc_alloc_buf_fn_t(void * pool_env, size_t size,
122  const AVRpiZcFrameGeometry * geo);
123 typedef void av_rpi_zc_free_pool_fn_t(void * pool_env);
124 
125 int av_rpi_zc_init2(struct AVCodecContext * const s,
126  void * pool_env, av_rpi_zc_alloc_buf_fn_t * alloc_buf_fn,
127  av_rpi_zc_free_pool_fn_t * free_pool_fn);
128 
129 // Free ZC from a context
130 void av_rpi_zc_uninit2(struct AVCodecContext * const s);
131 
133 AVZcEnvPtr av_rpi_zc_int_env_alloc(void * const logctx);
134 void av_rpi_zc_set_decoder_pool_size(const AVZcEnvPtr zc, const unsigned int pool_size);
135 unsigned int av_rpi_zc_get_decoder_pool_size(const AVZcEnvPtr zc);
136 
137 // Get buffer generates placeholders for later alloc
138 int av_rpi_zc_get_buffer(const AVZcEnvPtr zc, AVFrame * const frame);
139 // Resolve actually does the alloc (noop if already alloced)
140 // Set data pointers on a buffer/frame that was copied before the alloc
141 // accured
142 #define ZC_RESOLVE_FAIL 0 // return error on invalid
143 #define ZC_RESOLVE_ALLOC 1 // alloc as invalid
144 #define ZC_RESOLVE_WAIT_VALID 2 // wait for valid
145 #define ZC_RESOLVE_ALLOC_VALID 3 // alloc as valid
146 int av_rpi_zc_resolve_buffer(AVBufferRef * const buf, const int may_alloc);
147 int av_rpi_zc_resolve_frame(AVFrame * const frame, const int may_alloc);
148 
151 
152 
153 typedef struct av_rpi_zc_buf_fn_tab_s {
154  void (* free)(void * v);
155 
156  unsigned int (* vcsm_handle)(void * v);
157  unsigned int (* vc_handle)(void * v);
158  void * (* map_arm)(void * v);
159  unsigned int (* map_vc)(void * v);
161 
162 AVBufferRef * av_rpi_zc_buf(size_t numbytes, int addr_offset, void * v, const av_rpi_zc_buf_fn_tab_t * fn_tab);
163 void * av_rpi_zc_buf_v(AVBufferRef * const buf);
164 
165 
166 AVZcEnvPtr av_rpi_zc_env_alloc(void * logctx,
167  void * pool_env,
168  av_rpi_zc_alloc_buf_fn_t * alloc_buf_fn,
169  av_rpi_zc_free_pool_fn_t * free_pool_fn);
170 void av_rpi_zc_env_release(const AVZcEnvPtr zc);
171 
172 
173 #endif
174 
unsigned int planes_c
Definition: rpi_zc.h:64
void av_rpi_zc_free_pool_fn_t(void *pool_env)
Definition: rpi_zc.h:123
unsigned int stripes
Definition: rpi_zc.h:65
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVZcEnvPtr av_rpi_zc_env_alloc(void *logctx, void *pool_env, av_rpi_zc_alloc_buf_fn_t *alloc_buf_fn, av_rpi_zc_free_pool_fn_t *free_pool_fn)
void * av_rpi_zc_buf_v(AVBufferRef *const buf)
unsigned int av_rpi_zc_vcsm_handle(const AVRpiZcRefPtr fr_ref)
AVBufferRef * av_rpi_zc_alloc_buf_fn_t(void *pool_env, size_t size, const AVRpiZcFrameGeometry *geo)
Definition: rpi_zc.h:121
void av_rpi_zc_uninit2(struct AVCodecContext *const s)
int av_rpi_zc_vc_handle(const AVRpiZcRefPtr fr_ref)
const AVRpiZcFrameGeometry * av_rpi_zc_geometry(const AVRpiZcRefPtr fr_ref)
int av_rpi_zc_resolve_buffer(AVBufferRef *const buf, const int may_alloc)
int av_rpi_zc_set_valid_frame(AVFrame *const frame)
static AVFrame * frame
AVRpiZcRefPtr av_rpi_zc_ref(void *const logging_context, const AVZcEnvPtr zc, const struct AVFrame *const frame, const enum AVPixelFormat expected_format, const int maycopy)
unsigned int av_rpi_zc_get_decoder_pool_size(const AVZcEnvPtr zc)
AVZcEnvPtr av_rpi_zc_int_env_alloc(void *const logctx)
int av_rpi_zc_init2(struct AVCodecContext *const s, void *pool_env, av_rpi_zc_alloc_buf_fn_t *alloc_buf_fn, av_rpi_zc_free_pool_fn_t *free_pool_fn)
unsigned int height_y
Definition: rpi_zc.h:61
int av_rpi_zc_length(const AVRpiZcRefPtr fr_ref)
AVRpiZcFrameGeometry av_rpi_zc_frame_geometry(const int format, const unsigned int video_width, const unsigned int video_height)
void av_rpi_zc_env_release(const AVZcEnvPtr zc)
int av_rpi_zc_offset(const AVRpiZcRefPtr fr_ref)
unsigned int bytes_per_pel
Definition: rpi_zc.h:66
main external API structure.
Definition: avcodec.h:1533
struct AVZcEnv * AVZcEnvPtr
Definition: rpi_zc.h:56
AVBufferRef * av_rpi_zc_buf(size_t numbytes, int addr_offset, void *v, const av_rpi_zc_buf_fn_tab_t *fn_tab)
void av_rpi_zc_set_decoder_pool_size(const AVZcEnvPtr zc, const unsigned int pool_size)
unsigned int stride_y
Definition: rpi_zc.h:60
int av_rpi_zc_set_broken_frame(AVFrame *const frame)
unsigned int height_c
Definition: rpi_zc.h:63
unsigned int video_height
Definition: rpi_zc.h:71
unsigned int video_width
Definition: rpi_zc.h:70
int av_rpi_zc_get_buffer(const AVZcEnvPtr zc, AVFrame *const frame)
int av_rpi_zc_resolve_frame(AVFrame *const frame, const int may_alloc)
A reference to a data buffer.
Definition: buffer.h:81
unsigned int stride_c
Definition: rpi_zc.h:62
int av_rpi_zc_numbytes(const AVRpiZcRefPtr fr_ref)
void av_rpi_zc_int_env_freep(AVZcEnvPtr *zc)
int av_rpi_zc_in_use(const struct AVCodecContext *const s)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
void av_rpi_zc_unref(AVRpiZcRefPtr fr_ref)