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

Stefano Sabatini stefasab at gmail.com
Thu Feb 1 01:57:36 EET 2024


On date Sunday 2024-01-28 16:59:22 -0600, Marth64 wrote:
> - Add option to play only certain chapters or chapter ranges
> - Add option to do a second pass indexing for accurate chapter markers
> - Add documentation
> - Fixes issues with PCM audio
> - Support for essential track dispositions (forced, visual impaired, commentary)
> - Better support for structure variances and padding cells/frames
> - More user friendly log messages and errors
> - dvdread/dvdnav logging is relatively very verbose, map lower levels as DEBUG logs
> 
> Signed-off-by: Marth64 <marth64 at proxyid.net>
> ---
>  Changelog                 |    1 +
>  configure                 |    8 +
>  doc/demuxers.texi         |  129 ++++
>  libavformat/Makefile      |    1 +
>  libavformat/allformats.c  |    1 +
>  libavformat/dvdvideodec.c | 1409 +++++++++++++++++++++++++++++++++++++
>  6 files changed, 1549 insertions(+)
>  create mode 100644 libavformat/dvdvideodec.c
> 
> diff --git a/Changelog b/Changelog
> index c1fd66b4bd..9de157abe4 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -23,6 +23,7 @@ version <next>:
>  - ffmpeg CLI -bsf option may now be used for input as well as output
>  - ffmpeg CLI options may now be used as -/opt <path>, which is equivalent
>    to -opt <contents of file <path>>
> +- DVD-Video demuxer, powered by libdvdnav and libdvdread
>  
>  version 6.1:
>  - libaribcaption decoder
> diff --git a/configure b/configure
> index 21663000f8..d81420992a 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
> @@ -3520,6 +3524,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"
> @@ -6761,6 +6767,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/doc/demuxers.texi b/doc/demuxers.texi
> index e4c5b560a6..f7f9e6769a 100644
> --- a/doc/demuxers.texi
> +++ b/doc/demuxers.texi
> @@ -285,6 +285,135 @@ This demuxer accepts the following option:
>  
>  @end table
>  
> + at section dvdvideo
> +
> +DVD-Video demuxer, powered by libdvdnav and libdvdread.
> +
> +Can directly ingest DVD titles, specifically sequential PGCs (see below),
> +into a conversion pipeline. Menus and seeking are not supported at this time.
> +Block devices (DVD drives), ISO files, and directory structures are accepted.
> +Activate with @code{-f dvdvideo} in front of one of these inputs.
> +
> +Underlying playback is fully handled by libdvdnav, and structure parsing by libdvdread.

> +Therefore, the ffmpeg build must be configured with these GPL libraries enabled.

nit, specifically mention the @code{--enable-libdvdread} and
@code{--enable-libdvdnav} options as done in other places.

> +
> +You will need to provide either the desired "title" number or exact PGC/PG coordinates.
> +Many open-source DVD tools and players can aid in providing this information.
> +If not specified, the demuxer will default to title 1 which works for many discs.
> +However, due to the flexibility of the DVD standard, it is recommended to check manually.
> +There are many discs that are authored strangely or with invalid headers.
> +
> +If the input is a real DVD drive, please note that there are some drives which may
> +silently fail on reading bad sectors from the disc, returning random bits instead
> +which is effectively corrupt data. This is especially prominent on aging or rotting discs.
> +A second pass and integrity checks would be needed to detect the corruption.
> +This is not an ffmpeg issue.
> +
> + at subsection Background
> +
> +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 MPEG-PS streams that are stored in headerless VOB files.
> +To the end-user, these streams are known simply as "titles", but the actual
> +logical playback sequence is defined by one or more "PGCs", or Program Group Chains,
> +within the title. The sequence is in turn comprised of multiple "PGs", or Programs",
> +which are the actual video segments. The PGC structure, along with stream layout
> +and metadata, are stored in IFO files that need to be parsed.
> +
> +A typical DVD player relies on user GUI interaction via menus and an internal VM
> +to drive the direction of demuxing. Generally, the user would either navigate (via menus)
> +or automatically be redirected to the PGC of their choice. During this process and
> +the subsequent playback, the DVD player's internal VM also maintains a state and
> +executes instructions that can create jumps to different sectors during playback.
> +This is why libdvdnav is involved, as a linear read of the MPEG-PS blobs on the
> +disc (VOBs) is not enough to produce the right sequence in many cases.
> +
> +There are many other DVD structures (a long subject) that will not be discussed here.
> +NAV packets, in particular, are handled by this demuxer to build accurate timing
> +but not emitted as a stream. For a good high-level understanding, refer to:
> + at url{https://code.videolan.org/videolan/libdvdnav/-/blob/master/doc/dvd_structures}
> +
> + at subsection Options
> +
> +This demuxer accepts the following options:
> +
> + at table @option
> +
> + at item title

> +The title number to play. Must be set if @code{-pgc/pg} are not set.

the @code{-pgc/pg} is a bit cryptic, as it seems to refer to an option
(whic is missing).

> +Default is 1, which is used by many discs.
> +
> + at item chapter_start
> +The chapter, or PTT (part-of-title), number to start at. Default is 1.
> +
> + at item chapter_end
> +The chapter, or PTT (part-of-title), number to end at. Default is 0,
> +which is a special value to signal end at the last possible chapter.
> +
> + at item angle
> +The video angle number, referring to what is essentially an alternative
> +video stream that is interleaved in the VOBs. Default is 1.
> +
> + at item region
> +The region code to use for playback. Some discs may use this to default playback
> +at a particular angle in different regions. This option will not affect the region code
> +of a real DVD drive, if used as an input. Default is 0, "world".
> +
> + at end table
> +
> + at subsection Examples
> +

nit: use

@itemize
@item

to introduce each item and make it clear where each example stands

> +Open title 3 from a given DVD structure:
> + at example
> +ffmpeg -f dvdvideo -title 3 -i <path to DVD> ...
> + at end example
> +
> +Open chapters 3-6 from title 1 from a given DVD structure:
> + at example
> +ffmpeg -f dvdvideo -chapter_start 3 -chapter_end 6 -title 1 -i <path to DVD> ...
> + at end example
> +
> +Open only chapters 5 from title 1 from a given DVD structure:
> + at example
> +ffmpeg -f dvdvideo -chapter_start 5 -chapter_end 5 -title 1 -i <path to DVD> ...
> + at end example
> +
> + at subsection Advanced Options
> +
> + at table @option
> +
> + at item pgc

> +The entry PGC to start playback, in conjunction with @code{-pg}.

use style:
@option{pg}

here and below

Also I'd merge the two options sections (since there is usually a
single options section).

> +Alternative to setting @code{-title}.
> +Chapter markers not supported at this time.
> +Default is 0, automatically resolve from value of @code{-title}.
> +
> + at item pg
> +The entry PG to start playback, in conjunction with @code{-pgc}.
> +Alternative to setting @code{-title}.
> +Chapter markers not supported at this time.
> +Default is 0, automatically resolve from value of @code{-title}.
> +
> + at item preindex
> +Enable this to have accurate chapter (PTT) markers and duration measurement,
> +which requires a slow second pass read in order to index the chapter
> +timestamps from NAV packets. This is non-ideal extra work for physical DVD drives.
> +Can give imperfect results if not starting playback from chapter 1.
> +Not compatible with @code{-pgc/pg}.
> +Default is 0, false.
> +

> + at item trim
> +Trim padding cells (i.e. cells shorter than 1 second) from the beginning of the title.

Maybe add a note why/when this is needed

> +Default is 1, true.
> +
> + at item wait_for_audio
> +Wait for the first audio keyframe before emitting frames. This is necessary
> +for a uniform output from many discs which insert AC3 delay or filler frames
> +at the start of the PGC.
> +Default is 1, true.
> +
> + at end table
> +
>  @section ea
>  
>  Electronic Arts Multimedia format demuxer.
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index dcc99eeac4..f0b42188a2 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 b04b43cab3..3e905c23f8 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/dvdvideodec.c b/libavformat/dvdvideodec.c
> new file mode 100644
> index 0000000000..b9ec3acaaf
> --- /dev/null
> +++ b/libavformat/dvdvideodec.c
> @@ -0,0 +1,1409 @@
> +/*
> + * 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
> + */
> +
> +/*
> + * See doc/demuxers.texi for a high-level overview.
> + *
> + * The tactical approach is as follows:
> + * 1) Open the volume with dvdread
> + * 2) Analyze the user-requested title and PGC coordinates in the IFO structures
> + * 3) Request playback at the coordinates and chosen angle with dvdnav
> + * 5) Begin the playback (reading and demuxing) of MPEG-PS blocks
> + * 6) End playback if navigation goes backwards, to a menu, or a different PGC or angle
> + * 7) Close the dvdnav VM, and free dvdnav's IFO structures
> + */
> +
> +#include <dvdnav/dvdnav.h>
> +#include <dvdread/dvd_reader.h>
> +#include <dvdread/ifo_read.h>
> +#include <dvdread/ifo_types.h>
> +#include <dvdread/nav_read.h>
> +
> +#include "libavcodec/avcodec.h"
> +#include "libavutil/avstring.h"
> +#include "libavutil/avutil.h"
> +#include "libavutil/intreadwrite.h"
> +#include "libavutil/mem.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/samplefmt.h"
> +#include "libavutil/time.h"
> +#include "libavutil/timestamp.h"
> +
> +#include "avformat.h"
> +#include "avio_internal.h"
> +#include "avlanguage.h"
> +#include "demux.h"
> +#include "internal.h"
> +#include "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                          64 /* VOBUs use 32 (PES allows 33) */
> +#define DVDVIDEO_LIBDVDX_LOG_BUFFER_SIZE                1024
> +
> +enum DVDVideoSubpictureViewport {
> +    DVDVIDEO_SUBP_VIEWPORT_FULLSCREEN,
> +    DVDVIDEO_SUBP_VIEWPORT_WIDESCREEN,
> +    DVDVIDEO_SUBP_VIEWPORT_LETTERBOX,
> +    DVDVIDEO_SUBP_VIEWPORT_PANSCAN
> +};
> +const char* VIEWPORT_LABELS[4] = { "Fullscreen", "Widescreen", "Letterbox", "Pan and Scan" };
> +
> +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;
> +    int disposition;
> +    char *lang_iso;
> +} DVDVideoPGCAudioStreamEntry;
> +
> +typedef struct DVDVideoPGCSubtitleStreamEntry {
> +    int startcode;
> +    int disposition;
> +    char *lang_iso;
> +    enum DVDVideoSubpictureViewport viewport;
> +} DVDVideoPGCSubtitleStreamEntry;
> +
> +typedef struct DVDVideoPlaybackState {
> +    int                         celln;              /* ID of the active cell */
> +    int                         entry_pgn;          /* ID of the PG we are starting in */
> +    int                         in_pgc;             /* if our navigator is in the PGC */
> +    int                         in_ps;              /* if our navigator is in the program stream */
> +    int                         in_vts;             /* if our navigator is in the VTS */
> +    int64_t                     nav_pts;            /* PTS according to IFO, not frame-accurate */
> +    uint64_t                    pgc_duration_est;   /* estimated duration as reported by IFO */
> +    uint64_t                    pgc_elapsed;        /* the elapsed time of the PGC, cell-relative */
> +    int                         pgc_nb_pg_est;      /* number of PGs as reported by IFOs */
> +    int                         pgcn;               /* ID of the PGC we are playing */
> +    int                         pgn;                /* ID of the PG we are in now */
> +    int                         ptt;                /* ID of the chapter we are in now */
> +    int64_t                     ts_offset;          /* PTS discontinuity offset (ex. VOB change) */
> +    uint32_t                    vobu_duration;      /* duration of the current VOBU */
> +    uint32_t                    vobu_e_ptm;         /* end PTS of the current VOBU */
> +    int                         vtsn;               /* ID of the active VTS (video title set) */
> +    uint64_t                    *pgc_pg_times_est;  /* PG start times as reported by IFO */
> +    pgc_t                       *pgc;               /* handle to the active PGC */
> +    dvdnav_t                    *dvdnav;            /* handle to libdvdnav */
> +} DVDVideoPlaybackState;
> +
> +typedef struct DVDVideoDemuxContext {
> +    const AVClass               *class;
> +
> +    /* options */
> +    int                         opt_title;          /* the user-provided title number (1-indexed) */
> +    int                         opt_chapter_start;  /* the user-provided entry PTT (1-indexed) */
> +    int                         opt_chapter_end;    /* the user-provided exit PTT (0 for last) */
> +    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 */
> +    int                         opt_preindex;       /* pre-indexing mode (2-pass read) */
> +    int                         opt_trim;           /* trim padding cells at beginning and end */
> +    int                         opt_wait_for_audio; /* wait for audio streams to start, if any */
> +
> +    /* 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) */
> +
> +    /* playback control */
> +    int                         play_end;           /* signal EOF to the parent demuxer */
> +    DVDVideoPlaybackState       play_state;         /* the active playback state */
> +    int                         play_started;       /* signal that playback has started */
> +    int                         play_seg_started;   /* signal that segment has started */
> +    int64_t                     play_pts_offset;    /* the PTS offset of the PGC */
> +    int                         play_has_audio;     /* if we are expecting audio streams */
> +    uint64_t                    duration_ptm;       /* total duration in DVD MPEG timebase */
> +} DVDVideoDemuxContext;
> +
> +static void dvdvideo_libdvdread_log(void *opaque, dvd_logger_level_t level,
> +                                    const char *msg, va_list msg_va)
> +{
> +    AVFormatContext *s = opaque;
> +    int lavu_level;
> +
> +    char msg_buf[DVDVIDEO_LIBDVDX_LOG_BUFFER_SIZE] = {0};
> +    vsnprintf(msg_buf, DVDVIDEO_LIBDVDX_LOG_BUFFER_SIZE, msg, msg_va);
> +
> +    switch (level) {
> +        case DVD_LOGGER_LEVEL_ERROR:
> +            lavu_level = AV_LOG_ERROR;
> +            break;
> +        case DVD_LOGGER_LEVEL_WARN:
> +            lavu_level = AV_LOG_WARNING;
> +            break;
> +        /* dvdread's info messages are relatively very verbose, muffle them as debug */
> +        case DVD_LOGGER_LEVEL_INFO:
> +        case DVD_LOGGER_LEVEL_DEBUG:
> +        default:
> +            lavu_level = AV_LOG_DEBUG;
> +    }
> +
> +    av_log(s, lavu_level, "libdvdread: %s\n", msg_buf);
> +}
> +
> +static void dvdvideo_libdvdnav_log(void *opaque, dvdnav_logger_level_t level,
> +                                   const char *msg, va_list msg_va)
> +{
> +    AVFormatContext *s = opaque;
> +    int lavu_level;
> +
> +    char msg_buf[DVDVIDEO_LIBDVDX_LOG_BUFFER_SIZE] = {0};
> +    vsnprintf(msg_buf, DVDVIDEO_LIBDVDX_LOG_BUFFER_SIZE, msg, msg_va);
> +
> +    switch (level) {
> +        case DVDNAV_LOGGER_LEVEL_ERROR:
> +            lavu_level = AV_LOG_ERROR;
> +            break;
> +        case DVDNAV_LOGGER_LEVEL_WARN:
> +            /* some discs have invalid language codes set for their menus, muffle the noise */
> +            lavu_level = av_strstart(msg, "Language", NULL) ? AV_LOG_DEBUG : AV_LOG_WARNING;
> +            break;
> +        /* dvdnav's info messages are relatively very verbose, muffle them as debug */
> +        case DVDNAV_LOGGER_LEVEL_INFO:
> +        case DVDNAV_LOGGER_LEVEL_DEBUG:
> +        default:
> +            lavu_level = AV_LOG_DEBUG;
> +    }
> +
> +    av_log(s, lavu_level, "libdvdnav: %s\n", msg_buf);
> +}
> +
> +static void dvdvideo_ifo_close(AVFormatContext *s)
> +{
> +    DVDVideoDemuxContext *c = s->priv_data;
> +
> +    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_ifo_open(AVFormatContext *s)
> +{
> +    DVDVideoDemuxContext *c = s->priv_data;
> +
> +    dvd_logger_cb dvdread_log_cb;
> +    title_info_t title_info;
> +
> +    dvdread_log_cb = (dvd_logger_cb) { .pf_log = dvdvideo_libdvdread_log };
> +    c->dvdread = DVDOpen2(s, &dvdread_log_cb, s->url);
> +
> +    if (!c->dvdread) {
> +        av_log(s, AV_LOG_ERROR, "Unable to open the DVD-Video structure\n");
> +
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    if (!(c->vmg_ifo = ifoOpen(c->dvdread, 0))) {
> +        av_log(s, AV_LOG_ERROR, "Unable to open the VMG (VIDEO_TS.IFO)\n");
> +
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    if (c->opt_title > c->vmg_ifo->tt_srpt->nr_of_srpts) {
> +        av_log(s, AV_LOG_ERROR, "Title %d not found\n", c->opt_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 %d not found\n", c->opt_angle);
> +
> +        return AVERROR_STREAM_NOT_FOUND;
> +    }
> +
> +    if (title_info.nr_of_ptts < 1) {
> +        av_log(s, AV_LOG_ERROR, "Title %d has invalid headers in VMG\n", c->opt_title);
> +
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    if (c->opt_chapter_start > title_info.nr_of_ptts
> +            || (c->opt_chapter_end > 0 && c->opt_chapter_end > title_info.nr_of_ptts)) {

> +        av_log(s, AV_LOG_ERROR, "Chapter (PTT) range provided is invalid\n");

you might also print the provided range and the title_info to aid
debugging

> +
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    if (!(c->vts_ifo = ifoOpen(c->dvdread, title_info.title_set_nr))) {
> +        av_log(s, AV_LOG_ERROR, "Unable to process the VTS IFO structure\n");
> +
> +        return AVERROR_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) {
> +        av_log(s, AV_LOG_ERROR, "Title %d has invalid headers in VTS\n", c->opt_title);
> +
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    return 0;
> +}
> +
> +static void dvdvideo_play_close(AVFormatContext *s, DVDVideoPlaybackState *state)
> +{
> +    if (!state->dvdnav)
> +        return;
> +
> +    /* not allocated by av_malloc() */
> +    if (state->pgc_pg_times_est)
> +        free(state->pgc_pg_times_est);
> +
> +    if (dvdnav_close(state->dvdnav) < 0)
> +        av_log(s, AV_LOG_ERROR, "Unable to cleanly close dvdnav\n");
> +}
> +
> +static int dvdvideo_play_open(AVFormatContext *s, DVDVideoPlaybackState *state)
> +{
> +    DVDVideoDemuxContext *c = s->priv_data;
> +
> +    int ret = 0;
> +    dvdnav_logger_cb dvdnav_log_cb;
> +    dvdnav_status_t dvdnav_open_status;
> +    int cur_title, cur_pgcn, cur_pgn;
> +    pgc_t *pgc;
> +
> +    int32_t disc_region_mask;
> +    int32_t player_region_mask;
> +
> +    dvdnav_log_cb = (dvdnav_logger_cb) { .pf_log = dvdvideo_libdvdnav_log };
> +    dvdnav_open_status = dvdnav_open2(&state->dvdnav, s, &dvdnav_log_cb, s->url);
> +
> +    if (!state->dvdnav
> +            || dvdnav_open_status != DVDNAV_STATUS_OK
> +            || dvdnav_set_readahead_flag(state->dvdnav, 0) != DVDNAV_STATUS_OK
> +            || dvdnav_set_PGC_positioning_flag(state->dvdnav, 1) != DVDNAV_STATUS_OK
> +            || dvdnav_get_region_mask(state->dvdnav, &disc_region_mask) != DVDNAV_STATUS_OK) {
> +        av_log(s, AV_LOG_ERROR, "Unable to open the DVD for playback\n");
> +
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    player_region_mask = c->opt_region > 0 ? (1 << (c->opt_region - 1)) : disc_region_mask;
> +    if (dvdnav_set_region_mask(state->dvdnav, player_region_mask) != DVDNAV_STATUS_OK) {
> +        av_log(s, AV_LOG_ERROR, "Unable to set the playback region\n");
> +
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    if (c->opt_pgc > 0 && c->opt_pg > 0) {
> +        if (dvdnav_program_play(state->dvdnav, c->opt_title,
> +                                c->opt_pgc, c->opt_pg) != DVDNAV_STATUS_OK) {

> +            av_log(s, AV_LOG_ERROR, "Unable to start playback at the given title or part\n");

again, you can print the opt_title and other options to aid debugging
> +
> +            return AVERROR_EXTERNAL;
> +        }
> +
> +        state->pgcn = c->opt_pgc;
> +        state->entry_pgn = c->opt_pg;
> +    } else {
> +        if (dvdnav_part_play(state->dvdnav, c->opt_title, c->opt_chapter_start) != DVDNAV_STATUS_OK
> +                || dvdnav_current_title_program(state->dvdnav, &cur_title,
> +                                                &cur_pgcn, &cur_pgn) != DVDNAV_STATUS_OK) {
> +            av_log(s, AV_LOG_ERROR, "Unable to start playback at the given PGC/PG coordinates\n");
> +
> +            return AVERROR_EXTERNAL;
> +        }
> +
> +        state->pgcn = cur_pgcn;
> +        state->entry_pgn = cur_pgn;
> +    }
> +
> +    pgc = c->vts_ifo->vts_pgcit->pgci_srp[state->pgcn - 1].pgc;
> +
> +    if (pgc->pg_playback_mode != 0) {
> +        av_log(s, AV_LOG_ERROR, "Sorry, non-sequential PGCs are not supported\n");

nit: Non-sequential PGCs found which are not supported
?

> +        return AVERROR_PATCHWELCOME;
> +    }
> +
> +    if (dvdnav_angle_change(state->dvdnav, c->opt_angle) != DVDNAV_STATUS_OK) {
> +        av_log(s, AV_LOG_ERROR, "Unable to start playback at the given angle\n");

show the angle
> +
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    /* dvdnav_describe_title_chapters() performs several safety checks on the title structure */
> +    /* take advantage of this side effect to ensure a safe navigation path */
> +    state->pgc_nb_pg_est = dvdnav_describe_title_chapters(state->dvdnav, c->opt_title,
> +                                                          &state->pgc_pg_times_est,
> +                                                          &state->pgc_duration_est);
> +    if (!state->pgc_nb_pg_est) {
> +        av_log(s, AV_LOG_ERROR, "Unable to validate title structure\n");
> +
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    state->nav_pts = dvdnav_get_current_time(state->dvdnav);
> +    state->vtsn = c->vmg_ifo->tt_srpt->title[c->opt_title - 1].title_set_nr;
> +
> +    state->pgc = pgc;
> +
> +    return ret;
> +}
> +
> +static int dvdvideo_play_is_cell_promising(AVFormatContext *s, DVDVideoPlaybackState *state,
> +                                           int celln)
> +{
> +    DVDVideoDemuxContext *c = s->priv_data;
> +    dvd_time_t cell_duration;
> +
> +    if (!c->opt_trim)
> +        return 1;
> +
> +    cell_duration = state->pgc->cell_playback[celln - 1].playback_time;
> +
> +    return cell_duration.second >= 1 || cell_duration.minute >= 1 || cell_duration.hour >= 1;
> +}
> +
> +static int dvdvideo_play_next_ps_block(AVFormatContext *s, DVDVideoPlaybackState *state,
> +                                       uint8_t *buf, int buf_size,
> +                                       int *p_nav_event,
> +                                       void (*flush_cb)(AVFormatContext *s))
> +{
> +    DVDVideoDemuxContext *c = s->priv_data;
> +
> +    uint8_t nav_buf[DVDVIDEO_BLOCK_SIZE] = {0};
> +    int nav_event;
> +    int nav_len;
> +
> +    dvdnav_vts_change_event_t *e_vts;
> +    dvdnav_cell_change_event_t *e_cell;
> +    int cur_title, cur_pgcn, cur_pgn, cur_angle, cur_title_unused, cur_ptt, cur_nb_angles;
> +    int is_cell_promising = 0;
> +    pci_t *e_pci;
> +    dsi_t *e_dsi;
> +
> +    if (buf_size != DVDVIDEO_BLOCK_SIZE) {
> +        av_log(s, AV_LOG_ERROR, "Invalid buffer size\n");
> +
> +        return AVERROR(ENOMEM);
> +    }
> +
> +    for (int i = 0; i < DVDVIDEO_MAX_PS_SEARCH_BLOCKS; i++) {
> +        if (ff_check_interrupt(&s->interrupt_callback))
> +            return AVERROR_EXIT;
> +
> +        if (dvdnav_get_next_block(state->dvdnav, nav_buf, &nav_event, &nav_len) != DVDNAV_STATUS_OK) {
> +            av_log(s, AV_LOG_ERROR, "Unable to read next block of PGC\n");
> +
> +            return AVERROR_INVALIDDATA;
> +        }
> +
> +        /* some discs follow NOPs with a premature stop event */
> +        if (nav_event == DVDNAV_STOP)
> +            return AVERROR_EOF;
> +
> +        if (nav_len > DVDVIDEO_BLOCK_SIZE) {
> +            av_log(s, AV_LOG_ERROR, "Invalid block size\n");
> +
> +            return AVERROR_INVALIDDATA;
> +        }
> +
> +        if (dvdnav_current_title_info(state->dvdnav, &cur_title,
> +                                      &cur_ptt) != DVDNAV_STATUS_OK) {
> +            av_log(s, AV_LOG_ERROR, "Unable to validate title coordinates\n");
> +
> +            return AVERROR_INVALIDDATA;
> +        }
> +
> +        /* we somehow navigated to a menu */
> +        if (cur_title == 0 || !dvdnav_is_domain_vts(state->dvdnav))
> +            return AVERROR_EOF;
> +
> +        if (dvdnav_current_title_program(state->dvdnav, &cur_title_unused,
> +                                         &cur_pgcn, &cur_pgn) != DVDNAV_STATUS_OK) {
> +            av_log(s, AV_LOG_ERROR, "Unable to validate PGC coordinates\n");
> +
> +            return AVERROR_INVALIDDATA;
> +        }
> +
> +        /* we somehow left the PGC */
> +        if (state->in_pgc && cur_pgcn != state->pgcn)
> +            return AVERROR_EOF;
> +
> +        if (dvdnav_get_angle_info(state->dvdnav, &cur_angle, &cur_nb_angles) != DVDNAV_STATUS_OK) {
> +            av_log(s, AV_LOG_ERROR, "Unable to validate angle coordinates\n");
> +
> +            return AVERROR_INVALIDDATA;
> +        }
> +

> +        av_log(s, nav_event == DVDNAV_BLOCK_OK ? AV_LOG_TRACE : AV_LOG_DEBUG,
> +                                "new block: i=%d nav_event=%d nav_len=%d cur_title=%d "
> +                                "cur_ptt=%d cur_angle=%d cur_celln=%d cur_pgcn=%d cur_pgn=%d "
> +                                "play_in_vts=%d play_in_pgc=%d play_in_ps=%d\n",
> +                                i, nav_event, nav_len, cur_title,
> +                                cur_ptt, cur_angle, state->celln, cur_pgcn, cur_pgn,
> +                                state->in_vts, state->in_pgc, state->in_ps);

nit: weird indent

[will continue review from here]

> +static const AVOption dvdvideo_options[] = {
> +    {"title",           "title number",                                             OFFSET(opt_title),          AV_OPT_TYPE_INT,    { .i64=0 },     0,          99,        AV_OPT_FLAG_DECODING_PARAM },
> +    {"chapter_start",   "entry chapter (PTT) number",                               OFFSET(opt_chapter_start),  AV_OPT_TYPE_INT,    { .i64=1 },     1,          99,        AV_OPT_FLAG_DECODING_PARAM },
> +    {"chapter_end",     "exit chapter (PTT) number",                                OFFSET(opt_chapter_end),    AV_OPT_TYPE_INT,    { .i64=0 },     0,          99,        AV_OPT_FLAG_DECODING_PARAM },
> +    {"angle",           "playback angle number",                                    OFFSET(opt_angle),          AV_OPT_TYPE_INT,    { .i64=1 },     1,          9,         AV_OPT_FLAG_DECODING_PARAM },
> +    {"region",          "playback region number (0=free)",                          OFFSET(opt_region),         AV_OPT_TYPE_INT,    { .i64=0 },     0,          8,         AV_OPT_FLAG_DECODING_PARAM },
> +    /* advanced options */
> +    {"pgc",             "entry PGC number (0=auto)",                                OFFSET(opt_pgc),            AV_OPT_TYPE_INT,    { .i64=0 },     0,          999,       AV_OPT_FLAG_DECODING_PARAM },
> +    {"pg",              "entry PG number (0=auto)",                                 OFFSET(opt_pg),             AV_OPT_TYPE_INT,    { .i64=0 },     0,          255,       AV_OPT_FLAG_DECODING_PARAM },
> +    {"preindex",        "enable for accurate chapter markers, slow (2-pass read)",  OFFSET(opt_preindex),       AV_OPT_TYPE_BOOL,   { .i64=0 },     0,          1,         AV_OPT_FLAG_DECODING_PARAM },
> +    {"trim",            "trim padding cells from start",                            OFFSET(opt_trim),           AV_OPT_TYPE_BOOL,   { .i64=1 },     0,          1,         AV_OPT_FLAG_DECODING_PARAM },
> +    {"wait_for_audio",  "wait for audio keyframe at start, if any",                 OFFSET(opt_wait_for_audio), AV_OPT_TYPE_BOOL,   { .i64=1 },     0,          1,         AV_OPT_FLAG_DECODING_PARAM },
> +    {NULL}

sort by name?


More information about the ffmpeg-devel mailing list