Re: [libav-devel] [PATCH 6/6] examples/output: switch to the new encoding API

2016-09-22 Thread Vittorio Giovara
On Thu, Sep 22, 2016 at 3:35 AM, Anton Khirnov  wrote:
> ---
>  doc/examples/output.c | 71 
> +++
>  1 file changed, 44 insertions(+), 27 deletions(-)
>
> diff --git a/doc/examples/output.c b/doc/examples/output.c
> index 44a55f5..bb0da30 100644
> --- a/doc/examples/output.c
> +++ b/doc/examples/output.c
> @@ -233,25 +233,37 @@ static AVFrame *get_audio_frame(OutputStream *ost)
>  static int encode_audio_frame(AVFormatContext *oc, OutputStream *ost,
>AVFrame *frame)
>  {
> -AVPacket pkt = { 0 }; // data and size must be 0;
> -int got_packet;
> +int ret;
>
> -av_init_packet();
> -avcodec_encode_audio2(ost->enc, , frame, _packet);
> +ret = avcodec_send_frame(ost->enc, frame);
> +if (ret < 0) {
> +fprintf(stderr, "Error submitting a frame for encoding\n");
> +exit(1);
> +}
>
> -if (got_packet) {
> -pkt.stream_index = ost->st->index;
> +while (ret >= 0) {
> +AVPacket pkt = { 0 }; // data and size must be 0;
>
> -av_packet_rescale_ts(, ost->enc->time_base, ost->st->time_base);
> +av_init_packet();
>
> -/* Write the compressed frame to the media file. */
> -if (av_interleaved_write_frame(oc, ) != 0) {
> -fprintf(stderr, "Error while writing audio frame\n");
> +ret = avcodec_receive_packet(ost->enc, );
> +if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
> +fprintf(stderr, "Error encoding a video frame\n");
>  exit(1);
> +} else if (ret >= 0) {
> +av_packet_rescale_ts(, ost->enc->time_base, 
> ost->st->time_base);
> +pkt.stream_index = ost->st->index;
> +
> +/* Write the compressed frame to the media file. */
> +ret = av_interleaved_write_frame(oc, );
> +if (ret < 0) {
> +fprintf(stderr, "Error while writing video frame\n");
> +exit(1);
> +}
>  }
>  }
>
> -return (frame || got_packet) ? 0 : 1;
> +return ret == AVERROR_EOF;
>  }
>
>  /*
> @@ -517,36 +529,41 @@ static int write_video_frame(AVFormatContext *oc, 
> OutputStream *ost)
>  int ret;
>  AVCodecContext *c;
>  AVFrame *frame;
> -AVPacket pkt   = { 0 };
> -int got_packet = 0;
>
>  c = ost->enc;
>
>  frame = get_video_frame(ost);
>
> -av_init_packet();
> -
>  /* encode the image */
> -ret = avcodec_encode_video2(c, , frame, _packet);
> +ret = avcodec_send_frame(c, frame);
>  if (ret < 0) {
> -fprintf(stderr, "Error encoding a video frame\n");
> +fprintf(stderr, "Error submitting a frame for encoding\n");
>  exit(1);
>  }
>
> -if (got_packet) {
> -av_packet_rescale_ts(, c->time_base, ost->st->time_base);
> -pkt.stream_index = ost->st->index;
> +while (ret >= 0) {
> +AVPacket pkt = { 0 };
>
> -/* Write the compressed frame to the media file. */
> -ret = av_interleaved_write_frame(oc, );
> -}
> +av_init_packet();
>
> -if (ret != 0) {
> -fprintf(stderr, "Error while writing video frame\n");
> -exit(1);
> +ret = avcodec_receive_packet(c, );
> +if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
> +fprintf(stderr, "Error encoding a video frame\n");
> +exit(1);
> +} else if (ret >= 0) {
> +av_packet_rescale_ts(, c->time_base, ost->st->time_base);
> +pkt.stream_index = ost->st->index;
> +
> +/* Write the compressed frame to the media file. */
> +ret = av_interleaved_write_frame(oc, );
> +if (ret < 0) {
> +fprintf(stderr, "Error while writing video frame\n");
> +exit(1);
> +}
> +}
>  }
>
> -return (frame || got_packet) ? 0 : 1;
> +return ret == AVERROR_EOF;
>  }
>
>  static void close_stream(AVFormatContext *oc, OutputStream *ost)
> --

ok i think

-- 
Vittorio
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel


[libav-devel] [PATCH 6/6] examples/output: switch to the new encoding API

2016-09-22 Thread Anton Khirnov
---
 doc/examples/output.c | 71 +++
 1 file changed, 44 insertions(+), 27 deletions(-)

diff --git a/doc/examples/output.c b/doc/examples/output.c
index 44a55f5..bb0da30 100644
--- a/doc/examples/output.c
+++ b/doc/examples/output.c
@@ -233,25 +233,37 @@ static AVFrame *get_audio_frame(OutputStream *ost)
 static int encode_audio_frame(AVFormatContext *oc, OutputStream *ost,
   AVFrame *frame)
 {
-AVPacket pkt = { 0 }; // data and size must be 0;
-int got_packet;
+int ret;
 
-av_init_packet();
-avcodec_encode_audio2(ost->enc, , frame, _packet);
+ret = avcodec_send_frame(ost->enc, frame);
+if (ret < 0) {
+fprintf(stderr, "Error submitting a frame for encoding\n");
+exit(1);
+}
 
-if (got_packet) {
-pkt.stream_index = ost->st->index;
+while (ret >= 0) {
+AVPacket pkt = { 0 }; // data and size must be 0;
 
-av_packet_rescale_ts(, ost->enc->time_base, ost->st->time_base);
+av_init_packet();
 
-/* Write the compressed frame to the media file. */
-if (av_interleaved_write_frame(oc, ) != 0) {
-fprintf(stderr, "Error while writing audio frame\n");
+ret = avcodec_receive_packet(ost->enc, );
+if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
+fprintf(stderr, "Error encoding a video frame\n");
 exit(1);
+} else if (ret >= 0) {
+av_packet_rescale_ts(, ost->enc->time_base, 
ost->st->time_base);
+pkt.stream_index = ost->st->index;
+
+/* Write the compressed frame to the media file. */
+ret = av_interleaved_write_frame(oc, );
+if (ret < 0) {
+fprintf(stderr, "Error while writing video frame\n");
+exit(1);
+}
 }
 }
 
-return (frame || got_packet) ? 0 : 1;
+return ret == AVERROR_EOF;
 }
 
 /*
@@ -517,36 +529,41 @@ static int write_video_frame(AVFormatContext *oc, 
OutputStream *ost)
 int ret;
 AVCodecContext *c;
 AVFrame *frame;
-AVPacket pkt   = { 0 };
-int got_packet = 0;
 
 c = ost->enc;
 
 frame = get_video_frame(ost);
 
-av_init_packet();
-
 /* encode the image */
-ret = avcodec_encode_video2(c, , frame, _packet);
+ret = avcodec_send_frame(c, frame);
 if (ret < 0) {
-fprintf(stderr, "Error encoding a video frame\n");
+fprintf(stderr, "Error submitting a frame for encoding\n");
 exit(1);
 }
 
-if (got_packet) {
-av_packet_rescale_ts(, c->time_base, ost->st->time_base);
-pkt.stream_index = ost->st->index;
+while (ret >= 0) {
+AVPacket pkt = { 0 };
 
-/* Write the compressed frame to the media file. */
-ret = av_interleaved_write_frame(oc, );
-}
+av_init_packet();
 
-if (ret != 0) {
-fprintf(stderr, "Error while writing video frame\n");
-exit(1);
+ret = avcodec_receive_packet(c, );
+if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
+fprintf(stderr, "Error encoding a video frame\n");
+exit(1);
+} else if (ret >= 0) {
+av_packet_rescale_ts(, c->time_base, ost->st->time_base);
+pkt.stream_index = ost->st->index;
+
+/* Write the compressed frame to the media file. */
+ret = av_interleaved_write_frame(oc, );
+if (ret < 0) {
+fprintf(stderr, "Error while writing video frame\n");
+exit(1);
+}
+}
 }
 
-return (frame || got_packet) ? 0 : 1;
+return ret == AVERROR_EOF;
 }
 
 static void close_stream(AVFormatContext *oc, OutputStream *ost)
-- 
2.0.0

___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel