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