[FFmpeg-devel] [PATCH v3] dvdvideo: add DVD-Video demuxer, powered by libdvdnav and libdvdread

Stefano Sabatini stefasab at gmail.com
Wed Jan 24 02:17:19 EET 2024


On date Wednesday 2024-01-10 21:46:38 -0600, Marth64 wrote:
> Fixes for several authoring styles/discs.
> ffmpeg CLI may report wrong output time and show progress strangely for some discs.
> This is not reproducible prior to `fftools/ffmpeg: convert to a threaded architecture`
> But, I am looking into it.
> 
> Signed-off-by: Marth64 <marth64 at proxyid.net>
> ---
>  Changelog                 |    1 +
>  configure                 |    8 +
>  libavformat/Makefile      |    1 +
>  libavformat/allformats.c  |    1 +
>  libavformat/avlanguage.c  |   10 +-
>  libavformat/dvdvideodec.c | 1001 +++++++++++++++++++++++++++++++++++++
>  6 files changed, 1020 insertions(+), 2 deletions(-)
>  create mode 100644 libavformat/dvdvideodec.c
> 
> diff --git a/Changelog b/Changelog
> index 5b2899d05b..1b377fed2f 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -18,6 +18,7 @@ version <next>:
>  - lavu/eval: introduce randomi() function in expressions
>  - VVC decoder
>  - fsync filter
> +- DVD-Video demuxer, powered by libdvdnav and libdvdread
>  
>  version 6.1:
>  - libaribcaption decoder
> diff --git a/configure b/configure
> index e87a09ce83..1f21f4f1c2 100755
> --- a/configure
> +++ b/configure
> @@ -227,6 +227,8 @@ External library support:
>    --enable-libdavs2        enable AVS2 decoding via libdavs2 [no]
>    --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
>                             and libraw1394 [no]
> +  --enable-libdvdnav       enable libdvdnav, needed for DVD demuxing [no]
> +  --enable-libdvdread       enable libdvdread, needed for DVD demuxing [no]
>    --enable-libfdk-aac      enable AAC de/encoding via libfdk-aac [no]
>    --enable-libflite        enable flite (voice synthesis) support via libflite [no]
>    --enable-libfontconfig   enable libfontconfig, useful for drawtext filter [no]
> @@ -1806,6 +1808,8 @@ EXTERNAL_LIBRARY_GPL_LIST="
>      frei0r
>      libcdio
>      libdavs2
> +    libdvdnav
> +    libdvdread
>      librubberband
>      libvidstab
>      libx264
> @@ -3519,6 +3523,8 @@ dts_demuxer_select="dca_parser"
>  dtshd_demuxer_select="dca_parser"
>  dv_demuxer_select="dvprofile"
>  dv_muxer_select="dvprofile"
> +dvdvideo_demuxer_select="mpegps_demuxer"
> +dvdvideo_demuxer_deps="libdvdnav libdvdread"
>  dxa_demuxer_select="riffdec"
>  eac3_demuxer_select="ac3_parser"
>  evc_demuxer_select="evc_frame_merge_bsf evc_parser"
> @@ -6760,6 +6766,8 @@ enabled libdav1d          && require_pkg_config libdav1d "dav1d >= 0.5.0" "dav1d
>  enabled libdavs2          && require_pkg_config libdavs2 "davs2 >= 1.6.0" davs2.h davs2_decoder_open
>  enabled libdc1394         && require_pkg_config libdc1394 libdc1394-2 dc1394/dc1394.h dc1394_new
>  enabled libdrm            && check_pkg_config libdrm libdrm xf86drm.h drmGetVersion
> +enabled libdvdnav         && require_pkg_config libdvdnav "dvdnav >= 6.1.1" dvdnav/dvdnav.h dvdnav_open2
> +enabled libdvdread        && require_pkg_config libdvdread "dvdread >= 6.1.2" dvdread/dvd_reader.h DVDOpen2 -ldvdread
>  enabled libfdk_aac        && { check_pkg_config libfdk_aac fdk-aac "fdk-aac/aacenc_lib.h" aacEncOpen ||
>                                 { require libfdk_aac fdk-aac/aacenc_lib.h aacEncOpen -lfdk-aac &&
>                                   warn "using libfdk without pkg-config"; } }
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 581e378d95..3c1cb21fe2 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -192,6 +192,7 @@ OBJS-$(CONFIG_DTS_MUXER)                 += rawenc.o
>  OBJS-$(CONFIG_DV_MUXER)                  += dvenc.o
>  OBJS-$(CONFIG_DVBSUB_DEMUXER)            += dvbsub.o rawdec.o
>  OBJS-$(CONFIG_DVBTXT_DEMUXER)            += dvbtxt.o rawdec.o
> +OBJS-$(CONFIG_DVDVIDEO_DEMUXER)          += dvdvideodec.o
>  OBJS-$(CONFIG_DXA_DEMUXER)               += dxa.o
>  OBJS-$(CONFIG_EA_CDATA_DEMUXER)          += eacdata.o
>  OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index ce6be5f04d..ea88d4c094 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -150,6 +150,7 @@ extern const AVInputFormat  ff_dv_demuxer;
>  extern const FFOutputFormat ff_dv_muxer;
>  extern const AVInputFormat  ff_dvbsub_demuxer;
>  extern const AVInputFormat  ff_dvbtxt_demuxer;
> +extern const AVInputFormat  ff_dvdvideo_demuxer;
>  extern const AVInputFormat  ff_dxa_demuxer;
>  extern const AVInputFormat  ff_ea_demuxer;
>  extern const AVInputFormat  ff_ea_cdata_demuxer;
> diff --git a/libavformat/avlanguage.c b/libavformat/avlanguage.c
> index 782a58adb2..202d9aa835 100644

> --- a/libavformat/avlanguage.c
> +++ b/libavformat/avlanguage.c
> @@ -29,7 +29,7 @@ typedef struct LangEntry {
>      uint16_t next_equivalent;
>  } LangEntry;
>  
> -static const uint16_t lang_table_counts[] = { 484, 20, 184 };
> +static const uint16_t lang_table_counts[] = { 484, 20, 190 };
>  static const uint16_t lang_table_offsets[] = { 0, 484, 504 };
>  
>  static const LangEntry lang_table[] = {
> @@ -539,7 +539,7 @@ static const LangEntry lang_table[] = {
>      /*0501*/ { "slk",  647 },
>      /*0502*/ { "sqi",  652 },
>      /*0503*/ { "zho",  686 },
> -    /*----- AV_LANG_ISO639_1 entries (184) -----*/
> +    /*----- AV_LANG_ISO639_1 entries (190) -----*/
>      /*0504*/ { "aa" ,    0 },
>      /*0505*/ { "ab" ,    1 },
>      /*0506*/ { "ae" ,   33 },
> @@ -724,6 +724,12 @@ static const LangEntry lang_table[] = {
>      /*0685*/ { "za" ,  478 },
>      /*0686*/ { "zh" ,   78 },
>      /*0687*/ { "zu" ,  480 },
> +    /*0688*/ { "in" ,  195 }, /* deprecated */
> +    /*0689*/ { "iw" ,  172 }, /* deprecated */
> +    /*0690*/ { "ji" ,  472 }, /* deprecated */
> +    /*0691*/ { "jw" ,  202 }, /* deprecated */
> +    /*0692*/ { "mo" ,  358 }, /* deprecated */
> +    /*0693*/ { "sh" ,  693 }, /* deprecated (no equivalent) */
>      { "", 0 }
>  };

unrelated? might be committed as a separate patch
>  
> diff --git a/libavformat/dvdvideodec.c b/libavformat/dvdvideodec.c
> new file mode 100644
> index 0000000000..856951a8f0
> --- /dev/null
> +++ b/libavformat/dvdvideodec.c
> @@ -0,0 +1,1001 @@
> +/*
> + * DVD-Video demuxer, powered by libdvdnav and libdvdread
> + * Author: Marth64 <marth64 at proxyid.net>
> + *
> + * 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
> + */
> +

> +/**
> + * DVD-Video is not a directly accessible, linear container format in the
> + * traditional sense. Instead, it allows for complex and programmatic
> + * playback of carefully muxed streams. A typical DVD player relies on
> + * user GUI interaction to drive the direction of the demuxing.

> + * Ultimately, the logical playback sequence is defined by a title's PGC

PGC?

> + * and a user selected "angle". An additional layer of control is defined by
> + * NAV packets in the MPEG-PS, but as these are processed by libdvdnav,
> + * they are witheld from the output of this demuxer.
> + *
> + * Therefore, the high-level approach is as follows:
> + * 1) Open the volume with libdvdread
> + * 2) Gather information about the user-requested title and PGC coordinates
> + * 3) Request playback at the coordinates and chosen angle with libdvdnav
> + * 4) Seek playback to first cell at the coordinates (skipping stills, etc.)
> + * 5) Begin the playback (reading and demuxing) of MPEG-PS blocks

> + * 6) End playback if the PGC or angle change, or nav leads to a menu or backwards

typo: angle changes
> + * 7) Close resources
> + **/

some of this content migth be used to provide documentation in
doc/demuxers.texi.

> +
> +#include <dvdread/dvd_reader.h>
> +#include <dvdread/ifo_read.h>
> +#include <dvdread/ifo_types.h>
> +#include <dvdread/nav_read.h>
> +#include <dvdnav/dvdnav.h>
> +
> +#include "libavutil/avutil.h"
> +#include "libavutil/intreadwrite.h"
> +#include "libavutil/mem.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/samplefmt.h"
> +#include "libavutil/timestamp.h"
> +
> +#include "libavcodec/avcodec.h"
> +#include "libavformat/avio_internal.h"
> +#include "libavformat/avlanguage.h"
> +#include "libavformat/avformat.h"
> +#include "libavformat/demux.h"
> +#include "libavformat/internal.h"
> +#include "libavformat/url.h"
> +
> +#define DVDVIDEO_MAX_PS_SEARCH_BLOCKS                   128
> +#define DVDVIDEO_BLOCK_SIZE                             2048
> +#define DVDVIDEO_TIME_BASE_Q                            (AVRational) { 1, 90000 }
> +#define DVDVIDEO_PTS_WRAP_BITS                          32 /* DVD uses 32 (PES allows 33) */
> +
> +#define DVDVIDEO_SUBP_CLUT_LEN                          16
> +#define DVDVIDEO_SUBP_CLUT_SIZE                         DVDVIDEO_SUBP_CLUT_LEN * sizeof(uint32_t)
> +
> +typedef struct DVDVideoVTSVideoStreamEntry {
> +    int startcode;
> +    enum AVCodecID codec_id;
> +    int width;
> +    int height;
> +    AVRational dar;
> +    AVRational framerate;
> +    int has_cc;
> +} DVDVideoVTSVideoStreamEntry;
> +
> +typedef struct DVDVideoPGCAudioStreamEntry {
> +    int startcode;
> +    enum AVCodecID codec_id;
> +    int sample_fmt;
> +    int sample_rate;
> +    int bit_depth;
> +    int nb_channels;
> +    AVChannelLayout ch_layout;
> +    char *lang_iso;
> +} DVDVideoPGCAudioStreamEntry;
> +
> +typedef struct DVDVideoPGCSubtitleStreamEntry {
> +    int startcode;
> +    uint32_t *clut;
> +    char *lang_iso;
> +} DVDVideoPGCSubtitleStreamEntry;
> +
> +typedef struct DVDVideoDemuxContext {
> +    const AVClass               *class;
> +
> +    /* options */
> +    int                         opt_title;          /* the user-provided title number (1-indexed) */
> +    int                         opt_ptt;            /* the user-provided PTT number (1-indexed) */
> +    int                         opt_pgc;            /* the user-provided PGC number (1-indexed) */
> +    int                         opt_pg;             /* the user-provided PG number (1-indexed) */
> +    int                         opt_angle;          /* the user-provided angle number (1-indexed) */
> +    int                         opt_region;         /* the user-provided region digit */
> +
> +    /* subdemux */
> +    const AVInputFormat         *mpeg_fmt;          /* inner MPEG-PS (VOB) demuxer */
> +    AVFormatContext             *mpeg_ctx;          /* context for inner demuxer */
> +    uint8_t                     *mpeg_buf;          /* buffer for inner demuxer */
> +    FFIOContext                 mpeg_pb;            /* buffer context for inner demuxer */
> +
> +    /* volume */
> +    dvd_reader_t                *dvdread;           /* handle to libdvdread */
> +    ifo_handle_t                *vmg_ifo;           /* handle to the VMG (VIDEO_TS.IFO) */
> +    ifo_handle_t                *vts_ifo;           /* handle to the active VTS (VTS_nn_n.IFO) */
> +    dvdnav_t                    *dvdnav;            /* handle to libdvdnav */
> +
> +    /* playback control */
> +    pgc_t                       *play_pgc;          /* handle to the active PGC */
> +    int64_t                     play_ts_offset;     /* PTS discontinuity offset (e.g. VOB change) */
> +    int64_t                     play_vobu_e_ptm;    /* end PTS of the current VOBU */
> +    int                         play_vtsn;          /* number of the active VTS (video title set) */
> +    int                         play_celln;         /* number of the active cell */
> +    int                         play_pgn;           /* number of the active program */
> +    int                         play_ptt;           /* number of the active PTT (chapter) */
> +    int                         play_in_vts;        /* if our play state is in the VTS */
> +    int                         play_in_pgc;        /* if our play state is in the PGC */
> +    int                         play_in_ps;         /* if our play state is in the program stream */
> +    int                         play_skip_cell;     /* if this cell is being skipped*/
> +    int                         play_skip_cell_last;/* if last cell was skipped */
> +    int                         play_end;           /* signal to the parent demuxer that we are done */
> +
> +} DVDVideoDemuxContext;
> +
> +static void dvdvideo_pgc_close(AVFormatContext *s)
> +{
> +    DVDVideoDemuxContext *c = s->priv_data;
> +
> +    av_log(s, AV_LOG_TRACE, "closing DVD volume\n");
> +
> +    if (c->dvdnav)
> +        dvdnav_close(c->dvdnav);
> +    
> +    if (c->vts_ifo)
> +        ifoClose(c->vts_ifo);
> +
> +    if (c->vmg_ifo)
> +        ifoClose(c->vmg_ifo);
> +
> +    if (c->dvdread)
> +        DVDClose(c->dvdread);
> +}
> +
> +static int dvdvideo_pgc_open(AVFormatContext *s)
> +{
> +    DVDVideoDemuxContext *c = s->priv_data;
> +
> +    dvdnav_status_t dvdnav_open_status;
> +
> +    title_info_t title_info;
> +    int cur_title, cur_pgcn, cur_pgn;
> +
> +    int32_t disc_region_mask;
> +    int32_t player_region_mask;
> +
> +    c->dvdread = DVDOpen(s->url);
> +    if (!c->dvdread)
> +        goto end_fail_external;
> +
> +    if (!(c->vmg_ifo = ifoOpen(c->dvdread, 0)))
> +        goto end_fail_external;
> +
> +    if (c->opt_title > c->vmg_ifo->tt_srpt->nr_of_srpts) {

> +        av_log(s, AV_LOG_ERROR, "Title not found\n");

mention the index of the title

> +
> +        return AVERROR_STREAM_NOT_FOUND;
> +    }
> +
> +    title_info = c->vmg_ifo->tt_srpt->title[c->opt_title - 1];

> +    if (c->opt_angle > title_info.nr_of_angles) {
> +        av_log(s, AV_LOG_ERROR, "Angle not found\n");

ditto

> +
> +        return AVERROR_STREAM_NOT_FOUND;
> +    }
> +
> +    if (title_info.nr_of_ptts < 1) {

> +        av_log(s, AV_LOG_ERROR, "Title invalid\n");

provide more context here to aid debugging

> +
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    if (!(c->vts_ifo = ifoOpen(c->dvdread, title_info.title_set_nr)))
> +        goto end_fail_external;
> +

> +    if (title_info.vts_ttn < 1
> +            || title_info.vts_ttn > 99
> +            || title_info.vts_ttn > c->vts_ifo->vts_ptt_srpt->nr_of_srpts
> +            || c->vts_ifo->vtsi_mat->nr_of_vts_audio_streams > 8
> +            || c->vts_ifo->vtsi_mat->nr_of_vts_subp_streams > 32) {

nit+: style, align clauses to title_info, move || in the previous line


> +        av_log(s, AV_LOG_ERROR, "Title invalid in VTS\n");

provide more context about what is wrong here

> +
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    dvdnav_open_status = dvdnav_open(&c->dvdnav, s->url);
> +    if (!c->dvdnav)
> +        goto end_fail_external;
> +
> +    if (dvdnav_open_status != DVDNAV_STATUS_OK

> +            || dvdnav_set_readahead_flag(c->dvdnav, 0) != DVDNAV_STATUS_OK
> +            || dvdnav_set_PGC_positioning_flag(c->dvdnav, 1) != DVDNAV_STATUS_OK
> +            || dvdnav_get_region_mask(c->dvdnav, &disc_region_mask) != DVDNAV_STATUS_OK)

ditto here and below

> +        goto end_fail_external;
> +
> +    player_region_mask = c->opt_region > 0 ? (1 << (c->opt_region - 1)) : disc_region_mask;
> +    if (dvdnav_set_region_mask(c->dvdnav, player_region_mask) != DVDNAV_STATUS_OK)
> +        goto end_fail_external;
> +
> +    if (c->opt_pgc > 0 && c->opt_pg > 0) {

> +        if (dvdnav_program_play(c->dvdnav, c->opt_title, c->opt_pgc, c->opt_pg) != DVDNAV_STATUS_OK)
> +            goto end_fail_external;
> +    } else {
> +        if (dvdnav_part_play(c->dvdnav, c->opt_title, c->opt_ptt) != DVDNAV_STATUS_OK
> +                || dvdnav_current_title_program(c->dvdnav, &cur_title, &cur_pgcn, &cur_pgn) != DVDNAV_STATUS_OK)
> +            goto end_fail_external;

please provide more debugging info here

[...]

Thanks.


More information about the ffmpeg-devel mailing list