Blender V5.3
ffmpeg_codecs.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
7#include "BKE_gtest_base.hh"
8
9extern "C" {
10#include "ffmpeg_compat.h"
11
12#include <libavcodec/avcodec.h>
13#include <libavutil/channel_layout.h>
14#include <libavutil/log.h>
15}
16
17namespace {
18
19bool test_vcodec(const AVCodec *codec, AVPixelFormat pixelformat)
20{
21 av_log_set_level(AV_LOG_QUIET);
22 bool result = false;
23 if (codec) {
24 AVCodecContext *ctx = avcodec_alloc_context3(codec);
25 if (ctx) {
26 ctx->time_base.num = 1;
27 ctx->time_base.den = 25;
28 ctx->pix_fmt = pixelformat;
29 ctx->width = 720;
30 ctx->height = 576;
31 int open = avcodec_open2(ctx, codec, nullptr);
32 if (open >= 0) {
33 avcodec_free_context(&ctx);
34 result = true;
35 }
36 }
37 }
38 return result;
39}
40bool test_acodec(const AVCodec *codec, AVSampleFormat fmt)
41{
42 av_log_set_level(AV_LOG_QUIET);
43 bool result = false;
44 if (codec) {
45 AVCodecContext *ctx = avcodec_alloc_context3(codec);
46 if (ctx) {
47 ctx->sample_fmt = fmt;
48 ctx->sample_rate = 48000;
49#ifdef FFMPEG_USE_OLD_CHANNEL_VARS
50 ctx->channel_layout = AV_CH_LAYOUT_MONO;
51#else
52 av_channel_layout_from_mask(&ctx->ch_layout, AV_CH_LAYOUT_MONO);
53#endif
54 ctx->bit_rate = 128000;
55 int open = avcodec_open2(ctx, codec, nullptr);
56 if (open >= 0) {
57 avcodec_free_context(&ctx);
58 result = true;
59 }
60 }
61 }
62 return result;
63}
64
65bool test_codec_video_by_codecid(AVCodecID codec_id, AVPixelFormat pixelformat)
66{
67 bool result = false;
68 const AVCodec *codec = avcodec_find_encoder(codec_id);
69 if (codec) {
70 result = test_vcodec(codec, pixelformat);
71 }
72 return result;
73}
74
75bool test_codec_video_by_name(const char *codecname, AVPixelFormat pixelformat)
76{
77 bool result = false;
78 const AVCodec *codec = avcodec_find_encoder_by_name(codecname);
79 if (codec) {
80 result = test_vcodec(codec, pixelformat);
81 }
82 return result;
83}
84
85bool test_codec_audio_by_codecid(AVCodecID codec_id, AVSampleFormat fmt)
86{
87 bool result = false;
88 const AVCodec *codec = avcodec_find_encoder(codec_id);
89 if (codec) {
90 result = test_acodec(codec, fmt);
91 }
92 return result;
93}
94
95bool test_codec_audio_by_name(const char *codecname, AVSampleFormat fmt)
96{
97 bool result = false;
98 const AVCodec *codec = avcodec_find_encoder_by_name(codecname);
99 if (codec) {
100 result = test_acodec(codec, fmt);
101 }
102 return result;
103}
104
105class FFmpegTest : public blender::bke::BlenderGTestBase {};
106
107#define str(s) #s
108#define FFMPEG_TEST_VCODEC_ID(codec, fmt) \
109 TEST_F(FFmpegTest, codec##_##fmt) \
110 { \
111 EXPECT_TRUE(test_codec_video_by_codecid(codec, fmt)); \
112 }
113
114#define FFMPEG_TEST_VCODEC_NAME(codec, fmt) \
115 TEST_F(FFmpegTest, codec##_##fmt) \
116 { \
117 EXPECT_TRUE(test_codec_video_by_name(str(codec), fmt)); \
118 }
119
120#define FFMPEG_TEST_ACODEC_ID(codec, fmt) \
121 TEST_F(FFmpegTest, codec##_##fmt) \
122 { \
123 EXPECT_TRUE(test_codec_audio_by_codecid(codec, fmt)); \
124 }
125
126#define FFMPEG_TEST_ACODEC_NAME(codec, fmt) \
127 TEST_F(FFmpegTest, codec) \
128 { \
129 EXPECT_TRUE(test_codec_audio_by_name(str(codec), fmt)); \
130 }
131
132} // namespace
133
134/* generic codec ID's used in blender */
135
136FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_HUFFYUV, AV_PIX_FMT_BGRA)
137FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_HUFFYUV, AV_PIX_FMT_RGB32)
138FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_FFV1, AV_PIX_FMT_RGB32)
139FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_QTRLE, AV_PIX_FMT_ARGB)
140FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_VP9, AV_PIX_FMT_YUVA420P)
141FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_PNG, AV_PIX_FMT_RGBA)
142FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_H264, AV_PIX_FMT_YUV420P)
143FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_H264, AV_PIX_FMT_YUV420P10LE)
144FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_H265, AV_PIX_FMT_YUV420P)
145FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_H265, AV_PIX_FMT_YUV420P10LE)
146FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_H265, AV_PIX_FMT_YUV420P12LE)
147FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_MPEG4, AV_PIX_FMT_YUV420P)
148FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_THEORA, AV_PIX_FMT_YUV420P)
149FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_DVVIDEO, AV_PIX_FMT_YUV420P)
150FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_MPEG1VIDEO, AV_PIX_FMT_YUV420P)
151FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_MPEG2VIDEO, AV_PIX_FMT_YUV420P)
152FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_FLV1, AV_PIX_FMT_YUV420P)
153FFMPEG_TEST_VCODEC_ID(AV_CODEC_ID_AV1, AV_PIX_FMT_YUV420P)
154
155/* Audio codecs */
156
157FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_AAC, AV_SAMPLE_FMT_FLTP)
158FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_AC3, AV_SAMPLE_FMT_FLTP)
159FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_FLAC, AV_SAMPLE_FMT_S16)
160FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_MP2, AV_SAMPLE_FMT_S16)
161FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_MP3, AV_SAMPLE_FMT_FLTP)
162FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_OPUS, AV_SAMPLE_FMT_FLT)
163FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_PCM_S16LE, AV_SAMPLE_FMT_S16)
164FFMPEG_TEST_ACODEC_ID(AV_CODEC_ID_VORBIS, AV_SAMPLE_FMT_FLTP)
165
166/* Libraries we count on ffmpeg being linked against */
167
168FFMPEG_TEST_VCODEC_NAME(libtheora, AV_PIX_FMT_YUV420P)
169FFMPEG_TEST_VCODEC_NAME(libx264, AV_PIX_FMT_YUV420P)
170FFMPEG_TEST_VCODEC_NAME(libvpx, AV_PIX_FMT_YUV420P)
171FFMPEG_TEST_VCODEC_NAME(libopenjpeg, AV_PIX_FMT_YUV420P)
172/* AOM's AV1 encoder is `libaom-av1`. `FFMPEG_TEST_VCODEC_NAME(libaom-av1, ...)`
173 * will not work because the dash will not work with the test macro. */
174TEST_F(FFmpegTest, libaom_av1_AV_PIX_FMT_YUV420P)
175{
176 EXPECT_TRUE(test_codec_video_by_name("libaom-av1", AV_PIX_FMT_YUV420P));
177}
178FFMPEG_TEST_ACODEC_NAME(libvorbis, AV_SAMPLE_FMT_FLTP)
179FFMPEG_TEST_ACODEC_NAME(libopus, AV_SAMPLE_FMT_FLT)
180FFMPEG_TEST_ACODEC_NAME(libmp3lame, AV_SAMPLE_FMT_FLTP)
#define FFMPEG_TEST_VCODEC_NAME(codec, fmt)
TEST_F(FFmpegTest, libaom_av1_AV_PIX_FMT_YUV420P)
#define FFMPEG_TEST_ACODEC_NAME(codec, fmt)
#define FFMPEG_TEST_ACODEC_ID(codec, fmt)
#define FFMPEG_TEST_VCODEC_ID(codec, fmt)
#define EXPECT_TRUE(result)