Commit 1ef7752d authored by Jorge Ramirez-Ortiz's avatar Jorge Ramirez-Ortiz Committed by wm4

libavcodec: v4l2: add support for v4l2 mem2mem codecs

    This patchset enhances Alexis Ballier's original patch and validates
    it using Qualcomm's Venus hardware (driver recently landed upstream
    [1]).

    This has been tested on Qualcomm's DragonBoard 410c and 820c
    Configure/make scripts have been validated on Ubuntu 10.04 and
    16.04.

    Tested decoders:
           - h264
           - h263
           - mpeg4
           - vp8
           - vp9
           - hevc

    Tested encoders:
           - h264
           - h263
           - mpeg4

    Tested transcoding (concurrent encoding/decoding)

    Some of the changes introduced:
        - v4l2: code cleanup and abstractions added
        - v4l2: follow the new encode/decode api.
        - v4l2: fix display size for NV12 output pool.
        - v4l2: handle EOS (EPIPE and draining)
        - v4l2: vp8 and mpeg4 decoding and encoding.
        - v4l2: hevc and vp9 support.
        - v4l2: generate EOF on dequeue errors.
        - v4l2: h264_mp4toannexb filtering.
        - v4l2: fixed make install and fate issues.
        - v4l2: codecs enabled/disabled depending on pixfmt defined
        - v4l2: pass timebase/framerate to the context
        - v4l2: runtime decoder reconfiguration.
        - v4l2: add more frame information
        - v4l2: free hardware resources on last reference being released
        - v4l2: encoding: disable b-frames for upstreaming (patch required)

    [1] https://lwn.net/Articles/697956/

    System Level view:
        v42l_m2m_enc/dec --> v4l2_m2m --> v4l2_context --> v4l2_buffers
Reviewed-by: 's avatarJorge Ramirez <jorge.ramirez-ortiz@linaro.org>
Reviewed-by: 's avatarAlexis Ballier <aballier@gentoo.org>
Tested-by: 's avatarJorge Ramirez <jorge.ramirez-ortiz@linaro.org>
Signed-off-by: 's avatarwm4 <nfxjfg@googlemail.com>
parent afe67473
......@@ -49,6 +49,7 @@ version <next>:
- VP9 tile threading support
- KMS screen grabber
- CUDA thumbnail filter
- V4L2 mem2mem HW assisted codecs
version 3.3:
......
......@@ -185,6 +185,7 @@ Individual component options:
--enable-filter=NAME enable filter NAME
--disable-filter=NAME disable filter NAME
--disable-filters disable all filters
--disable-v4l2_m2m disable V4L2 mem2mem code [autodetect]
External library support:
......@@ -1628,6 +1629,7 @@ HWACCEL_AUTODETECT_LIBRARY_LIST="
vda
vdpau
videotoolbox
v4l2_m2m
xvmc
"
......@@ -2755,6 +2757,7 @@ omx_rpi_select="omx"
qsvdec_select="qsv"
qsvenc_select="qsv"
vaapi_encode_deps="vaapi"
v4l2_m2m_deps_any="linux_videodev2_h"
hwupload_cuda_filter_deps="cuda"
scale_npp_filter_deps="cuda libnpp"
......@@ -2765,6 +2768,8 @@ nvenc_deps="cuda"
nvenc_deps_any="dlopen LoadLibrary"
nvenc_encoder_deps="nvenc"
h263_v4l2m2m_decoder_deps="v4l2_m2m h263_v4l2_m2m"
h263_v4l2m2m_encoder_deps="v4l2_m2m h263_v4l2_m2m"
h264_crystalhd_decoder_select="crystalhd h264_mp4toannexb_bsf h264_parser"
h264_cuvid_decoder_deps="cuda cuvid"
h264_cuvid_decoder_select="h264_mp4toannexb_bsf"
......@@ -2783,6 +2788,8 @@ h264_vda_decoder_deps="vda"
h264_vda_decoder_select="h264_decoder"
h264_vdpau_decoder_deps="vdpau"
h264_vdpau_decoder_select="h264_decoder"
h264_v4l2m2m_decoder_deps="v4l2_m2m h264_v4l2_m2m"
h264_v4l2m2m_encoder_deps="v4l2_m2m h264_v4l2_m2m"
hevc_cuvid_decoder_deps="cuda cuvid"
hevc_cuvid_decoder_select="hevc_mp4toannexb_bsf"
hevc_mediacodec_decoder_deps="mediacodec"
......@@ -2794,12 +2801,15 @@ hevc_qsv_encoder_deps="libmfx"
hevc_qsv_encoder_select="hevcparse qsvenc"
hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC"
hevc_vaapi_encoder_select="vaapi_encode golomb"
hevc_v4l2m2m_decoder_deps="v4l2_m2m hevc_v4l2_m2m"
hevc_v4l2m2m_encoder_deps="v4l2_m2m hevc_v4l2_m2m"
mjpeg_cuvid_decoder_deps="cuda cuvid"
mjpeg_vaapi_encoder_deps="VAEncPictureParameterBufferJPEG"
mjpeg_vaapi_encoder_select="vaapi_encode jpegtables"
mpeg1_cuvid_decoder_deps="cuda cuvid"
mpeg1_vdpau_decoder_deps="vdpau"
mpeg1_vdpau_decoder_select="mpeg1video_decoder"
mpeg1_v4l2m2m_decoder_deps="v4l2_m2m mpeg1_v4l2_m2m"
mpeg2_crystalhd_decoder_select="crystalhd"
mpeg2_cuvid_decoder_deps="cuda cuvid"
mpeg2_mmal_decoder_deps="mmal"
......@@ -2810,6 +2820,7 @@ mpeg2_qsv_encoder_deps="libmfx"
mpeg2_qsv_encoder_select="qsvenc"
mpeg2_vaapi_encoder_deps="VAEncPictureParameterBufferMPEG2"
mpeg2_vaapi_encoder_select="vaapi_encode"
mpeg2_v4l2m2m_decoder_deps="v4l2_m2m mpeg2_v4l2_m2m"
mpeg4_crystalhd_decoder_select="crystalhd"
mpeg4_cuvid_decoder_deps="cuda cuvid"
mpeg4_mediacodec_decoder_deps="mediacodec"
......@@ -2817,6 +2828,8 @@ mpeg4_mmal_decoder_deps="mmal"
mpeg4_omx_encoder_deps="omx"
mpeg4_vdpau_decoder_deps="vdpau"
mpeg4_vdpau_decoder_select="mpeg4_decoder"
mpeg4_v4l2m2m_decoder_deps="v4l2_m2m mpeg4_v4l2_m2m"
mpeg4_v4l2m2m_encoder_deps="v4l2_m2m mpeg4_v4l2_m2m"
mpeg_vdpau_decoder_deps="vdpau"
mpeg_vdpau_decoder_select="mpeg2video_decoder"
msmpeg4_crystalhd_decoder_select="crystalhd"
......@@ -2827,16 +2840,20 @@ vc1_cuvid_decoder_deps="cuda cuvid"
vc1_mmal_decoder_deps="mmal"
vc1_vdpau_decoder_deps="vdpau"
vc1_vdpau_decoder_select="vc1_decoder"
vc1_v4l2m2m_decoder_deps="v4l2_m2m vc1_v4l2_m2m"
vp8_cuvid_decoder_deps="cuda cuvid"
vp8_mediacodec_decoder_deps="mediacodec"
vp8_qsv_decoder_deps="libmfx"
vp8_qsv_decoder_select="qsvdec vp8_qsv_hwaccel vp8_parser"
vp8_vaapi_encoder_deps="VAEncPictureParameterBufferVP8"
vp8_vaapi_encoder_select="vaapi_encode"
vp8_v4l2m2m_decoder_deps="v4l2_m2m vp8_v4l2_m2m"
vp8_v4l2m2m_encoder_deps="v4l2_m2m vp8_v4l2_m2m"
vp9_cuvid_decoder_deps="cuda cuvid"
vp9_mediacodec_decoder_deps="mediacodec"
vp9_vaapi_encoder_deps="VAEncPictureParameterBufferVP9"
vp9_vaapi_encoder_select="vaapi_encode"
vp9_v4l2m2m_decoder_deps="v4l2_m2m vp9_v4l2_m2m"
wmv3_crystalhd_decoder_select="crystalhd"
wmv3_vdpau_decoder_select="vc1_vdpau_decoder"
......@@ -6109,9 +6126,20 @@ perl -v > /dev/null 2>&1 && enable perl || disable perl
pod2man --help > /dev/null 2>&1 && enable pod2man || disable pod2man
rsync --help 2> /dev/null | grep -q 'contimeout' && enable rsync_contimeout || disable rsync_contimeout
# check V4L2 codecs available in the API
check_header linux/fb.h
check_header linux/videodev2.h
check_code cc linux/videodev2.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_safe struct_v4l2_frmivalenum_discrete
check_code cc linux/videodev2.h "int i = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_VIDEO_M2M | V4L2_BUF_FLAG_LAST;" || disable v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_VC1_ANNEX_G;" && enable vc1_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_MPEG1;" && enable mpeg1_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_MPEG2;" && enable mpeg2_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_MPEG4;" && enable mpeg4_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_HEVC;" && enable hevc_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_H263;" && enable h263_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_H264;" && enable h264_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_VP8;" && enable vp8_v4l2_m2m
check_code cc linux/videodev2.h "int i = V4L2_PIX_FMT_VP9;" && enable vp9_v4l2_m2m
check_header sys/videoio.h
check_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_safe struct_v4l2_frmivalenum_discrete
......
......@@ -137,6 +137,7 @@ OBJS-$(CONFIG_VIDEODSP) += videodsp.o
OBJS-$(CONFIG_VP3DSP) += vp3dsp.o
OBJS-$(CONFIG_VP56DSP) += vp56dsp.o
OBJS-$(CONFIG_VP8DSP) += vp8dsp.o
OBJS-$(CONFIG_V4L2_M2M) += v4l2_m2m.o v4l2_context.o v4l2_buffers.o v4l2_fmt.o
OBJS-$(CONFIG_WMA_FREQS) += wma_freqs.o
OBJS-$(CONFIG_WMV2DSP) += wmv2dsp.o
......@@ -323,6 +324,8 @@ OBJS-$(CONFIG_H263_DECODER) += h263dec.o h263.o ituh263dec.o \
intelh263dec.o h263data.o
OBJS-$(CONFIG_H263_ENCODER) += mpeg4videoenc.o mpeg4video.o \
h263.o ituh263enc.o flvenc.o h263data.o
OBJS-$(CONFIG_H263_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_H263_V4L2M2M_ENCODER) += v4l2_m2m_enc.o
OBJS-$(CONFIG_H264_DECODER) += h264dec.o h264_cabac.o h264_cavlc.o \
h264_direct.o h264_loopfilter.o \
h264_mb.o h264_picture.o \
......@@ -340,6 +343,8 @@ OBJS-$(CONFIG_H264_QSV_DECODER) += qsvdec_h2645.o
OBJS-$(CONFIG_H264_QSV_ENCODER) += qsvenc_h264.o
OBJS-$(CONFIG_H264_VAAPI_ENCODER) += vaapi_encode_h264.o vaapi_encode_h26x.o
OBJS-$(CONFIG_H264_VIDEOTOOLBOX_ENCODER) += videotoolboxenc.o
OBJS-$(CONFIG_H264_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_H264_V4L2M2M_ENCODER) += v4l2_m2m_enc.o
OBJS-$(CONFIG_HAP_DECODER) += hapdec.o hap.o
OBJS-$(CONFIG_HAP_ENCODER) += hapenc.o hap.o
OBJS-$(CONFIG_HEVC_DECODER) += hevcdec.o hevc_mvs.o \
......@@ -353,6 +358,8 @@ OBJS-$(CONFIG_HEVC_QSV_DECODER) += qsvdec_h2645.o
OBJS-$(CONFIG_HEVC_QSV_ENCODER) += qsvenc_hevc.o hevc_ps_enc.o \
hevc_data.o
OBJS-$(CONFIG_HEVC_VAAPI_ENCODER) += vaapi_encode_h265.o vaapi_encode_h26x.o
OBJS-$(CONFIG_HEVC_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_HEVC_V4L2M2M_ENCODER) += v4l2_m2m_enc.o
OBJS-$(CONFIG_HNM4_VIDEO_DECODER) += hnm4video.o
OBJS-$(CONFIG_HQ_HQA_DECODER) += hq_hqa.o hq_hqadata.o hq_hqadsp.o \
canopus.o
......@@ -422,6 +429,7 @@ OBJS-$(CONFIG_MPC8_DECODER) += mpc8.o mpc.o
OBJS-$(CONFIG_MPEGVIDEO_DECODER) += mpeg12dec.o mpeg12.o mpeg12data.o
OBJS-$(CONFIG_MPEG1VIDEO_DECODER) += mpeg12dec.o mpeg12.o mpeg12data.o
OBJS-$(CONFIG_MPEG1VIDEO_ENCODER) += mpeg12enc.o mpeg12.o
OBJS-$(CONFIG_MPEG1_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_MPEG2_MMAL_DECODER) += mmaldec.o
OBJS-$(CONFIG_MPEG2_QSV_DECODER) += qsvdec_other.o
OBJS-$(CONFIG_MPEG2_QSV_ENCODER) += qsvenc_mpeg2.o
......@@ -429,9 +437,12 @@ OBJS-$(CONFIG_MPEG2VIDEO_DECODER) += mpeg12dec.o mpeg12.o mpeg12data.o
OBJS-$(CONFIG_MPEG2VIDEO_ENCODER) += mpeg12enc.o mpeg12.o
OBJS-$(CONFIG_MPEG2_MEDIACODEC_DECODER) += mediacodecdec.o
OBJS-$(CONFIG_MPEG2_VAAPI_ENCODER) += vaapi_encode_mpeg2.o
OBJS-$(CONFIG_MPEG2_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_MPEG4_DECODER) += xvididct.o
OBJS-$(CONFIG_MPEG4_MEDIACODEC_DECODER) += mediacodecdec.o
OBJS-$(CONFIG_MPEG4_OMX_ENCODER) += omx.o
OBJS-$(CONFIG_MPEG4_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_MPEG4_V4L2M2M_ENCODER) += v4l2_m2m_enc.o
OBJS-$(CONFIG_MPL2_DECODER) += mpl2dec.o ass.o
OBJS-$(CONFIG_MSA1_DECODER) += mss3.o
OBJS-$(CONFIG_MSCC_DECODER) += mscc.o
......@@ -605,6 +616,7 @@ OBJS-$(CONFIG_VC1_DECODER) += vc1dec.o vc1_block.o vc1_loopfilter.o
OBJS-$(CONFIG_VC1_CUVID_DECODER) += cuvid.o
OBJS-$(CONFIG_VC1_MMAL_DECODER) += mmaldec.o
OBJS-$(CONFIG_VC1_QSV_DECODER) += qsvdec_other.o
OBJS-$(CONFIG_VC1_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_VC2_ENCODER) += vc2enc.o vc2enc_dwt.o diractab.o
OBJS-$(CONFIG_VCR1_DECODER) += vcr1.o
OBJS-$(CONFIG_VMDAUDIO_DECODER) += vmdaudio.o
......@@ -624,6 +636,8 @@ OBJS-$(CONFIG_VP8_CUVID_DECODER) += cuvid.o
OBJS-$(CONFIG_VP8_MEDIACODEC_DECODER) += mediacodecdec.o
OBJS-$(CONFIG_VP8_QSV_DECODER) += qsvdec_other.o
OBJS-$(CONFIG_VP8_VAAPI_ENCODER) += vaapi_encode_vp8.o
OBJS-$(CONFIG_VP8_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_VP8_V4L2M2M_ENCODER) += v4l2_m2m_enc.o
OBJS-$(CONFIG_VP9_DECODER) += vp9.o vp9data.o vp9dsp.o vp9lpf.o vp9recon.o \
vp9block.o vp9prob.o vp9mvs.o vp56rac.o \
vp9dsp_8bpp.o vp9dsp_10bpp.o vp9dsp_12bpp.o
......@@ -631,6 +645,7 @@ OBJS-$(CONFIG_VP9_CUVID_DECODER) += cuvid.o
OBJS-$(CONFIG_VP9_MEDIACODEC_DECODER) += mediacodecdec.o
OBJS-$(CONFIG_VP9_VAAPI_ENCODER) += vaapi_encode_vp9.o
OBJS-$(CONFIG_VPLAYER_DECODER) += textdec.o ass.o
OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o
OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o
OBJS-$(CONFIG_WAVPACK_DECODER) += wavpack.o
OBJS-$(CONFIG_WAVPACK_ENCODER) += wavpackenc.o
......
......@@ -208,8 +208,10 @@ static void register_all(void)
REGISTER_ENCDEC (H263, h263);
REGISTER_DECODER(H263I, h263i);
REGISTER_ENCDEC (H263P, h263p);
REGISTER_ENCDEC (H263_V4L2M2M, h263_v4l2m2m);
REGISTER_DECODER(H264, h264);
REGISTER_DECODER(H264_CRYSTALHD, h264_crystalhd);
REGISTER_ENCDEC (H264_V4L2M2M, h264_v4l2m2m);
REGISTER_DECODER(H264_MEDIACODEC, h264_mediacodec);
REGISTER_DECODER(H264_MMAL, h264_mmal);
REGISTER_DECODER(H264_QSV, h264_qsv);
......@@ -220,6 +222,7 @@ static void register_all(void)
REGISTER_ENCDEC (HAP, hap);
REGISTER_DECODER(HEVC, hevc);
REGISTER_DECODER(HEVC_QSV, hevc_qsv);
REGISTER_ENCDEC (HEVC_V4L2M2M, hevc_v4l2m2m);
REGISTER_DECODER(HNM4_VIDEO, hnm4_video);
REGISTER_DECODER(HQ_HQA, hq_hqa);
REGISTER_DECODER(HQX, hqx);
......@@ -254,6 +257,7 @@ static void register_all(void)
REGISTER_ENCDEC (MPEG2VIDEO, mpeg2video);
REGISTER_ENCDEC (MPEG4, mpeg4);
REGISTER_DECODER(MPEG4_CRYSTALHD, mpeg4_crystalhd);
REGISTER_ENCDEC (MPEG4_V4L2M2M, mpeg4_v4l2m2m);
REGISTER_DECODER(MPEG4_MMAL, mpeg4_mmal);
#if FF_API_VDPAU
REGISTER_DECODER(MPEG4_VDPAU, mpeg4_vdpau);
......@@ -263,8 +267,10 @@ static void register_all(void)
REGISTER_DECODER(MPEG_VDPAU, mpeg_vdpau);
REGISTER_DECODER(MPEG1_VDPAU, mpeg1_vdpau);
#endif
REGISTER_DECODER(MPEG1_V4L2M2M, mpeg1_v4l2m2m);
REGISTER_DECODER(MPEG2_MMAL, mpeg2_mmal);
REGISTER_DECODER(MPEG2_CRYSTALHD, mpeg2_crystalhd);
REGISTER_DECODER(MPEG2_V4L2M2M, mpeg2_v4l2m2m);
REGISTER_DECODER(MPEG2_QSV, mpeg2_qsv);
REGISTER_DECODER(MPEG2_MEDIACODEC, mpeg2_mediacodec);
REGISTER_DECODER(MSA1, msa1);
......@@ -362,6 +368,7 @@ static void register_all(void)
REGISTER_DECODER(VC1IMAGE, vc1image);
REGISTER_DECODER(VC1_MMAL, vc1_mmal);
REGISTER_DECODER(VC1_QSV, vc1_qsv);
REGISTER_DECODER(VC1_V4L2M2M, vc1_v4l2m2m);
REGISTER_ENCODER(VC2, vc2);
REGISTER_DECODER(VCR1, vcr1);
REGISTER_DECODER(VMDVIDEO, vmdvideo);
......@@ -373,7 +380,9 @@ static void register_all(void)
REGISTER_DECODER(VP6F, vp6f);
REGISTER_DECODER(VP7, vp7);
REGISTER_DECODER(VP8, vp8);
REGISTER_ENCDEC (VP8_V4L2M2M, vp8_v4l2m2m);
REGISTER_DECODER(VP9, vp9);
REGISTER_DECODER(VP9_V4L2M2M, vp9_v4l2m2m);
REGISTER_DECODER(VQA, vqa);
REGISTER_DECODER(BITPACKED, bitpacked);
REGISTER_DECODER(WEBP, webp);
......
This diff is collapsed.
/*
* V4L2 buffer helper functions.
*
* Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
* Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_V4L2_BUFFERS_H
#define AVCODEC_V4L2_BUFFERS_H
enum V4L2Buffer_status {
V4L2BUF_AVAILABLE,
V4L2BUF_IN_DRIVER,
V4L2BUF_RET_USER,
};
/**
* V4L2Buffer (wrapper for v4l2_buffer management)
*/
typedef struct V4L2Buffer {
/* each buffer needs to have a reference to its context */
struct V4L2Context *context;
/* keep track of the mmap address and mmap length */
struct V4L2Plane_info {
int bytesperline;
void * mm_addr;
size_t length;
} plane_info[VIDEO_MAX_PLANES];
int num_planes;
/* the v4l2_buffer buf.m.planes pointer uses the planes[] mem */
struct v4l2_buffer buf;
struct v4l2_plane planes[VIDEO_MAX_PLANES];
int flags;
enum V4L2Buffer_status status;
} V4L2Buffer;
/**
* Extracts the data from a V4L2Buffer to an AVFrame
*
* @param[in] frame The AVFRame to push the information to
* @param[in] buf The V4L2Buffer to get the information from
*
* @returns 0 in case of success, EINVAL if the number of planes is incorrect,
* ENOMEM if the AVBufferRef cant be created.
*/
int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *buf);
/**
* Extracts the data from a V4L2Buffer to an AVPacket
*
* @param[in] pkt The AVPacket to push the information to
* @param[in] buf The V4L2Buffer to get the information from
*
* @returns 0 in case of success, EINVAL if the number of planes is incorrect,
* ENOMEM if the AVBufferRef cant be created.
*
*/
int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *buf);
/**
* Extracts the data from an AVPacket to a V4L2Buffer
*
* @param[in] frame AVPacket to get the data from
* @param[in] avbuf V4L2Bfuffer to push the information to
*
* @returns 0 in case of success, negative otherwise
*/
int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out);
/**
* Extracts the data from an AVFrame to a V4L2Buffer
*
* @param[in] frame AVFrame to get the data from
* @param[in] avbuf V4L2Bfuffer to push the information to
*
* @returns 0 in case of success, negative otherwise
*/
int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer* out);
/**
* Initializes a V4L2Buffer
*
* @param[in] avbuf V4L2Bfuffer to initialize
* @param[in] index v4l2 buffer id
*
* @returns 0 in case of success, negative otherwise
*/
int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index);
/**
* Enqueues a V4L2Buffer
*
* @param[in] avbuf V4L2Bfuffer to push to the driver
*
* @returns 0 in case of success, negative otherwise
*/
int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf);
#endif // AVCODEC_V4L2_BUFFERS_H
This diff is collapsed.
/*
* V4L2 context helper functions.
*
* Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
* Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_V4L2_CONTEXT_H
#define AVCODEC_V4L2_CONTEXT_H
#include <stdatomic.h>
#include "libavcodec/avcodec.h"
#include "libavutil/pixfmt.h"
#include "libavutil/frame.h"
#include "libavutil/buffer.h"
#include "v4l2_buffers.h"
typedef struct V4L2Context {
/**
* context name.
*/
const char* name;
/**
* Type of this buffer context.
* See V4L2_BUF_TYPE_VIDEO_* in videodev2.h
* Readonly after init.
*/
enum v4l2_buf_type type;
/**
* AVPixelFormat corresponding to this buffer context.
* AV_PIX_FMT_NONE means this is an encoded stream.
*/
enum AVPixelFormat av_pix_fmt;
/**
* AVCodecID corresponding to this buffer context.
* AV_CODEC_ID_RAWVIDEO means this is a raw stream and av_pix_fmt must be set to a valid value.
*/
enum AVCodecID av_codec_id;
/**
* Format returned by the driver after initializing the buffer context.
* Readonly after init.
*/
struct v4l2_format format;
/**
* Width and height of the frames it produces (in case of a capture context, e.g. when decoding)
* or accepts (in case of an output context, e.g. when encoding).
*/
int width, height;
/**
* Indexed array of V4L2Buffers
*/
V4L2Buffer *buffers;
/**
* Readonly after init.
*/
int num_buffers;
/**
* Whether the stream has been started (VIDIOC_STREAMON has been sent).
*/
int streamon;
/**
* Either no more buffers available or an unrecoverable error was notified
* by the V4L2 kernel driver: once set the context has to be exited.
*/
int done;
} V4L2Context;
/**
* Initializes a V4L2Context.
*
* @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
* @return 0 in case of success, a negative value representing the error otherwise.
*/
int ff_v4l2_context_init(V4L2Context* ctx);
/**
* Sets the V4L2Context format in the v4l2 driver.
*
* @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
* @return 0 in case of success, a negative value representing the error otherwise.
*/
int ff_v4l2_context_set_format(V4L2Context* ctx);
/**
* Queries the driver for a valid v4l2 format and copies it to the context.
*
* @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
* @return 0 in case of success, a negative value representing the error otherwise.
*/
int ff_v4l2_context_get_format(V4L2Context* ctx);
/**
* Releases a V4L2Context.
*
* @param[in] ctx A pointer to a V4L2Context.
* The caller is reponsible for freeing it.
* It must not be used after calling this function.
*/
void ff_v4l2_context_release(V4L2Context* ctx);
/**
* Sets the status of a V4L2Context.
*
* @param[in] ctx A pointer to a V4L2Context.
* @param[in] cmd The status to set (VIDIOC_STREAMON or VIDIOC_STREAMOFF).
* Warning: If VIDIOC_STREAMOFF is sent to a buffer context that still has some frames buffered,
* those frames will be dropped.
* @return 0 in case of success, a negative value representing the error otherwise.
*/
int ff_v4l2_context_set_status(V4L2Context* ctx, int cmd);
/**
* Dequeues a buffer from a V4L2Context to an AVPacket.
*
* The pkt must be non NULL.
* @param[in] ctx The V4L2Context to dequeue from.
* @param[inout] pkt The AVPacket to dequeue to.
* @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error.
*/
int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt);
/**
* Dequeues a buffer from a V4L2Context to an AVFrame.
*
* The frame must be non NULL.
* @param[in] ctx The V4L2Context to dequeue from.
* @param[inout] f The AVFrame to dequeue to.
* @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error.
*/
int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* f);
/**
* Enqueues a buffer to a V4L2Context from an AVPacket
*
* The packet must be non NULL.
* When the size of the pkt is null, the buffer is not queued but a V4L2_DEC_CMD_STOP command is sent instead to the driver.
*
* @param[in] ctx The V4L2Context to enqueue to.
* @param[in] pkt A pointer to an AVPacket.
* @return 0 in case of success, a negative error otherwise.
*/
int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt);
/**
* Enqueues a buffer to a V4L2Context from an AVFrame
*
* The frame must be non NULL.
*
* @param[in] ctx The V4L2Context to enqueue to.
* @param[in] f A pointer to an AVFrame to enqueue.
* @return 0 in case of success, a negative error otherwise.
*/
int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* f);
#endif // AVCODEC_V4L2_CONTEXT_H
/*
* V4L2 format helper functions
*
* Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
* Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <linux/videodev2.h>
#include <search.h>
#include "v4l2_fmt.h"
#define V4L2_FMT(x) V4L2_PIX_FMT_##x
#define AV_CODEC(x) AV_CODEC_ID_##x
#define AV_FMT(x) AV_PIX_FMT_##x
static const struct fmt_conversion {
enum AVPixelFormat avfmt;
enum AVCodecID avcodec;
uint32_t v4l2_fmt;
} fmt_map[] = {
{ AV_FMT(RGB555LE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB555) },
{ AV_FMT(RGB555BE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB555X) },
{ AV_FMT(RGB565LE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB565) },
{ AV_FMT(RGB565BE), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB565X) },
{ AV_FMT(BGR24), AV_CODEC(RAWVIDEO), V4L2_FMT(BGR24) },
{ AV_FMT(RGB24), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB24) },
{ AV_FMT(BGR0), AV_CODEC(RAWVIDEO), V4L2_FMT(BGR32) },
{ AV_FMT(0RGB), AV_CODEC(RAWVIDEO), V4L2_FMT(RGB32) },
{ AV_FMT(GRAY8), AV_CODEC(RAWVIDEO), V4L2_FMT(GREY) },
{ AV_FMT(YUV420P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV420) },
{ AV_FMT(YUYV422), AV_CODEC(RAWVIDEO), V4L2_FMT(YUYV) },
{ AV_FMT(UYVY422), AV_CODEC(RAWVIDEO), V4L2_FMT(UYVY) },
{ AV_FMT(YUV422P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV422P) },
{ AV_FMT(YUV411P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV411P) },
{ AV_FMT(YUV410P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV410) },
{ AV_FMT(YUV410P), AV_CODEC(RAWVIDEO), V4L2_FMT(YVU410) },
{ AV_FMT(NV12), AV_CODEC(RAWVIDEO), V4L2_FMT(NV12) },
{ AV_FMT(NONE), AV_CODEC(MJPEG), V4L2_FMT(MJPEG) },
{ AV_FMT(NONE), AV_CODEC(MJPEG), V4L2_FMT(JPEG) },
#ifdef V4L2_PIX_FMT_SRGGB8
{ AV_FMT(BAYER_BGGR8), AV_CODEC(RAWVIDEO), V4L2_FMT(SBGGR8) },
{ AV_FMT(BAYER_GBRG8), AV_CODEC(RAWVIDEO), V4L2_FMT(SGBRG8) },
{ AV_FMT(BAYER_GRBG8), AV_CODEC(RAWVIDEO), V4L2_FMT(SGRBG8) },
{ AV_FMT(BAYER_RGGB8), AV_CODEC(RAWVIDEO), V4L2_FMT(SRGGB8) },
#endif
#ifdef V4L2_PIX_FMT_Y16
{ AV_FMT(GRAY16LE), AV_CODEC(RAWVIDEO), V4L2_FMT(Y16) },
#endif
#ifdef V4L2_PIX_FMT_NV12M
{ AV_FMT(NV12), AV_CODEC(RAWVIDEO), V4L2_FMT(NV12M) },
#endif
#ifdef V4L2_PIX_FMT_NV21M
{ AV_FMT(NV21), AV_CODEC(RAWVIDEO), V4L2_FMT(NV21M) },
#endif
#ifdef V4L2_PIX_FMT_YUV420M
{ AV_FMT(YUV420P), AV_CODEC(RAWVIDEO), V4L2_FMT(YUV420M) },
#endif
#ifdef V4L2_PIX_FMT_NV16M
{ AV_FMT(NV16), AV_CODEC(RAWVIDEO), V4L2_FMT(NV16M) },
#endif
#ifdef V4L2_PIX_FMT_H263
{ AV_FMT(NONE), AV_CODEC(H263), V4L2_FMT(H263) },
#endif
#ifdef V4L2_PIX_FMT_H264
{ AV_FMT(NONE), AV_CODEC(H264), V4L2_FMT(H264) },
#endif
#ifdef V4L2_PIX_FMT_MPEG4
{ AV_FMT(NONE), AV_CODEC(MPEG4), V4L2_FMT(MPEG4) },
#endif
#ifdef V4L2_PIX_FMT_CPIA1
{ AV_FMT(NONE), AV_CODEC(CPIA), V4L2_FMT(CPIA1) },
#endif
#ifdef V4L2_PIX_FMT_DV
{ AV_FMT(NONE), AV_CODEC(DVVIDEO), V4L2_FMT(DV) },
#endif
#ifdef V4L2_PIX_FMT_MPEG1
{ AV_FMT(NONE), AV_CODEC(MPEG1VIDEO), V4L2_FMT(MPEG1) },
#endif
#ifdef V4L2_PIX_FMT_MPEG2
{ AV_FMT(NONE), AV_CODEC(MPEG2VIDEO), V4L2_FMT(MPEG2) },
#endif
#ifdef V4L2_PIX_FMT_VP8
{ AV_FMT(NONE), AV_CODEC(VP8), V4L2_FMT(VP8) },
#endif
#ifdef V4L2_PIX_FMT_VP9
{ AV_FMT(NONE), AV_CODEC(VP9), V4L2_FMT(VP9) },
#endif
#ifdef V4L2_PIX_FMT_HEVC
{ AV_FMT(NONE), AV_CODEC(HEVC), V4L2_FMT(HEVC) },
#endif
#ifdef V4L2_PIX_FMT_VC1_ANNEX_G
{ AV_FMT(NONE), AV_CODEC(VC1), V4L2_FMT(VC1_ANNEX_G) },
#endif
};
static int match_codec(const void *a, const void *b)
{
if (*(enum AVCodecID *)a == ((struct fmt_conversion *)b)->avcodec)
return 0;
return 1;
}
uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec)
{
size_t len = FF_ARRAY_ELEMS(fmt_map);
struct fmt_conversion *item;
item = lfind(&avcodec, fmt_map, &len, sizeof(fmt_map[0]), match_codec);
if (item)
return item->v4l2_fmt;
return 0;
}
static int match_fmt(const void *a, const void *b)
{
if ( *(enum AVPixelFormat *)a == ((struct fmt_conversion *)b)->avfmt)
return 0;
return 1;
}
uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt)
{
size_t len = FF_ARRAY_ELEMS(fmt_map);
struct fmt_conversion *item;
item = lfind(&avfmt, fmt_map, &len, sizeof(fmt_map[0]), match_fmt);
if (item)
return item->v4l2_fmt;
return 0;
}
struct v4l2fmt_avcodec_pair {
enum AVCodecID avcodec;
uint32_t v4l2_fmt;
};
static int match_codecfmt(const void *a, const void *b)
{
struct v4l2fmt_avcodec_pair *key = (struct v4l2fmt_avcodec_pair *) a;
struct fmt_conversion *item = (struct fmt_conversion *) b;
if (key->avcodec == item->avcodec && key->v4l2_fmt == item->v4l2_fmt)
return 0;
return 1;
}
enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
{
struct v4l2fmt_avcodec_pair const key = {
.v4l2_fmt = v4l2_fmt,
.avcodec = avcodec,
};
size_t len = FF_ARRAY_ELEMS(fmt_map);
struct fmt_conversion *item;
item = lfind(&key, fmt_map, &len, sizeof(fmt_map[0]), match_codecfmt);
if (item)
return item->avfmt;
return AV_PIX_FMT_NONE;
}
/*
* V4L2 format helper functions
*
* Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
* Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_V4L2_FMT_H
#define AVCODEC_V4L2_FMT_H
#include "libavcodec/avcodec.h"
#include "libavutil/pixfmt.h"
enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec);
uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec);
uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt);
#endif /* AVCODEC_V4L2_FMT_H*/
This diff is collapsed.
/*
* V4L2 mem2mem helper functions
*
* Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
* Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_V4L2_M2M_H
#define AVCODEC_V4L2_M2M_H
#include <semaphore.h>
#include <unistd.h>
#include <dirent.h>
#include "libavcodec/avcodec.h"
#include "v4l2_context.h"
#define container_of(ptr, type, member) ({ \
const __typeof__(((type *)0)->member ) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type,member) );})
#define V4L_M2M_DEFAULT_OPTS \
{ "num_output_buffers", "Number of buffers in the output context",\
OFFSET(output.num_buffers), AV_OPT_TYPE_INT, { .i64 = 16 }, 6, INT_MAX, FLAGS }
typedef struct V4L2m2mContext
{
AVClass *class;
char devname[PATH_MAX];
int fd;
/* the codec context queues */
V4L2Context capture;
V4L2Context output;
/* refcount of buffers held by the user */
atomic_uint refcount;
/* dynamic stream reconfig */
AVCodecContext *avctx;
sem_t refsync;
int reinit;
/* null frame/packet received */
int draining;
} V4L2m2mContext;
/**
* Probes the video nodes looking for the required codec capabilities.
*
* @param[in] ctx The AVCodecContext instantiated by the encoder/decoder.
*
* @returns 0 if a driver is found, a negative number otherwise.
*/
int ff_v4l2_m2m_codec_init(AVCodecContext *avctx);
/**
* Releases all the codec resources if all AVBufferRefs have been returned to the
* ctx. Otherwise keep the driver open.
*
* @param[in] The AVCodecContext instantiated by the encoder/decoder.
*
* @returns 0
*
*/
int ff_v4l2_m2m_codec_end(AVCodecContext *avctx);
/**
* Reinitializes the V4L2m2mContext when the driver cant continue processing
* with the capture parameters.
*
* @param[in] ctx The V4L2m2mContext instantiated by the encoder/decoder.
*
* @returns 0 in case of success, negative number otherwise
*/
int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *ctx);
/**
* Reinitializes the V4L2m2mContext when the driver cant continue processing
* with the any of the current V4L2Contexts (ie, changes in output and capture).
*
* @param[in] ctx The V4L2m2mContext instantiated by the encoder/decoder.
*
* @returns 0 in case of success, negative number otherwise
*/
int ff_v4l2_m2m_codec_full_reinit(V4L2m2mContext *ctx);
#endif /* AVCODEC_V4L2_M2M_H */
/*
* V4L2 mem2mem decoders
*
* Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
* Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include "libavutil/pixfmt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/decode.h"
#include "v4l2_context.h"
#include "v4l2_m2m.h"
#include "v4l2_fmt.h"
static int v4l2_try_start(AVCodecContext *avctx)
{
V4L2m2mContext *s = avctx->priv_data;
V4L2Context *const capture = &s->capture;
V4L2Context *const output = &s->output;
struct v4l2_selection selection;
int ret;
/* 1. start the output process */
if (!output->streamon) {
ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
if (ret < 0) {
av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON on output context\n");
return ret;
}
}
if (capture->streamon)
return 0;
/* 2. get the capture format */
capture->format.type = capture->type;
ret = ioctl(s->fd, VIDIOC_G_FMT, &capture->format);
if (ret) {
av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_FMT ioctl\n");
return ret;
}
/* 2.1 update the AVCodecContext */
avctx->pix_fmt = ff_v4l2_format_v4l2_to_avfmt(capture->format.fmt.pix_mp.pixelformat, AV_CODEC_ID_RAWVIDEO);
capture->av_pix_fmt = avctx->pix_fmt;
/* 3. set the crop parameters */
selection.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
selection.r.height = avctx->coded_height;
selection.r.width = avctx->coded_width;
ret = ioctl(s->fd, VIDIOC_S_SELECTION, &selection);
if (!ret) {
ret = ioctl(s->fd, VIDIOC_G_SELECTION, &selection);
if (ret) {
av_log(avctx, AV_LOG_WARNING, "VIDIOC_G_SELECTION ioctl\n");
} else {
av_log(avctx, AV_LOG_DEBUG, "crop output %dx%d\n", selection.r.width, selection.r.height);
/* update the size of the resulting frame */
capture->height = selection.r.height;
capture->width = selection.r.width;
}
}
/* 4. init the capture context now that we have the capture format */
if (!capture->buffers) {
ret = ff_v4l2_context_init(capture);
if (ret) {
av_log(avctx, AV_LOG_DEBUG, "can't request output buffers\n");
return ret;
}
}
/* 5. start the capture process */
ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON);
if (ret) {
av_log(avctx, AV_LOG_DEBUG, "VIDIOC_STREAMON, on capture context\n");
return ret;
}
return 0;
}
static int v4l2_prepare_decoder(V4L2m2mContext *s)
{
struct v4l2_event_subscription sub;
V4L2Context *output = &s->output;
int ret;
/**
* requirements
*/
memset(&sub, 0, sizeof(sub));
sub.type = V4L2_EVENT_SOURCE_CHANGE;
ret = ioctl(s->fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
if ( ret < 0) {
if (output->height == 0 || output->width == 0) {
av_log(s->avctx, AV_LOG_ERROR,
"the v4l2 driver does not support VIDIOC_SUBSCRIBE_EVENT\n"
"you must provide codec_height and codec_width on input\n");
return ret;
}
}
return 0;
}
static int v4l2_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
V4L2m2mContext *s = avctx->priv_data;
V4L2Context *const capture = &s->capture;
V4L2Context *const output = &s->output;
AVPacket avpkt = {0};
int ret;
ret = ff_decode_get_packet(avctx, &avpkt);
if (ret < 0 && ret != AVERROR_EOF)
return ret;
if (s->draining)
goto dequeue;
ret = ff_v4l2_context_enqueue_packet(output, &avpkt);
if (ret < 0) {
if (ret != AVERROR(ENOMEM))
return ret;
/* no input buffers available, continue dequeing */
}
if (avpkt.size) {
ret = v4l2_try_start(avctx);
if (ret)
return 0;
}
dequeue:
return ff_v4l2_context_dequeue_frame(capture, frame);
}
static av_cold int v4l2_decode_init(AVCodecContext *avctx)
{
V4L2m2mContext *s = avctx->priv_data;
V4L2Context *capture = &s->capture;
V4L2Context *output = &s->output;
int ret;
/* if these dimensions are invalid (ie, 0 or too small) an event will be raised
* by the v4l2 driver; this event will trigger a full pipeline reconfig and
* the proper values will be retrieved from the kernel driver.
*/
output->height = capture->height = avctx->coded_height;
output->width = capture->width = avctx->coded_width;
output->av_codec_id = avctx->codec_id;
output->av_pix_fmt = AV_PIX_FMT_NONE;
capture->av_codec_id = AV_CODEC_ID_RAWVIDEO;
capture->av_pix_fmt = avctx->pix_fmt;
ret = ff_v4l2_m2m_codec_init(avctx);
if (ret) {
av_log(avctx, AV_LOG_ERROR, "can't configure decoder\n");
return ret;
}
return v4l2_prepare_decoder(s);
}
#define OFFSET(x) offsetof(V4L2m2mContext, x)
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
static const AVOption options[] = {
V4L_M2M_DEFAULT_OPTS,
{ "num_capture_buffers", "Number of buffers in the capture context",
OFFSET(capture.num_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 20, INT_MAX, FLAGS },
{ NULL},
};
#define M2MDEC(NAME, LONGNAME, CODEC, bsf_name) \
static const AVClass v4l2_m2m_ ## NAME ## _dec_class = {\
.class_name = #NAME "_v4l2_m2m_decoder",\
.item_name = av_default_item_name,\
.option = options,\
.version = LIBAVUTIL_VERSION_INT,\
};\
\
AVCodec ff_ ## NAME ## _v4l2m2m_decoder = { \
.name = #NAME "_v4l2m2m" ,\
.long_name = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " decoder wrapper"),\
.type = AVMEDIA_TYPE_VIDEO,\
.id = CODEC ,\
.priv_data_size = sizeof(V4L2m2mContext),\
.priv_class = &v4l2_m2m_ ## NAME ## _dec_class,\
.init = v4l2_decode_init,\
.receive_frame = v4l2_receive_frame,\
.close = ff_v4l2_m2m_codec_end,\
.bsfs = bsf_name, \
};
M2MDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb");
M2MDEC(hevc, "HEVC", AV_CODEC_ID_HEVC, "hevc_mp4toannexb");
M2MDEC(mpeg1, "MPEG1", AV_CODEC_ID_MPEG1VIDEO, NULL);
M2MDEC(mpeg2, "MPEG2", AV_CODEC_ID_MPEG2VIDEO, NULL);
M2MDEC(mpeg4, "MPEG4", AV_CODEC_ID_MPEG4, NULL);
M2MDEC(h263, "H.263", AV_CODEC_ID_H263, NULL);
M2MDEC(vc1 , "VC1", AV_CODEC_ID_VC1, NULL);
M2MDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL);
M2MDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL);
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment