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