1 /* GStreamer
2 * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifndef __GST_FFMPEG_UTILS_H__
21 #define __GST_FFMPEG_UTILS_H__
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <libavcodec/avcodec.h>
28 #include <libavutil/mathematics.h>
29
30 #include <gst/gst.h>
31
32 /* Introduced since ffmpeg version 4.3
33 *
34 * Note: Not all ffmpeg encoders seem to be reusable after flushing/draining.
35 * So if ffmpeg encoder doesn't support it, we should reopen encoding session.
36 *
37 * Before ffmpeg 4.3, avcodec_flush_buffers() was implemented in
38 * libavcodec/decodec.c but it was moved to libavcodec/utils.c and it would be
39 * accepted if encoder supports AV_CODEC_CAP_ENCODER_FLUSH flag.
40 * That implies that avcodec_flush_buffers() wasn't intended to be working
41 * properly for encoders.
42 */
43 #ifndef AV_CODEC_CAP_ENCODER_FLUSH
44 /*
45 * This encoder can be flushed using avcodec_flush_buffers(). If this flag is
46 * not set, the encoder must be closed and reopened to ensure that no frames
47 * remain pending.
48 */
49 #define AV_CODEC_CAP_ENCODER_FLUSH (1 << 21)
50 #endif
51
52 /*
53 *Get the size of an picture
54 */
55 int
56 gst_ffmpeg_avpicture_get_size (int pix_fmt, int width, int height);
57
58 /*
59 * Fill in pointers in an AVFrame, aligned by 4 (required by X).
60 */
61
62 int
63 gst_ffmpeg_avpicture_fill (AVFrame * picture,
64 uint8_t * ptr,
65 enum AVPixelFormat pix_fmt,
66 int width,
67 int height);
68
69 /*
70 * Convert from/to a GStreamer <-> FFMpeg timestamp.
71 */
72 static inline guint64
gst_ffmpeg_time_ff_to_gst(gint64 pts,AVRational base)73 gst_ffmpeg_time_ff_to_gst (gint64 pts, AVRational base)
74 {
75 guint64 out;
76
77 if (pts == AV_NOPTS_VALUE){
78 out = GST_CLOCK_TIME_NONE;
79 } else {
80 AVRational bq = { 1, GST_SECOND };
81 out = av_rescale_q (pts, base, bq);
82 }
83
84 return out;
85 }
86
87 static inline gint64
gst_ffmpeg_time_gst_to_ff(guint64 time,AVRational base)88 gst_ffmpeg_time_gst_to_ff (guint64 time, AVRational base)
89 {
90 gint64 out;
91
92 if (!GST_CLOCK_TIME_IS_VALID (time) || base.num == 0) {
93 out = AV_NOPTS_VALUE;
94 } else {
95 AVRational bq = { 1, GST_SECOND };
96 out = av_rescale_q (time, bq, base);
97 }
98
99 return out;
100 }
101
102 void
103 gst_ffmpeg_init_pix_fmt_info(void);
104
105 int
106 gst_ffmpeg_auto_max_threads(void);
107
108 const gchar *
109 gst_ffmpeg_get_codecid_longname (enum AVCodecID codec_id);
110
111 gint
112 av_smp_format_depth(enum AVSampleFormat smp_fmt);
113
114 GstBuffer *
115 new_aligned_buffer (gint size);
116
117 #endif /* __GST_FFMPEG_UTILS_H__ */
118