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 "libavutil/avassert.h"
20 #include "libavutil/frame.h"
21 #include "libavutil/imgutils.h"
22 #include "libavutil/log.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/refstruct.h"
25 
26 #include "decode.h"
27 #include "lcevcdec.h"
28 
29 static LCEVC_ColorFormat map_format(int format)
30 {
31  switch (format) {
32  case AV_PIX_FMT_YUV420P:
33  return LCEVC_I420_8;
35  return LCEVC_I420_10_LE;
36  case AV_PIX_FMT_NV12:
37  return LCEVC_NV12_8;
38  case AV_PIX_FMT_NV21:
39  return LCEVC_NV21_8;
40  case AV_PIX_FMT_GRAY8:
41  return LCEVC_GRAY_8;
42  }
43 
44  return LCEVC_ColorFormat_Unknown;
45 }
46 
47 static int alloc_base_frame(void *logctx, FFLCEVCContext *lcevc,
48  const AVFrame *frame, LCEVC_PictureHandle *picture)
49 {
50  LCEVC_PictureDesc desc;
51  LCEVC_ColorFormat fmt = map_format(frame->format);
52  LCEVC_PicturePlaneDesc planes[AV_VIDEO_MAX_PLANES] = { 0 };
53  int width = frame->width - frame->crop_left - frame->crop_right;
54  int height = frame->height - frame->crop_top - frame->crop_bottom;
55  LCEVC_ReturnCode res;
56 
57  res = LCEVC_DefaultPictureDesc(&desc, fmt, width, height);
58  if (res != LCEVC_Success)
59  return AVERROR_EXTERNAL;
60 
61  desc.cropTop = frame->crop_top;
62  desc.cropBottom = frame->crop_bottom;
63  desc.cropLeft = frame->crop_left;
64  desc.cropRight = frame->crop_right;
65  desc.sampleAspectRatioNum = frame->sample_aspect_ratio.num;
66  desc.sampleAspectRatioDen = frame->sample_aspect_ratio.den;
67 
68  for (int i = 0; i < AV_VIDEO_MAX_PLANES; i++) {
69  planes[i].firstSample = frame->data[i];
70  planes[i].rowByteStride = frame->linesize[i];
71  }
72 
73  /* Allocate LCEVC Picture */
74  res = LCEVC_AllocPictureExternal(lcevc->decoder, &desc, NULL, planes, picture);
75  if (res != LCEVC_Success) {
76  return AVERROR_EXTERNAL;
77  }
78 
79  return 0;
80 }
81 
82 static int alloc_enhanced_frame(void *logctx, FFLCEVCFrame *frame_ctx,
83  LCEVC_PictureHandle *picture)
84 {
85  FFLCEVCContext *lcevc = frame_ctx->lcevc;
86  LCEVC_PictureDesc desc ;
87  LCEVC_ColorFormat fmt = map_format(frame_ctx->frame->format);
88  LCEVC_PicturePlaneDesc planes[4] = { 0 };
89  LCEVC_ReturnCode res;
90 
91  res = LCEVC_DefaultPictureDesc(&desc, fmt, frame_ctx->frame->width, frame_ctx->frame->height);
92  if (res != LCEVC_Success)
93  return AVERROR_EXTERNAL;
94 
95  /* Set plane description */
96  for (int i = 0; i < 4; i++) {
97  planes[i].firstSample = frame_ctx->frame->data[i];
98  planes[i].rowByteStride = frame_ctx->frame->linesize[i];
99  }
100 
101  /* Allocate LCEVC Picture */
102  res = LCEVC_AllocPictureExternal(lcevc->decoder, &desc, NULL, planes, picture);
103  if (res != LCEVC_Success) {
104  return AVERROR_EXTERNAL;
105  }
106  return 0;
107 }
108 
109 static int lcevc_send_frame(void *logctx, FFLCEVCFrame *frame_ctx, const AVFrame *in)
110 {
111  FFLCEVCContext *lcevc = frame_ctx->lcevc;
113  LCEVC_PictureHandle picture;
114  LCEVC_ReturnCode res;
115  int ret = 0;
116 
117  if (!sd)
118  return 1;
119 
120  res = LCEVC_SendDecoderEnhancementData(lcevc->decoder, in->pts, sd->data, sd->size);
121  if (res != LCEVC_Success)
122  return AVERROR_EXTERNAL;
123 
124  ret = alloc_base_frame(logctx, lcevc, in, &picture);
125  if (ret < 0)
126  return ret;
127 
128  res = LCEVC_SendDecoderBase(lcevc->decoder, in->pts, picture, -1, NULL);
129  if (res != LCEVC_Success) {
130  LCEVC_FreePicture(lcevc->decoder, picture);
131  return AVERROR_EXTERNAL;
132  }
133 
134  memset(&picture, 0, sizeof(picture));
135  ret = alloc_enhanced_frame(logctx, frame_ctx, &picture);
136  if (ret < 0)
137  return ret;
138 
139  res = LCEVC_SendDecoderPicture(lcevc->decoder, picture);
140  if (res != LCEVC_Success) {
141  LCEVC_FreePicture(lcevc->decoder, picture);
142  return AVERROR_EXTERNAL;
143  }
144 
145  return 0;
146 }
147 
148 static int generate_output(void *logctx, FFLCEVCFrame *frame_ctx, AVFrame *out)
149 {
150  FFLCEVCContext *lcevc = frame_ctx->lcevc;
151  LCEVC_PictureDesc desc;
152  LCEVC_DecodeInformation info;
153  LCEVC_PictureHandle picture;
154  LCEVC_ReturnCode res;
155 
156  res = LCEVC_ReceiveDecoderPicture(lcevc->decoder, &picture, &info);
157  if (res != LCEVC_Success)
158  return AVERROR_EXTERNAL;
159 
160  res = LCEVC_GetPictureDesc(lcevc->decoder, picture, &desc);
161  if (res != LCEVC_Success) {
162  LCEVC_FreePicture(lcevc->decoder, picture);
163  return AVERROR_EXTERNAL;
164  }
165 
166  out->crop_top = desc.cropTop;
167  out->crop_bottom = desc.cropBottom;
168  out->crop_left = desc.cropLeft;
169  out->crop_right = desc.cropRight;
170  out->sample_aspect_ratio.num = desc.sampleAspectRatioNum;
171  out->sample_aspect_ratio.den = desc.sampleAspectRatioDen;
172 
173  av_frame_copy_props(frame_ctx->frame, out);
175  av_frame_move_ref(out, frame_ctx->frame);
176 
177  out->width = desc.width + out->crop_left + out->crop_right;
178  out->height = desc.height + out->crop_top + out->crop_bottom;
179 
180  res = LCEVC_FreePicture(lcevc->decoder, picture);
181  if (res != LCEVC_Success)
182  return AVERROR_EXTERNAL;
183 
184  return 0;
185 }
186 
187 static int lcevc_receive_frame(void *logctx, FFLCEVCFrame *frame_ctx, AVFrame *out)
188 {
189  FFLCEVCContext *lcevc = frame_ctx->lcevc;
190  LCEVC_PictureHandle picture;
191  LCEVC_ReturnCode res;
192  int ret;
193 
194  ret = generate_output(logctx, frame_ctx, out);
195  if (ret < 0)
196  return ret;
197 
198  while (1) {
199  res = LCEVC_ReceiveDecoderBase (lcevc->decoder, &picture);
200  if (res != LCEVC_Success && res != LCEVC_Again)
201  return AVERROR_EXTERNAL;
202 
203  if (res == LCEVC_Again)
204  break;
205 
206  res = LCEVC_FreePicture(lcevc->decoder, picture);
207  if (res != LCEVC_Success)
208  return AVERROR_EXTERNAL;
209  }
210 
211  return 0;
212 }
213 
214 static void event_callback(LCEVC_DecoderHandle dec, LCEVC_Event event,
215  LCEVC_PictureHandle pic, const LCEVC_DecodeInformation *info,
216  const uint8_t *data, uint32_t size, void *logctx)
217 {
218  switch (event) {
219  case LCEVC_Log:
220  av_log(logctx, AV_LOG_INFO, "%s\n", data);
221  break;
222  default:
223  break;
224  }
225 }
226 
227 static void lcevc_free(AVRefStructOpaque unused, void *obj)
228 {
229  FFLCEVCContext *lcevc = obj;
230  if (lcevc->initialized)
231  LCEVC_DestroyDecoder(lcevc->decoder);
232  memset(lcevc, 0, sizeof(*lcevc));
233 }
234 
235 static int lcevc_init(FFLCEVCContext *lcevc, void *logctx)
236 {
237  LCEVC_AccelContextHandle dummy = { 0 };
238  const int32_t event = LCEVC_Log;
239 
240  if (LCEVC_CreateDecoder(&lcevc->decoder, dummy) != LCEVC_Success) {
241  av_log(logctx, AV_LOG_ERROR, "Failed to create LCEVC decoder\n");
242  return AVERROR_EXTERNAL;
243  }
244 
245  LCEVC_ConfigureDecoderInt(lcevc->decoder, "log_level", 4);
246  LCEVC_ConfigureDecoderIntArray(lcevc->decoder, "events", 1, &event);
247  LCEVC_SetDecoderEventCallback(lcevc->decoder, event_callback, logctx);
248 
249  if (LCEVC_InitializeDecoder(lcevc->decoder) != LCEVC_Success) {
250  av_log(logctx, AV_LOG_ERROR, "Failed to initialize LCEVC decoder\n");
251  LCEVC_DestroyDecoder(lcevc->decoder);
252  return AVERROR_EXTERNAL;
253  }
254 
255  lcevc->initialized = 1;
256 
257  return 0;
258 }
259 
260 int ff_lcevc_process(void *logctx, AVFrame *frame)
261 {
262  FrameDecodeData *fdd = frame->private_ref;
263  FFLCEVCFrame *frame_ctx = fdd->post_process_opaque;
264  FFLCEVCContext *lcevc = frame_ctx->lcevc;
265  int ret;
266 
267  if (!lcevc->initialized) {
268  ret = lcevc_init(lcevc, logctx);
269  if (ret < 0)
270  return ret;
271  }
272 
273  av_assert0(frame_ctx->frame);
274 
275 
276  ret = lcevc_send_frame(logctx, frame_ctx, frame);
277  if (ret)
278  return ret < 0 ? ret : 0;
279 
280  lcevc_receive_frame(logctx, frame_ctx, frame);
281  if (ret < 0)
282  return ret;
283 
285 
286  return 0;
287 }
288 
290 {
291  FFLCEVCContext *lcevc = NULL;
292  lcevc = av_refstruct_alloc_ext(sizeof(*lcevc), 0, NULL, lcevc_free);
293  if (!lcevc)
294  return AVERROR(ENOMEM);
295  *plcevc = lcevc;
296  return 0;
297 }
298 
299 void ff_lcevc_unref(void *opaque)
300 {
301  FFLCEVCFrame *lcevc = opaque;
302  av_refstruct_unref(&lcevc->lcevc);
303  av_frame_free(&lcevc->frame);
304  av_free(opaque);
305 }
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:659
ff_lcevc_alloc
int ff_lcevc_alloc(FFLCEVCContext **plcevc)
Definition: lcevcdec.c:289
AVRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
AV_VIDEO_MAX_PLANES
#define AV_VIDEO_MAX_PLANES
Maximum number of planes in any pixel format.
Definition: pixfmt.h:40
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
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:427
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:529
AVFrame::width
int width
Definition: frame.h:499
FFLCEVCContext
Definition: lcevcdec.h:31
data
const char data[16]
Definition: mxf.c:149
lcevc_init
static int lcevc_init(FFLCEVCContext *lcevc, void *logctx)
Definition: lcevcdec.c:235
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:539
FFLCEVCContext::initialized
int initialized
Definition: lcevcdec.h:33
FFLCEVCContext::decoder
LCEVC_DecoderHandle decoder
Definition: lcevcdec.h:32
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:448
dummy
int dummy
Definition: motion.c:64
ff_lcevc_process
int ff_lcevc_process(void *logctx, AVFrame *frame)
Definition: lcevcdec.c:260
refstruct.h
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
AVFrameSideData::size
size_t size
Definition: frame.h:285
FrameDecodeData::post_process_opaque
void * post_process_opaque
Definition: decode.h:45
info
MIPS optimizations info
Definition: mips.txt:2
lcevc_free
static void lcevc_free(AVRefStructOpaque unused, void *obj)
Definition: lcevcdec.c:227
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
av_refstruct_alloc_ext
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:94
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
map_format
static LCEVC_ColorFormat map_format(int format)
Definition: lcevcdec.c:29
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:599
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
ff_lcevc_unref
void ff_lcevc_unref(void *opaque)
Definition: lcevcdec.c:299
FFLCEVCFrame::lcevc
FFLCEVCContext * lcevc
Definition: lcevcdec.h:39
planes
static const struct @548 planes[]
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
alloc_base_frame
static int alloc_base_frame(void *logctx, FFLCEVCContext *lcevc, const AVFrame *frame, LCEVC_PictureHandle *picture)
Definition: lcevcdec.c:47
event_callback
static void event_callback(LCEVC_DecoderHandle dec, LCEVC_Event event, LCEVC_PictureHandle pic, const LCEVC_DecodeInformation *info, const uint8_t *data, uint32_t size, void *logctx)
Definition: lcevcdec.c:214
height
#define height
Definition: dsp.h:89
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
AVFrameSideData::data
uint8_t * data
Definition: frame.h:284
generate_output
static int generate_output(void *logctx, FFLCEVCFrame *frame_ctx, AVFrame *out)
Definition: lcevcdec.c:148
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:514
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:725
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
alloc_enhanced_frame
static int alloc_enhanced_frame(void *logctx, FFLCEVCFrame *frame_ctx, LCEVC_PictureHandle *picture)
Definition: lcevcdec.c:82
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
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:523
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
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:265
AVFrame::height
int height
Definition: frame.h:499
lcevc_send_frame
static int lcevc_send_frame(void *logctx, FFLCEVCFrame *frame_ctx, const AVFrame *in)
Definition: lcevcdec.c:109
desc
const char * desc
Definition: libsvtav1.c:78
mem.h
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:282
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
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:472
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
width
#define width
Definition: dsp.h:89
FFLCEVCFrame
Definition: lcevcdec.h:38
FFLCEVCFrame::frame
struct AVFrame * frame
Definition: lcevcdec.h:40
lcevc_receive_frame
static int lcevc_receive_frame(void *logctx, FFLCEVCFrame *frame_ctx, AVFrame *out)
Definition: lcevcdec.c:187