Re: [FFmpeg-devel] [PATCH] area changed: scdet filter

2024-05-28 Thread radu.taraibuta
> -Original Message-
> From: radu.taraib...@gmail.com 
> Sent: duminică, 19 mai 2024 19:05
> To: ffmpeg-devel@ffmpeg.org
> Subject: RE: [PATCH] area changed: scdet filter
> 
> 
> > -Original Message-
> > From: radu.taraib...@gmail.com 
> > Sent: luni, 13 mai 2024 18:52
> > To: ffmpeg-devel@ffmpeg.org
> > Subject: [PATCH] area changed: scdet filter
> >
> > Previous observations:
> >
> >  - Inconsistent code style with other filters. (Mostly using
> > AVFilterLink* link instead of AVFilterLink *link).
> > I hope it's fine now.
> >
> >  - Unrelated changes, please split trivial unrelated changes into
> > separate patches.
> > Removed trivial changes from this patch.
> >
> >  - Can't tables be generated at .init/.config_props time? No point in
> > storing them into binary.
> > Done.
> >
> >  - Adding extra delay is not backward compatible change, it should be
> > implemented properly by adding option for users to select mode: next &
> prev
> > frame or just next or prev frame.
> > Added legacy option to the mode parameter.
> >
> >  - Could split frame clone change into earlier separate patch.
> > Cannot be done. It's either frame clone or 1 frame delay.
> >
> >  - Where are results of improvements with accuracy so it can be
confirmed?
> > Here are my test results with manual labeling of scene changes:
> > 2379Full length movie
> >
> > Method  Threshold   TP  FP  FN  Precision
> > Recall  F
> > Cubic   7   2357423 22  0.847841727
0.990752417
> > 0.913742973
> > Cubic   10  2297200 82  0.919903885
0.965531736
> > 0.94216571
> > Cubic   12  2217146 162 0.938214135
0.931904161
> > 0.935048503
> > Cubic   15  2049101 330 0.953023256
0.861286255
> > 0.904835505
> > Linear  2.8 2357106022  0.689786362
> 0.990752417
> > 0.813319531
> > Linear  8   2099236 280 0.898929336
> 0.882303489
> > 0.890538821
> > Linear  10  1886173 493 0.91597863
> 0.792770071
> > 0.849932402
> > Legacy  5   22351260144 0.639484979
> 0.939470366
> > 0.760980592
> > Legacy  8   1998414 381 0.828358209
> 0.839848676
> > 0.83406387
> > Legacy  10  1743193 636 0.900309917
> 0.732660782
> > 0.80787949
> >
> > 15  HDR10Plus_PB_EAC3JOC
> > https://mega.nz/file/nehDka6Z#C5_OPbSZkONdOp1jRmc09C9-
> > viDc3zMj8ZHruHcWKyA
> >
> > Method  Threshold   TP  FP  FN  Precision
> > Recall  F
> > Cubic   10  15  0   0   1   1   1
> > Linear  5   13  1   2   0.928571429
> 0.86667
> > 0.896551724
> > Legacy  5   12  2   3   0.857142857 0.8
> > 0.827586207
> >
> > 21  (HDR HEVC 10-bit BT.2020 24fps) Exodus Sample
> >
> https://mega.nz/file/Sfw1hDpK#ErxCOpQDVjcI1gq6ZbX3vIfdtXZompkFe0jq47E
> > hR2o
> >
> > Method  Threshold   TP  FP  FN  Precision
> > Recall  F
> > Cubic   10  21  0   0   1   1   1
> > Linear  4   20  0   1   1   0.952380952
> > 0.975609756
> > Legacy  4   19  0   2   1   0.904761905
> 0.95
> >
> > 94  Bieber Grammys
> > https://mega.nz/#!c9dhAaKA!MG5Yi-
> > MJNATE2_KqcnNJZCRKtTWvdjJP1NwG8Ggdw3E
> >
> > Method  Threshold   TP  FP  FN  Precision
> > Recall  F
> > Cubic   15  91  23  3   0.798245614
0.968085106
> > 0.875
> > Cubic   18  85  9   9   0.904255319
0.904255319
> > 0.904255319
> > Linear  7   79  49  15  0.6171875
> 0.840425532
> > 0.711711712
> > Linear  8   74  28  20  0.725490196
> 0.787234043
> > 0.755102041
> > Legacy  7   74  40  20  0.649122807
> 0.787234043
> > 0.711538462
> > Legacy  8   71  26  23  0.731958763
> 0.755319149
> > 0.743455497
> >
> >
> > Improve scene detection accuracy by comparing frame with both previous
> > and next frame (creates one frame delay).
> > Add new mode parameter and new method to compute the frame difference
> > using cubic square to increase the weight of small changes and new
> > mean formula.
> > This improves accuracy significantly. Slightly improve performance by
> > not using frame clone.
> > Add legacy mode for backward compatibility.
> >
> > Signed-off-by: raduct 
> > ---
> >  doc/filters.texi|  16 
> >  libavfilter/scene_sad.c | 151 ++
> >  libavfilter/scene_sad.h |   6 ++
> >  libavfilter/vf_scdet.c  | 156 +---
> >  tests/fate/filter-video.mak |   3 +
> >  5 files changed, 284 insertions(+), 48 deletions(-)
> >
> > d

[FFmpeg-devel] [PATCH 0/1] avfilter/framesync: fix forward EOF pts

2024-05-28 Thread Nicolas Gaullier
This a new ping but I post the patch again to get fate cleanly completed
on patchwork.
BTW, the initial design of framesync/EOF was in n3.4-dev-1799-g4e0e9ce2dc,
so one can say that time has past...

Thank you in advance for the review.
Nicolas

Nicolas Gaullier (1):
  avfilter/framesync: fix forward EOF pts

 libavfilter/framesync.c | 23 +++
 1 file changed, 11 insertions(+), 12 deletions(-)

-- 
2.30.2

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

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


[FFmpeg-devel] [PATCH 1/1] avfilter/framesync: fix forward EOF pts

2024-05-28 Thread Nicolas Gaullier
Note1: when the EOF pts is not accurate enough, the last frame
can be dropped by vf_fps with default rounding.

Note2: vf_scale use framesync since e82a3997cdd6c0894869b33ba42430ac3,
so this is a very commonplace scenario.

For example:
./ffprobe -f lavfi testsrc=d=1,scale,fps -of flat \
  -count_frames -show_entries stream=nb_read_frames

Before:
streams.stream.0.nb_read_frames="24"

After:
streams.stream.0.nb_read_frames="25"
---
 libavfilter/framesync.c | 23 +++
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/libavfilter/framesync.c b/libavfilter/framesync.c
index 535fbe9c7c..28a992ba6d 100644
--- a/libavfilter/framesync.c
+++ b/libavfilter/framesync.c
@@ -103,14 +103,14 @@ int ff_framesync_init(FFFrameSync *fs, AVFilterContext 
*parent, unsigned nb_in)
 return 0;
 }
 
-static void framesync_eof(FFFrameSync *fs)
+static void framesync_eof(FFFrameSync *fs, int64_t pts)
 {
 fs->eof = 1;
 fs->frame_ready = 0;
-ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
+ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, pts);
 }
 
-static void framesync_sync_level_update(FFFrameSync *fs)
+static void framesync_sync_level_update(FFFrameSync *fs, int64_t eof_pts)
 {
 unsigned i, level = 0;
 
@@ -131,7 +131,7 @@ static void framesync_sync_level_update(FFFrameSync *fs)
 if (level)
 fs->sync_level = level;
 else
-framesync_eof(fs);
+framesync_eof(fs, eof_pts);
 }
 
 int ff_framesync_configure(FFFrameSync *fs)
@@ -179,7 +179,7 @@ int ff_framesync_configure(FFFrameSync *fs)
 for (i = 0; i < fs->nb_in; i++)
 fs->in[i].pts = fs->in[i].pts_next = AV_NOPTS_VALUE;
 fs->sync_level = UINT_MAX;
-framesync_sync_level_update(fs);
+framesync_sync_level_update(fs, AV_NOPTS_VALUE);
 
 return 0;
 }
@@ -200,7 +200,7 @@ static int framesync_advance(FFFrameSync *fs)
 if (fs->in[i].have_next && fs->in[i].pts_next < pts)
 pts = fs->in[i].pts_next;
 if (pts == INT64_MAX) {
-framesync_eof(fs);
+framesync_eof(fs, AV_NOPTS_VALUE);
 break;
 }
 for (i = 0; i < fs->nb_in; i++) {
@@ -222,7 +222,7 @@ static int framesync_advance(FFFrameSync *fs)
 fs->frame_ready = 1;
 if (fs->in[i].state == STATE_EOF &&
 fs->in[i].after == EXT_STOP)
-framesync_eof(fs);
+framesync_eof(fs, AV_NOPTS_VALUE);
 }
 }
 if (fs->frame_ready)
@@ -255,15 +255,14 @@ static void framesync_inject_frame(FFFrameSync *fs, 
unsigned in, AVFrame *frame)
 fs->in[in].have_next  = 1;
 }
 
-static void framesync_inject_status(FFFrameSync *fs, unsigned in, int status, 
int64_t pts)
+static void framesync_inject_status(FFFrameSync *fs, unsigned in, int status, 
int64_t eof_pts)
 {
 av_assert0(!fs->in[in].have_next);
-pts = fs->in[in].state != STATE_RUN || fs->in[in].after == EXT_INFINITY
-? INT64_MAX : framesync_pts_extrapolate(fs, in, fs->in[in].pts);
 fs->in[in].sync = 0;
-framesync_sync_level_update(fs);
+framesync_sync_level_update(fs, status == AVERROR_EOF ? eof_pts : 
AV_NOPTS_VALUE);
 fs->in[in].frame_next = NULL;
-fs->in[in].pts_next   = pts;
+fs->in[in].pts_next   = fs->in[in].state != STATE_RUN || fs->in[in].after 
== EXT_INFINITY
+? INT64_MAX : framesync_pts_extrapolate(fs, in, 
fs->in[in].pts);
 fs->in[in].have_next  = 1;
 }
 
-- 
2.30.2

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

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


Re: [FFmpeg-devel] [PATCH v2] avformat/hls: update current segment reference before use

2024-05-28 Thread J. Dekker
"J. Dekker"  writes:

> Kacper Michajłow  writes:
>
>> It may be invalidated by the time it is used.
>>
>> Fixes use after free when accessing current segment.
>>
>> Fixes: #10825
>> Signed-off-by: Kacper Michajłow 
>> ---
>>  libavformat/hls.c | 3 +++
>>  1 file changed, 3 insertions(+)
>>
>> diff --git a/libavformat/hls.c b/libavformat/hls.c
>> index 214a99c7ba..62473a15dd 100644
>> --- a/libavformat/hls.c
>> +++ b/libavformat/hls.c
>> @@ -2099,6 +2099,7 @@ static int hls_read_header(AVFormatContext *s)
>>   * If encryption scheme is SAMPLE-AES and audio setup information 
>> is present in external audio track,
>>   * use that information to find the media format, otherwise probe 
>> input data
>>   */
>> +seg = current_segment(pls);
>>  if (seg && seg->key_type == KEY_SAMPLE_AES && 
>> pls->is_id3_timestamped &&
>>  pls->audio_setup_info.codec_id != AV_CODEC_ID_NONE) {
>>  av_assert1(pls->audio_setup_info.codec_id == AV_CODEC_ID_AAC ||
>> @@ -2127,6 +2128,7 @@ static int hls_read_header(AVFormatContext *s)
>>  av_free(url);
>>  }
>>  
>> +seg = current_segment(pls);
>>  if (seg && seg->key_type == KEY_SAMPLE_AES) {
>>  if (strstr(in_fmt->name, "mov")) {
>>  char key[33];
>> @@ -2173,6 +2175,7 @@ static int hls_read_header(AVFormatContext *s)
>>   * on us if they want to.
>>   */
>>  if (pls->is_id3_timestamped || (pls->n_renditions > 0 && 
>> pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO)) {
>> +seg = current_segment(pls);
>>  if (seg && seg->key_type == KEY_SAMPLE_AES && 
>> pls->audio_setup_info.setup_data_length > 0 &&
>>  pls->ctx->nb_streams == 1)
>>  ret = 
>> ff_hls_senc_parse_audio_setup_info(pls->ctx->streams[0], 
>> &pls->audio_setup_info);
>
> This fixes several twitch.tv streams which crash for me, same as in the
> ticket. If no one has any comments I will push on Tuesday.

Pushed.

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

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


Re: [FFmpeg-devel] [PATCH] area changed: scdet filter

2024-05-28 Thread Paul B Mahol
On Tue, May 28, 2024 at 9:51 AM  wrote:

> > -Original Message-
> > From: radu.taraib...@gmail.com 
> > Sent: duminică, 19 mai 2024 19:05
> > To: ffmpeg-devel@ffmpeg.org
> > Subject: RE: [PATCH] area changed: scdet filter
> >
> >
> > > -Original Message-
> > > From: radu.taraib...@gmail.com 
> > > Sent: luni, 13 mai 2024 18:52
> > > To: ffmpeg-devel@ffmpeg.org
> > > Subject: [PATCH] area changed: scdet filter
> > >
> > > Previous observations:
> > >
> > >  - Inconsistent code style with other filters. (Mostly using
> > > AVFilterLink* link instead of AVFilterLink *link).
> > > I hope it's fine now.
> > >
> > >  - Unrelated changes, please split trivial unrelated changes into
> > > separate patches.
> > > Removed trivial changes from this patch.
> > >
> > >  - Can't tables be generated at .init/.config_props time? No point in
> > > storing them into binary.
> > > Done.
> > >
> > >  - Adding extra delay is not backward compatible change, it should be
> > > implemented properly by adding option for users to select mode: next &
> > prev
> > > frame or just next or prev frame.
> > > Added legacy option to the mode parameter.
> > >
> > >  - Could split frame clone change into earlier separate patch.
> > > Cannot be done. It's either frame clone or 1 frame delay.
> > >
> > >  - Where are results of improvements with accuracy so it can be
> confirmed?
> > > Here are my test results with manual labeling of scene changes:
> > > 2379Full length movie
> > >
> > > Method  Threshold   TP  FP  FN  Precision
> > > Recall  F
> > > Cubic   7   2357423 22  0.847841727
> 0.990752417
> > > 0.913742973
> > > Cubic   10  2297200 82  0.919903885
> 0.965531736
> > > 0.94216571
> > > Cubic   12  2217146 162 0.938214135
> 0.931904161
> > > 0.935048503
> > > Cubic   15  2049101 330 0.953023256
> 0.861286255
> > > 0.904835505
> > > Linear  2.8 2357106022  0.689786362
> > 0.990752417
> > > 0.813319531
> > > Linear  8   2099236 280 0.898929336
> > 0.882303489
> > > 0.890538821
> > > Linear  10  1886173 493 0.91597863
> > 0.792770071
> > > 0.849932402
> > > Legacy  5   22351260144 0.639484979
> > 0.939470366
> > > 0.760980592
> > > Legacy  8   1998414 381 0.828358209
> > 0.839848676
> > > 0.83406387
> > > Legacy  10  1743193 636 0.900309917
> > 0.732660782
> > > 0.80787949
> > >
> > > 15  HDR10Plus_PB_EAC3JOC
> > > https://mega.nz/file/nehDka6Z#C5_OPbSZkONdOp1jRmc09C9-
> > > viDc3zMj8ZHruHcWKyA
> > >
> > > Method  Threshold   TP  FP  FN  Precision
> > > Recall  F
> > > Cubic   10  15  0   0   1   1   1
> > > Linear  5   13  1   2   0.928571429
> > 0.86667
> > > 0.896551724
> > > Legacy  5   12  2   3   0.857142857 0.8
> > > 0.827586207
> > >
> > > 21  (HDR HEVC 10-bit BT.2020 24fps) Exodus Sample
> > >
> > https://mega.nz/file/Sfw1hDpK#ErxCOpQDVjcI1gq6ZbX3vIfdtXZompkFe0jq47E
> > > hR2o
> > >
> > > Method  Threshold   TP  FP  FN  Precision
> > > Recall  F
> > > Cubic   10  21  0   0   1   1   1
> > > Linear  4   20  0   1   1   0.952380952
> > > 0.975609756
> > > Legacy  4   19  0   2   1   0.904761905
> > 0.95
> > >
> > > 94  Bieber Grammys
> > > https://mega.nz/#!c9dhAaKA!MG5Yi-
> > > MJNATE2_KqcnNJZCRKtTWvdjJP1NwG8Ggdw3E
> > >
> > > Method  Threshold   TP  FP  FN  Precision
> > > Recall  F
> > > Cubic   15  91  23  3   0.798245614
> 0.968085106
> > > 0.875
> > > Cubic   18  85  9   9   0.904255319
> 0.904255319
> > > 0.904255319
> > > Linear  7   79  49  15  0.6171875
> > 0.840425532
> > > 0.711711712
> > > Linear  8   74  28  20  0.725490196
> > 0.787234043
> > > 0.755102041
> > > Legacy  7   74  40  20  0.649122807
> > 0.787234043
> > > 0.711538462
> > > Legacy  8   71  26  23  0.731958763
> > 0.755319149
> > > 0.743455497
> > >
> > >
> > > Improve scene detection accuracy by comparing frame with both previous
> > > and next frame (creates one frame delay).
> > > Add new mode parameter and new method to compute the frame difference
> > > using cubic square to increase the weight of small changes and new
> > > mean formula.
> > > This improves accuracy significantly. Slightly improve performance by
> > > not using frame clone.
> > > Add legacy mode for backward compatibility.
> > >
> > > Signed-off-by: raduct 
> > > ---
> > >  doc/filters.texi   

[FFmpeg-devel] [PATCH v2 03/10] lavc/hevcdec: allocate local_ctx as array of structs rather than pointers

2024-05-28 Thread Anton Khirnov
It is more efficient and easier to manage.
---
 libavcodec/hevcdec.c | 57 +---
 libavcodec/hevcdec.h |  6 -
 2 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index e84f45e3f8..88a481c043 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2598,7 +2598,7 @@ static int hls_slice_data(HEVCContext *s)
 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist,
 int job, int self_id)
 {
-HEVCLocalContext *lc = ((HEVCLocalContext**)hevc_lclist)[self_id];
+HEVCLocalContext *lc = &((HEVCLocalContext*)hevc_lclist)[self_id];
 const HEVCContext *const s = lc->parent;
 int ctb_size= 1 << s->ps.sps->log2_ctb_size;
 int more_data   = 1;
@@ -2682,7 +2682,7 @@ static int hls_slice_data_wpp(HEVCContext *s, const 
H2645NAL *nal)
 {
 const uint8_t *data = nal->data;
 int length  = nal->size;
-HEVCLocalContext *lc = s->HEVClc;
+HEVCLocalContext *lc;
 int *ret;
 int64_t offset;
 int64_t startheader, cmpt = 0;
@@ -2696,19 +2696,31 @@ static int hls_slice_data_wpp(HEVCContext *s, const 
H2645NAL *nal)
 return AVERROR_INVALIDDATA;
 }
 
-for (i = 1; i < s->threads_number; i++) {
-if (i < s->nb_local_ctx)
-continue;
-s->local_ctx[i] = av_mallocz(sizeof(HEVCLocalContext));
-if (!s->local_ctx[i])
-return AVERROR(ENOMEM);
-s->nb_local_ctx++;
+if (s->threads_number > s->nb_local_ctx) {
+HEVCLocalContext *tmp = av_malloc_array(s->threads_number, 
sizeof(*s->local_ctx));
 
-s->local_ctx[i]->logctx = s->avctx;
-s->local_ctx[i]->parent = s;
-s->local_ctx[i]->common_cabac_state = &s->cabac;
+if (!tmp)
+return AVERROR(ENOMEM);
+
+memcpy(tmp, s->local_ctx, sizeof(*s->local_ctx) * s->nb_local_ctx);
+av_free(s->local_ctx);
+s->local_ctx = tmp;
+s->HEVClc= &s->local_ctx[0];
+
+for (unsigned i = s->nb_local_ctx; i < s->threads_number; i++) {
+tmp = &s->local_ctx[i];
+
+memset(tmp, 0, sizeof(*tmp));
+
+tmp->logctx = s->avctx;
+tmp->parent = s;
+tmp->common_cabac_state = &s->cabac;
+}
+
+s->nb_local_ctx = s->threads_number;
 }
 
+lc = &s->local_ctx[0];
 offset = (lc->gb.index >> 3);
 
 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; 
j < nal->skipped_bytes; j++) {
@@ -2744,8 +2756,8 @@ static int hls_slice_data_wpp(HEVCContext *s, const 
H2645NAL *nal)
 s->data = data;
 
 for (i = 1; i < s->threads_number; i++) {
-s->local_ctx[i]->first_qp_group = 1;
-s->local_ctx[i]->qp_y = s->HEVClc->qp_y;
+s->local_ctx[i].first_qp_group = 1;
+s->local_ctx[i].qp_y = s->HEVClc->qp_y;
 }
 
 atomic_store(&s->wpp_err, 0);
@@ -3474,12 +3486,6 @@ static av_cold int hevc_decode_free(AVCodecContext 
*avctx)
 av_freep(&s->sh.offset);
 av_freep(&s->sh.size);
 
-if (s->local_ctx) {
-for (i = 1; i < s->nb_local_ctx; i++) {
-av_freep(&s->local_ctx[i]);
-}
-}
-av_freep(&s->HEVClc);
 av_freep(&s->local_ctx);
 
 ff_h2645_packet_uninit(&s->pkt);
@@ -3496,15 +3502,16 @@ static av_cold int hevc_init_context(AVCodecContext 
*avctx)
 
 s->avctx = avctx;
 
-s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
-s->local_ctx = av_mallocz(sizeof(HEVCLocalContext*) * s->threads_number);
-if (!s->HEVClc || !s->local_ctx)
+s->local_ctx = av_mallocz(sizeof(*s->local_ctx));
+if (!s->local_ctx)
 return AVERROR(ENOMEM);
+s->nb_local_ctx = 1;
+
+s->HEVClc = &s->local_ctx[0];
+
 s->HEVClc->parent = s;
 s->HEVClc->logctx = avctx;
 s->HEVClc->common_cabac_state = &s->cabac;
-s->local_ctx[0] = s->HEVClc;
-s->nb_local_ctx = 1;
 
 s->output_frame = av_frame_alloc();
 if (!s->output_frame)
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index ca68fb54a7..5aa3d40450 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -439,13 +439,17 @@ typedef struct HEVCLocalContext {
 /* properties of the boundary of the current CTB for the purposes
  * of the deblocking filter */
 int boundary_flags;
+
+// an array of these structs is used for per-thread state - pad its size
+// to avoid false sharing
+char padding[128];
 } HEVCLocalContext;
 
 typedef struct HEVCContext {
 const AVClass *c;  // needed by private avoptions
 AVCodecContext *avctx;
 
-HEVCLocalContext**local_ctx;
+HEVCLocalContext *local_ctx;
 unsigned   nb_local_ctx;
 
 HEVCLocalContext*HEVClc;
-- 
2.43.0

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

To unsubscribe

[FFmpeg-devel] [PATCH 2/2] avcodec/diracdec: Mark flush as av_cold

2024-05-28 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/diracdec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c
index 5bf0dcc2db..76209aebba 100644
--- a/libavcodec/diracdec.c
+++ b/libavcodec/diracdec.c
@@ -351,7 +351,7 @@ static int alloc_buffers(DiracContext *s, int stride)
 return 0;
 }
 
-static void free_sequence_buffers(DiracContext *s)
+static av_cold void free_sequence_buffers(DiracContext *s)
 {
 int i, j, k;
 
@@ -413,7 +413,7 @@ static av_cold int dirac_decode_init(AVCodecContext *avctx)
 return 0;
 }
 
-static void dirac_decode_flush(AVCodecContext *avctx)
+static av_cold void dirac_decode_flush(AVCodecContext *avctx)
 {
 DiracContext *s = avctx->priv_data;
 free_sequence_buffers(s);
-- 
2.40.1

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

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


[FFmpeg-devel] [PATCH 1/2] avcodec/diracdec: Use FF_CODEC_CAP_INIT_CLEANUP

2024-05-28 Thread Andreas Rheinhardt
This was one of the few decoders incompatible with the flag.
Also only call free_sequence_buffers(), dirac_decode_flush()
in dirac_decode_end().

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/diracdec.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c
index 3a36479c59..5bf0dcc2db 100644
--- a/libavcodec/diracdec.c
+++ b/libavcodec/diracdec.c
@@ -403,11 +403,8 @@ static av_cold int dirac_decode_init(AVCodecContext *avctx)
 
 for (i = 0; i < MAX_FRAMES; i++) {
 s->all_frames[i].avframe = av_frame_alloc();
-if (!s->all_frames[i].avframe) {
-while (i > 0)
-av_frame_free(&s->all_frames[--i].avframe);
+if (!s->all_frames[i].avframe)
 return AVERROR(ENOMEM);
-}
 }
 ret = ff_thread_once(&dirac_arith_init, ff_dirac_init_arith_tables);
 if (ret != 0)
@@ -429,7 +426,9 @@ static av_cold int dirac_decode_end(AVCodecContext *avctx)
 DiracContext *s = avctx->priv_data;
 int i;
 
-dirac_decode_flush(avctx);
+// Necessary in case dirac_decode_init() failed
+if (s->all_frames[MAX_FRAMES - 1].avframe)
+free_sequence_buffers(s);
 for (i = 0; i < MAX_FRAMES; i++)
 av_frame_free(&s->all_frames[i].avframe);
 
@@ -2371,4 +2370,5 @@ const FFCodec ff_dirac_decoder = {
 FF_CODEC_DECODE_CB(dirac_decode_frame),
 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS | 
AV_CODEC_CAP_DR1,
 .flush  = dirac_decode_flush,
+.caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
-- 
2.40.1

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

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


Re: [FFmpeg-devel] [PATCH 2/2] avcodec/diracdec: Mark flush as av_cold

2024-05-28 Thread Lynne via ffmpeg-devel

On 28/05/2024 16:03, Andreas Rheinhardt wrote:

Signed-off-by: Andreas Rheinhardt 
---
  libavcodec/diracdec.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c
index 5bf0dcc2db..76209aebba 100644
--- a/libavcodec/diracdec.c
+++ b/libavcodec/diracdec.c
@@ -351,7 +351,7 @@ static int alloc_buffers(DiracContext *s, int stride)
  return 0;
  }
  
-static void free_sequence_buffers(DiracContext *s)

+static av_cold void free_sequence_buffers(DiracContext *s)
  {
  int i, j, k;
  
@@ -413,7 +413,7 @@ static av_cold int dirac_decode_init(AVCodecContext *avctx)

  return 0;
  }
  
-static void dirac_decode_flush(AVCodecContext *avctx)

+static av_cold void dirac_decode_flush(AVCodecContext *avctx)
  {
  DiracContext *s = avctx->priv_data;
  free_sequence_buffers(s);


Both patches look good to me.
I'll remember to mark future decoders' flush with av_cold.


OpenPGP_0xA2FEA5F03F034464.asc
Description: OpenPGP public key


OpenPGP_signature.asc
Description: OpenPGP digital signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [PATCH 5/7] avcodec/vc2enc: Avoid relocations for short strings

2024-05-28 Thread Lynne via ffmpeg-devel

On 28/05/2024 04:49, Andreas Rheinhardt wrote:

These strings are so short that they can be put directly
into the containing structure, avoiding the pointer
and putting it into .rodata.
Also use chars for interlaced and level while at it, as
these are so small.

Signed-off-by: Andreas Rheinhardt 
---
  libavcodec/vc2enc.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libavcodec/vc2enc.c b/libavcodec/vc2enc.c
index 3285218724..8b9641916a 100644
--- a/libavcodec/vc2enc.c
+++ b/libavcodec/vc2enc.c
@@ -41,8 +41,9 @@
  typedef struct VC2BaseVideoFormat {
  enum AVPixelFormat pix_fmt;
  AVRational time_base;
-int width, height, interlaced, level;
-const char *name;
+int width, height;
+char interlaced, level;
+char name[13];
  } VC2BaseVideoFormat;
  
  static const VC2BaseVideoFormat base_video_fmts[] = {


Patchset LGTM if interlaced and level are uint8_t like other commented.


OpenPGP_0xA2FEA5F03F034464.asc
Description: OpenPGP public key


OpenPGP_signature.asc
Description: OpenPGP digital signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [PATCH v10 12/13] avcodec: add D3D12VA hardware HEVC encoder

2024-05-28 Thread Andrew Sayers
On Wed, May 22, 2024 at 09:26:25AM +0800, tong1.wu-at-intel@ffmpeg.org 
wrote:
> +static int d3d12va_create_encoder_heap(AVCodecContext* avctx)

Nitpick: s/\* / \*/

I'm trying to write typo-detection bot.  This is the only problem it noticed in
this patchset, but more nits incoming elsewhere.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [RFC 00/13] flvdec/flvenc: add support for enhanced rtmp codecs and multitrack/multichannel

2024-05-28 Thread Andrew Sayers
On Tue, May 21, 2024 at 11:02:09AM +0200, Timo Rothenpieler wrote:
> This is based on the preliminary spec for enhanced rtmp v2:
> https://veovera.org/docs/enhanced/enhanced-rtmp-v2
> 
> The spec is not final, and can still undergo breaking changes, hence this set 
> is purely for comments and review, and not ready to be merged until the final 
> v2 spec is published.
> 
> There are no samples out in the wild yet, so testing interoperability with 
> other software has not happened yet either.
> Specially the two other multitrack modes, where multiple tracks are in the 
> same packet, have not been tested at all, since no software can write such 
> files.
> 
> The set can also be found on GitHub, where ignoring whitespaces makes 
> specially the last patch a lot more readable:
> https://github.com/BtbN/FFmpeg/tree/enhanced-flv
> 

I ran this against a little review bot I'm working on.
Please do s/\* / \*/g on the following:


avformat/flvenc: add support for writing multi track audio

> +static void flv_write_multichannel_header(AVFormatContext* s, 
> AVCodecParameters* par, int64_t ts, int stream_index)


avformat/flvenc: write enhanced rtmp multichannel info for audio with more than 
two channels

> +static void flv_write_multichannel_body(AVFormatContext* s, 
> AVCodecParameters* par)
> +static int flv_get_multichannel_body_size(AVCodecParameters* par)
> +static void flv_write_multichannel_header(AVFormatContext* s, 
> AVCodecParameters* par, int64_t ts)


avformat/flvenc: add enhanced audio codecs

> +static void flv_write_aac_header(AVFormatContext* s, AVCodecParameters* par)


avformat/flvenc: Implement support for multi-track video

> +static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* 
> par, int64_t ts, int stream_index) {
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [PATCH v3 2/3] avfilter/af_volumedetect.c: Add 32bit float audio support

2024-05-28 Thread Andrew Sayers
On Mon, May 20, 2024 at 11:16:05PM +0300, Yigithan Yigit wrote:
> ---
>  libavfilter/af_volumedetect.c | 159 --
>  1 file changed, 133 insertions(+), 26 deletions(-)
> 
> diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c
> index 327801a7f9..dbbcd037a5 100644
> --- a/libavfilter/af_volumedetect.c
> +++ b/libavfilter/af_volumedetect.c
> @@ -20,27 +20,51 @@
>  
>  #include "libavutil/channel_layout.h"
>  #include "libavutil/avassert.h"
> +#include "libavutil/mem.h"
>  #include "audio.h"
>  #include "avfilter.h"
>  #include "internal.h"
>  
> +#define MAX_DB_FLT 1024
>  #define MAX_DB 91
> +#define HISTOGRAM_SIZE 0x1
> +#define HISTOGRAM_SIZE_FLT (MAX_DB_FLT*2)
>  
>  typedef struct VolDetectContext {
> -/**
> - * Number of samples at each PCM value.
> - * histogram[0x8000 + i] is the number of samples at value i.
> - * The extra element is there for symmetry.
> - */
> -uint64_t histogram[0x10001];
> +uint64_t* histogram; ///< for integer number of samples at each PCM 
> value, for float number of samples at each dB

Nitpick (from an in-progress review bot): s/\* / \*/
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [PATCH] avcodec/mediacodec: Add support of dynamic bitrate

2024-05-28 Thread Andrew Sayers
On Mon, May 27, 2024 at 01:49:47PM +0100, Dmitrii Okunev wrote:
> MediaCodec supports parameter "video-bitrate" to change the bitrate
> on fly. This commit add possibility to use it.
> 
> It adds option -bitrate_ctrl_socket to the encoder which makes
> the encoder to create an UNIX socket and listen for messages
> to change the bitrate.
> 
> An example of ffmpeg execution:
> 
> ffmpeg -listen 1 -i rtmp://0.0.0.0:1935/live/myStream -c:v 
> hevc_mediacodec -bitrate_ctrl_socket /run/bitrate.sock -b:v 8M -f rtsp 
> rtsp://127.0.0.1:1935/live/reEncoded
> 
> An example of changing the bitrate to 1000 BPS:
> 
> printf '%016X' 1000 | xxd -r -p | socat -u STDIN UNIX:/run/bitrate.sock

Nitpick: please do s/\* / \*/g on the following lines:

> +const FFAMediaFormat* format_ctx)
> +static int mediacodec_ndk_setParameters(FFAMediaCodec* ctx,
> +const FFAMediaFormat* format_ctx)
> +int (*setParameters)(FFAMediaCodec* codec, const FFAMediaFormat* format);

(found by an in-progress review bot)
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


[FFmpeg-devel] [PATCH v12 01/15] avcodec/vaapi_encode: introduce a base layer for vaapi encode

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Since VAAPI and future D3D12VA implementation may share some common parameters,
a base layer encode context is introduced as vaapi context's base.

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.h | 56 +
 libavcodec/vaapi_encode.h   | 39 +-
 2 files changed, 63 insertions(+), 32 deletions(-)
 create mode 100644 libavcodec/hw_base_encode.h

diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
new file mode 100644
index 00..ffcb6bcbb2
--- /dev/null
+++ b/libavcodec/hw_base_encode.h
@@ -0,0 +1,56 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_HW_BASE_ENCODE_H
+#define AVCODEC_HW_BASE_ENCODE_H
+
+#define MAX_DPB_SIZE 16
+#define MAX_PICTURE_REFERENCES 2
+#define MAX_REORDER_DELAY 16
+#define MAX_ASYNC_DEPTH 64
+#define MAX_REFERENCE_LIST_NUM 2
+
+enum {
+PICTURE_TYPE_IDR = 0,
+PICTURE_TYPE_I   = 1,
+PICTURE_TYPE_P   = 2,
+PICTURE_TYPE_B   = 3,
+};
+
+enum {
+// Codec supports controlling the subdivision of pictures into slices.
+FLAG_SLICE_CONTROL = 1 << 0,
+// Codec only supports constant quality (no rate control).
+FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
+// Codec is intra-only.
+FLAG_INTRA_ONLY= 1 << 2,
+// Codec supports B-pictures.
+FLAG_B_PICTURES= 1 << 3,
+// Codec supports referencing B-pictures.
+FLAG_B_PICTURE_REFERENCES  = 1 << 4,
+// Codec supports non-IDR key pictures (that is, key pictures do
+// not necessarily empty the DPB).
+FLAG_NON_IDR_KEY_PICTURES  = 1 << 5,
+};
+
+typedef struct FFHWBaseEncodeContext {
+const AVClass *class;
+} FFHWBaseEncodeContext;
+
+#endif /* AVCODEC_HW_BASE_ENCODE_H */
+
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
index 0eed9691ca..cae7af8725 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -33,34 +33,27 @@
 
 #include "avcodec.h"
 #include "hwconfig.h"
+#include "hw_base_encode.h"
 
 struct VAAPIEncodeType;
 struct VAAPIEncodePicture;
 
+// Codec output packet without timestamp delay, which means the
+// output packet has same PTS and DTS.
+#define FLAG_TIMESTAMP_NO_DELAY 1 << 6
+
 enum {
 MAX_CONFIG_ATTRIBUTES  = 4,
 MAX_GLOBAL_PARAMS  = 4,
-MAX_DPB_SIZE   = 16,
-MAX_PICTURE_REFERENCES = 2,
-MAX_REORDER_DELAY  = 16,
 MAX_PARAM_BUFFER_SIZE  = 1024,
 // A.4.1: table A.6 allows at most 22 tile rows for any level.
 MAX_TILE_ROWS  = 22,
 // A.4.1: table A.6 allows at most 20 tile columns for any level.
 MAX_TILE_COLS  = 20,
-MAX_ASYNC_DEPTH= 64,
-MAX_REFERENCE_LIST_NUM = 2,
 };
 
 extern const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[];
 
-enum {
-PICTURE_TYPE_IDR = 0,
-PICTURE_TYPE_I   = 1,
-PICTURE_TYPE_P   = 2,
-PICTURE_TYPE_B   = 3,
-};
-
 typedef struct VAAPIEncodeSlice {
 int index;
 int row_start;
@@ -193,7 +186,8 @@ typedef struct VAAPIEncodeRCMode {
 } VAAPIEncodeRCMode;
 
 typedef struct VAAPIEncodeContext {
-const AVClass *class;
+// Base context.
+FFHWBaseEncodeContext base;
 
 // Codec-specific hooks.
 const struct VAAPIEncodeType *codec;
@@ -397,25 +391,6 @@ typedef struct VAAPIEncodeContext {
 AVPacket*tail_pkt;
 } VAAPIEncodeContext;
 
-enum {
-// Codec supports controlling the subdivision of pictures into slices.
-FLAG_SLICE_CONTROL = 1 << 0,
-// Codec only supports constant quality (no rate control).
-FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
-// Codec is intra-only.
-FLAG_INTRA_ONLY= 1 << 2,
-// Codec supports B-pictures.
-FLAG_B_PICTURES= 1 << 3,
-// Codec supports referencing B-pictures.
-FLAG_B_PICTURE_REFERENCES  = 1 << 4,
-// Codec supports non-IDR key pictures (that is, key pictures do
-// not necessarily empty the DPB).
-FLAG_NON_IDR_KEY_PICTURES  = 1 << 5,
-// Codec output packet without timestamp delay, which means the
-// output packet has same PTS and DTS.
-FLAG_TIMESTAMP_NO_DELAY= 1 << 6,
-};
-
 typedef struct VAAPIEncodeType {
 // List of supported profiles and corr

[FFmpeg-devel] [PATCH v12 02/15] avcodec/hw_base_encode: add FF_HW_ prefix for two enums

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

PICTURE_TYPE_* and FLAG_* are added FF_HW_ prefix after being moved to
base layer.

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.h | 20 ++---
 libavcodec/vaapi_encode.c   | 50 -
 libavcodec/vaapi_encode_av1.c   | 12 
 libavcodec/vaapi_encode_h264.c  | 50 -
 libavcodec/vaapi_encode_h265.c  | 44 ++---
 libavcodec/vaapi_encode_mjpeg.c |  6 ++--
 libavcodec/vaapi_encode_mpeg2.c | 30 ++--
 libavcodec/vaapi_encode_vp8.c   | 10 +++
 libavcodec/vaapi_encode_vp9.c   | 16 +--
 9 files changed, 119 insertions(+), 119 deletions(-)

diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index ffcb6bcbb2..f3c9f32977 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -26,26 +26,26 @@
 #define MAX_REFERENCE_LIST_NUM 2
 
 enum {
-PICTURE_TYPE_IDR = 0,
-PICTURE_TYPE_I   = 1,
-PICTURE_TYPE_P   = 2,
-PICTURE_TYPE_B   = 3,
+FF_HW_PICTURE_TYPE_IDR = 0,
+FF_HW_PICTURE_TYPE_I   = 1,
+FF_HW_PICTURE_TYPE_P   = 2,
+FF_HW_PICTURE_TYPE_B   = 3,
 };
 
 enum {
 // Codec supports controlling the subdivision of pictures into slices.
-FLAG_SLICE_CONTROL = 1 << 0,
+FF_HW_FLAG_SLICE_CONTROL = 1 << 0,
 // Codec only supports constant quality (no rate control).
-FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
+FF_HW_FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
 // Codec is intra-only.
-FLAG_INTRA_ONLY= 1 << 2,
+FF_HW_FLAG_INTRA_ONLY= 1 << 2,
 // Codec supports B-pictures.
-FLAG_B_PICTURES= 1 << 3,
+FF_HW_FLAG_B_PICTURES= 1 << 3,
 // Codec supports referencing B-pictures.
-FLAG_B_PICTURE_REFERENCES  = 1 << 4,
+FF_HW_FLAG_B_PICTURE_REFERENCES  = 1 << 4,
 // Codec supports non-IDR key pictures (that is, key pictures do
 // not necessarily empty the DPB).
-FLAG_NON_IDR_KEY_PICTURES  = 1 << 5,
+FF_HW_FLAG_NON_IDR_KEY_PICTURES  = 1 << 5,
 };
 
 typedef struct FFHWBaseEncodeContext {
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index f54b2579ec..3c3d6a37c7 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -345,7 +345,7 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
 
 pic->nb_param_buffers = 0;
 
-if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
+if (pic->type == FF_HW_PICTURE_TYPE_IDR && 
ctx->codec->init_sequence_params) {
 err = vaapi_encode_make_param_buffer(avctx, pic,
  VAEncSequenceParameterBufferType,
  ctx->codec_sequence_params,
@@ -354,7 +354,7 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
 goto fail;
 }
 
-if (pic->type == PICTURE_TYPE_IDR) {
+if (pic->type == FF_HW_PICTURE_TYPE_IDR) {
 for (i = 0; i < ctx->nb_global_params; i++) {
 err = vaapi_encode_make_misc_param_buffer(avctx, pic,
   
ctx->global_params_type[i],
@@ -391,7 +391,7 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
 }
 #endif
 
-if (pic->type == PICTURE_TYPE_IDR) {
+if (pic->type == FF_HW_PICTURE_TYPE_IDR) {
 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
 ctx->codec->write_sequence_header) {
 bit_len = 8 * sizeof(data);
@@ -671,7 +671,7 @@ static int vaapi_encode_set_output_property(AVCodecContext 
*avctx,
 {
 VAAPIEncodeContext *ctx = avctx->priv_data;
 
-if (pic->type == PICTURE_TYPE_IDR)
+if (pic->type == FF_HW_PICTURE_TYPE_IDR)
 pkt->flags |= AV_PKT_FLAG_KEY;
 
 pkt->pts = pic->pts;
@@ -996,7 +996,7 @@ static void vaapi_encode_remove_refs(AVCodecContext *avctx,
 av_assert0(pic->dpb[i]->ref_count[level] >= 0);
 }
 
-av_assert0(pic->prev || pic->type == PICTURE_TYPE_IDR);
+av_assert0(pic->prev || pic->type == FF_HW_PICTURE_TYPE_IDR);
 if (pic->prev) {
 --pic->prev->ref_count[level];
 av_assert0(pic->prev->ref_count[level] >= 0);
@@ -1025,7 +1025,7 @@ static void vaapi_encode_set_b_pictures(AVCodecContext 
*avctx,
 for (pic = start->next; pic; pic = pic->next) {
 if (pic == end)
 break;
-pic->type= PICTURE_TYPE_B;
+pic->type= FF_HW_PICTURE_TYPE_B;
 pic->b_depth = current_depth;
 
 vaapi_encode_add_ref(avctx, pic, start, 1, 1, 0);
@@ -1045,7 +1045,7 @@ static void vaapi_encode_set_b_pictures(AVCodecContext 
*avctx,
 ++len;
 for (pic = start->next, i = 1; 2 * i < len; pic = pic->next, i++);
 
-pic->type= PICTURE_TYPE_B;
+pic->type= FF_HW_PICTURE_TYPE_B;
 pic->b_depth = current_depth;
 
 pic->is_reference = 1;
@@ -1078,7 +1078,7 @@ static void vaapi_enco

[FFmpeg-devel] [PATCH v12 03/15] avcodec/vaapi_encode: add async_depth to common options

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.h | 10 +-
 libavcodec/vaapi_encode.c   | 13 -
 libavcodec/vaapi_encode.h   |  7 ---
 libavcodec/vaapi_encode_av1.c   |  1 +
 libavcodec/vaapi_encode_h264.c  |  1 +
 libavcodec/vaapi_encode_h265.c  |  1 +
 libavcodec/vaapi_encode_mjpeg.c |  1 +
 libavcodec/vaapi_encode_mpeg2.c |  1 +
 libavcodec/vaapi_encode_vp8.c   |  1 +
 libavcodec/vaapi_encode_vp9.c   |  1 +
 10 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index f3c9f32977..c14c174102 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -50,7 +50,15 @@ enum {
 
 typedef struct FFHWBaseEncodeContext {
 const AVClass *class;
+
+// Max number of frame buffered in encoder.
+int async_depth;
 } FFHWBaseEncodeContext;
 
-#endif /* AVCODEC_HW_BASE_ENCODE_H */
+#define HW_BASE_ENCODE_COMMON_OPTIONS \
+{ "async_depth", "Maximum processing parallelism. " \
+  "Increase this to improve single channel performance.", \
+  OFFSET(common.base.async_depth), AV_OPT_TYPE_INT, \
+  { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }
 
+#endif /* AVCODEC_HW_BASE_ENCODE_H */
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 3c3d6a37c7..77f539dc6d 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -669,7 +669,8 @@ static int vaapi_encode_set_output_property(AVCodecContext 
*avctx,
 VAAPIEncodePicture *pic,
 AVPacket *pkt)
 {
-VAAPIEncodeContext *ctx = avctx->priv_data;
+FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
+VAAPIEncodeContext *ctx = avctx->priv_data;
 
 if (pic->type == FF_HW_PICTURE_TYPE_IDR)
 pkt->flags |= AV_PKT_FLAG_KEY;
@@ -699,7 +700,7 @@ static int vaapi_encode_set_output_property(AVCodecContext 
*avctx,
 pkt->dts = ctx->ts_ring[pic->encode_order] - ctx->dts_pts_diff;
 } else {
 pkt->dts = ctx->ts_ring[(pic->encode_order - ctx->decode_delay) %
-(3 * ctx->output_delay + ctx->async_depth)];
+(3 * ctx->output_delay + 
base_ctx->async_depth)];
 }
 
 return 0;
@@ -1320,6 +1321,7 @@ static int vaapi_encode_check_frame(AVCodecContext *avctx,
 
 static int vaapi_encode_send_frame(AVCodecContext *avctx, AVFrame *frame)
 {
+FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
 VAAPIEncodeContext *ctx = avctx->priv_data;
 VAAPIEncodePicture *pic;
 int err;
@@ -1365,7 +1367,7 @@ static int vaapi_encode_send_frame(AVCodecContext *avctx, 
AVFrame *frame)
 ctx->dts_pts_diff = pic->pts - ctx->first_pts;
 if (ctx->output_delay > 0)
 ctx->ts_ring[ctx->input_order %
-(3 * ctx->output_delay + ctx->async_depth)] = pic->pts;
+(3 * ctx->output_delay + base_ctx->async_depth)] = 
pic->pts;
 
 pic->display_order = ctx->input_order;
 ++ctx->input_order;
@@ -2773,7 +2775,8 @@ static av_cold int 
vaapi_encode_create_recon_frames(AVCodecContext *avctx)
 
 av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
 {
-VAAPIEncodeContext *ctx = avctx->priv_data;
+FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
+VAAPIEncodeContext *ctx = avctx->priv_data;
 AVVAAPIFramesContext *recon_hwctx = NULL;
 VAStatus vas;
 int err;
@@ -2966,7 +2969,7 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
 vas = vaSyncBuffer(ctx->hwctx->display, VA_INVALID_ID, 0);
 if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) {
 ctx->has_sync_buffer_func = 1;
-ctx->encode_fifo = av_fifo_alloc2(ctx->async_depth,
+ctx->encode_fifo = av_fifo_alloc2(base_ctx->async_depth,
   sizeof(VAAPIEncodePicture *),
   0);
 if (!ctx->encode_fifo)
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
index cae7af8725..9fdb945b18 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -374,8 +374,6 @@ typedef struct VAAPIEncodeContext {
 int has_sync_buffer_func;
 // Store buffered pic
 AVFifo  *encode_fifo;
-// Max number of frame buffered in encoder.
-int async_depth;
 
 /** Head data for current output pkt, used only for AV1. */
 //void  *header_data;
@@ -491,11 +489,6 @@ int ff_vaapi_encode_close(AVCodecContext *avctx);
   "Maximum B-frame reference depth", \
   OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
   { .i64 = 1 }, 1, INT_MAX, FLAGS }, \
-{ "async_depth", "Maximum processing parallelism. " \
-  "Increase this to improve single channel performance. This option " \
-  "doesn't work if driver doesn't implement vaSyncBuffer function.", \
-  OFFSET(co

[FFmpeg-devel] [PATCH v12 04/15] avcodec/vaapi_encode: add picture type name to base

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.h | 6 ++
 libavcodec/vaapi_encode.c   | 4 +---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index c14c174102..858450afa8 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -25,6 +25,12 @@
 #define MAX_ASYNC_DEPTH 64
 #define MAX_REFERENCE_LIST_NUM 2
 
+static inline const char *ff_hw_base_encode_get_pictype_name(const int type)
+{
+const char * const picture_type_name[] = { "IDR", "I", "P", "B" };
+return picture_type_name[type];
+}
+
 enum {
 FF_HW_PICTURE_TYPE_IDR = 0,
 FF_HW_PICTURE_TYPE_I   = 1,
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 77f539dc6d..54bdd73902 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -38,8 +38,6 @@ const AVCodecHWConfigInternal *const 
ff_vaapi_encode_hw_configs[] = {
 NULL,
 };
 
-static const char * const picture_type_name[] = { "IDR", "I", "P", "B" };
-
 static int vaapi_encode_make_packed_header(AVCodecContext *avctx,
VAAPIEncodePicture *pic,
int type, char *data, size_t 
bit_len)
@@ -277,7 +275,7 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
 
 av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
"as type %s.\n", pic->display_order, pic->encode_order,
-   picture_type_name[pic->type]);
+   ff_hw_base_encode_get_pictype_name(pic->type));
 if (pic->nb_refs[0] == 0 && pic->nb_refs[1] == 0) {
 av_log(avctx, AV_LOG_DEBUG, "No reference pictures.\n");
 } else {
-- 
2.41.0.windows.1

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

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


[FFmpeg-devel] [PATCH v12 05/15] avcodec/vaapi_encode: move pic->input_surface initialization to encode_alloc

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

When allocating the VAAPIEncodePicture, pic->input_surface can be
initialized right in the place. This movement simplifies the send_frame
logic and is the preparation for moving vaapi_encode_send_frame to the base 
layer.

Signed-off-by: Tong Wu 
---
 libavcodec/vaapi_encode.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 54bdd73902..194422b36d 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -878,7 +878,8 @@ static int vaapi_encode_discard(AVCodecContext *avctx,
 return 0;
 }
 
-static VAAPIEncodePicture *vaapi_encode_alloc(AVCodecContext *avctx)
+static VAAPIEncodePicture *vaapi_encode_alloc(AVCodecContext *avctx,
+  const AVFrame *frame)
 {
 VAAPIEncodeContext *ctx = avctx->priv_data;
 VAAPIEncodePicture *pic;
@@ -895,7 +896,7 @@ static VAAPIEncodePicture 
*vaapi_encode_alloc(AVCodecContext *avctx)
 }
 }
 
-pic->input_surface = VA_INVALID_ID;
+pic->input_surface = (VASurfaceID)(uintptr_t)frame->data[3];
 pic->recon_surface = VA_INVALID_ID;
 pic->output_buffer = VA_INVALID_ID;
 
@@ -1332,7 +1333,7 @@ static int vaapi_encode_send_frame(AVCodecContext *avctx, 
AVFrame *frame)
 if (err < 0)
 return err;
 
-pic = vaapi_encode_alloc(avctx);
+pic = vaapi_encode_alloc(avctx, frame);
 if (!pic)
 return AVERROR(ENOMEM);
 
@@ -1345,7 +1346,6 @@ static int vaapi_encode_send_frame(AVCodecContext *avctx, 
AVFrame *frame)
 if (ctx->input_order == 0 || frame->pict_type == AV_PICTURE_TYPE_I)
 pic->force_idr = 1;
 
-pic->input_surface = (VASurfaceID)(uintptr_t)frame->data[3];
 pic->pts = frame->pts;
 pic->duration = frame->duration;
 
-- 
2.41.0.windows.1

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

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


[FFmpeg-devel] [PATCH v12 08/15] avcodec/vaapi_encode: extract gop configuration and two options to base layer

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

idr_interval and desired_b_depth are moved to HW_BASE_ENCODE_COMMON_OPTIONS.

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.c | 54 +
 libavcodec/hw_base_encode.h | 19 +
 libavcodec/vaapi_encode.c   | 52 +++
 libavcodec/vaapi_encode.h   | 16 ---
 4 files changed, 77 insertions(+), 64 deletions(-)

diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index c4789380b6..56dc015e2e 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -593,6 +593,60 @@ end:
 return 0;
 }
 
+int ff_hw_base_init_gop_structure(AVCodecContext *avctx, uint32_t ref_l0, 
uint32_t ref_l1,
+  int flags, int prediction_pre_only)
+{
+FFHWBaseEncodeContext *ctx = avctx->priv_data;
+
+if (flags & FF_HW_FLAG_INTRA_ONLY || avctx->gop_size <= 1) {
+av_log(avctx, AV_LOG_VERBOSE, "Using intra frames only.\n");
+ctx->gop_size = 1;
+} else if (ref_l0 < 1) {
+av_log(avctx, AV_LOG_ERROR, "Driver does not support any "
+   "reference frames.\n");
+return AVERROR(EINVAL);
+} else if (!(flags & FF_HW_FLAG_B_PICTURES) || ref_l1 < 1 ||
+   avctx->max_b_frames < 1 || prediction_pre_only) {
+if (ctx->p_to_gpb)
+   av_log(avctx, AV_LOG_VERBOSE, "Using intra and B-frames "
+  "(supported references: %d / %d).\n",
+  ref_l0, ref_l1);
+else
+av_log(avctx, AV_LOG_VERBOSE, "Using intra and P-frames "
+   "(supported references: %d / %d).\n", ref_l0, ref_l1);
+ctx->gop_size = avctx->gop_size;
+ctx->p_per_i  = INT_MAX;
+ctx->b_per_p  = 0;
+} else {
+   if (ctx->p_to_gpb)
+   av_log(avctx, AV_LOG_VERBOSE, "Using intra and B-frames "
+  "(supported references: %d / %d).\n",
+  ref_l0, ref_l1);
+   else
+   av_log(avctx, AV_LOG_VERBOSE, "Using intra, P- and B-frames "
+  "(supported references: %d / %d).\n", ref_l0, ref_l1);
+ctx->gop_size = avctx->gop_size;
+ctx->p_per_i  = INT_MAX;
+ctx->b_per_p  = avctx->max_b_frames;
+if (flags & FF_HW_FLAG_B_PICTURE_REFERENCES) {
+ctx->max_b_depth = FFMIN(ctx->desired_b_depth,
+ av_log2(ctx->b_per_p) + 1);
+} else {
+ctx->max_b_depth = 1;
+}
+}
+
+if (flags & FF_HW_FLAG_NON_IDR_KEY_PICTURES) {
+ctx->closed_gop  = !!(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP);
+ctx->gop_per_idr = ctx->idr_interval + 1;
+} else {
+ctx->closed_gop  = 1;
+ctx->gop_per_idr = 1;
+}
+
+return 0;
+}
+
 int ff_hw_base_encode_init(AVCodecContext *avctx)
 {
 FFHWBaseEncodeContext *ctx = avctx->priv_data;
diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index a062009860..c7e535d35d 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -120,6 +120,14 @@ typedef struct FFHWBaseEncodeContext {
 // Hardware-specific hooks.
 const struct FFHWEncodePictureOperation *op;
 
+// Global options.
+
+// Number of I frames between IDR frames.
+int idr_interval;
+
+// Desired B frame reference depth.
+int desired_b_depth;
+
 // The hardware device context.
 AVBufferRef*device_ref;
 AVHWDeviceContext *device;
@@ -198,11 +206,22 @@ typedef struct FFHWBaseEncodeContext {
 
 int ff_hw_base_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
 
+int ff_hw_base_init_gop_structure(AVCodecContext *avctx, uint32_t ref_l0, 
uint32_t ref_l1,
+  int flags, int prediction_pre_only);
+
 int ff_hw_base_encode_init(AVCodecContext *avctx);
 
 int ff_hw_base_encode_close(AVCodecContext *avctx);
 
 #define HW_BASE_ENCODE_COMMON_OPTIONS \
+{ "idr_interval", \
+  "Distance (in I-frames) between key frames", \
+  OFFSET(common.base.idr_interval), AV_OPT_TYPE_INT, \
+  { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
+{ "b_depth", \
+  "Maximum B-frame reference depth", \
+  OFFSET(common.base.desired_b_depth), AV_OPT_TYPE_INT, \
+  { .i64 = 1 }, 1, INT_MAX, FLAGS }, \
 { "async_depth", "Maximum processing parallelism. " \
   "Increase this to improve single channel performance.", \
   OFFSET(common.base.async_depth), AV_OPT_TYPE_INT, \
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 1b3bab2c14..c29493e2c8 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -1638,7 +1638,7 @@ static av_cold int 
vaapi_encode_init_gop_structure(AVCodecContext *avctx)
 VAStatus vas;
 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
 uint32_t ref_l0, ref_l1;
-int prediction_pre_only;
+int prediction_pre_only, err;
 
 vas = vaGetConfigAttributes(ctx->hwctx-

[FFmpeg-devel] [PATCH v12 06/15] avcodec/vaapi_encode: move the dpb logic from VAAPI to base layer

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Move receive_packet function to base. This requires adding *alloc,
*issue, *output, *free as hardware callbacks. HWBaseEncodePicture is
introduced as the base layer structure. The related parameters in
VAAPIEncodeContext are also extracted to HWBaseEncodeContext. Then DPB
management logic can be fully extracted to base layer as-is.

Signed-off-by: Tong Wu 
---
 libavcodec/Makefile |   2 +-
 libavcodec/hw_base_encode.c | 594 
 libavcodec/hw_base_encode.h | 124 +
 libavcodec/vaapi_encode.c   | 793 +---
 libavcodec/vaapi_encode.h   | 102 +---
 libavcodec/vaapi_encode_av1.c   |  35 +-
 libavcodec/vaapi_encode_h264.c  |  84 ++--
 libavcodec/vaapi_encode_h265.c  |  53 ++-
 libavcodec/vaapi_encode_mjpeg.c |  13 +-
 libavcodec/vaapi_encode_mpeg2.c |  33 +-
 libavcodec/vaapi_encode_vp8.c   |  18 +-
 libavcodec/vaapi_encode_vp9.c   |  24 +-
 12 files changed, 985 insertions(+), 890 deletions(-)
 create mode 100644 libavcodec/hw_base_encode.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 2443d2c6fd..998f6b7e12 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -165,7 +165,7 @@ OBJS-$(CONFIG_STARTCODE)   += startcode.o
 OBJS-$(CONFIG_TEXTUREDSP)  += texturedsp.o
 OBJS-$(CONFIG_TEXTUREDSPENC)   += texturedspenc.o
 OBJS-$(CONFIG_TPELDSP) += tpeldsp.o
-OBJS-$(CONFIG_VAAPI_ENCODE)+= vaapi_encode.o
+OBJS-$(CONFIG_VAAPI_ENCODE)+= vaapi_encode.o hw_base_encode.o
 OBJS-$(CONFIG_AV1_AMF_ENCODER) += amfenc_av1.o
 OBJS-$(CONFIG_VC1DSP)  += vc1dsp.o
 OBJS-$(CONFIG_VIDEODSP)+= videodsp.o
diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
new file mode 100644
index 00..16afaa37be
--- /dev/null
+++ b/libavcodec/hw_base_encode.c
@@ -0,0 +1,594 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/common.h"
+#include "libavutil/internal.h"
+#include "libavutil/log.h"
+#include "libavutil/mem.h"
+#include "libavutil/pixdesc.h"
+
+#include "encode.h"
+#include "avcodec.h"
+#include "hw_base_encode.h"
+
+static void hw_base_encode_add_ref(FFHWBaseEncodePicture *pic,
+   FFHWBaseEncodePicture *target,
+   int is_ref, int in_dpb, int prev)
+{
+int refs = 0;
+
+if (is_ref) {
+av_assert0(pic != target);
+av_assert0(pic->nb_refs[0] < MAX_PICTURE_REFERENCES &&
+   pic->nb_refs[1] < MAX_PICTURE_REFERENCES);
+if (target->display_order < pic->display_order)
+pic->refs[0][pic->nb_refs[0]++] = target;
+else
+pic->refs[1][pic->nb_refs[1]++] = target;
+++refs;
+}
+
+if (in_dpb) {
+av_assert0(pic->nb_dpb_pics < MAX_DPB_SIZE);
+pic->dpb[pic->nb_dpb_pics++] = target;
+++refs;
+}
+
+if (prev) {
+av_assert0(!pic->prev);
+pic->prev = target;
+++refs;
+}
+
+target->ref_count[0] += refs;
+target->ref_count[1] += refs;
+}
+
+static void hw_base_encode_remove_refs(FFHWBaseEncodePicture *pic, int level)
+{
+int i;
+
+if (pic->ref_removed[level])
+return;
+
+for (i = 0; i < pic->nb_refs[0]; i++) {
+av_assert0(pic->refs[0][i]);
+--pic->refs[0][i]->ref_count[level];
+av_assert0(pic->refs[0][i]->ref_count[level] >= 0);
+}
+
+for (i = 0; i < pic->nb_refs[1]; i++) {
+av_assert0(pic->refs[1][i]);
+--pic->refs[1][i]->ref_count[level];
+av_assert0(pic->refs[1][i]->ref_count[level] >= 0);
+}
+
+for (i = 0; i < pic->nb_dpb_pics; i++) {
+av_assert0(pic->dpb[i]);
+--pic->dpb[i]->ref_count[level];
+av_assert0(pic->dpb[i]->ref_count[level] >= 0);
+}
+
+av_assert0(pic->prev || pic->type == FF_HW_PICTURE_TYPE_IDR);
+if (pic->prev) {
+--pic->prev->ref_count[level];
+av_assert0(pic->prev->ref_count[level] >= 0);
+}
+
+pic->ref_removed[level] = 1;
+}
+
+static void hw_base_encode_set_b_pictures(AVCodecContext *avctx,
+  FFHWBaseEncodePicture *st

[FFmpeg-devel] [PATCH v12 09/15] avcodec/vaapi_encode: extract set_output_property to base layer

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.c | 40 +
 libavcodec/hw_base_encode.h |  3 +++
 libavcodec/vaapi_encode.c   | 44 ++---
 3 files changed, 45 insertions(+), 42 deletions(-)

diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 56dc015e2e..25fcfdbb5e 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -488,6 +488,46 @@ fail:
 return err;
 }
 
+int ff_hw_base_encode_set_output_property(AVCodecContext *avctx,
+  FFHWBaseEncodePicture *pic,
+  AVPacket *pkt, int flag_no_delay)
+{
+FFHWBaseEncodeContext *ctx = avctx->priv_data;
+
+if (pic->type == FF_HW_PICTURE_TYPE_IDR)
+pkt->flags |= AV_PKT_FLAG_KEY;
+
+pkt->pts = pic->pts;
+pkt->duration = pic->duration;
+
+// for no-delay encoders this is handled in generic codec
+if (avctx->codec->capabilities & AV_CODEC_CAP_DELAY &&
+avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
+pkt->opaque  = pic->opaque;
+pkt->opaque_ref  = pic->opaque_ref;
+pic->opaque_ref = NULL;
+}
+
+if (flag_no_delay) {
+pkt->dts = pkt->pts;
+return 0;
+}
+
+if (ctx->output_delay == 0) {
+pkt->dts = pkt->pts;
+} else if (pic->encode_order < ctx->decode_delay) {
+if (ctx->ts_ring[pic->encode_order] < INT64_MIN + ctx->dts_pts_diff)
+pkt->dts = INT64_MIN;
+else
+pkt->dts = ctx->ts_ring[pic->encode_order] - ctx->dts_pts_diff;
+} else {
+pkt->dts = ctx->ts_ring[(pic->encode_order - ctx->decode_delay) %
+(3 * ctx->output_delay + ctx->async_depth)];
+}
+
+return 0;
+}
+
 int ff_hw_base_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
 {
 FFHWBaseEncodeContext *ctx = avctx->priv_data;
diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index c7e535d35d..f097d826a7 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -204,6 +204,9 @@ typedef struct FFHWBaseEncodeContext {
 AVPacket*tail_pkt;
 } FFHWBaseEncodeContext;
 
+int ff_hw_base_encode_set_output_property(AVCodecContext *avctx, 
FFHWBaseEncodePicture *pic,
+  AVPacket *pkt, int flag_no_delay);
+
 int ff_hw_base_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
 
 int ff_hw_base_init_gop_structure(AVCodecContext *avctx, uint32_t ref_l0, 
uint32_t ref_l1,
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index c29493e2c8..4193f3838f 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -660,47 +660,6 @@ fail_at_end:
 return err;
 }
 
-static int vaapi_encode_set_output_property(AVCodecContext *avctx,
-FFHWBaseEncodePicture *pic,
-AVPacket *pkt)
-{
-FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
-VAAPIEncodeContext *ctx = avctx->priv_data;
-
-if (pic->type == FF_HW_PICTURE_TYPE_IDR)
-pkt->flags |= AV_PKT_FLAG_KEY;
-
-pkt->pts = pic->pts;
-pkt->duration = pic->duration;
-
-// for no-delay encoders this is handled in generic codec
-if (avctx->codec->capabilities & AV_CODEC_CAP_DELAY &&
-avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
-pkt->opaque = pic->opaque;
-pkt->opaque_ref = pic->opaque_ref;
-pic->opaque_ref = NULL;
-}
-
-if (ctx->codec->flags & FLAG_TIMESTAMP_NO_DELAY) {
-pkt->dts = pkt->pts;
-return 0;
-}
-
-if (base_ctx->output_delay == 0) {
-pkt->dts = pkt->pts;
-} else if (pic->encode_order < base_ctx->decode_delay) {
-if (base_ctx->ts_ring[pic->encode_order] < INT64_MIN + 
base_ctx->dts_pts_diff)
-pkt->dts = INT64_MIN;
-else
-pkt->dts = base_ctx->ts_ring[pic->encode_order] - 
base_ctx->dts_pts_diff;
-} else {
-pkt->dts = base_ctx->ts_ring[(pic->encode_order - 
base_ctx->decode_delay) %
- (3 * base_ctx->output_delay + 
base_ctx->async_depth)];
-}
-
-return 0;
-}
-
 static int vaapi_encode_get_coded_buffer_size(AVCodecContext *avctx, 
VABufferID buf_id)
 {
 VAAPIEncodeContext *ctx = avctx->priv_data;
@@ -852,7 +811,8 @@ static int vaapi_encode_output(AVCodecContext *avctx,
 av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
base_pic->display_order, base_pic->encode_order);
 
-vaapi_encode_set_output_property(avctx, (FFHWBaseEncodePicture*)pic, 
pkt_ptr);
+ff_hw_base_encode_set_output_property(avctx, 
(FFHWBaseEncodePicture*)base_pic, pkt_ptr,
+  ctx->codec->flags & 
FLAG_TIMESTAMP_NO_DELAY);
 
 end:
 ff_refstruct_unref(&pic->output_buffer_ref);
-- 
2.41.

[FFmpeg-devel] [PATCH v12 10/15] avcodec/vaapi_encode: extract a get_recon_format function to base layer

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Surface size and block size parameters are also moved to base layer.

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.c | 58 +++
 libavcodec/hw_base_encode.h | 12 +
 libavcodec/vaapi_encode.c   | 81 -
 libavcodec/vaapi_encode.h   | 10 
 libavcodec/vaapi_encode_av1.c   | 10 ++--
 libavcodec/vaapi_encode_h264.c  | 11 +++--
 libavcodec/vaapi_encode_h265.c  | 25 +-
 libavcodec/vaapi_encode_mjpeg.c |  5 +-
 libavcodec/vaapi_encode_vp9.c   |  6 +--
 9 files changed, 118 insertions(+), 100 deletions(-)

diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 25fcfdbb5e..31046bd73e 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -687,6 +687,64 @@ int ff_hw_base_init_gop_structure(AVCodecContext *avctx, 
uint32_t ref_l0, uint32
 return 0;
 }
 
+int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig, 
enum AVPixelFormat *fmt)
+{
+FFHWBaseEncodeContext *ctx = avctx->priv_data;
+AVHWFramesConstraints *constraints = NULL;
+enum AVPixelFormat recon_format;
+int err, i;
+
+constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
+  hwconfig);
+if (!constraints) {
+err = AVERROR(ENOMEM);
+goto fail;
+}
+
+// Probably we can use the input surface format as the surface format
+// of the reconstructed frames.  If not, we just pick the first (only?)
+// format in the valid list and hope that it all works.
+recon_format = AV_PIX_FMT_NONE;
+if (constraints->valid_sw_formats) {
+for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
+if (ctx->input_frames->sw_format ==
+constraints->valid_sw_formats[i]) {
+recon_format = ctx->input_frames->sw_format;
+break;
+}
+}
+if (recon_format == AV_PIX_FMT_NONE) {
+// No match.  Just use the first in the supported list and
+// hope for the best.
+recon_format = constraints->valid_sw_formats[0];
+}
+} else {
+// No idea what to use; copy input format.
+recon_format = ctx->input_frames->sw_format;
+}
+av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
+   "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
+
+if (ctx->surface_width  < constraints->min_width  ||
+ctx->surface_height < constraints->min_height ||
+ctx->surface_width  > constraints->max_width ||
+ctx->surface_height > constraints->max_height) {
+av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
+   "size %dx%d (constraints: width %d-%d height %d-%d).\n",
+   ctx->surface_width, ctx->surface_height,
+   constraints->min_width,  constraints->max_width,
+   constraints->min_height, constraints->max_height);
+err = AVERROR(EINVAL);
+goto fail;
+}
+
+*fmt = recon_format;
+err = 0;
+fail:
+av_hwframe_constraints_free(&constraints);
+return err;
+}
+
 int ff_hw_base_encode_init(AVCodecContext *avctx)
 {
 FFHWBaseEncodeContext *ctx = avctx->priv_data;
diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index f097d826a7..f5414e2c28 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -128,6 +128,16 @@ typedef struct FFHWBaseEncodeContext {
 // Desired B frame reference depth.
 int desired_b_depth;
 
+// The required size of surfaces.  This is probably the input
+// size (AVCodecContext.width|height) aligned up to whatever
+// block size is required by the codec.
+int surface_width;
+int surface_height;
+
+// The block size for slice calculations.
+int slice_block_width;
+int slice_block_height;
+
 // The hardware device context.
 AVBufferRef*device_ref;
 AVHWDeviceContext *device;
@@ -212,6 +222,8 @@ int ff_hw_base_encode_receive_packet(AVCodecContext *avctx, 
AVPacket *pkt);
 int ff_hw_base_init_gop_structure(AVCodecContext *avctx, uint32_t ref_l0, 
uint32_t ref_l1,
   int flags, int prediction_pre_only);
 
+int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig, 
enum AVPixelFormat *fmt);
+
 int ff_hw_base_encode_init(AVCodecContext *avctx);
 
 int ff_hw_base_encode_close(AVCodecContext *avctx);
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 4193f3838f..d96f146b28 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -1777,6 +1777,7 @@ static av_cold int 
vaapi_encode_init_tile_slice_structure(AVCodecContext *avctx,
 
 static av_cold int vaapi_encode_init_slice_structure(AVCodecContext *avctx)
 {
+FFHWBaseEncodeContext *base_ctx = a

[FFmpeg-devel] [PATCH v12 11/15] avcodec/vaapi_encode: extract a free funtion to base layer

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.c | 11 +++
 libavcodec/hw_base_encode.h |  2 ++
 libavcodec/vaapi_encode.c   |  6 +-
 3 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 31046bd73e..92f69bb78c 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -745,6 +745,17 @@ fail:
 return err;
 }
 
+int ff_hw_base_encode_free(FFHWBaseEncodePicture *pic)
+{
+av_frame_free(&pic->input_image);
+av_frame_free(&pic->recon_image);
+
+av_buffer_unref(&pic->opaque_ref);
+av_freep(&pic->priv_data);
+
+return 0;
+}
+
 int ff_hw_base_encode_init(AVCodecContext *avctx)
 {
 FFHWBaseEncodeContext *ctx = avctx->priv_data;
diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index f5414e2c28..15ef3d7ac6 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -224,6 +224,8 @@ int ff_hw_base_init_gop_structure(AVCodecContext *avctx, 
uint32_t ref_l0, uint32
 
 int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig, 
enum AVPixelFormat *fmt);
 
+int ff_hw_base_encode_free(FFHWBaseEncodePicture *pic);
+
 int ff_hw_base_encode_init(AVCodecContext *avctx);
 
 int ff_hw_base_encode_close(AVCodecContext *avctx);
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index d96f146b28..b35a23e852 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -878,17 +878,13 @@ static int vaapi_encode_free(AVCodecContext *avctx,
 av_freep(&pic->slices[i].codec_slice_params);
 }
 
-av_frame_free(&base_pic->input_image);
-av_frame_free(&base_pic->recon_image);
-
-av_buffer_unref(&base_pic->opaque_ref);
+ff_hw_base_encode_free(base_pic);
 
 av_freep(&pic->param_buffers);
 av_freep(&pic->slices);
 // Output buffer should already be destroyed.
 av_assert0(pic->output_buffer == VA_INVALID_ID);
 
-av_freep(&base_pic->priv_data);
 av_freep(&pic->codec_picture_params);
 av_freep(&pic->roi);
 
-- 
2.41.0.windows.1

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

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


[FFmpeg-devel] [PATCH v12 12/15] avutil/hwcontext_d3d12va: add Flags for resource creation

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Flags field is added to support diffferent resource creation.

Signed-off-by: Tong Wu 
---
 doc/APIchanges| 3 +++
 libavutil/hwcontext_d3d12va.c | 2 +-
 libavutil/hwcontext_d3d12va.h | 8 
 libavutil/version.h   | 2 +-
 4 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 60f056b863..0c3d0221e8 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07
 
 API changes, most recent first:
 
+2024-01-xx - xx - lavu 59.21.100 - hwcontext_d3d12va.h
+ Add AVD3D12VAFramesContext.flags
+
 2024-05-23 - xx - lavu 59.20.100 - channel_layout.h
   Add av_channel_layout_ambisonic_order().
 
diff --git a/libavutil/hwcontext_d3d12va.c b/libavutil/hwcontext_d3d12va.c
index cfc016315d..6507cf69c1 100644
--- a/libavutil/hwcontext_d3d12va.c
+++ b/libavutil/hwcontext_d3d12va.c
@@ -247,7 +247,7 @@ static AVBufferRef *d3d12va_pool_alloc(void *opaque, size_t 
size)
 .Format   = hwctx->format,
 .SampleDesc   = {.Count = 1, .Quality = 0 },
 .Layout   = D3D12_TEXTURE_LAYOUT_UNKNOWN,
-.Flags= D3D12_RESOURCE_FLAG_NONE,
+.Flags= hwctx->flags,
 };
 
 frame = av_mallocz(sizeof(AVD3D12VAFrame));
diff --git a/libavutil/hwcontext_d3d12va.h b/libavutil/hwcontext_d3d12va.h
index ff06e6f2ef..212a6a6146 100644
--- a/libavutil/hwcontext_d3d12va.h
+++ b/libavutil/hwcontext_d3d12va.h
@@ -129,6 +129,14 @@ typedef struct AVD3D12VAFramesContext {
  * If unset, will be automatically set.
  */
 DXGI_FORMAT format;
+
+/**
+ * Options for working with resources.
+ * If unset, this will be D3D12_RESOURCE_FLAG_NONE.
+ *
+ * @see 
https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_resource_flags
+ */
+D3D12_RESOURCE_FLAGS flags;
 } AVD3D12VAFramesContext;
 
 #endif /* AVUTIL_HWCONTEXT_D3D12VA_H */
diff --git a/libavutil/version.h b/libavutil/version.h
index 9c7146c228..9d08d56884 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  59
-#define LIBAVUTIL_VERSION_MINOR  20
+#define LIBAVUTIL_VERSION_MINOR  21
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
2.41.0.windows.1

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

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


[FFmpeg-devel] [PATCH v12 13/15] avcodec: add D3D12VA hardware HEVC encoder

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

This implementation is based on D3D12 Video Encoding Spec:
https://microsoft.github.io/DirectX-Specs/d3d/D3D12VideoEncoding.html

Sample command line for transcoding:
ffmpeg.exe -hwaccel d3d12va -hwaccel_output_format d3d12 -i input.mp4
-c:v hevc_d3d12va output.mp4

Signed-off-by: Tong Wu 
---
 configure|6 +
 libavcodec/Makefile  |5 +-
 libavcodec/allcodecs.c   |1 +
 libavcodec/d3d12va_encode.c  | 1558 ++
 libavcodec/d3d12va_encode.h  |  334 +++
 libavcodec/d3d12va_encode_hevc.c | 1007 +++
 6 files changed, 2910 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/d3d12va_encode.c
 create mode 100644 libavcodec/d3d12va_encode.h
 create mode 100644 libavcodec/d3d12va_encode_hevc.c

diff --git a/configure b/configure
index b16722d83d..127d68e60c 100755
--- a/configure
+++ b/configure
@@ -2551,6 +2551,7 @@ CONFIG_EXTRA="
 cbs_mpeg2
 cbs_vp8
 cbs_vp9
+d3d12va_encode
 deflate_wrapper
 dirac_parse
 dnn
@@ -3287,6 +3288,7 @@ wmv3_vaapi_hwaccel_select="vc1_vaapi_hwaccel"
 wmv3_vdpau_hwaccel_select="vc1_vdpau_hwaccel"
 
 # hardware-accelerated codecs
+d3d12va_encode_deps="d3d12va ID3D12VideoEncoder d3d12_encoder_feature"
 mediafoundation_deps="mftransform_h MFCreateAlignedMemoryBuffer"
 omx_deps="libdl pthreads"
 omx_rpi_select="omx"
@@ -3354,6 +3356,7 @@ h264_v4l2m2m_encoder_deps="v4l2_m2m h264_v4l2_m2m"
 hevc_amf_encoder_deps="amf"
 hevc_cuvid_decoder_deps="cuvid"
 hevc_cuvid_decoder_select="hevc_mp4toannexb_bsf"
+hevc_d3d12va_encoder_select="cbs_h265 d3d12va_encode"
 hevc_mediacodec_decoder_deps="mediacodec"
 hevc_mediacodec_decoder_select="hevc_mp4toannexb_bsf hevc_parser"
 hevc_mediacodec_encoder_deps="mediacodec"
@@ -6725,6 +6728,9 @@ check_type "windows.h d3d11.h" "ID3D11VideoDecoder"
 check_type "windows.h d3d11.h" "ID3D11VideoContext"
 check_type "windows.h d3d12.h" "ID3D12Device"
 check_type "windows.h d3d12video.h" "ID3D12VideoDecoder"
+check_type "windows.h d3d12video.h" "ID3D12VideoEncoder"
+test_code cc "windows.h d3d12video.h" "D3D12_FEATURE_VIDEO feature = 
D3D12_FEATURE_VIDEO_ENCODER_CODEC" && \
+test_code cc "windows.h d3d12video.h" 
"D3D12_FEATURE_DATA_VIDEO_ENCODER_RESOURCE_REQUIREMENTS req" && enable 
d3d12_encoder_feature
 check_type "windows.h" "DPI_AWARENESS_CONTEXT" -D_WIN32_WINNT=0x0A00
 check_type "d3d9.h dxva2api.h" DXVA2_ConfigPictureDecode -D_WIN32_WINNT=0x0602
 check_func_headers mfapi.h MFCreateAlignedMemoryBuffer -lmfplat
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 998f6b7e12..6c4500ce6d 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -86,6 +86,7 @@ OBJS-$(CONFIG_CBS_JPEG)+= cbs_jpeg.o
 OBJS-$(CONFIG_CBS_MPEG2)   += cbs_mpeg2.o
 OBJS-$(CONFIG_CBS_VP8) += cbs_vp8.o vp8data.o
 OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
+OBJS-$(CONFIG_D3D12VA_ENCODE)  += d3d12va_encode.o hw_base_encode.o
 OBJS-$(CONFIG_DEFLATE_WRAPPER) += zlib_wrapper.o
 OBJS-$(CONFIG_DOVI_RPUDEC) += dovi_rpu.o dovi_rpudec.o
 OBJS-$(CONFIG_DOVI_RPUENC) += dovi_rpu.o dovi_rpuenc.o
@@ -436,6 +437,8 @@ OBJS-$(CONFIG_HEVC_DECODER)+= hevcdec.o 
hevc_mvs.o \
   h274.o aom_film_grain.o
 OBJS-$(CONFIG_HEVC_AMF_ENCODER)+= amfenc_hevc.o
 OBJS-$(CONFIG_HEVC_CUVID_DECODER)  += cuviddec.o
+OBJS-$(CONFIG_HEVC_D3D12VA_ENCODER)+= d3d12va_encode_hevc.o 
h265_profile_level.o \
+  h2645data.o
 OBJS-$(CONFIG_HEVC_MEDIACODEC_DECODER) += mediacodecdec.o
 OBJS-$(CONFIG_HEVC_MEDIACODEC_ENCODER) += mediacodecenc.o
 OBJS-$(CONFIG_HEVC_MF_ENCODER) += mfenc.o mf_utils.o
@@ -1265,7 +1268,7 @@ SKIPHEADERS+= %_tablegen.h
  \
 
 SKIPHEADERS-$(CONFIG_AMF)  += amfenc.h
 SKIPHEADERS-$(CONFIG_D3D11VA)  += d3d11va.h dxva2_internal.h
-SKIPHEADERS-$(CONFIG_D3D12VA)  += d3d12va_decode.h
+SKIPHEADERS-$(CONFIG_D3D12VA)  += d3d12va_decode.h d3d12va_encode.h
 SKIPHEADERS-$(CONFIG_DXVA2)+= dxva2.h dxva2_internal.h
 SKIPHEADERS-$(CONFIG_JNI)  += ffjni.h
 SKIPHEADERS-$(CONFIG_LCMS2)+= fflcms2.h
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index b102a8069e..463ffbbd08 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -848,6 +848,7 @@ extern const FFCodec ff_h264_vaapi_encoder;
 extern const FFCodec ff_h264_videotoolbox_encoder;
 extern const FFCodec ff_hevc_amf_encoder;
 extern const FFCodec ff_hevc_cuvid_decoder;
+extern const FFCodec ff_hevc_d3d12va_encoder;
 extern const FFCodec ff_hevc_mediacodec_decoder;
 extern const FFCodec ff_hevc_mediacodec_encoder;
 extern const FFCodec ff_hevc_mf_encoder;
diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
new file mode 100644
index 00..0fbf8eb07c
--- /

[FFmpeg-devel] [PATCH v12 07/15] avcodec/vaapi_encode: extract the init and close function to base layer

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Related parameters such as device context, frame context are also moved
to base layer.

Signed-off-by: Tong Wu 
---
 libavcodec/hw_base_encode.c | 49 ++
 libavcodec/hw_base_encode.h | 17 +++
 libavcodec/vaapi_encode.c   | 90 +++--
 libavcodec/vaapi_encode.h   | 10 
 libavcodec/vaapi_encode_av1.c   |  2 +-
 libavcodec/vaapi_encode_h264.c  |  2 +-
 libavcodec/vaapi_encode_h265.c  |  2 +-
 libavcodec/vaapi_encode_mjpeg.c |  6 ++-
 8 files changed, 102 insertions(+), 76 deletions(-)

diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 16afaa37be..c4789380b6 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -592,3 +592,52 @@ end:
 
 return 0;
 }
+
+int ff_hw_base_encode_init(AVCodecContext *avctx)
+{
+FFHWBaseEncodeContext *ctx = avctx->priv_data;
+
+ctx->frame = av_frame_alloc();
+if (!ctx->frame)
+return AVERROR(ENOMEM);
+
+if (!avctx->hw_frames_ctx) {
+av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
+   "required to associate the encoding device.\n");
+return AVERROR(EINVAL);
+}
+
+ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
+if (!ctx->input_frames_ref)
+return AVERROR(ENOMEM);
+
+ctx->input_frames = (AVHWFramesContext *)ctx->input_frames_ref->data;
+
+ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
+if (!ctx->device_ref)
+return AVERROR(ENOMEM);
+
+ctx->device = (AVHWDeviceContext *)ctx->device_ref->data;
+
+ctx->tail_pkt = av_packet_alloc();
+if (!ctx->tail_pkt)
+return AVERROR(ENOMEM);
+
+return 0;
+}
+
+int ff_hw_base_encode_close(AVCodecContext *avctx)
+{
+FFHWBaseEncodeContext *ctx = avctx->priv_data;
+
+av_fifo_freep2(&ctx->encode_fifo);
+
+av_frame_free(&ctx->frame);
+av_packet_free(&ctx->tail_pkt);
+
+av_buffer_unref(&ctx->device_ref);
+av_buffer_unref(&ctx->input_frames_ref);
+av_buffer_unref(&ctx->recon_frames_ref);
+
+return 0;
+}
diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h
index 81e6c87036..a062009860 100644
--- a/libavcodec/hw_base_encode.h
+++ b/libavcodec/hw_base_encode.h
@@ -19,6 +19,7 @@
 #ifndef AVCODEC_HW_BASE_ENCODE_H
 #define AVCODEC_HW_BASE_ENCODE_H
 
+#include "libavutil/hwcontext.h"
 #include "libavutil/fifo.h"
 
 #define MAX_DPB_SIZE 16
@@ -119,6 +120,18 @@ typedef struct FFHWBaseEncodeContext {
 // Hardware-specific hooks.
 const struct FFHWEncodePictureOperation *op;
 
+// The hardware device context.
+AVBufferRef*device_ref;
+AVHWDeviceContext *device;
+
+// The hardware frame context containing the input frames.
+AVBufferRef*input_frames_ref;
+AVHWFramesContext *input_frames;
+
+// The hardware frame context containing the reconstructed frames.
+AVBufferRef*recon_frames_ref;
+AVHWFramesContext *recon_frames;
+
 // Current encoding window, in display (input) order.
 FFHWBaseEncodePicture *pic_start, *pic_end;
 // The next picture to use as the previous reference picture in
@@ -185,6 +198,10 @@ typedef struct FFHWBaseEncodeContext {
 
 int ff_hw_base_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
 
+int ff_hw_base_encode_init(AVCodecContext *avctx);
+
+int ff_hw_base_encode_close(AVCodecContext *avctx);
+
 #define HW_BASE_ENCODE_COMMON_OPTIONS \
 { "async_depth", "Maximum processing parallelism. " \
   "Increase this to improve single channel performance.", \
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 1055fca0b1..1b3bab2c14 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -314,7 +314,7 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
 
 av_log(avctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
 
-err = av_hwframe_get_buffer(ctx->recon_frames_ref, base_pic->recon_image, 
0);
+err = av_hwframe_get_buffer(base_ctx->recon_frames_ref, 
base_pic->recon_image, 0);
 if (err < 0) {
 err = AVERROR(ENOMEM);
 goto fail;
@@ -996,9 +996,10 @@ static const VAEntrypoint 
vaapi_encode_entrypoints_low_power[] = {
 
 static av_cold int vaapi_encode_profile_entrypoint(AVCodecContext *avctx)
 {
-VAAPIEncodeContext  *ctx = avctx->priv_data;
-VAProfile*va_profiles= NULL;
-VAEntrypoint *va_entrypoints = NULL;
+FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
+VAAPIEncodeContext *ctx = avctx->priv_data;
+VAProfile   *va_profiles= NULL;
+VAEntrypoint*va_entrypoints = NULL;
 VAStatus vas;
 const VAEntrypoint *usable_entrypoints;
 const VAAPIEncodeProfile *profile;
@@ -1021,10 +1022,10 @@ static av_cold int 
vaapi_encode_profile_entrypoint(AVCodecContext *avctx)
 usable_entrypoints = vaapi_encode_entrypoints_normal;
 }
 
-desc = av_pix_fmt_desc_get(ctx->input_frame

[FFmpeg-devel] [PATCH v12 14/15] Changelog: add D3D12VA HEVC encoder changelog

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

Signed-off-by: Tong Wu 
---
 Changelog | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Changelog b/Changelog
index 12770e4296..c5d57b3813 100644
--- a/Changelog
+++ b/Changelog
@@ -11,7 +11,7 @@ version :
 - vf_scale2ref deprecated
 - qsv_params option added for QSV encoders
 - VVC decoder compatible with DVB test content
-
+- D3D12VA HEVC encoder
 
 version 7.0:
 - DXV DXT1 encoder
-- 
2.41.0.windows.1

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

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


[FFmpeg-devel] [PATCH v12 15/15] avcodec/hw_base_encode: add avctx pointer for FFHWBaseEncodeContext

2024-05-28 Thread tong1 . wu-at-intel . com
From: Tong Wu 

An avctx pointer is added to FFHWBaseEncodeContext. This is to make
FFHWBaseEncodeContext a standalone component for ff_hw_base_* functions.
This patch also removes some unnecessary AVCodecContext arguments.

Signed-off-by: Tong Wu 
---
 libavcodec/d3d12va_encode.c |  6 +++---
 libavcodec/hw_base_encode.c | 31 +--
 libavcodec/hw_base_encode.h |  8 +---
 libavcodec/vaapi_encode.c   |  6 +++---
 4 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
index 0fbf8eb07c..6d3a53c6ca 100644
--- a/libavcodec/d3d12va_encode.c
+++ b/libavcodec/d3d12va_encode.c
@@ -1351,7 +1351,7 @@ static int 
d3d12va_encode_create_recon_frames(AVCodecContext *avctx)
 enum AVPixelFormat recon_format;
 int err;
 
-err = ff_hw_base_get_recon_format(avctx, NULL, &recon_format);
+err = ff_hw_base_get_recon_format(base_ctx, NULL, &recon_format);
 if (err < 0)
 return err;
 
@@ -1398,7 +1398,7 @@ int ff_d3d12va_encode_init(AVCodecContext *avctx)
 int err;
 HRESULT hr;
 
-err = ff_hw_base_encode_init(avctx);
+err = ff_hw_base_encode_init(avctx, base_ctx);
 if (err < 0)
 goto fail;
 
@@ -1552,7 +1552,7 @@ int ff_d3d12va_encode_close(AVCodecContext *avctx)
 D3D12_OBJECT_RELEASE(ctx->video_device3);
 D3D12_OBJECT_RELEASE(ctx->device3);
 
-ff_hw_base_encode_close(avctx);
+ff_hw_base_encode_close(base_ctx);
 
 return 0;
 }
diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 92f69bb78c..88efdf672c 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -94,14 +94,13 @@ static void 
hw_base_encode_remove_refs(FFHWBaseEncodePicture *pic, int level)
 pic->ref_removed[level] = 1;
 }
 
-static void hw_base_encode_set_b_pictures(AVCodecContext *avctx,
+static void hw_base_encode_set_b_pictures(FFHWBaseEncodeContext *ctx,
   FFHWBaseEncodePicture *start,
   FFHWBaseEncodePicture *end,
   FFHWBaseEncodePicture *prev,
   int current_depth,
   FFHWBaseEncodePicture **last)
 {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
 FFHWBaseEncodePicture *pic, *next, *ref;
 int i, len;
 
@@ -148,20 +147,19 @@ static void hw_base_encode_set_b_pictures(AVCodecContext 
*avctx,
 hw_base_encode_add_ref(pic, ref, 0, 1, 0);
 
 if (i > 1)
-hw_base_encode_set_b_pictures(avctx, start, pic, pic,
+hw_base_encode_set_b_pictures(ctx, start, pic, pic,
   current_depth + 1, &next);
 else
 next = pic;
 
-hw_base_encode_set_b_pictures(avctx, pic, end, next,
+hw_base_encode_set_b_pictures(ctx, pic, end, next,
   current_depth + 1, last);
 }
 }
 
-static void hw_base_encode_add_next_prev(AVCodecContext *avctx,
+static void hw_base_encode_add_next_prev(FFHWBaseEncodeContext *ctx,
  FFHWBaseEncodePicture *pic)
 {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
 int i;
 
 if (!pic)
@@ -333,12 +331,12 @@ static int hw_base_encode_pick_next(AVCodecContext *avctx,
 }
 
 if (b_counter > 0) {
-hw_base_encode_set_b_pictures(avctx, start, pic, pic, 1,
+hw_base_encode_set_b_pictures(ctx, start, pic, pic, 1,
   &prev);
 } else {
 prev = pic;
 }
-hw_base_encode_add_next_prev(avctx, prev);
+hw_base_encode_add_next_prev(ctx, prev);
 
 return 0;
 }
@@ -687,9 +685,9 @@ int ff_hw_base_init_gop_structure(AVCodecContext *avctx, 
uint32_t ref_l0, uint32
 return 0;
 }
 
-int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig, 
enum AVPixelFormat *fmt)
+int ff_hw_base_get_recon_format(FFHWBaseEncodeContext *ctx, const void 
*hwconfig,
+enum AVPixelFormat *fmt)
 {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
 AVHWFramesConstraints *constraints = NULL;
 enum AVPixelFormat recon_format;
 int err, i;
@@ -722,14 +720,14 @@ int ff_hw_base_get_recon_format(AVCodecContext *avctx, 
const void *hwconfig, enu
 // No idea what to use; copy input format.
 recon_format = ctx->input_frames->sw_format;
 }
-av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
+av_log(ctx->avctx, AV_LOG_DEBUG, "Using %s as format of "
"reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
 
 if (ctx->surface_width  < constraints->min_width  ||
 ctx->surface_height < constraints->min_height ||
 ctx->surface_width  > constraints->max_width ||
 ctx->surface_height > constraints->max_height) {
-av_log(avctx, AV_LOG_ERROR, "Hardware does not support

Re: [FFmpeg-devel] [PATCH v11 07/14] avcodec/vaapi_encode: extract the init and close function to base layer

2024-05-28 Thread Wu, Tong1
>From: Lynne 
>Sent: Monday, May 27, 2024 10:04 AM
>To: Wu, Tong1 ; FFmpeg development discussions and
>patches 
>Subject: Re: [FFmpeg-devel] [PATCH v11 07/14] avcodec/vaapi_encode: extract
>the init and close function to base layer
>
>On 27/05/2024 02:35, Wu, Tong1 wrote:
>>> From: ffmpeg-devel  On Behalf Of
>Lynne
>>> via ffmpeg-devel
>>> Sent: Saturday, May 25, 2024 10:07 PM
>>> To: ffmpeg-devel@ffmpeg.org
>>> Cc: Lynne 
>>> Subject: Re: [FFmpeg-devel] [PATCH v11 07/14] avcodec/vaapi_encode:
>extract
>>> the init and close function to base layer
>>>
>>> On 25/05/2024 12:30, tong1.wu-at-intel@ffmpeg.org wrote:
 From: Tong Wu 

 Related parameters such as device context, frame context are also moved
 to base layer.

 Signed-off-by: Tong Wu 
 ---
libavcodec/hw_base_encode.c | 49 ++
libavcodec/hw_base_encode.h | 17 +++
libavcodec/vaapi_encode.c   | 90 +++--
libavcodec/vaapi_encode.h   | 10 
libavcodec/vaapi_encode_av1.c   |  2 +-
libavcodec/vaapi_encode_h264.c  |  2 +-
libavcodec/vaapi_encode_h265.c  |  2 +-
libavcodec/vaapi_encode_mjpeg.c |  6 ++-
8 files changed, 102 insertions(+), 76 deletions(-)

 diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
 index 16afaa37be..c4789380b6 100644
 --- a/libavcodec/hw_base_encode.c
 +++ b/libavcodec/hw_base_encode.c
 @@ -592,3 +592,52 @@ end:

return 0;
}
 +
 +int ff_hw_base_encode_init(AVCodecContext *avctx)
 +{
 +FFHWBaseEncodeContext *ctx = avctx->priv_data;
>>>
>>> This is the issue I was talking about, this requires that
>>> FFHWBaseEncodeContext is always the main context.
>>>
>>> Could you change it so everything takes FFHWBaseEncodeContext as an
>>> argument, rather than AVCodecContext (apart from where the function
>>> absolutely must read some data from it)?
>>
>> I'm trying to understand it more.
>>
>> In ff_hw_base_encode_init we also have the following code:
>>
>>  if (!avctx->hw_frames_ctx) {
>>  av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
>> "required to associate the encoding device.\n");
>>  return AVERROR(EINVAL);
>>  }
>>
>> In this scenario we still need avctx right? So maybe this is the "must read 
>> data
>from it" situation and we keep AVCodecContext as the main context?
>
>Yup. My point is that FFHWBaseEncodeContext doesn't become the primary
>context that must be used, but a separate component other users can use
>standalone.
>
>> Plus I have this av_log concern which is there's indeed some function that
>the only use of avctx is its av_log's context. Do you think I should instead 
>use
>FFHWBaseEncodeContext as the main context and pass it to av_log for this
>situation while keeping AVCodecContext as the main context for other
>functions which actually read from AVCodecContext. That might lead to
>different av_log context in the same file.
>
>Just save a pointer to avctx on init in FFHWBaseEncodeContext. That's
>what most of our code does.
>
>> Do you think the callbacks in FFHWEncodePictureOperation should be
>changed too? Or only all the functions in hw_base_encode.c should be
>concerned. Thank you.
>
>No, the callbacks should stay as-is. Users need to retrieve their own
>context via avctx. That's also a reason why you should keep a pointer to
>avctx on init.
>

I've updated v12 and made this change with a separate(15th) patch. Please see 
if that is what you wanted. Thank you.

-Tong

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

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


Re: [FFmpeg-devel] [PATCH v12 15/15] avcodec/hw_base_encode: add avctx pointer for FFHWBaseEncodeContext

2024-05-28 Thread Lynne via ffmpeg-devel

On 28/05/2024 17:48, tong1.wu-at-intel@ffmpeg.org wrote:

From: Tong Wu 

An avctx pointer is added to FFHWBaseEncodeContext. This is to make
FFHWBaseEncodeContext a standalone component for ff_hw_base_* functions.
This patch also removes some unnecessary AVCodecContext arguments.

Signed-off-by: Tong Wu 
---
  libavcodec/d3d12va_encode.c |  6 +++---
  libavcodec/hw_base_encode.c | 31 +--
  libavcodec/hw_base_encode.h |  8 +---
  libavcodec/vaapi_encode.c   |  6 +++---
  4 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
index 0fbf8eb07c..6d3a53c6ca 100644
--- a/libavcodec/d3d12va_encode.c
+++ b/libavcodec/d3d12va_encode.c
@@ -1351,7 +1351,7 @@ static int 
d3d12va_encode_create_recon_frames(AVCodecContext *avctx)
  enum AVPixelFormat recon_format;
  int err;
  
-err = ff_hw_base_get_recon_format(avctx, NULL, &recon_format);

+err = ff_hw_base_get_recon_format(base_ctx, NULL, &recon_format);
  if (err < 0)
  return err;
  
@@ -1398,7 +1398,7 @@ int ff_d3d12va_encode_init(AVCodecContext *avctx)

  int err;
  HRESULT hr;
  
-err = ff_hw_base_encode_init(avctx);

+err = ff_hw_base_encode_init(avctx, base_ctx);
  if (err < 0)
  goto fail;
  
@@ -1552,7 +1552,7 @@ int ff_d3d12va_encode_close(AVCodecContext *avctx)

  D3D12_OBJECT_RELEASE(ctx->video_device3);
  D3D12_OBJECT_RELEASE(ctx->device3);
  
-ff_hw_base_encode_close(avctx);

+ff_hw_base_encode_close(base_ctx);
  
  return 0;

  }
diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 92f69bb78c..88efdf672c 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -94,14 +94,13 @@ static void 
hw_base_encode_remove_refs(FFHWBaseEncodePicture *pic, int level)
  pic->ref_removed[level] = 1;
  }
  
-static void hw_base_encode_set_b_pictures(AVCodecContext *avctx,

+static void hw_base_encode_set_b_pictures(FFHWBaseEncodeContext *ctx,
FFHWBaseEncodePicture *start,
FFHWBaseEncodePicture *end,
FFHWBaseEncodePicture *prev,
int current_depth,
FFHWBaseEncodePicture **last)
  {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
  FFHWBaseEncodePicture *pic, *next, *ref;
  int i, len;
  
@@ -148,20 +147,19 @@ static void hw_base_encode_set_b_pictures(AVCodecContext *avctx,

  hw_base_encode_add_ref(pic, ref, 0, 1, 0);
  
  if (i > 1)

-hw_base_encode_set_b_pictures(avctx, start, pic, pic,
+hw_base_encode_set_b_pictures(ctx, start, pic, pic,
current_depth + 1, &next);
  else
  next = pic;
  
-hw_base_encode_set_b_pictures(avctx, pic, end, next,

+hw_base_encode_set_b_pictures(ctx, pic, end, next,
current_depth + 1, last);
  }
  }
  
-static void hw_base_encode_add_next_prev(AVCodecContext *avctx,

+static void hw_base_encode_add_next_prev(FFHWBaseEncodeContext *ctx,
   FFHWBaseEncodePicture *pic)
  {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
  int i;
  
  if (!pic)

@@ -333,12 +331,12 @@ static int hw_base_encode_pick_next(AVCodecContext *avctx,
  }
  
  if (b_counter > 0) {

-hw_base_encode_set_b_pictures(avctx, start, pic, pic, 1,
+hw_base_encode_set_b_pictures(ctx, start, pic, pic, 1,
&prev);
  } else {
  prev = pic;
  }
-hw_base_encode_add_next_prev(avctx, prev);
+hw_base_encode_add_next_prev(ctx, prev);
  
  return 0;

  }
@@ -687,9 +685,9 @@ int ff_hw_base_init_gop_structure(AVCodecContext *avctx, 
uint32_t ref_l0, uint32
  return 0;
  }
  
-int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig, enum AVPixelFormat *fmt)

+int ff_hw_base_get_recon_format(FFHWBaseEncodeContext *ctx, const void 
*hwconfig,
+enum AVPixelFormat *fmt)
  {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
  AVHWFramesConstraints *constraints = NULL;
  enum AVPixelFormat recon_format;
  int err, i;
@@ -722,14 +720,14 @@ int ff_hw_base_get_recon_format(AVCodecContext *avctx, 
const void *hwconfig, enu
  // No idea what to use; copy input format.
  recon_format = ctx->input_frames->sw_format;
  }
-av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
+av_log(ctx->avctx, AV_LOG_DEBUG, "Using %s as format of "
 "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
  
  if (ctx->surface_width  < constraints->min_width  ||

  ctx->surface_height < constraints->min_height ||
  ctx->surface_wid

[FFmpeg-devel] [PATCH v4 0/1] Add support for H266/VVC encoding

2024-05-28 Thread Christian Bartnik
This patch is based on the latest patchset from Thomas Siedel
(thomas...@spin-digital.com).

The libvvenc patch has been changed with following changes:
  avcodec: add external encoder libvvenc for H266/VVC
- complete makeover (renaming,cleanup)
- remove struct VVenCOptions, move option members to VVenCContext
- using int-based version number for macros instead of major/minor/patch
- fix initialization of framerate
- use av_dict_iterate() insteat of av_dict_get()
- changing 2pass mode handling (not using vvenc-params anymore,
  using -pass and -stats|-passlogfile for statistics filename)
- remove options: bitdepth8, subjopt
- add option: qpa to switch between ssim/psnr based optimization (default: 
enabled)
- fix payload allocation check
- fix supported colorspace from AV_PIX_FMT_YUV420P10LE to AV_PIX_FMT_YUV420P10
- change level option by using either -level(int) or -level:v (char*) as 
libx264 does
- adding capability AV_CODEC_CAP_DR1
- adding caps_internal FF_CODEC_CAP_INIT_CLEANUP and remove calls: 
vvenc_encoder_close
- fftools/ffmpeg_mux_init.c set default stats filename as for libx264
- add vvenc documentation in doc/encoders.texi

The libvvdec patch has been removed from this patchset.

Christian Bartnik (1):
  avcodec: add external enc libvvenc for H266/VVC

 configure |   4 +
 doc/encoders.texi |  64 +
 fftools/ffmpeg_mux_init.c |   2 +-
 libavcodec/Makefile   |   1 +
 libavcodec/allcodecs.c|   1 +
 libavcodec/libvvenc.c | 524 ++
 6 files changed, 595 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/libvvenc.c

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

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


[FFmpeg-devel] [PATCH v4 1/1] avcodec: add external enc libvvenc for H266/VVC

2024-05-28 Thread Christian Bartnik
From: Thomas Siedel 

Add external encoder VVenC for H266/VVC encoding.
Register new encoder libvvenc.
Add libvvenc to wrap the vvenc interface.
libvvenc implements encoder option: preset,qp,qpa,period,
passlogfile,stats,vvenc-params,level,tier.
Enable encoder by adding --enable-libvvenc in configure step.

Co-authored-by: Christian Bartnik chris1031...@gmail.com
Signed-off-by: Thomas Siedel 
---
 configure |   4 +
 doc/encoders.texi |  64 +
 fftools/ffmpeg_mux_init.c |   2 +-
 libavcodec/Makefile   |   1 +
 libavcodec/allcodecs.c|   1 +
 libavcodec/libvvenc.c | 507 ++
 6 files changed, 578 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/libvvenc.c

diff --git a/configure b/configure
index 96b181fd21..082cbca7bb 100755
--- a/configure
+++ b/configure
@@ -293,6 +293,7 @@ External library support:
   --enable-libvorbis   enable Vorbis en/decoding via libvorbis,
native implementation exists [no]
   --enable-libvpx  enable VP8 and VP9 de/encoding via libvpx [no]
+  --enable-libvvencenable H.266/VVC encoding via vvenc [no]
   --enable-libwebp enable WebP encoding via libwebp [no]
   --enable-libx264 enable H.264 encoding via x264 [no]
   --enable-libx265 enable HEVC encoding via x265 [no]
@@ -1966,6 +1967,7 @@ EXTERNAL_LIBRARY_LIST="
 libvmaf
 libvorbis
 libvpx
+libvvenc
 libwebp
 libxevd
 libxeve
@@ -3560,6 +3562,7 @@ libvpx_vp8_decoder_deps="libvpx"
 libvpx_vp8_encoder_deps="libvpx"
 libvpx_vp9_decoder_deps="libvpx"
 libvpx_vp9_encoder_deps="libvpx"
+libvvenc_encoder_deps="libvvenc"
 libwebp_encoder_deps="libwebp"
 libwebp_anim_encoder_deps="libwebp"
 libx262_encoder_deps="libx262"
@@ -7030,6 +7033,7 @@ enabled libvpx&& {
 fi
 }
 
+enabled libvvenc  && require_pkg_config libvvenc "libvvenc >= 1.6.1" 
"vvenc/vvenc.h" vvenc_get_version
 enabled libwebp   && {
 enabled libwebp_encoder  && require_pkg_config libwebp "libwebp >= 
0.2.0" webp/encode.h WebPGetEncoderVersion
 enabled libwebp_anim_encoder && check_pkg_config libwebp_anim_encoder 
"libwebpmux >= 0.4.0" webp/mux.h WebPAnimEncoderOptionsInit; }
diff --git a/doc/encoders.texi b/doc/encoders.texi
index c82f316f94..496852faeb 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -2378,6 +2378,70 @@ Indicates frame duration
 For more information about libvpx see:
 @url{http://www.webmproject.org/}
 
+@section libvvenc
+
+VVenC H.266/VVC encoder wrapper.
+
+This encoder requires the presence of the libvvenc headers and library
+during configuration. You need to explicitly configure the build with
+@option{--enable-libvvenc}.
+
+The VVenC project website is at
+@url{https://github.com/fraunhoferhhi/vvenc}.
+
+@subsection Supported Pixel Formats
+
+VVenC supports only 10-bit color spaces as input. But the internal (encoded)
+bit depth can be set to 8-bit or 10-bit at runtime.
+
+@subsection Options
+
+@table @option
+@item b
+Sets target video bitrate.
+
+@item g
+Set the GOP size. Currently support for g=1 (Intra only) or default.
+
+@item preset
+Set the VVenC preset.
+
+@item levelidc
+Set level idc.
+
+@item tier
+Set vvc tier.
+
+@item qp
+Set constant quantization parameter.
+
+@item subopt @var{boolean}
+Set subjective (perceptually motivated) optimization. Default is 1 (on).
+
+@item bitdepth8 @var{boolean}
+Set 8bit coding mode instead of using 10bit. Default is 0 (off).
+
+@item period
+set (intra) refresh period in seconds.
+
+@item vvenc-params
+Set vvenc options using a list of @var{key}=@var{value} couples separated
+by ":". See @command{vvencapp --fullhelp} or @command{vvencFFapp --fullhelp} 
for a list of options.
+
+For example, the options might be provided as:
+
+@example
+intraperiod=64:decodingrefreshtype=idr:poc0idr=1:internalbitdepth=8
+@end example
+
+For example the encoding options might be provided with @option{-vvenc-params}:
+
+@example
+ffmpeg -i input -c:v libvvenc -b 1M -vvenc-params 
intraperiod=64:decodingrefreshtype=idr:poc0idr=1:internalbitdepth=8 output.mp4
+@end example
+
+@end table
+
 @section libwebp
 
 libwebp WebP Image encoder wrapper
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 8797265145..ef2922854a 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -739,7 +739,7 @@ static int new_stream_video(Muxer *mux, const 
OptionsContext *o,
  ost->logfile_prefix ? ost->logfile_prefix :
DEFAULT_PASS_LOGFILENAME_PREFIX,
  ost_idx);
-if (!strcmp(ost->enc_ctx->codec->name, "libx264")) {
+if (!strcmp(ost->enc_ctx->codec->name, "libx264") || 
!strcmp(ost->enc_ctx->codec->name, "libvvenc")) {
 av_dict_set(&ost->encoder_opts, "stats", logfilename, 
AV_DICT_DONT_OVERWRITE);
 } else {
 if (video_enc->flag

[FFmpeg-devel] lavfi: add perlin noise generator

2024-05-28 Thread Stefano Sabatini
On date Monday 2024-05-27 23:37:33 +0200, Stefano Sabatini wrote:
> Hi,
> 
> still missing documentation and might be optimized (and maybe extended
> to support gray16 - this should be simple), comments are welcome.

Updated with documentation.
>From 607459e7a184ab2d111b65f5017fb7f76e3bd58d Mon Sep 17 00:00:00 2001
From: Stefano Sabatini 
Date: Mon, 27 May 2024 11:19:08 +0200
Subject: [PATCH] lavfi: add perlin noise generator

---
 Changelog |   1 +
 doc/filters.texi  | 100 +
 libavfilter/Makefile  |   1 +
 libavfilter/allfilters.c  |   1 +
 libavfilter/perlin.c  | 224 ++
 libavfilter/perlin.h  | 100 +
 libavfilter/vsrc_perlin.c | 169 
 7 files changed, 596 insertions(+)
 create mode 100644 libavfilter/perlin.c
 create mode 100644 libavfilter/perlin.h
 create mode 100644 libavfilter/vsrc_perlin.c

diff --git a/Changelog b/Changelog
index 12770e4296..1670038d00 100644
--- a/Changelog
+++ b/Changelog
@@ -11,6 +11,7 @@ version :
 - vf_scale2ref deprecated
 - qsv_params option added for QSV encoders
 - VVC decoder compatible with DVB test content
+- perlin source
 
 
 version 7.0:
diff --git a/doc/filters.texi b/doc/filters.texi
index f5bf475d13..baed8c5530 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -17290,6 +17290,9 @@ The command accepts the same syntax of the corresponding option.
 If the specified expression is not valid, it is kept at its current
 value.
 
+@anchor{lutrgb}
+@anchor{lutyuv}
+@anchor{lut}
 @section lut, lutrgb, lutyuv
 
 Compute a look-up table for binding each pixel component input value
@@ -29280,6 +29283,103 @@ ffplay -f lavfi life=s=300x200:mold=10:r=60:ratio=0.1:death_color=#C83232:life_c
 @end example
 @end itemize
 
+@section perlin
+Generate Perlin noise.
+
+Perlin noise is a kind of noise with local continuity in space. This
+can be used to generate patterns with continuity in space and time,
+e.g. to simulate smoke, fluids, or terrain.
+
+In case more than one octave is specified through the @option{octaves}
+option, Perlin noise is generated as a sum of components, each one
+with doubled frequency. In this case the @option{persistence} option
+specify the ratio of the amplitude with respect to the previous
+component. More octave components enable to specify more high
+frequency details in the generated noise (e.g. small size variations
+due to bolders in a generated terrain).
+
+@subsection Options
+@table @option
+
+@item size, s
+Specify the size (width and height) of the buffered video frames. For the
+syntax of this option, check the
+@ref{video size syntax,,"Video size" section in the ffmpeg-utils manual,ffmpeg-utils}.
+
+@item rate, r
+Specify the frame rate expected for the video stream, expressed as a
+number of frames per second.
+
+@item octaves
+Specify the total number of components making up the noise, each one
+with doubled frequency.
+
+@item persistence
+Set the ratio used to compute the amplitude of the next octave
+component with respect to the previous component amplitude.
+
+@item xscale
+@item yscale
+Define a scale factor used to multiple the x, y coordinates. This can
+be useful to define an effect with a pattern stretched along the x or
+y axis.
+
+@item tscale
+Define a scale factor used to multiple the time coordinate. This can
+be useful to change the time variation speed.
+
+@item random_mode
+Set random mode used to compute initial pattern.
+
+Supported values are:
+@table @option
+@item random
+Compute and use random seed.
+
+@item ken
+Use the predefined initial pattern defined by Ken Perlin in the
+original article, can be useful to compare the output with other
+sources.
+
+@item seed
+Use the value specified by @option{random_seed} option.
+@end table
+
+@item random_seed, seed
+Use this value to compute the initial pattern, it is only considered
+when @option{random_mode} is set to @var{random_seed}.
+@end table
+
+@subsection Examples
+@itemize
+@item
+Generate single component:
+@example
+perlin
+@end example
+
+@item
+Use Perlin noise with 7 components, each one with a halved contribute
+to total amplitude:
+@example
+perlin=octaves=7:persistence=0.5
+@end example
+
+@item 
+Chain Perlin noise with the @ref{lutyuv} to generate a black&white
+effect:
+@example
+perlin:octaves=7:tscale=0.3,lutyuv=y='if(lt(val\,128)\,255\,0)'
+@end example
+
+@item
+Stretch noise along the y axis, and convert gray level to red-only
+signal:
+@example
+perlin=octaves=7:tscale=0.4:yscale=0.3,lutrgb=r=val:b=0:g=0
+@end example
+@end itemize
+
 @section qrencodesrc
 
 Generate a QR code using the libqrencode library (see
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 5992fd161f..63088e9286 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -603,6 +603,7 @@ OBJS-$(CONFIG_NULLSRC_FILTER)+= vsrc_testsrc.o
 OBJS-$(CONFIG_OPENCLSRC_FILTER)  += vf_program_opencl.o opencl.o

Re: [FFmpeg-devel] [RFC 00/13] flvdec/flvenc: add support for enhanced rtmp codecs and multitrack/multichannel

2024-05-28 Thread Timo Rothenpieler

On 28.05.2024 17:14, Andrew Sayers wrote:

On Tue, May 21, 2024 at 11:02:09AM +0200, Timo Rothenpieler wrote:

This is based on the preliminary spec for enhanced rtmp v2:
https://veovera.org/docs/enhanced/enhanced-rtmp-v2

The spec is not final, and can still undergo breaking changes, hence this set 
is purely for comments and review, and not ready to be merged until the final 
v2 spec is published.

There are no samples out in the wild yet, so testing interoperability with 
other software has not happened yet either.
Specially the two other multitrack modes, where multiple tracks are in the same 
packet, have not been tested at all, since no software can write such files.

The set can also be found on GitHub, where ignoring whitespaces makes specially 
the last patch a lot more readable:
https://github.com/BtbN/FFmpeg/tree/enhanced-flv



I ran this against a little review bot I'm working on.
Please do s/\* / \*/g on the following:


The usage of that is already inconsistent throughout the flv codebase, 
so running that would make the patch touch a lot of unrelated things.


Seems more sane to just do a general style-cleanup after the fact.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [PATCH v4 1/4] doc: Explain what "context" means

2024-05-28 Thread Stefano Sabatini
On date Sunday 2024-05-26 13:06:52 +0100, Andrew Sayers wrote:
> It feels like we've got through most of the mid-level "how FFmpeg works" 
> stuff,
> and now we're left with language choices (e.g "options" vs. "introspection")
> and philosophical discussions (e.g. the relationship between contexts and 
> OOP).
> It's probably best to philosophise first, then come back to language.
> 
> This message has been sent as a reply to one specific message, but is actually
> springboarding off messages from two sub-threads.  Hopefully that will keep
> the big questions contained in one place.
> 
> On Sat, May 25, 2024 at 11:49:48AM +0200, Stefano Sabatini wrote:
> > What perplexes me is that "context" is not part of the standard OOP
> > jargon, so this is probably adding more to the confusion.
> 

> This actually speaks to a more fundamental issue about how we learn.
> To be clear, everything I'm about to describe applies every human that ever
> lived, but starting with this message makes it easier to explain
> (for reasons that will hopefully become obvious).
> 

> When you ask why "context" is not part of OOP jargon, one could equally ask
> why "object" isn't part of FFmpeg jargon.  The document hints at some 
> arguments:
> their lifetime stages are different, their rules are enforced at the
> language vs. community level, OOP encourages homogenous interfaces while 
> FFmpeg
> embraces unique interfaces that precisely suit each use case, and so on.
> But the honest answer is much simpler - humans are lazy, and we want the 
> things
> we learn today to resemble the things we learnt yesterday.
> 
> Put another way - if we had infinite time every day, we could probably write 
> an
> object-oriented interface to FFmpeg.  But our time is sadly finite so we stick
> with the thing that's proven to work.  Similarly, if our readers had infinite
> free time every day, they could probably learn a completely new approach to
> programming.  But their time is finite, so they stick to what they know.
> 
> That means people reading this document aren't just passively soaking up
> information, they're looking for shortcuts that fit their assumptions.
> And as anyone that's ever seen a political discussion can tell you,
> humans are *really good* at finding shortcuts that fit their assumptions.
> For example, when an OOP developer sees the words "alloc" and "init",
> they will assume these map precisely to OOP allocators and initializers.  One
> reason for the long section about context lifetimes is because it needs to
> meet them where they are, then walk them step-by-step to a better place.

I think we start with different assumptions: you assume that most of
the readers are familiar with OOP jargon, and that they will leverage
the OOP jargon to understand the FFmpeg API. I think this is
misleading: historically what defined FFmpeg is its minimalistic
approach, in this case we don't want the user, familiar with C, to be
familiar with OOP in order to use and understand the FFmpeg API, as
this is simply not necessary as FFmpeg is plain C language.

In fact, if this might help with some users, this will probably
confuse all the others. Even more, if we adopt the FFmpeg jargon to
OOP (using "context") we are adding more confusion, as now the OOP
reader will have to adopt the FFmpeg jargon applied to OOP.

My advice is to move all the content to OOP to a dedicated section, or
to make the references to OOP jargon as less as possible, to not get
in the way with the non-OOP reader.

> Aside: if FFmpeg had a blog, I could turn this discussion into a great post
> called something like "reflections on object- vs. context-oriented 
> development".
> But the project's voice is more objective than that, so this document is 
> limited
> to discussing the subset of issues that relate specifically to the FFmpeg API.

One option would be the wiki:
http://trac.ffmpeg.org/

but probably a personal blog entry would be better, given that this
would not be reference material but more as an article (since the API
and its own philosophy is a moving target).

> 
> On Sat, May 25, 2024 at 01:00:14PM +0200, Stefano Sabatini wrote:
> > > +Some functions fit awkwardly within FFmpeg's context idiom.  For example,
> > > +av_ambient_viewing_environment_create_side_data() creates an
> > > +AVAmbientViewingEnvironment context, then adds it to the side-data of an
> > > +AVFrame context.
> > 
> > To go back to this unfitting example, can you state what would be
> > fitting in this case?
> 
> "Awkwardly" probably isn't the right word to use, but that's a language choice
> we can come back to.
> 
> The problem with FFmpeg's interface isn't that any one part is illogical,
> it's that different parts of the interface follow incompatible logic.
> 
> It's hard to give specific examples, because any given learner's journey looks
> like a random walk through the API, and you can always say "well nobody else
> would have that problem".  But if everyone has a different probl

[FFmpeg-devel] [PATCH v1 1/2][GSoC 2024] libavcode/x86/vvc: change label to vvc_sad_16 to reflect block sizes

2024-05-28 Thread Stone Chen
According to the VVC specification (section 8.5.1), the maximum width/height of 
a subblock passed for DMVR SAD is 16. This along with previous constraint 
requiring width * height >= 128 means that  8x16, 16x8, and 16x16 are the only 
allowed sizes. This re-labels vvc_sad_16_128 to vvc_sad_16 to reflect this and 
adds a comment about the block size constraints. There's no functionality 
change.
---
 libavcodec/x86/vvc/vvc_sad.asm | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libavcodec/x86/vvc/vvc_sad.asm b/libavcodec/x86/vvc/vvc_sad.asm
index b468d89ac2..982951a370 100644
--- a/libavcodec/x86/vvc/vvc_sad.asm
+++ b/libavcodec/x86/vvc/vvc_sad.asm
@@ -29,6 +29,7 @@ SECTION_RODATA
 pw_1: times 2 dw 1
 
 ; DMVR SAD is only calculated on even rows to reduce complexity
+; Additionally the only valid sizes are 8x16, 16x8, and 16x16
 SECTION .text
 
 %macro MIN_MAX_SAD 3
@@ -77,7 +78,7 @@ cglobal vvc_sad, 6, 9, 5, src1, src2, dx, dy, block_w, 
block_h, off1, off2, row_
 vpbroadcastd   m4, [pw_1]
 
 cmp  block_wd, 16
-jgevvc_sad_16_128
+je vvc_sad_16
 
 vvc_sad_8:
 .loop_height:
@@ -100,7 +101,7 @@ cglobal vvc_sad, 6, 9, 5, src1, src2, dx, dy, block_w, 
block_h, off1, off2, row_
 movd  eax, xm0
 RET
 
-vvc_sad_16_128:
+vvc_sad_16:
 sar  block_wd, 4
 .loop_height:
 mov off1q, src1q
-- 
2.45.0

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

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


[FFmpeg-devel] [PATCH v1 2/2][GSoC 2024] tests/checkasm/vvc_mc: for SAD, only test valid subblock sizes

2024-05-28 Thread Stone Chen
According to the VVC specification (section 8.5.1), the maximum width/height of 
a subblock passed for DMVR SAD is 16. This along with previous constraint 
requiring width * height >= 128 means that  8x16, 16x8, and 16x16 are the only 
allowed sizes.

This changes check_vvc_sad() to only test and benchmark those sizes.
---
 tests/checkasm/vvc_mc.c | 12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/tests/checkasm/vvc_mc.c b/tests/checkasm/vvc_mc.c
index 1e889e2cff..09cac82edb 100644
--- a/tests/checkasm/vvc_mc.c
+++ b/tests/checkasm/vvc_mc.c
@@ -337,11 +337,12 @@ static void check_vvc_sad(void)
 memset(src1, 0, MAX_CTU_SIZE * MAX_CTU_SIZE * 4 * sizeof(uint16_t));
 
 randomize_pixels(src0, src1, MAX_CTU_SIZE * MAX_CTU_SIZE * 4);
- for (int h = 8; h <= MAX_CTU_SIZE; h *= 2) {
-for (int w = 8; w <= MAX_CTU_SIZE; w *= 2) {
+for (int h = 8; h <= 16; h *= 2) {
+for (int w = 8; w <= 16; w *= 2) {
 for(int offy = 0; offy <= 4; offy++) {
 for(int offx = 0; offx <= 4; offx++) {
-if(check_func(c.inter.sad, "sad_%dx%d", w, h)) {
+if(w * h >= 128) {
+if(check_func(c.inter.sad, "sad_%dx%d", w, h)) {
 int result0;
 int result1;
 
@@ -350,13 +351,14 @@ static void check_vvc_sad(void)
 
 if (result1 != result0)
 fail();
-if(w == h && offx == 0 && offy == 0)
+if(offx == 0 && offy == 0)
 bench_new(src0 + PIXEL_STRIDE * 2 + 2, src1 + 
PIXEL_STRIDE * 2 + 2, offx, offy, w, h);
+}
 }
 }
 }
 }
- }
+}
 
 report("sad");
 }
-- 
2.45.0

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

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


[FFmpeg-devel] [PATCH] checkasm: add linear least square tests

2024-05-28 Thread Rémi Denis-Courmont
---
 tests/checkasm/Makefile   |   1 +
 tests/checkasm/checkasm.c |   1 +
 tests/checkasm/checkasm.h |   1 +
 tests/checkasm/lls.c  | 110 ++
 tests/fate/checkasm.mak   |   1 +
 5 files changed, 114 insertions(+)
 create mode 100644 tests/checkasm/lls.c

diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile
index 1dc770e9da..6eb94d10d5 100644
--- a/tests/checkasm/Makefile
+++ b/tests/checkasm/Makefile
@@ -71,6 +71,7 @@ CHECKASMOBJS-$(CONFIG_SWSCALE)  += $(SWSCALEOBJS)
 AVUTILOBJS  += av_tx.o
 AVUTILOBJS  += fixed_dsp.o
 AVUTILOBJS  += float_dsp.o
+AVUTILOBJS  += lls.o
 
 CHECKASMOBJS-$(CONFIG_AVUTIL)  += $(AVUTILOBJS)
 
diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index 97c9b9fdd4..d7aa2a9c09 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -254,6 +254,7 @@ static const struct {
 #if CONFIG_AVUTIL
 { "fixed_dsp", checkasm_check_fixed_dsp },
 { "float_dsp", checkasm_check_float_dsp },
+{ "lls",   checkasm_check_lls },
 { "av_tx", checkasm_check_av_tx },
 #endif
 { NULL }
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index a42645c4ae..211d7f52e6 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -105,6 +105,7 @@ void checkasm_check_huffyuvdsp(void);
 void checkasm_check_idctdsp(void);
 void checkasm_check_jpeg2000dsp(void);
 void checkasm_check_llauddsp(void);
+void checkasm_check_lls(void);
 void checkasm_check_llviddsp(void);
 void checkasm_check_llviddspenc(void);
 void checkasm_check_lpc(void);
diff --git a/tests/checkasm/lls.c b/tests/checkasm/lls.c
new file mode 100644
index 00..96c97cd7a3
--- /dev/null
+++ b/tests/checkasm/lls.c
@@ -0,0 +1,110 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include 
+//#include 
+
+#include "libavutil/lls.h"
+//#include "libavutil/internal.h"
+//#include "libavutil/mem.h"
+//#include "libavutil/mem_internal.h"
+
+#include "checkasm.h"
+
+#define LEN 256
+
+#define randomize_buffer(buf) \
+do {  \
+int i;\
+double bmg[2], stddev = 10.0, mean = 0.0; \
+  \
+for (i = 0; i < LEN; i += 2) {\
+av_bmg_get(&checkasm_lfg, bmg);   \
+buf[i] = bmg[0] * stddev + mean;  \
+buf[i + 1] = bmg[1] * stddev + mean;  \
+} \
+} while(0);
+
+static void test_update(LLSModel *lls, const double *var)
+{
+double refcovar[MAX_VARS][MAX_VARS];
+declare_func(void, LLSModel *, const double *);
+
+call_ref(lls, var);
+
+for (size_t i = 0; i < MAX_VARS; i++)
+for (size_t j = 0; j < MAX_VARS; j++)
+refcovar[i][j] = lls->covariance[i][j];
+
+memset(lls->covariance, 0, sizeof (lls->covariance));
+call_new(lls, var);
+
+for (size_t i = 0; i < lls->indep_count; i++)
+for (size_t j = i; j < lls->indep_count; j++)
+if (!double_near_abs_eps(refcovar[i][j], lls->covariance[i][j],
+ 2 * DBL_EPSILON)) {
+fprintf(stderr, "%zu, %zu: %- .12f - %- .12f = % .12g\n", i, j,
+refcovar[i][j], lls->covariance[i][j],
+refcovar[i][j] - lls->covariance[i][j]);
+fail();
+}
+
+bench_new(lls, var);
+}
+
+#define EPS 0.2
+static void test_evaluate(LLSModel *lls, const double *param, int order)
+{
+double refprod, newprod;
+declare_func_float(double, LLSModel *, const double *, int);
+
+refprod = call_ref(lls, param, order);
+newprod = call_new(lls, param, order);
+
+if (!double_near_abs_eps(refprod, newprod, EPS)) {
+fprintf(stderr, "%- .12f - %- .12f = % .12g\n",
+refprod, newprod, refprod - newprod);
+fail();
+}
+
+if (order == lls->indep_count)
+bench_new(lls, param, order);
+}
+
+void checkasm_check_lls(void)
+{
+static const unsigned char counts[] = { 8, 12, MAX_VARS, };
+
+for (size_t i = 0; i < FF_ARRAY_ELEMS(counts)

Re: [FFmpeg-devel] [PATCH] checkasm: add linear least square tests

2024-05-28 Thread James Almer

On 5/28/2024 4:57 PM, Rémi Denis-Courmont wrote:

---
  tests/checkasm/Makefile   |   1 +
  tests/checkasm/checkasm.c |   1 +
  tests/checkasm/checkasm.h |   1 +
  tests/checkasm/lls.c  | 110 ++
  tests/fate/checkasm.mak   |   1 +
  5 files changed, 114 insertions(+)
  create mode 100644 tests/checkasm/lls.c

diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile
index 1dc770e9da..6eb94d10d5 100644
--- a/tests/checkasm/Makefile
+++ b/tests/checkasm/Makefile
@@ -71,6 +71,7 @@ CHECKASMOBJS-$(CONFIG_SWSCALE)  += $(SWSCALEOBJS)
  AVUTILOBJS  += av_tx.o
  AVUTILOBJS  += fixed_dsp.o
  AVUTILOBJS  += float_dsp.o
+AVUTILOBJS  += lls.o
  
  CHECKASMOBJS-$(CONFIG_AVUTIL)  += $(AVUTILOBJS)
  
diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c

index 97c9b9fdd4..d7aa2a9c09 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -254,6 +254,7 @@ static const struct {
  #if CONFIG_AVUTIL
  { "fixed_dsp", checkasm_check_fixed_dsp },
  { "float_dsp", checkasm_check_float_dsp },
+{ "lls",   checkasm_check_lls },
  { "av_tx", checkasm_check_av_tx },
  #endif
  { NULL }
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index a42645c4ae..211d7f52e6 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -105,6 +105,7 @@ void checkasm_check_huffyuvdsp(void);
  void checkasm_check_idctdsp(void);
  void checkasm_check_jpeg2000dsp(void);
  void checkasm_check_llauddsp(void);
+void checkasm_check_lls(void);
  void checkasm_check_llviddsp(void);
  void checkasm_check_llviddspenc(void);
  void checkasm_check_lpc(void);
diff --git a/tests/checkasm/lls.c b/tests/checkasm/lls.c
new file mode 100644
index 00..96c97cd7a3
--- /dev/null
+++ b/tests/checkasm/lls.c
@@ -0,0 +1,110 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include 
+//#include 
+
+#include "libavutil/lls.h"
+//#include "libavutil/internal.h"
+//#include "libavutil/mem.h"
+//#include "libavutil/mem_internal.h"
+
+#include "checkasm.h"
+
+#define LEN 256
+
+#define randomize_buffer(buf) \
+do {  \
+int i;\
+double bmg[2], stddev = 10.0, mean = 0.0; \
+  \
+for (i = 0; i < LEN; i += 2) {\


MAX_VARS, not LEN. The latter isn't even needed.


+av_bmg_get(&checkasm_lfg, bmg);   \
+buf[i] = bmg[0] * stddev + mean;  \
+buf[i + 1] = bmg[1] * stddev + mean;  \
+} \
+} while(0);
+
+static void test_update(LLSModel *lls, const double *var)
+{
+double refcovar[MAX_VARS][MAX_VARS];
+declare_func(void, LLSModel *, const double *);
+
+call_ref(lls, var);
+
+for (size_t i = 0; i < MAX_VARS; i++)
+for (size_t j = 0; j < MAX_VARS; j++)
+refcovar[i][j] = lls->covariance[i][j];
+
+memset(lls->covariance, 0, sizeof (lls->covariance));
+call_new(lls, var);
+
+for (size_t i = 0; i < lls->indep_count; i++)
+for (size_t j = i; j < lls->indep_count; j++)
+if (!double_near_abs_eps(refcovar[i][j], lls->covariance[i][j],
+ 2 * DBL_EPSILON)) {
+fprintf(stderr, "%zu, %zu: %- .12f - %- .12f = % .12g\n", i, j,
+refcovar[i][j], lls->covariance[i][j],
+refcovar[i][j] - lls->covariance[i][j]);
+fail();
+}
+
+bench_new(lls, var);
+}
+
+#define EPS 0.2
+static void test_evaluate(LLSModel *lls, const double *param, int order)
+{
+double refprod, newprod;
+declare_func_float(double, LLSModel *, const double *, int);
+
+refprod = call_ref(lls, param, order);
+newprod = call_new(lls, param, order);
+
+if (!double_near_abs_eps(refprod, newprod, EPS)) {
+fprintf(stderr, "%- .12f - %- .12f = % .12g\n",
+refprod, newprod, refprod - newprod);
+fail();
+}
+
+if (order == lls->indep_count)
+bench_new(lls, param, order);
+}
+
+void checkasm_check_

[FFmpeg-devel] [PATCH] avcodec/mediacodec: Add support of dynamic bitrate

2024-05-28 Thread Dmitrii Okunev
MediaCodec supports parameter "video-bitrate" to change the bitrate
on fly. This commit adds capability to use it.

It adds option -bitrate_ctrl_socket to the encoder which makes
the encoder to create an UNIX socket and listen for messages
to change the bitrate.

An example of ffmpeg execution:

ffmpeg -f flv -i rtmp://0.0.0.0:1935/live/myStream -c:v hevc_mediacodec 
-bitrate_ctrl_socket /run/bitrate.sock -b:v 8M -f flv 
rtmp://127.0.0.1:1935/live/reEncoded

An example of changing the bitrate to 1000 Kbps:

printf '%016X' 100 | xxd -r -p | socat -u STDIN UNIX:/run/bitrate.sock

Signed-off-by: Dmitrii Okunev 
---
 libavcodec/Makefile |   2 +-
 libavcodec/mediacodec_wrapper.c |  50 
 libavcodec/mediacodec_wrapper.h |  12 ++
 libavcodec/mediacodecenc.c  | 128 ++---
 libavcodec/mediacodecenc.h  |  97 
 libavcodec/mediacodecenc_ctrl.c | 194 
 libavcodec/mediacodecenc_ctrl.h |  50 
 7 files changed, 469 insertions(+), 64 deletions(-)
 create mode 100644 libavcodec/mediacodecenc.h
 create mode 100644 libavcodec/mediacodecenc_ctrl.c
 create mode 100644 libavcodec/mediacodecenc_ctrl.h

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 2443d2c6fd..393877c8c6 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -258,7 +258,7 @@ OBJS-$(CONFIG_AURA2_DECODER)   += aura.o
 OBJS-$(CONFIG_AV1_DECODER) += av1dec.o av1_parse.o
 OBJS-$(CONFIG_AV1_CUVID_DECODER)   += cuviddec.o
 OBJS-$(CONFIG_AV1_MEDIACODEC_DECODER)  += mediacodecdec.o
-OBJS-$(CONFIG_AV1_MEDIACODEC_ENCODER)  += mediacodecenc.o
+OBJS-$(CONFIG_AV1_MEDIACODEC_ENCODER)  += mediacodecenc.o mediacodecenc_ctrl.o
 OBJS-$(CONFIG_AV1_NVENC_ENCODER)   += nvenc_av1.o nvenc.o
 OBJS-$(CONFIG_AV1_QSV_ENCODER) += qsvenc_av1.o
 OBJS-$(CONFIG_AV1_VAAPI_ENCODER)   += vaapi_encode_av1.o av1_levels.o
diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
index 96c88a..fe0e291cad 100644
--- a/libavcodec/mediacodec_wrapper.c
+++ b/libavcodec/mediacodec_wrapper.c
@@ -174,6 +174,7 @@ struct JNIAMediaCodecFields {
 jmethodID get_name_id;
 
 jmethodID configure_id;
+jmethodID set_parameters_id;
 jmethodID start_id;
 jmethodID flush_id;
 jmethodID stop_id;
@@ -247,6 +248,9 @@ static const struct FFJniField jni_amediacodec_mapping[] = {
 
 { "android/media/MediaCodec", "setInputSurface", 
"(Landroid/view/Surface;)V", FF_JNI_METHOD, OFFSET(set_input_surface_id), 0 },
 { "android/media/MediaCodec", "signalEndOfInputStream", "()V", 
FF_JNI_METHOD, OFFSET(signal_end_of_input_stream_id), 0 },
+#if __ANDROID_API__ >= 26
+{ "android/media/MediaCodec", "setParameters", 
"(Landroid/media/MediaFormat;)V", FF_JNI_METHOD, OFFSET(set_parameters_id), 1 },
+#endif
 
 { "android/media/MediaCodec$BufferInfo", NULL, NULL, FF_JNI_CLASS, 
OFFSET(mediainfo_class), 1 },
 
@@ -1411,6 +1415,27 @@ fail:
 return ret;
 }
 
+#if __ANDROID_API__ >= 26
+static int mediacodec_jni_setParameters(FFAMediaCodec *ctx,
+const FFAMediaFormat *format_ctx)
+{
+int ret = 0;
+JNIEnv *env = NULL;
+FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx;
+const FFAMediaFormatJni *format = (FFAMediaFormatJni *)format_ctx;
+
+JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
+
+(*env)->CallVoidMethod(env, codec->object, 
codec->jfields.set_parameters_id, format->object);
+
+if (ff_jni_exception_check(env, 1, codec) < 0) {
+ret = AVERROR_EXTERNAL;
+}
+
+return ret;
+}
+#endif // __ANDROID_API__ >= 26
+
 static int mediacodec_jni_start(FFAMediaCodec* ctx)
 {
 int ret = 0;
@@ -1821,6 +1846,10 @@ static const FFAMediaCodec media_codec_jni = {
 .getConfigureFlagEncode = mediacodec_jni_getConfigureFlagEncode,
 .cleanOutputBuffers = mediacodec_jni_cleanOutputBuffers,
 .signalEndOfInputStream = mediacodec_jni_signalEndOfInputStream,
+
+#if __ANDROID_API__ >= 26
+.setParameters = mediacodec_jni_setParameters,
+#endif //__ANDROID_API__ >= 26
 };
 
 typedef struct FFAMediaFormatNdk {
@@ -2178,6 +2207,23 @@ static int mediacodec_ndk_configure(FFAMediaCodec* ctx,
 return 0;
 }
 
+#if __ANDROID_API__ >= 26
+static int mediacodec_ndk_setParameters(FFAMediaCodec *ctx,
+const FFAMediaFormat *format_ctx)
+{
+FFAMediaCodecNdk *codec = (FFAMediaCodecNdk *)ctx;
+FFAMediaFormatNdk *format = (FFAMediaFormatNdk *)format_ctx;
+
+int status = AMediaCodec_setParameters(codec->impl, format->impl);
+if (status != AMEDIA_OK) {
+av_log(codec, AV_LOG_ERROR, "codec setParameters failed, %d\n", 
status);
+return AVERROR_EXTERNAL;
+}
+
+return 0;
+}
+#endif //__ANDROID_API__ >= 26
+
 #define MEDIACODEC_NDK_WRAPPER(method)   \
 static int mediacodec_ndk_ ## method(FFAMediaCodec* ctx) \
 { 

[FFmpeg-devel] [PATCH] avcodec/mediacodec: Add support of dynamic bitrate

2024-05-28 Thread Dmitrii Okunev
MediaCodec supports parameter "video-bitrate" to change the bitrate
on fly. This commit adds capability to use it.

It adds option -bitrate_ctrl_socket to the encoder which makes
the encoder to create an UNIX socket and listen for messages
to change the bitrate.

An example of ffmpeg execution:

ffmpeg -f flv -i rtmp://0.0.0.0:1935/live/myStream -c:v hevc_mediacodec 
-bitrate_ctrl_socket /run/bitrate.sock -b:v 8M -f flv 
rtmp://127.0.0.1:1935/live/reEncoded

An example of changing the bitrate to 1000 Kbps:

printf '%016X' 100 | xxd -r -p | socat -u STDIN UNIX:/run/bitrate.sock

Signed-off-by: Dmitrii Okunev 
---
 libavcodec/Makefile |   2 +-
 libavcodec/mediacodec_wrapper.c |  50 
 libavcodec/mediacodec_wrapper.h |  12 ++
 libavcodec/mediacodecenc.c  | 128 ++---
 libavcodec/mediacodecenc.h  |  97 
 libavcodec/mediacodecenc_ctrl.c | 194 
 libavcodec/mediacodecenc_ctrl.h |  50 
 7 files changed, 469 insertions(+), 64 deletions(-)
 create mode 100644 libavcodec/mediacodecenc.h
 create mode 100644 libavcodec/mediacodecenc_ctrl.c
 create mode 100644 libavcodec/mediacodecenc_ctrl.h

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 2443d2c6fd..393877c8c6 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -258,7 +258,7 @@ OBJS-$(CONFIG_AURA2_DECODER)   += aura.o
 OBJS-$(CONFIG_AV1_DECODER) += av1dec.o av1_parse.o
 OBJS-$(CONFIG_AV1_CUVID_DECODER)   += cuviddec.o
 OBJS-$(CONFIG_AV1_MEDIACODEC_DECODER)  += mediacodecdec.o
-OBJS-$(CONFIG_AV1_MEDIACODEC_ENCODER)  += mediacodecenc.o
+OBJS-$(CONFIG_AV1_MEDIACODEC_ENCODER)  += mediacodecenc.o mediacodecenc_ctrl.o
 OBJS-$(CONFIG_AV1_NVENC_ENCODER)   += nvenc_av1.o nvenc.o
 OBJS-$(CONFIG_AV1_QSV_ENCODER) += qsvenc_av1.o
 OBJS-$(CONFIG_AV1_VAAPI_ENCODER)   += vaapi_encode_av1.o av1_levels.o
diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
index 96c88a..fe0e291cad 100644
--- a/libavcodec/mediacodec_wrapper.c
+++ b/libavcodec/mediacodec_wrapper.c
@@ -174,6 +174,7 @@ struct JNIAMediaCodecFields {
 jmethodID get_name_id;
 
 jmethodID configure_id;
+jmethodID set_parameters_id;
 jmethodID start_id;
 jmethodID flush_id;
 jmethodID stop_id;
@@ -247,6 +248,9 @@ static const struct FFJniField jni_amediacodec_mapping[] = {
 
 { "android/media/MediaCodec", "setInputSurface", 
"(Landroid/view/Surface;)V", FF_JNI_METHOD, OFFSET(set_input_surface_id), 0 },
 { "android/media/MediaCodec", "signalEndOfInputStream", "()V", 
FF_JNI_METHOD, OFFSET(signal_end_of_input_stream_id), 0 },
+#if __ANDROID_API__ >= 26
+{ "android/media/MediaCodec", "setParameters", 
"(Landroid/media/MediaFormat;)V", FF_JNI_METHOD, OFFSET(set_parameters_id), 1 },
+#endif
 
 { "android/media/MediaCodec$BufferInfo", NULL, NULL, FF_JNI_CLASS, 
OFFSET(mediainfo_class), 1 },
 
@@ -1411,6 +1415,27 @@ fail:
 return ret;
 }
 
+#if __ANDROID_API__ >= 26
+static int mediacodec_jni_setParameters(FFAMediaCodec *ctx,
+const FFAMediaFormat *format_ctx)
+{
+int ret = 0;
+JNIEnv *env = NULL;
+FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx;
+const FFAMediaFormatJni *format = (FFAMediaFormatJni *)format_ctx;
+
+JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
+
+(*env)->CallVoidMethod(env, codec->object, 
codec->jfields.set_parameters_id, format->object);
+
+if (ff_jni_exception_check(env, 1, codec) < 0) {
+ret = AVERROR_EXTERNAL;
+}
+
+return ret;
+}
+#endif // __ANDROID_API__ >= 26
+
 static int mediacodec_jni_start(FFAMediaCodec* ctx)
 {
 int ret = 0;
@@ -1821,6 +1846,10 @@ static const FFAMediaCodec media_codec_jni = {
 .getConfigureFlagEncode = mediacodec_jni_getConfigureFlagEncode,
 .cleanOutputBuffers = mediacodec_jni_cleanOutputBuffers,
 .signalEndOfInputStream = mediacodec_jni_signalEndOfInputStream,
+
+#if __ANDROID_API__ >= 26
+.setParameters = mediacodec_jni_setParameters,
+#endif //__ANDROID_API__ >= 26
 };
 
 typedef struct FFAMediaFormatNdk {
@@ -2178,6 +2207,23 @@ static int mediacodec_ndk_configure(FFAMediaCodec* ctx,
 return 0;
 }
 
+#if __ANDROID_API__ >= 26
+static int mediacodec_ndk_setParameters(FFAMediaCodec *ctx,
+const FFAMediaFormat *format_ctx)
+{
+FFAMediaCodecNdk *codec = (FFAMediaCodecNdk *)ctx;
+FFAMediaFormatNdk *format = (FFAMediaFormatNdk *)format_ctx;
+
+int status = AMediaCodec_setParameters(codec->impl, format->impl);
+if (status != AMEDIA_OK) {
+av_log(codec, AV_LOG_ERROR, "codec setParameters failed, %d\n", 
status);
+return AVERROR_EXTERNAL;
+}
+
+return 0;
+}
+#endif //__ANDROID_API__ >= 26
+
 #define MEDIACODEC_NDK_WRAPPER(method)   \
 static int mediacodec_ndk_ ## method(FFAMediaCodec* ctx) \
 { 

Re: [FFmpeg-devel] [PATCH] avcodec/mediacodec: Add support of dynamic bitrate

2024-05-28 Thread Dmitrii Okunev
Sorry for re-posting the patch twice. To send to the mailing list I had 
to change the settings of my local postfix and made a mistake.


Anyway, just for convenience providing the list of changes since the 
previous revision of the patch:


< on fly. This commit add possibility to use it.
---
> on fly. This commit adds capability to use it.
32c15
<     ffmpeg -listen 1 -i rtmp://0.0.0.0:1935/live/myStream -c:v 
hevc_mediacodec -bitrate_ctrl_socket /run/bitrate.sock -b:v 8M -f rtsp 
rtsp://127.0.0.1:1935/live/reEncoded

---
>     ffmpeg -f flv -i rtmp://0.0.0.0:1935/live/myStream -c:v 
hevc_mediacodec -bitrate_ctrl_socket /run/bitrate.sock -b:v 8M -f flv 
rtmp://127.0.0.1:1935/live/reEncoded

34c17
< An example of changing the bitrate to 1000 BPS:
---
> An example of changing the bitrate to 1000 Kbps:
36c19
<     printf '%016X' 1000 | xxd -r -p | socat -u STDIN 
UNIX:/run/bitrate.sock

---
>     printf '%016X' 100 | xxd -r -p | socat -u STDIN 
UNIX:/run/bitrate.sock

66c49
< index 96c88a..fa288b2acc 100644
---
> index 96c88a..fe0e291cad 100644
93c76
< +                                    const FFAMediaFormat* format_ctx)
---
> +                                    const FFAMediaFormat *format_ctx)
131,132c114,115
< +static int mediacodec_ndk_setParameters(FFAMediaCodec* ctx,
< +                                    const FFAMediaFormat* format_ctx)
---
> +static int mediacodec_ndk_setParameters(FFAMediaCodec *ctx,
> +                                    const FFAMediaFormat *format_ctx)

Please let me know if I should've amended the patch differently. I'm a 
spoiled GitHub person, so I'm open to guidance/feedback :)


Best regards, Dmitrii.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [PATCH v12 15/15] avcodec/hw_base_encode: add avctx pointer for FFHWBaseEncodeContext

2024-05-28 Thread Wu, Tong1
>From: ffmpeg-devel  On Behalf Of Lynne
>via ffmpeg-devel
>Sent: Wednesday, May 29, 2024 1:08 AM
>To: ffmpeg-devel@ffmpeg.org
>Cc: Lynne 
>Subject: Re: [FFmpeg-devel] [PATCH v12 15/15] avcodec/hw_base_encode: add
>avctx pointer for FFHWBaseEncodeContext
>
>On 28/05/2024 17:48, tong1.wu-at-intel@ffmpeg.org wrote:
>> From: Tong Wu 
>>
>> An avctx pointer is added to FFHWBaseEncodeContext. This is to make
>> FFHWBaseEncodeContext a standalone component for ff_hw_base_*
>functions.
>> This patch also removes some unnecessary AVCodecContext arguments.
>>
>> Signed-off-by: Tong Wu 
>> ---
>>   libavcodec/d3d12va_encode.c |  6 +++---
>>   libavcodec/hw_base_encode.c | 31 +--
>>   libavcodec/hw_base_encode.h |  8 +---
>>   libavcodec/vaapi_encode.c   |  6 +++---
>>   4 files changed, 24 insertions(+), 27 deletions(-)
>>
>> diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
>> index 0fbf8eb07c..6d3a53c6ca 100644
>> --- a/libavcodec/d3d12va_encode.c
>> +++ b/libavcodec/d3d12va_encode.c
>> @@ -1351,7 +1351,7 @@ static int
>d3d12va_encode_create_recon_frames(AVCodecContext *avctx)
>>   enum AVPixelFormat recon_format;
>>   int err;
>>
>> -err = ff_hw_base_get_recon_format(avctx, NULL, &recon_format);
>> +err = ff_hw_base_get_recon_format(base_ctx, NULL, &recon_format);
>>   if (err < 0)
>>   return err;
>>
>> @@ -1398,7 +1398,7 @@ int ff_d3d12va_encode_init(AVCodecContext
>*avctx)
>>   int err;
>>   HRESULT hr;
>>
>> -err = ff_hw_base_encode_init(avctx);
>> +err = ff_hw_base_encode_init(avctx, base_ctx);
>>   if (err < 0)
>>   goto fail;
>>
>> @@ -1552,7 +1552,7 @@ int ff_d3d12va_encode_close(AVCodecContext
>*avctx)
>>   D3D12_OBJECT_RELEASE(ctx->video_device3);
>>   D3D12_OBJECT_RELEASE(ctx->device3);
>>
>> -ff_hw_base_encode_close(avctx);
>> +ff_hw_base_encode_close(base_ctx);
>>
>>   return 0;
>>   }
>> diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
>> index 92f69bb78c..88efdf672c 100644
>> --- a/libavcodec/hw_base_encode.c
>> +++ b/libavcodec/hw_base_encode.c
>> @@ -94,14 +94,13 @@ static void
>hw_base_encode_remove_refs(FFHWBaseEncodePicture *pic, int level)
>>   pic->ref_removed[level] = 1;
>>   }
>>
>> -static void hw_base_encode_set_b_pictures(AVCodecContext *avctx,
>> +static void hw_base_encode_set_b_pictures(FFHWBaseEncodeContext *ctx,
>> FFHWBaseEncodePicture *start,
>> FFHWBaseEncodePicture *end,
>> FFHWBaseEncodePicture *prev,
>> int current_depth,
>> FFHWBaseEncodePicture **last)
>>   {
>> -FFHWBaseEncodeContext *ctx = avctx->priv_data;
>>   FFHWBaseEncodePicture *pic, *next, *ref;
>>   int i, len;
>>
>> @@ -148,20 +147,19 @@ static void
>hw_base_encode_set_b_pictures(AVCodecContext *avctx,
>>   hw_base_encode_add_ref(pic, ref, 0, 1, 0);
>>
>>   if (i > 1)
>> -hw_base_encode_set_b_pictures(avctx, start, pic, pic,
>> +hw_base_encode_set_b_pictures(ctx, start, pic, pic,
>> current_depth + 1, &next);
>>   else
>>   next = pic;
>>
>> -hw_base_encode_set_b_pictures(avctx, pic, end, next,
>> +hw_base_encode_set_b_pictures(ctx, pic, end, next,
>> current_depth + 1, last);
>>   }
>>   }
>>
>> -static void hw_base_encode_add_next_prev(AVCodecContext *avctx,
>> +static void hw_base_encode_add_next_prev(FFHWBaseEncodeContext
>*ctx,
>>FFHWBaseEncodePicture *pic)
>>   {
>> -FFHWBaseEncodeContext *ctx = avctx->priv_data;
>>   int i;
>>
>>   if (!pic)
>> @@ -333,12 +331,12 @@ static int
>hw_base_encode_pick_next(AVCodecContext *avctx,
>>   }
>>
>>   if (b_counter > 0) {
>> -hw_base_encode_set_b_pictures(avctx, start, pic, pic, 1,
>> +hw_base_encode_set_b_pictures(ctx, start, pic, pic, 1,
>> &prev);
>>   } else {
>>   prev = pic;
>>   }
>> -hw_base_encode_add_next_prev(avctx, prev);
>> +hw_base_encode_add_next_prev(ctx, prev);
>>
>>   return 0;
>>   }
>> @@ -687,9 +685,9 @@ int ff_hw_base_init_gop_structure(AVCodecContext
>*avctx, uint32_t ref_l0, uint32
>>   return 0;
>>   }
>>
>> -int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void
>*hwconfig, enum AVPixelFormat *fmt)
>> +int ff_hw_base_get_recon_format(FFHWBaseEncodeContext *ctx, const
>void *hwconfig,
>> +enum AVPixelFormat *fmt)
>>   {
>> -FFHWBaseEncodeContext *ctx = avctx->priv_data;
>>   AVHWFramesConstraints *constraints = NULL;
>>   enum AVPixelFormat recon_format;
>>   int err, i;
>> @@ -722,14 +720

Re: [FFmpeg-devel] [PATCH v12 15/15] avcodec/hw_base_encode: add avctx pointer for FFHWBaseEncodeContext

2024-05-28 Thread Lynne via ffmpeg-devel

On 29/05/2024 00:53, Wu, Tong1 wrote:

From: ffmpeg-devel  On Behalf Of Lynne
via ffmpeg-devel
Sent: Wednesday, May 29, 2024 1:08 AM
To: ffmpeg-devel@ffmpeg.org
Cc: Lynne 
Subject: Re: [FFmpeg-devel] [PATCH v12 15/15] avcodec/hw_base_encode: add
avctx pointer for FFHWBaseEncodeContext

On 28/05/2024 17:48, tong1.wu-at-intel@ffmpeg.org wrote:

From: Tong Wu 

An avctx pointer is added to FFHWBaseEncodeContext. This is to make
FFHWBaseEncodeContext a standalone component for ff_hw_base_*

functions.

This patch also removes some unnecessary AVCodecContext arguments.

Signed-off-by: Tong Wu 
---
   libavcodec/d3d12va_encode.c |  6 +++---
   libavcodec/hw_base_encode.c | 31 +--
   libavcodec/hw_base_encode.h |  8 +---
   libavcodec/vaapi_encode.c   |  6 +++---
   4 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
index 0fbf8eb07c..6d3a53c6ca 100644
--- a/libavcodec/d3d12va_encode.c
+++ b/libavcodec/d3d12va_encode.c
@@ -1351,7 +1351,7 @@ static int

d3d12va_encode_create_recon_frames(AVCodecContext *avctx)

   enum AVPixelFormat recon_format;
   int err;

-err = ff_hw_base_get_recon_format(avctx, NULL, &recon_format);
+err = ff_hw_base_get_recon_format(base_ctx, NULL, &recon_format);
   if (err < 0)
   return err;

@@ -1398,7 +1398,7 @@ int ff_d3d12va_encode_init(AVCodecContext

*avctx)

   int err;
   HRESULT hr;

-err = ff_hw_base_encode_init(avctx);
+err = ff_hw_base_encode_init(avctx, base_ctx);
   if (err < 0)
   goto fail;

@@ -1552,7 +1552,7 @@ int ff_d3d12va_encode_close(AVCodecContext

*avctx)

   D3D12_OBJECT_RELEASE(ctx->video_device3);
   D3D12_OBJECT_RELEASE(ctx->device3);

-ff_hw_base_encode_close(avctx);
+ff_hw_base_encode_close(base_ctx);

   return 0;
   }
diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c
index 92f69bb78c..88efdf672c 100644
--- a/libavcodec/hw_base_encode.c
+++ b/libavcodec/hw_base_encode.c
@@ -94,14 +94,13 @@ static void

hw_base_encode_remove_refs(FFHWBaseEncodePicture *pic, int level)

   pic->ref_removed[level] = 1;
   }

-static void hw_base_encode_set_b_pictures(AVCodecContext *avctx,
+static void hw_base_encode_set_b_pictures(FFHWBaseEncodeContext *ctx,
 FFHWBaseEncodePicture *start,
 FFHWBaseEncodePicture *end,
 FFHWBaseEncodePicture *prev,
 int current_depth,
 FFHWBaseEncodePicture **last)
   {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
   FFHWBaseEncodePicture *pic, *next, *ref;
   int i, len;

@@ -148,20 +147,19 @@ static void

hw_base_encode_set_b_pictures(AVCodecContext *avctx,

   hw_base_encode_add_ref(pic, ref, 0, 1, 0);

   if (i > 1)
-hw_base_encode_set_b_pictures(avctx, start, pic, pic,
+hw_base_encode_set_b_pictures(ctx, start, pic, pic,
 current_depth + 1, &next);
   else
   next = pic;

-hw_base_encode_set_b_pictures(avctx, pic, end, next,
+hw_base_encode_set_b_pictures(ctx, pic, end, next,
 current_depth + 1, last);
   }
   }

-static void hw_base_encode_add_next_prev(AVCodecContext *avctx,
+static void hw_base_encode_add_next_prev(FFHWBaseEncodeContext

*ctx,

FFHWBaseEncodePicture *pic)
   {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
   int i;

   if (!pic)
@@ -333,12 +331,12 @@ static int

hw_base_encode_pick_next(AVCodecContext *avctx,

   }

   if (b_counter > 0) {
-hw_base_encode_set_b_pictures(avctx, start, pic, pic, 1,
+hw_base_encode_set_b_pictures(ctx, start, pic, pic, 1,
 &prev);
   } else {
   prev = pic;
   }
-hw_base_encode_add_next_prev(avctx, prev);
+hw_base_encode_add_next_prev(ctx, prev);

   return 0;
   }
@@ -687,9 +685,9 @@ int ff_hw_base_init_gop_structure(AVCodecContext

*avctx, uint32_t ref_l0, uint32

   return 0;
   }

-int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void

*hwconfig, enum AVPixelFormat *fmt)

+int ff_hw_base_get_recon_format(FFHWBaseEncodeContext *ctx, const

void *hwconfig,

+enum AVPixelFormat *fmt)
   {
-FFHWBaseEncodeContext *ctx = avctx->priv_data;
   AVHWFramesConstraints *constraints = NULL;
   enum AVPixelFormat recon_format;
   int err, i;
@@ -722,14 +720,14 @@ int

ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig,
enu

   // No idea what to use; copy input format.
   recon_format = ctx->input_frames->sw_format;
   }
-av_log(avctx, AV_LOG_DE

Re: [FFmpeg-devel] [PATCH] avcodec/mediacodec: Add support of dynamic bitrate

2024-05-28 Thread Zhao Zhili

> 在 2024年5月29日,上午5:56,Dmitrii Okunev  写道:
> 
> MediaCodec supports parameter "video-bitrate" to change the bitrate
> on fly. This commit adds capability to use it.
> 
> It adds option -bitrate_ctrl_socket to the encoder which makes
> the encoder to create an UNIX socket and listen for messages
> to change the bitrate.

Thank you for your contribution. However, libavcodec as a library has a clear 
define of margin, IO such as socket cannot be put into libavcodec. Reconfigure 
bitrate should be done via libavcodec API.

> 
> An example of ffmpeg execution:
> 
>ffmpeg -f flv -i rtmp://0.0.0.0:1935/live/myStream -c:v hevc_mediacodec 
> -bitrate_ctrl_socket /run/bitrate.sock -b:v 8M -f flv 
> rtmp://127.0.0.1:1935/live/reEncoded
> 
> An example of changing the bitrate to 1000 Kbps:
> 
>printf '%016X' 100 | xxd -r -p | socat -u STDIN UNIX:/run/bitrate.sock
> 
> Signed-off-by: Dmitrii Okunev 
> ---
> libavcodec/Makefile |   2 +-
> libavcodec/mediacodec_wrapper.c |  50 
> libavcodec/mediacodec_wrapper.h |  12 ++
> libavcodec/mediacodecenc.c  | 128 ++---
> libavcodec/mediacodecenc.h  |  97 
> libavcodec/mediacodecenc_ctrl.c | 194 
> libavcodec/mediacodecenc_ctrl.h |  50 
> 7 files changed, 469 insertions(+), 64 deletions(-)
> create mode 100644 libavcodec/mediacodecenc.h
> create mode 100644 libavcodec/mediacodecenc_ctrl.c
> create mode 100644 libavcodec/mediacodecenc_ctrl.h
> 
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 2443d2c6fd..393877c8c6 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -258,7 +258,7 @@ OBJS-$(CONFIG_AURA2_DECODER)   += aura.o
> OBJS-$(CONFIG_AV1_DECODER) += av1dec.o av1_parse.o
> OBJS-$(CONFIG_AV1_CUVID_DECODER)   += cuviddec.o
> OBJS-$(CONFIG_AV1_MEDIACODEC_DECODER)  += mediacodecdec.o
> -OBJS-$(CONFIG_AV1_MEDIACODEC_ENCODER)  += mediacodecenc.o
> +OBJS-$(CONFIG_AV1_MEDIACODEC_ENCODER)  += mediacodecenc.o 
> mediacodecenc_ctrl.o
> OBJS-$(CONFIG_AV1_NVENC_ENCODER)   += nvenc_av1.o nvenc.o
> OBJS-$(CONFIG_AV1_QSV_ENCODER) += qsvenc_av1.o
> OBJS-$(CONFIG_AV1_VAAPI_ENCODER)   += vaapi_encode_av1.o av1_levels.o
> diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
> index 96c88a..fe0e291cad 100644
> --- a/libavcodec/mediacodec_wrapper.c
> +++ b/libavcodec/mediacodec_wrapper.c
> @@ -174,6 +174,7 @@ struct JNIAMediaCodecFields {
> jmethodID get_name_id;
> 
> jmethodID configure_id;
> +jmethodID set_parameters_id;
> jmethodID start_id;
> jmethodID flush_id;
> jmethodID stop_id;
> @@ -247,6 +248,9 @@ static const struct FFJniField jni_amediacodec_mapping[] 
> = {
> 
> { "android/media/MediaCodec", "setInputSurface", 
> "(Landroid/view/Surface;)V", FF_JNI_METHOD, OFFSET(set_input_surface_id), 0 },
> { "android/media/MediaCodec", "signalEndOfInputStream", "()V", 
> FF_JNI_METHOD, OFFSET(signal_end_of_input_stream_id), 0 },
> +#if __ANDROID_API__ >= 26
> +{ "android/media/MediaCodec", "setParameters", 
> "(Landroid/media/MediaFormat;)V", FF_JNI_METHOD, OFFSET(set_parameters_id), 1 
> },
> +#endif
> 
> { "android/media/MediaCodec$BufferInfo", NULL, NULL, FF_JNI_CLASS, 
> OFFSET(mediainfo_class), 1 },
> 
> @@ -1411,6 +1415,27 @@ fail:
> return ret;
> }
> 
> +#if __ANDROID_API__ >= 26
> +static int mediacodec_jni_setParameters(FFAMediaCodec *ctx,
> +const FFAMediaFormat *format_ctx)
> +{
> +int ret = 0;
> +JNIEnv *env = NULL;
> +FFAMediaCodecJni *codec = (FFAMediaCodecJni *)ctx;
> +const FFAMediaFormatJni *format = (FFAMediaFormatJni *)format_ctx;
> +
> +JNI_GET_ENV_OR_RETURN(env, codec, AVERROR_EXTERNAL);
> +
> +(*env)->CallVoidMethod(env, codec->object, 
> codec->jfields.set_parameters_id, format->object);
> +
> +if (ff_jni_exception_check(env, 1, codec) < 0) {
> +ret = AVERROR_EXTERNAL;
> +}
> +
> +return ret;
> +}
> +#endif // __ANDROID_API__ >= 26
> +
> static int mediacodec_jni_start(FFAMediaCodec* ctx)
> {
> int ret = 0;
> @@ -1821,6 +1846,10 @@ static const FFAMediaCodec media_codec_jni = {
> .getConfigureFlagEncode = mediacodec_jni_getConfigureFlagEncode,
> .cleanOutputBuffers = mediacodec_jni_cleanOutputBuffers,
> .signalEndOfInputStream = mediacodec_jni_signalEndOfInputStream,
> +
> +#if __ANDROID_API__ >= 26
> +.setParameters = mediacodec_jni_setParameters,
> +#endif //__ANDROID_API__ >= 26
> };
> 
> typedef struct FFAMediaFormatNdk {
> @@ -2178,6 +2207,23 @@ static int mediacodec_ndk_configure(FFAMediaCodec* ctx,
> return 0;
> }
> 
> +#if __ANDROID_API__ >= 26
> +static int mediacodec_ndk_setParameters(FFAMediaCodec *ctx,
> +const FFAMediaFormat *format_ctx)
> +{
> +FFAMediaCodecNdk *codec = (FFAMediaCodecNdk *)ctx;
> +FFAMediaFormatNdk *format = (FFAMediaFormatNdk *)forma