[FFmpeg-cvslog] avcodec/pngdec: Fix APNG_DISPOSE_OP_BACKGROUND

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: release/5.1 | Andreas Rheinhardt 
 | Sat Aug 20 21:21:40 2022 +0200| 
[bc7df3bc64c04476b816404eb3e9c22997515c06] | committer: Andreas Rheinhardt

avcodec/pngdec: Fix APNG_DISPOSE_OP_BACKGROUND

APNG works with a single reference frame and an output frame.
According to the spec, decoding APNG works by decoding
the current IDAT/fdAT chunks (which decodes to a rectangular
subregion of the whole image region), followed by either
overwriting the region of the output frame with the newly
decoded data or by blending the newly decoded data with
the data from the reference frame onto the current subregion
of the output frame. The remainder of the output frame
is just copied from the reference frame.
Then the reference frame might be left untouched
(APNG_DISPOSE_OP_PREVIOUS), it might be replaced by the output
frame (APNG_DISPOSE_OP_NONE) or the rectangular subregion
corresponding to the just decoded frame has to be reset
to black (APNG_DISPOSE_OP_BACKGROUND).

The latter case is not handled correctly by our decoder:
It only performs resetting the rectangle in the reference frame
when decoding the next frame; and since commit
b593abda6c642cb0c3959752dd235c2faf66837f it does not reset
the reference frame permanently, but only temporarily (i.e.
it only affects decoding the frame after the frame with
APNG_DISPOSE_OP_BACKGROUND). This is a problem if the
frame after the APNG_DISPOSE_OP_BACKGROUND frame uses
APNG_DISPOSE_OP_PREVIOUS, because then the frame after
the APNG_DISPOSE_OP_PREVIOUS frame has an incorrect reference
frame. (If it is not followed by an APNG_DISPOSE_OP_PREVIOUS
frame, the decoder only keeps a reference to the output frame,
which is ok.)

This commit fixes this by being much closer to the spec
than the earlier code: Resetting the background is no longer
postponed until the next frame; instead it is applied to
the reference frame.

Fixes ticket #9602.

(For multithreaded decoding it was actually already broken
since commit 5663301560d77486c7f7c03c1aa5f542fab23c24.)

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=bc7df3bc64c04476b816404eb3e9c22997515c06
---

 libavcodec/pngdec.c | 98 ++---
 1 file changed, 48 insertions(+), 50 deletions(-)

diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index 87b0c639e3..5fa9491f9c 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -78,11 +78,8 @@ typedef struct PNGDecContext {
 enum PNGImageState pic_state;
 int width, height;
 int cur_w, cur_h;
-int last_w, last_h;
 int x_offset, y_offset;
-int last_x_offset, last_y_offset;
 uint8_t dispose_op, blend_op;
-uint8_t last_dispose_op;
 int bit_depth;
 int color_type;
 int compression_type;
@@ -94,8 +91,6 @@ typedef struct PNGDecContext {
 int has_trns;
 uint8_t transparent_color_be[6];
 
-uint8_t *background_buf;
-unsigned background_buf_allocated;
 uint32_t palette[256];
 uint8_t *crow_buf;
 uint8_t *last_row;
@@ -725,9 +720,30 @@ static int decode_idat_chunk(AVCodecContext *avctx, 
PNGDecContext *s,
 }
 
 ff_thread_release_ext_buffer(avctx, &s->picture);
-if ((ret = ff_thread_get_ext_buffer(avctx, &s->picture,
-AV_GET_BUFFER_FLAG_REF)) < 0)
-return ret;
+if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
+/* We only need a buffer for the current picture. */
+ret = ff_thread_get_buffer(avctx, p, 0);
+if (ret < 0)
+return ret;
+} else if (s->dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
+/* We need a buffer for the current picture as well as
+ * a buffer for the reference to retain. */
+ret = ff_thread_get_ext_buffer(avctx, &s->picture,
+   AV_GET_BUFFER_FLAG_REF);
+if (ret < 0)
+return ret;
+ret = ff_thread_get_buffer(avctx, p, 0);
+if (ret < 0)
+return ret;
+} else {
+/* The picture output this time and the reference to retain 
coincide. */
+if ((ret = ff_thread_get_ext_buffer(avctx, &s->picture,
+AV_GET_BUFFER_FLAG_REF)) < 0)
+return ret;
+ret = av_frame_ref(p, s->picture.f);
+if (ret < 0)
+return ret;
+}
 
 p->pict_type= AV_PICTURE_TYPE_I;
 p->key_frame= 1;
@@ -985,12 +1001,6 @@ static int decode_fctl_chunk(AVCodecContext *avctx, 
PNGDecContext *s,
 return AVERROR_INVALIDDATA;
 }
 
-s->last_w = s->cur_w;
-s->last_h = s->cur_h;
-s->last_x_offset = s->x_offset;
-s->last_y_offset = s->y_offset;
-s->last_dispose_op = s->dispose_op;
-
 sequence_number = bytestream2_get_be32(gb);
 cur_w   = bytestream2_get_be32(gb);

[FFmpeg-cvslog] avformat/tests/imf: Test ff_imf_parse_cpl_from_xml_dom cleanup on error

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Thu Aug 25 22:55:05 2022 +0200| [d27c5bce333d6c9c74b855b1f2e747fd541cf37f] | 
committer: Andreas Rheinhardt

avformat/tests/imf: Test ff_imf_parse_cpl_from_xml_dom cleanup on error

Improves the test; also should fix Coverity issue #1512408.

Reviewed-by: Pierre-Anthony Lemieux 
Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=d27c5bce333d6c9c74b855b1f2e747fd541cf37f
---

 libavformat/tests/imf.c | 12 
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libavformat/tests/imf.c b/libavformat/tests/imf.c
index e65629ccbc..a71de692f9 100644
--- a/libavformat/tests/imf.c
+++ b/libavformat/tests/imf.c
@@ -338,10 +338,9 @@ static int test_cpl_parsing(void)
 return 0;
 }
 
-static int test_bad_cpl_parsing(void)
+static int test_bad_cpl_parsing(FFIMFCPL **cpl)
 {
 xmlDocPtr doc;
-FFIMFCPL *cpl;
 int ret;
 
 doc = xmlReadMemory(cpl_bad_doc, strlen(cpl_bad_doc), NULL, NULL, 0);
@@ -350,7 +349,7 @@ static int test_bad_cpl_parsing(void)
 return 1;
 }
 
-ret = ff_imf_parse_cpl_from_xml_dom(doc, &cpl);
+ret = ff_imf_parse_cpl_from_xml_dom(doc, cpl);
 xmlFreeDoc(doc);
 if (ret) {
 printf("CPL parsing failed.\n");
@@ -506,6 +505,7 @@ fail:
 
 int main(int argc, char *argv[])
 {
+FFIMFCPL *cpl;
 int ret = 0;
 
 if (test_cpl_parsing() != 0)
@@ -518,8 +518,12 @@ int main(int argc, char *argv[])
 ret = 1;
 
 printf(" The following should fail \n");
-if (test_bad_cpl_parsing() == 0)
+if (test_bad_cpl_parsing(&cpl) == 0) {
 ret = 1;
+} else if (cpl) {
+printf("Improper cleanup after failed CPL parsing\n");
+ret = 1;
+}
 printf(" End failing test \n");
 
 return ret;

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/encode: Apply intra_only_flag for receive_packet-API, too

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue Aug 23 17:56:31 2022 +0200| [8e56e6b2be454b7f4f27110793bbf585649f111e] | 
committer: Andreas Rheinhardt

avcodec/encode: Apply intra_only_flag for receive_packet-API, too

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=8e56e6b2be454b7f4f27110793bbf585649f111e
---

 libavcodec/encode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index bd66f138a3..9f413095e4 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -240,7 +240,6 @@ static int encode_simple_internal(AVCodecContext *avctx, 
AVPacket *avpkt)
 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
 avpkt->dts = avpkt->pts;
 }
-avpkt->flags |= avci->intra_only_flag;
 }
 
 if (avci->draining && !got_packet)
@@ -301,6 +300,8 @@ static int encode_receive_packet_internal(AVCodecContext 
*avctx, AVPacket *avpkt
 av_assert0(!avpkt->data || avpkt->buf);
 } else
 ret = encode_simple_receive_packet(avctx, avpkt);
+if (ret >= 0)
+avpkt->flags |= avci->intra_only_flag;
 
 if (ret == AVERROR_EOF)
 avci->draining_done = 1;

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/tests/avcodec: Mark frame-thrd encoder incompatible with delay

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue Aug 23 17:28:57 2022 +0200| [e405298ebded794a4ad84222c56b6c0245530afc] | 
committer: Andreas Rheinhardt

avcodec/tests/avcodec: Mark frame-thrd encoder incompatible with delay

The API for frame-threaded encoders only works
for one-in-one-out encoders.

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e405298ebded794a4ad84222c56b6c0245530afc
---

 libavcodec/tests/avcodec.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index 08b5fbede1..3288a85f64 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -155,6 +155,9 @@ int main(void){
 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
 codec->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)
 ERR("Frame-threaded encoder %s claims to support flushing\n");
+if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
+codec->capabilities & AV_CODEC_CAP_DELAY)
+ERR("Frame-threaded encoder %s claims to have delay\n");
 } else {
 if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == 
FF_CODEC_CB_TYPE_DECODE_SUB))
 ERR("Subtitle decoder %s does not implement decode_sub 
callback\n");

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/aptx: Move AudioFrameQueue to aptxenc.c

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue Aug 23 18:12:23 2022 +0200| [18e55de45a4d0ea197eaeae3a3a9daea186159b9] | 
committer: Andreas Rheinhardt

avcodec/aptx: Move AudioFrameQueue to aptxenc.c

It is only used by the encoder.

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=18e55de45a4d0ea197eaeae3a3a9daea186159b9
---

 libavcodec/aptx.c|  1 -
 libavcodec/aptx.h|  2 --
 libavcodec/aptxenc.c | 32 
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/libavcodec/aptx.c b/libavcodec/aptx.c
index f2604be60c..8e110acc97 100644
--- a/libavcodec/aptx.c
+++ b/libavcodec/aptx.c
@@ -534,6 +534,5 @@ av_cold int ff_aptx_init(AVCodecContext *avctx)
 }
 }
 
-ff_af_queue_init(avctx, &s->afq);
 return 0;
 }
diff --git a/libavcodec/aptx.h b/libavcodec/aptx.h
index abb49e6faa..da0697e652 100644
--- a/libavcodec/aptx.h
+++ b/libavcodec/aptx.h
@@ -26,7 +26,6 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "mathops.h"
-#include "audio_frame_queue.h"
 
 
 enum channels {
@@ -95,7 +94,6 @@ typedef struct {
 int block_size;
 int32_t sync_idx;
 Channel channels[NB_CHANNELS];
-AudioFrameQueue afq;
 } AptXContext;
 
 typedef const struct {
diff --git a/libavcodec/aptxenc.c b/libavcodec/aptxenc.c
index 453146f154..2a0d8e06eb 100644
--- a/libavcodec/aptxenc.c
+++ b/libavcodec/aptxenc.c
@@ -24,9 +24,15 @@
 
 #include "libavutil/channel_layout.h"
 #include "aptx.h"
+#include "audio_frame_queue.h"
 #include "codec_internal.h"
 #include "encode.h"
 
+typedef struct AptXEncContext {
+AptXContext common;
+AudioFrameQueue afq;
+} AptXEncContext;
+
 /*
  * Half-band QMF analysis filter realized with a polyphase FIR filter.
  * Split into 2 subbands and downsample by 2.
@@ -212,10 +218,11 @@ static void aptx_encode_samples(AptXContext *ctx,
 static int aptx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  const AVFrame *frame, int *got_packet_ptr)
 {
-AptXContext *s = avctx->priv_data;
+AptXEncContext *const s0 = avctx->priv_data;
+AptXContext *const s = &s0->common;
 int pos, ipos, channel, sample, output_size, ret;
 
-if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
+if ((ret = ff_af_queue_add(&s0->afq, frame)) < 0)
 return ret;
 
 output_size = s->block_size * frame->nb_samples/4;
@@ -232,18 +239,27 @@ static int aptx_encode_frame(AVCodecContext *avctx, 
AVPacket *avpkt,
 aptx_encode_samples(s, samples, avpkt->data + pos);
 }
 
-ff_af_queue_remove(&s->afq, frame->nb_samples, &avpkt->pts, 
&avpkt->duration);
+ff_af_queue_remove(&s0->afq, frame->nb_samples, &avpkt->pts, 
&avpkt->duration);
 *got_packet_ptr = 1;
 return 0;
 }
 
 static av_cold int aptx_close(AVCodecContext *avctx)
 {
-AptXContext *s = avctx->priv_data;
+AptXEncContext *const s = avctx->priv_data;
 ff_af_queue_close(&s->afq);
 return 0;
 }
 
+static av_cold int aptx_encode_init(AVCodecContext *avctx)
+{
+AptXEncContext *const s = avctx->priv_data;
+
+ff_af_queue_init(avctx, &s->afq);
+
+return ff_aptx_init(avctx);
+}
+
 #if CONFIG_APTX_ENCODER
 const FFCodec ff_aptx_encoder = {
 .p.name= "aptx",
@@ -251,8 +267,8 @@ const FFCodec ff_aptx_encoder = {
 .p.type= AVMEDIA_TYPE_AUDIO,
 .p.id  = AV_CODEC_ID_APTX,
 .p.capabilities= AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME,
-.priv_data_size= sizeof(AptXContext),
-.init  = ff_aptx_init,
+.priv_data_size= sizeof(AptXEncContext),
+.init  = aptx_encode_init,
 FF_CODEC_ENCODE_CB(aptx_encode_frame),
 .close = aptx_close,
 #if FF_API_OLD_CHANNEL_LAYOUT
@@ -272,8 +288,8 @@ const FFCodec ff_aptx_hd_encoder = {
 .p.type= AVMEDIA_TYPE_AUDIO,
 .p.id  = AV_CODEC_ID_APTX_HD,
 .p.capabilities= AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME,
-.priv_data_size= sizeof(AptXContext),
-.init  = ff_aptx_init,
+.priv_data_size= sizeof(AptXEncContext),
+.init  = aptx_encode_init,
 FF_CODEC_ENCODE_CB(aptx_encode_frame),
 .close = aptx_close,
 #if FF_API_OLD_CHANNEL_LAYOUT

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/encode: Simplify check for frame-threaded encoder

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue Aug 23 19:48:58 2022 +0200| [312d4467f379d34257f60aeb7ad88fb29b11caeb] | 
committer: Andreas Rheinhardt

avcodec/encode: Simplify check for frame-threaded encoder

AVCodecInternal.frame_thread_encoder is only set iff
active_thread_type is FF_THREAD_FRAME.

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=312d4467f379d34257f60aeb7ad88fb29b11caeb
---

 libavcodec/encode.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 9f413095e4..01b59bbf70 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -192,7 +192,7 @@ static int encode_simple_internal(AVCodecContext *avctx, 
AVPacket *avpkt)
 
 if (!frame->buf[0]) {
 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
-  (avci->frame_thread_encoder && avctx->active_thread_type & 
FF_THREAD_FRAME)))
+  avci->frame_thread_encoder))
 return AVERROR_EOF;
 
 // Flushing is signaled with a NULL frame
@@ -203,8 +203,7 @@ static int encode_simple_internal(AVCodecContext *avctx, 
AVPacket *avpkt)
 
 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
 
-if (CONFIG_FRAME_THREAD_ENCODER &&
-avci->frame_thread_encoder && (avctx->active_thread_type & 
FF_THREAD_FRAME))
+if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
 /* This might modify frame, but it doesn't matter, because
  * the frame properties used below are not used for video
  * (due to the delay inherent in frame threaded encoding, it makes

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/frame_thread_encoder: Forward got_packet directly

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue Aug 23 22:38:24 2022 +0200| [4dddcd08c47850fbf3cef2ff6b31f65133856e0f] | 
committer: Andreas Rheinhardt

avcodec/frame_thread_encoder: Forward got_packet directly

Instead of indicating whether we got a packet by setting
pkt->data and pkt->size to zero.

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=4dddcd08c47850fbf3cef2ff6b31f65133856e0f
---

 libavcodec/frame_thread_encoder.c | 8 +++-
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libavcodec/frame_thread_encoder.c 
b/libavcodec/frame_thread_encoder.c
index 07d310a986..b5765b6343 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -45,6 +45,7 @@ typedef struct{
 AVPacket *outdata;
 int   return_code;
 int   finished;
+int   got_packet;
 } Task;
 
 typedef struct{
@@ -110,10 +111,8 @@ static void * attribute_align_arg worker(void *v){
 if (ret >= 0 && ret2 < 0)
 ret = ret2;
 pkt->pts = pkt->dts = frame->pts;
-} else {
-pkt->data = NULL;
-pkt->size = 0;
 }
+task->got_packet = got_packet;
 pthread_mutex_lock(&c->buffer_mutex);
 av_frame_unref(frame);
 pthread_mutex_unlock(&c->buffer_mutex);
@@ -315,8 +314,7 @@ int ff_thread_video_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt,
  * because there is no outstanding task with this index. */
 outtask->finished = 0;
 av_packet_move_ref(pkt, outtask->outdata);
-if(pkt->data)
-*got_packet_ptr = 1;
+*got_packet_ptr = outtask->got_packet;
 c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
 
 return outtask->return_code;

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/encode, frame_thread_encoder: Unify calling encode callback

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue Aug 23 23:26:57 2022 +0200| [1e6307f46c486d17f670043672d49335ea1bae97] | 
committer: Andreas Rheinhardt

avcodec/encode, frame_thread_encoder: Unify calling encode callback

The encode-callback (the callback used by the FF_CODEC_CB_TYPE_ENCODE
encoders) is currently called in two places: encode_simple_internal()
and by the worker threads of frame-threaded encoders.

After the call, some packet properties are set based upon
the corresponding AVFrame properties and the packet is made
refcounted if it isn't already. So there is some code duplication.

There was also non-duplicated code in encode_simple_internal()
which is executed even when using frame-threading. This included
an emms_c() (which is needed for frame-threading, too, if it is
needed for the single-threaded case, because there are allocations
(via av_packet_make_refcounted()) immediately after returning
from the encode-callback).

Furthermore, some further properties are only set in
encode_simple_internal(): For audio, pts and duration are derived
from the corresponding fields of the frame if the encoder does not
have the AV_CODEC_CAP_DELAY set. Yet this is wrong for frame-threaded
encoders, because frame-threading always introduces delay regardless
of whether the underlying codec has said cap. This only worked because
there are no frame-threaded audio encoders.

This commit fixes the code duplication and the above issue by factoring
this code out and reusing it in both places. It would work in case
of audio codecs with frame-threading, because now the values are
derived from the correct AVFrame.

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=1e6307f46c486d17f670043672d49335ea1bae97
---

 libavcodec/encode.c   | 86 ---
 libavcodec/encode.h   |  3 ++
 libavcodec/frame_thread_encoder.c | 15 ++-
 3 files changed, 50 insertions(+), 54 deletions(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 01b59bbf70..f7b13c8ba1 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -172,6 +172,48 @@ int ff_encode_get_frame(AVCodecContext *avctx, AVFrame 
*frame)
 return 0;
 }
 
+int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
+const AVFrame *frame, int *got_packet)
+{
+const FFCodec *const codec = ffcodec(avctx->codec);
+int ret;
+
+ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
+emms_c();
+av_assert0(ret <= 0);
+
+if (!ret && *got_packet) {
+if (avpkt->data) {
+ret = av_packet_make_refcounted(avpkt);
+if (ret < 0)
+goto unref;
+// Date returned by encoders must always be ref-counted
+av_assert0(avpkt->buf);
+}
+
+if (avctx->codec->type == AVMEDIA_TYPE_VIDEO &&
+!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
+avpkt->pts = avpkt->dts = frame->pts;
+if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
+if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
+if (avpkt->pts == AV_NOPTS_VALUE)
+avpkt->pts = frame->pts;
+if (!avpkt->duration)
+avpkt->duration = ff_samples_to_time_base(avctx,
+  
frame->nb_samples);
+}
+}
+if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
+avpkt->dts = avpkt->pts;
+}
+} else {
+unref:
+av_packet_unref(avpkt);
+}
+
+return ret;
+}
+
 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
 {
 AVCodecInternal   *avci = avctx->internal;
@@ -204,58 +246,18 @@ static int encode_simple_internal(AVCodecContext *avctx, 
AVPacket *avpkt)
 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
 
 if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
-/* This might modify frame, but it doesn't matter, because
- * the frame properties used below are not used for video
- * (due to the delay inherent in frame threaded encoding, it makes
- *  no sense to use the properties of the current frame anyway). */
+/* This might unref frame. */
 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
 else {
-ret = codec->cb.encode(avctx, avpkt, frame, &got_packet);
-if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
-!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
-avpkt->pts = avpkt->dts = frame->pts;
-}
-
-av_assert0(ret <= 0);
-
-emms_c();
-
-if (!ret && got_packet) {
-if (avpkt->data) {
-ret = av_packet_make_refcounted(avpkt);
-if (ret < 0)
-goto end;
-}
-
-if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_

[FFmpeg-cvslog] avcodec: Make ff_alloc_packet() based encoders accept user buffers

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue May 11 20:52:13 2021 +0200| [a499b4345b2dbc731d6c24aa6a9b319d4c3a0d4c] | 
committer: Andreas Rheinhardt

avcodec: Make ff_alloc_packet() based encoders accept user buffers

Up until now, these encoders received non-refcounted packets
(whose data was owned by the corresponding AVCodecContext)
from ff_alloc_packet(); these packets were made refcounted lateron
by av_packet_make_refcounted() generically.
This commit makes these encoders accept user-supplied buffers by
replacing av_packet_make_refcounted() with an equivalent function
that is based upon get_encode_buffer().

(I am pretty certain that one can also set the flag for mpegvideo-
based encoders, but I want to double-check this later. What is certain
is that it reallocates the buffer owned by the AVCodecContext
which should maybe be moved to encode.c, so that proresenc_kostya.c
and ttaenc.c can make use of it, too.)

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=a499b4345b2dbc731d6c24aa6a9b319d4c3a0d4c
---

 libavcodec/aacenc.c |  3 ++-
 libavcodec/alacenc.c|  2 +-
 libavcodec/aliaspixenc.c|  1 +
 libavcodec/asvenc.c |  2 ++
 libavcodec/cfhdenc.c|  2 +-
 libavcodec/cinepakenc.c |  1 +
 libavcodec/encode.c | 19 ++-
 libavcodec/ffv1enc.c|  3 ++-
 libavcodec/flashsv2enc.c|  1 +
 libavcodec/flashsvenc.c |  1 +
 libavcodec/gif.c|  1 +
 libavcodec/hapenc.c |  2 +-
 libavcodec/huffyuvenc.c |  4 ++--
 libavcodec/j2kenc.c |  1 +
 libavcodec/lclenc.c |  2 +-
 libavcodec/libfdk-aacenc.c  |  3 ++-
 libavcodec/libilbc.c|  1 +
 libavcodec/libopencore-amr.c|  3 ++-
 libavcodec/libopusenc.c |  3 ++-
 libavcodec/libspeexenc.c|  2 +-
 libavcodec/libtwolame.c |  2 +-
 libavcodec/libvo-amrwbenc.c |  1 +
 libavcodec/libxvid.c|  1 +
 libavcodec/ljpegenc.c   |  2 +-
 libavcodec/magicyuvenc.c|  2 +-
 libavcodec/mlpenc.c |  7 +--
 libavcodec/mpegaudioenc_fixed.c |  1 +
 libavcodec/mpegaudioenc_float.c |  1 +
 libavcodec/opusenc.c|  3 ++-
 libavcodec/pcxenc.c |  1 +
 libavcodec/pngenc.c |  2 +-
 libavcodec/proresenc_anatoliy.c |  4 ++--
 libavcodec/qoienc.c |  2 +-
 libavcodec/qtrleenc.c   |  1 +
 libavcodec/roqvideoenc.c|  1 +
 libavcodec/rpzaenc.c|  1 +
 libavcodec/sgienc.c |  1 +
 libavcodec/smcenc.c |  1 +
 libavcodec/snowenc.c|  1 +
 libavcodec/sonic.c  |  4 ++--
 libavcodec/sunrastenc.c |  1 +
 libavcodec/svq1enc.c|  1 +
 libavcodec/targaenc.c   |  1 +
 libavcodec/tiffenc.c|  2 +-
 libavcodec/ttaenc.c |  2 +-
 libavcodec/utvideoenc.c |  2 +-
 libavcodec/vorbisenc.c  |  3 ++-
 libavcodec/wavpackenc.c |  2 +-
 libavcodec/wmaenc.c |  2 ++
 libavcodec/xbmenc.c |  1 +
 50 files changed, 83 insertions(+), 30 deletions(-)

diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index 4f51485fc4..a0e5d2942e 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -1417,6 +1417,8 @@ const FFCodec ff_aac_encoder = {
 .p.long_name= NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
 .p.type = AVMEDIA_TYPE_AUDIO,
 .p.id   = AV_CODEC_ID_AAC,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
+  AV_CODEC_CAP_SMALL_LAST_FRAME,
 .priv_data_size = sizeof(AACEncContext),
 .init   = aac_encode_init,
 FF_CODEC_ENCODE_CB(aac_encode_frame),
@@ -1424,7 +1426,6 @@ const FFCodec ff_aac_encoder = {
 .defaults   = aac_encode_defaults,
 .p.supported_samplerates = ff_mpeg4audio_sample_rates,
 .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
-.p.capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,
 .p.sample_fmts  = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  AV_SAMPLE_FMT_NONE },
 .p.priv_class   = &aacenc_class,
diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c
index 10dab0a67c..20711d242f 100644
--- a/libavcodec/alacenc.c
+++ b/libavcodec/alacenc.c
@@ -654,12 +654,12 @@ const FFCodec ff_alac_encoder = {
 .p.long_name= NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio 
Codec)"),
 .p.type = AVMEDIA_TYPE_AUDIO,
 .p.id   = AV_CODEC_ID_ALAC,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME,
 .priv_data_size = sizeof(AlacEncodeContext),
 .p.priv_class   = &alacenc_class,
 .init   = alac_encode_init,
 FF_CODEC_ENCODE_CB(alac_encode_frame),
 .close  = alac_encode_close,
-.p.capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
 #if FF_API_OLD

[FFmpeg-cvslog] avcodec/(dca|tta|pcm-bluray|pcm-dvd|wavpack)enc: Set pts+dur generically

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Wed Aug 24 00:33:40 2022 +0200| [7360e97e4beec13ef5aa87657490d8f272be9f26] | 
committer: Andreas Rheinhardt

avcodec/(dca|tta|pcm-bluray|pcm-dvd|wavpack)enc: Set pts+dur generically

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=7360e97e4beec13ef5aa87657490d8f272be9f26
---

 libavcodec/dcaenc.c| 4 
 libavcodec/pcm-blurayenc.c | 3 ---
 libavcodec/pcm-dvdenc.c| 3 ---
 libavcodec/ttaenc.c| 3 ---
 libavcodec/wavpackenc.c| 3 ---
 5 files changed, 16 deletions(-)

diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c
index 2481c4d3ec..6901e67860 100644
--- a/libavcodec/dcaenc.c
+++ b/libavcodec/dcaenc.c
@@ -39,8 +39,6 @@
 #include "dcaenc.h"
 #include "encode.h"
 #include "fft.h"
-#include "internal.h"
-#include "mathops.h"
 #include "put_bits.h"
 
 #define MAX_CHANNELS 6
@@ -1215,8 +1213,6 @@ static int encode_frame(AVCodecContext *avctx, AVPacket 
*avpkt,
 flush_put_bits(&c->pb);
 memset(put_bits_ptr(&c->pb), 0, put_bytes_left(&c->pb, 0));
 
-avpkt->pts  = frame->pts;
-avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
 *got_packet_ptr = 1;
 return 0;
 }
diff --git a/libavcodec/pcm-blurayenc.c b/libavcodec/pcm-blurayenc.c
index 6a5cdb2dcd..6543bc2213 100644
--- a/libavcodec/pcm-blurayenc.c
+++ b/libavcodec/pcm-blurayenc.c
@@ -23,7 +23,6 @@
 #include "bytestream.h"
 #include "codec_internal.h"
 #include "encode.h"
-#include "internal.h"
 
 typedef struct BlurayPCMEncContext {
 uint16_t header;  // Header added to every frame
@@ -266,8 +265,6 @@ static int pcm_bluray_encode_frame(AVCodecContext *avctx, 
AVPacket *avpkt,
 return AVERROR_BUG;
 }
 
-avpkt->pts = frame->pts;
-avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
 *got_packet_ptr = 1;
 
 return 0;
diff --git a/libavcodec/pcm-dvdenc.c b/libavcodec/pcm-dvdenc.c
index a7023d148f..4bc635ab1f 100644
--- a/libavcodec/pcm-dvdenc.c
+++ b/libavcodec/pcm-dvdenc.c
@@ -24,7 +24,6 @@
 #include "bytestream.h"
 #include "codec_internal.h"
 #include "encode.h"
-#include "internal.h"
 
 typedef struct PCMDVDContext {
 uint8_t header[3];   // Header added to every frame
@@ -167,8 +166,6 @@ static int pcm_dvd_encode_frame(AVCodecContext *avctx, 
AVPacket *avpkt,
 break;
 }
 
-avpkt->pts  = frame->pts;
-avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
 *got_packet_ptr = 1;
 
 return 0;
diff --git a/libavcodec/ttaenc.c b/libavcodec/ttaenc.c
index 25113d4a72..6a8c2b122d 100644
--- a/libavcodec/ttaenc.c
+++ b/libavcodec/ttaenc.c
@@ -25,7 +25,6 @@
 #include "codec_internal.h"
 #include "encode.h"
 #include "put_bits.h"
-#include "internal.h"
 #include "libavutil/crc.h"
 
 typedef struct TTAEncContext {
@@ -188,9 +187,7 @@ pkt_alloc:
 put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ 
UINT32_MAX);
 flush_put_bits(&pb);
 
-avpkt->pts  = frame->pts;
 avpkt->size = out_bytes + 4;
-avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
 *got_packet_ptr = 1;
 return 0;
 }
diff --git a/libavcodec/wavpackenc.c b/libavcodec/wavpackenc.c
index 7f7ed804ee..07dfb22bbd 100644
--- a/libavcodec/wavpackenc.c
+++ b/libavcodec/wavpackenc.c
@@ -26,7 +26,6 @@
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
-#include "internal.h"
 #include "put_bits.h"
 #include "bytestream.h"
 #include "wavpackenc.h"
@@ -2905,9 +2904,7 @@ static int wavpack_encode_frame(AVCodecContext *avctx, 
AVPacket *avpkt,
 }
 s->sample_index += frame->nb_samples;
 
-avpkt->pts  = frame->pts;
 avpkt->size = buf - avpkt->data;
-avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
 *got_packet_ptr = 1;
 return 0;
 }

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/encode: Remove redundant check

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Tue Aug 23 21:16:00 2022 +0200| [52dcf0e0f56b4a696ba134221c03facdc166c7fc] | 
committer: Andreas Rheinhardt

avcodec/encode: Remove redundant check

frame is always set at this point for no-delay encoders.

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=52dcf0e0f56b4a696ba134221c03facdc166c7fc
---

 libavcodec/encode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index f7b13c8ba1..f66e2f9ba8 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -194,7 +194,7 @@ int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket 
*avpkt,
 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO &&
 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
 avpkt->pts = avpkt->dts = frame->pts;
-if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
+if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
 if (avpkt->pts == AV_NOPTS_VALUE)
 avpkt->pts = frame->pts;

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/internal: Fix outdated comment

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Wed Aug 24 03:19:05 2022 +0200| [5c217119c84a2b2b02b421d2c2e3aa0dee22e11f] | 
committer: Andreas Rheinhardt

avcodec/internal: Fix outdated comment

The legacy API is long gone.

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=5c217119c84a2b2b02b421d2c2e3aa0dee22e11f
---

 libavcodec/internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 52e7f111c1..e8c24d81bd 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -137,7 +137,7 @@ typedef struct AVCodecInternal {
 int draining;
 
 /**
- * buffers for using new encode/decode API through legacy API
+ * Temporary buffers for newly received or not yet output packets/frames.
  */
 AVPacket *buffer_pkt;
 AVFrame *buffer_frame;

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-cvslog] avcodec/encode: Fix outdated comment

2022-08-26 Thread Andreas Rheinhardt
ffmpeg | branch: master | Andreas Rheinhardt  | 
Wed Aug 24 03:18:47 2022 +0200| [3fdfd4b725cf84b6dd9dddb53cd37c12cb3ce76f] | 
committer: Andreas Rheinhardt

avcodec/encode: Fix outdated comment

Signed-off-by: Andreas Rheinhardt 

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=3fdfd4b725cf84b6dd9dddb53cd37c12cb3ce76f
---

 libavcodec/encode.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/encode.h b/libavcodec/encode.h
index 10c36435ad..e5d6b754b1 100644
--- a/libavcodec/encode.h
+++ b/libavcodec/encode.h
@@ -57,7 +57,7 @@ int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame 
*frame);
 /**
  * Check AVPacket size and allocate data.
  *
- * Encoders supporting FFCodec.encode2() can use this as a convenience to
+ * Encoders of type FF_CODEC_CB_TYPE_ENCODE can use this as a convenience to
  * obtain a big enough buffer for the encoded bitstream.
  *
  * @param avctx   the AVCodecContext of the encoder

___
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".