static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned int duration, aom_enc_frame_flags_t flags, AvxVideoWriter *writer) { int got_pkts = 0; aom_codec_iter_t iter = NULL; const aom_codec_cx_pkt_t *pkt = NULL; const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags); if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame."); while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) { got_pkts = 1; if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, pkt->data.frame.sz, pkt->data.frame.pts)) die_codec(ctx, "Failed to write compressed frame."); printf(keyframe ? "K" : "."); fflush(stdout); } } return got_pkts; }
static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img, int frame_index, int flags, AvxVideoWriter *writer) { int got_pkts = 0; aom_codec_iter_t iter = NULL; const aom_codec_cx_pkt_t *pkt = NULL; const aom_codec_err_t res = aom_codec_encode(codec, img, frame_index, 1, flags, AOM_DL_GOOD_QUALITY); if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame"); while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) { got_pkts = 1; if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, pkt->data.frame.sz, pkt->data.frame.pts)) { die_codec(codec, "Failed to write compressed frame"); } printf(keyframe ? "K" : "."); fflush(stdout); } } return got_pkts; }
static GstFlowReturn gst_av1_enc_process (GstAV1Enc * encoder) { aom_codec_iter_t iter = NULL; const aom_codec_cx_pkt_t *pkt; GstVideoCodecFrame *frame; GstVideoEncoder *video_encoder; GstFlowReturn ret = GST_FLOW_CUSTOM_SUCCESS; video_encoder = GST_VIDEO_ENCODER (encoder); while ((pkt = aom_codec_get_cx_data (&encoder->encoder, &iter)) != NULL) { if (pkt->kind == AOM_CODEC_STATS_PKT) { GST_WARNING_OBJECT (encoder, "Unhandled stats packet"); } else if (pkt->kind == AOM_CODEC_FPMB_STATS_PKT) { GST_WARNING_OBJECT (encoder, "Unhandled FPMB pkt"); } else if (pkt->kind == AOM_CODEC_PSNR_PKT) { GST_WARNING_OBJECT (encoder, "Unhandled PSNR packet"); } else if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { frame = gst_video_encoder_get_oldest_frame (video_encoder); g_assert (frame != NULL); if ((pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0) { GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame); } else { GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame); } frame->output_buffer = gst_buffer_new_wrapped (g_memdup (pkt->data.frame.buf, pkt->data.frame.sz), pkt->data.frame.sz); if ((pkt->data.frame.flags & AOM_FRAME_IS_DROPPABLE) != 0) GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DROPPABLE); if ((pkt->data.frame.flags & AOM_FRAME_IS_INVISIBLE) != 0) GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DECODE_ONLY); ret = gst_video_encoder_finish_frame (video_encoder, frame); if (ret != GST_FLOW_OK) break; } } return ret; }
static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned int duration, aom_enc_frame_flags_t flags, aom_fixed_buf_t *stats) { int got_pkts = 0; aom_codec_iter_t iter = NULL; const aom_codec_cx_pkt_t *pkt = NULL; const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags); if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats."); while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) { got_pkts = 1; if (pkt->kind == AOM_CODEC_STATS_PKT) { const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf; const size_t pkt_size = pkt->data.twopass_stats.sz; stats->buf = realloc(stats->buf, stats->sz + pkt_size); memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size); stats->sz += pkt_size; } } return got_pkts; }