From patchwork Sat Aug 15 14:27:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 21677 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id D394A44B587 for ; Sat, 15 Aug 2020 17:27:54 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9F76968B6A1; Sat, 15 Aug 2020 17:27:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 920F1689ABF for ; Sat, 15 Aug 2020 17:27:47 +0300 (EEST) Received: by mail-pg1-f194.google.com with SMTP id g33so5879616pgb.4 for ; Sat, 15 Aug 2020 07:27:47 -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:in-reply-to:references; bh=hiRb1B0YtuLrm/P7VSlLxM5h9G1549hpljRnEaZAuC4=; b=NJK5BaHtOF+6IkaQXUn6wVptZ3l67rINWiBM8mljr0hINgm9S2WfuNylVd55NaGBXc xdK/bFYibuMIk9b5eoG8QCu8sAtO4EfVYQkt6cSa3nTpnCiwcv7R9tgnZrNFvVt8gpaJ MmUQQBoMgraPhUbfn2NQMJZdGsvonAG1flUk5E/tMi2u/ZQ7V0zqK0ymc6f+BeVIr/UI W75YxL9pXHeMU+5CDn6OSUz35X4IGjZTezw8TD10Te8vTqbzb3mEjz8pm7fgJNvlRDnh W6jUEWY184Yh8bePG4i+ESr4TMwOwft3V+i0QPqb/V5gd0hgjUnDl/o/Qup8kdz2qUkT 9F5g== 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:in-reply-to :references; bh=hiRb1B0YtuLrm/P7VSlLxM5h9G1549hpljRnEaZAuC4=; b=ZNacsoAbSxLC64cjUEb6TDb03+3Izoehl1rpODoQ8OJ5Dtp14Gur7MnZLFMq5j1Kd0 25MIyFB5z6IQOz8EN4Ki0pbAfW4wRT4MRIfyNhR4zAv4dIpiuqBuxe1JDIf3nN8IGCeX XHSCjfhWy4XJoubYMqcqjnYpJW4awpvLYSaOneqssFYrnfx1cF0/iynBEVdAyXKgyfxk 4Z995lmj1NNfEryMu7GK/1mgpsOBAZI5JGk/7cptQk+NsVaKZqBGvMTiAw4zGsbNQzVD zhFHX8S6t0P+PO9WzCUVe2kMD6HFUiDbRb7HDBAu2ovPV4Mhb5MpFe88WGT3Ji6gaVxp DUuw== X-Gm-Message-State: AOAM530NFucvvGdw7mi29kDeoti3h4bUwV+/axyQyv5oRaVqLVsYg3kK XNNMFZ3ROvPf+Hp069GqbX4r7BiwQck= X-Google-Smtp-Source: ABdhPJx4N2Baur8/3nrMmd8qJNrdfUkAGXI9a5w1X1Eogj6hd6Eg2TbrNG5JC3dRxks/7gQ9YbhrAA== X-Received: by 2002:aa7:8c56:: with SMTP id e22mr5202437pfd.238.1597501665315; Sat, 15 Aug 2020 07:27:45 -0700 (PDT) Received: from vpn2.localdomain ([161.117.202.209]) by smtp.gmail.com with ESMTPSA id k88sm3625411pjk.19.2020.08.15.07.27.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Aug 2020 07:27:44 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Aug 2020 22:27:38 +0800 Message-Id: <1597501658-27467-1-git-send-email-lance.lmwang@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1597415717-22817-1-git-send-email-lance.lmwang@gmail.com> References: <1597415717-22817-1-git-send-email-lance.lmwang@gmail.com> Subject: [FFmpeg-devel] [PATCH v2] avformat/mpegtsenc: support DVB 6A descriptor for AC-3 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: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang Signed-off-by: Limin Wang --- configure | 2 +- libavformat/mpegts.h | 16 ++++++++++ libavformat/mpegtsenc.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/configure b/configure index d4a1fea..a9d87b2 100755 --- a/configure +++ b/configure @@ -3324,7 +3324,7 @@ mp3_demuxer_select="mpegaudio_parser" mp3_muxer_select="mpegaudioheader" mp4_muxer_select="mov_muxer" mpegts_demuxer_select="iso_media" -mpegts_muxer_select="adts_muxer latm_muxer h264_mp4toannexb_bsf hevc_mp4toannexb_bsf" +mpegts_muxer_select="ac3_parser adts_muxer latm_muxer h264_mp4toannexb_bsf hevc_mp4toannexb_bsf" mpegtsraw_demuxer_select="mpegts_demuxer" mxf_muxer_select="golomb pcm_rechunk_bsf" mxf_d10_muxer_select="mxf_muxer" diff --git a/libavformat/mpegts.h b/libavformat/mpegts.h index fe10b38..d70b25d 100644 --- a/libavformat/mpegts.h +++ b/libavformat/mpegts.h @@ -175,6 +175,22 @@ typedef struct Mp4Descr { SLConfigDescr sl; } Mp4Descr; +/* + * ETSI 300 468 descriptor 0x6A(AC-3) + * Refer to: ETSI EN 300 468 V1.11.1 (2010-04) (SI in DVB systems) + */ +typedef struct DVBAC3Descriptor { + uint8_t component_type_flag; + uint8_t bsid_flag; + uint8_t mainid_flag; + uint8_t asvc_flag; + uint8_t reserved_flags; + uint8_t component_type; + uint8_t bsid; + uint8_t mainid; + uint8_t asvc; +} DVBAC3Descriptor; + /** * Parse an MPEG-2 descriptor * @param[in] fc Format context (used for logging only) diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index 718ddab..876ba99 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -27,6 +27,7 @@ #include "libavutil/mathematics.h" #include "libavutil/opt.h" +#include "libavcodec/ac3_parser_internal.h" #include "libavcodec/internal.h" #include "avformat.h" @@ -244,6 +245,8 @@ typedef struct MpegTSWriteStream { /* For Opus */ int opus_queued_samples; int opus_pending_trim_start; + + DVBAC3Descriptor *dvb_ac3_desc; } MpegTSWriteStream; static void mpegts_write_pat(AVFormatContext *s) @@ -486,9 +489,28 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service) case AVMEDIA_TYPE_AUDIO: if (ts->flags & MPEGTS_FLAG_SYSTEM_B) { if (codec_id == AV_CODEC_ID_AC3) { + DVBAC3Descriptor *dvb_ac3_desc = ts_st->dvb_ac3_desc; + *q++=0x6a; // AC3 descriptor see A038 DVB SI - *q++=1; // 1 byte, all flags sets to 0 - *q++=0; // omit all fields... + if (dvb_ac3_desc) { + int len = 1 + + !!(dvb_ac3_desc->component_type_flag) + + !!(dvb_ac3_desc->bsid_flag) + + !!(dvb_ac3_desc->mainid_flag) + + !!(dvb_ac3_desc->asvc_flag); + + *q++ = len; + *q++ = dvb_ac3_desc->component_type_flag << 7 | dvb_ac3_desc->bsid_flag << 6 | + dvb_ac3_desc->mainid_flag << 5 | dvb_ac3_desc->asvc_flag << 4; + + if (dvb_ac3_desc->component_type_flag) *q++ = dvb_ac3_desc->component_type; + if (dvb_ac3_desc->bsid_flag) *q++ = dvb_ac3_desc->bsid; + if (dvb_ac3_desc->mainid_flag) *q++ = dvb_ac3_desc->mainid; + if (dvb_ac3_desc->asvc_flag) *q++ = dvb_ac3_desc->asvc; + } else { + *q++=1; // 1 byte, all flags sets to 0 + *q++=0; // omit all fields... + } } else if (codec_id == AV_CODEC_ID_EAC3) { *q++=0x7a; // EAC3 descriptor see A038 DVB SI *q++=1; // 1 byte, all flags sets to 0 @@ -1843,6 +1865,63 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) * need to count the samples of that too! */ av_log(s, AV_LOG_WARNING, "Got MPEG-TS formatted Opus data, unhandled"); } + } else if (st->codecpar->codec_id == AV_CODEC_ID_AC3 && !ts_st->dvb_ac3_desc) { + AC3HeaderInfo *hdr = NULL; + + if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) >= 0) { + uint8_t number_of_channels_flag; + uint8_t service_type_flag; + uint8_t full_service_flag = 1; + DVBAC3Descriptor *dvb_ac3_desc; + + dvb_ac3_desc = av_mallocz(sizeof(*dvb_ac3_desc)); + if (!dvb_ac3_desc) { + av_free(hdr); + return AVERROR(ENOMEM); + } + + service_type_flag = hdr->bitstream_mode; + switch (hdr->channel_mode) { + case AC3_CHMODE_DUALMONO: + number_of_channels_flag = 1; + break; + case AC3_CHMODE_MONO: + number_of_channels_flag = 0; + break; + case AC3_CHMODE_STEREO: + if (hdr->dolby_surround_mode == AC3_DSURMOD_ON) + number_of_channels_flag = 3; + else + number_of_channels_flag = 2; + break; + case AC3_CHMODE_3F: + case AC3_CHMODE_2F1R: + case AC3_CHMODE_3F1R: + case AC3_CHMODE_2F2R: + case AC3_CHMODE_3F2R: + number_of_channels_flag = 4; + break; + default: /* reserved */ + number_of_channels_flag = 7; + break; + } + + if (service_type_flag == 1 || service_type_flag == 4 || + (service_type_flag == 7 && !number_of_channels_flag)) + full_service_flag = 0; + + dvb_ac3_desc->component_type_flag = 1; + dvb_ac3_desc->component_type = (full_service_flag << 6) | + ((service_type_flag & 0x7) << 3) | + (number_of_channels_flag & 0x7); + dvb_ac3_desc->bsid_flag = 1; + dvb_ac3_desc->bsid = hdr->bitstream_id; + dvb_ac3_desc->mainid_flag = 0; + dvb_ac3_desc->asvc_flag = 0; + + ts_st->dvb_ac3_desc = dvb_ac3_desc; + } + av_free(hdr); } if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size || @@ -1934,6 +2013,7 @@ static void mpegts_deinit(AVFormatContext *s) AVStream *st = s->streams[i]; MpegTSWriteStream *ts_st = st->priv_data; if (ts_st) { + av_freep(&ts_st->dvb_ac3_desc); av_freep(&ts_st->payload); if (ts_st->amux) { avformat_free_context(ts_st->amux);