PR #26123 opened by yiijima URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26123 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26123.patch
# Summary of changes Fix MPEG-2 field pictures being rejected with `-err_detect explode`, and preserve errors from the first field when slice threading is enabled. At the end of a field row, `mb_y` advances by two, but `ff_er_add_slice()` with `endx == -1` steps back by one row. Correct the endpoint in both decoding paths and account for bottom-field parity in the last worker range. Collect worker return values and check each field's aggregate before the next field resets the counters. Include missing rows before the first slice, respecting `skip_top` (including negative values, which previously behaved as zero). Add `fate-mpeg2-field-er`, a self-contained API test that generates 32x128 grey MPEG-2 pictures. Its 156 cases cover frame pictures, both field orders, missing/zeroed first, middle and last slices, 1/2/4/auto threads, output pixels, flushing, and `skip_top`. No new FATE sample files are required. # Validation Tested on macOS arm64 against master `882d3313028897abefc0d236c8769267c528ec85`: - FFmpeg and the new test build successfully. - `make fate-mpeg2-field-er` passes; the same test with the pristine decoder fails 27 cases. Disabling the leading-row accounting fails 15 cases; inverting the per-field error check fails 33. Restoring the fix passes. - `make -j6 fate` passes all 321 tests available in the minimal build without an external sample corpus. - Five MPEG-2 broadcast ES extracts (3,232 frames total) and the first complete GOP of [the public field-encoding sample](https://fate-suite.ffmpeg.org/mpeg2/mpeg2_field_encoding.ts) (12 frames) decode successfully with `-xerror -err_detect explode` at 1/2/4/auto threads. All framemd5 frame rows match pristine ordinary decoding. - Five damaged ES controls, including missing and zeroed slices in either field, are rejected at every tested thread count. The public transport sample has incomplete outer packets; the comparison uses the first complete 201,872-byte GOP of its extracted MPEG-2 ES. The regression test itself does not depend on this sample. >From 8c3659e66080fb0e6a7b1ded355a62c8ad695fd4 Mon Sep 17 00:00:00 2001 From: yiijima <[email protected]> Date: Tue, 17 Nov 2026 05:12:29 +0000 Subject: [PATCH] avcodec/mpeg12dec: fix field-picture error accounting At the end of a field row, mb_y advances by two rows, whereas ff_er_add_slice() with endx == -1 steps back by only one. Adjust the endpoint in both the serial and slice-threaded paths, and include the bottom-field parity in the last worker's end row. Propagate slice-worker failures under AV_EF_EXPLODE and check the aggregated error count before the next field resets it. Count missing rows before the first worker as well, while preserving skip_top behavior. Add a self-contained FATE test generating small grey MPEG-2 frame and field pictures. Cover both field orders, missing and zeroed first, middle and last slices, 1/2/4/auto threads, decoded pixels, flushing, and positive/negative skip_top values. --- libavcodec/Makefile | 1 + libavcodec/mpeg12dec.c | 38 +++++-- libavcodec/tests/mpeg2_field.c | 195 +++++++++++++++++++++++++++++++++ tests/fate/libavcodec.mak | 5 + 4 files changed, 231 insertions(+), 8 deletions(-) create mode 100644 libavcodec/tests/mpeg2_field.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index f12ad9c8e8d..e1151ef3561 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1406,6 +1406,7 @@ TESTPROGS-$(CONFIG_DXV_ENCODER) += hashtable TESTPROGS-$(CONFIG_MJPEG_ENCODER) += mjpegenc_huffman TESTPROGS-$(CONFIG_MPEGAUDIOHEADER) += mpegaudiodecheader TESTPROGS-$(CONFIG_MPEGVIDEO) += mpeg12framerate +TESTPROGS-$(CONFIG_MPEG2VIDEO_DECODER) += mpeg2_field TESTPROGS-$(CONFIG_H264_METADATA_BSF) += h264_levels TESTPROGS-$(CONFIG_HEVC_METADATA_BSF) += h265_levels TESTPROGS-$(CONFIG_RANGECODER) += rangecoder diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index f1ced4fb563..417e0b02a0a 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -1681,8 +1681,10 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) s->c.mb_x, s->c.mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR); } else { + // With endx == -1, ER steps back one row, but fields advance by two. ff_er_add_slice(&s->c.er, s->c.resync_mb_x, s->c.resync_mb_y, - s->c.mb_x - 1, s->c.mb_y, + s->c.mb_x - 1, + s->c.mb_y - (field_pic && !s->c.mb_x), ER_AC_END | ER_DC_END | ER_MV_END); } @@ -2218,22 +2220,32 @@ static int mpeg_decode_gop(AVCodecContext *avctx, return 0; } -static void mpeg12_execute_slice_threads(AVCodecContext *avctx, - Mpeg1Context *const s) +static int mpeg12_execute_slice_threads(AVCodecContext *avctx, + Mpeg1Context *const s) { if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && !avctx->hwaccel) { MPVContext *const s2 = &s->slice.c; int error_count = 0; + int ret[MAX_THREADS]; + + if (s->slice_count) { + int skip_top = s2->codec_id == AV_CODEC_ID_MPEG2VIDEO ? FFMAX(avctx->skip_top, 0) : 0; + // Worker counts start at the first slice, excluding missing rows before it. + error_count = 3 * s2->mb_width * ((s2->start_mb_y - skip_top) >> + (s2->picture_structure != PICT_FRAME)); + } avctx->execute(avctx, slice_decode_thread, - s2->mpeg12_contexts, NULL, + s2->mpeg12_contexts, ret, s->slice_count, sizeof(s2->mpeg12_contexts[0])); for (int i = 0; i < s->slice_count; i++) { MpegEncContext *const slice = s2->thread_context[i]; int slice_err = atomic_load_explicit(&slice->er.error_count, memory_order_relaxed); + if (ret[i] < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) + return ret[i]; // error_count can get set to INT_MAX on serious errors. // So use saturated addition. if ((unsigned)slice_err > INT_MAX - error_count) { @@ -2244,7 +2256,11 @@ static void mpeg12_execute_slice_threads(AVCodecContext *avctx, } atomic_store_explicit(&s2->er.error_count, error_count, memory_order_relaxed); + // Check each field before the next one resets the slice error counts. + if (error_count && (avctx->err_recognition & AV_EF_EXPLODE)) + return AVERROR_INVALIDDATA; } + return 0; } static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, @@ -2264,7 +2280,9 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code); if (start_code > 0x1ff) { if (!skip_frame) { - mpeg12_execute_slice_threads(avctx, s); + ret = mpeg12_execute_slice_threads(avctx, s); + if (ret < 0) + return ret; ret = slice_end(avctx, picture, got_output); if (ret < 0) @@ -2326,8 +2344,10 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, s2->intra_matrix[0]= 1; } if (s->slice_count) { - mpeg12_execute_slice_threads(avctx, s); + ret = mpeg12_execute_slice_threads(avctx, s); s->slice_count = 0; + if (ret < 0) + return ret; } if (last_code == 0 || last_code == SLICE_MIN_START_CODE) { ret = mpeg_decode_postinit(avctx); @@ -2531,7 +2551,8 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, Mpeg12SliceContext *const thread_context = s2->mpeg12_contexts[s->slice_count]; thread_context->c.start_mb_y = mb_y; - thread_context->c.end_mb_y = s2->mb_height; + thread_context->c.end_mb_y = s2->mb_height + + (s2->picture_structure == PICT_BOTTOM_FIELD); if (s->slice_count) { s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y; ret = ff_update_duplicate_context(&thread_context->c, s2); @@ -2556,7 +2577,8 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR); } else { ff_er_add_slice(&s2->er, s2->resync_mb_x, - s2->resync_mb_y, s2->mb_x - 1, s2->mb_y, + s2->resync_mb_y, s2->mb_x - 1, + s2->mb_y - (field_pic && !s2->mb_x), ER_AC_END | ER_DC_END | ER_MV_END); } } diff --git a/libavcodec/tests/mpeg2_field.c b/libavcodec/tests/mpeg2_field.c new file mode 100644 index 00000000000..949071e23c5 --- /dev/null +++ b/libavcodec/tests/mpeg2_field.c @@ -0,0 +1,195 @@ +/* + * 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 <limits.h> +#include <stdio.h> +#include <string.h> + +#include "config.h" +#include "libavutil/error.h" +#include "libavcodec/avcodec.h" +#include "libavcodec/put_bits.h" + +static void start_code(PutBitContext *pb, int code) +{ + align_put_bits(pb); + put_bits32(pb, 0x100 | code); +} + +/* A 32x128 grey I picture, encoded as a frame or two fields, with one + * two-macroblock slice per row. Damage a slice by + * omitting it (1), or replacing its macroblock data with zeroes (2). */ +static int make_picture(uint8_t *data, int size, int first, int damaged, int damage, int damaged_row) +{ + PutBitContext pb; + init_put_bits(&pb, data, size); + start_code(&pb, 0xb3); + put_bits(&pb, 12, 32); // horizontal_size + put_bits(&pb, 12, 128); // vertical_size + put_bits(&pb, 4, 1); // square pixels + put_bits(&pb, 4, 3); // 25 fps + put_bits(&pb, 18, 0x3ffff); // variable bitrate + put_bits(&pb, 1, 1); // marker_bit + put_bits(&pb, 10, 112); // vbv_buffer_size + put_bits(&pb, 3, 0); // constrained_parameters, default matrices + start_code(&pb, 0xb5); + put_bits(&pb, 4, 1); // sequence extension + put_bits(&pb, 8, 0x48); // Main Profile, Main Level + put_bits(&pb, 1, 0); // interlaced sequence + put_bits(&pb, 2, 1); // 4:2:0 + put_bits(&pb, 16, 0); // size and bitrate extensions + put_bits(&pb, 1, 1); // marker_bit + put_bits(&pb, 8, 0); // vbv_buffer_size_extension + put_bits(&pb, 1, 1); // low_delay + put_bits(&pb, 7, 0); // frame rate extensions + + for (int field = 1; field <= (first == 3 ? 1 : 2); field++) { + int structure = field == 1 ? first : 3 - first; + start_code(&pb, 0); + put_bits(&pb, 10, 0); // temporal_reference + put_bits(&pb, 3, 1); // I picture + put_bits(&pb, 16, 0xffff); + put_bits(&pb, 1, 0); // extra_bit_picture + start_code(&pb, 0xb5); + put_bits(&pb, 4, 8); // picture coding extension + put_bits(&pb, 16, 0xffff); + put_bits(&pb, 2, 0); // intra_dc_precision + put_bits(&pb, 2, structure); + put_bits(&pb, 1, first == 1); // top_field_first + put_bits(&pb, 1, structure == 3); // frame_pred_frame_dct + put_bits(&pb, 8, 0); // remaining picture coding flags + for (int row = 1; row <= (structure == 3 ? 8 : 4); row++) { + if (field == damaged && row == damaged_row && damage == 1) + continue; + start_code(&pb, row); + put_bits(&pb, 5, 2); // quantiser_scale_code + put_bits(&pb, 1, 0); // extra_bit_slice + for (int mb = 0; mb < 2; mb++) { + if (field == damaged && row == damaged_row && damage == 2) { + put_bits(&pb, 30, 0); + continue; + } + put_bits(&pb, 1, 1); // macroblock_address_increment = 1 + put_bits(&pb, 1, 1); // intra macroblock + for (int block = 0; block < 6; block++) { + put_bits(&pb, block < 4 ? 3 : 2, block < 4 ? 4 : 0); + put_bits(&pb, 2, 2); // zero DC difference, then EOB + } + } + } + } + start_code(&pb, 0xb7); + flush_put_bits(&pb); + return put_bytes_output(&pb); +} + +static int check_picture(int threads, int first, int damaged, int damage, + int damaged_row, int skip_top) +{ + const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MPEG2VIDEO); + AVCodecContext *ctx = avcodec_alloc_context3(codec); + AVPacket *pkt = av_packet_alloc(); + AVFrame *frame = av_frame_alloc(); + int ret = AVERROR(ENOMEM); + + if (!ctx || !pkt || !frame) + goto end; + ctx->thread_count = threads; + ctx->thread_type = FF_THREAD_SLICE; + ctx->err_recognition = AV_EF_EXPLODE; + ctx->skip_top = skip_top; + ret = avcodec_open2(ctx, codec, NULL); + if (ret < 0) + goto end; + ret = av_new_packet(pkt, 256); + if (ret < 0) + goto end; + memset(pkt->data, 0, pkt->size); + pkt->size = make_picture(pkt->data, pkt->size, first, damaged, damage, damaged_row); + ret = avcodec_send_packet(ctx, pkt); + if (ret >= 0) + ret = avcodec_receive_frame(ctx, frame); + if (damaged) { + ret = ret == AVERROR_INVALIDDATA ? 0 : AVERROR_BUG; + goto end; + } + if (ret < 0) + goto end; + if (frame->width != 32 || frame->height != 128 || + frame->format != AV_PIX_FMT_YUV420P) { + ret = AVERROR_BUG; + goto end; + } + for (int plane = 0; plane < 3; plane++) { + int width = plane ? 16 : 32; + int height = plane ? 64 : 128; + int skipped_rows = FFMAX(skip_top, 0); + if (first != 3) + skipped_rows = (skipped_rows + 1) & ~1; + for (int y = skipped_rows * (plane ? 8 : 16); y < height; y++) { + for (int x = 0; x < width; x++) { + if (frame->data[plane][y * frame->linesize[plane] + x] != 128) { + ret = AVERROR_BUG; + goto end; + } + } + } + } + av_frame_unref(frame); + ret = avcodec_send_packet(ctx, NULL); + if (ret >= 0) + ret = avcodec_receive_frame(ctx, frame); + ret = ret == AVERROR_EOF ? 0 : AVERROR_BUG; +end: + if (ret < 0) + fprintf(stderr, "threads=%d first=%d damaged=%d damage=%d row=%d skip_top=%d: %s\n", + threads, first, damaged, damage, damaged_row, skip_top, av_err2str(ret)); + av_frame_free(&frame); + av_packet_free(&pkt); + avcodec_free_context(&ctx); + return ret < 0; +} + +int main(void) +{ + const int threads[] = { 1, 2, 4, 0 }; + int ret = 0; + av_log_set_level(AV_LOG_QUIET); + for (int t = 0; t < FF_ARRAY_ELEMS(threads); t++) { + if (!HAVE_THREADS && threads[t] != 1) + continue; + for (int first = 1; first <= 3; first++) { + const int rows[] = { 1, 2, first == 3 ? 8 : 4 }; + ret |= check_picture(threads[t], first, 0, 0, 0, 0); + // Single-thread decoding already treats skip_top as a missing slice. + if (HAVE_THREADS && threads[t] > 1) { + ret |= check_picture(threads[t], first, 0, 0, 0, -1); + ret |= check_picture(threads[t], first, 0, 0, 0, INT_MIN); + ret |= check_picture(threads[t], first, 0, 0, 0, 1); + ret |= check_picture(threads[t], first, 0, 0, 0, 2); + } + for (int field = 1; field <= (first == 3 ? 1 : 2); field++) { + for (int damage = 1; damage <= 2; damage++) { + for (int r = 0; r < FF_ARRAY_ELEMS(rows); r++) + ret |= check_picture(threads[t], first, field, damage, rows[r], 0); + } + } + } + } + return ret; +} diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak index 2e2dabcd148..5da859a3891 100644 --- a/tests/fate/libavcodec.mak +++ b/tests/fate/libavcodec.mak @@ -103,6 +103,11 @@ fate-mpeg12framerate: libavcodec/tests/mpeg12framerate$(EXESUF) fate-mpeg12framerate: CMD = run libavcodec/tests/mpeg12framerate$(EXESUF) fate-mpeg12framerate: REF = /dev/null +FATE_LIBAVCODEC-$(CONFIG_MPEG2VIDEO_DECODER) += fate-mpeg2-field-er +fate-mpeg2-field-er: libavcodec/tests/mpeg2_field$(EXESUF) +fate-mpeg2-field-er: CMD = run libavcodec/tests/mpeg2_field$(EXESUF) +fate-mpeg2-field-er: REF = /dev/null + FATE_LIBAVCODEC-$(CONFIG_RANGECODER) += fate-rangecoder fate-rangecoder: libavcodec/tests/rangecoder$(EXESUF) fate-rangecoder: CMD = run libavcodec/tests/rangecoder$(EXESUF) -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
