Commit graph

250 commits

Author SHA1 Message Date
Sushant
f308c61af0 avcenc: cleanup for thread pool memory handling
Test: avcenc -c enc.cfg
Change-Id: I5f5cf7f705d389f6938fc641daa558bedfbc573c
2025-05-23 14:59:56 +05:30
Sushant
c2714ee61c avcenc: Fixed thread handle parameter
Test: avcenc -c enc.cfg
Change-Id: I5f5cf7f705d389f6938fc641daa558bedfbc462b
2025-05-19 20:37:53 +05:30
Sushant
5e9c764903 avcenc: Replacing KEEP_THREADS_ACTIVE with runtime check
Test: avcenc -c enc.cfg
Change-Id: If6c8c29d2b5b1322796b8b25dc74231d13b61243
2025-04-22 17:09:57 +05:30
Sushant
f17635d2eb avcenc: moving mutex and cond variable init and destruction
Change-Id: I7123f43b5f3a85355b43370ae3df1930ebe5b570
2025-04-21 18:54:01 +05:30
Sushant
cdbee17b6e avcenc: add thread pool shutdown on last frame with KEEP_THREADS_ACTIVE
Test: avcenc -c enc.cfg
Change-Id: I85c9b535e2b81bf7f07a80bd7038d4f0429eecfe
2025-04-21 18:54:01 +05:30
Sushant
ee0c417a64 avcenc: add support for KEEP_THREADS_ACTIVE
Currently avc encoder creates desired number of threads at
the start of every frame and joins after frame is processed.
This change modifies the thread creation part. Now the threads
are created at the start of the sequence. Kept alive through
out the sequence and joined at the end of the sequence. This
helps in reduction of thread creation and deletion overhead.

This change does not effect the encoded bitstream. That is,
encoded output with this change is same as encoded output
without this change.

Bug: 288998933
Test: avcenc -c enc.cfg
Change-Id: I98784169052a5f05c109aaf1de97a5e46d7a773d
2025-03-20 07:11:55 -07:00
Suyog Pawar
266cda3c3b libavc : Enable support for MacOS
Test: ./avcenc
      ./avcdec

Change-Id: If03196cf979d7f6638b99d5b13afba6df3364178
2024-10-04 06:51:30 -07:00
Srujan Vandrangi
0d29250b99 libavcenc: fix build issues on android
Test: Build
2023-12-19 20:45:31 -08:00
Ashwin Natesan
63b8be0524 svcenc: Integer overflow in irc_ba_get_cur_frm_est_texture_bits
'isvce_svc_rc_params_validate' was not being invoked prior to
call to 'isvce_rc_init'. This resulted in an erroneous state
within RC's context wherein the instantaneous estimate for the
texture bits for the frame being processed exceeded INT_MAX.
'isvce_svc_rc_params_validate' has code that detects such a
state and is now being correctly invoked where apprpriate.

Bug = ossfuzz:63175
Test: svc_enc_fuzzer
2023-12-13 07:40:47 -08:00
Ram Mohan M
0e72348961 libavcenc: validate dimensions after applying config params
Before applying the config params (dimensions), the api is validating
the input buffer dimensions. This causes encode fails wrongly. This is
corrected.
2023-11-20 22:40:30 -08:00
Ashwin Natesan
51268b3a71 svcenc: Redundant code removed
The following lines of code have been removed to improve coverage -
  [x] Functions -
      - isvc_interleaved_copy
      - isvc_16bit_interleaved_copy
      - isvc_16bit_interleaved_memset
      - isvc_iquant_itrans_recon_chroma_4x4_neon
      - isvc_iquant_itrans_recon_chroma_4x4_sse42
      - isvc_iquant_itrans_recon_4x4_dc_with_res_output_neon
      - isvc_iquant_itrans_recon_res_dc_4x4_sse42
      - isvc_iquant_itrans_recon_4x4_dc_with_res_accumulate_neon
      - isvc_iquant_itrans_recon_res_dc_with_res_acc_4x4_sse42
      - isvce_wait_for_thread
  [x] Function pointer initialisations for the functions above

Test: svc_enc_fuzzer
2023-10-27 06:14:32 -07:00
Ram Mohan
196f0dbaec libavcenc: use number_t while computing number of bits in period
For large intra frame intervals or for a choice of bit rate that causes
large bits per frame, it possible that while computing number of bits
available in the gop overflow the datatype bounds. For these
calculations use number_t as this offers wider space

Bug: oss-fuzz-63044
Test: avc_enc_fuzzer

Change-Id: Ifee9130f0021f77905aea67832abc044537088e5
2023-10-18 06:55:59 -07:00
Ashwin Natesan
ea694873e0 svcenc: Enabled and verified 420sp inputs
Test: svc_enc_fuzzer
2023-10-10 07:07:21 -07:00
Ashwin Natesan
b5a861581e svcenc: RC parameters verified before RC init
RC uses int32_t to store bitrates and other
parameters internally. For specific magnitudes of
bitrate, framerate, and GOP period, this can
result in signed integer overflow. This is now
detected before calls to RC init.

Note that calls to the 'ISVCE_CMD_CTL_SET_BITRATE' API can also
result in this behaviour but it will be appropriately handled
by 'isvce_svc_frame_params_validate'.

Bug = ossfuzz:63053
Test: svc_enc_fuzzer
2023-10-09 06:50:02 -07:00
Ram Mohan M
a7b6206fab libavcenc: Sync default values of library and application
I and IDR interval default values do not comply with requirements
of the codec. This is fixed. Also corrected few other default values.
2023-10-04 13:51:14 -07:00
Harish Mahendrakar
f9f0475b63 Revert "libavcenc: Correct default values in ih264e_defs.h"
This reverts commit 7f0c8a545b.

Reason: This changes behavior w.r.t. frame rates in terms of units.
2023-10-04 23:38:08 +05:30
Ram Mohan M
7f0c8a545b libavcenc: Correct default values in ih264e_defs.h
I and IDR interval default values do not comply with requirements
of the codec. This is fixed. Also corrected few other default values.
2023-10-03 14:24:25 -07:00
Ram Mohan M
0c9dc5e7b2 libavcenc: use signed var for integer operations 2023-10-03 14:17:51 -07:00
Ram Mohan M
e4574d306f libavcenc: increment picture count only on valid inputs
If the encoder receives inputs that are empty or frames
that have to be skipped due to frame rate pull down,
do not increment picture count.

Bug: oss-fuzz-62827
Test: ./avc-enc-fuzzer
2023-10-03 14:17:51 -07:00
Ashwin Natesan
56a598ca6f svcenc: Fixes for incorrect handling of entropy coding errors
There were cases where errors in a process thread
executing the last MB row would result in the entropy
job list without a terminnating entry, which would
result in a call to ih264_list_dequeue blocking infinitely.
The enum 'ISVCE_ENTROPY_THREAD_STATES_T' is now used to
monitor the eecution state of the entropy thread and handle
the situation described appropriately.

Bug = ossfuzz:62786
Test: svc_enc_fuzzer
2023-10-02 07:09:21 -07:00
Ram Mohan M
2e128ba913 libavcenc: Modified conversion tables between Qstep & QP
avc-qp(QP) scale is logarithmic and Qstep scale is linear.
The translation between log to linear is carried during
rate control modelling.

Look up tables for these translations are modified as per
the literature
2023-10-01 01:37:17 +05:30
Ram Mohan M
31fc8edb9c libavcenc: Correct Lagrange parameter for I, P & B slices
The Lambda tables for intra and inter(P) slices are slightly
modified. Lambda tables for B slices is completely replaced
wih new set.

This change is done in accordance with JVT-O079.doc
2023-10-01 01:37:17 +05:30
Ram Mohan M
50fd862b8d libavcenc: Remove duplicate code and improve readability
This commit does not introduce any new functionality w.r.t previous
commit. But it fixes few things. They are listed below:
1. Guard Bands in header files are fixed
2. Header files contains function definition comments. These are same
as in source file. Maintaining same comment at two locations is
unnecessary. These are removed.
3. Improved consistency and code indentation
4. Removed comments that dont align with implementation
5. Grouped headers of a workspace together
2023-10-01 01:37:17 +05:30
Ram Mohan M
f0b559e4dc libavcenc: forward current frame intra cost to rc accurately 2023-10-01 01:37:17 +05:30
Ram Mohan
c68f552fce libavcenc: signal header bits consumed count accurately to rc 2023-10-01 01:37:17 +05:30
Ram Mohan
7429c17294 libavcenc: reset mb qp delta ctxt during cabac encoding
When mb qp delta is not signalled due to cbp being zero,
mb qp delta ctxt is not being reset. This is causing bitstream
decode problems when mb qp is enabled
2023-09-29 01:34:57 +05:30
Ram Mohan
d9ef5790ef libavcenc: fix computations that are resulting in overflow
Intermediate precision for few computations are exceeding int max.
This is addressed
2023-09-29 01:34:57 +05:30
Ram Mohan
f1f3a33bc4 libavcenc: fix the order of arguments passed to get_min_level 2023-09-28 07:24:09 -07:00
Ram Mohan
eb20999248 libavcenc: Cleanup air Mode and refresh period config options
Allow any refresh period if air mode is selected as none
2023-09-28 07:24:09 -07:00
Hamsalekha S
78dddbc900 libavcenc: Added more validity checks for IDR_interval and I_interval
When the i frame interval is larger than idr frame interval, bitrate
deviations are seen in bitstream.

Forced idr_interval to be a multiple of I frame interval.
2023-09-28 07:24:09 -07:00
Hamsalekha S
a24d3cf61f libavcenc: Fixed bug in the case of IDR frame being skipped.
When IDR frame is skipped due to VBV constraints, the frame
number should not be reset. Since it was reset earlier, it
was leading to bistream being not decodable in such a case.
Restore frame_num during idr skip.
2023-09-28 07:24:09 -07:00
Ram Mohan M
7c59eeb920 libavcenc: model CBR-NLDRC as CBR mode with out frame drops
In frame drop mode, the stream generated is non-compliant.
For now disable frame drop mode.

Bug: oss-fuzz-61202
Bug: oss-fuzz-59598
Test: ./avc_enc_fuzzer

Change-Id: Ifed950082ae2c6e70d6a7da4547b6ffeb44cf759
2023-09-27 13:57:19 -07:00
Ram Mohan M
3fcf959359 libavcenc: relocate the update post enc routines
During bitstream overflow errors, it is possible that update post
enc gets skipped as last row might never have to be entropy coded
as we have already consumed the buffer. This can be fatal for
encoding further frames. Move this update to a location where it
is guaranteed to be updated

Bug: oss-fuzz-62378
Test: ./avc_enc_fuzzer

Change-Id: I279deb1f30c15c057c0b6e909834313a1df4fdac
2023-09-27 13:57:19 -07:00
Ram Mohan
2ff6f15376 libavcenc: return partially encoded buffer for application
As bitstream overflow errors are not marked as fatal errors and
these recons are being used for reference, its best to return
the partial encoded buffers for application

Change-Id: I30a8e9907cf42d3bc883ee78b28cbae723bad7ac
2023-09-27 13:57:19 -07:00
Ram Mohan
29f519228a libavcenc: update bitstream context state only in successful writes
During entropy error, even though the bitstream buffer is not written
with additional bytes, the bitstream context state is getting updated.
As the number of bits left in current word is updated, the get_num_bits
used for computing header and texture bits can become negative causing
overflow errors

Change-Id: I2f990071a9935b2ee328dbd3915dfbefccbab4c5
2023-09-27 13:57:19 -07:00
Ram Mohan
30858f49ad libavcenc: do not reset status before all threads are made aware
At the end of encoding of a frame, the entropy thread communicates
the encoded bit stream size to rc module for update. After this
update, if rc decides to skip the frame due to vbv overflow, the
bitstream context is reset and frame is marked for skip.

Due to an oversight, if entropy encoding sees an error, then this
update is happening at the end of each row. Now rc has decided to
skip the frame and the context is reset. As the bitstream context is
reset, other threads are unaware of this problem and continue encoding.

This is causing issues.

Restrict the rc update to the thread that entropy code the last row.

Bug: oss-fuzz:59543
Bug: 285891354
Test: avc_enc_fuzzer

Change-Id: If45a5f34abb59ece812733af8f54f72ae5474d03
2023-09-13 20:38:10 -07:00
Ashwin Natesan
c759935aad svcenc: Handling frames with few MB's in In-frame RC
A 'consumption ratio' is computed fr every MB when
in-frame RC is enabled. This computation can result in
divide-by-zeros in certain circumstances. Such cases
are now appropriately handled.

Bug: ossFuzz:60828
Test: svc_enc_fuzzer
2023-07-25 08:06:32 -07:00
Aayush Soni
bbf593f216 libavcenc: fix null buffer dereferencing
During encoding, if encoder encounters an error, the library returns the
same. If the application ignores it and feeds further input, library
while processing the new input is facing a null dereference.

1. Added a error check in encode API call before processing input
2. Made entropy bitstream buffer overflow errors non-fatal.

Bug: 187499509
Test: POC in bug descriptions
      atest VtsHalMediaC2V1_0TargetVideoEncTest
      atest -c CtsMediaV2TestCases:CodecEncoderTest
2023-06-01 22:39:26 -07:00
Srujan Vandrangi
7acfe28555 encoder: Add checks for ME search algorithm
Bug: oss-fuzz:57331
Test: avc_enc_fuzzer
2023-04-04 22:33:36 -07:00
Harish Mahendrakar
ba34616a11 encoder: Initialize s_inp_buf and s_out_buf to zero in ih264e_encode
In some cases, s_inp_buf and s_out_buf on stack in ih264e_encode()
can be accessed unininitialized. This is fixed by initializing these
two structures.

Bug: oss-fuzz:57333
Bug: 274906999
Test: avc_enc_fuzzer
2023-04-04 21:48:18 +05:30
Ashwin Natesan
5beaa86db4 svcenc: nullSan errors in ILPMv fixed
The ILP MV struct pointer will be set to NULL for I slices and
for spatial layer ID 0. A NULL check ought to be used in all places
that access this pointer. This was missing in 2 places and has been
added.

Test: svc_enc_fuzzer
2023-03-21 10:17:44 -07:00
Ashwin Natesan
c5f3b0c976 svcenc: RC bitrate overflows prevented
RC uses int32_t for storing multiple quantities related to
bits in a given period.
'isvce_svc_frame_params_validate' has been added, which queries
RC API for the relevant quantities and returns with and error
if computations involving those quantities exceed INT32_MAX.

Bug: 274221347
Bug: 274265498
Test: svc_enc_fuzzer
2023-03-19 23:02:06 -07:00
Suyog Pawar
1806721a4d avcenc: Fix out of bound read in initialize process context
Using incorrect stride during copy from source buffer to intermediate buffer. This is corrected

Bug: 242379731
Bug: 242386193
Test: avcenc -c enc.cfg

Change-Id: I307525906ee6f6df5d8e114ebbafa40d6442662d
(cherry picked from commit 0a87a34fccffe15c2cc80670cf1058e7a3313c6a)
Merged-In: I307525906ee6f6df5d8e114ebbafa40d6442662d
2023-03-17 10:22:38 -07:00
Ashwin Natesan
ab835f3240 svcenc: fixed incorrect EPB checks
If 'u4_strm_buf_offset < 4' when 'isvce_cabac_flush' is called,
then EPB could not have been inserted into the stream buffer.

BUG = ossfuzz:56816
Test: svc_enc_fuzzer
2023-03-09 06:51:21 -08:00
Harish Mahendrakar
7164209291 Updates to Android.bp and some formatting fixes
- Only libavcdec is marked as available to apex modules instead
  of marking all decoder libraries to be available to apex modules.
- some formatting changes for consistency with neighboring lines.

Test: Builds
2023-03-08 20:27:18 -08:00
Ashwin Natesan
ed00dfc325 svcenc: fixed a bug in low bitrate encodes
Certain ILP buffers are reset when encoded bitrate exceeds
configured bitrate. The size used for one of them was incorrect.
2023-02-20 08:03:08 -08:00
Ashwin Natesan
c38af025ab svcenc: fixed incorrect streamBuf accesses
If 'u4_strm_buf_offset == 0' when 'isvce_cabac_flush' is called,
then 'carry' is implicitly 0.

'u4_strm_buf_offset == 0' implies stream buffer is empty.

Invocation of 'isvce_cabac_flush' implies all MB's in a slice
have been encoded.

BUG = ossfuzz:56137
Test: svc_enc_fuzzer
2023-02-20 08:02:26 -08:00
Mallikarjun Kamble
7f19ac2206 Remove printf's from sei
These printf's were inserted for debugging
2023-02-14 22:00:13 -08:00
Mallikarjun Kamble
8e41a50de4 libavc: Added support for SII SEI message
- Add SII flag and SII parameters for the encoder and decoder.
- Encoder: Added support for SII SEI
- Decoder: Added support for SII SEI parsing and exporting
2023-02-14 22:00:13 -08:00
Ashwin Natesan
47125f49a4 svcenc: increased minimum outbuf size
For certain configurations of the fuzzer input, the minimum
output buffer size is insufficient.
The minimum value has been doubled in this commit.

BUG=oss-fuzz:55797
Test: svc_enc_fuzzer
2023-02-13 10:06:08 -08:00