From patchwork Sun Oct 8 20:01:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 5468 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.90 with SMTP id m26csp1905657jah; Sun, 8 Oct 2017 13:03:32 -0700 (PDT) X-Received: by 10.223.176.75 with SMTP id g11mr7465285wra.211.1507493012004; Sun, 08 Oct 2017 13:03:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507493011; cv=none; d=google.com; s=arc-20160816; b=YpuOfpRfWKpn9VgKQf7h/DvWAnHVHCzCuXpvNEkes+RA6NB1uk9qKmhvlVnCzl+0R3 Q5YHgfEUX300sPh0MAUJEpnMBkj8ENPzvZ2kDt7ssC+Qi0mEoQ00d10ept11MOr3mof8 Nck/4SzczHF9Vj97lVpvClXrg8a7IzIeowcyijV57cJk9XMuGF4zQQw/GX6e25WUvj8h RG/Pnr1VDHD7GUFCyk3z7BlBYablVO0z8PZEsRXcHsl7D+rlpWx8UZiFzw8B+ASQXe4b EK9xynWDupFcW+1LQdcts8utCTImll+unC5/9x/c49uSEh1LS7A7zybay3aaiALzUzRn hC1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=G/DaNGqgUAfasS8/bfwyakr7K07FhWvu5nTnaSZiPrU=; b=oOPf4vLXcFBu+bo9p+4uWWyM2yeJBJQpmv+0qtVO/+4I3FOLhHeBqLE7kT83rbLM3u Yn+f21l1HzCPQ3Wvwxm5L+Kfe0BlPqYyVwCMjZX+xKDt8eYvEYJS6+9+Rtr8LNhGMVNB Wn3qtYKB6IlRUKgKwZ0haNecx9gqHr1gkFeDUhPCCX1L6adfL6WG6ij7oPINKk5aR3Y/ WxL7XbPbl5und/K/BpAc4KVNuCN0Z2aj3SNupdVssKzNZNpX3MjWWHumaIPGSltefNCc hTmjBlHwaxz/jY5xOekuu0j3NID19aiiZeRz1Q24rPkxDXh0wwlnRrSnYFeNhgCWqx46 vqlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=yyTTr4AS; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h190si5427469wmd.25.2017.10.08.13.03.31; Sun, 08 Oct 2017 13:03:31 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=yyTTr4AS; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 60E44689E33; Sun, 8 Oct 2017 23:02:10 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f46.google.com (mail-wm0-f46.google.com [74.125.82.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3F92D689DC0 for ; Sun, 8 Oct 2017 23:02:08 +0300 (EEST) Received: by mail-wm0-f46.google.com with SMTP id q132so18490651wmd.2 for ; Sun, 08 Oct 2017 13:02:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=4P7kG/Szegg1oZ2t1UNRG3SVBv0hHbQ1A6OhXyfv/GY=; b=yyTTr4ASEHbj1SSQBbCuG5+GPxPUCap0ZnARf0+B97y0J3TuU7RwfbJjI+GRX/UMc2 TzdOUb3XsdaZgYb3lT/A/rLV1VAxuMfyzcdogoDq3YTSYXsabR9lQomNVMW7fe01WRWb 31SFIFZojQDX99ht0yJ1BXYYwltt3LJsSCg4YC4VqMzLt+Bks6slvm60NZSYszYQ7K+O RtbQ9/wxkfvvAHNZa2glvjt4qUD343ZQWCCdGNdu0yON64y0AenEnKgWWodMhTd2Se+L iejzdvLTGMwzyY1U3jPj31jWBNS/ZStCJZ3YvgzQaWYS49hEDvvXsLzQqm/b0ZB4f0S7 n98Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=4P7kG/Szegg1oZ2t1UNRG3SVBv0hHbQ1A6OhXyfv/GY=; b=RnX9lI/qR4L5Gywett9pXCfWEtnIhr7tvvaARzA+oSkxLUte0sR0eGgzxHIOycFlCg 4pDOOTYYux7j3jwVwOpM5iPHZPKUEICAqMbZQF7M2TrmEPb92x0eY1tRrZQ5/DYl+ejD 5GyLH0j6z/bJhCMdtdMwAHxdorMokWRbTIiKJYzWph8+IeaFtRm7oH5xtGyp+V2ddp9V PjpiVrM6lgqgXANtFVSm5B1ez2SrZq+7njihv1RUXwsJDabMFSfijpWgvE3GFI81lMEl KdJOi6HGI6l+ZBC+uPdorD1/SuWUt87kir85+RglUIzmRh0Fi0dOCjQ0w5fBc9mMU7DG q8fQ== X-Gm-Message-State: AMCzsaVP8Z+wi2wS52NtJZgnfRP7NRftvR2ZwEkpqu63BU5FKOW3+nRZ f5iFG0B/soI3qpq7e5luYqrNMj2C X-Google-Smtp-Source: AOwi7QCtYcCEH5dpT4wABwPhhLp+cYy1Gt7jvTjSjKERhRiIAtgGJtPF86fQDO5/rn0jMt/cTVY8Pw== X-Received: by 10.223.177.143 with SMTP id q15mr2578401wra.269.1507492929098; Sun, 08 Oct 2017 13:02:09 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id x49sm2897856wrb.25.2017.10.08.13.02.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Oct 2017 13:02:08 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sun, 8 Oct 2017 21:01:45 +0100 Message-Id: <20171008200154.4873-12-sw@jkqxz.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171008200154.4873-1-sw@jkqxz.net> References: <20171008200154.4873-1-sw@jkqxz.net> Subject: [FFmpeg-devel] [PATCH 11/20] mpeg12: Add a common function to find the best frame rate X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This will be used later in both the mpeg2_metadata filter and the VAAPI MPEG-2 encoder. Also adds a unit test. (cherry picked from commit b5859e0b04bdbe12c97cb12ac10a45d51d2d73c9) --- libavcodec/Makefile | 1 + libavcodec/mpeg12.h | 4 ++ libavcodec/mpeg12framerate.c | 64 ++++++++++++++++++++++++++++ libavcodec/tests/mpeg12framerate.c | 87 ++++++++++++++++++++++++++++++++++++++ tests/fate/libavcodec.mak | 5 +++ 5 files changed, 161 insertions(+) create mode 100644 libavcodec/tests/mpeg12framerate.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 8a68c1b929..a0b23a794e 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1090,6 +1090,7 @@ TESTPROGS-$(CONFIG_GOLOMB) += golomb TESTPROGS-$(CONFIG_IDCTDSP) += dct TESTPROGS-$(CONFIG_IIRFILTER) += iirfilter TESTPROGS-$(HAVE_MMX) += motion +TESTPROGS-$(CONFIG_MPEGVIDEO) += mpeg12framerate TESTPROGS-$(CONFIG_RANGECODER) += rangecoder TESTPROGS-$(CONFIG_SNOW_ENCODER) += snowenc diff --git a/libavcodec/mpeg12.h b/libavcodec/mpeg12.h index f551504b8c..1ec99f17e1 100644 --- a/libavcodec/mpeg12.h +++ b/libavcodec/mpeg12.h @@ -73,4 +73,8 @@ void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64], void ff_mpeg1_encode_init(MpegEncContext *s); void ff_mpeg1_encode_slice_header(MpegEncContext *s); +void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, + int *code, int *ext_n, int *ext_d, + int nonstandard); + #endif /* AVCODEC_MPEG12_H */ diff --git a/libavcodec/mpeg12framerate.c b/libavcodec/mpeg12framerate.c index 094cd180a5..ab3d351173 100644 --- a/libavcodec/mpeg12framerate.c +++ b/libavcodec/mpeg12framerate.c @@ -18,6 +18,9 @@ #include "libavutil/rational.h" +#include "mpeg12.h" +#include "mpeg12data.h" + const AVRational ff_mpeg12_frame_rate_tab[16] = { { 0, 0}, {24000, 1001}, @@ -37,3 +40,64 @@ const AVRational ff_mpeg12_frame_rate_tab[16] = { { 15, 1}, { 0, 0}, }; + +void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, + int *code, int *ext_n, int *ext_d, + int nonstandard) +{ + int mpeg2 = ext_n && ext_d; + int max_code = nonstandard ? 12 : 8; + int c, n, d, best_c, best_n, best_d; + AVRational best_error = { INT_MAX, 1 }; + + // Default to NTSC if the inputs make no sense. + best_c = 4; + best_n = best_d = 1; + + for (c = 1; c <= max_code; c++) { + if (av_cmp_q(frame_rate, ff_mpeg12_frame_rate_tab[c]) == 0) { + best_c = c; + goto found; + } + } + + for (c = 1; c <= max_code; c++) { + for (n = 1; n <= (mpeg2 ? 4 : 1); n++) { + for (d = 1; d <= (mpeg2 ? 32 : 1); d++) { + AVRational test, error; + int cmp; + + test = av_mul_q(ff_mpeg12_frame_rate_tab[c], + (AVRational) { n, d }); + + cmp = av_cmp_q(test, frame_rate); + if (cmp == 0) { + best_c = c; + best_n = n; + best_d = d; + goto found; + } + + if (cmp < 0) + error = av_div_q(frame_rate, test); + else + error = av_div_q(test, frame_rate); + + cmp = av_cmp_q(error, best_error); + if (cmp < 0 || (cmp == 0 && n == 1 && d == 1)) { + best_c = c; + best_n = n; + best_d = d; + best_error = error; + } + } + } + } + +found: + *code = best_c; + if (mpeg2) { + *ext_n = best_n - 1; + *ext_d = best_d - 1; + } +} diff --git a/libavcodec/tests/mpeg12framerate.c b/libavcodec/tests/mpeg12framerate.c new file mode 100644 index 0000000000..595bdb278a --- /dev/null +++ b/libavcodec/tests/mpeg12framerate.c @@ -0,0 +1,87 @@ +/* + * 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 "libavcodec/mpeg12.h" +#include "libavcodec/mpeg12data.h" + +int main(void) +{ + int i; + +#define TEST_MATCH(frame_rate, code, ext_n, ext_d) do { \ + AVRational fr = frame_rate; \ + int c, n, d; \ + ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \ + if (c != code || n != ext_n || d != ext_d) { \ + av_log(NULL, AV_LOG_ERROR, "Failed to match %d/%d: " \ + "code = %d, ext_n = %d, ext_d = %d.\n", \ + fr.num, fr.den, c, n, d); \ + return 1; \ + } \ + } while (0) +#define TEST_EXACT(frn, frd) do { \ + AVRational fr = (AVRational) { frn, frd }; \ + int c, n, d; \ + ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \ + if (av_cmp_q(fr, av_mul_q(ff_mpeg12_frame_rate_tab[c], \ + (AVRational) { n + 1, d + 1 })) != 0) { \ + av_log(NULL, AV_LOG_ERROR, "Failed to find exact %d/%d: " \ + "code = %d, ext_n = %d, ext_d = %d.\n", \ + fr.num, fr.den, c, n, d); \ + return 1; \ + } \ + } while (0) + + // Framerates in the table must be chosen exactly. + for (i = 1; i <= 8; i++) + TEST_MATCH(ff_mpeg12_frame_rate_tab[i], i, 0, 0); + + // As should the same ones with small perturbations. + // (1/1000 used here to be smaller than half the difference + // between 24 and 24000/1001.) + for (i = 1; i <= 8; i++) { + TEST_MATCH(av_sub_q(ff_mpeg12_frame_rate_tab[i], + (AVRational) { 1, 1000 }), i, 0, 0); + TEST_MATCH(av_add_q(ff_mpeg12_frame_rate_tab[i], + (AVRational) { 1, 1000 }), i, 0, 0); + } + + // Exactly constructable framerates should be exact. Note that some + // values can be made in multiple ways (e.g. 12 = 24 / 2 == 60 / 5), + // and there is no reason to favour any particular choice. + TEST_EXACT( 1, 1); + TEST_EXACT( 2, 1); + TEST_EXACT( 12, 1); + TEST_EXACT( 15000, 1001); + TEST_EXACT( 15, 1); + TEST_EXACT( 120, 1); + TEST_EXACT(120000, 1001); + TEST_EXACT( 200, 1); + TEST_EXACT( 240, 1); + + // Values higher than 240 (the highest representable, as 60 * 4 / 1) + // should be mapped to 240. + for (i = 240; i < 1000; i += 10) + TEST_MATCH(((AVRational) { i, 1 }), 8, 3, 0); + // Values lower than 24000/32032 (the lowest representable, as + // 24000/1001 * 1 / 32) should be mapped to 24000/32032. + for (i = 74; i > 0; i--) + TEST_MATCH(((AVRational) { i, 100 }), 1, 0, 31); + + return 0; +} diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak index 27d631d16c..a2d5d9fe33 100644 --- a/tests/fate/libavcodec.mak +++ b/tests/fate/libavcodec.mak @@ -45,6 +45,11 @@ FATE_LIBAVCODEC-$(CONFIG_IIRFILTER) += fate-iirfilter fate-iirfilter: libavcodec/tests/iirfilter$(EXESUF) fate-iirfilter: CMD = run libavcodec/tests/iirfilter +FATE_LIBAVCODEC-$(CONFIG_MPEGVIDEO) += fate-mpeg12framerate +fate-mpeg12framerate: libavcodec/tests/mpeg12framerate$(EXESUF) +fate-mpeg12framerate: CMD = run libavcodec/tests/mpeg12framerate +fate-mpeg12framerate: REF = /dev/null + FATE_LIBAVCODEC-yes += fate-libavcodec-options fate-libavcodec-options: libavcodec/tests/options$(EXESUF) fate-libavcodec-options: CMD = run libavcodec/tests/options