diff mbox

[FFmpeg-devel] avformat/mpegenc - fix PCM 16BE muxing and disallow, unsupported audio codecs

Message ID c9ad251a-4a9f-2105-a86f-84e21e52d704@gmail.com
State New
Headers show

Commit Message

Gyan Jan. 26, 2018, 2 p.m. UTC
From 7c31072230e392ca8be218df4affff1f5a2c1b5d Mon Sep 17 00:00:00 2001
From: Gyan Doshi <gyandoshi@gmail.com>
Date: Fri, 26 Jan 2018 19:15:28 +0530
Subject: [PATCH] avformat/mpegenc - fix PCM 16BE muxing and disallow
 unsupported audio codecs

PCM_S16BE streams in MPEG-PS are recognized as PCM_DVD by the demuxer
which prevents their proper remuxing in MPEG-1/2 PS.

In addition, the muxer only supports five audio codecs but will silently
go ahead and process any codec. Demuxing of these other streams fails.

Couple of error messages added and one error typo (VBV size) corrected.
---
 libavformat/mpegenc.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

Comments

Michael Niedermayer Jan. 28, 2018, 2:10 p.m. UTC | #1
On Fri, Jan 26, 2018 at 07:30:10PM +0530, Gyan Doshi wrote:
>  mpegenc.c |   27 +++++++++++++++++++++------
>  1 file changed, 21 insertions(+), 6 deletions(-)
> fcea723cb22ab82ae2cbac686716a824ff08aabd  0001-avformat-mpegenc-fix-PCM-16BE-muxing-and-disallow-un.patch
> From 7c31072230e392ca8be218df4affff1f5a2c1b5d Mon Sep 17 00:00:00 2001
> From: Gyan Doshi <gyandoshi@gmail.com>
> Date: Fri, 26 Jan 2018 19:15:28 +0530
> Subject: [PATCH] avformat/mpegenc - fix PCM 16BE muxing and disallow
>  unsupported audio codecs
> 
> PCM_S16BE streams in MPEG-PS are recognized as PCM_DVD by the demuxer
> which prevents their proper remuxing in MPEG-1/2 PS.
> 
> In addition, the muxer only supports five audio codecs but will silently
> go ahead and process any codec. Demuxing of these other streams fails.
> 
> Couple of error messages added and one error typo (VBV size) corrected.

This sounds like multiple independant changes and issues
please split the patch, one patch per issue

thx

[...]
diff mbox

Patch

diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index c77c3dfe41..1b20cb7282 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -353,7 +353,8 @@  static av_cold int mpeg_mux_init(AVFormatContext *ctx)
             if (!s->is_mpeg2 &&
                 (st->codecpar->codec_id == AV_CODEC_ID_AC3 ||
                  st->codecpar->codec_id == AV_CODEC_ID_DTS ||
-                 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE))
+                 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ||
+                 st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD))
                  av_log(ctx, AV_LOG_WARNING,
                         "%s in MPEG-1 system streams is not widely supported, "
                         "consider using the vob or the dvd muxer "
@@ -363,20 +364,34 @@  static av_cold int mpeg_mux_init(AVFormatContext *ctx)
                 stream->id = ac3_id++;
             } else if (st->codecpar->codec_id == AV_CODEC_ID_DTS) {
                 stream->id = dts_id++;
-            } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE) {
+            } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ||
+                       st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
                 stream->id = lpcm_id++;
                 for (j = 0; j < 4; j++) {
                     if (lpcm_freq_tab[j] == st->codecpar->sample_rate)
                         break;
                 }
-                if (j == 4)
+                if (j == 4) {
+                    int sr;
+                    av_log(ctx, AV_LOG_ERROR, "Invalid sampling rate for PCM stream.\n");
+                    av_log(ctx, AV_LOG_INFO, "Allowed sampling rates:");
+                    for (sr = 0; sr < 4; sr++)
+                        av_log(ctx, AV_LOG_INFO, " %d", lpcm_freq_tab[sr]);
+                    av_log(ctx, AV_LOG_INFO, "\n");
                     goto fail;
-                if (st->codecpar->channels > 8)
-                    return -1;
+                }
+                if (st->codecpar->channels > 8) {
+                    av_log(ctx, AV_LOG_ERROR, "At most 8 channels allowed for LPCM stream.\n");
+                    return AVERROR(EINVAL);
+                }
                 stream->lpcm_header[0] = 0x0c;
                 stream->lpcm_header[1] = (st->codecpar->channels - 1) | (j << 4);
                 stream->lpcm_header[2] = 0x80;
                 stream->lpcm_align     = st->codecpar->channels * 2;
+            } else if (st->codecpar->codec_id != AV_CODEC_ID_MP2 &&
+                       st->codecpar->codec_id != AV_CODEC_ID_MP3) {
+                       av_log(ctx, AV_LOG_ERROR, "Unsupported audio codec. Must be one of mp2, mp3, pcm_s16be, ac3 or dts.\n");
+                       return AVERROR(EINVAL);
             } else {
                 stream->id = mpa_id++;
             }
@@ -397,7 +412,7 @@  static av_cold int mpeg_mux_init(AVFormatContext *ctx)
                 stream->max_buffer_size = 6 * 1024 + props->buffer_size / 8;
             else {
                 av_log(ctx, AV_LOG_WARNING,
-                       "VBV buffer size not set, using default size of 130KB\n"
+                       "VBV buffer size not set, using default size of 230KB\n"
                        "If you want the mpeg file to be compliant to some specification\n"
                        "Like DVD, VCD or others, make sure you set the correct buffer size\n");
                 // FIXME: this is probably too small as default