FFmpeg  4.1.4
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 // Replacement fn for avctx->get_buffer2
80 // Should be set before calling avcodec_decode_open2
81 //
82 // N.B. in addition to to setting avctx->get_buffer2, avctx->refcounted_frames
83 // must be set to 1 as otherwise the buffer info is killed before being returned
84 // by avcodec_decode_video2. Note also that this means that the AVFrame that is
85 // returned must be manually derefed with av_frame_unref. This should be done
86 // after av_rpi_zc_ref has been called.
87 int av_rpi_zc_get_buffer2(struct AVCodecContext *s, AVFrame *frame, int flags);
88 
89 // Generate a ZC reference to the buffer(s) in this frame
90 // If the buffer doesn't appear to be one allocated by ZC
91 // then the behaviour depends on maycopy:
92 // If maycopy=0 then return NULL
93 // If maycopy=1 && the src frame is in a form where we can easily copy
94 // the data, then allocate a new buffer and copy the data into it
95 // Otherwise return NULL
96 // If maycopy == 0 then ZC may be NULL
97 AVRpiZcRefPtr av_rpi_zc_ref(void * const logging_context, const AVZcEnvPtr zc,
98  const struct AVFrame * const frame, const enum AVPixelFormat expected_format, const int maycopy);
99 
100 // Get the vc_handle from the frame ref
101 // Returns -1 if ref doesn't look valid
102 int av_rpi_zc_vc_handle(const AVRpiZcRefPtr fr_ref);
103 // Get offset from the start of the memory referenced
104 // by the vc_handle to valid data
105 int av_rpi_zc_offset(const AVRpiZcRefPtr fr_ref);
106 // Length of buffer data
107 int av_rpi_zc_length(const AVRpiZcRefPtr fr_ref);
108 // Get the number of bytes allocated from the frame ref
109 // Returns 0 if ref doesn't look valid
110 int av_rpi_zc_numbytes(const AVRpiZcRefPtr fr_ref);
111 
112 // Unreference the buffer refed/allocated by _zc_ref
113 // If fr_ref is NULL then this will NOP
114 void av_rpi_zc_unref(AVRpiZcRefPtr fr_ref);
115 
116 // Test to see if the context is using zc (checks get_buffer2)
117 int av_rpi_zc_in_use(const struct AVCodecContext * const s);
118 
119 // Init ZC into a context
120 // There is nothing magic in this fn - it just packages setting
121 // get_buffer2 & get_buffer_context
122 
123 typedef AVBufferRef * av_rpi_zc_alloc_buf_fn_t(void * pool_env, size_t size,
124  const AVRpiZcFrameGeometry * geo);
125 typedef void av_rpi_zc_free_pool_fn_t(void * pool_env);
126 
127 int av_rpi_zc_init2(struct AVCodecContext * const s,
128  void * pool_env, av_rpi_zc_alloc_buf_fn_t * alloc_buf_fn,
129  av_rpi_zc_free_pool_fn_t * free_pool_fn);
130 
131 // Free ZC from a context
132 // There is nothing magic in this fn - it just packages unsetting
133 // get_buffer2 & get_buffer_context
134 void av_rpi_zc_uninit2(struct AVCodecContext * const s);
135 
137 AVZcEnvPtr av_rpi_zc_int_env_alloc(void * const logctx);
138 void av_rpi_zc_set_decoder_pool_size(const AVZcEnvPtr zc, const unsigned int pool_size);
139 unsigned int av_rpi_zc_get_decoder_pool_size(const AVZcEnvPtr zc);
140 
141 // Get buffer generates placeholders for later alloc
142 int av_rpi_zc_get_buffer(const AVZcEnvPtr zc, AVFrame * const frame);
143 // Resolve actually does the alloc (noop if already alloced)
144 // Set data pointers on a buffer/frame that was copied before the alloc
145 // accured
146 #define ZC_RESOLVE_FAIL 0 // return error on invalid
147 #define ZC_RESOLVE_ALLOC 1 // alloc as invalid
148 #define ZC_RESOLVE_WAIT_VALID 2 // wait for valid
149 #define ZC_RESOLVE_ALLOC_VALID 3 // alloc as valid
150 int av_rpi_zc_resolve_buffer(AVBufferRef * const buf, const int may_alloc);
151 int av_rpi_zc_resolve_frame(AVFrame * const frame, const int may_alloc);
152 
155 
156 
157 typedef struct av_rpi_zc_buf_fn_tab_s {
158  void (* free)(void * v);
159 
160  unsigned int (* vcsm_handle)(void * v);
161  unsigned int (* vc_handle)(void * v);
162  void * (* map_arm)(void * v);
163  unsigned int (* map_vc)(void * v);
165 
166 AVBufferRef * av_rpi_zc_buf(size_t numbytes, int addr_offset, void * v, const av_rpi_zc_buf_fn_tab_t * fn_tab);
167 void * av_rpi_zc_buf_v(AVBufferRef * const buf);
168 
169 
170 AVZcEnvPtr av_rpi_zc_env_alloc(void * logctx,
171  void * pool_env,
172  av_rpi_zc_alloc_buf_fn_t * alloc_buf_fn,
173  av_rpi_zc_free_pool_fn_t * free_pool_fn);
174 void av_rpi_zc_env_release(const AVZcEnvPtr zc);
175 
176 
177 #endif
178 
unsigned int planes_c
Definition: rpi_zc.h:64
void av_rpi_zc_free_pool_fn_t(void *pool_env)
Definition: rpi_zc.h:125
unsigned int stripes
Definition: rpi_zc.h:65
int av_rpi_zc_get_buffer2(struct AVCodecContext *s, AVFrame *frame, int flags)
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)
AVBufferRef * av_rpi_zc_alloc_buf_fn_t(void *pool_env, size_t size, const AVRpiZcFrameGeometry *geo)
Definition: rpi_zc.h:123
void av_rpi_zc_uninit2(struct AVCodecContext *const s)
int av_rpi_zc_vc_handle(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)