24 #include <linux/videodev2.h> 25 #include <sys/ioctl.h> 37 #define MPEG_CID(x) V4L2_CID_MPEG_VIDEO_##x 38 #define MPEG_VIDEO(x) V4L2_MPEG_VIDEO_##x 42 struct v4l2_streamparm parm = { 0 };
44 parm.type = V4L2_TYPE_IS_MULTIPLANAR(s->
output.
type) ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT;
45 parm.parm.output.timeperframe.denominator = den;
46 parm.parm.output.timeperframe.numerator = num;
48 if (ioctl(s->
fd, VIDIOC_S_PARM, &parm) < 0)
54 struct v4l2_ext_controls ctrls = { { 0 } };
55 struct v4l2_ext_control ctrl = { 0 };
58 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
59 ctrls.controls = &ctrl;
66 if (ioctl(s->
fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
68 "Failed to set %s: %s\n", name, strerror(errno));
70 av_log(s->
avctx, AV_LOG_DEBUG,
"Encoder: %s = %d\n", name, value);
75 struct v4l2_ext_controls ctrls = { { 0 } };
76 struct v4l2_ext_control ctrl = { 0 };
80 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
81 ctrls.controls = &ctrl;
87 ret = ioctl(s->
fd, VIDIOC_G_EXT_CTRLS, &ctrls);
90 "Failed to get %s\n", name);
101 static const struct h264_profile {
102 unsigned int ffmpeg_val;
103 unsigned int v4l2_val;
120 if (
profile[i].ffmpeg_val == p)
128 static const struct mpeg4_profile {
129 unsigned int ffmpeg_val;
130 unsigned int v4l2_val;
141 if (
profile[i].ffmpeg_val == p)
164 struct v4l2_event_subscription sub;
166 memset(&sub, 0,
sizeof(sub));
167 sub.type = V4L2_EVENT_EOS;
168 if (ioctl(s->
fd, VIDIOC_SUBSCRIBE_EVENT, &sub) < 0)
170 "the v4l2 driver does not support end of stream VIDIOC_SUBSCRIBE_EVENT\n");
176 int qmin_cid, qmax_cid, qmin, qmax;
201 "Encoder Context: id (%d), profile (%d), frame rate(%d/%d), number b-frames (%d), " 202 "gop size (%d), bit rate (%"PRId64
"), qmin (%d), qmax (%d)\n",
259 "exceed qmax\n", avctx->
qmin, avctx->
qmax);
261 qmin = avctx->
qmin >= 0 ? avctx->
qmin : qmin;
262 qmax = avctx->
qmax >= 0 ? avctx->
qmax : qmax;
278 #ifdef V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME 322 uint32_t v4l2_fmt_output;
351 if (V4L2_TYPE_IS_MULTIPLANAR(output->
type))
352 v4l2_fmt_output = output->
format.fmt.pix_mp.pixelformat;
354 v4l2_fmt_output = output->
format.fmt.pix.pixelformat;
357 if (pix_fmt_output != avctx->
pix_fmt) {
371 #define OFFSET(x) offsetof(V4L2m2mPriv, x) 372 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 374 #define V4L_M2M_CAPTURE_OPTS \ 375 V4L_M2M_DEFAULT_OPTS,\ 376 { "num_capture_buffers", "Number of buffers in the capture context", \ 377 OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 4 }, 4, INT_MAX, FLAGS } 396 #define M2MENC_CLASS(NAME, OPTIONS_NAME) \ 397 static const AVClass v4l2_m2m_ ## NAME ## _enc_class = { \ 398 .class_name = #NAME "_v4l2m2m_encoder", \ 399 .item_name = av_default_item_name, \ 400 .option = OPTIONS_NAME, \ 401 .version = LIBAVUTIL_VERSION_INT, \ 404 #define M2MENC(NAME, LONGNAME, OPTIONS_NAME, CODEC) \ 405 M2MENC_CLASS(NAME, OPTIONS_NAME) \ 406 AVCodec ff_ ## NAME ## _v4l2m2m_encoder = { \ 407 .name = #NAME "_v4l2m2m" , \ 408 .long_name = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " encoder wrapper"), \ 409 .type = AVMEDIA_TYPE_VIDEO, \ 411 .priv_data_size = sizeof(V4L2m2mPriv), \ 412 .priv_class = &v4l2_m2m_ ## NAME ##_enc_class, \ 413 .init = v4l2_encode_init, \ 414 .send_frame = v4l2_send_frame, \ 415 .receive_packet = v4l2_receive_packet, \ 416 .close = v4l2_encode_close, \ 417 .defaults = v4l2_m2m_defaults, \ 418 .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY, \ 419 .wrapper_name = "v4l2m2m", \ #define FF_PROFILE_H264_MAIN
enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
#define FF_PROFILE_MPEG4_SIMPLE
static const AVOption options[]
static const AVCodecDefault v4l2_m2m_defaults[]
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
static int v4l2_get_ext_ctrl(V4L2m2mContext *s, unsigned int id, signed int *value, const char *name, int log_warning)
static const AVOption mpeg4_options[]
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
static int v4l2_mpeg4_profile_from_ff(int p)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static void v4l2_set_ext_ctrl(V4L2m2mContext *s, unsigned int id, signed int value, const char *name, int log_warning)
int width
Width and height of the frames it produces (in case of a capture context, e.g.
#define FF_PROFILE_MPEG4_SIMPLE_SCALABLE
int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv)
Releases all the codec resources if all AVBufferRefs have been returned to the ctx.
int ff_v4l2_context_dequeue_packet(V4L2Context *ctx, AVPacket *pkt)
Dequeues a buffer from a V4L2Context to an AVPacket.
static int v4l2_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
static av_cold int v4l2_encode_init(AVCodecContext *avctx)
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv)
Probes the video nodes looking for the required codec capabilities.
enum AVCodecID av_codec_id
AVCodecID corresponding to this buffer context.
#define FF_PROFILE_H264_BASELINE
int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s)
Allocate a new context and references for a V4L2 M2M instance.
#define FF_PROFILE_MPEG4_CORE
static int v4l2_send_frame(AVCodecContext *avctx, const AVFrame *frame)
static void v4l2_set_timeperframe(V4L2m2mContext *s, unsigned int num, unsigned int den)
#define FF_PROFILE_H264_EXTENDED
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_cold int v4l2_encode_close(AVCodecContext *avctx)
int qmax
maximum quantizer
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define FF_PROFILE_H264_HIGH_422
int flags
AV_CODEC_FLAG_*.
#define FF_PROFILE_H264_HIGH
enum AVPixelFormat av_pix_fmt
AVPixelFormat corresponding to this buffer context.
int streamon
Whether the stream has been started (VIDIOC_STREAMON has been sent).
enum AVPictureType pict_type
Picture type of the frame.
static int v4l2_check_b_frame_support(V4L2m2mContext *s)
int width
picture width / height.
#define FF_PROFILE_UNKNOWN
#define FF_MPEG4_PROFILE_OPTS
#define FF_ARRAY_ELEMS(a)
struct v4l2_format format
Format returned by the driver after initializing the buffer context.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int ff_v4l2_context_set_status(V4L2Context *ctx, uint32_t cmd)
Sets the status of a V4L2Context.
Libavcodec external API header.
static void v4l2_subscribe_eos_event(V4L2m2mContext *s)
#define FF_PROFILE_H264_HIGH_422_INTRA
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
main external API structure.
#define FF_PROFILE_MPEG4_ADVANCED_SIMPLE
int qmin
minimum quantizer
#define V4L_M2M_CAPTURE_OPTS
#define FF_PROFILE_H264_HIGH_10_INTRA
#define FF_PROFILE_MPEG4_ADVANCED_CODING
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define AV_CODEC_FLAG_QPEL
Use qpel MC.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
int ff_v4l2_context_enqueue_frame(V4L2Context *ctx, const AVFrame *frame)
Enqueues a buffer to a V4L2Context from an AVFrame.
common internal api header.
#define M2MENC(NAME, LONGNAME, OPTIONS_NAME, CODEC)
static int v4l2_prepare_encoder(V4L2m2mContext *s)
static unsigned int v4l2_h264_profile_from_ff(int p)
#define FF_PROFILE_H264_HIGH_444_INTRA
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
#define FF_PROFILE_H264_HIGH_10
AVPixelFormat
Pixel format.
static double val(void *priv, double ch)
This structure stores compressed data.
enum v4l2_buf_type type
Type of this buffer context.