MCPcopy Create free account
hub / github.com/78/tenbox / EncodeFrame

Method EncodeFrame

src/daemon/ffmpeg_video_encoder.cpp:406–459  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

404}
405
406bool FfmpegH264VideoEncoder::EncodeFrame(int64_t pts_us, EncodedVideoFrame* output, std::string* error) {
407 if (!impl_ || !impl_->codec || !impl_->frame || !impl_->packet) {
408 if (error) *error = "FFmpeg encoder is not open";
409 return false;
410 }
411 if (!impl_->frame_initialized) {
412 if (error) *error = "FFmpeg encoder has not received a full-frame seed yet";
413 return false;
414 }
415
416 // Release the previous packet (if any). The caller's span returned from
417 // the prior EncodeFrame call is expected to have been consumed by now;
418 // keeping the packet alive across calls saves a memcpy of the encoded
419 // bitstream into a separate `encoded_` buffer.
420 av_packet_unref(impl_->packet);
421
422 impl_->frame->pts = impl_->next_pts++;
423 impl_->frame->pict_type = impl_->force_keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_NONE;
424 impl_->force_keyframe = false;
425
426 int rc = avcodec_send_frame(impl_->codec, impl_->frame);
427 if (rc < 0) {
428 if (error) *error = "failed to send frame to FFmpeg encoder: " + AvError(rc);
429 return false;
430 }
431
432 rc = avcodec_receive_packet(impl_->codec, impl_->packet);
433 if (rc == AVERROR(EAGAIN)) return false;
434 if (rc < 0) {
435 if (error) *error = "failed to receive FFmpeg encoded packet: " + AvError(rc);
436 return false;
437 }
438 if (impl_->packet->size <= 0 || !impl_->packet->data) {
439 return false;
440 }
441
442 const bool keyframe = (impl_->packet->flags & AV_PKT_FLAG_KEY) != 0;
443 if (output) {
444 std::span<const uint8_t> payload(impl_->packet->data,
445 static_cast<size_t>(impl_->packet->size));
446 if (impl_->output_annexb) {
447 if (!AnnexBToLengthPrefixed(payload, &impl_->encoded_)) {
448 if (error) *error = "failed to convert FFmpeg Annex B packet to AVCC";
449 return false;
450 }
451 payload = std::span<const uint8_t>(impl_->encoded_.data(), impl_->encoded_.size());
452 }
453 output->codec = VideoCodec::kH264;
454 output->data = payload;
455 output->keyframe = keyframe;
456 output->pts_us = pts_us;
457 }
458 return true;
459}
460
461bool FfmpegH264VideoEncoder::HasFullSeed() const {
462 return impl_ && impl_->frame_initialized;

Callers 1

VideoPumpMainMethod · 0.80

Calls 4

AvErrorFunction · 0.85
AnnexBToLengthPrefixedFunction · 0.85
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected