Commit graph

605 commits

Author SHA1 Message Date
Jun Zhao
c51a420b59 doc/examples/hw_decode: check fopen return value for output file
The output file fopen() result is not checked. If it fails (e.g.
permission denied or invalid path), output_file is NULL and the
subsequent fwrite() call will crash.

Add a NULL check with an error message, consistent with the
existing error handling pattern in this example.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
c29206e456 doc/examples/transcode: query encoder for supported configs
avcodec_get_supported_config() is called with dec_ctx (the decoder
context) to query supported pixel formats and sample formats, but
the intent is to configure the encoder. The decoder supported
format list may differ from the encoder, leading to format
negotiation failures or incorrect output.

Pass enc_ctx instead so the actual encoder capabilities are
queried.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
d25d6b991d doc/examples/vaapi_encode: return raw error codes from encode_write
encode_write() mapped all return values from avcodec_receive_packet()
into 0 or -1, which destroyed the AVERROR_EOF signal needed by the
caller. The flush call in main() could never see AVERROR_EOF, so a
successful encode always exited with a non-zero status.

Let encode_write() return the original error code and have each
call site handle the expected status:

  - Encoding loop: ignore AVERROR(EAGAIN) (need more input)
  - Flush path:    ignore AVERROR_EOF (normal end-of-stream)

This makes the control flow explicit and easier to follow for
anyone reading the example.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
8d80b13cbe doc/examples/remux: fix NULL pointer dereference in cleanup
The cleanup path uses `ofmt->flags` to check AVFMT_NOFILE, but
`ofmt` is only assigned after avformat_alloc_output_context2
succeeds. If a failure occurs between output context allocation
and the `ofmt` assignment (e.g. stream_mapping allocation fails),
ofmt_ctx is non-NULL while ofmt is still NULL, causing a crash.

Use ofmt_ctx->oformat->flags instead, which is always valid when
ofmt_ctx is non-NULL.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
68e18d3a1c doc/examples/encode_audio: fix hardcoded stereo sample stride
The sample generation loop hardcodes a stride of 2 (stereo) with
samples[2*j], but the channel count is dynamically selected by
select_channel_layout() which picks the layout with the highest
channel count. If the encoder supports more than 2 channels,
samples will be written at wrong offsets.

Use c->ch_layout.nb_channels as the stride instead.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
042ff80562 doc/examples/qsv_decode: fix raw frame dump for chroma planes
The output loop used sw_frame->width as the write size for all
planes. This is only correct for NV12 where the interleaved UV
plane happens to have the same byte width as the Y plane. For
other pixel formats (e.g. YUV420P where U/V planes are half
width, or P010 where samples are 2 bytes), the output would be
corrupted.

Use av_image_get_linesize() to compute the correct byte width
for each plane based on the actual pixel format.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
2c47383d74 doc/examples/hw_decode: fix fwrite error check
fwrite() returns size_t (unsigned), so comparing its return value
with < 0 is always false and write errors are silently ignored.
Check against the expected byte count instead.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
1cf02df122 doc/examples/decode_video: check fopen return value in pgm_save
pgm_save() passes the FILE pointer from fopen() directly to
fprintf() and fwrite() without a NULL check. If fopen() fails
(e.g. permission denied or disk full), this causes a NULL pointer
dereference and crash.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
752faebaa6 doc/examples/vaapi_encode: open raw YUV input in binary mode
fopen() with "r" opens the file in text mode, which on Windows
translates \r\n to \n, corrupting raw NV12 pixel data. Use "rb"
to open in binary mode, matching the output file which already
uses "w+b".

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
Jun Zhao
40f085ac3d doc/examples: fix output context cleanup in transcode examples
avformat_close_input() is designed for input format contexts only.
Using it on output contexts is API misuse — it accesses iformat
(which is NULL for output contexts) and does not follow the correct
output cleanup path.

Replace with the proper pattern already used in remux.c and
transcode.c: avio_closep() to close the IO handle, followed by
avformat_free_context() to free the format context.

Signed-off-by: Jun Zhao <barryjzhao@tencent.com>
2026-03-18 02:08:09 +00:00
0xBat
f59968720c
doc/examples/extract_mvs: fix memory leak in codec context initialization
Ensure the allocated AVCodecContext is properly freed if avcodec_parameters_to_context fails.

Signed-off-by: 0xBat <monsterbat02@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2026-01-09 23:45:07 +01:00
0xBat
786e245e80
doc/examples/qsv_transcode: check for allocation failure
Validate return value of av_malloc for dynamic_setting to avoid null pointer dereference.

Signed-off-by: 0xBat <monsterbat02@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2026-01-09 23:44:59 +01:00
0xBat
3b0409dd13
doc/examples/hw_decode: fix memory leak in codec context initialization
Properly free decoder_ctx on failure to prevent a memory leak during initialization.

Signed-off-by: 0xBat <monsterbat02@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2026-01-09 23:44:58 +01:00
Zhao Zhili
09856e4e48 doc/examples/vaapi_encode: fix invalid check on fwrite
enc_pkt->size is 0 after av_packet_unref, which makes the check invalid.

Fix regression from 3e4bfff2.

Co-Authored-by: Jin Bo <jinbo@loongson.cn>
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2025-09-08 20:43:07 +00:00
Kacper Michajłow
9b94b652db doc/examples/decode_filter_video: use av_usleep
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-08-11 19:29:53 +00:00
Kacper Michajłow
5a768e8dd0 doc/examples/decode_filter_audio: remove unused unistd.h include
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
2025-08-11 19:29:53 +00:00
Jiasheng Jiang
c64cff64a2
examples: Add check and replace av_free() to avoid potential memory errors
Add check for the return value of av_packet_alloc() to avoid potential NULL pointer dereference.
Moreover, replace redundant av_free() with fprintf().

Fixes: 9a38184a14 ("examples/decode_audio: allocate the packet dynamically")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-08-07 03:07:45 +02:00
Jiasheng Jiang
39592f2bab
examples: Add proper deallocations to avoid potential memory leaks
Add proper deallocations in the error paths to avoid potential memory leaks.

Fixes: e7dfaf16a4 ("libavfilter: example audio filtering program")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-08-07 03:07:44 +02:00
Jiasheng Jiang
9ca58424de
examples: Add av_freep to avoid potential memory leak
Add av_freep() to free avio_ctx_buffer if avio_alloc_context fails
to avoid potential memory leak.

Fixes: 5fc4dea39c ("examples: add avio_reading.c example")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2025-08-04 01:29:44 +02:00
Timo Rothenpieler
262d41c804 all: fix typos found by codespell 2025-08-03 13:48:47 +02:00
Tristan Matthews
9b9a287872 doc/examples/qsv_decode: use av_err2str
Signed-off-by: Marvin Scholz <epirat07@gmail.com>
2025-05-22 22:26:30 +02:00
Tristan Matthews
e93a43b511 doc/examples/filter_audio: use av_err2str
Signed-off-by: Marvin Scholz <epirat07@gmail.com>
2025-05-22 22:26:30 +02:00
Anton Khirnov
b150fdd500 doc/examples/decode_filter_video: switch to new buffersink options 2024-09-30 09:10:20 +02:00
Anton Khirnov
0dbd59242a doc/examples/decode_filter_audio: switch to new buffersink options
Use a mix of av_opt_set() and av_opt_set_array() to demonstrate
different ways the options can be set.
2024-09-30 09:10:20 +02:00
Anton Khirnov
f6f0e05815 doc/examples/transcode: handle audio encoder frame size restrictions 2024-09-28 17:04:33 +02:00
Anton Khirnov
cde307c783 doc/examples/transcode: switch to avcodec_get_supported_config() 2024-09-28 17:04:33 +02:00
Anton Khirnov
d18d119b8f doc/examples/transcode: stop using avfilter_graph_create_filter() incorrectly
See previous commits for details.
2024-09-28 17:04:33 +02:00
Anton Khirnov
87faa8aeba doc/examples/decode_filter_video: stop using avfilter_graph_create_filter() incorrectly
See previous commits for details.
2024-09-28 17:04:33 +02:00
Anton Khirnov
27564f9020 doc/examples/decode_filter_audio: stop using avfilter_graph_create_filter() incorrectly
See previous commit for details.
2024-09-28 17:04:33 +02:00
Michael Niedermayer
e6c0c5731e
doc/examples/mux: remove nop
Found through code review related to CID1604493 Overflowed constant

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-07-12 16:40:51 +02:00
Michael Niedermayer
3e4bfff211
doc/examples/vaapi_encode: Try to check fwrite() for failure
Fixes: CID1604548 Unused value

Sponsored-by: Sovereign Tech Fund
Reviewed-by: "Xiang, Haihao" <haihao.xiang-at-intel.com@ffmpeg.org>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-07-10 18:10:07 +02:00
Michael Niedermayer
91d27f7e02
doc/examples/demux_decode: Simplify loop
Fixes: CID1463550 Logically dead code

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-05-09 16:17:39 +02:00
Michael Niedermayer
cae0f2bc55
doc/examples/qsv_transcode: Initialize pointer before free
Fixees: CID1517023 Uninitialized pointer read

Sponsored-by: Sovereign Tech Fund
Reviewed-by: "Xiang, Haihao" <haihao.xiang@intel.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-04-25 03:24:59 +02:00
Michael Niedermayer
191950d1bf
doc/examples/qsv_transcode: Simplify str_to_dict() loop
Fixes: CID1517022 Logically dead code

Sponsored-by: Sovereign Tech Fund
Reviewed-by: "Xiang, Haihao" <haihao.xiang@intel.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-04-25 03:24:59 +02:00
Michael Niedermayer
c9c11a0866
doc/examples/vaapi_transcode: Simplify loop
Fixes: CID1428858(1/2) Logically dead code

Sponsored-by: Sovereign Tech Fund
Reviewed-by: "mypopy@gmail.com" <mypopy@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-04-25 03:24:58 +02:00
Michael Niedermayer
82cce20934
doc/examples/qsv_transcode: Simplify loop
Fixes: CID1428858(2/2) Logically dead code

Sponsored-by: Sovereign Tech Fund
Reviewed-by: "Xiang, Haihao" <haihao.xiang@intel.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-04-25 03:24:58 +02:00
Andreas Rheinhardt
790f793844 avutil/common: Don't auto-include mem.h
There are lots of files that don't need it: The number of object
files that actually need it went down from 2011 to 884 here.

Keep it for external users in order to not cause breakages.

Also improve the other headers a bit while just at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-03-31 00:08:43 +01:00
Tobias Rapp
02eb2fc577 examples/decode_filter_video: Add loop for draining the filtergraph
Depending on the filters used, the filtergraph may produce trailing data
after feeding it the last input frame. Update the example to include the
necessary loop for draining the filtergraph.

Reviewed-by: Stefano Sabatini <stefasab@gmail.com>
Signed-off-by: Tobias Rapp <t.rapp@noa-archive.com>
2024-03-28 12:04:09 +01:00
Tobias Rapp
55ce666062 examples/decode_filter_audio: Add loop for draining the filtergraph
Depending on the filters used, the filtergraph may produce trailing data
after feeding it the last input frame. Update the example to include the
necessary loop for draining the filtergraph.

Reviewed-by: Stefano Sabatini <stefasab@gmail.com>
Signed-off-by: Tobias Rapp <t.rapp@noa-archive.com>
2024-03-28 12:03:57 +01:00
Andreas Rheinhardt
9223c92c88 doc/examples: Always use <> includes
Reviewed-by: Stefano Sabatini <stefasab@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-03-27 17:07:06 +01:00
hung kuishing
78803a4b8a doc/examples/qsv_decode: remove unused config.h header file
Signed-off-by: clarkh <hungkuishing@outlook.com>
2024-03-12 11:26:06 +01:00
hung kuishing
6d129d8df5 doc/examples/qsv_transcode: EINVAL is more appropriate and ENAVAIL will fail build with visual studio
Signed-off-by: clarkh <hungkuishing@outlook.com>
2023-12-18 09:39:24 +08:00
Zhao Zhili
7665139656 examples/transcode: fix log message
'encoder' can be audio or video encoder.
2023-12-12 00:35:49 +01:00
Andreas Rheinhardt
423b6a7e49 avutil/imgutils: Add wrapper for av_image_copy() to avoid casts
av_image_copy() accepts const uint8_t* const * as source;
lots of user have uint8_t* const * and therefore either
cast (the majority) or copy the array of pointers.

This commit changes this by adding a static inline wrapper
for av_image_copy() that casts between the two types
so that we do not need to add casts everywhere else.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-09-12 09:42:27 +02:00
Zhao Zhili
67d392b979 examples/transcode: flush decoder on EOF
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2023-08-26 17:02:21 +08:00
Sebastian Ramacher
7de3fcb059 examples: fix build of mux and resample_audio
The commits eac4324bfb and
cd8211527e renamed the examples, but the
targets were not updated. Hence, the builds are missing -lm.

Signed-off-by: Sebastian Ramacher <sramacher@debian.org>
Signed-off-by: James Almer <jamrial@gmail.com>
2023-07-28 09:20:39 -03:00
Anton Khirnov
ba32f28498 doc/examples/transcode: convert timestamps between filtering and encoding
The timebases do not have to match.
2023-06-03 11:27:42 +02:00
Anton Khirnov
811b0e02be doc/examples/transcode: stop using decoder's AVCodecContext.time_base
The contents of this field are not defined for decoding. Use
pkt_timebase, which is the timebase of demuxed packets.

Drop a tautological av_packet_rescale_ts() call, as the stream and
decoder timebases are the same.
2023-06-03 11:27:23 +02:00
Anton Khirnov
828d3b4432 doc/examples/transcode: set packet timebase for decoding
It is recommended for callers to set it, though not required.
2023-06-03 11:27:11 +02:00
James Almer
ed3691338b doc/examples/transcode_aac: use av_samples_alloc_array_and_samples to allocate the input samples buffer and pointers
Fixes -Wuse-after-free warnings and simplifies code.

Signed-off-by: James Almer <jamrial@gmail.com>
2023-05-05 15:28:28 -03:00