MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open_device()

bool MayaFlux::IO::VideoStreamContext::open_device ( const FFmpegDemuxContext demux,
uint32_t  target_width = 0,
uint32_t  target_height = 0,
int  target_format = -1 
)

Open codec only, without initialising the SwsContext scaler.

Intended for live capture devices (dshow, v4l2, avfoundation) where pix_fmt is AV_PIX_FMT_NONE until the first decoded frame arrives and sws_getContext therefore cannot be called at open time.

After this call is_codec_valid() returns true; is_valid() returns false until rebuild_scaler_from_frame() has been called successfully.

Parameters
demuxOpen demux context (must outlive this object).
target_widthDesired output width (0 = use frame width).
target_heightDesired output height (0 = use frame height).
target_formatDesired AVPixelFormat (negative = AV_PIX_FMT_RGBA).
Returns
True if codec was opened successfully.

Definition at line 208 of file VideoStreamContext.cpp.

210{
211 close();
213
214 if (!demux.is_open()) {
215 m_last_error = "Demux context is not open";
216 return false;
217 }
218
219 const AVCodec* codec = nullptr;
220 stream_index = demux.find_best_stream(AVMEDIA_TYPE_VIDEO,
221 reinterpret_cast<const void**>(&codec));
222 if (stream_index < 0 || !codec) {
223 m_last_error = "No video stream found";
224 return false;
225 }
226
227 codec_context = avcodec_alloc_context3(codec);
228 if (!codec_context) {
229 m_last_error = "avcodec_alloc_context3 failed";
230 return false;
231 }
232
233 AVStream* stream = demux.get_stream(stream_index);
234 if (avcodec_parameters_to_context(codec_context, stream->codecpar) < 0) {
235 m_last_error = "avcodec_parameters_to_context failed";
236 close();
237 return false;
238 }
239
240 if (avcodec_open2(codec_context, codec, nullptr) < 0) {
241 m_last_error = "avcodec_open2 failed";
242 close();
243 return false;
244 }
245
246 target_width = tw;
247 target_height = th;
248 target_format = tf;
249
251
252 width = static_cast<uint32_t>(codec_context->width);
253 height = static_cast<uint32_t>(codec_context->height);
254
255 if (width == 0 || height == 0) {
256 AVStream* s = demux.get_stream(stream_index);
257 width = static_cast<uint32_t>(s->codecpar->width);
258 height = static_cast<uint32_t>(s->codecpar->height);
259 }
260
261 if (width == 0 || height == 0) {
264 }
265
266#ifdef MAYAFLUX_PLATFORM_WINDOWS
267 if (target_width > 0 && target_height > 0
269 std::swap(width, height);
270 }
271#endif
272
275
276 if (stream->avg_frame_rate.den > 0 && stream->avg_frame_rate.num > 0)
277 frame_rate = av_q2d(stream->avg_frame_rate);
278 else if (stream->r_frame_rate.den > 0 && stream->r_frame_rate.num > 0)
279 frame_rate = av_q2d(stream->r_frame_rate);
280
281 auto fmt = av_get_pix_fmt_name(static_cast<AVPixelFormat>(src_pixel_format));
282
284 "[VideoStreamContext] open_device: stream #{} | {}x{} | pix_fmt={}",
285 stream_index, width, height, fmt ? fmt : "none");
286
287 return true;
288}
#define MF_INFO(comp, ctx,...)
static void init_ffmpeg()
Initialise FFmpeg logging level once per process.
uint32_t target_width
Requested output width (0 = source).
void close()
Release codec and scaler resources.
uint32_t target_height
Requested output height (0 = source).
double frame_rate
Average frame rate (fps).
uint32_t out_height
Output height after scaling.
uint32_t height
Source height in pixels.
uint32_t width
Source width in pixels.
int src_pixel_format
Source AVPixelFormat.
AVCodecContext * codec_context
Owned; freed in destructor.
int target_format
Requested AVPixelFormat (negative = RGBA).
uint32_t out_width
Output width after scaling.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.

References close(), codec_context, MayaFlux::Journal::FileIO, MayaFlux::IO::FFmpegDemuxContext::find_best_stream(), frame_rate, MayaFlux::IO::FFmpegDemuxContext::get_stream(), height, MayaFlux::IO::FFmpegDemuxContext::init_ffmpeg(), MayaFlux::Journal::IO, MayaFlux::IO::FFmpegDemuxContext::is_open(), m_last_error, MF_INFO, out_height, out_width, src_pixel_format, stream_index, target_format, target_height, target_width, and width.

+ Here is the call graph for this function: