X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=doc%2Fexamples%2Ftranscoding.c;h=3a97426e2c42e4304d951b95d29066af3d08fe78;hb=626535f6a169e2d821b969e0ea77125ba7482113;hp=e48837cbd28cca6fd07d47a0955bccacf6d7ae3b;hpb=8e789d244cc946bc350672eeb02453918b21a09f;p=ffmpeg
diff --git a/doc/examples/transcoding.c b/doc/examples/transcoding.c
index e48837cbd28..3a97426e2c4 100644
--- a/doc/examples/transcoding.c
+++ b/doc/examples/transcoding.c
@@ -41,12 +41,17 @@ typedef struct FilteringContext {
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
+
+ AVPacket *enc_pkt;
+ AVFrame *filtered_frame;
} FilteringContext;
static FilteringContext *filter_ctx;
typedef struct StreamContext {
AVCodecContext *dec_ctx;
AVCodecContext *enc_ctx;
+
+ AVFrame *dec_frame;
} StreamContext;
static StreamContext *stream_ctx;
@@ -72,7 +77,7 @@ static int open_input_file(const char *filename)
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
AVStream *stream = ifmt_ctx->streams[i];
- AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
+ const AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
AVCodecContext *codec_ctx;
if (!dec) {
av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);
@@ -102,6 +107,10 @@ static int open_input_file(const char *filename)
}
}
stream_ctx[i].dec_ctx = codec_ctx;
+
+ stream_ctx[i].dec_frame = av_frame_alloc();
+ if (!stream_ctx[i].dec_frame)
+ return AVERROR(ENOMEM);
}
av_dump_format(ifmt_ctx, 0, filename, 0);
@@ -113,7 +122,7 @@ static int open_output_file(const char *filename)
AVStream *out_stream;
AVStream *in_stream;
AVCodecContext *dec_ctx, *enc_ctx;
- AVCodec *encoder;
+ const AVCodec *encoder;
int ret;
unsigned int i;
@@ -398,54 +407,63 @@ static int init_filters(void)
stream_ctx[i].enc_ctx, filter_spec);
if (ret)
return ret;
+
+ filter_ctx[i].enc_pkt = av_packet_alloc();
+ if (!filter_ctx[i].enc_pkt)
+ return AVERROR(ENOMEM);
+
+ filter_ctx[i].filtered_frame = av_frame_alloc();
+ if (!filter_ctx[i].filtered_frame)
+ return AVERROR(ENOMEM);
}
return 0;
}
-static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
+static int encode_write_frame(unsigned int stream_index, int flush)
+{
+ StreamContext *stream = &stream_ctx[stream_index];
+ FilteringContext *filter = &filter_ctx[stream_index];
+ AVFrame *filt_frame = flush ? NULL : filter->filtered_frame;
+ AVPacket *enc_pkt = filter->enc_pkt;
int ret;
- int got_frame_local;
- AVPacket enc_pkt;
- int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
- (ifmt_ctx->streams[stream_index]->codecpar->codec_type ==
- AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;
-
- if (!got_frame)
- got_frame = &got_frame_local;
av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
/* encode filtered frame */
- enc_pkt.data = NULL;
- enc_pkt.size = 0;
- av_init_packet(&enc_pkt);
- ret = enc_func(stream_ctx[stream_index].enc_ctx, &enc_pkt,
- filt_frame, got_frame);
- av_frame_free(&filt_frame);
+ av_packet_unref(enc_pkt);
+
+ ret = avcodec_send_frame(stream->enc_ctx, filt_frame);
+
if (ret < 0)
return ret;
- if (!(*got_frame))
- return 0;
- /* prepare packet for muxing */
- enc_pkt.stream_index = stream_index;
- av_packet_rescale_ts(&enc_pkt,
- stream_ctx[stream_index].enc_ctx->time_base,
- ofmt_ctx->streams[stream_index]->time_base);
+ while (ret >= 0) {
+ ret = avcodec_receive_packet(stream->enc_ctx, enc_pkt);
+
+ if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+ return 0;
+
+ /* prepare packet for muxing */
+ enc_pkt->stream_index = stream_index;
+ av_packet_rescale_ts(enc_pkt,
+ stream->enc_ctx->time_base,
+ ofmt_ctx->streams[stream_index]->time_base);
+
+ av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
+ /* mux encoded frame */
+ ret = av_interleaved_write_frame(ofmt_ctx, enc_pkt);
+ }
- av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
- /* mux encoded frame */
- ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
return ret;
}
static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
{
+ FilteringContext *filter = &filter_ctx[stream_index];
int ret;
- AVFrame *filt_frame;
av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
/* push the decoded frame into the filtergraph */
- ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
+ ret = av_buffersrc_add_frame_flags(filter->buffersrc_ctx,
frame, 0);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
@@ -454,14 +472,9 @@ static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
/* pull filtered frames from the filtergraph */
while (1) {
- filt_frame = av_frame_alloc();
- if (!filt_frame) {
- ret = AVERROR(ENOMEM);
- break;
- }
av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
- ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
- filt_frame);
+ ret = av_buffersink_get_frame(filter->buffersink_ctx,
+ filter->filtered_frame);
if (ret < 0) {
/* if no more frames for output - returns AVERROR(EAGAIN)
* if flushed and no more frames for output - returns AVERROR_EOF
@@ -469,12 +482,12 @@ static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
*/
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
ret = 0;
- av_frame_free(&filt_frame);
break;
}
- filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
- ret = encode_write_frame(filt_frame, stream_index, NULL);
+ filter->filtered_frame->pict_type = AV_PICTURE_TYPE_NONE;
+ ret = encode_write_frame(stream_index, 0);
+ av_frame_unref(filter->filtered_frame);
if (ret < 0)
break;
}
@@ -484,34 +497,20 @@ static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
static int flush_encoder(unsigned int stream_index)
{
- int ret;
- int got_frame;
-
if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
AV_CODEC_CAP_DELAY))
return 0;
- while (1) {
- av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
- ret = encode_write_frame(NULL, stream_index, &got_frame);
- if (ret < 0)
- break;
- if (!got_frame)
- return 0;
- }
- return ret;
+ av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
+ return encode_write_frame(stream_index, 1);
}
int main(int argc, char **argv)
{
int ret;
- AVPacket packet = { .data = NULL, .size = 0 };
- AVFrame *frame = NULL;
- enum AVMediaType type;
+ AVPacket *packet = NULL;
unsigned int stream_index;
unsigned int i;
- int got_frame;
- int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
if (argc != 3) {
av_log(NULL, AV_LOG_ERROR, "Usage: %s