From patchwork Tue Aug 1 12:22:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Griffiths X-Patchwork-Id: 4568 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.85 with SMTP id 82csp3599880vsb; Tue, 1 Aug 2017 05:29:01 -0700 (PDT) X-Received: by 10.223.161.195 with SMTP id v3mr14041628wrv.125.1501590541093; Tue, 01 Aug 2017 05:29:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501590541; cv=none; d=google.com; s=arc-20160816; b=y3DmfNJ3Mg+CiBNUdgtEgewrkXEHPjKet3NtoLGFLst08KGU4LrGLvzFDBcIkA3BGx 2bBTgXfdB/HKqt8gQeHSDOgJRbdNMqRpxYi3k3NVSK51Lyq7VvCCxQEUA1Hoc8j8cO7J xE7FoqwzuVsGa+lu29piXUmoqfLbSie5fEuZ5j1jdAkrIT0BHXLkLRydVS3df8jMNQtZ ov8Mhix9YwRPbNlaYbtvRNCO+J6mnKrx6TFyu9MQq5bpkvui//oO5hAdKmpBi7Mq+mLs kq5fMzjy3avp3T73sHRYgg5W0+hkuotG5ZM3ar9wfLrpDZf8zpchu9gXbiOI1XVwt8F+ z6aw== 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:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=WfbdpeX6ROOwF+ra3Ig1tD92U5UCytObwqTyOHPnJn0=; b=UyTI7b9l15RAYXXUODDJX+MBFq9u6BluyvwBPr4WqpjIDXVu+dIxZW//gkOmM+I38b k5fxWPRqwph9z+nC+dxwXBVSH00ncSc88WOnAIE0F7e1nuSR3x+9mfETJXbmsSoF4jxZ 7sutxfIUMhEqJi/LxADGkFxn5iZMLLVbnY4quzNUH8JYnFA4f8QeZ4uxy88IKRFuxB+D j1pqAxqHAgTaESUDQdMZjeB4tgoKRc9nhXmbmqREnD1phffEY3jS6o/t4mriIUW2OujS VDCnvh44huGjqW3uSfs0GXD0vYgeNDrFJikDiVs6ZD4GwCI56jZkY96xWKFMaTfDWHuE dS2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=stHUVQpv; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 201si661433wmr.136.2017.08.01.05.29.00; Tue, 01 Aug 2017 05:29:01 -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=@gmail.com header.s=20161025 header.b=stHUVQpv; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9722468A3E8; Tue, 1 Aug 2017 15:28:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 66AF168A368 for ; Tue, 1 Aug 2017 15:28:49 +0300 (EEST) Received: by mail-wm0-f66.google.com with SMTP id r77so2811836wmd.2 for ; Tue, 01 Aug 2017 05:28:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Q0bCvRysjlR0PfwIBnwD71oTXnmvhrvGFryFAwZ+Y2s=; b=stHUVQpv8HQSOdNveSd/NNEOcQ/fyQIGmGDBPby+zdaunMp4E1XpYMnk6wzEaWK7zl Gww3gtjdtwoz8o1QBt+j00vHd4+dT7gg+IuAIUSAiG+DmJM7ES3DHGhjsRX7z1CRvwWa oX32dO3knHsUR2YjjhNmluJ8yPrcwJjQYE3UTzjBJrnNeczsoATkp0S01kHwrEB6RUrL fX75IYJPj+Dl9I1Ifxlp4LmNityzxupUZNP9FSGBj+htgSWlFq+BGzVERZbiyjlgGJ4B kb9iyOix+NxModkWSf78CkT8TecWVOx5lit2YuqOBvrZqR1qgvLbIvoMOxq0zDcNQyNb pvYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Q0bCvRysjlR0PfwIBnwD71oTXnmvhrvGFryFAwZ+Y2s=; b=Zn+k7ZRw13Yad6NFxJ7lltSVNeZYHAjIwmiieUaGofeWGpTxqXTbpxFTeipAgJE8Dq lEdlAq6PaUGIKdR1V8NShLkwEkwmlySpVoJEcXOe9dTf7vBZxHfKG/ZirYajHgwXgGY4 s5cEO28VaRXEn46ENKe4EZ8EXjtkpGfngG5gRApO1VqR2kS9BjuCLwMo1WeOSwHFC6zC 5mjAMGkQCQaehuuR0TnIYQTh9oQDXIgocI++Z54d+Bo5bC/tBKIgXJwSGIHF6l7Knhsq sebp5FebtOVjBhK4/bSdYJ2gHrvDcZ7LyrVV91DeTW9iBIxC1jD4LVsvBelbwjLGvm3Q jz6g== X-Gm-Message-State: AIVw111MxIptV5HMuHP6/50t46qb7EBo9c+lqv/kDx80zY5t1Q6ri6hS 6zV/ECLwV2rG3Yi7 X-Received: by 10.80.165.114 with SMTP id z47mr17510085edb.60.1501590163418; Tue, 01 Aug 2017 05:22:43 -0700 (PDT) Received: from dhcp-9-20-80-186.hursley.ibm.com (gbibp9ph1--blueice1n0.emea.ibm.com. [195.212.29.66]) by smtp.gmail.com with ESMTPSA id w26sm620453edw.94.2017.08.01.05.22.42 (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 01 Aug 2017 05:22:42 -0700 (PDT) From: David Griffiths To: ffmpeg-devel@ffmpeg.org Date: Tue, 1 Aug 2017 13:22:33 +0100 Message-Id: <20170801122233.15918-1-david.griffiths@gmail.com> X-Mailer: git-send-email 2.13.3 Subject: [FFmpeg-devel] [PATCH] Add new MPEG bitstream filter 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 Cc: David Griffiths MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Given an MPEG1/2 stream, this bitstream filter can be used to modify the sequence headers. The most common use would be to change the aspect ration without the need for re-encoding. Some MOD files have the aspect ratio incorrectly set to 4:3 (see https://en.wikipedia.org/wiki/MOD_and_TOD). --- doc/bitstream_filters.texi | 21 ++++++++ libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/mod_mpeg_seq_header_bsf.c | 102 +++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 libavcodec/mod_mpeg_seq_header_bsf.c diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 2dffe021f9..c9801f966c 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -186,6 +186,27 @@ ffmpeg -i frame_%d.jpg -c:v copy rotated.avi Add an MJPEG A header to the bitstream, to enable decoding by Quicktime. +@section mod_mpeg_seq_header + +Given an MPEG1/2 stream, this bitstream filter can be used to modify +the sequence headers. The most common use would be to change the +aspect ration without the need for re-encoding. Some MOD files have +the aspect ratio incorrectly set to 4:3 (see +@url{https://en.wikipedia.org/wiki/MOD_and_TOD}). + +To set widescreen format in a mod file do this: + +@example +ffmpeg -i in.mod -bsf:v mod_mpeg_seq_header=aspect_ratio=3 -codec copy out.mpeg +@end example + +@table @option +@item aspect_ratio +Common values are 2 for 4:3 format and 3 for 16:9 (widescreen). +@item frame_rate +Use 2 for 24 fps and 3 for 25 fps. +@end table + @anchor{mov2textsub} @section mov2textsub diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 357fa1a361..e275eb6062 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1002,6 +1002,7 @@ OBJS-$(CONFIG_TEXT2MOVSUB_BSF) += movsub_bsf.o OBJS-$(CONFIG_VP9_RAW_REORDER_BSF) += vp9_raw_reorder_bsf.o OBJS-$(CONFIG_VP9_SUPERFRAME_BSF) += vp9_superframe_bsf.o OBJS-$(CONFIG_VP9_SUPERFRAME_SPLIT_BSF) += vp9_superframe_split_bsf.o +OBJS-$(CONFIG_MOD_MPEG_SEQ_HEADER_BSF) += mod_mpeg_seq_header_bsf.o # thread libraries OBJS-$(HAVE_LIBC_MSVCRT) += file_open.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index ce34de640d..dfe46e3e0c 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -44,6 +44,7 @@ extern const AVBitStreamFilter ff_text2movsub_bsf; extern const AVBitStreamFilter ff_vp9_raw_reorder_bsf; extern const AVBitStreamFilter ff_vp9_superframe_bsf; extern const AVBitStreamFilter ff_vp9_superframe_split_bsf; +extern const AVBitStreamFilter ff_mod_mpeg_seq_header_bsf; #include "libavcodec/bsf_list.c" diff --git a/libavcodec/mod_mpeg_seq_header_bsf.c b/libavcodec/mod_mpeg_seq_header_bsf.c new file mode 100644 index 0000000000..824197746b --- /dev/null +++ b/libavcodec/mod_mpeg_seq_header_bsf.c @@ -0,0 +1,102 @@ +/* + * Modify MPEG Sequence Headers bitstream filter + * Copyright (c) 2017 David Griffiths + * + * 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 + */ + +/** + * @file + * Modify MPEG Sequence Headers bitstream filter + */ + +#include "avcodec.h" +#include "bsf.h" +#include "internal.h" + +#include "libavutil/opt.h" + +typedef struct ModifySeqHeaderContext { + const AVClass *class; + int aspect_ratio; + int frame_rate; + int done_log; +} ModifySeqHeaderContext; + +/** + * This filter can be used to modify the sequence headers for aspect ratio + * and/or frame rate without the need for transcoding. + */ +static int mod_mpeg_seq_header_filter(AVBSFContext *ctx, AVPacket *out) +{ + ModifySeqHeaderContext *s = ctx->priv_data; + AVPacket *in; + int ret; + + ret = ff_bsf_get_packet(ctx, &in); + if (ret < 0) + return ret; + + if (in->data[0] == 0 && in->data[1] == 0 && in->data[2] == 1 && + in->data[3] == 0xb3) { + if (!s->done_log) + av_log(ctx, AV_LOG_INFO, "old aspect byte was %x\n", in->data[7]); + if (s->aspect_ratio != 0) { + in->data[7] = (s->aspect_ratio << 4) | (in->data[7] & 0xf); + } + if (s->frame_rate != 0) { + in->data[7] = s->frame_rate | (in->data[7] & 0xf0); + } + if (!s->done_log) + av_log(ctx, AV_LOG_INFO, "new aspect byte is %x\n", in->data[7]); + s->done_log = 1; + } + + av_packet_move_ref(out, in); + av_packet_free(&in); + + return 0; +} + +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_MPEG1VIDEO, + AV_CODEC_ID_MPEG2VIDEO, + AV_CODEC_ID_NONE, +}; + +#define OFFSET(x) offsetof(ModifySeqHeaderContext, x) +static const AVOption options[] = { + { "aspect_ratio", NULL, OFFSET(aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 14 }, + { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8 }, + { NULL }, +}; + +static const AVClass mod_mpeg_seq_header_class = { + .class_name = "mod_mpeg_seq_header", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + + +const AVBitStreamFilter ff_mod_mpeg_seq_header_bsf = { + .name = "mod_mpeg_seq_header", + .priv_data_size = sizeof(ModifySeqHeaderContext), + .priv_class = &mod_mpeg_seq_header_class, + .filter = mod_mpeg_seq_header_filter, + .codec_ids = codec_ids, +};