1cabdff1aSopenharmony_ci/* 2cabdff1aSopenharmony_ci * V4L2 context helper functions. 3cabdff1aSopenharmony_ci * 4cabdff1aSopenharmony_ci * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org> 5cabdff1aSopenharmony_ci * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org> 6cabdff1aSopenharmony_ci * 7cabdff1aSopenharmony_ci * This file is part of FFmpeg. 8cabdff1aSopenharmony_ci * 9cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or 10cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 11cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either 12cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 13cabdff1aSopenharmony_ci * 14cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful, 15cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 16cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17cabdff1aSopenharmony_ci * Lesser General Public License for more details. 18cabdff1aSopenharmony_ci * 19cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 20cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software 21cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22cabdff1aSopenharmony_ci */ 23cabdff1aSopenharmony_ci 24cabdff1aSopenharmony_ci#ifndef AVCODEC_V4L2_CONTEXT_H 25cabdff1aSopenharmony_ci#define AVCODEC_V4L2_CONTEXT_H 26cabdff1aSopenharmony_ci 27cabdff1aSopenharmony_ci#include <stdint.h> 28cabdff1aSopenharmony_ci#include <linux/videodev2.h> 29cabdff1aSopenharmony_ci 30cabdff1aSopenharmony_ci#include "libavutil/pixfmt.h" 31cabdff1aSopenharmony_ci#include "libavutil/frame.h" 32cabdff1aSopenharmony_ci#include "libavutil/rational.h" 33cabdff1aSopenharmony_ci#include "codec_id.h" 34cabdff1aSopenharmony_ci#include "packet.h" 35cabdff1aSopenharmony_ci#include "v4l2_buffers.h" 36cabdff1aSopenharmony_ci 37cabdff1aSopenharmony_citypedef struct V4L2Context { 38cabdff1aSopenharmony_ci /** 39cabdff1aSopenharmony_ci * context name. 40cabdff1aSopenharmony_ci */ 41cabdff1aSopenharmony_ci const char* name; 42cabdff1aSopenharmony_ci 43cabdff1aSopenharmony_ci /** 44cabdff1aSopenharmony_ci * Type of this buffer context. 45cabdff1aSopenharmony_ci * See V4L2_BUF_TYPE_VIDEO_* in videodev2.h 46cabdff1aSopenharmony_ci * Readonly after init. 47cabdff1aSopenharmony_ci */ 48cabdff1aSopenharmony_ci enum v4l2_buf_type type; 49cabdff1aSopenharmony_ci 50cabdff1aSopenharmony_ci /** 51cabdff1aSopenharmony_ci * AVPixelFormat corresponding to this buffer context. 52cabdff1aSopenharmony_ci * AV_PIX_FMT_NONE means this is an encoded stream. 53cabdff1aSopenharmony_ci */ 54cabdff1aSopenharmony_ci enum AVPixelFormat av_pix_fmt; 55cabdff1aSopenharmony_ci 56cabdff1aSopenharmony_ci /** 57cabdff1aSopenharmony_ci * AVCodecID corresponding to this buffer context. 58cabdff1aSopenharmony_ci * AV_CODEC_ID_RAWVIDEO means this is a raw stream and av_pix_fmt must be set to a valid value. 59cabdff1aSopenharmony_ci */ 60cabdff1aSopenharmony_ci enum AVCodecID av_codec_id; 61cabdff1aSopenharmony_ci 62cabdff1aSopenharmony_ci /** 63cabdff1aSopenharmony_ci * Format returned by the driver after initializing the buffer context. 64cabdff1aSopenharmony_ci * Readonly after init. 65cabdff1aSopenharmony_ci */ 66cabdff1aSopenharmony_ci struct v4l2_format format; 67cabdff1aSopenharmony_ci 68cabdff1aSopenharmony_ci /** 69cabdff1aSopenharmony_ci * Width and height of the frames it produces (in case of a capture context, e.g. when decoding) 70cabdff1aSopenharmony_ci * or accepts (in case of an output context, e.g. when encoding). 71cabdff1aSopenharmony_ci */ 72cabdff1aSopenharmony_ci int width, height; 73cabdff1aSopenharmony_ci AVRational sample_aspect_ratio; 74cabdff1aSopenharmony_ci 75cabdff1aSopenharmony_ci /** 76cabdff1aSopenharmony_ci * Indexed array of V4L2Buffers 77cabdff1aSopenharmony_ci */ 78cabdff1aSopenharmony_ci V4L2Buffer *buffers; 79cabdff1aSopenharmony_ci 80cabdff1aSopenharmony_ci /** 81cabdff1aSopenharmony_ci * Readonly after init. 82cabdff1aSopenharmony_ci */ 83cabdff1aSopenharmony_ci int num_buffers; 84cabdff1aSopenharmony_ci 85cabdff1aSopenharmony_ci /** 86cabdff1aSopenharmony_ci * Whether the stream has been started (VIDIOC_STREAMON has been sent). 87cabdff1aSopenharmony_ci */ 88cabdff1aSopenharmony_ci int streamon; 89cabdff1aSopenharmony_ci 90cabdff1aSopenharmony_ci /** 91cabdff1aSopenharmony_ci * Either no more buffers available or an unrecoverable error was notified 92cabdff1aSopenharmony_ci * by the V4L2 kernel driver: once set the context has to be exited. 93cabdff1aSopenharmony_ci */ 94cabdff1aSopenharmony_ci int done; 95cabdff1aSopenharmony_ci 96cabdff1aSopenharmony_ci} V4L2Context; 97cabdff1aSopenharmony_ci 98cabdff1aSopenharmony_ci/** 99cabdff1aSopenharmony_ci * Initializes a V4L2Context. 100cabdff1aSopenharmony_ci * 101cabdff1aSopenharmony_ci * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables. 102cabdff1aSopenharmony_ci * @return 0 in case of success, a negative value representing the error otherwise. 103cabdff1aSopenharmony_ci */ 104cabdff1aSopenharmony_ciint ff_v4l2_context_init(V4L2Context* ctx); 105cabdff1aSopenharmony_ci 106cabdff1aSopenharmony_ci/** 107cabdff1aSopenharmony_ci * Sets the V4L2Context format in the v4l2 driver. 108cabdff1aSopenharmony_ci * 109cabdff1aSopenharmony_ci * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables. 110cabdff1aSopenharmony_ci * @return 0 in case of success, a negative value representing the error otherwise. 111cabdff1aSopenharmony_ci */ 112cabdff1aSopenharmony_ciint ff_v4l2_context_set_format(V4L2Context* ctx); 113cabdff1aSopenharmony_ci 114cabdff1aSopenharmony_ci/** 115cabdff1aSopenharmony_ci * Queries the driver for a valid v4l2 format and copies it to the context. 116cabdff1aSopenharmony_ci * 117cabdff1aSopenharmony_ci * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables. 118cabdff1aSopenharmony_ci * @param[in] probe Probe only and ignore changes to the format. 119cabdff1aSopenharmony_ci * @return 0 in case of success, a negative value representing the error otherwise. 120cabdff1aSopenharmony_ci */ 121cabdff1aSopenharmony_ciint ff_v4l2_context_get_format(V4L2Context* ctx, int probe); 122cabdff1aSopenharmony_ci 123cabdff1aSopenharmony_ci/** 124cabdff1aSopenharmony_ci * Releases a V4L2Context. 125cabdff1aSopenharmony_ci * 126cabdff1aSopenharmony_ci * @param[in] ctx A pointer to a V4L2Context. 127cabdff1aSopenharmony_ci * The caller is reponsible for freeing it. 128cabdff1aSopenharmony_ci * It must not be used after calling this function. 129cabdff1aSopenharmony_ci */ 130cabdff1aSopenharmony_civoid ff_v4l2_context_release(V4L2Context* ctx); 131cabdff1aSopenharmony_ci 132cabdff1aSopenharmony_ci/** 133cabdff1aSopenharmony_ci * Sets the status of a V4L2Context. 134cabdff1aSopenharmony_ci * 135cabdff1aSopenharmony_ci * @param[in] ctx A pointer to a V4L2Context. 136cabdff1aSopenharmony_ci * @param[in] cmd The status to set (VIDIOC_STREAMON or VIDIOC_STREAMOFF). 137cabdff1aSopenharmony_ci * Warning: If VIDIOC_STREAMOFF is sent to a buffer context that still has some frames buffered, 138cabdff1aSopenharmony_ci * those frames will be dropped. 139cabdff1aSopenharmony_ci * @return 0 in case of success, a negative value representing the error otherwise. 140cabdff1aSopenharmony_ci */ 141cabdff1aSopenharmony_ciint ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd); 142cabdff1aSopenharmony_ci 143cabdff1aSopenharmony_ci/** 144cabdff1aSopenharmony_ci * Dequeues a buffer from a V4L2Context to an AVPacket. 145cabdff1aSopenharmony_ci * 146cabdff1aSopenharmony_ci * The pkt must be non NULL. 147cabdff1aSopenharmony_ci * @param[in] ctx The V4L2Context to dequeue from. 148cabdff1aSopenharmony_ci * @param[inout] pkt The AVPacket to dequeue to. 149cabdff1aSopenharmony_ci * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error. 150cabdff1aSopenharmony_ci */ 151cabdff1aSopenharmony_ciint ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt); 152cabdff1aSopenharmony_ci 153cabdff1aSopenharmony_ci/** 154cabdff1aSopenharmony_ci * Dequeues a buffer from a V4L2Context to an AVFrame. 155cabdff1aSopenharmony_ci * 156cabdff1aSopenharmony_ci * The frame must be non NULL. 157cabdff1aSopenharmony_ci * @param[in] ctx The V4L2Context to dequeue from. 158cabdff1aSopenharmony_ci * @param[inout] f The AVFrame to dequeue to. 159cabdff1aSopenharmony_ci * @param[in] timeout The timeout for dequeue (-1 to block, 0 to return immediately, or milliseconds) 160cabdff1aSopenharmony_ci * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error. 161cabdff1aSopenharmony_ci */ 162cabdff1aSopenharmony_ciint ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* f, int timeout); 163cabdff1aSopenharmony_ci 164cabdff1aSopenharmony_ci/** 165cabdff1aSopenharmony_ci * Enqueues a buffer to a V4L2Context from an AVPacket 166cabdff1aSopenharmony_ci * 167cabdff1aSopenharmony_ci * The packet must be non NULL. 168cabdff1aSopenharmony_ci * 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. 169cabdff1aSopenharmony_ci * 170cabdff1aSopenharmony_ci * @param[in] ctx The V4L2Context to enqueue to. 171cabdff1aSopenharmony_ci * @param[in] pkt A pointer to an AVPacket. 172cabdff1aSopenharmony_ci * @return 0 in case of success, a negative error otherwise. 173cabdff1aSopenharmony_ci */ 174cabdff1aSopenharmony_ciint ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt); 175cabdff1aSopenharmony_ci 176cabdff1aSopenharmony_ci/** 177cabdff1aSopenharmony_ci * Enqueues a buffer to a V4L2Context from an AVFrame 178cabdff1aSopenharmony_ci * 179cabdff1aSopenharmony_ci * The frame must be non NULL. 180cabdff1aSopenharmony_ci * 181cabdff1aSopenharmony_ci * @param[in] ctx The V4L2Context to enqueue to. 182cabdff1aSopenharmony_ci * @param[in] f A pointer to an AVFrame to enqueue. 183cabdff1aSopenharmony_ci * @return 0 in case of success, a negative error otherwise. 184cabdff1aSopenharmony_ci */ 185cabdff1aSopenharmony_ciint ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* f); 186cabdff1aSopenharmony_ci 187cabdff1aSopenharmony_ci#endif // AVCODEC_V4L2_CONTEXT_H 188