FFmpeg: libavcodec/apv_decode.c Source File
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 <stdatomic.h>
20
26
37
38
43 // The spec uses an extra element on the end of these arrays
44 // not corresponding to any tile.
48
52
55
59
61
65
68 { 0 }, // 4:2:0 is not valid.
72 };
73
75
78 {
80
83
85 if (bit_depth < 8 || bit_depth > 16 ||
bit_depth % 2) {
88 }
91
95 }
96
100 if (err < 0) {
101 // Unsupported frame size.
102 return err;
103 }
106
108
115
118
119 return 0;
120 }
121
125 };
126
128
130 {
132 }
133
135 {
137 int err;
138
140
142 if (err < 0)
143 return err;
144
149
150 // Extradata could be set here, but is ignored by the decoder.
151
154
156
157 return 0;
158 }
159
161 {
163
166 ff_cbs_fragment_reset(&apv->
au);
167 ff_cbs_flush(apv->
cbc);
168 }
169
171 {
173
174 ff_cbs_fragment_free(&apv->
au);
175 ff_cbs_close(&apv->
cbc);
176
177 return 0;
178 }
179
182 ptrdiff_t pitch,
186 int qp_shift,
187 const uint16_t *qmatrix)
188 {
190 int err;
191
193 memset(
coeff, 0, 64 *
sizeof(int16_t));
194
196 if (err < 0)
197 return err;
198
202
203 return 0;
204 }
205
207 int job, int thread)
208 {
213
214 int tile_index = job / apv_cbc->
num_comp;
215 int comp_index = job % apv_cbc->
num_comp;
216
219
220 int sub_w_shift = comp_index == 0 ? 0 : pix_fmt_desc->
log2_chroma_w;
221 int sub_h_shift = comp_index == 0 ? 0 : pix_fmt_desc->
log2_chroma_h;
222
224
225 int tile_y = tile_index /
tile_info->tile_cols;
226 int tile_x = tile_index %
tile_info->tile_cols;
227
228 int tile_start_x =
tile_info->col_starts[tile_x];
229 int tile_start_y =
tile_info->row_starts[tile_y];
230
231 int tile_width =
tile_info->col_starts[tile_x + 1] - tile_start_x;
232 int tile_height =
tile_info->row_starts[tile_y + 1] - tile_start_y;
233
236
237 int blk_mb_width = 2 >> sub_w_shift;
238 int blk_mb_height = 2 >> sub_h_shift;
239
241 int qp_shift;
243
245
249 .prev_dc = 0,
250 .prev_k_dc = 5,
251 .prev_k_level = 0,
252 };
253
254 int err;
255
257 tile->tile_header.tile_data_size[comp_index]);
258 if (err < 0)
260
261 // Combine the bitstream quantisation matrix with the qp scaling
262 // in advance. (Including qp_shift as well would overflow 16 bits.)
263 // Fix the row ordering at the same time.
264 {
265 static const uint8_t apv_level_scale[6] = { 40, 45, 51, 57, 64, 71 };
266 int qp =
tile->tile_header.tile_qp[comp_index];
268
270 qp_shift = qp / 6;
271
272 for (int y = 0; y < 8; y++) {
273 for (int x = 0; x < 8; x++)
275 input->frame_header.quantization_matrix.q_matrix[comp_index][x][y];
276 }
277 }
278
279 for (int mb_y = 0; mb_y < tile_mb_height; mb_y++) {
280 for (int mb_x = 0; mb_x < tile_mb_width; mb_x++) {
281 for (int blk_y = 0; blk_y < blk_mb_height; blk_y++) {
282 for (int blk_x = 0; blk_x < blk_mb_width; blk_x++) {
283 int frame_y = (tile_start_y +
286 int frame_x = (tile_start_x +
289
292 frame_y * frame_pitch + 2 * frame_x;
293
295 block_start, frame_pitch,
296 &gbc, &entropy_state,
298 qp_shift,
299 qmatrix_scaled);
300 if (err < 0) {
301 // Error in block decode means entropy desync,
302 // so this is not recoverable.
304 }
305 }
306 }
307 }
308 }
309
311 "Decoded tile %d component %d: %dx%d MBs starting at (%d,%d)\n",
312 tile_index, comp_index, tile_mb_width, tile_mb_height,
313 tile_start_x, tile_start_y);
314
315 return 0;
316
319 "Decode error in tile %d component %d.\n",
320 tile_index, comp_index);
322 return err;
323 }
324
327 {
331
332 start_mb = 0;
333 for (
i = 0; start_mb < frame_width_in_mbs;
i++) {
336 }
339
340 start_mb = 0;
341 for (
i = 0; start_mb < frame_height_in_mbs;
i++) {
344 }
347
349 }
350
353 {
357 int err, job_count;
358
360 if (err < 0) {
362 return err;
363 }
364
366 return 0;
367
370
372 if (err < 0)
373 return err;
374
377
379
380 // Each component within a tile is independent of every other,
381 // so we can decode all in parallel.
383
386
388 if (err > 0) {
390 "Decode errors in %d tile components.\n", err);
392 // Output the frame anyway.
394 } else {
396 }
397 }
398
399 return 0;
400 }
401
404 {
405 int err;
406
407 for (
int i = 0;
i <
md->metadata_count;
i++) {
409
412 {
415
417 if (err < 0)
418 return err;
419
420 if (mdm) {
421 for (int j = 0; j < 3; j++) {
426 }
427
432
437
440 }
441 }
442 break;
444 {
447
449 if (err < 0)
450 return err;
451
452 if (clm) {
455 }
456 }
457 break;
458 default:
459 // Ignore other types of metadata.
460 break;
461 }
462 }
463
464 return 0;
465 }
466
468 {
472
473 for (
i = apv->
nb_unit; i < au->nb_units;
i++) {
475
480 goto end;
483 break;
491 "Stream contains additional non-primary frames "
492 "which will be ignored by the decoder.\n");
494 }
495 break;
498 // Not relevant to the decoder.
499 break;
500 default:
504 "Stream contains PBUs with unknown types "
505 "which will be ignored by the decoder.\n");
507 }
508 break;
509 }
510 }
511
513 end:
516
519 ff_cbs_fragment_reset(&apv->
au);
521 }
522 if (!err && !
frame->buf[0])
524
525 return err;
526 }
527
529 {
531 int err;
532
533 do {
536 if (err < 0)
537 return err;
538
539 err = ff_cbs_read_packet(apv->
cbc, &apv->
au, apv->
pkt);
540 if (err < 0) {
541 ff_cbs_fragment_reset(&apv->
au);
544 return err;
545 }
546
550 }
551
553 }
while (err ==
AVERROR(EAGAIN));
554
555 return err;
556 }
557
572 };
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel 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 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
void * priv_data
Internal codec-specific data.
enum AVColorSpace colorspace
YUV colorspace type.
@ APV_PBU_NON_PRIMARY_FRAME
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
static int apv_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
void * content
Pointer to the decomposed form of this unit.
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
unsigned MaxCLL
Max content light level (cd/m^2).
This structure describes decoded (raw) audio or video data.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
@ AVCOL_RANGE_JPEG
Full range content.
const static int level_scale[2][6]
static int apv_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Context structure for coded bitstream operations.
static int apv_decode_block(AVCodecContext *avctx, void *output, ptrdiff_t pitch, GetBitContext *gbc, APVEntropyState *entropy_state, int bit_depth, int qp_shift, const uint16_t *qmatrix)
#define AV_LOG_VERBOSE
Detailed information.
CodedBitstreamUnitType type
Codec-specific type of this unit.
uint32_t tile_width_in_mbs
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
APVDerivedTileInfo tile_info
Coded bitstream unit structure.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
static av_cold void close(AVCodecParserContext *s)
int is_copy
When using frame-threaded decoding, this field is set for the first worker thread (e....
#define AV_CODEC_FLAG_OUTPUT_CORRUPT
Output even those frames that might be corrupted.
AVCodec p
The public AVCodec.
static AVOnce apv_entropy_once
static int FUNC() tile_info(CodedBitstreamContext *ctx, RWContext *rw, APVRawTileInfo *current, const APVRawFrameHeader *fh)
enum AVDiscard skip_frame
Skip decoding for selected frames.
#define AV_PIX_FMT_YUVA444P16
static av_cold int apv_decode_init(AVCodecContext *avctx)
int refs
number of reference frames
static av_cold void apv_entropy_build_decode_lut(void)
int flags
AV_CODEC_FLAG_*.
#define AV_PIX_FMT_GRAY16
static int apv_decode_tile_component(AVCodecContext *avctx, void *data, int job, int thread)
static APVVLCLUT decode_lut
#define AV_PIX_FMT_YUV444P10
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
static int ff_thread_once(char *control, void(*routine)(void))
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
#define AV_PIX_FMT_YUV422P16
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static int apv_decode(AVCodecContext *avctx, AVFrame *output, APVRawFrame *input)
int has_b_frames
Size of the frame reordering buffer in the decoder.
Coded bitstream fragment structure, combining one or more units.
@ APV_PBU_ACCESS_UNIT_INFORMATION
#define AV_PIX_FMT_YUV444P16
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
uint32_t tile_height_in_mbs
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
#define AV_PIX_FMT_YUVA444P12
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
CodedBitstreamFragment au
#define AV_PIX_FMT_GRAY14
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
static av_cold void apv_decode_flush(AVCodecContext *avctx)
#define CODEC_LONG_NAME(str)
int ff_decode_mastering_display_new(const AVCodecContext *avctx, AVFrame *frame, AVMasteringDisplayMetadata **mdm)
Wrapper around av_mastering_display_metadata_create_side_data(), which rejects side data overridden b...
#define AV_PIX_FMT_GRAY10
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
@ AVDISCARD_ALL
discard all
#define LOCAL_ALIGNED_32(t, v,...)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Rational number (pair of numerator and denominator).
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
struct AVCodecInternal * internal
Private context used for internal data.
void(* flush)(AVBSFContext *ctx)
#define AV_PIX_FMT_YUV422P10
void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut)
Build the decoder VLC look-up tables.
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
static void apv_derive_tile_info(APVDerivedTileInfo *ti, const APVRawFrameHeader *fh)
int level
Encoding level descriptor.
#define atomic_load_explicit(object, order)
int(* init)(AVBSFContext *ctx)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
static int apv_decode_check_format(AVCodecContext *avctx, const APVRawFrameHeader *header)
#define AV_PIX_FMT_YUV422P12
#define atomic_fetch_add_explicit(object, operand, order)
static AVRational av_make_q(int num, int den)
Create an AVRational.
av_cold void ff_apv_dsp_init(APVDSPContext *dsp)
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
#define AV_PIX_FMT_YUV444P12
static const uint8_t header[24]
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AV_PIX_FMT_YUVA444P10
uint8_t warned_unknown_pbu_types
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
uint16_t row_starts[APV_MAX_TILE_ROWS+1]
#define i(width, name, range_min, range_max)
#define AV_FRAME_FLAG_CORRUPT
The frame data may be corrupted, e.g.
static av_cold int apv_decode_close(AVCodecContext *avctx)
AVPacket * in_pkt
This packet is used to hold the packet given to decoders implementing the .decode API; it is unused b...
static enum AVPixelFormat apv_format_table[5][5]
#define atomic_store_explicit(object, desired, order)
const char * name
Name of the codec implementation.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
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
const FFCodec ff_apv_decoder
static int FUNC() tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
int ff_decode_content_light_new(const AVCodecContext *avctx, AVFrame *frame, AVContentLightMetadata **clm)
Wrapper around av_content_light_metadata_create_side_data(), which rejects side data overridden by th...
#define AV_PIX_FMT_YUV422P14
main external API structure.
#define FF_CODEC_RECEIVE_FRAME_CB(func)
uint16_t col_starts[APV_MAX_TILE_COLS+1]
uint8_t warned_additional_frames
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
static const CodedBitstreamUnitType apv_decompose_unit_types[]
#define avpriv_request_sample(...)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
CodedBitstreamContext * cbc
unsigned MaxFALL
Max average light level per frame (cd/m^2).
This structure stores compressed data.
int nb_decompose_unit_types
Length of the decompose_unit_types array.
const CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
int width
picture width / height.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
static const double coeff[2][5]
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int apv_decode_metadata(AVCodecContext *avctx, AVFrame *frame, const APVRawMetadata *md)
#define AV_PIX_FMT_YUV444P14
int ff_apv_entropy_decode_block(int16_t *restrict coeff, GetBitContext *restrict gbc, APVEntropyState *restrict state)
Entropy decode a single 8x8 block to coefficients.
#define atomic_init(obj, value)
#define AV_PIX_FMT_GRAY12
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
int nb_units
Number of units in this fragment.
void(* decode_transquant)(void *output, ptrdiff_t pitch, const int16_t *input, const int16_t *qmatrix, int bit_depth, int qp_shift)
Generated on Wed Nov 19 2025 19:21:47 for FFmpeg by
doxygen
1.8.17