ffmpeg_compat.h 16 KB

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