ffmpeg_compat.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * compatibility macros to make every ffmpeg installation appear
  3. * like the most current installation (wrapping some functionality sometimes)
  4. * it also includes all ffmpeg header files at once, no need to do it
  5. * separately.
  6. *
  7. * Copyright (c) 2011 Peter Schlaile
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #ifndef __FFMPEG_COMPAT_H__
  20. #define __FFMPEG_COMPAT_H__
  21. #include <libavformat/avformat.h>
  22. /* check our ffmpeg is new enough, avoids user complaints */
  23. #if (LIBAVFORMAT_VERSION_MAJOR < 52) || \
  24. ((LIBAVFORMAT_VERSION_MAJOR == 52) && (LIBAVFORMAT_VERSION_MINOR <= 64))
  25. # error "FFmpeg 0.7 or newer is needed, Upgrade your FFmpeg or disable it"
  26. #endif
  27. /* end sanity check */
  28. /* visual studio 2012 does not define inline for C */
  29. #ifdef _MSC_VER
  30. # define FFMPEG_INLINE static __inline
  31. #else
  32. # define FFMPEG_INLINE static inline
  33. #endif
  34. #include <libavcodec/avcodec.h>
  35. #include <libavutil/rational.h>
  36. #include <libavutil/opt.h>
  37. #include <libavutil/mathematics.h>
  38. #if (LIBAVFORMAT_VERSION_MAJOR > 52) || \
  39. ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 101))
  40. # define FFMPEG_HAVE_PARSE_UTILS 1
  41. # include <libavutil/parseutils.h>
  42. #endif
  43. #include <libswscale/swscale.h>
  44. /* Stupid way to distinguish FFmpeg from Libav:
  45. * - FFmpeg's MICRO version starts from 100 and goes up, while
  46. * - Libav's micro is always below 100.
  47. */
  48. #if LIBAVCODEC_VERSION_MICRO >= 100
  49. # define AV_USING_FFMPEG
  50. #else
  51. # define AV_USING_LIBAV
  52. #endif
  53. #if (LIBAVFORMAT_VERSION_MAJOR > 52) || \
  54. ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 105))
  55. # define FFMPEG_HAVE_AVIO 1
  56. #endif
  57. #if (LIBAVCODEC_VERSION_MAJOR > 53) || \
  58. ((LIBAVCODEC_VERSION_MAJOR == 53) && (LIBAVCODEC_VERSION_MINOR > 1)) || \
  59. ((LIBAVCODEC_VERSION_MAJOR == 53) && (LIBAVCODEC_VERSION_MINOR == 1) && \
  60. (LIBAVCODEC_VERSION_MICRO >= 1)) || \
  61. ((LIBAVCODEC_VERSION_MAJOR == 52) && (LIBAVCODEC_VERSION_MINOR >= 121))
  62. # define FFMPEG_HAVE_DEFAULT_VAL_UNION 1
  63. #endif
  64. #if (LIBAVFORMAT_VERSION_MAJOR > 52) || \
  65. ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 101))
  66. # define FFMPEG_HAVE_AV_DUMP_FORMAT 1
  67. #endif
  68. #if (LIBAVFORMAT_VERSION_MAJOR > 52) || \
  69. ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 45))
  70. # define FFMPEG_HAVE_AV_GUESS_FORMAT 1
  71. #endif
  72. #if (LIBAVCODEC_VERSION_MAJOR > 52) || \
  73. ((LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 23))
  74. # define FFMPEG_HAVE_DECODE_AUDIO3 1
  75. # define FFMPEG_HAVE_DECODE_VIDEO2 1
  76. #endif
  77. #if (LIBAVCODEC_VERSION_MAJOR > 52) || \
  78. ((LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 64))
  79. # define FFMPEG_HAVE_AVMEDIA_TYPES 1
  80. #endif
  81. #if ((LIBAVCODEC_VERSION_MAJOR > 52) || \
  82. (LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 29)) && \
  83. ((LIBSWSCALE_VERSION_MAJOR > 0) || \
  84. (LIBSWSCALE_VERSION_MAJOR >= 0) && (LIBSWSCALE_VERSION_MINOR >= 10))
  85. # define FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
  86. #endif
  87. #if ((LIBAVCODEC_VERSION_MAJOR > 54) || \
  88. (LIBAVCODEC_VERSION_MAJOR >= 54) && (LIBAVCODEC_VERSION_MINOR > 14))
  89. # define FFMPEG_HAVE_CANON_H264_RESOLUTION_FIX
  90. #endif
  91. #if ((LIBAVCODEC_VERSION_MAJOR > 53) || \
  92. (LIBAVCODEC_VERSION_MAJOR >= 53) && (LIBAVCODEC_VERSION_MINOR >= 60))
  93. # define FFMPEG_HAVE_ENCODE_AUDIO2
  94. #endif
  95. #if ((LIBAVCODEC_VERSION_MAJOR > 53) || \
  96. (LIBAVCODEC_VERSION_MAJOR >= 53) && (LIBAVCODEC_VERSION_MINOR >= 42))
  97. # define FFMPEG_HAVE_DECODE_AUDIO4
  98. #endif
  99. #if ((LIBAVCODEC_VERSION_MAJOR > 54) || \
  100. (LIBAVCODEC_VERSION_MAJOR >= 54) && (LIBAVCODEC_VERSION_MINOR >= 13))
  101. # define FFMPEG_HAVE_AVFRAME_SAMPLE_RATE
  102. #endif
  103. #if ((LIBAVUTIL_VERSION_MAJOR > 51) || \
  104. (LIBAVUTIL_VERSION_MAJOR == 51) && (LIBAVUTIL_VERSION_MINOR >= 21))
  105. # define FFMPEG_FFV1_ALPHA_SUPPORTED
  106. # define FFMPEG_SAMPLE_FMT_S16P_SUPPORTED
  107. #else
  108. FFMPEG_INLINE
  109. int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
  110. {
  111. /* no planar formats in FFmpeg < 0.9 */
  112. (void)sample_fmt;
  113. return 0;
  114. }
  115. #endif
  116. /* XXX TODO Probably fix to correct modern flags in code? Not sure how old FFMPEG we want to
  117. * support though, so for now this will do. */
  118. #ifndef FF_MIN_BUFFER_SIZE
  119. # ifdef AV_INPUT_BUFFER_MIN_SIZE
  120. # define FF_MIN_BUFFER_SIZE AV_INPUT_BUFFER_MIN_SIZE
  121. # endif
  122. #endif
  123. #ifndef FF_INPUT_BUFFER_PADDING_SIZE
  124. # ifdef AV_INPUT_BUFFER_PADDING_SIZE
  125. # define FF_INPUT_BUFFER_PADDING_SIZE AV_INPUT_BUFFER_PADDING_SIZE
  126. # endif
  127. #endif
  128. #ifndef CODEC_FLAG_GLOBAL_HEADER
  129. # ifdef AV_CODEC_FLAG_GLOBAL_HEADER
  130. # define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
  131. # endif
  132. #endif
  133. #ifndef CODEC_FLAG_GLOBAL_HEADER
  134. # ifdef AV_CODEC_FLAG_GLOBAL_HEADER
  135. # define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
  136. # endif
  137. #endif
  138. #ifndef CODEC_FLAG_INTERLACED_DCT
  139. # ifdef AV_CODEC_FLAG_INTERLACED_DCT
  140. # define CODEC_FLAG_INTERLACED_DCT AV_CODEC_FLAG_INTERLACED_DCT
  141. # endif
  142. #endif
  143. #ifndef CODEC_FLAG_INTERLACED_ME
  144. # ifdef AV_CODEC_FLAG_INTERLACED_ME
  145. # define CODEC_FLAG_INTERLACED_ME AV_CODEC_FLAG_INTERLACED_ME
  146. # endif
  147. #endif
  148. /* FFmpeg upstream 1.0 is the first who added AV_ prefix. */
  149. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
  150. # define AV_CODEC_ID_NONE CODEC_ID_NONE
  151. # define AV_CODEC_ID_MPEG4 CODEC_ID_MPEG4
  152. # define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG
  153. # define AV_CODEC_ID_DNXHD CODEC_ID_DNXHD
  154. # define AV_CODEC_ID_MPEG2VIDEO CODEC_ID_MPEG2VIDEO
  155. # define AV_CODEC_ID_MPEG1VIDEO CODEC_ID_MPEG1VIDEO
  156. # define AV_CODEC_ID_DVVIDEO CODEC_ID_DVVIDEO
  157. # define AV_CODEC_ID_THEORA CODEC_ID_THEORA
  158. # define AV_CODEC_ID_PNG CODEC_ID_PNG
  159. # define AV_CODEC_ID_QTRLE CODEC_ID_QTRLE
  160. # define AV_CODEC_ID_FFV1 CODEC_ID_FFV1
  161. # define AV_CODEC_ID_HUFFYUV CODEC_ID_HUFFYUV
  162. # define AV_CODEC_ID_H264 CODEC_ID_H264
  163. # define AV_CODEC_ID_FLV1 CODEC_ID_FLV1
  164. # define AV_CODEC_ID_AAC CODEC_ID_AAC
  165. # define AV_CODEC_ID_AC3 CODEC_ID_AC3
  166. # define AV_CODEC_ID_MP3 CODEC_ID_MP3
  167. # define AV_CODEC_ID_MP2 CODEC_ID_MP2
  168. # define AV_CODEC_ID_FLAC CODEC_ID_FLAC
  169. # define AV_CODEC_ID_PCM_U8 CODEC_ID_PCM_U8
  170. # define AV_CODEC_ID_PCM_S16LE CODEC_ID_PCM_S16LE
  171. # define AV_CODEC_ID_PCM_S24LE CODEC_ID_PCM_S24LE
  172. # define AV_CODEC_ID_PCM_S32LE CODEC_ID_PCM_S32LE
  173. # define AV_CODEC_ID_PCM_F32LE CODEC_ID_PCM_F32LE
  174. # define AV_CODEC_ID_PCM_F64LE CODEC_ID_PCM_F64LE
  175. # define AV_CODEC_ID_VORBIS CODEC_ID_VORBIS
  176. #endif
  177. FFMPEG_INLINE
  178. int av_get_cropped_height_from_codec(AVCodecContext *pCodecCtx)
  179. {
  180. int y = pCodecCtx->height;
  181. #ifndef FFMPEG_HAVE_CANON_H264_RESOLUTION_FIX
  182. /* really bad hack to remove this dreadfull black bar at the bottom
  183. with Canon footage and old ffmpeg versions.
  184. (to fix this properly in older ffmpeg versions one has to write a new
  185. demuxer...)
  186. see the actual fix here for reference:
  187. http://git.libav.org/?p=libav.git;a=commit;h=30f515091c323da59c0f1b533703dedca2f4b95d
  188. We do our best to apply this only to matching footage.
  189. */
  190. if (pCodecCtx->width == 1920 && pCodecCtx->height == 1088 &&
  191. pCodecCtx->pix_fmt == PIX_FMT_YUVJ420P && pCodecCtx->codec_id == AV_CODEC_ID_H264) {
  192. y = 1080;
  193. }
  194. #endif
  195. return y;
  196. }
  197. #if ((LIBAVUTIL_VERSION_MAJOR < 51) || \
  198. (LIBAVUTIL_VERSION_MAJOR == 51) && (LIBAVUTIL_VERSION_MINOR < 22))
  199. FFMPEG_INLINE
  200. int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  201. {
  202. const AVOption *rv = NULL;
  203. (void)search_flags;
  204. av_set_string3(obj, name, val, 1, &rv);
  205. return rv != NULL;
  206. }
  207. FFMPEG_INLINE
  208. int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  209. {
  210. const AVOption *rv = NULL;
  211. (void)search_flags;
  212. rv = av_set_int(obj, name, val);
  213. return rv != NULL;
  214. }
  215. FFMPEG_INLINE
  216. int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  217. {
  218. const AVOption *rv = NULL;
  219. (void)search_flags;
  220. rv = av_set_double(obj, name, val);
  221. return rv != NULL;
  222. }
  223. # define AV_OPT_TYPE_INT FF_OPT_TYPE_INT
  224. # define AV_OPT_TYPE_INT64 FF_OPT_TYPE_INT64
  225. # define AV_OPT_TYPE_STRING FF_OPT_TYPE_STRING
  226. # define AV_OPT_TYPE_CONST FF_OPT_TYPE_CONST
  227. # define AV_OPT_TYPE_DOUBLE FF_OPT_TYPE_DOUBLE
  228. # define AV_OPT_TYPE_FLOAT FF_OPT_TYPE_FLOAT
  229. #endif
  230. #if ((LIBAVUTIL_VERSION_MAJOR < 51) || \
  231. (LIBAVUTIL_VERSION_MAJOR == 51) && (LIBAVUTIL_VERSION_MINOR < 54))
  232. FFMPEG_INLINE
  233. enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
  234. {
  235. if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
  236. return AV_SAMPLE_FMT_NONE;
  237. return sample_fmt;
  238. }
  239. #endif
  240. #if ((LIBAVCODEC_VERSION_MAJOR < 53) || \
  241. (LIBAVCODEC_VERSION_MAJOR == 53 && LIBAVCODEC_VERSION_MINOR < 35))
  242. FFMPEG_INLINE
  243. int avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  244. {
  245. /* TODO: no options are taking into account */
  246. (void)options;
  247. return avcodec_open(avctx, codec);
  248. }
  249. #endif
  250. #if ((LIBAVFORMAT_VERSION_MAJOR < 53) || \
  251. (LIBAVFORMAT_VERSION_MAJOR == 53 && LIBAVFORMAT_VERSION_MINOR < 21))
  252. FFMPEG_INLINE
  253. AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c)
  254. {
  255. /* TODO: no codec is taking into account */
  256. (void)c;
  257. return av_new_stream(s, 0);
  258. }
  259. FFMPEG_INLINE
  260. int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
  261. {
  262. /* TODO: no options are taking into account */
  263. (void)options;
  264. return av_find_stream_info(ic);
  265. }
  266. #endif
  267. #if ((LIBAVFORMAT_VERSION_MAJOR > 53) || \
  268. ((LIBAVFORMAT_VERSION_MAJOR == 53) && (LIBAVFORMAT_VERSION_MINOR > 32)) || \
  269. ((LIBAVFORMAT_VERSION_MAJOR == 53) && (LIBAVFORMAT_VERSION_MINOR == 24) && \
  270. (LIBAVFORMAT_VERSION_MICRO >= 100)))
  271. FFMPEG_INLINE
  272. void my_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
  273. {
  274. int i;
  275. for (i = 0; i < s->nb_streams; i++) {
  276. AVStream *st = s->streams[i];
  277. st->cur_dts = av_rescale(timestamp,
  278. st->time_base.den * (int64_t)ref_st->time_base.num,
  279. st->time_base.num * (int64_t)ref_st->time_base.den);
  280. }
  281. }
  282. FFMPEG_INLINE
  283. void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
  284. {
  285. my_update_cur_dts(s, ref_st, timestamp);
  286. }
  287. #endif
  288. #if ((LIBAVCODEC_VERSION_MAJOR < 54) || \
  289. (LIBAVCODEC_VERSION_MAJOR == 54 && LIBAVCODEC_VERSION_MINOR < 28))
  290. FFMPEG_INLINE
  291. void avcodec_free_frame(AVFrame **frame)
  292. {
  293. /* don't need to do anything with old AVFrame
  294. * since it does not have malloced members */
  295. (void)frame;
  296. }
  297. #endif
  298. #if ((LIBAVCODEC_VERSION_MAJOR > 54) || \
  299. (LIBAVCODEC_VERSION_MAJOR >= 54) && (LIBAVCODEC_VERSION_MINOR >= 13))
  300. # define FFMPEG_HAVE_AVFRAME_SAMPLE_RATE
  301. #endif
  302. #if ((LIBAVCODEC_VERSION_MAJOR > 54) || \
  303. (LIBAVCODEC_VERSION_MAJOR == 54 && LIBAVCODEC_VERSION_MINOR >= 13))
  304. # define FFMPEG_HAVE_FRAME_CHANNEL_LAYOUT
  305. #endif
  306. #ifndef FFMPEG_HAVE_AVIO
  307. # define AVIO_FLAG_WRITE URL_WRONLY
  308. # define avio_open url_fopen
  309. # define avio_tell url_ftell
  310. # define avio_close url_fclose
  311. # define avio_size url_fsize
  312. #endif
  313. /* there are some version inbetween, which have avio_... functions but no
  314. * AVIO_FLAG_... */
  315. #ifndef AVIO_FLAG_WRITE
  316. # define AVIO_FLAG_WRITE URL_WRONLY
  317. #endif
  318. #ifndef AV_PKT_FLAG_KEY
  319. # define AV_PKT_FLAG_KEY PKT_FLAG_KEY
  320. #endif
  321. #ifndef FFMPEG_HAVE_AV_DUMP_FORMAT
  322. # define av_dump_format dump_format
  323. #endif
  324. #ifndef FFMPEG_HAVE_AV_GUESS_FORMAT
  325. # define av_guess_format guess_format
  326. #endif
  327. #ifndef FFMPEG_HAVE_PARSE_UTILS
  328. # define av_parse_video_rate av_parse_video_frame_rate
  329. #endif
  330. #ifdef FFMPEG_HAVE_DEFAULT_VAL_UNION
  331. # define FFMPEG_DEF_OPT_VAL_INT(OPT) OPT->default_val.i64
  332. # define FFMPEG_DEF_OPT_VAL_DOUBLE(OPT) OPT->default_val.dbl
  333. #else
  334. # define FFMPEG_DEF_OPT_VAL_INT(OPT) OPT->default_val
  335. # define FFMPEG_DEF_OPT_VAL_DOUBLE(OPT) OPT->default_val
  336. #endif
  337. #ifndef FFMPEG_HAVE_AVMEDIA_TYPES
  338. # define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
  339. # define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
  340. #endif
  341. #ifndef FFMPEG_HAVE_DECODE_AUDIO3
  342. FFMPEG_INLINE
  343. int avcodec_decode_audio3(AVCodecContext *avctx,
  344. int16_t *samples,
  345. int *frame_size_ptr,
  346. AVPacket *avpkt)
  347. {
  348. return avcodec_decode_audio2(avctx, samples, frame_size_ptr, avpkt->data, avpkt->size);
  349. }
  350. #endif
  351. #ifndef FFMPEG_HAVE_DECODE_VIDEO2
  352. FFMPEG_INLINE
  353. int avcodec_decode_video2(AVCodecContext *avctx,
  354. AVFrame *picture,
  355. int *got_picture_ptr,
  356. AVPacket *avpkt)
  357. {
  358. return avcodec_decode_video(avctx, picture, got_picture_ptr, avpkt->data, avpkt->size);
  359. }
  360. #endif
  361. FFMPEG_INLINE
  362. int64_t av_get_pts_from_frame(AVFormatContext *avctx, AVFrame *picture)
  363. {
  364. int64_t pts;
  365. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 34, 100)
  366. pts = picture->pts;
  367. #else
  368. pts = picture->pkt_pts;
  369. #endif
  370. if (pts == AV_NOPTS_VALUE) {
  371. pts = picture->pkt_dts;
  372. }
  373. if (pts == AV_NOPTS_VALUE) {
  374. pts = 0;
  375. }
  376. (void)avctx;
  377. return pts;
  378. }
  379. /* obsolete constant formerly defined in FFMpeg libavcodec/avcodec.h */
  380. #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
  381. # define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
  382. #endif
  383. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 1, 0)
  384. FFMPEG_INLINE
  385. int avcodec_encode_video2(AVCodecContext *avctx,
  386. AVPacket *pkt,
  387. const AVFrame *frame,
  388. int *got_output)
  389. {
  390. int outsize, ret;
  391. ret = av_new_packet(pkt, avctx->width * avctx->height * 7 + 10000);
  392. if (ret < 0)
  393. return ret;
  394. outsize = avcodec_encode_video(avctx, pkt->data, pkt->size, frame);
  395. if (outsize <= 0) {
  396. *got_output = 0;
  397. av_free_packet(pkt);
  398. }
  399. else {
  400. *got_output = 1;
  401. av_shrink_packet(pkt, outsize);
  402. if (avctx->coded_frame) {
  403. pkt->pts = avctx->coded_frame->pts;
  404. if (avctx->coded_frame->key_frame)
  405. pkt->flags |= AV_PKT_FLAG_KEY;
  406. }
  407. }
  408. return outsize >= 0 ? 0 : outsize;
  409. }
  410. #endif
  411. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 17, 0)
  412. FFMPEG_INLINE
  413. void avformat_close_input(AVFormatContext **ctx)
  414. {
  415. av_close_input_file(*ctx);
  416. *ctx = NULL;
  417. }
  418. #endif
  419. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 8, 0)
  420. FFMPEG_INLINE
  421. AVFrame *av_frame_alloc(void)
  422. {
  423. return avcodec_alloc_frame();
  424. }
  425. FFMPEG_INLINE
  426. void av_frame_free(AVFrame **frame)
  427. {
  428. av_freep(frame);
  429. }
  430. #endif
  431. FFMPEG_INLINE
  432. const char *av_get_metadata_key_value(AVDictionary *metadata, const char *key)
  433. {
  434. if (metadata == NULL) {
  435. return NULL;
  436. }
  437. AVDictionaryEntry *tag = NULL;
  438. while ((tag = av_dict_get(metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  439. if (!strcmp(tag->key, key)) {
  440. return tag->value;
  441. }
  442. }
  443. return NULL;
  444. }
  445. FFMPEG_INLINE
  446. bool av_check_encoded_with_ffmpeg(AVFormatContext *ctx)
  447. {
  448. const char *encoder = av_get_metadata_key_value(ctx->metadata, "ENCODER");
  449. if (encoder != NULL && !strncmp(encoder, "Lavf", 4)) {
  450. return true;
  451. }
  452. return false;
  453. }
  454. FFMPEG_INLINE
  455. AVRational av_get_r_frame_rate_compat(AVFormatContext *ctx, const AVStream *stream)
  456. {
  457. /* If the video is encoded with FFmpeg and we are decoding with FFmpeg
  458. * as well it seems to be more reliable to use r_frame_rate (tbr).
  459. *
  460. * For other cases we fall back to avg_frame_rate (fps) when possible.
  461. */
  462. #ifdef AV_USING_FFMPEG
  463. if (av_check_encoded_with_ffmpeg(ctx)) {
  464. return stream->r_frame_rate;
  465. }
  466. #endif
  467. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 23, 1)
  468. /* For until r_frame_rate was deprecated use it. */
  469. return stream->r_frame_rate;
  470. #else
  471. # ifdef AV_USING_FFMPEG
  472. /* Some of the videos might have average frame rate set to, while the
  473. * r_frame_rate will show a correct value. This happens, for example, for
  474. * OGG video files saved with Blender. */
  475. if (stream->avg_frame_rate.den == 0) {
  476. return stream->r_frame_rate;
  477. }
  478. # endif
  479. return stream->avg_frame_rate;
  480. #endif
  481. }
  482. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(51, 32, 0)
  483. # define AV_OPT_SEARCH_FAKE_OBJ 0
  484. #endif
  485. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
  486. # define FFMPEG_HAVE_DEPRECATED_FLAGS2
  487. #endif
  488. /* Since FFmpeg-1.1 this constant have AV_ prefix. */
  489. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 3, 100)
  490. # define AV_PIX_FMT_BGR32 PIX_FMT_BGR32
  491. # define AV_PIX_FMT_YUV422P PIX_FMT_YUV422P
  492. # define AV_PIX_FMT_BGRA PIX_FMT_BGRA
  493. # define AV_PIX_FMT_ARGB PIX_FMT_ARGB
  494. # define AV_PIX_FMT_RGBA PIX_FMT_RGBA
  495. #endif
  496. /* New API from FFmpeg-2.0 which soon became recommended one. */
  497. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 38, 100)
  498. # define av_frame_alloc avcodec_alloc_frame
  499. # define av_frame_free avcodec_free_frame
  500. # define av_frame_unref avcodec_get_frame_defaults
  501. #endif
  502. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 24, 102)
  503. /* NOTE: The code in this block are from FFmpeg 2.6.4, which is licensed by LGPL. */
  504. # define MAX_NEG_CROP 1024
  505. # define times4(x) x, x, x, x
  506. # define times256(x) times4(times4(times4(times4(times4(x)))))
  507. static const uint8_t ff_compat_crop_tab[256 + 2 * MAX_NEG_CROP] = {
  508. times256(0x00), 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
  509. 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  510. 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22,
  511. 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
  512. 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
  513. 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
  514. 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52,
  515. 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E,
  516. 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  517. 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
  518. 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82,
  519. 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E,
  520. 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
  521. 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
  522. 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2,
  523. 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE,
  524. 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
  525. 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
  526. 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2,
  527. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE,
  528. 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
  529. 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, times256(0xFF)};
  530. # undef times4
  531. # undef times256
  532. /* filter parameters: [-1 4 2 4 -1] // 8 */
  533. FFMPEG_INLINE
  534. void deinterlace_line(uint8_t *dst,
  535. const uint8_t *lum_m4,
  536. const uint8_t *lum_m3,
  537. const uint8_t *lum_m2,
  538. const uint8_t *lum_m1,
  539. const uint8_t *lum,
  540. int size)
  541. {
  542. const uint8_t *cm = ff_compat_crop_tab + MAX_NEG_CROP;
  543. int sum;
  544. for (; size > 0; size--) {
  545. sum = -lum_m4[0];
  546. sum += lum_m3[0] << 2;
  547. sum += lum_m2[0] << 1;
  548. sum += lum_m1[0] << 2;
  549. sum += -lum[0];
  550. dst[0] = cm[(sum + 4) >> 3];
  551. lum_m4++;
  552. lum_m3++;
  553. lum_m2++;
  554. lum_m1++;
  555. lum++;
  556. dst++;
  557. }
  558. }
  559. FFMPEG_INLINE
  560. void deinterlace_line_inplace(
  561. uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum, int size)
  562. {
  563. const uint8_t *cm = ff_compat_crop_tab + MAX_NEG_CROP;
  564. int sum;
  565. for (; size > 0; size--) {
  566. sum = -lum_m4[0];
  567. sum += lum_m3[0] << 2;
  568. sum += lum_m2[0] << 1;
  569. lum_m4[0] = lum_m2[0];
  570. sum += lum_m1[0] << 2;
  571. sum += -lum[0];
  572. lum_m2[0] = cm[(sum + 4) >> 3];
  573. lum_m4++;
  574. lum_m3++;
  575. lum_m2++;
  576. lum_m1++;
  577. lum++;
  578. }
  579. }
  580. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  581. top field is copied as is, but the bottom field is deinterlaced
  582. against the top field. */
  583. FFMPEG_INLINE
  584. void deinterlace_bottom_field(
  585. uint8_t *dst, int dst_wrap, const uint8_t *src1, int src_wrap, int width, int height)
  586. {
  587. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  588. int y;
  589. src_m2 = src1;
  590. src_m1 = src1;
  591. src_0 = &src_m1[src_wrap];
  592. src_p1 = &src_0[src_wrap];
  593. src_p2 = &src_p1[src_wrap];
  594. for (y = 0; y < (height - 2); y += 2) {
  595. memcpy(dst, src_m1, width);
  596. dst += dst_wrap;
  597. deinterlace_line(dst, src_m2, src_m1, src_0, src_p1, src_p2, width);
  598. src_m2 = src_0;
  599. src_m1 = src_p1;
  600. src_0 = src_p2;
  601. src_p1 += 2 * src_wrap;
  602. src_p2 += 2 * src_wrap;
  603. dst += dst_wrap;
  604. }
  605. memcpy(dst, src_m1, width);
  606. dst += dst_wrap;
  607. /* do last line */
  608. deinterlace_line(dst, src_m2, src_m1, src_0, src_0, src_0, width);
  609. }
  610. FFMPEG_INLINE
  611. int deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap, int width, int height)
  612. {
  613. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  614. int y;
  615. uint8_t *buf = (uint8_t *)av_malloc(width);
  616. if (!buf)
  617. return AVERROR(ENOMEM);
  618. src_m1 = src1;
  619. memcpy(buf, src_m1, width);
  620. src_0 = &src_m1[src_wrap];
  621. src_p1 = &src_0[src_wrap];
  622. src_p2 = &src_p1[src_wrap];
  623. for (y = 0; y < (height - 2); y += 2) {
  624. deinterlace_line_inplace(buf, src_m1, src_0, src_p1, src_p2, width);
  625. src_m1 = src_p1;
  626. src_0 = src_p2;
  627. src_p1 += 2 * src_wrap;
  628. src_p2 += 2 * src_wrap;
  629. }
  630. /* do last line */
  631. deinterlace_line_inplace(buf, src_m1, src_0, src_0, src_0, width);
  632. av_free(buf);
  633. return 0;
  634. }
  635. # ifdef __GNUC__
  636. # pragma GCC diagnostic push
  637. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  638. # endif
  639. FFMPEG_INLINE
  640. int avpicture_deinterlace(
  641. AVPicture *dst, const AVPicture *src, enum AVPixelFormat pix_fmt, int width, int height)
  642. {
  643. int i, ret;
  644. if (pix_fmt != AV_PIX_FMT_YUV420P && pix_fmt != AV_PIX_FMT_YUVJ420P &&
  645. pix_fmt != AV_PIX_FMT_YUV422P && pix_fmt != AV_PIX_FMT_YUVJ422P &&
  646. pix_fmt != AV_PIX_FMT_YUV444P && pix_fmt != AV_PIX_FMT_YUV411P &&
  647. pix_fmt != AV_PIX_FMT_GRAY8)
  648. return -1;
  649. if ((width & 3) != 0 || (height & 3) != 0)
  650. return -1;
  651. for (i = 0; i < 3; i++) {
  652. if (i == 1) {
  653. switch (pix_fmt) {
  654. case AV_PIX_FMT_YUVJ420P:
  655. case AV_PIX_FMT_YUV420P:
  656. width >>= 1;
  657. height >>= 1;
  658. break;
  659. case AV_PIX_FMT_YUV422P:
  660. case AV_PIX_FMT_YUVJ422P:
  661. width >>= 1;
  662. break;
  663. case AV_PIX_FMT_YUV411P:
  664. width >>= 2;
  665. break;
  666. default:
  667. break;
  668. }
  669. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  670. break;
  671. }
  672. }
  673. if (src == dst) {
  674. ret = deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i], width, height);
  675. if (ret < 0)
  676. return ret;
  677. }
  678. else {
  679. deinterlace_bottom_field(
  680. dst->data[i], dst->linesize[i], src->data[i], src->linesize[i], width, height);
  681. }
  682. }
  683. return 0;
  684. }
  685. # ifdef __GNUC__
  686. # pragma GCC diagnostic pop
  687. # endif
  688. #endif
  689. #endif