FFmpeg
lcevcdec.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_components.h"
20 
21 #include "libavutil/avassert.h"
22 #include "libavutil/frame.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "decode.h"
27 #include "lcevcdec.h"
28 
29 #if CONFIG_LIBLCEVC_DEC
30 static LCEVC_ColorFormat map_format(int format)
31 {
32  switch (format) {
33  case AV_PIX_FMT_YUV420P:
34  return LCEVC_I420_8;
36  return LCEVC_I420_10_LE;
37  case AV_PIX_FMT_NV12:
38  return LCEVC_NV12_8;
39  case AV_PIX_FMT_NV21:
40  return LCEVC_NV21_8;
41  case AV_PIX_FMT_GRAY8:
42  return LCEVC_GRAY_8;
43  }
44 
45  return LCEVC_ColorFormat_Unknown;
46 }
47 
48 static int alloc_base_frame(void *logctx, FFLCEVCContext *lcevc,
49  const AVFrame *frame, LCEVC_PictureHandle *picture)
50 {
51  LCEVC_PictureDesc desc;
52  LCEVC_ColorFormat fmt = map_format(frame->format);
53  LCEVC_PictureLockHandle lock;
54  uint8_t *data[4] = { NULL };
55  int linesizes[4] = { 0 };
56  uint32_t planes;
57  LCEVC_ReturnCode res;
58 
59  res = LCEVC_DefaultPictureDesc(&desc, fmt, frame->width, frame->height);
60  if (res != LCEVC_Success)
61  return AVERROR_EXTERNAL;
62 
63  desc.cropTop = frame->crop_top;
64  desc.cropBottom = frame->crop_bottom;
65  desc.cropLeft = frame->crop_left;
66  desc.cropRight = frame->crop_right;
67  desc.sampleAspectRatioNum = frame->sample_aspect_ratio.num;
68  desc.sampleAspectRatioDen = frame->sample_aspect_ratio.den;
69 
70  /* Allocate LCEVC Picture */
71  res = LCEVC_AllocPicture(lcevc->decoder, &desc, picture);
72  if (res != LCEVC_Success) {
73  return AVERROR_EXTERNAL;
74  }
75  res = LCEVC_LockPicture(lcevc->decoder, *picture, LCEVC_Access_Write, &lock);
76  if (res != LCEVC_Success)
77  return AVERROR_EXTERNAL;
78 
79  res = LCEVC_GetPicturePlaneCount(lcevc->decoder, *picture, &planes);
80  if (res != LCEVC_Success)
81  return AVERROR_EXTERNAL;
82 
83  for (unsigned i = 0; i < planes; i++) {
84  LCEVC_PicturePlaneDesc plane;
85 
86  res = LCEVC_GetPictureLockPlaneDesc(lcevc->decoder, lock, i, &plane);
87  if (res != LCEVC_Success)
88  return AVERROR_EXTERNAL;
89 
90  data[i] = plane.firstSample;
91  linesizes[i] = plane.rowByteStride;
92  }
93 
94  av_image_copy2(data, linesizes, frame->data, frame->linesize,
95  frame->format, frame->width, frame->height);
96 
97  res = LCEVC_UnlockPicture(lcevc->decoder, lock);
98  if (res != LCEVC_Success)
99  return AVERROR_EXTERNAL;
100 
101  return 0;
102 }
103 
104 static int alloc_enhanced_frame(void *logctx, FFLCEVCFrame *frame_ctx,
105  LCEVC_PictureHandle *picture)
106 {
107  FFLCEVCContext *lcevc = frame_ctx->lcevc;
108  LCEVC_PictureDesc desc ;
109  LCEVC_ColorFormat fmt = map_format(frame_ctx->frame->format);
110  LCEVC_PicturePlaneDesc planes[4] = { 0 };
111  LCEVC_ReturnCode res;
112 
113  res = LCEVC_DefaultPictureDesc(&desc, fmt, frame_ctx->frame->width, frame_ctx->frame->height);
114  if (res != LCEVC_Success)
115  return AVERROR_EXTERNAL;
116 
117  /* Set plane description */
118  for (int i = 0; i < 4; i++) {
119  planes[i].firstSample = frame_ctx->frame->data[i];
120  planes[i].rowByteStride = frame_ctx->frame->linesize[i];
121  }
122 
123  /* Allocate LCEVC Picture */
124  res = LCEVC_AllocPictureExternal(lcevc->decoder, &desc, NULL, planes, picture);
125  if (res != LCEVC_Success) {
126  return AVERROR_EXTERNAL;
127  }
128  return 0;
129 }
130 
131 static int lcevc_send_frame(void *logctx, FFLCEVCFrame *frame_ctx, const AVFrame *in)
132 {
133  FFLCEVCContext *lcevc = frame_ctx->lcevc;
135  LCEVC_PictureHandle picture;
136  LCEVC_ReturnCode res;
137  int ret = 0;
138 
139  if (!sd)
140  return 1;
141 
142  res = LCEVC_SendDecoderEnhancementData(lcevc->decoder, in->pts, 0, sd->data, sd->size);
143  if (res != LCEVC_Success)
144  return AVERROR_EXTERNAL;
145 
146  ret = alloc_base_frame(logctx, lcevc, in, &picture);
147  if (ret < 0)
148  return ret;
149 
150  res = LCEVC_SendDecoderBase(lcevc->decoder, in->pts, 0, picture, -1, NULL);
151  if (res != LCEVC_Success)
152  return AVERROR_EXTERNAL;
153 
154  memset(&picture, 0, sizeof(picture));
155  ret = alloc_enhanced_frame(logctx, frame_ctx, &picture);
156  if (ret < 0)
157  return ret;
158 
159  res = LCEVC_SendDecoderPicture(lcevc->decoder, picture);
160  if (res != LCEVC_Success)
161  return AVERROR_EXTERNAL;
162 
163  return 0;
164 }
165 
166 static int generate_output(void *logctx, FFLCEVCFrame *frame_ctx, AVFrame *out)
167 {
168  FFLCEVCContext *lcevc = frame_ctx->lcevc;
169  LCEVC_PictureDesc desc;
170  LCEVC_DecodeInformation info;
171  LCEVC_PictureHandle picture;
172  LCEVC_ReturnCode res;
173 
174  res = LCEVC_ReceiveDecoderPicture(lcevc->decoder, &picture, &info);
175  if (res != LCEVC_Success)
176  return AVERROR_EXTERNAL;
177 
178  res = LCEVC_GetPictureDesc(lcevc->decoder, picture, &desc);
179  if (res != LCEVC_Success)
180  return AVERROR_EXTERNAL;
181 
182  out->crop_top = desc.cropTop;
183  out->crop_bottom = desc.cropBottom;
184  out->crop_left = desc.cropLeft;
185  out->crop_right = desc.cropRight;
186  out->sample_aspect_ratio.num = desc.sampleAspectRatioNum;
187  out->sample_aspect_ratio.den = desc.sampleAspectRatioDen;
188 
189  av_frame_copy_props(frame_ctx->frame, out);
191  av_frame_move_ref(out, frame_ctx->frame);
192 
193  out->width = desc.width + out->crop_left + out->crop_right;
194  out->height = desc.height + out->crop_top + out->crop_bottom;
195 
196  res = LCEVC_FreePicture(lcevc->decoder, picture);
197  if (res != LCEVC_Success)
198  return AVERROR_EXTERNAL;
199 
200  return 0;
201 }
202 
203 static int lcevc_receive_frame(void *logctx, FFLCEVCFrame *frame_ctx, AVFrame *out)
204 {
205  FFLCEVCContext *lcevc = frame_ctx->lcevc;
206  LCEVC_PictureHandle picture;
207  LCEVC_ReturnCode res;
208  int ret;
209 
210  ret = generate_output(logctx, frame_ctx, out);
211  if (ret < 0)
212  return ret;
213 
214  while (1) {
215  res = LCEVC_ReceiveDecoderBase (lcevc->decoder, &picture);
216  if (res != LCEVC_Success && res != LCEVC_Again)
217  return AVERROR_EXTERNAL;
218 
219  if (res == LCEVC_Again)
220  break;
221 
222  res = LCEVC_FreePicture(lcevc->decoder, picture);
223  if (res != LCEVC_Success)
224  return AVERROR_EXTERNAL;
225  }
226 
227  return 0;
228 }
229 
230 static void event_callback(LCEVC_DecoderHandle dec, LCEVC_Event event,
231  LCEVC_PictureHandle pic, const LCEVC_DecodeInformation *info,
232  const uint8_t *data, uint32_t size, void *logctx)
233 {
234  switch (event) {
235  case LCEVC_Log:
236  av_log(logctx, AV_LOG_INFO, "%s\n", data);
237  break;
238  default:
239  break;
240  }
241 }
242 
243 static void lcevc_free(FFRefStructOpaque unused, void *obj)
244 {
245  FFLCEVCContext *lcevc = obj;
246  if (lcevc->initialized)
247  LCEVC_DestroyDecoder(lcevc->decoder);
248  memset(lcevc, 0, sizeof(*lcevc));
249 }
250 #endif
251 
252 static int lcevc_init(FFLCEVCContext *lcevc, void *logctx)
253 {
254 #if CONFIG_LIBLCEVC_DEC
255  LCEVC_AccelContextHandle dummy = { 0 };
256  const int32_t event = LCEVC_Log;
257 
258  if (LCEVC_CreateDecoder(&lcevc->decoder, dummy) != LCEVC_Success) {
259  av_log(logctx, AV_LOG_ERROR, "Failed to create LCEVC decoder\n");
260  return AVERROR_EXTERNAL;
261  }
262 
263  LCEVC_ConfigureDecoderInt(lcevc->decoder, "log_level", 4);
264  LCEVC_ConfigureDecoderIntArray(lcevc->decoder, "events", 1, &event);
265  LCEVC_SetDecoderEventCallback(lcevc->decoder, event_callback, logctx);
266 
267  if (LCEVC_InitializeDecoder(lcevc->decoder) != LCEVC_Success) {
268  av_log(logctx, AV_LOG_ERROR, "Failed to initialize LCEVC decoder\n");
269  LCEVC_DestroyDecoder(lcevc->decoder);
270  return AVERROR_EXTERNAL;
271  }
272 
273 #endif
274  lcevc->initialized = 1;
275 
276  return 0;
277 }
278 
279 int ff_lcevc_process(void *logctx, AVFrame *frame)
280 {
281  FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
282  FFLCEVCFrame *frame_ctx = fdd->post_process_opaque;
283  FFLCEVCContext *lcevc = frame_ctx->lcevc;
284  int ret;
285 
286  if (!lcevc->initialized) {
287  ret = lcevc_init(lcevc, logctx);
288  if (ret < 0)
289  return ret;
290  }
291 
292 #if CONFIG_LIBLCEVC_DEC
293  av_assert0(frame_ctx->frame);
294 
295 
296  ret = lcevc_send_frame(logctx, frame_ctx, frame);
297  if (ret)
298  return ret < 0 ? ret : 0;
299 
300  lcevc_receive_frame(logctx, frame_ctx, frame);
301  if (ret < 0)
302  return ret;
303 
305 #endif
306 
307  return 0;
308 }
309 
311 {
312  FFLCEVCContext *lcevc = NULL;
313 #if CONFIG_LIBLCEVC_DEC
314  lcevc = ff_refstruct_alloc_ext(sizeof(*lcevc), 0, NULL, lcevc_free);
315  if (!lcevc)
316  return AVERROR(ENOMEM);
317 #endif
318  *plcevc = lcevc;
319  return 0;
320 }
321 
322 void ff_lcevc_unref(void *opaque)
323 {
324  FFLCEVCFrame *lcevc = opaque;
325  ff_refstruct_unref(&lcevc->lcevc);
326  av_frame_free(&lcevc->frame);
327  av_free(opaque);
328 }
lcevcdec.h
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
out
FILE * out
Definition: movenc.c:55
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:951
ff_lcevc_alloc
int ff_lcevc_alloc(FFLCEVCContext **plcevc)
Definition: lcevcdec.c:310
ff_refstruct_alloc_ext
static void * ff_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(FFRefStructOpaque opaque, void *obj))
A wrapper around ff_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:94
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FrameDecodeData
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:501
AVFrame::width
int width
Definition: frame.h:461
FFLCEVCContext
Definition: lcevcdec.h:32
data
const char data[16]
Definition: mxf.c:148
lcevc_init
static int lcevc_init(FFLCEVCContext *lcevc, void *logctx)
Definition: lcevcdec.c:252
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:478
FFLCEVCContext::initialized
int initialized
Definition: lcevcdec.h:34
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
map_format
static LCEVC_ColorFormat map_format(int format)
Definition: vf_lcevc.c:35
FFLCEVCContext::decoder
LCEVC_DecoderHandle decoder
Definition: lcevcdec.h:33
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
dummy
int dummy
Definition: motion.c:66
ff_lcevc_process
int ff_lcevc_process(void *logctx, AVFrame *frame)
Definition: lcevcdec.c:279
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVFrameSideData::size
size_t size
Definition: frame.h:268
FrameDecodeData::post_process_opaque
void * post_process_opaque
Definition: decode.h:45
format
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 format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
info
MIPS optimizations info
Definition: mips.txt:2
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
decode.h
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
alloc_enhanced_frame
static int alloc_enhanced_frame(AVFilterLink *inlink, const AVFrame *out, LCEVC_PictureHandle *picture)
Definition: vf_lcevc.c:161
generate_output
static int generate_output(AVFilterLink *inlink, AVFrame *out)
Definition: vf_lcevc.c:189
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:713
ff_lcevc_unref
void ff_lcevc_unref(void *opaque)
Definition: lcevcdec.c:322
FFLCEVCFrame::lcevc
FFLCEVCContext * lcevc
Definition: lcevcdec.h:40
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AV_FRAME_DATA_LCEVC
@ AV_FRAME_DATA_LCEVC
Raw LCEVC payload data, as a uint8_t array, with NAL emulation bytes intact.
Definition: frame.h:236
size
int size
Definition: twinvq_data.h:10344
LCEVC_DecoderHandle
uintptr_t LCEVC_DecoderHandle
Definition: lcevcdec.h:28
alloc_base_frame
static int alloc_base_frame(AVFilterLink *inlink, const AVFrame *in, LCEVC_PictureHandle *picture)
Definition: vf_lcevc.c:79
AVFrameSideData::data
uint8_t * data
Definition: frame.h:267
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:476
frame.h
av_frame_remove_side_data
void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
Remove and free all side data instances of the given type.
Definition: frame.c:1017
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
lock
static pthread_mutex_t lock
Definition: ffjni.c:39
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:637
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:610
AV_PIX_FMT_NV21
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:97
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVFrame::height
int height
Definition: frame.h:461
planes
static const struct @455 planes[]
av_image_copy2
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition: imgutils.h:184
desc
const char * desc
Definition: libsvtav1.c:79
mem.h
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:265
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
int32_t
int32_t
Definition: audioconvert.c:56
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FFLCEVCFrame
Definition: lcevcdec.h:39
FFLCEVCFrame::frame
struct AVFrame * frame
Definition: lcevcdec.h:41
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120