This is an automated email from the git hooks/post-receive script. Git pushed a commit to branch master in repository ffmpeg.
commit fa456433196deda8677258e695f6614d0f094c38 Author: Baptiste Coudurier <[email protected]> AuthorDate: Sat Oct 17 11:03:58 2026 -0700 Commit: Baptiste Coudurier <[email protected]> CommitDate: Thu Nov 26 20:47:39 2026 +0000 avcodec/mjpegenc: use standard sampling factors for 4:2:2 and 4:4:4 Use Y 2x1/chroma 1x1 for 4:2:2 and 1x1 for 4:4:4. Encode each macroblock row in up to two 8-line passes for Apple JPEG compatibility. Skip empty passes before quantization to preserve denoising history. Signed-off-by: Baptiste Coudurier <[email protected]> Assisted-by: Fairy --- libavcodec/mjpegenc.c | 77 ++++---------- libavcodec/mjpegenc_common.c | 22 ++-- libavcodec/mpegvideo_enc.c | 188 +++++++++++++++++++-------------- libavcodec/mpegvideoenc.h | 2 +- tests/fate/ffmpeg.mak | 15 +++ tests/ref/fate/ffmpeg-mjpeg-422-slice | 7 ++ tests/ref/fate/ffmpeg-mjpeg-444-psnr | 6 ++ tests/ref/fate/ffmpeg-mjpeg-444-qns | 4 +- tests/ref/fate/ffmpeg-mjpeg-444-slice | 7 ++ tests/ref/fate/ffmpeg-mjpeg-empty-pass | 7 ++ tests/ref/fate/jpeg-mpf-gainmap | 4 +- tests/ref/fate/jpg-icc | 6 +- tests/ref/vsynth/vsynth1-mjpeg-422 | 4 +- tests/ref/vsynth/vsynth1-mjpeg-444 | 4 +- tests/ref/vsynth/vsynth2-mjpeg-422 | 4 +- tests/ref/vsynth/vsynth2-mjpeg-444 | 4 +- tests/ref/vsynth/vsynth3-mjpeg-422 | 4 +- tests/ref/vsynth/vsynth3-mjpeg-444 | 4 +- tests/ref/vsynth/vsynth_lena-mjpeg-422 | 4 +- tests/ref/vsynth/vsynth_lena-mjpeg-444 | 4 +- 20 files changed, 208 insertions(+), 169 deletions(-) diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c index 4bdeef8b66..4966878642 100644 --- a/libavcodec/mjpegenc.c +++ b/libavcodec/mjpegenc.c @@ -462,70 +462,33 @@ static void encode_block(MPVEncContext *const s, int16_t block[], int n) put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]); } -static void mjpeg_record_mb(MPVEncContext *const s, int16_t block[][64], - int unused_x, int unused_y) -{ - if (s->c.chroma_format == CHROMA_444) { - record_block(s, block[0], 0); - record_block(s, block[2], 2); - record_block(s, block[4], 4); - record_block(s, block[8], 8); - record_block(s, block[5], 5); - record_block(s, block[9], 9); - - if (16*s->c.mb_x+8 < s->c.width) { - record_block(s, block[1], 1); - record_block(s, block[3], 3); - record_block(s, block[6], 6); - record_block(s, block[10], 10); - record_block(s, block[7], 7); - record_block(s, block[11], 11); - } - } else { - for (int i = 0; i < 5; i++) - record_block(s, block[i], i); - if (s->c.chroma_format == CHROMA_420) { - record_block(s, block[5], 5); - } else { - record_block(s, block[6], 6); - record_block(s, block[5], 5); - record_block(s, block[7], 7); - } - } -} - static void mjpeg_encode_mb(MPVEncContext *const s, int16_t block[][64], int unused_x, int unused_y) { - if (s->c.chroma_format == CHROMA_444) { - encode_block(s, block[0], 0); - encode_block(s, block[2], 2); - encode_block(s, block[4], 4); - encode_block(s, block[8], 8); - encode_block(s, block[5], 5); - encode_block(s, block[9], 9); + static const uint8_t blocks[][12] = { + [CHROMA_420] = { 0, 1, 2, 3, 4, 5 }, + [CHROMA_422] = { 0, 1, 4, 5, 2, 3, 6, 7 }, + [CHROMA_444] = { 0, 4, 5, 1, 6, 7, 2, 8, 9, 3, 10, 11 }, + }; + const MJpegContext *const m = s->mjpeg_ctx; + const int bottom = s->skipdct & 1; + int count = s->c.chroma_format == CHROMA_422 ? 4 : 6; + const uint8_t *order = blocks[s->c.chroma_format] + bottom * count; - if (16 * s->c.mb_x + 8 < s->c.width) { - encode_block(s, block[1], 1); - encode_block(s, block[3], 3); - encode_block(s, block[6], 6); - encode_block(s, block[10], 10); - encode_block(s, block[7], 7); - encode_block(s, block[11], 11); + if (s->c.chroma_format == CHROMA_444 && 16 * s->c.mb_x + 8 >= s->c.width) + count = 3; + if (m->huffman == HUFFMAN_TABLE_OPTIMAL) { + for (int i = 0; i < count; i++) { + int n = order[i]; + record_block(s, block[n], n); } } else { - for (int i = 0; i < 5; i++) - encode_block(s, block[i], i); - if (s->c.chroma_format == CHROMA_420) { - encode_block(s, block[5], 5); - } else { - encode_block(s, block[6], 6); - encode_block(s, block[5], 5); - encode_block(s, block[7], 7); + for (int i = 0; i < count; i++) { + int n = order[i]; + encode_block(s, block[n], n); } + s->i_tex_bits += get_bits_diff(s); } - - s->i_tex_bits += get_bits_diff(s); } static av_cold int mjpeg_encode_init(AVCodecContext *avctx) @@ -537,7 +500,6 @@ static av_cold int mjpeg_encode_init(AVCodecContext *avctx) s->mjpeg_ctx = m; m2->mpeg.encode_picture_header = mjpeg_amv_encode_picture_header; - // May be overridden below s->encode_mb = mjpeg_encode_mb; if (s->mpv_flags & FF_MPV_FLAG_QP_RD) { @@ -601,7 +563,6 @@ static av_cold int mjpeg_encode_init(AVCodecContext *avctx) if (m->huffman == HUFFMAN_TABLE_OPTIMAL) { // If we are here, we have only one slice_context. So no loop necessary. - s->encode_mb = mjpeg_record_mb; return alloc_huffman(m2); } diff --git a/libavcodec/mjpegenc_common.c b/libavcodec/mjpegenc_common.c index bc58ca9903..b44650d04b 100644 --- a/libavcodec/mjpegenc_common.c +++ b/libavcodec/mjpegenc_common.c @@ -56,12 +56,11 @@ static int put_huffman_table(PutBitContext *p, int table_class, int table_id, return n + 17; } -static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p, - const MJpegContext *m, +static void jpeg_table_header(PutBitContext *p, const MJpegContext *m, const uint8_t intra_matrix_permutation[64], const uint16_t luma_intra_matrix[64], const uint16_t chroma_intra_matrix[64], - int hsample[3], int use_slices, int matrices_differ) + int restart_interval, int matrices_differ) { int size; uint8_t *ptr; @@ -90,10 +89,10 @@ static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p, } } - if (use_slices) { + if (restart_interval) { put_marker(p, DRI); put_bits(p, 16, 4); - put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1); + put_bits(p, 16, restart_interval); } /* huffman table */ @@ -349,6 +348,8 @@ void ff_mjpeg_init_hvsample(const AVCodecContext *avctx, int hsample[4], int vsa hsample[1] = 2 >> chroma_h_shift; hsample[2] = 2 >> chroma_h_shift; } + if (avctx->codec_id == AV_CODEC_ID_MJPEG && vsample[1] == 2) + vsample[0] = vsample[1] = vsample[2] = 1; } void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, @@ -361,7 +362,7 @@ void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, const int lossless = !m; int hsample[4], vsample[4]; int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); - int chroma_matrix; + int chroma_matrix, restart_interval = 0; ff_mjpeg_init_hvsample(avctx, hsample, vsample); @@ -376,9 +377,12 @@ void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, chroma_matrix = !lossless && !!memcmp(luma_intra_matrix, chroma_intra_matrix, sizeof(luma_intra_matrix[0]) * 64); - jpeg_table_header(avctx, pb, m, intra_matrix_permutation, - luma_intra_matrix, chroma_intra_matrix, hsample, - use_slices, chroma_matrix); + /* Restart interval of one macroblock row. */ + if (use_slices) + restart_interval = ((avctx->width - 1) / (8 * hsample[0]) + 1) * (2 / vsample[0]); + jpeg_table_header(pb, m, intra_matrix_permutation, + luma_intra_matrix, chroma_intra_matrix, + restart_interval, chroma_matrix); switch (avctx->codec_id) { case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break; diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index d027b49799..6357b40b3c 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -1128,6 +1128,8 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) static inline void put_dct(MPVEncContext *const s, int16_t *block, int i, uint8_t *dest, int line_size, int qscale) { + if (s->skipdct & (1U << i)) + return; s->c.dct_unquantize_intra(&s->c, block, i, qscale); s->c.idsp.idct_put(dest, line_size, block); } @@ -1142,6 +1144,13 @@ static inline void add_dequant_dct(MPVEncContext *const s, } } +static inline int chroma_block_offset(int pair, int chroma_x_shift, int row_offset) +{ + if (chroma_x_shift) + return pair * row_offset; + return (pair >> 1) * row_offset + (pair & 1) * 8; +} + /** * Performs dequantization and IDCT (if necessary) */ @@ -1151,6 +1160,8 @@ static void mpv_reconstruct_mb(MPVEncContext *const s, int16_t block[12][64]) /* print DCT coefficients */ av_log(s->c.avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->c.mb_x, s->c.mb_y); for (int i = 0; i < 6; i++) { + if (s->c.mb_intra && (s->skipdct & (1U << i))) + continue; for (int j = 0; j < 64; j++) { av_log(s->c.avctx, AV_LOG_DEBUG, "%5d", block[i][s->c.idsp.idct_permutation[j]]); @@ -1200,17 +1211,16 @@ static void mpv_reconstruct_mb(MPVEncContext *const s, int16_t block[12][64]) if (s->c.chroma_y_shift) { put_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale); put_dct(s, block[5], 5, dest_cr, uvlinesize, s->c.chroma_qscale); - } else if (!s->c.chroma_x_shift) { + } else if (s->mjpeg_ctx || !s->c.chroma_x_shift) { const int uv_dct_linesize = uvlinesize << s->c.interlaced_dct; const int uv_dct_offset = s->c.interlaced_dct ? uvlinesize : uvlinesize * block_size; - put_dct(s, block[ 4], 4, dest_cb, uv_dct_linesize, s->c.chroma_qscale); - put_dct(s, block[ 5], 5, dest_cr, uv_dct_linesize, s->c.chroma_qscale); - put_dct(s, block[ 6], 6, dest_cb + block_size, uv_dct_linesize, s->c.chroma_qscale); - put_dct(s, block[ 7], 7, dest_cr + block_size, uv_dct_linesize, s->c.chroma_qscale); - put_dct(s, block[ 8], 8, dest_cb + uv_dct_offset, uv_dct_linesize, s->c.chroma_qscale); - put_dct(s, block[ 9], 9, dest_cr + uv_dct_offset, uv_dct_linesize, s->c.chroma_qscale); - put_dct(s, block[10], 10, dest_cb + uv_dct_offset + block_size, uv_dct_linesize, s->c.chroma_qscale); - put_dct(s, block[11], 11, dest_cr + uv_dct_offset + block_size, uv_dct_linesize, s->c.chroma_qscale); + for (int i = 0; i < (4 >> s->c.chroma_x_shift); i++) { + int offset = chroma_block_offset(i, s->c.chroma_x_shift, uv_dct_offset); + put_dct(s, block[4 + 2 * i], 4 + 2 * i, dest_cb + offset, + uv_dct_linesize, s->c.chroma_qscale); + put_dct(s, block[5 + 2 * i], 5 + 2 * i, dest_cr + offset, + uv_dct_linesize, s->c.chroma_qscale); + } } else { dct_offset >>= 1; dct_linesize >>= 1; @@ -1807,9 +1817,11 @@ static int select_input_picture(MPVMainEncContext *const m) m->reordered_input_picture[0]->reference = m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_B; - if (m->reordered_input_picture[0]->shared || s->c.avctx->rc_buffer_size) { - // input is a shared pix, so we can't modify it -> allocate a new - // one & ensure that the shared one is reusable + if (m->reordered_input_picture[0]->shared || s->c.avctx->rc_buffer_size || + (s->mjpeg_ctx && !s->c.chroma_y_shift && + (s->c.avctx->flags & (AV_CODEC_FLAG_GRAY | AV_CODEC_FLAG_PSNR)) == + (AV_CODEC_FLAG_GRAY | AV_CODEC_FLAG_PSNR))) { + // Preserve input for sharing, VBV retries or grayscale PSNR. av_frame_move_ref(s->new_pic, m->reordered_input_picture[0]->f); ret = prepare_picture(s, m->reordered_input_picture[0]->f, s->new_pic); @@ -2279,6 +2291,25 @@ static void get_visual_weight(int16_t *weight, const uint8_t *ptr, int stride) } } +static av_always_inline void quantize_blocks(MPVEncContext *const s, + const int *skip_dct, int first, int last) +{ + for (int i = first; i < last; i++) { + if (!skip_dct[i]) { + int overflow; + s->c.block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->c.qscale, &overflow); + // FIXME we could decide to change to quantizer instead of + // clipping + // JS: I don't think that would be a good idea it could lower + // quality instead of improve it. Just INTRADC clipping + // deserves changes in quantizer + if (overflow) + clip_coeffs(s, s->block[i], s->c.block_last_index[i]); + } else + s->c.block_last_index[i] = -1; + } +} + static av_always_inline void encode_mb_internal(MPVEncContext *const s, int motion_x, int motion_y, int mb_block_height, @@ -2304,7 +2335,7 @@ static av_always_inline void encode_mb_internal(MPVEncContext *const s, ptrdiff_t wrap_y, wrap_c; for (i = 0; i < mb_block_count; i++) - skip_dct[i] = s->skipdct; + skip_dct[i] = s->skipdct & (1U << i); if (s->adaptive_quant) { const int last_qp = s->c.qscale; @@ -2396,27 +2427,24 @@ static av_always_inline void encode_mb_internal(MPVEncContext *const s, } } - s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y); - s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y); - s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y); - s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y); + for (i = 0; i < 4; i++) { + if (!skip_dct[i]) + s->pdsp.get_pixels(s->block[i], + ptr_y + (i & 1) * 8 + (i >> 1) * dct_offset, + wrap_y); + } if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) { skip_dct[4] = 1; skip_dct[5] = 1; } else { - s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c); - s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c); - if (chroma_format == CHROMA_422) { - s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c); - s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c); - } else if (chroma_format == CHROMA_444) { - s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c); - s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c); - s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c); - s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c); - s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c); - s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c); + for (i = 4; i < mb_block_count; i += 2) { + int offset; + if (skip_dct[i]) + continue; + offset = chroma_block_offset((i - 4) / 2, chroma_x_shift, uv_dct_offset); + s->pdsp.get_pixels(s->block[i], ptr_cb + offset, wrap_c); + s->pdsp.get_pixels(s->block[i + 1], ptr_cr + offset, wrap_c); } } } else { @@ -2539,24 +2567,12 @@ static av_always_inline void encode_mb_internal(MPVEncContext *const s, get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y); if (!skip_dct[3]) get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y); - if (!skip_dct[4]) - get_visual_weight(weight[4], ptr_cb , wrap_c); - if (!skip_dct[5]) - get_visual_weight(weight[5], ptr_cr , wrap_c); - if (chroma_format == CHROMA_422) { - if (!skip_dct[6]) - get_visual_weight(weight[6], ptr_cb + uv_dct_offset, - wrap_c); - if (!skip_dct[7]) - get_visual_weight(weight[7], ptr_cr + uv_dct_offset, - wrap_c); - } else if (chroma_format == CHROMA_444) { - for (i = 6; i < 12; i++) { - const uint8_t *ptr = i & 1 ? ptr_cr : ptr_cb; - if (!skip_dct[i]) - get_visual_weight(weight[i], ptr + (i >= 8) * uv_dct_offset + - ((i >> 1) & 1) * 8, wrap_c); - } + for (i = 4; i < mb_block_count; i++) { + int offset; + if (skip_dct[i]) + continue; + offset = chroma_block_offset((i - 4) / 2, chroma_x_shift, uv_dct_offset); + get_visual_weight(weight[i], (i & 1 ? ptr_cr : ptr_cb) + offset, wrap_c); } memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count); } @@ -2564,19 +2580,16 @@ static av_always_inline void encode_mb_internal(MPVEncContext *const s, /* DCT & quantize */ av_assert2(s->c.out_format != FMT_MJPEG || s->c.qscale == 8); { - for (i = 0; i < mb_block_count; i++) { - if (!skip_dct[i]) { - int overflow; - s->c.block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->c.qscale, &overflow); - // FIXME we could decide to change to quantizer instead of - // clipping - // JS: I don't think that would be a good idea it could lower - // quality instead of improve it. Just INTRADC clipping - // deserves changes in quantizer - if (overflow) - clip_coeffs(s, s->block[i], s->c.block_last_index[i]); - } else - s->c.block_last_index[i] = -1; + if (s->mjpeg_ctx && !chroma_y_shift) { + const int bottom = s->skipdct & 1; + const int chroma_blocks = mb_block_count / 2 - 2; + int first = bottom * 2; + memset(s->c.block_last_index, -1, mb_block_count * sizeof(*s->c.block_last_index)); + quantize_blocks(s, skip_dct, first, first + 2); + first = 4 + bottom * chroma_blocks; + quantize_blocks(s, skip_dct, first, first + chroma_blocks); + } else { + quantize_blocks(s, skip_dct, 0, mb_block_count); } if (s->quantizer_noise_shaping) { for (i = 0; i < mb_block_count; i++) { @@ -2789,6 +2802,9 @@ static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t if(w==16 && h==16) return s->sse_cmp[0](NULL, src1, src2, stride, 16); + else if (w == 16 && h == 8) + return s->sse_cmp[1](NULL, src1, src2, stride, 8) + + s->sse_cmp[1](NULL, src1 + 8, src2 + 8, stride, 8); else if(w==8 && h==8) return s->sse_cmp[1](NULL, src1, src2, stride, 8); @@ -2987,6 +3003,8 @@ int ff_mpv_reallocate_putbitbuffer(MPVEncContext *const s, size_t threshold, siz static int encode_thread(AVCodecContext *c, void *arg){ MPVEncContext *const s = *(void**)arg; int chr_w = 16 >> s->c.chroma_x_shift; + const int row_passes = s->mjpeg_ctx && !s->c.chroma_y_shift ? 2 : 1; + const int psnr_per_pass = row_passes == 2 && !(c->flags & AV_CODEC_FLAG_GRAY); int chr_h = 16 >> s->c.chroma_y_shift; int i; MBBackup best_s = { 0 }, backup_s; @@ -3035,7 +3053,10 @@ static int encode_thread(AVCodecContext *c, void *arg){ s->c.resync_mb_y = 0; s->c.first_slice_line = 1; s->ptr_lastgob = s->pb.buf; - for (int mb_y_order = s->c.start_mb_y; mb_y_order < s->c.end_mb_y; mb_y_order++) { + /* JPEG 4:2:2 and 4:4:4 encode each row in two 8-pixel passes. */ + for (int mb_y_order = s->c.start_mb_y * row_passes; + mb_y_order < s->c.end_mb_y * row_passes; mb_y_order++) { + const int pass = mb_y_order % row_passes; int mb_y; if (CONFIG_SPEEDHQ_ENCODER && s->c.codec_id == AV_CODEC_ID_SPEEDHQ) { int first_in_slice; @@ -3044,8 +3065,17 @@ static int encode_thread(AVCodecContext *c, void *arg){ ff_speedhq_end_slice(s); s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 1024; } else { - mb_y = mb_y_order; + mb_y = mb_y_order / row_passes; } + const int has_bottom = row_passes == 2 && 16 * mb_y + 8 < s->c.height; + /* Do not feed replicated rows from an absent MCU row to the denoiser. */ + if (pass && !has_bottom) + break; + + /* Bit 0 also selects the bottom pass in the quantizer and scan writer. */ + s->skipdct = row_passes == 1 ? 0 + : s->c.chroma_format == CHROMA_422 + ? (pass ? 0x033 : 0x0cc) : (pass ? 0x0f3 : 0xf0c); s->c.mb_x = 0; s->c.mb_y = mb_y; @@ -3083,7 +3113,7 @@ static int encode_thread(AVCodecContext *c, void *arg){ mb_type = s->mb_type[xy]; /* write gob / video packet header */ - if(s->rtp_mode){ + if (s->rtp_mode && !pass) { int current_packet_size, is_gob_start; current_packet_size = put_bytes_count(&s->pb, 1) @@ -3411,7 +3441,7 @@ static int encode_thread(AVCodecContext *c, void *arg){ s->c.mv[1][0][0] = best_s.mv[1][0][0]; s->c.mv[1][0][1] = best_s.mv[1][0][1];*/ backup_s.dquant= 0; - s->skipdct=1; + s->skipdct=~0U; encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb, &dmin, &next_block, mx, my); s->skipdct=0; @@ -3587,22 +3617,24 @@ static int encode_thread(AVCodecContext *c, void *arg){ #endif } - if (s->c.avctx->flags & AV_CODEC_FLAG_PSNR) { - int w= 16; - int h= 16; - - if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16; - if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16; + if ((c->flags & AV_CODEC_FLAG_PSNR) && + (psnr_per_pass || pass || !has_bottom)) { + /* Compare each pass before the next macroblock can overwrite its input. */ + const int line = psnr_per_pass ? pass * 8 : 0; + const int w = FFMIN(16, s->c.width - 16 * s->c.mb_x); + const int h = FFMIN(psnr_per_pass ? 8 : 16, s->c.height - 16 * s->c.mb_y - line); + const int y_offset = (16 * s->c.mb_y + line) * s->c.linesize; + const int c_offset = (chr_h * s->c.mb_y + line) * s->c.uvlinesize; s->encoding_error[0] += sse( - s, s->new_pic->data[0] + s->c.mb_x*16 + s->c.mb_y*s->c.linesize*16, - s->c.dest[0], w, h, s->c.linesize); - s->encoding_error[1] += sse( - s, s->new_pic->data[1] + s->c.mb_x*chr_w + s->c.mb_y*s->c.uvlinesize*chr_h, - s->c.dest[1], w>>s->c.chroma_x_shift, h>>s->c.chroma_y_shift, s->c.uvlinesize); - s->encoding_error[2] += sse( - s, s->new_pic->data[2] + s->c.mb_x*chr_w + s->c.mb_y*s->c.uvlinesize*chr_h, - s->c.dest[2], w>>s->c.chroma_x_shift, h>>s->c.chroma_y_shift, s->c.uvlinesize); + s, s->new_pic->data[0] + s->c.mb_x * 16 + y_offset, + s->c.dest[0] + line * s->c.linesize, w, h, s->c.linesize); + for (int plane = 1; plane < 3; plane++) { + s->encoding_error[plane] += sse( + s, s->new_pic->data[plane] + s->c.mb_x * chr_w + c_offset, + s->c.dest[plane] + line * s->c.uvlinesize, + w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize); + } } if (s->loop_filter) { if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263) diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h index c4275d2b01..848205273c 100644 --- a/libavcodec/mpegvideoenc.h +++ b/libavcodec/mpegvideoenc.h @@ -54,7 +54,7 @@ typedef struct MPVEncContext { int *lambda_table; int adaptive_quant; ///< use adaptive quantization int dquant; ///< qscale difference to prev qscale - int skipdct; ///< skip dct and code zero residual + unsigned skipdct; ///< bitmask of blocks whose DCT is skipped int quantizer_noise_shaping; diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak index 4aba883b09..d704f01be1 100644 --- a/tests/fate/ffmpeg.mak +++ b/tests/fate/ffmpeg.mak @@ -573,3 +573,18 @@ fate-ffmpeg-mjpeg-444-qns: CMD = framecrc -cpuflags 0 -f lavfi -i "testsrc2=s=32 # alpha merging fate-ffmpeg-heif-merge-alpha: CMD = framecrc -auto_conversion_filters -i $(TARGET_SAMPLES)/heif/alpha.heic FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MOV, HEVC, HEVC_PARSER SCALE_FILTER) += fate-ffmpeg-heif-merge-alpha + +# Quality Stats side data includes the encoder's reconstruction error. +# Use scalar reconstruction to keep rounding consistent across architectures. +FATE_FFMPEG-$(call ALLYES, TESTSRC2_FILTER FORMAT_FILTER NOISE_FILTER LAVFI_INDEV WRAPPED_AVFRAME_DECODER MJPEG_ENCODER FRAMECRC_MUXER PIPE_PROTOCOL) += fate-ffmpeg-mjpeg-444-psnr +fate-ffmpeg-mjpeg-444-psnr: CMD = framecrc -cpuflags 0 -f lavfi -i "testsrc2=s=128x120,format=yuvj444p,noise=alls=100:allf=t+u" -flags +bitexact+psnr -dct fastint -idct simple -threads 1 -c:v mjpeg -huffman default -q:v 3 -frames:v 1 + +# Cover restart intervals and partial MCU rows/columns with two slices. +FATE_FFMPEG-$(call ALLYES, TESTSRC2_FILTER FORMAT_FILTER NOISE_FILTER LAVFI_INDEV WRAPPED_AVFRAME_DECODER MJPEG_ENCODER FRAMECRC_MUXER PIPE_PROTOCOL) += fate-ffmpeg-mjpeg-422-slice fate-ffmpeg-mjpeg-444-slice +fate-ffmpeg-mjpeg-422-slice: PIXFMT = yuvj422p +fate-ffmpeg-mjpeg-444-slice: PIXFMT = yuvj444p +fate-ffmpeg-mjpeg-422-slice fate-ffmpeg-mjpeg-444-slice: CMD = framecrc -cpuflags 0 -f lavfi -i "testsrc2=s=34x50,format=$(PIXFMT),noise=alls=100:allf=t+u" -flags +bitexact -dct fastint -idct simple -threads 2 -thread_type slice -slices 2 -c:v mjpeg -huffman default -q:v 3 -frames:v 2 + +# Padding in an empty bottom pass must not affect denoising of the next frame. +FATE_FFMPEG-$(call ALLYES, TESTSRC2_FILTER FORMAT_FILTER NOISE_FILTER LAVFI_INDEV WRAPPED_AVFRAME_DECODER MJPEG_ENCODER FRAMECRC_MUXER PIPE_PROTOCOL) += fate-ffmpeg-mjpeg-empty-pass +fate-ffmpeg-mjpeg-empty-pass: CMD = framecrc -cpuflags 0 -f lavfi -i "testsrc2=s=352x120,format=yuvj422p,noise=alls=100:allf=t+u" -flags +bitexact -dct fastint -idct simple -threads 1 -c:v mjpeg -huffman default -q:v 6 -mbd rd -noise_reduction 2000 -frames:v 2 diff --git a/tests/ref/fate/ffmpeg-mjpeg-422-slice b/tests/ref/fate/ffmpeg-mjpeg-422-slice new file mode 100644 index 0000000000..8e9a9d4398 --- /dev/null +++ b/tests/ref/fate/ffmpeg-mjpeg-422-slice @@ -0,0 +1,7 @@ +#tb 0: 1/25 +#media_type 0: video +#codec_id 0: mjpeg +#dimensions 0: 34x50 +#sar 0: 1/1 +0, 0, 0, 1, 2973, 0x94f48b0e, S=1, Quality stats, 8, 0x031b0064 +0, 1, 1, 1, 3002, 0x4d1ca960, S=1, Quality stats, 8, 0x031b0064 diff --git a/tests/ref/fate/ffmpeg-mjpeg-444-psnr b/tests/ref/fate/ffmpeg-mjpeg-444-psnr new file mode 100644 index 0000000000..21d3babef3 --- /dev/null +++ b/tests/ref/fate/ffmpeg-mjpeg-444-psnr @@ -0,0 +1,6 @@ +#tb 0: 1/25 +#media_type 0: video +#codec_id 0: mjpeg +#dimensions 0: 128x120 +#sar 0: 1/1 +0, 0, 0, 1, 24930, 0x8a250718, S=1, Quality stats, 32, 0x3f8d0332 diff --git a/tests/ref/fate/ffmpeg-mjpeg-444-qns b/tests/ref/fate/ffmpeg-mjpeg-444-qns index bb9bb3c6cc..cfbc7fb287 100644 --- a/tests/ref/fate/ffmpeg-mjpeg-444-qns +++ b/tests/ref/fate/ffmpeg-mjpeg-444-qns @@ -3,5 +3,5 @@ #codec_id 0: mjpeg #dimensions 0: 32x32 #sar 0: 1/1 -0, 0, 0, 1, 1957, 0xc0f86f6f, S=1, Quality stats, 8, 0x031b0064 -0, 1, 1, 1, 1990, 0xe8208929, S=1, Quality stats, 8, 0x031b0064 +0, 0, 0, 1, 1962, 0xc40c720b, S=1, Quality stats, 8, 0x031b0064 +0, 1, 1, 1, 1994, 0x2f8bad3e, S=1, Quality stats, 8, 0x031b0064 diff --git a/tests/ref/fate/ffmpeg-mjpeg-444-slice b/tests/ref/fate/ffmpeg-mjpeg-444-slice new file mode 100644 index 0000000000..ebf32047e7 --- /dev/null +++ b/tests/ref/fate/ffmpeg-mjpeg-444-slice @@ -0,0 +1,7 @@ +#tb 0: 1/25 +#media_type 0: video +#codec_id 0: mjpeg +#dimensions 0: 34x50 +#sar 0: 1/1 +0, 0, 0, 1, 4059, 0xb274a67d, S=1, Quality stats, 8, 0x031b0064 +0, 1, 1, 1, 4073, 0x7bc5b6fb, S=1, Quality stats, 8, 0x031b0064 diff --git a/tests/ref/fate/ffmpeg-mjpeg-empty-pass b/tests/ref/fate/ffmpeg-mjpeg-empty-pass new file mode 100644 index 0000000000..0baa6532b2 --- /dev/null +++ b/tests/ref/fate/ffmpeg-mjpeg-empty-pass @@ -0,0 +1,7 @@ +#tb 0: 1/25 +#media_type 0: video +#codec_id 0: mjpeg +#dimensions 0: 352x120 +#sar 0: 1/1 +0, 0, 0, 1, 30798, 0x14074a76, S=1, Quality stats, 8, 0x063200c7 +0, 1, 1, 1, 27164, 0x40ec71a8, S=1, Quality stats, 8, 0x063200c7 diff --git a/tests/ref/fate/jpeg-mpf-gainmap b/tests/ref/fate/jpeg-mpf-gainmap index 17f3723651..11e7e124c9 100644 --- a/tests/ref/fate/jpeg-mpf-gainmap +++ b/tests/ref/fate/jpeg-mpf-gainmap @@ -1,5 +1,5 @@ -dcf02a726b98394f2084bb83c3f45311 *tests/data/fate/jpeg-mpf-gainmap.jpeg_mpf -5607 tests/data/fate/jpeg-mpf-gainmap.jpeg_mpf +06ffc07709cd942140f0655be0376a33 *tests/data/fate/jpeg-mpf-gainmap.jpeg_mpf +5595 tests/data/fate/jpeg-mpf-gainmap.jpeg_mpf #tb 0: 1/25 #media_type 0: video #codec_id 0: rawvideo diff --git a/tests/ref/fate/jpg-icc b/tests/ref/fate/jpg-icc index 6117cd5656..c6c8dccb68 100644 --- a/tests/ref/fate/jpg-icc +++ b/tests/ref/fate/jpg-icc @@ -1,5 +1,5 @@ -1cc2f8fe008f2d401c68564529627a04 *tests/data/fate/jpg-icc.mjpeg -11007 tests/data/fate/jpg-icc.mjpeg +69b7c60e70350623170d1428ce4bab74 *tests/data/fate/jpg-icc.mjpeg +11041 tests/data/fate/jpg-icc.mjpeg #tb 0: 1/25 #media_type 0: video #codec_id 0: rawvideo @@ -19,7 +19,7 @@ best_effort_timestamp_time=0.000000 duration=1 duration_time=0.040000 pkt_pos=0 -pkt_size=11007 +pkt_size=11041 width=128 height=128 crop_top=0 diff --git a/tests/ref/vsynth/vsynth1-mjpeg-422 b/tests/ref/vsynth/vsynth1-mjpeg-422 index 2378d34649..3c80e7b875 100644 --- a/tests/ref/vsynth/vsynth1-mjpeg-422 +++ b/tests/ref/vsynth/vsynth1-mjpeg-422 @@ -1,4 +1,4 @@ -d83c33032d1e44fbe52474aab938c537 *tests/data/fate/vsynth1-mjpeg-422.avi -1611530 tests/data/fate/vsynth1-mjpeg-422.avi +f26a2f8889727a9e1ef7458e647ec2ef *tests/data/fate/vsynth1-mjpeg-422.avi +1613108 tests/data/fate/vsynth1-mjpeg-422.avi b0085bd74aa6d79fc35f3ff31e99f187 *tests/data/fate/vsynth1-mjpeg-422.out.rawvideo stddev: 7.45 PSNR: 30.69 MAXDIFF: 63 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-mjpeg-444 b/tests/ref/vsynth/vsynth1-mjpeg-444 index 1c6f7219ff..6e4c6f8fe2 100644 --- a/tests/ref/vsynth/vsynth1-mjpeg-444 +++ b/tests/ref/vsynth/vsynth1-mjpeg-444 @@ -1,4 +1,4 @@ -89eda18748c40c0f764737092eedb126 *tests/data/fate/vsynth1-mjpeg-444.avi -1831008 tests/data/fate/vsynth1-mjpeg-444.avi +e83e7b0aa10b4f0a2784ef3f5b69acd2 *tests/data/fate/vsynth1-mjpeg-444.avi +1828032 tests/data/fate/vsynth1-mjpeg-444.avi 34fc0d76860b4c66171b229a4c646939 *tests/data/fate/vsynth1-mjpeg-444.out.rawvideo stddev: 7.37 PSNR: 30.77 MAXDIFF: 63 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-mjpeg-422 b/tests/ref/vsynth/vsynth2-mjpeg-422 index 39210370c2..a3b52b50b8 100644 --- a/tests/ref/vsynth/vsynth2-mjpeg-422 +++ b/tests/ref/vsynth/vsynth2-mjpeg-422 @@ -1,4 +1,4 @@ -a4a733cd9a907fd513f204dbf259c6ab *tests/data/fate/vsynth2-mjpeg-422.avi -877232 tests/data/fate/vsynth2-mjpeg-422.avi +a2045acb68c18e401c0e32bf51b9804d *tests/data/fate/vsynth2-mjpeg-422.avi +874792 tests/data/fate/vsynth2-mjpeg-422.avi bc48c77b4401b3d74cb5d8de7113d908 *tests/data/fate/vsynth2-mjpeg-422.out.rawvideo stddev: 4.69 PSNR: 34.69 MAXDIFF: 55 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-mjpeg-444 b/tests/ref/vsynth/vsynth2-mjpeg-444 index 32d6ec340b..1425a700fb 100644 --- a/tests/ref/vsynth/vsynth2-mjpeg-444 +++ b/tests/ref/vsynth/vsynth2-mjpeg-444 @@ -1,4 +1,4 @@ -032fc918dc65d6096aea29e530bade8a *tests/data/fate/vsynth2-mjpeg-444.avi -1004674 tests/data/fate/vsynth2-mjpeg-444.avi +531bad3860bab46ed0dbae5865a6a385 *tests/data/fate/vsynth2-mjpeg-444.avi +993370 tests/data/fate/vsynth2-mjpeg-444.avi e026aeacdb2e5286b857a1be8499772f *tests/data/fate/vsynth2-mjpeg-444.out.rawvideo stddev: 4.57 PSNR: 34.93 MAXDIFF: 55 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth3-mjpeg-422 b/tests/ref/vsynth/vsynth3-mjpeg-422 index fda59ead01..8662884c4a 100644 --- a/tests/ref/vsynth/vsynth3-mjpeg-422 +++ b/tests/ref/vsynth/vsynth3-mjpeg-422 @@ -1,4 +1,4 @@ -66e29e4154d9648eadc337ea4f129a09 *tests/data/fate/vsynth3-mjpeg-422.avi -52590 tests/data/fate/vsynth3-mjpeg-422.avi +223e94050ddf789ede90fb43fbbeb89a *tests/data/fate/vsynth3-mjpeg-422.avi +49050 tests/data/fate/vsynth3-mjpeg-422.avi df0ccf6369e71097bb31896967f548ef *tests/data/fate/vsynth3-mjpeg-422.out.rawvideo stddev: 8.22 PSNR: 29.82 MAXDIFF: 58 bytes: 86700/ 86700 diff --git a/tests/ref/vsynth/vsynth3-mjpeg-444 b/tests/ref/vsynth/vsynth3-mjpeg-444 index 214dd0e6e5..be4bb999ad 100644 --- a/tests/ref/vsynth/vsynth3-mjpeg-444 +++ b/tests/ref/vsynth/vsynth3-mjpeg-444 @@ -1,4 +1,4 @@ -da1a3e51af1a4f286e96c07fa890df9c *tests/data/fate/vsynth3-mjpeg-444.avi -53970 tests/data/fate/vsynth3-mjpeg-444.avi +e559a8d7018f611a9670b9b2b43010cd *tests/data/fate/vsynth3-mjpeg-444.avi +49980 tests/data/fate/vsynth3-mjpeg-444.avi 1e39838a62f6bddfff9b19aa269ac524 *tests/data/fate/vsynth3-mjpeg-444.out.rawvideo stddev: 8.21 PSNR: 29.84 MAXDIFF: 58 bytes: 86700/ 86700 diff --git a/tests/ref/vsynth/vsynth_lena-mjpeg-422 b/tests/ref/vsynth/vsynth_lena-mjpeg-422 index eab74a30cb..6e85db298b 100644 --- a/tests/ref/vsynth/vsynth_lena-mjpeg-422 +++ b/tests/ref/vsynth/vsynth_lena-mjpeg-422 @@ -1,4 +1,4 @@ -67d060f18debfb7b883e208ab936b0e0 *tests/data/fate/vsynth_lena-mjpeg-422.avi -707100 tests/data/fate/vsynth_lena-mjpeg-422.avi +d187fab02009f142e76b1deaa36d445e *tests/data/fate/vsynth_lena-mjpeg-422.avi +712096 tests/data/fate/vsynth_lena-mjpeg-422.avi e2857389d3c4005ffcf9e8cefb594de7 *tests/data/fate/vsynth_lena-mjpeg-422.out.rawvideo stddev: 4.18 PSNR: 35.70 MAXDIFF: 49 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-mjpeg-444 b/tests/ref/vsynth/vsynth_lena-mjpeg-444 index f497b94f70..d0248f69af 100644 --- a/tests/ref/vsynth/vsynth_lena-mjpeg-444 +++ b/tests/ref/vsynth/vsynth_lena-mjpeg-444 @@ -1,4 +1,4 @@ -2572624967989792957ed34bef3c8dcb *tests/data/fate/vsynth_lena-mjpeg-444.avi -807418 tests/data/fate/vsynth_lena-mjpeg-444.avi +a925da3e98de70894ee6245e266abd4c *tests/data/fate/vsynth_lena-mjpeg-444.avi +812278 tests/data/fate/vsynth_lena-mjpeg-444.avi 45ff52d3bedf515fad74e7a5f42e9c40 *tests/data/fate/vsynth_lena-mjpeg-444.out.rawvideo stddev: 4.05 PSNR: 35.96 MAXDIFF: 49 bytes: 7603200/ 7603200 -- To stop receiving notification emails like this one, please contact [email protected]. _______________________________________________ ffmpeg-cvslog mailing list -- [email protected] To unsubscribe send an email to [email protected]
