Open the encoder and register a video stream in the mux context.
Selects the encoder: if codec_id is AV_CODEC_ID_NONE the container's default video codec is used (MP4/MKV -> H.264, WebM -> VP9, AVI -> MPEG4). Allocates AVCodecContext, initialises SwsContext converting from src_pixel_format to the encoder's required pixel format, allocates the scratch AVFrame, and copies codec parameters to the new AVStream.
Must be called after FFmpegMuxContext::open() and before FFmpegMuxContext::write_header().
- Parameters
-
| mux | Open mux context that will own the output stream. |
| width | Frame width in pixels. |
| height | Frame height in pixels. |
| frame_rate | Average frame rate in frames per second. |
| src_pixel_format | Source AVPixelFormat delivered by the readback thread. |
| codec_id | Encoder override; AV_CODEC_ID_NONE = container default. |
- Returns
- True on success; false sets the internal error string.
< let encoder decide from crf
Definition at line 106 of file VideoEncodeContext.cpp.
112{
114
115 if (!mux.is_open()) {
116 m_last_error =
"VideoEncodeContext::open: mux context is not open";
117 return false;
118 }
119
120 if (
width == 0 ||
height == 0 || frame_rate <= 0.0) {
121 m_last_error =
"VideoEncodeContext::open: invalid dimensions or frame rate";
122 return false;
123 }
124
130
131 if (codec_id == AV_CODEC_ID_NONE)
132 codec_id = infer_video_codec(mux.format_context);
133
134 const AVCodec* codec = avcodec_find_encoder(codec_id);
135 if (!codec) {
136 m_last_error = std::string(
"avcodec_find_encoder failed for codec_id=")
137 + std::to_string(static_cast<int>(codec_id));
138 return false;
139 }
140
144 return false;
145 }
146
147
150
151
152
153
154
155 const int fps_num = static_cast<int>(std::round(frame_rate * 1000.0));
158
159
160
161
162
163
164
166
167
168
169
170
172 if (codec_id == AV_CODEC_ID_H264 || codec_id == AV_CODEC_ID_H265) {
175 }
176
177 if (mux.format_context->oformat->flags & AVFMT_GLOBALHEADER)
179
183 return false;
184 }
185
188 m_last_error =
"FFmpegMuxContext::add_stream returned nullptr";
190 return false;
191 }
192
194 m_last_error =
"avcodec_parameters_from_context failed";
196 return false;
197 }
198
201
202
203
204
208 src_pixel_format,
212 SWS_BILINEAR,
213 nullptr, nullptr, nullptr);
214
218 return false;
219 }
220
221
222
223
228 return false;
229 }
230
234
238 return false;
239 }
240
242 "[VideoEncodeContext] open: {}x{} @ {:.3f} fps | src={} enc={} stream#{}",
244 av_get_pix_fmt_name(src_pixel_format) ? av_get_pix_fmt_name(src_pixel_format) : "unknown",
245 avcodec_get_name(codec_id),
247
248 return true;
249}
#define MF_INFO(comp, ctx,...)
AVStream * m_stream
Weak ref into FFmpegMuxContext; not owned.
AVFrame * m_scratch_frame
Owned scratch buffer for encoder input.
void close()
Release all owned resources.
SwsContext * sws_context
Owned; freed in destructor.
AVCodecContext * codec_context
Owned; freed in destructor.
AVPixelFormat m_src_pixel_fmt
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
References MayaFlux::IO::FFmpegMuxContext::add_stream(), close(), codec_context, MayaFlux::Journal::FileIO, MayaFlux::IO::FFmpegMuxContext::format_context, height(), MayaFlux::Journal::IO, MayaFlux::IO::FFmpegMuxContext::is_open(), m_height, m_last_error, m_scratch_frame, m_src_pixel_fmt, m_src_src_bpp, m_stream, m_stream_index, m_width, MF_INFO, sws_context, and width().
Referenced by MayaFlux::IO::VideoFileWriter::worker_loop().