ffmpeg_compat.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #pragma once
  2. /**
  3. * FFmpeg Version Compatibility Header
  4. * This file provides compatibility macros and functions for different FFmpeg versions
  5. * Supports FFmpeg 3.x, 4.x, 5.x, 6.x, and 7.x
  6. */
  7. extern "C" {
  8. #include <libavcodec/avcodec.h>
  9. #include <libavformat/avformat.h>
  10. #include <libavdevice/avdevice.h>
  11. #include <libavutil/channel_layout.h>
  12. #include <libavcodec/bsf.h>
  13. }
  14. // FFmpeg version detection
  15. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  16. #define FFMPEG_VERSION_MAJOR 3
  17. #elif LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  18. #define FFMPEG_VERSION_MAJOR 4
  19. #elif LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(60, 0, 100)
  20. #define FFMPEG_VERSION_MAJOR 5
  21. #elif LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(61, 0, 100)
  22. #define FFMPEG_VERSION_MAJOR 6
  23. #else
  24. #define FFMPEG_VERSION_MAJOR 7
  25. #endif
  26. // Compatibility functions for deprecated functions
  27. inline void ffmpeg_register_all() {
  28. #if FFMPEG_VERSION_MAJOR < 4
  29. av_register_all();
  30. #endif
  31. // No-op in FFmpeg 4.0+
  32. }
  33. inline void ffmpeg_register_devices() {
  34. #if FFMPEG_VERSION_MAJOR < 4
  35. avdevice_register_all();
  36. #endif
  37. // No-op in FFmpeg 4.0+
  38. }
  39. // Compatibility for AVInputFormat/AVOutputFormat
  40. #if FFMPEG_VERSION_MAJOR >= 4
  41. using FFmpegInputFormat = const AVInputFormat;
  42. using FFmpegOutputFormat = const AVOutputFormat;
  43. #else
  44. using FFmpegInputFormat = AVInputFormat;
  45. using FFmpegOutputFormat = AVOutputFormat;
  46. #endif
  47. // Compatibility for AVCodec pointer
  48. #if FFMPEG_VERSION_MAJOR >= 7
  49. using FFmpegCodec = const AVCodec;
  50. #else
  51. using FFmpegCodec = AVCodec;
  52. #endif
  53. // Compatibility for av_err2str macro (C++ safe version)
  54. #ifdef __cplusplus
  55. #undef av_err2str
  56. #ifdef _MSC_VER
  57. #include <malloc.h>
  58. #define av_err2str(errnum) av_make_error_string((char*)_alloca(AV_ERROR_MAX_STRING_SIZE), AV_ERROR_MAX_STRING_SIZE, errnum)
  59. #else
  60. #define av_err2str(errnum) av_make_error_string((char*)__builtin_alloca(AV_ERROR_MAX_STRING_SIZE), AV_ERROR_MAX_STRING_SIZE, errnum)
  61. #endif
  62. #endif
  63. // Compatibility for bitstream filter context
  64. #if FFMPEG_VERSION_MAJOR >= 6
  65. using AVBitStreamFilterContext = AVBSFContext;
  66. #endif
  67. // Compatibility functions for codec context access
  68. inline AVMediaType ffmpeg_get_codec_type(AVStream* stream) {
  69. #if FFMPEG_VERSION_MAJOR >= 4
  70. return stream->codecpar->codec_type;
  71. #else
  72. return stream->codec->codec_type;
  73. #endif
  74. }
  75. inline AVCodecID ffmpeg_get_codec_id(AVStream* stream) {
  76. #if FFMPEG_VERSION_MAJOR >= 4
  77. return stream->codecpar->codec_id;
  78. #else
  79. return stream->codec->codec_id;
  80. #endif
  81. }
  82. inline int ffmpeg_get_codec_width(AVStream* stream) {
  83. #if FFMPEG_VERSION_MAJOR >= 4
  84. return stream->codecpar->width;
  85. #else
  86. return stream->codec->width;
  87. #endif
  88. }
  89. inline int ffmpeg_get_codec_height(AVStream* stream) {
  90. #if FFMPEG_VERSION_MAJOR >= 4
  91. return stream->codecpar->height;
  92. #else
  93. return stream->codec->height;
  94. #endif
  95. }
  96. inline AVPixelFormat ffmpeg_get_codec_pix_fmt(AVStream* stream) {
  97. #if FFMPEG_VERSION_MAJOR >= 4
  98. return static_cast<AVPixelFormat>(stream->codecpar->format);
  99. #else
  100. return stream->codec->pix_fmt;
  101. #endif
  102. }
  103. inline int ffmpeg_get_codec_sample_rate(AVStream* stream) {
  104. #if FFMPEG_VERSION_MAJOR >= 4
  105. return stream->codecpar->sample_rate;
  106. #else
  107. return stream->codec->sample_rate;
  108. #endif
  109. }
  110. inline int ffmpeg_get_codec_channels(AVStream* stream) {
  111. #if FFMPEG_VERSION_MAJOR >= 7
  112. return stream->codecpar->ch_layout.nb_channels;
  113. #elif FFMPEG_VERSION_MAJOR >= 4
  114. return stream->codecpar->channels;
  115. #else
  116. return stream->codec->channels;
  117. #endif
  118. }
  119. inline uint64_t ffmpeg_get_codec_channel_layout(AVStream* stream) {
  120. #if FFMPEG_VERSION_MAJOR >= 7
  121. // In FFmpeg 7, use ch_layout.u.mask directly if it's a mask layout
  122. if (stream->codecpar->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) {
  123. return stream->codecpar->ch_layout.u.mask;
  124. } else {
  125. // For non-mask layouts, return a default stereo layout
  126. return AV_CH_LAYOUT_STEREO;
  127. }
  128. #elif FFMPEG_VERSION_MAJOR >= 4
  129. return stream->codecpar->channel_layout;
  130. #else
  131. return stream->codec->channel_layout;
  132. #endif
  133. }
  134. inline AVSampleFormat ffmpeg_get_codec_sample_fmt(AVStream* stream) {
  135. #if FFMPEG_VERSION_MAJOR >= 4
  136. return static_cast<AVSampleFormat>(stream->codecpar->format);
  137. #else
  138. return stream->codec->sample_fmt;
  139. #endif
  140. }
  141. // Compatibility for channel layout functions
  142. inline uint64_t ffmpeg_get_default_channel_layout(int channels) {
  143. #if FFMPEG_VERSION_MAJOR >= 7
  144. AVChannelLayout ch_layout;
  145. av_channel_layout_default(&ch_layout, channels);
  146. if (ch_layout.order == AV_CHANNEL_ORDER_NATIVE) {
  147. return ch_layout.u.mask;
  148. } else {
  149. // Fallback for common channel counts
  150. switch (channels) {
  151. case 1: return AV_CH_LAYOUT_MONO;
  152. case 2: return AV_CH_LAYOUT_STEREO;
  153. case 6: return AV_CH_LAYOUT_5POINT1;
  154. case 8: return AV_CH_LAYOUT_7POINT1;
  155. default: return AV_CH_LAYOUT_STEREO;
  156. }
  157. }
  158. #else
  159. return av_get_default_channel_layout(channels);
  160. #endif
  161. }
  162. // Compatibility for filter registration
  163. inline void ffmpeg_register_filters() {
  164. #if FFMPEG_VERSION_MAJOR < 4
  165. avfilter_register_all();
  166. #endif
  167. // No-op in FFmpeg 4.0+
  168. }
  169. // Compatibility for channel layout string functions
  170. inline int ffmpeg_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout) {
  171. #if FFMPEG_VERSION_MAJOR >= 7
  172. (void)nb_channels; // Suppress unused parameter warning
  173. AVChannelLayout ch_layout;
  174. av_channel_layout_from_mask(&ch_layout, channel_layout);
  175. return av_channel_layout_describe(&ch_layout, buf, buf_size);
  176. #else
  177. av_get_channel_layout_string(buf, buf_size, nb_channels, channel_layout);
  178. return strlen(buf);
  179. #endif
  180. }
  181. // Compatibility for AVFrame channels access
  182. inline int ffmpeg_get_frame_channels(AVFrame* frame) {
  183. #if FFMPEG_VERSION_MAJOR >= 7
  184. return frame->ch_layout.nb_channels;
  185. #else
  186. return frame->channels;
  187. #endif
  188. }
  189. // Compatibility for av_samples_get_buffer_size
  190. inline int ffmpeg_get_buffer_size(enum AVSampleFormat sample_fmt, int nb_channels, int nb_samples, int align) {
  191. #if FFMPEG_VERSION_MAJOR >= 7
  192. return av_samples_get_buffer_size(nullptr, nb_channels, nb_samples, sample_fmt, align);
  193. #else
  194. return av_samples_get_buffer_size(nullptr, nb_channels, nb_samples, sample_fmt, align);
  195. #endif
  196. }
  197. // Compatibility for codec context creation from stream
  198. inline AVCodecContext* ffmpeg_get_codec_context(AVStream* stream) {
  199. #if FFMPEG_VERSION_MAJOR >= 4
  200. const AVCodec* codec = avcodec_find_decoder(stream->codecpar->codec_id);
  201. if (!codec) return nullptr;
  202. AVCodecContext* ctx = avcodec_alloc_context3(codec);
  203. if (!ctx) return nullptr;
  204. if (avcodec_parameters_to_context(ctx, stream->codecpar) < 0) {
  205. avcodec_free_context(&ctx);
  206. return nullptr;
  207. }
  208. return ctx;
  209. #else
  210. return stream->codec;
  211. #endif
  212. }
  213. // Compatibility for setting stream codec parameters
  214. inline void ffmpeg_set_stream_codec_id(AVStream* stream, enum AVCodecID codec_id) {
  215. #if FFMPEG_VERSION_MAJOR >= 4
  216. stream->codecpar->codec_id = codec_id;
  217. #else
  218. stream->codec->codec_id = codec_id;
  219. #endif
  220. }
  221. inline void ffmpeg_set_stream_codec_type(AVStream* stream, enum AVMediaType codec_type) {
  222. #if FFMPEG_VERSION_MAJOR >= 4
  223. stream->codecpar->codec_type = codec_type;
  224. #else
  225. stream->codec->codec_type = codec_type;
  226. #endif
  227. }
  228. inline void ffmpeg_set_stream_bit_rate(AVStream* stream, int64_t bit_rate) {
  229. #if FFMPEG_VERSION_MAJOR >= 4
  230. stream->codecpar->bit_rate = bit_rate;
  231. #else
  232. stream->codec->bit_rate = bit_rate;
  233. #endif
  234. }
  235. inline void ffmpeg_set_stream_pix_fmt(AVStream* stream, enum AVPixelFormat pix_fmt) {
  236. #if FFMPEG_VERSION_MAJOR >= 4
  237. stream->codecpar->format = pix_fmt;
  238. #else
  239. stream->codec->pix_fmt = pix_fmt;
  240. #endif
  241. }
  242. inline void ffmpeg_set_stream_dimensions(AVStream* stream, int width, int height) {
  243. #if FFMPEG_VERSION_MAJOR >= 4
  244. stream->codecpar->width = width;
  245. stream->codecpar->height = height;
  246. #else
  247. stream->codec->width = width;
  248. stream->codec->height = height;
  249. #endif
  250. }
  251. // Compatibility for stream codec parameters
  252. inline int ffmpeg_copy_codec_params_to_stream(AVStream* stream, AVCodecContext* codec_ctx) {
  253. #if FFMPEG_VERSION_MAJOR >= 4
  254. return avcodec_parameters_from_context(stream->codecpar, codec_ctx);
  255. #else
  256. return 0; // No-op for older versions
  257. #endif
  258. }
  259. // Compatibility for extradata access
  260. inline uint8_t* ffmpeg_get_extradata(AVStream* stream) {
  261. #if FFMPEG_VERSION_MAJOR >= 4
  262. return stream->codecpar->extradata;
  263. #else
  264. return stream->codec->extradata;
  265. #endif
  266. }
  267. inline int ffmpeg_get_extradata_size(AVStream* stream) {
  268. #if FFMPEG_VERSION_MAJOR >= 4
  269. return stream->codecpar->extradata_size;
  270. #else
  271. return stream->codec->extradata_size;
  272. #endif
  273. }
  274. inline void ffmpeg_set_stream_extradata(AVStream* stream, uint8_t* data, int size) {
  275. #if FFMPEG_VERSION_MAJOR >= 4
  276. stream->codecpar->extradata = data;
  277. stream->codecpar->extradata_size = size;
  278. #else
  279. stream->codec->extradata = data;
  280. stream->codec->extradata_size = size;
  281. #endif
  282. }
  283. // Compatibility for AVCodecContext channels and channel_layout
  284. inline void ffmpeg_set_codec_channels(AVCodecContext* ctx, int channels) {
  285. #if FFMPEG_VERSION_MAJOR >= 7
  286. av_channel_layout_default(&ctx->ch_layout, channels);
  287. #else
  288. ctx->channels = channels;
  289. #endif
  290. }
  291. inline void ffmpeg_set_codec_channel_layout(AVCodecContext* ctx, uint64_t channel_layout) {
  292. #if FFMPEG_VERSION_MAJOR >= 7
  293. av_channel_layout_from_mask(&ctx->ch_layout, channel_layout);
  294. #else
  295. ctx->channel_layout = channel_layout;
  296. #endif
  297. }
  298. inline int ffmpeg_get_codec_context_channels(AVCodecContext* ctx) {
  299. #if FFMPEG_VERSION_MAJOR >= 7
  300. return ctx->ch_layout.nb_channels;
  301. #else
  302. return ctx->channels;
  303. #endif
  304. }
  305. // Compatibility for swr_alloc_set_opts
  306. inline SwrContext* ffmpeg_swr_alloc_set_opts(SwrContext *s,
  307. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  308. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  309. int log_offset, void *log_ctx) {
  310. #if FFMPEG_VERSION_MAJOR >= 7
  311. SwrContext *swr_ctx = swr_alloc();
  312. if (!swr_ctx) return NULL;
  313. AVChannelLayout out_ch_layout_new, in_ch_layout_new;
  314. av_channel_layout_from_mask(&out_ch_layout_new, out_ch_layout);
  315. av_channel_layout_from_mask(&in_ch_layout_new, in_ch_layout);
  316. av_opt_set_chlayout(swr_ctx, "ochl", &out_ch_layout_new, 0);
  317. av_opt_set_int(swr_ctx, "osf", out_sample_fmt, 0);
  318. av_opt_set_int(swr_ctx, "osr", out_sample_rate, 0);
  319. av_opt_set_chlayout(swr_ctx, "ichl", &in_ch_layout_new, 0);
  320. av_opt_set_int(swr_ctx, "isf", in_sample_fmt, 0);
  321. av_opt_set_int(swr_ctx, "isr", in_sample_rate, 0);
  322. av_channel_layout_uninit(&out_ch_layout_new);
  323. av_channel_layout_uninit(&in_ch_layout_new);
  324. return swr_ctx;
  325. #else
  326. return swr_alloc_set_opts(s, out_ch_layout, out_sample_fmt, out_sample_rate,
  327. in_ch_layout, in_sample_fmt, in_sample_rate,
  328. log_offset, log_ctx);
  329. #endif
  330. }
  331. // Compatibility for codec parameters setting
  332. inline int ffmpeg_av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
  333. int nb_samples, enum AVSampleFormat sample_fmt, int align) {
  334. #if FFMPEG_VERSION_MAJOR >= 7
  335. return av_samples_alloc_array_and_samples(&audio_data, linesize, nb_channels, nb_samples, sample_fmt, align);
  336. #else
  337. return av_samples_alloc(audio_data, linesize, nb_channels, nb_samples, sample_fmt, align);
  338. #endif
  339. }
  340. inline void ffmpeg_set_frame_channel_layout(AVFrame* frame, AVCodecContext* codec_ctx) {
  341. #if FFMPEG_VERSION_MAJOR >= 7
  342. av_channel_layout_copy(&frame->ch_layout, &codec_ctx->ch_layout);
  343. #else
  344. frame->channel_layout = codec_ctx->channel_layout;
  345. #endif
  346. }
  347. inline void ffmpeg_set_stream_codec_params(AVStream* stream, AVCodecContext* codec_ctx) {
  348. #if FFMPEG_VERSION_MAJOR >= 7
  349. stream->codecpar->codec_id = codec_ctx->codec_id;
  350. stream->codecpar->bit_rate = codec_ctx->bit_rate;
  351. stream->codecpar->width = codec_ctx->width;
  352. stream->codecpar->height = codec_ctx->height;
  353. stream->codecpar->format = codec_ctx->pix_fmt;
  354. stream->codecpar->sample_rate = codec_ctx->sample_rate;
  355. av_channel_layout_copy(&stream->codecpar->ch_layout, &codec_ctx->ch_layout);
  356. stream->time_base = codec_ctx->time_base;
  357. #elif FFMPEG_VERSION_MAJOR >= 4
  358. stream->codecpar->codec_id = codec_ctx->codec_id;
  359. stream->codecpar->bit_rate = codec_ctx->bit_rate;
  360. stream->codecpar->width = codec_ctx->width;
  361. stream->codecpar->height = codec_ctx->height;
  362. stream->codecpar->format = codec_ctx->pix_fmt;
  363. stream->codecpar->sample_rate = codec_ctx->sample_rate;
  364. stream->codecpar->channels = codec_ctx->channels;
  365. stream->codecpar->channel_layout = codec_ctx->channel_layout;
  366. stream->time_base = codec_ctx->time_base;
  367. #else
  368. *(stream->codec) = *codec_ctx;
  369. #endif
  370. }
  371. // Additional compatibility functions for missing identifiers
  372. inline int ffmpeg_get_channels(AVCodecContext* ctx) {
  373. #if FFMPEG_VERSION_MAJOR >= 7
  374. return ctx->ch_layout.nb_channels;
  375. #else
  376. return ctx->channels;
  377. #endif
  378. }
  379. inline void ffmpeg_set_channels(AVCodecContext* ctx, int channels) {
  380. #if FFMPEG_VERSION_MAJOR >= 7
  381. av_channel_layout_default(&ctx->ch_layout, channels);
  382. #else
  383. ctx->channels = channels;
  384. #endif
  385. }
  386. inline void ffmpeg_set_channel_layout(AVCodecContext* ctx, uint64_t channel_layout) {
  387. #if FFMPEG_VERSION_MAJOR >= 7
  388. av_channel_layout_from_mask(&ctx->ch_layout, channel_layout);
  389. #else
  390. ctx->channel_layout = channel_layout;
  391. #endif
  392. }
  393. inline void ffmpeg_set_frame_channels(AVFrame* frame, int channels) {
  394. #if FFMPEG_VERSION_MAJOR >= 7
  395. av_channel_layout_default(&frame->ch_layout, channels);
  396. #else
  397. frame->channels = channels;
  398. #endif
  399. }
  400. inline void ffmpeg_set_frame_channel_layout(AVFrame* frame, uint64_t channel_layout) {
  401. #if FFMPEG_VERSION_MAJOR >= 7
  402. av_channel_layout_from_mask(&frame->ch_layout, channel_layout);
  403. #else
  404. frame->channel_layout = channel_layout;
  405. #endif
  406. }
  407. inline void ffmpeg_set_frame_pkt_pts(AVFrame* frame, int64_t pts) {
  408. #if FFMPEG_VERSION_MAJOR >= 7
  409. frame->pts = pts;
  410. #else
  411. frame->pkt_pts = pts;
  412. #endif
  413. }
  414. // Compatibility for bitstream filter initialization
  415. inline AVBitStreamFilterContext* ffmpeg_bitstream_filter_init(const char* name) {
  416. #if FFMPEG_VERSION_MAJOR >= 6
  417. const AVBitStreamFilter* bsf = av_bsf_get_by_name(name);
  418. if (!bsf) return nullptr;
  419. AVBSFContext* ctx = nullptr;
  420. if (av_bsf_alloc(bsf, &ctx) < 0) return nullptr;
  421. return ctx;
  422. #else
  423. return av_bitstream_filter_init(name);
  424. #endif
  425. }